Initial public release
This commit is contained in:
+365
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
* arm/adler32_impl.h - ARM implementations of Adler-32 checksum algorithm
|
||||
*
|
||||
* Copyright 2016 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIB_ARM_ADLER32_IMPL_H
|
||||
#define LIB_ARM_ADLER32_IMPL_H
|
||||
|
||||
#include "cpu_features.h"
|
||||
|
||||
/* Regular NEON implementation */
|
||||
#if HAVE_NEON_INTRIN && CPU_IS_LITTLE_ENDIAN()
|
||||
# define adler32_arm_neon adler32_arm_neon
|
||||
# if HAVE_NEON_NATIVE
|
||||
/*
|
||||
* Use no attributes if none are needed, to support old versions of clang
|
||||
* that don't accept the simd target attribute.
|
||||
*/
|
||||
# define ATTRIBUTES
|
||||
# elif defined(ARCH_ARM32)
|
||||
# define ATTRIBUTES _target_attribute("fpu=neon")
|
||||
# elif defined(__clang__)
|
||||
# define ATTRIBUTES _target_attribute("simd")
|
||||
# else
|
||||
# define ATTRIBUTES _target_attribute("+simd")
|
||||
# endif
|
||||
static ATTRIBUTES MAYBE_UNUSED u32
|
||||
adler32_arm_neon(u32 adler, const u8 *p, size_t len)
|
||||
{
|
||||
static const u16 _aligned_attribute(16) mults[64] = {
|
||||
64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
|
||||
48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
|
||||
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
|
||||
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
|
||||
};
|
||||
const uint16x8_t mults_a = vld1q_u16(&mults[0]);
|
||||
const uint16x8_t mults_b = vld1q_u16(&mults[8]);
|
||||
const uint16x8_t mults_c = vld1q_u16(&mults[16]);
|
||||
const uint16x8_t mults_d = vld1q_u16(&mults[24]);
|
||||
const uint16x8_t mults_e = vld1q_u16(&mults[32]);
|
||||
const uint16x8_t mults_f = vld1q_u16(&mults[40]);
|
||||
const uint16x8_t mults_g = vld1q_u16(&mults[48]);
|
||||
const uint16x8_t mults_h = vld1q_u16(&mults[56]);
|
||||
u32 s1 = adler & 0xFFFF;
|
||||
u32 s2 = adler >> 16;
|
||||
|
||||
/*
|
||||
* If the length is large and the pointer is misaligned, align it.
|
||||
* For smaller lengths, just take the misaligned load penalty.
|
||||
*/
|
||||
if (unlikely(len > 32768 && ((uintptr_t)p & 15))) {
|
||||
do {
|
||||
s1 += *p++;
|
||||
s2 += s1;
|
||||
len--;
|
||||
} while ((uintptr_t)p & 15);
|
||||
s1 %= DIVISOR;
|
||||
s2 %= DIVISOR;
|
||||
}
|
||||
|
||||
while (len) {
|
||||
/*
|
||||
* Calculate the length of the next data chunk such that s1 and
|
||||
* s2 are guaranteed to not exceed UINT32_MAX.
|
||||
*/
|
||||
size_t n = MIN(len, MAX_CHUNK_LEN & ~63);
|
||||
|
||||
len -= n;
|
||||
|
||||
if (n >= 64) {
|
||||
uint32x4_t v_s1 = vdupq_n_u32(0);
|
||||
uint32x4_t v_s2 = vdupq_n_u32(0);
|
||||
/*
|
||||
* v_byte_sums_* contain the sum of the bytes at index i
|
||||
* across all 64-byte segments, for each index 0..63.
|
||||
*/
|
||||
uint16x8_t v_byte_sums_a = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_b = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_c = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_d = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_e = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_f = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_g = vdupq_n_u16(0);
|
||||
uint16x8_t v_byte_sums_h = vdupq_n_u16(0);
|
||||
|
||||
s2 += s1 * (n & ~63);
|
||||
|
||||
do {
|
||||
/* Load the next 64 data bytes. */
|
||||
const uint8x16_t data_a = vld1q_u8(p + 0);
|
||||
const uint8x16_t data_b = vld1q_u8(p + 16);
|
||||
const uint8x16_t data_c = vld1q_u8(p + 32);
|
||||
const uint8x16_t data_d = vld1q_u8(p + 48);
|
||||
uint16x8_t tmp;
|
||||
|
||||
/*
|
||||
* Accumulate the previous s1 counters into the
|
||||
* s2 counters. The needed multiplication by 64
|
||||
* is delayed to later.
|
||||
*/
|
||||
v_s2 = vaddq_u32(v_s2, v_s1);
|
||||
|
||||
/*
|
||||
* Add the 64 data bytes to their v_byte_sums
|
||||
* counters, while also accumulating the sums of
|
||||
* each adjacent set of 4 bytes into v_s1.
|
||||
*/
|
||||
tmp = vpaddlq_u8(data_a);
|
||||
v_byte_sums_a = vaddw_u8(v_byte_sums_a,
|
||||
vget_low_u8(data_a));
|
||||
v_byte_sums_b = vaddw_u8(v_byte_sums_b,
|
||||
vget_high_u8(data_a));
|
||||
tmp = vpadalq_u8(tmp, data_b);
|
||||
v_byte_sums_c = vaddw_u8(v_byte_sums_c,
|
||||
vget_low_u8(data_b));
|
||||
v_byte_sums_d = vaddw_u8(v_byte_sums_d,
|
||||
vget_high_u8(data_b));
|
||||
tmp = vpadalq_u8(tmp, data_c);
|
||||
v_byte_sums_e = vaddw_u8(v_byte_sums_e,
|
||||
vget_low_u8(data_c));
|
||||
v_byte_sums_f = vaddw_u8(v_byte_sums_f,
|
||||
vget_high_u8(data_c));
|
||||
tmp = vpadalq_u8(tmp, data_d);
|
||||
v_byte_sums_g = vaddw_u8(v_byte_sums_g,
|
||||
vget_low_u8(data_d));
|
||||
v_byte_sums_h = vaddw_u8(v_byte_sums_h,
|
||||
vget_high_u8(data_d));
|
||||
v_s1 = vpadalq_u16(v_s1, tmp);
|
||||
|
||||
p += 64;
|
||||
n -= 64;
|
||||
} while (n >= 64);
|
||||
|
||||
/* s2 = 64*s2 + (64*bytesum0 + 63*bytesum1 + ... + 1*bytesum63) */
|
||||
#ifdef ARCH_ARM32
|
||||
# define umlal2(a, b, c) vmlal_u16((a), vget_high_u16(b), vget_high_u16(c))
|
||||
#else
|
||||
# define umlal2 vmlal_high_u16
|
||||
#endif
|
||||
v_s2 = vqshlq_n_u32(v_s2, 6);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_a),
|
||||
vget_low_u16(mults_a));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_a, mults_a);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_b),
|
||||
vget_low_u16(mults_b));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_b, mults_b);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_c),
|
||||
vget_low_u16(mults_c));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_c, mults_c);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_d),
|
||||
vget_low_u16(mults_d));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_d, mults_d);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_e),
|
||||
vget_low_u16(mults_e));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_e, mults_e);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_f),
|
||||
vget_low_u16(mults_f));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_f, mults_f);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_g),
|
||||
vget_low_u16(mults_g));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_g, mults_g);
|
||||
v_s2 = vmlal_u16(v_s2, vget_low_u16(v_byte_sums_h),
|
||||
vget_low_u16(mults_h));
|
||||
v_s2 = umlal2(v_s2, v_byte_sums_h, mults_h);
|
||||
#undef umlal2
|
||||
|
||||
/* Horizontal sum to finish up */
|
||||
#ifdef ARCH_ARM32
|
||||
s1 += vgetq_lane_u32(v_s1, 0) + vgetq_lane_u32(v_s1, 1) +
|
||||
vgetq_lane_u32(v_s1, 2) + vgetq_lane_u32(v_s1, 3);
|
||||
s2 += vgetq_lane_u32(v_s2, 0) + vgetq_lane_u32(v_s2, 1) +
|
||||
vgetq_lane_u32(v_s2, 2) + vgetq_lane_u32(v_s2, 3);
|
||||
#else
|
||||
s1 += vaddvq_u32(v_s1);
|
||||
s2 += vaddvq_u32(v_s2);
|
||||
#endif
|
||||
}
|
||||
/*
|
||||
* Process the last 0 <= n < 64 bytes of the chunk using
|
||||
* scalar instructions and reduce s1 and s2 mod DIVISOR.
|
||||
*/
|
||||
ADLER32_CHUNK(s1, s2, p, n);
|
||||
}
|
||||
return (s2 << 16) | s1;
|
||||
}
|
||||
#undef ATTRIBUTES
|
||||
#endif /* Regular NEON implementation */
|
||||
|
||||
/* NEON+dotprod implementation */
|
||||
#if HAVE_DOTPROD_INTRIN && CPU_IS_LITTLE_ENDIAN() && \
|
||||
!defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_DOTPROD)
|
||||
# define adler32_arm_neon_dotprod adler32_arm_neon_dotprod
|
||||
# ifdef __clang__
|
||||
# define ATTRIBUTES _target_attribute("dotprod")
|
||||
/*
|
||||
* Both gcc and binutils originally considered dotprod to depend on
|
||||
* arch=armv8.2-a or later. This was fixed in gcc 13.2 by commit
|
||||
* 9aac37ab8a7b ("aarch64: Remove architecture dependencies from intrinsics")
|
||||
* and in binutils 2.41 by commit 205e4380c800 ("aarch64: Remove version
|
||||
* dependencies from features"). Unfortunately, always using arch=armv8.2-a
|
||||
* causes build errors with some compiler options because it may reduce the
|
||||
* arch rather than increase it. Therefore we try to omit the arch whenever
|
||||
* possible. If gcc is 14 or later, then both gcc and binutils are probably
|
||||
* fixed, so we omit the arch. We also omit the arch if a feature that
|
||||
* depends on armv8.2-a or later (in gcc 13.1 and earlier) is present.
|
||||
*/
|
||||
# elif GCC_PREREQ(14, 0) || defined(__ARM_FEATURE_JCVT) \
|
||||
|| defined(__ARM_FEATURE_DOTPROD)
|
||||
# define ATTRIBUTES _target_attribute("+dotprod")
|
||||
# else
|
||||
# define ATTRIBUTES _target_attribute("arch=armv8.2-a+dotprod")
|
||||
# endif
|
||||
static ATTRIBUTES u32
|
||||
adler32_arm_neon_dotprod(u32 adler, const u8 *p, size_t len)
|
||||
{
|
||||
static const u8 _aligned_attribute(16) mults[64] = {
|
||||
64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
|
||||
48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
|
||||
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
|
||||
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
|
||||
};
|
||||
const uint8x16_t mults_a = vld1q_u8(&mults[0]);
|
||||
const uint8x16_t mults_b = vld1q_u8(&mults[16]);
|
||||
const uint8x16_t mults_c = vld1q_u8(&mults[32]);
|
||||
const uint8x16_t mults_d = vld1q_u8(&mults[48]);
|
||||
const uint8x16_t ones = vdupq_n_u8(1);
|
||||
u32 s1 = adler & 0xFFFF;
|
||||
u32 s2 = adler >> 16;
|
||||
|
||||
/*
|
||||
* If the length is large and the pointer is misaligned, align it.
|
||||
* For smaller lengths, just take the misaligned load penalty.
|
||||
*/
|
||||
if (unlikely(len > 32768 && ((uintptr_t)p & 15))) {
|
||||
do {
|
||||
s1 += *p++;
|
||||
s2 += s1;
|
||||
len--;
|
||||
} while ((uintptr_t)p & 15);
|
||||
s1 %= DIVISOR;
|
||||
s2 %= DIVISOR;
|
||||
}
|
||||
|
||||
while (len) {
|
||||
/*
|
||||
* Calculate the length of the next data chunk such that s1 and
|
||||
* s2 are guaranteed to not exceed UINT32_MAX.
|
||||
*/
|
||||
size_t n = MIN(len, MAX_CHUNK_LEN & ~63);
|
||||
|
||||
len -= n;
|
||||
|
||||
if (n >= 64) {
|
||||
uint32x4_t v_s1_a = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_b = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_c = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_d = vdupq_n_u32(0);
|
||||
uint32x4_t v_s2_a = vdupq_n_u32(0);
|
||||
uint32x4_t v_s2_b = vdupq_n_u32(0);
|
||||
uint32x4_t v_s2_c = vdupq_n_u32(0);
|
||||
uint32x4_t v_s2_d = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_sums_a = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_sums_b = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_sums_c = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1_sums_d = vdupq_n_u32(0);
|
||||
uint32x4_t v_s1;
|
||||
uint32x4_t v_s2;
|
||||
uint32x4_t v_s1_sums;
|
||||
|
||||
s2 += s1 * (n & ~63);
|
||||
|
||||
do {
|
||||
uint8x16_t data_a = vld1q_u8(p + 0);
|
||||
uint8x16_t data_b = vld1q_u8(p + 16);
|
||||
uint8x16_t data_c = vld1q_u8(p + 32);
|
||||
uint8x16_t data_d = vld1q_u8(p + 48);
|
||||
|
||||
v_s1_sums_a = vaddq_u32(v_s1_sums_a, v_s1_a);
|
||||
v_s1_a = vdotq_u32(v_s1_a, data_a, ones);
|
||||
v_s2_a = vdotq_u32(v_s2_a, data_a, mults_a);
|
||||
|
||||
v_s1_sums_b = vaddq_u32(v_s1_sums_b, v_s1_b);
|
||||
v_s1_b = vdotq_u32(v_s1_b, data_b, ones);
|
||||
v_s2_b = vdotq_u32(v_s2_b, data_b, mults_b);
|
||||
|
||||
v_s1_sums_c = vaddq_u32(v_s1_sums_c, v_s1_c);
|
||||
v_s1_c = vdotq_u32(v_s1_c, data_c, ones);
|
||||
v_s2_c = vdotq_u32(v_s2_c, data_c, mults_c);
|
||||
|
||||
v_s1_sums_d = vaddq_u32(v_s1_sums_d, v_s1_d);
|
||||
v_s1_d = vdotq_u32(v_s1_d, data_d, ones);
|
||||
v_s2_d = vdotq_u32(v_s2_d, data_d, mults_d);
|
||||
|
||||
p += 64;
|
||||
n -= 64;
|
||||
} while (n >= 64);
|
||||
|
||||
v_s1 = vaddq_u32(vaddq_u32(v_s1_a, v_s1_b),
|
||||
vaddq_u32(v_s1_c, v_s1_d));
|
||||
v_s2 = vaddq_u32(vaddq_u32(v_s2_a, v_s2_b),
|
||||
vaddq_u32(v_s2_c, v_s2_d));
|
||||
v_s1_sums = vaddq_u32(vaddq_u32(v_s1_sums_a,
|
||||
v_s1_sums_b),
|
||||
vaddq_u32(v_s1_sums_c,
|
||||
v_s1_sums_d));
|
||||
v_s2 = vaddq_u32(v_s2, vqshlq_n_u32(v_s1_sums, 6));
|
||||
|
||||
s1 += vaddvq_u32(v_s1);
|
||||
s2 += vaddvq_u32(v_s2);
|
||||
}
|
||||
/*
|
||||
* Process the last 0 <= n < 64 bytes of the chunk using
|
||||
* scalar instructions and reduce s1 and s2 mod DIVISOR.
|
||||
*/
|
||||
ADLER32_CHUNK(s1, s2, p, n);
|
||||
}
|
||||
return (s2 << 16) | s1;
|
||||
}
|
||||
#undef ATTRIBUTES
|
||||
#endif /* NEON+dotprod implementation */
|
||||
|
||||
#if defined(adler32_arm_neon_dotprod) && defined(__ARM_FEATURE_DOTPROD)
|
||||
#define DEFAULT_IMPL adler32_arm_neon_dotprod
|
||||
#else
|
||||
static inline adler32_func_t
|
||||
arch_select_adler32_func(void)
|
||||
{
|
||||
const u32 features MAYBE_UNUSED = get_arm_cpu_features();
|
||||
|
||||
#ifdef adler32_arm_neon_dotprod
|
||||
if (HAVE_NEON(features) && HAVE_DOTPROD(features))
|
||||
return adler32_arm_neon_dotprod;
|
||||
#endif
|
||||
#ifdef adler32_arm_neon
|
||||
if (HAVE_NEON(features))
|
||||
return adler32_arm_neon;
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
#define arch_select_adler32_func arch_select_adler32_func
|
||||
#endif
|
||||
|
||||
#endif /* LIB_ARM_ADLER32_IMPL_H */
|
||||
+292
@@ -0,0 +1,292 @@
|
||||
/*
|
||||
* arm/cpu_features.c - feature detection for ARM CPUs
|
||||
*
|
||||
* Copyright 2018 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* ARM CPUs don't have a standard way for unprivileged programs to detect CPU
|
||||
* features. But an OS-specific way can be used when available.
|
||||
*/
|
||||
|
||||
#ifdef __APPLE__
|
||||
# undef _ANSI_SOURCE
|
||||
# undef _DARWIN_C_SOURCE
|
||||
# define _DARWIN_C_SOURCE /* for sysctlbyname() */
|
||||
#endif
|
||||
|
||||
#include "../cpu_features_common.h" /* must be included first */
|
||||
#include "cpu_features.h"
|
||||
|
||||
#ifdef ARM_CPU_FEATURES_KNOWN
|
||||
/* Runtime ARM CPU feature detection is supported. */
|
||||
|
||||
#ifdef __linux__
|
||||
/*
|
||||
* On Linux, arm32 and arm64 CPU features can be detected by reading the
|
||||
* AT_HWCAP and AT_HWCAP2 values from /proc/self/auxv.
|
||||
*
|
||||
* Ideally we'd use the C library function getauxval(), but it's not guaranteed
|
||||
* to be available: it was only added to glibc in 2.16, and in Android it was
|
||||
* added to API level 18 for arm32 and level 21 for arm64.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define AT_HWCAP 16
|
||||
#define AT_HWCAP2 26
|
||||
|
||||
static void scan_auxv(unsigned long *hwcap, unsigned long *hwcap2)
|
||||
{
|
||||
int fd;
|
||||
unsigned long auxbuf[32];
|
||||
int filled = 0;
|
||||
int i;
|
||||
|
||||
fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return;
|
||||
|
||||
for (;;) {
|
||||
do {
|
||||
int ret = read(fd, &((char *)auxbuf)[filled],
|
||||
sizeof(auxbuf) - filled);
|
||||
if (ret <= 0) {
|
||||
if (ret < 0 && errno == EINTR)
|
||||
continue;
|
||||
goto out;
|
||||
}
|
||||
filled += ret;
|
||||
} while (filled < 2 * sizeof(long));
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
unsigned long type = auxbuf[i];
|
||||
unsigned long value = auxbuf[i + 1];
|
||||
|
||||
if (type == AT_HWCAP)
|
||||
*hwcap = value;
|
||||
else if (type == AT_HWCAP2)
|
||||
*hwcap2 = value;
|
||||
i += 2;
|
||||
filled -= 2 * sizeof(long);
|
||||
} while (filled >= 2 * sizeof(long));
|
||||
|
||||
memmove(auxbuf, &auxbuf[i], filled);
|
||||
}
|
||||
out:
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static u32 query_arm_cpu_features(void)
|
||||
{
|
||||
u32 features = 0;
|
||||
unsigned long hwcap = 0;
|
||||
unsigned long hwcap2 = 0;
|
||||
|
||||
scan_auxv(&hwcap, &hwcap2);
|
||||
|
||||
#ifdef ARCH_ARM32
|
||||
STATIC_ASSERT(sizeof(long) == 4);
|
||||
if (hwcap & (1 << 12)) /* HWCAP_NEON */
|
||||
features |= ARM_CPU_FEATURE_NEON;
|
||||
#else
|
||||
STATIC_ASSERT(sizeof(long) == 8);
|
||||
if (hwcap & (1 << 1)) /* HWCAP_ASIMD */
|
||||
features |= ARM_CPU_FEATURE_NEON;
|
||||
if (hwcap & (1 << 4)) /* HWCAP_PMULL */
|
||||
features |= ARM_CPU_FEATURE_PMULL;
|
||||
if (hwcap & (1 << 7)) /* HWCAP_CRC32 */
|
||||
features |= ARM_CPU_FEATURE_CRC32;
|
||||
if (hwcap & (1 << 17)) /* HWCAP_SHA3 */
|
||||
features |= ARM_CPU_FEATURE_SHA3;
|
||||
if (hwcap & (1 << 20)) /* HWCAP_ASIMDDP */
|
||||
features |= ARM_CPU_FEATURE_DOTPROD;
|
||||
#endif
|
||||
return features;
|
||||
}
|
||||
|
||||
#ifdef ARCH_ARM64
|
||||
/*
|
||||
* Return whether cpu0's MIDR_EL1 identifies one of the Arm Neoverse
|
||||
* V-class server cores (V1 / V2 / V3 / V3AE). MIDR_EL1 is exposed
|
||||
* unprivileged via sysfs (added in Linux 4.7). Reading cpu0 only is fine
|
||||
* in practice: no Neoverse V-class server SKU has shipped as part of a
|
||||
* big.LITTLE cluster. Any failure (file missing, read error, parse
|
||||
* failure, unrecognized CPU) returns false.
|
||||
*/
|
||||
static bool arm64_cpu_is_neoverse_v_class(void)
|
||||
{
|
||||
int fd;
|
||||
char buf[32];
|
||||
ssize_t n;
|
||||
unsigned long midr;
|
||||
u32 part;
|
||||
|
||||
fd = open("/sys/devices/system/cpu/cpu0/regs/identification/midr_el1",
|
||||
O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return false;
|
||||
do {
|
||||
n = read(fd, buf, sizeof(buf) - 1);
|
||||
} while (n < 0 && errno == EINTR);
|
||||
close(fd);
|
||||
if (n <= 0)
|
||||
return false;
|
||||
buf[n] = '\0';
|
||||
midr = strtoul(buf, NULL, 0); /* sysfs prints "0x%016llx\n" */
|
||||
|
||||
/* MIDR_EL1: [31:24]=Implementer, [15:4]=PartNum. */
|
||||
if (((midr >> 24) & 0xff) != 0x41) /* Implementer must be Arm Ltd. */
|
||||
return false;
|
||||
part = (midr >> 4) & 0xfff;
|
||||
switch (part) {
|
||||
case 0xd40: /* Neoverse V1 (e.g. AWS Graviton 3) */
|
||||
case 0xd4f: /* Neoverse V2 (e.g. AWS Graviton 4) */
|
||||
case 0xd83: /* Neoverse V3AE */
|
||||
case 0xd84: /* Neoverse V3 */
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif /* ARCH_ARM64 */
|
||||
|
||||
#elif defined(__APPLE__)
|
||||
/* On Apple platforms, arm64 CPU features can be detected via sysctlbyname(). */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <TargetConditionals.h>
|
||||
|
||||
static const struct {
|
||||
const char *name;
|
||||
u32 feature;
|
||||
} feature_sysctls[] = {
|
||||
{ "hw.optional.neon", ARM_CPU_FEATURE_NEON },
|
||||
{ "hw.optional.AdvSIMD", ARM_CPU_FEATURE_NEON },
|
||||
{ "hw.optional.arm.FEAT_PMULL", ARM_CPU_FEATURE_PMULL },
|
||||
{ "hw.optional.armv8_crc32", ARM_CPU_FEATURE_CRC32 },
|
||||
{ "hw.optional.armv8_2_sha3", ARM_CPU_FEATURE_SHA3 },
|
||||
{ "hw.optional.arm.FEAT_SHA3", ARM_CPU_FEATURE_SHA3 },
|
||||
{ "hw.optional.arm.FEAT_DotProd", ARM_CPU_FEATURE_DOTPROD },
|
||||
};
|
||||
|
||||
static u32 query_arm_cpu_features(void)
|
||||
{
|
||||
u32 features = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < ARRAY_LEN(feature_sysctls); i++) {
|
||||
const char *name = feature_sysctls[i].name;
|
||||
u32 val = 0;
|
||||
size_t valsize = sizeof(val);
|
||||
|
||||
if (sysctlbyname(name, &val, &valsize, NULL, 0) == 0 &&
|
||||
valsize == sizeof(val) && val == 1)
|
||||
features |= feature_sysctls[i].feature;
|
||||
}
|
||||
return features;
|
||||
}
|
||||
#elif defined(_WIN32)
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#ifndef PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE /* added in Windows SDK 20348 */
|
||||
# define PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE 43
|
||||
#endif
|
||||
|
||||
static u32 query_arm_cpu_features(void)
|
||||
{
|
||||
u32 features = ARM_CPU_FEATURE_NEON;
|
||||
|
||||
if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
|
||||
features |= ARM_CPU_FEATURE_PMULL;
|
||||
if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
|
||||
features |= ARM_CPU_FEATURE_CRC32;
|
||||
if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
|
||||
features |= ARM_CPU_FEATURE_DOTPROD;
|
||||
|
||||
/* FIXME: detect SHA3 support too. */
|
||||
|
||||
return features;
|
||||
}
|
||||
#else
|
||||
#error "unhandled case"
|
||||
#endif
|
||||
|
||||
static const struct cpu_feature arm_cpu_feature_table[] = {
|
||||
{ARM_CPU_FEATURE_NEON, "neon"},
|
||||
{ARM_CPU_FEATURE_PMULL, "pmull"},
|
||||
{ARM_CPU_FEATURE_PREFER_PMULL, "prefer_pmull"},
|
||||
{ARM_CPU_FEATURE_CRC32, "crc32"},
|
||||
{ARM_CPU_FEATURE_SHA3, "sha3"},
|
||||
{ARM_CPU_FEATURE_DOTPROD, "dotprod"},
|
||||
};
|
||||
|
||||
/*
|
||||
* Whether to set ARM_CPU_FEATURE_PREFER_PMULL on this CPU. This is the
|
||||
* right choice on CPUs whose pmull pipes have more aggregate throughput
|
||||
* than the crc32 unit -- in measured cases by a wide margin: the Apple M
|
||||
* series sustains ~68 GB/s on pmull vs ~25 GB/s on crc32 (M1), and the Arm
|
||||
* Neoverse V class sustains ~40 GB/s vs ~22 GB/s (Graviton 4 / V2).
|
||||
*
|
||||
* We detect Apple at compile time, and Neoverse V-class cores at runtime
|
||||
* via MIDR_EL1 on Linux. Elsewhere we leave this unset, and the dispatcher
|
||||
* picks the crc32-instruction path which is the right default for most
|
||||
* other modern ARM CPUs.
|
||||
*/
|
||||
static bool arm_cpu_prefers_pmull(void)
|
||||
{
|
||||
#if defined(__APPLE__) && TARGET_OS_OSX
|
||||
return true;
|
||||
#elif defined(__linux__) && defined(ARCH_ARM64)
|
||||
if (arm64_cpu_is_neoverse_v_class())
|
||||
return true;
|
||||
#endif
|
||||
#ifdef TEST_SUPPORT__DO_NOT_USE
|
||||
return true;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
volatile u32 libdeflate_arm_cpu_features = 0;
|
||||
|
||||
void libdeflate_init_arm_cpu_features(void)
|
||||
{
|
||||
u32 features = query_arm_cpu_features();
|
||||
|
||||
if (arm_cpu_prefers_pmull())
|
||||
features |= ARM_CPU_FEATURE_PREFER_PMULL;
|
||||
|
||||
disable_cpu_features_for_testing(&features, arm_cpu_feature_table,
|
||||
ARRAY_LEN(arm_cpu_feature_table));
|
||||
|
||||
libdeflate_arm_cpu_features = features | ARM_CPU_FEATURES_KNOWN;
|
||||
}
|
||||
|
||||
#endif /* ARM_CPU_FEATURES_KNOWN */
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* arm/cpu_features.h - feature detection for ARM CPUs
|
||||
*
|
||||
* Copyright 2018 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIB_ARM_CPU_FEATURES_H
|
||||
#define LIB_ARM_CPU_FEATURES_H
|
||||
|
||||
#include "../lib_common.h"
|
||||
|
||||
#if defined(ARCH_ARM32) || defined(ARCH_ARM64)
|
||||
|
||||
#define ARM_CPU_FEATURE_NEON (1 << 0)
|
||||
#define ARM_CPU_FEATURE_PMULL (1 << 1)
|
||||
/*
|
||||
* PREFER_PMULL indicates that the CPU has very high pmull throughput, and so
|
||||
* the 12x wide pmull-based CRC-32 implementation is likely to be faster than an
|
||||
* implementation based on the crc32 instructions.
|
||||
*/
|
||||
#define ARM_CPU_FEATURE_PREFER_PMULL (1 << 2)
|
||||
#define ARM_CPU_FEATURE_CRC32 (1 << 3)
|
||||
#define ARM_CPU_FEATURE_SHA3 (1 << 4)
|
||||
#define ARM_CPU_FEATURE_DOTPROD (1 << 5)
|
||||
|
||||
#if !defined(FREESTANDING) && \
|
||||
(defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) && \
|
||||
(defined(__linux__) || \
|
||||
(defined(__APPLE__) && defined(ARCH_ARM64)) || \
|
||||
(defined(_WIN32) && defined(ARCH_ARM64)))
|
||||
/* Runtime ARM CPU feature detection is supported. */
|
||||
# define ARM_CPU_FEATURES_KNOWN (1U << 31)
|
||||
extern volatile u32 libdeflate_arm_cpu_features;
|
||||
|
||||
void libdeflate_init_arm_cpu_features(void);
|
||||
|
||||
static inline u32 get_arm_cpu_features(void)
|
||||
{
|
||||
if (libdeflate_arm_cpu_features == 0)
|
||||
libdeflate_init_arm_cpu_features();
|
||||
return libdeflate_arm_cpu_features;
|
||||
}
|
||||
#else
|
||||
static inline u32 get_arm_cpu_features(void) { return 0; }
|
||||
#endif
|
||||
|
||||
/* NEON */
|
||||
#if defined(__ARM_NEON) || (defined(_MSC_VER) && defined(ARCH_ARM64))
|
||||
# define HAVE_NEON(features) 1
|
||||
# define HAVE_NEON_NATIVE 1
|
||||
#else
|
||||
# define HAVE_NEON(features) ((features) & ARM_CPU_FEATURE_NEON)
|
||||
# define HAVE_NEON_NATIVE 0
|
||||
#endif
|
||||
/*
|
||||
* With both gcc and clang, NEON intrinsics require that the main target has
|
||||
* NEON enabled already. Exception: with gcc 6.1 and later (r230411 for arm32,
|
||||
* r226563 for arm64), hardware floating point support is sufficient.
|
||||
*/
|
||||
#if (defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)) && \
|
||||
(HAVE_NEON_NATIVE || (GCC_PREREQ(6, 1) && defined(__ARM_FP)))
|
||||
# define HAVE_NEON_INTRIN 1
|
||||
# include <arm_neon.h>
|
||||
#else
|
||||
# define HAVE_NEON_INTRIN 0
|
||||
#endif
|
||||
|
||||
/* PMULL */
|
||||
#ifdef __ARM_FEATURE_CRYPTO
|
||||
# define HAVE_PMULL(features) 1
|
||||
#else
|
||||
# define HAVE_PMULL(features) ((features) & ARM_CPU_FEATURE_PMULL)
|
||||
#endif
|
||||
#if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \
|
||||
(GCC_PREREQ(7, 1) || defined(__clang__) || defined(_MSC_VER)) && \
|
||||
CPU_IS_LITTLE_ENDIAN() /* untested on big endian */
|
||||
# define HAVE_PMULL_INTRIN 1
|
||||
/* Work around MSVC's vmull_p64() taking poly64x1_t instead of poly64_t */
|
||||
# ifdef _MSC_VER
|
||||
# define compat_vmull_p64(a, b) vmull_p64(vcreate_p64(a), vcreate_p64(b))
|
||||
# else
|
||||
# define compat_vmull_p64(a, b) vmull_p64((a), (b))
|
||||
# endif
|
||||
#else
|
||||
# define HAVE_PMULL_INTRIN 0
|
||||
#endif
|
||||
|
||||
/* CRC32 */
|
||||
#ifdef __ARM_FEATURE_CRC32
|
||||
# define HAVE_CRC32(features) 1
|
||||
#else
|
||||
# define HAVE_CRC32(features) ((features) & ARM_CPU_FEATURE_CRC32)
|
||||
#endif
|
||||
#if defined(ARCH_ARM64) && \
|
||||
(defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER))
|
||||
# define HAVE_CRC32_INTRIN 1
|
||||
# if defined(__GNUC__) || defined(__clang__)
|
||||
# include <arm_acle.h>
|
||||
# endif
|
||||
/*
|
||||
* Use an inline assembly fallback for clang 15 and earlier, which only
|
||||
* defined the crc32 intrinsics when crc32 is enabled in the main target.
|
||||
*/
|
||||
# if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \
|
||||
!defined(__ARM_FEATURE_CRC32)
|
||||
# undef __crc32b
|
||||
# define __crc32b(a, b) \
|
||||
({ uint32_t res; \
|
||||
__asm__("crc32b %w0, %w1, %w2" \
|
||||
: "=r" (res) : "r" (a), "r" (b)); \
|
||||
res; })
|
||||
# undef __crc32h
|
||||
# define __crc32h(a, b) \
|
||||
({ uint32_t res; \
|
||||
__asm__("crc32h %w0, %w1, %w2" \
|
||||
: "=r" (res) : "r" (a), "r" (b)); \
|
||||
res; })
|
||||
# undef __crc32w
|
||||
# define __crc32w(a, b) \
|
||||
({ uint32_t res; \
|
||||
__asm__("crc32w %w0, %w1, %w2" \
|
||||
: "=r" (res) : "r" (a), "r" (b)); \
|
||||
res; })
|
||||
# undef __crc32d
|
||||
# define __crc32d(a, b) \
|
||||
({ uint32_t res; \
|
||||
__asm__("crc32x %w0, %w1, %2" \
|
||||
: "=r" (res) : "r" (a), "r" (b)); \
|
||||
res; })
|
||||
# pragma clang diagnostic ignored "-Wgnu-statement-expression"
|
||||
# endif
|
||||
#else
|
||||
# define HAVE_CRC32_INTRIN 0
|
||||
#endif
|
||||
|
||||
/* SHA3 (needed for the eor3 instruction) */
|
||||
#ifdef __ARM_FEATURE_SHA3
|
||||
# define HAVE_SHA3(features) 1
|
||||
#else
|
||||
# define HAVE_SHA3(features) ((features) & ARM_CPU_FEATURE_SHA3)
|
||||
#endif
|
||||
#if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \
|
||||
(GCC_PREREQ(9, 1) /* r268049 */ || \
|
||||
CLANG_PREREQ(7, 0, 10010463) /* r338010 */)
|
||||
# define HAVE_SHA3_INTRIN 1
|
||||
/*
|
||||
* Use an inline assembly fallback for clang 15 and earlier, which only
|
||||
* defined the sha3 intrinsics when sha3 is enabled in the main target.
|
||||
*/
|
||||
# if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \
|
||||
!defined(__ARM_FEATURE_SHA3)
|
||||
# undef veor3q_u8
|
||||
# define veor3q_u8(a, b, c) \
|
||||
({ uint8x16_t res; \
|
||||
__asm__("eor3 %0.16b, %1.16b, %2.16b, %3.16b" \
|
||||
: "=w" (res) : "w" (a), "w" (b), "w" (c)); \
|
||||
res; })
|
||||
# pragma clang diagnostic ignored "-Wgnu-statement-expression"
|
||||
# endif
|
||||
#else
|
||||
# define HAVE_SHA3_INTRIN 0
|
||||
#endif
|
||||
|
||||
/* dotprod */
|
||||
#ifdef __ARM_FEATURE_DOTPROD
|
||||
# define HAVE_DOTPROD(features) 1
|
||||
#else
|
||||
# define HAVE_DOTPROD(features) ((features) & ARM_CPU_FEATURE_DOTPROD)
|
||||
#endif
|
||||
#if defined(ARCH_ARM64) && HAVE_NEON_INTRIN && \
|
||||
(GCC_PREREQ(8, 1) || CLANG_PREREQ(7, 0, 10010000) || defined(_MSC_VER))
|
||||
# define HAVE_DOTPROD_INTRIN 1
|
||||
/*
|
||||
* Use an inline assembly fallback for clang 15 and earlier, which only
|
||||
* defined the dotprod intrinsics when dotprod is enabled in the main target.
|
||||
*/
|
||||
# if defined(__clang__) && !CLANG_PREREQ(16, 0, 16000000) && \
|
||||
!defined(__ARM_FEATURE_DOTPROD)
|
||||
# undef vdotq_u32
|
||||
# define vdotq_u32(a, b, c) \
|
||||
({ uint32x4_t res = (a); \
|
||||
__asm__("udot %0.4s, %1.16b, %2.16b" \
|
||||
: "+w" (res) : "w" (b), "w" (c)); \
|
||||
res; })
|
||||
# pragma clang diagnostic ignored "-Wgnu-statement-expression"
|
||||
# endif
|
||||
#else
|
||||
# define HAVE_DOTPROD_INTRIN 0
|
||||
#endif
|
||||
|
||||
#endif /* ARCH_ARM32 || ARCH_ARM64 */
|
||||
|
||||
#endif /* LIB_ARM_CPU_FEATURES_H */
|
||||
+594
@@ -0,0 +1,594 @@
|
||||
/*
|
||||
* arm/crc32_impl.h - ARM implementations of the gzip CRC-32 algorithm
|
||||
*
|
||||
* Copyright 2022 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIB_ARM_CRC32_IMPL_H
|
||||
#define LIB_ARM_CRC32_IMPL_H
|
||||
|
||||
#include "cpu_features.h"
|
||||
|
||||
/*
|
||||
* crc32_arm_crc() - implementation using crc32 instructions (only)
|
||||
*
|
||||
* In general this implementation is straightforward. However, naive use of the
|
||||
* crc32 instructions is serial: one of the two inputs to each crc32 instruction
|
||||
* is the output of the previous one. To take advantage of CPUs that can
|
||||
* execute multiple crc32 instructions in parallel, when possible we interleave
|
||||
* the checksumming of several adjacent chunks, then combine their CRCs.
|
||||
*
|
||||
* However, without pmull, combining CRCs is fairly slow. So in this pmull-less
|
||||
* version, we only use a large chunk length, and thus we only do chunked
|
||||
* processing if there is a lot of data to checksum. This also means that a
|
||||
* variable chunk length wouldn't help much, so we just support a fixed length.
|
||||
*/
|
||||
#if HAVE_CRC32_INTRIN
|
||||
# ifdef __clang__
|
||||
# define ATTRIBUTES _target_attribute("crc")
|
||||
# else
|
||||
# define ATTRIBUTES _target_attribute("+crc")
|
||||
# endif
|
||||
|
||||
/*
|
||||
* Combine the CRCs for 4 adjacent chunks of length L = CRC32_FIXED_CHUNK_LEN
|
||||
* bytes each by computing:
|
||||
*
|
||||
* [ crc0*x^(3*8*L) + crc1*x^(2*8*L) + crc2*x^(1*8*L) + crc3 ] mod G(x)
|
||||
*
|
||||
* This has been optimized in several ways:
|
||||
*
|
||||
* - The needed multipliers (x to some power, reduced mod G(x)) were
|
||||
* precomputed.
|
||||
*
|
||||
* - The 3 multiplications are interleaved.
|
||||
*
|
||||
* - The reduction mod G(x) is delayed to the end and done using __crc32d.
|
||||
* Note that the use of __crc32d introduces an extra factor of x^32. To
|
||||
* cancel that out along with the extra factor of x^1 that gets introduced
|
||||
* because of how the 63-bit products are aligned in their 64-bit integers,
|
||||
* the multipliers are actually x^(j*8*L - 33) instead of x^(j*8*L).
|
||||
*/
|
||||
static forceinline ATTRIBUTES u32
|
||||
combine_crcs_slow(u32 crc0, u32 crc1, u32 crc2, u32 crc3)
|
||||
{
|
||||
u64 res0 = 0, res1 = 0, res2 = 0;
|
||||
int i;
|
||||
|
||||
/* Multiply crc{0,1,2} by CRC32_FIXED_CHUNK_MULT_{3,2,1}. */
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (CRC32_FIXED_CHUNK_MULT_3 & (1U << i))
|
||||
res0 ^= (u64)crc0 << i;
|
||||
if (CRC32_FIXED_CHUNK_MULT_2 & (1U << i))
|
||||
res1 ^= (u64)crc1 << i;
|
||||
if (CRC32_FIXED_CHUNK_MULT_1 & (1U << i))
|
||||
res2 ^= (u64)crc2 << i;
|
||||
}
|
||||
/* Add the different parts and reduce mod G(x). */
|
||||
return __crc32d(0, res0 ^ res1 ^ res2) ^ crc3;
|
||||
}
|
||||
|
||||
#define crc32_arm_crc crc32_arm_crc
|
||||
static ATTRIBUTES u32
|
||||
crc32_arm_crc(u32 crc, const u8 *p, size_t len)
|
||||
{
|
||||
if (len >= 64) {
|
||||
const size_t align = -(uintptr_t)p & 7;
|
||||
|
||||
/* Align p to the next 8-byte boundary. */
|
||||
if (align) {
|
||||
if (align & 1)
|
||||
crc = __crc32b(crc, *p++);
|
||||
if (align & 2) {
|
||||
crc = __crc32h(crc, le16_bswap(*(u16 *)p));
|
||||
p += 2;
|
||||
}
|
||||
if (align & 4) {
|
||||
crc = __crc32w(crc, le32_bswap(*(u32 *)p));
|
||||
p += 4;
|
||||
}
|
||||
len -= align;
|
||||
}
|
||||
/*
|
||||
* Interleave the processing of multiple adjacent data chunks to
|
||||
* take advantage of instruction-level parallelism.
|
||||
*
|
||||
* Some CPUs don't prefetch the data if it's being fetched in
|
||||
* multiple interleaved streams, so do explicit prefetching.
|
||||
*/
|
||||
while (len >= CRC32_NUM_CHUNKS * CRC32_FIXED_CHUNK_LEN) {
|
||||
const u64 *wp0 = (const u64 *)p;
|
||||
const u64 * const wp0_end =
|
||||
(const u64 *)(p + CRC32_FIXED_CHUNK_LEN);
|
||||
u32 crc1 = 0, crc2 = 0, crc3 = 0;
|
||||
|
||||
STATIC_ASSERT(CRC32_NUM_CHUNKS == 4);
|
||||
STATIC_ASSERT(CRC32_FIXED_CHUNK_LEN % (4 * 8) == 0);
|
||||
do {
|
||||
prefetchr(&wp0[64 + 0*CRC32_FIXED_CHUNK_LEN/8]);
|
||||
prefetchr(&wp0[64 + 1*CRC32_FIXED_CHUNK_LEN/8]);
|
||||
prefetchr(&wp0[64 + 2*CRC32_FIXED_CHUNK_LEN/8]);
|
||||
prefetchr(&wp0[64 + 3*CRC32_FIXED_CHUNK_LEN/8]);
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_FIXED_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
} while (wp0 != wp0_end);
|
||||
crc = combine_crcs_slow(crc, crc1, crc2, crc3);
|
||||
p += CRC32_NUM_CHUNKS * CRC32_FIXED_CHUNK_LEN;
|
||||
len -= CRC32_NUM_CHUNKS * CRC32_FIXED_CHUNK_LEN;
|
||||
}
|
||||
/*
|
||||
* Due to the large fixed chunk length used above, there might
|
||||
* still be a lot of data left. So use a 64-byte loop here,
|
||||
* instead of a loop that is less unrolled.
|
||||
*/
|
||||
while (len >= 64) {
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 0)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 8)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 16)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 24)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 32)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 40)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 48)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 56)));
|
||||
p += 64;
|
||||
len -= 64;
|
||||
}
|
||||
}
|
||||
if (len & 32) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 0));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 8));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 16));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 24));
|
||||
p += 32;
|
||||
}
|
||||
if (len & 16) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 0));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 8));
|
||||
p += 16;
|
||||
}
|
||||
if (len & 8) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p));
|
||||
p += 8;
|
||||
}
|
||||
if (len & 4) {
|
||||
crc = __crc32w(crc, get_unaligned_le32(p));
|
||||
p += 4;
|
||||
}
|
||||
if (len & 2) {
|
||||
crc = __crc32h(crc, get_unaligned_le16(p));
|
||||
p += 2;
|
||||
}
|
||||
if (len & 1)
|
||||
crc = __crc32b(crc, *p);
|
||||
return crc;
|
||||
}
|
||||
#undef ATTRIBUTES
|
||||
#endif /* crc32_arm_crc() */
|
||||
|
||||
/*
|
||||
* crc32_arm_crc_pmullcombine() - implementation using crc32 instructions, plus
|
||||
* pmull instructions for CRC combining
|
||||
*
|
||||
* This is similar to crc32_arm_crc(), but it enables the use of pmull
|
||||
* (carryless multiplication) instructions for the steps where the CRCs of
|
||||
* adjacent data chunks are combined. As this greatly speeds up CRC
|
||||
* combination, this implementation also differs from crc32_arm_crc() in that it
|
||||
* uses a variable chunk length which can get fairly small. The precomputed
|
||||
* multipliers needed for the selected chunk length are loaded from a table.
|
||||
*
|
||||
* Note that pmull is used here only for combining the CRCs of separately
|
||||
* checksummed chunks, not for folding the data itself. See crc32_arm_pmull*()
|
||||
* for implementations that use pmull for folding the data itself.
|
||||
*/
|
||||
#if HAVE_CRC32_INTRIN && HAVE_PMULL_INTRIN
|
||||
# ifdef __clang__
|
||||
# define ATTRIBUTES _target_attribute("crc,aes")
|
||||
# else
|
||||
# define ATTRIBUTES _target_attribute("+crc,+crypto")
|
||||
# endif
|
||||
|
||||
/* Do carryless multiplication of two 32-bit values. */
|
||||
static forceinline ATTRIBUTES u64
|
||||
clmul_u32(u32 a, u32 b)
|
||||
{
|
||||
uint64x2_t res = vreinterpretq_u64_p128(
|
||||
compat_vmull_p64((poly64_t)a, (poly64_t)b));
|
||||
|
||||
return vgetq_lane_u64(res, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Like combine_crcs_slow(), but uses vmull_p64 to do the multiplications more
|
||||
* quickly, and supports a variable chunk length. The chunk length is
|
||||
* 'i * CRC32_MIN_VARIABLE_CHUNK_LEN'
|
||||
* where 1 <= i < ARRAY_LEN(crc32_mults_for_chunklen).
|
||||
*/
|
||||
static forceinline ATTRIBUTES u32
|
||||
combine_crcs_fast(u32 crc0, u32 crc1, u32 crc2, u32 crc3, size_t i)
|
||||
{
|
||||
u64 res0 = clmul_u32(crc0, crc32_mults_for_chunklen[i][0]);
|
||||
u64 res1 = clmul_u32(crc1, crc32_mults_for_chunklen[i][1]);
|
||||
u64 res2 = clmul_u32(crc2, crc32_mults_for_chunklen[i][2]);
|
||||
|
||||
return __crc32d(0, res0 ^ res1 ^ res2) ^ crc3;
|
||||
}
|
||||
|
||||
#define crc32_arm_crc_pmullcombine crc32_arm_crc_pmullcombine
|
||||
static ATTRIBUTES u32
|
||||
crc32_arm_crc_pmullcombine(u32 crc, const u8 *p, size_t len)
|
||||
{
|
||||
const size_t align = -(uintptr_t)p & 7;
|
||||
|
||||
if (len >= align + CRC32_NUM_CHUNKS * CRC32_MIN_VARIABLE_CHUNK_LEN) {
|
||||
/* Align p to the next 8-byte boundary. */
|
||||
if (align) {
|
||||
if (align & 1)
|
||||
crc = __crc32b(crc, *p++);
|
||||
if (align & 2) {
|
||||
crc = __crc32h(crc, le16_bswap(*(u16 *)p));
|
||||
p += 2;
|
||||
}
|
||||
if (align & 4) {
|
||||
crc = __crc32w(crc, le32_bswap(*(u32 *)p));
|
||||
p += 4;
|
||||
}
|
||||
len -= align;
|
||||
}
|
||||
/*
|
||||
* Handle CRC32_MAX_VARIABLE_CHUNK_LEN specially, so that better
|
||||
* code is generated for it.
|
||||
*/
|
||||
while (len >= CRC32_NUM_CHUNKS * CRC32_MAX_VARIABLE_CHUNK_LEN) {
|
||||
const u64 *wp0 = (const u64 *)p;
|
||||
const u64 * const wp0_end =
|
||||
(const u64 *)(p + CRC32_MAX_VARIABLE_CHUNK_LEN);
|
||||
u32 crc1 = 0, crc2 = 0, crc3 = 0;
|
||||
|
||||
STATIC_ASSERT(CRC32_NUM_CHUNKS == 4);
|
||||
STATIC_ASSERT(CRC32_MAX_VARIABLE_CHUNK_LEN % (4 * 8) == 0);
|
||||
do {
|
||||
prefetchr(&wp0[64 + 0*CRC32_MAX_VARIABLE_CHUNK_LEN/8]);
|
||||
prefetchr(&wp0[64 + 1*CRC32_MAX_VARIABLE_CHUNK_LEN/8]);
|
||||
prefetchr(&wp0[64 + 2*CRC32_MAX_VARIABLE_CHUNK_LEN/8]);
|
||||
prefetchr(&wp0[64 + 3*CRC32_MAX_VARIABLE_CHUNK_LEN/8]);
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
crc = __crc32d(crc, le64_bswap(wp0[0*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc1 = __crc32d(crc1, le64_bswap(wp0[1*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc2 = __crc32d(crc2, le64_bswap(wp0[2*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
crc3 = __crc32d(crc3, le64_bswap(wp0[3*CRC32_MAX_VARIABLE_CHUNK_LEN/8]));
|
||||
wp0++;
|
||||
} while (wp0 != wp0_end);
|
||||
crc = combine_crcs_fast(crc, crc1, crc2, crc3,
|
||||
ARRAY_LEN(crc32_mults_for_chunklen) - 1);
|
||||
p += CRC32_NUM_CHUNKS * CRC32_MAX_VARIABLE_CHUNK_LEN;
|
||||
len -= CRC32_NUM_CHUNKS * CRC32_MAX_VARIABLE_CHUNK_LEN;
|
||||
}
|
||||
/* Handle up to one variable-length chunk. */
|
||||
if (len >= CRC32_NUM_CHUNKS * CRC32_MIN_VARIABLE_CHUNK_LEN) {
|
||||
const size_t i = len / (CRC32_NUM_CHUNKS *
|
||||
CRC32_MIN_VARIABLE_CHUNK_LEN);
|
||||
const size_t chunk_len =
|
||||
i * CRC32_MIN_VARIABLE_CHUNK_LEN;
|
||||
const u64 *wp0 = (const u64 *)(p + 0*chunk_len);
|
||||
const u64 *wp1 = (const u64 *)(p + 1*chunk_len);
|
||||
const u64 *wp2 = (const u64 *)(p + 2*chunk_len);
|
||||
const u64 *wp3 = (const u64 *)(p + 3*chunk_len);
|
||||
const u64 * const wp0_end = wp1;
|
||||
u32 crc1 = 0, crc2 = 0, crc3 = 0;
|
||||
|
||||
STATIC_ASSERT(CRC32_NUM_CHUNKS == 4);
|
||||
STATIC_ASSERT(CRC32_MIN_VARIABLE_CHUNK_LEN % (4 * 8) == 0);
|
||||
do {
|
||||
prefetchr(wp0 + 64);
|
||||
prefetchr(wp1 + 64);
|
||||
prefetchr(wp2 + 64);
|
||||
prefetchr(wp3 + 64);
|
||||
crc = __crc32d(crc, le64_bswap(*wp0++));
|
||||
crc1 = __crc32d(crc1, le64_bswap(*wp1++));
|
||||
crc2 = __crc32d(crc2, le64_bswap(*wp2++));
|
||||
crc3 = __crc32d(crc3, le64_bswap(*wp3++));
|
||||
crc = __crc32d(crc, le64_bswap(*wp0++));
|
||||
crc1 = __crc32d(crc1, le64_bswap(*wp1++));
|
||||
crc2 = __crc32d(crc2, le64_bswap(*wp2++));
|
||||
crc3 = __crc32d(crc3, le64_bswap(*wp3++));
|
||||
crc = __crc32d(crc, le64_bswap(*wp0++));
|
||||
crc1 = __crc32d(crc1, le64_bswap(*wp1++));
|
||||
crc2 = __crc32d(crc2, le64_bswap(*wp2++));
|
||||
crc3 = __crc32d(crc3, le64_bswap(*wp3++));
|
||||
crc = __crc32d(crc, le64_bswap(*wp0++));
|
||||
crc1 = __crc32d(crc1, le64_bswap(*wp1++));
|
||||
crc2 = __crc32d(crc2, le64_bswap(*wp2++));
|
||||
crc3 = __crc32d(crc3, le64_bswap(*wp3++));
|
||||
} while (wp0 != wp0_end);
|
||||
crc = combine_crcs_fast(crc, crc1, crc2, crc3, i);
|
||||
p += CRC32_NUM_CHUNKS * chunk_len;
|
||||
len -= CRC32_NUM_CHUNKS * chunk_len;
|
||||
}
|
||||
|
||||
while (len >= 32) {
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 0)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 8)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 16)));
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)(p + 24)));
|
||||
p += 32;
|
||||
len -= 32;
|
||||
}
|
||||
} else {
|
||||
while (len >= 32) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 0));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 8));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 16));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 24));
|
||||
p += 32;
|
||||
len -= 32;
|
||||
}
|
||||
}
|
||||
if (len & 16) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 0));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 8));
|
||||
p += 16;
|
||||
}
|
||||
if (len & 8) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p));
|
||||
p += 8;
|
||||
}
|
||||
if (len & 4) {
|
||||
crc = __crc32w(crc, get_unaligned_le32(p));
|
||||
p += 4;
|
||||
}
|
||||
if (len & 2) {
|
||||
crc = __crc32h(crc, get_unaligned_le16(p));
|
||||
p += 2;
|
||||
}
|
||||
if (len & 1)
|
||||
crc = __crc32b(crc, *p);
|
||||
return crc;
|
||||
}
|
||||
#undef ATTRIBUTES
|
||||
#endif /* crc32_arm_crc_pmullcombine() */
|
||||
|
||||
/*
|
||||
* crc32_arm_pmullx4() - implementation using "folding" with pmull instructions
|
||||
*
|
||||
* This implementation is intended for CPUs that support pmull instructions but
|
||||
* not crc32 instructions.
|
||||
*/
|
||||
#if HAVE_PMULL_INTRIN
|
||||
# define crc32_arm_pmullx4 crc32_arm_pmullx4
|
||||
# define SUFFIX _pmullx4
|
||||
# ifdef __clang__
|
||||
/*
|
||||
* This used to use "crypto", but that stopped working with clang 16.
|
||||
* Now only "aes" works. "aes" works with older versions too, so use
|
||||
* that. No "+" prefix; clang 15 and earlier doesn't accept that.
|
||||
*/
|
||||
# define ATTRIBUTES _target_attribute("aes")
|
||||
# else
|
||||
/*
|
||||
* With gcc, only "+crypto" works. Both the "+" prefix and the
|
||||
* "crypto" (not "aes") are essential...
|
||||
*/
|
||||
# define ATTRIBUTES _target_attribute("+crypto")
|
||||
# endif
|
||||
# define ENABLE_EOR3 0
|
||||
# include "crc32_pmull_helpers.h"
|
||||
|
||||
static ATTRIBUTES u32
|
||||
crc32_arm_pmullx4(u32 crc, const u8 *p, size_t len)
|
||||
{
|
||||
static const u64 _aligned_attribute(16) mults[3][2] = {
|
||||
{ CRC32_X159_MODG, CRC32_X95_MODG }, /* 1 vecs */
|
||||
{ CRC32_X543_MODG, CRC32_X479_MODG }, /* 4 vecs */
|
||||
{ CRC32_X287_MODG, CRC32_X223_MODG }, /* 2 vecs */
|
||||
};
|
||||
static const u64 _aligned_attribute(16) barrett_consts[3][2] = {
|
||||
{ CRC32_X95_MODG, },
|
||||
{ CRC32_BARRETT_CONSTANT_1, },
|
||||
{ CRC32_BARRETT_CONSTANT_2, },
|
||||
};
|
||||
const poly64x2_t multipliers_1 = load_multipliers(mults[0]);
|
||||
uint8x16_t v0, v1, v2, v3;
|
||||
|
||||
if (len < 64 + 15) {
|
||||
if (len < 16)
|
||||
return crc32_slice1(crc, p, len);
|
||||
v0 = veorq_u8(vld1q_u8(p), u32_to_bytevec(crc));
|
||||
p += 16;
|
||||
len -= 16;
|
||||
while (len >= 16) {
|
||||
v0 = fold_vec(v0, vld1q_u8(p), multipliers_1);
|
||||
p += 16;
|
||||
len -= 16;
|
||||
}
|
||||
} else {
|
||||
const poly64x2_t multipliers_4 = load_multipliers(mults[1]);
|
||||
const poly64x2_t multipliers_2 = load_multipliers(mults[2]);
|
||||
const size_t align = -(uintptr_t)p & 15;
|
||||
const uint8x16_t *vp;
|
||||
|
||||
v0 = veorq_u8(vld1q_u8(p), u32_to_bytevec(crc));
|
||||
p += 16;
|
||||
/* Align p to the next 16-byte boundary. */
|
||||
if (align) {
|
||||
v0 = fold_partial_vec(v0, p, align, multipliers_1);
|
||||
p += align;
|
||||
len -= align;
|
||||
}
|
||||
vp = (const uint8x16_t *)p;
|
||||
v1 = *vp++;
|
||||
v2 = *vp++;
|
||||
v3 = *vp++;
|
||||
while (len >= 64 + 64) {
|
||||
v0 = fold_vec(v0, *vp++, multipliers_4);
|
||||
v1 = fold_vec(v1, *vp++, multipliers_4);
|
||||
v2 = fold_vec(v2, *vp++, multipliers_4);
|
||||
v3 = fold_vec(v3, *vp++, multipliers_4);
|
||||
len -= 64;
|
||||
}
|
||||
v0 = fold_vec(v0, v2, multipliers_2);
|
||||
v1 = fold_vec(v1, v3, multipliers_2);
|
||||
if (len & 32) {
|
||||
v0 = fold_vec(v0, *vp++, multipliers_2);
|
||||
v1 = fold_vec(v1, *vp++, multipliers_2);
|
||||
}
|
||||
v0 = fold_vec(v0, v1, multipliers_1);
|
||||
if (len & 16)
|
||||
v0 = fold_vec(v0, *vp++, multipliers_1);
|
||||
p = (const u8 *)vp;
|
||||
len &= 15;
|
||||
}
|
||||
|
||||
/* Handle any remaining partial block now before reducing to 32 bits. */
|
||||
if (len)
|
||||
v0 = fold_partial_vec(v0, p, len, multipliers_1);
|
||||
|
||||
/* Reduce to 32 bits, following lib/x86/crc32_pclmul_template.h */
|
||||
v0 = veorq_u8(clmul_low(v0, load_multipliers(barrett_consts[0])),
|
||||
vextq_u8(v0, vdupq_n_u8(0), 8));
|
||||
v1 = clmul_low(v0, load_multipliers(barrett_consts[1]));
|
||||
v1 = clmul_low(v1, load_multipliers(barrett_consts[2]));
|
||||
v0 = veorq_u8(v0, v1);
|
||||
return vgetq_lane_u32(vreinterpretq_u32_u8(v0), 2);
|
||||
}
|
||||
#undef SUFFIX
|
||||
#undef ATTRIBUTES
|
||||
#undef ENABLE_EOR3
|
||||
#endif /* crc32_arm_pmullx4() */
|
||||
|
||||
/*
|
||||
* crc32_arm_pmullx12_crc() - large-stride implementation using "folding" with
|
||||
* pmull instructions, where crc32 instructions are also available
|
||||
*
|
||||
* See crc32_pmull_wide.h for explanation.
|
||||
*/
|
||||
#if HAVE_PMULL_INTRIN && HAVE_CRC32_INTRIN
|
||||
# define crc32_arm_pmullx12_crc crc32_arm_pmullx12_crc
|
||||
# define SUFFIX _pmullx12_crc
|
||||
# ifdef __clang__
|
||||
# define ATTRIBUTES _target_attribute("aes,crc")
|
||||
# else
|
||||
# define ATTRIBUTES _target_attribute("+crypto,+crc")
|
||||
# endif
|
||||
# define ENABLE_EOR3 0
|
||||
# include "crc32_pmull_wide.h"
|
||||
#endif
|
||||
|
||||
/*
|
||||
* crc32_arm_pmullx12_crc_eor3()
|
||||
*
|
||||
* This like crc32_arm_pmullx12_crc(), but it adds the eor3 instruction (from
|
||||
* the sha3 extension) for even better performance.
|
||||
*/
|
||||
#if HAVE_PMULL_INTRIN && HAVE_CRC32_INTRIN && HAVE_SHA3_INTRIN && \
|
||||
!defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_SHA3)
|
||||
# define crc32_arm_pmullx12_crc_eor3 crc32_arm_pmullx12_crc_eor3
|
||||
# define SUFFIX _pmullx12_crc_eor3
|
||||
# ifdef __clang__
|
||||
# define ATTRIBUTES _target_attribute("aes,crc,sha3")
|
||||
/*
|
||||
* Both gcc and binutils originally considered sha3 to depend on
|
||||
* arch=armv8.2-a or later. This was fixed in gcc 13.2 by commit
|
||||
* 9aac37ab8a7b ("aarch64: Remove architecture dependencies from intrinsics")
|
||||
* and in binutils 2.41 by commit 205e4380c800 ("aarch64: Remove version
|
||||
* dependencies from features"). Unfortunately, always using arch=armv8.2-a
|
||||
* causes build errors with some compiler options because it may reduce the
|
||||
* arch rather than increase it. Therefore we try to omit the arch whenever
|
||||
* possible. If gcc is 14 or later, then both gcc and binutils are probably
|
||||
* fixed, so we omit the arch. We also omit the arch if a feature that
|
||||
* depends on armv8.2-a or later (in gcc 13.1 and earlier) is present.
|
||||
*/
|
||||
# elif GCC_PREREQ(14, 0) || defined(__ARM_FEATURE_JCVT) \
|
||||
|| defined(__ARM_FEATURE_DOTPROD)
|
||||
# define ATTRIBUTES _target_attribute("+crypto,+crc,+sha3")
|
||||
# else
|
||||
# define ATTRIBUTES _target_attribute("arch=armv8.2-a+crypto+crc+sha3")
|
||||
# endif
|
||||
# define ENABLE_EOR3 1
|
||||
# include "crc32_pmull_wide.h"
|
||||
#endif
|
||||
|
||||
static inline crc32_func_t
|
||||
arch_select_crc32_func(void)
|
||||
{
|
||||
const u32 features MAYBE_UNUSED = get_arm_cpu_features();
|
||||
|
||||
#ifdef crc32_arm_pmullx12_crc_eor3
|
||||
if ((features & ARM_CPU_FEATURE_PREFER_PMULL) &&
|
||||
HAVE_PMULL(features) && HAVE_CRC32(features) && HAVE_SHA3(features))
|
||||
return crc32_arm_pmullx12_crc_eor3;
|
||||
#endif
|
||||
#ifdef crc32_arm_pmullx12_crc
|
||||
if ((features & ARM_CPU_FEATURE_PREFER_PMULL) &&
|
||||
HAVE_PMULL(features) && HAVE_CRC32(features))
|
||||
return crc32_arm_pmullx12_crc;
|
||||
#endif
|
||||
#ifdef crc32_arm_crc_pmullcombine
|
||||
if (HAVE_CRC32(features) && HAVE_PMULL(features))
|
||||
return crc32_arm_crc_pmullcombine;
|
||||
#endif
|
||||
#ifdef crc32_arm_crc
|
||||
if (HAVE_CRC32(features))
|
||||
return crc32_arm_crc;
|
||||
#endif
|
||||
#ifdef crc32_arm_pmullx4
|
||||
if (HAVE_PMULL(features))
|
||||
return crc32_arm_pmullx4;
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
#define arch_select_crc32_func arch_select_crc32_func
|
||||
|
||||
#endif /* LIB_ARM_CRC32_IMPL_H */
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* arm/crc32_pmull_helpers.h - helper functions for CRC-32 folding with PMULL
|
||||
*
|
||||
* Copyright 2022 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is a "template" for instantiating helper functions for CRC folding
|
||||
* with pmull instructions. It accepts the following parameters:
|
||||
*
|
||||
* SUFFIX:
|
||||
* Name suffix to append to all instantiated functions.
|
||||
* ATTRIBUTES:
|
||||
* Target function attributes to use.
|
||||
* ENABLE_EOR3:
|
||||
* Use the eor3 instruction (from the sha3 extension).
|
||||
*/
|
||||
|
||||
/* Create a vector with 'a' in the first 4 bytes, and the rest zeroed out. */
|
||||
#undef u32_to_bytevec
|
||||
static forceinline ATTRIBUTES uint8x16_t
|
||||
ADD_SUFFIX(u32_to_bytevec)(u32 a)
|
||||
{
|
||||
return vreinterpretq_u8_u32(vsetq_lane_u32(a, vdupq_n_u32(0), 0));
|
||||
}
|
||||
#define u32_to_bytevec ADD_SUFFIX(u32_to_bytevec)
|
||||
|
||||
/* Load two 64-bit values into a vector. */
|
||||
#undef load_multipliers
|
||||
static forceinline ATTRIBUTES poly64x2_t
|
||||
ADD_SUFFIX(load_multipliers)(const u64 p[2])
|
||||
{
|
||||
return vreinterpretq_p64_u64(vld1q_u64(p));
|
||||
}
|
||||
#define load_multipliers ADD_SUFFIX(load_multipliers)
|
||||
|
||||
/* Do carryless multiplication of the low halves of two vectors. */
|
||||
#undef clmul_low
|
||||
static forceinline ATTRIBUTES uint8x16_t
|
||||
ADD_SUFFIX(clmul_low)(uint8x16_t a, poly64x2_t b)
|
||||
{
|
||||
return vreinterpretq_u8_p128(
|
||||
compat_vmull_p64(vgetq_lane_p64(vreinterpretq_p64_u8(a), 0),
|
||||
vgetq_lane_p64(b, 0)));
|
||||
}
|
||||
#define clmul_low ADD_SUFFIX(clmul_low)
|
||||
|
||||
/* Do carryless multiplication of the high halves of two vectors. */
|
||||
#undef clmul_high
|
||||
static forceinline ATTRIBUTES uint8x16_t
|
||||
ADD_SUFFIX(clmul_high)(uint8x16_t a, poly64x2_t b)
|
||||
{
|
||||
#ifdef __clang__
|
||||
/*
|
||||
* Use inline asm to ensure that pmull2 is really used. This works
|
||||
* around clang bug https://github.com/llvm/llvm-project/issues/52868.
|
||||
*/
|
||||
uint8x16_t res;
|
||||
|
||||
__asm__("pmull2 %0.1q, %1.2d, %2.2d" : "=w" (res) : "w" (a), "w" (b));
|
||||
return res;
|
||||
#else
|
||||
return vreinterpretq_u8_p128(vmull_high_p64(vreinterpretq_p64_u8(a), b));
|
||||
#endif
|
||||
}
|
||||
#define clmul_high ADD_SUFFIX(clmul_high)
|
||||
|
||||
#undef eor3
|
||||
static forceinline ATTRIBUTES uint8x16_t
|
||||
ADD_SUFFIX(eor3)(uint8x16_t a, uint8x16_t b, uint8x16_t c)
|
||||
{
|
||||
#if ENABLE_EOR3
|
||||
return veor3q_u8(a, b, c);
|
||||
#else
|
||||
return veorq_u8(veorq_u8(a, b), c);
|
||||
#endif
|
||||
}
|
||||
#define eor3 ADD_SUFFIX(eor3)
|
||||
|
||||
#undef fold_vec
|
||||
static forceinline ATTRIBUTES uint8x16_t
|
||||
ADD_SUFFIX(fold_vec)(uint8x16_t src, uint8x16_t dst, poly64x2_t multipliers)
|
||||
{
|
||||
uint8x16_t a = clmul_low(src, multipliers);
|
||||
uint8x16_t b = clmul_high(src, multipliers);
|
||||
|
||||
return eor3(a, b, dst);
|
||||
}
|
||||
#define fold_vec ADD_SUFFIX(fold_vec)
|
||||
|
||||
/*
|
||||
* Given v containing a 16-byte polynomial, and a pointer 'p' that points to the
|
||||
* next '1 <= len <= 15' data bytes, rearrange the concatenation of v and the
|
||||
* data into vectors x0 and x1 that contain 'len' bytes and 16 bytes,
|
||||
* respectively. Then fold x0 into x1 and return the result. Assumes that
|
||||
* 'p + len - 16' is in-bounds.
|
||||
*/
|
||||
#undef fold_partial_vec
|
||||
static forceinline ATTRIBUTES MAYBE_UNUSED uint8x16_t
|
||||
ADD_SUFFIX(fold_partial_vec)(uint8x16_t v, const u8 *p, size_t len,
|
||||
poly64x2_t multipliers_1)
|
||||
{
|
||||
/*
|
||||
* vqtbl1q_u8(v, shift_tab[len..len+15]) left shifts v by 16-len bytes.
|
||||
* vqtbl1q_u8(v, shift_tab[len+16..len+31]) right shifts v by len bytes.
|
||||
*/
|
||||
static const u8 shift_tab[48] = {
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
};
|
||||
const uint8x16_t lshift = vld1q_u8(&shift_tab[len]);
|
||||
const uint8x16_t rshift = vld1q_u8(&shift_tab[len + 16]);
|
||||
uint8x16_t x0, x1, bsl_mask;
|
||||
|
||||
/* x0 = v left-shifted by '16 - len' bytes */
|
||||
x0 = vqtbl1q_u8(v, lshift);
|
||||
|
||||
/* Create a vector of '16 - len' 0x00 bytes, then 'len' 0xff bytes. */
|
||||
bsl_mask = vreinterpretq_u8_s8(
|
||||
vshrq_n_s8(vreinterpretq_s8_u8(rshift), 7));
|
||||
|
||||
/*
|
||||
* x1 = the last '16 - len' bytes from v (i.e. v right-shifted by 'len'
|
||||
* bytes) followed by the remaining data.
|
||||
*/
|
||||
x1 = vbslq_u8(bsl_mask /* 0 bits select from arg3, 1 bits from arg2 */,
|
||||
vld1q_u8(p + len - 16), vqtbl1q_u8(v, rshift));
|
||||
|
||||
return fold_vec(x0, x1, multipliers_1);
|
||||
}
|
||||
#define fold_partial_vec ADD_SUFFIX(fold_partial_vec)
|
||||
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
* arm/crc32_pmull_wide.h - gzip CRC-32 with PMULL (extra-wide version)
|
||||
*
|
||||
* Copyright 2022 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is a "template" for instantiating PMULL-based crc32_arm functions.
|
||||
* The "parameters" are:
|
||||
*
|
||||
* SUFFIX:
|
||||
* Name suffix to append to all instantiated functions.
|
||||
* ATTRIBUTES:
|
||||
* Target function attributes to use.
|
||||
* ENABLE_EOR3:
|
||||
* Use the eor3 instruction (from the sha3 extension).
|
||||
*
|
||||
* This is the extra-wide version; it uses an unusually large stride length of
|
||||
* 12, and it assumes that crc32 instructions are available too. It's intended
|
||||
* for powerful CPUs that support both pmull and crc32 instructions, but where
|
||||
* throughput of pmull and xor (given enough instructions issued in parallel) is
|
||||
* significantly higher than that of crc32, thus making the crc32 instructions
|
||||
* (counterintuitively) not actually the fastest way to compute the CRC-32. The
|
||||
* Apple M1 processor is an example of such a CPU.
|
||||
*/
|
||||
|
||||
#include "crc32_pmull_helpers.h"
|
||||
|
||||
static ATTRIBUTES u32
|
||||
ADD_SUFFIX(crc32_arm)(u32 crc, const u8 *p, size_t len)
|
||||
{
|
||||
uint8x16_t v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11;
|
||||
|
||||
if (len < 3 * 192) {
|
||||
static const u64 _aligned_attribute(16) mults[3][2] = {
|
||||
{ CRC32_X543_MODG, CRC32_X479_MODG }, /* 4 vecs */
|
||||
{ CRC32_X287_MODG, CRC32_X223_MODG }, /* 2 vecs */
|
||||
{ CRC32_X159_MODG, CRC32_X95_MODG }, /* 1 vecs */
|
||||
};
|
||||
poly64x2_t multipliers_4, multipliers_2, multipliers_1;
|
||||
|
||||
if (len < 64)
|
||||
goto tail;
|
||||
multipliers_4 = load_multipliers(mults[0]);
|
||||
multipliers_2 = load_multipliers(mults[1]);
|
||||
multipliers_1 = load_multipliers(mults[2]);
|
||||
/*
|
||||
* Short length; don't bother aligning the pointer, and fold
|
||||
* 64 bytes (4 vectors) at a time, at most.
|
||||
*/
|
||||
v0 = veorq_u8(vld1q_u8(p + 0), u32_to_bytevec(crc));
|
||||
v1 = vld1q_u8(p + 16);
|
||||
v2 = vld1q_u8(p + 32);
|
||||
v3 = vld1q_u8(p + 48);
|
||||
p += 64;
|
||||
len -= 64;
|
||||
while (len >= 64) {
|
||||
v0 = fold_vec(v0, vld1q_u8(p + 0), multipliers_4);
|
||||
v1 = fold_vec(v1, vld1q_u8(p + 16), multipliers_4);
|
||||
v2 = fold_vec(v2, vld1q_u8(p + 32), multipliers_4);
|
||||
v3 = fold_vec(v3, vld1q_u8(p + 48), multipliers_4);
|
||||
p += 64;
|
||||
len -= 64;
|
||||
}
|
||||
v0 = fold_vec(v0, v2, multipliers_2);
|
||||
v1 = fold_vec(v1, v3, multipliers_2);
|
||||
if (len >= 32) {
|
||||
v0 = fold_vec(v0, vld1q_u8(p + 0), multipliers_2);
|
||||
v1 = fold_vec(v1, vld1q_u8(p + 16), multipliers_2);
|
||||
p += 32;
|
||||
len -= 32;
|
||||
}
|
||||
v0 = fold_vec(v0, v1, multipliers_1);
|
||||
} else {
|
||||
static const u64 _aligned_attribute(16) mults[4][2] = {
|
||||
{ CRC32_X1567_MODG, CRC32_X1503_MODG }, /* 12 vecs */
|
||||
{ CRC32_X799_MODG, CRC32_X735_MODG }, /* 6 vecs */
|
||||
{ CRC32_X415_MODG, CRC32_X351_MODG }, /* 3 vecs */
|
||||
{ CRC32_X159_MODG, CRC32_X95_MODG }, /* 1 vecs */
|
||||
};
|
||||
const poly64x2_t multipliers_12 = load_multipliers(mults[0]);
|
||||
const poly64x2_t multipliers_6 = load_multipliers(mults[1]);
|
||||
const poly64x2_t multipliers_3 = load_multipliers(mults[2]);
|
||||
const poly64x2_t multipliers_1 = load_multipliers(mults[3]);
|
||||
const size_t align = -(uintptr_t)p & 15;
|
||||
const uint8x16_t *vp;
|
||||
|
||||
/* Align p to the next 16-byte boundary. */
|
||||
if (align) {
|
||||
if (align & 1)
|
||||
crc = __crc32b(crc, *p++);
|
||||
if (align & 2) {
|
||||
crc = __crc32h(crc, le16_bswap(*(u16 *)p));
|
||||
p += 2;
|
||||
}
|
||||
if (align & 4) {
|
||||
crc = __crc32w(crc, le32_bswap(*(u32 *)p));
|
||||
p += 4;
|
||||
}
|
||||
if (align & 8) {
|
||||
crc = __crc32d(crc, le64_bswap(*(u64 *)p));
|
||||
p += 8;
|
||||
}
|
||||
len -= align;
|
||||
}
|
||||
vp = (const uint8x16_t *)p;
|
||||
v0 = veorq_u8(*vp++, u32_to_bytevec(crc));
|
||||
v1 = *vp++;
|
||||
v2 = *vp++;
|
||||
v3 = *vp++;
|
||||
v4 = *vp++;
|
||||
v5 = *vp++;
|
||||
v6 = *vp++;
|
||||
v7 = *vp++;
|
||||
v8 = *vp++;
|
||||
v9 = *vp++;
|
||||
v10 = *vp++;
|
||||
v11 = *vp++;
|
||||
len -= 192;
|
||||
/* Fold 192 bytes (12 vectors) at a time. */
|
||||
do {
|
||||
v0 = fold_vec(v0, *vp++, multipliers_12);
|
||||
v1 = fold_vec(v1, *vp++, multipliers_12);
|
||||
v2 = fold_vec(v2, *vp++, multipliers_12);
|
||||
v3 = fold_vec(v3, *vp++, multipliers_12);
|
||||
v4 = fold_vec(v4, *vp++, multipliers_12);
|
||||
v5 = fold_vec(v5, *vp++, multipliers_12);
|
||||
v6 = fold_vec(v6, *vp++, multipliers_12);
|
||||
v7 = fold_vec(v7, *vp++, multipliers_12);
|
||||
v8 = fold_vec(v8, *vp++, multipliers_12);
|
||||
v9 = fold_vec(v9, *vp++, multipliers_12);
|
||||
v10 = fold_vec(v10, *vp++, multipliers_12);
|
||||
v11 = fold_vec(v11, *vp++, multipliers_12);
|
||||
len -= 192;
|
||||
} while (len >= 192);
|
||||
|
||||
/*
|
||||
* Fewer than 192 bytes left. Fold v0-v11 down to just v0,
|
||||
* while processing up to 144 more bytes.
|
||||
*/
|
||||
v0 = fold_vec(v0, v6, multipliers_6);
|
||||
v1 = fold_vec(v1, v7, multipliers_6);
|
||||
v2 = fold_vec(v2, v8, multipliers_6);
|
||||
v3 = fold_vec(v3, v9, multipliers_6);
|
||||
v4 = fold_vec(v4, v10, multipliers_6);
|
||||
v5 = fold_vec(v5, v11, multipliers_6);
|
||||
if (len >= 96) {
|
||||
v0 = fold_vec(v0, *vp++, multipliers_6);
|
||||
v1 = fold_vec(v1, *vp++, multipliers_6);
|
||||
v2 = fold_vec(v2, *vp++, multipliers_6);
|
||||
v3 = fold_vec(v3, *vp++, multipliers_6);
|
||||
v4 = fold_vec(v4, *vp++, multipliers_6);
|
||||
v5 = fold_vec(v5, *vp++, multipliers_6);
|
||||
len -= 96;
|
||||
}
|
||||
v0 = fold_vec(v0, v3, multipliers_3);
|
||||
v1 = fold_vec(v1, v4, multipliers_3);
|
||||
v2 = fold_vec(v2, v5, multipliers_3);
|
||||
if (len >= 48) {
|
||||
v0 = fold_vec(v0, *vp++, multipliers_3);
|
||||
v1 = fold_vec(v1, *vp++, multipliers_3);
|
||||
v2 = fold_vec(v2, *vp++, multipliers_3);
|
||||
len -= 48;
|
||||
}
|
||||
v0 = fold_vec(v0, v1, multipliers_1);
|
||||
v0 = fold_vec(v0, v2, multipliers_1);
|
||||
p = (const u8 *)vp;
|
||||
}
|
||||
/* Reduce 128 to 32 bits using crc32 instructions. */
|
||||
crc = __crc32d(0, vgetq_lane_u64(vreinterpretq_u64_u8(v0), 0));
|
||||
crc = __crc32d(crc, vgetq_lane_u64(vreinterpretq_u64_u8(v0), 1));
|
||||
tail:
|
||||
/* Finish up the remainder using crc32 instructions. */
|
||||
if (len & 32) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 0));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 8));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 16));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 24));
|
||||
p += 32;
|
||||
}
|
||||
if (len & 16) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 0));
|
||||
crc = __crc32d(crc, get_unaligned_le64(p + 8));
|
||||
p += 16;
|
||||
}
|
||||
if (len & 8) {
|
||||
crc = __crc32d(crc, get_unaligned_le64(p));
|
||||
p += 8;
|
||||
}
|
||||
if (len & 4) {
|
||||
crc = __crc32w(crc, get_unaligned_le32(p));
|
||||
p += 4;
|
||||
}
|
||||
if (len & 2) {
|
||||
crc = __crc32h(crc, get_unaligned_le16(p));
|
||||
p += 2;
|
||||
}
|
||||
if (len & 1)
|
||||
crc = __crc32b(crc, *p);
|
||||
return crc;
|
||||
}
|
||||
|
||||
#undef SUFFIX
|
||||
#undef ATTRIBUTES
|
||||
#undef ENABLE_EOR3
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* arm/matchfinder_impl.h - ARM implementations of matchfinder functions
|
||||
*
|
||||
* Copyright 2016 Eric Biggers
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person
|
||||
* obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without
|
||||
* restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following
|
||||
* conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIB_ARM_MATCHFINDER_IMPL_H
|
||||
#define LIB_ARM_MATCHFINDER_IMPL_H
|
||||
|
||||
#include "cpu_features.h"
|
||||
|
||||
#if HAVE_NEON_NATIVE
|
||||
static forceinline void
|
||||
matchfinder_init_neon(mf_pos_t *data, size_t size)
|
||||
{
|
||||
int16x8_t *p = (int16x8_t *)data;
|
||||
int16x8_t v = vdupq_n_s16(MATCHFINDER_INITVAL);
|
||||
|
||||
STATIC_ASSERT(MATCHFINDER_MEM_ALIGNMENT % sizeof(*p) == 0);
|
||||
STATIC_ASSERT(MATCHFINDER_SIZE_ALIGNMENT % (4 * sizeof(*p)) == 0);
|
||||
STATIC_ASSERT(sizeof(mf_pos_t) == 2);
|
||||
|
||||
do {
|
||||
p[0] = v;
|
||||
p[1] = v;
|
||||
p[2] = v;
|
||||
p[3] = v;
|
||||
p += 4;
|
||||
size -= 4 * sizeof(*p);
|
||||
} while (size != 0);
|
||||
}
|
||||
#define matchfinder_init matchfinder_init_neon
|
||||
|
||||
static forceinline void
|
||||
matchfinder_rebase_neon(mf_pos_t *data, size_t size)
|
||||
{
|
||||
int16x8_t *p = (int16x8_t *)data;
|
||||
int16x8_t v = vdupq_n_s16((u16)-MATCHFINDER_WINDOW_SIZE);
|
||||
|
||||
STATIC_ASSERT(MATCHFINDER_MEM_ALIGNMENT % sizeof(*p) == 0);
|
||||
STATIC_ASSERT(MATCHFINDER_SIZE_ALIGNMENT % (4 * sizeof(*p)) == 0);
|
||||
STATIC_ASSERT(sizeof(mf_pos_t) == 2);
|
||||
|
||||
do {
|
||||
p[0] = vqaddq_s16(p[0], v);
|
||||
p[1] = vqaddq_s16(p[1], v);
|
||||
p[2] = vqaddq_s16(p[2], v);
|
||||
p[3] = vqaddq_s16(p[3], v);
|
||||
p += 4;
|
||||
size -= 4 * sizeof(*p);
|
||||
} while (size != 0);
|
||||
}
|
||||
#define matchfinder_rebase matchfinder_rebase_neon
|
||||
|
||||
#endif /* HAVE_NEON_NATIVE */
|
||||
|
||||
#endif /* LIB_ARM_MATCHFINDER_IMPL_H */
|
||||
Reference in New Issue
Block a user