19#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM)
31static const uint8_t shuffleMasks[] = {
32 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
33 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81, 0x80,
36TARGET_WITH_SIMD
FORCE_INLINE void shiftRight128(uint64x2_t in,
size_t n, uint64x2_t *outLeft, uint64x2_t *outRight)
38 const uint64x2_t maskA =
39 vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)(
const uint64x2_t *)(shuffleMasks + (16 - n))));
40 uint64x2_t b = vreinterpretq_u64_u8(vceqq_u8(vreinterpretq_u8_u64(vreinterpretq_u64_u32(vdupq_n_u32(0))),
41 vreinterpretq_u8_u64(vreinterpretq_u64_u32(vdupq_n_u32(0)))));
42 const uint64x2_t maskB = vreinterpretq_u64_u32(veorq_u32(vreinterpretq_u32_u64(maskA), vreinterpretq_u32_u64(b)));
44 *outLeft = mm_shuffle_epi8(in, maskB);
45 *outRight = mm_shuffle_epi8(in, maskA);
48TARGET_WITH_SIMD
FORCE_INLINE uint64x2_t fold(uint64x2_t in, uint64x2_t foldConstants)
50 return veorq_u64(sse2neon_vmull_p64(vget_low_u64(in), vget_low_u64(foldConstants)),
51 sse2neon_vmull_p64(vget_high_u64(in), vget_high_u64(foldConstants)));
62AARU_EXPORT TARGET_WITH_SIMD uint64_t
AARU_CALL aaruf_crc64_vmull(uint64_t previous_crc,
const uint8_t *data,
long len)
64 TRACE(
"Entering aaruf_crc64_vmull(%llu, %p, %ld)", previous_crc, data, len);
66 const uint64_t k1 = 0xe05dd497ca393ae4;
67 const uint64_t k2 = 0xdabe95afc7875f40;
68 const uint64_t mu = 0x9c3e466c172963d5;
69 const uint64_t p = 0x92d8af2baf0e1e85;
71 const uint64x2_t foldConstants1 = vcombine_u64(vcreate_u64(k1), vcreate_u64(k2));
72 const uint64x2_t foldConstants2 = vcombine_u64(vcreate_u64(mu), vcreate_u64(p));
74 const uint8_t *end = data + len;
77 const uint64x2_t *alignedData = (
const uint64x2_t *)((uintptr_t)data & ~(uintptr_t)15);
78 const uint64x2_t *alignedEnd = (
const uint64x2_t *)(((uintptr_t)end + 15) & ~(uintptr_t)15);
80 const size_t leadInSize = data - (
const uint8_t *)alignedData;
81 const size_t leadOutSize = (
const uint8_t *)alignedEnd - end;
83 const size_t alignedLength = alignedEnd - alignedData;
85 const uint64x2_t leadInMask =
86 vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)(
const uint64x2_t *)(shuffleMasks + (16 - leadInSize))));
87 uint64x2_t a = vreinterpretq_u64_u32(vdupq_n_u32(0));
88 uint64x2_t b = vreinterpretq_u64_u32(
89 vld1q_u32((
const uint32_t *)alignedData));
90 const uint64x2_t data0 =
91 vreinterpretq_u64_u8(vbslq_u8(vreinterpretq_u8_s8(vshrq_n_s8(vreinterpretq_s8_u64(leadInMask), 7)),
92 vreinterpretq_u8_u64(b), vreinterpretq_u8_u64(a)));
94 const uint64x2_t initialCrc = vsetq_lane_u64(~previous_crc, vdupq_n_u64(0), 0);
97 if(alignedLength == 1)
100 uint64x2_t crc0, crc1;
101 shiftRight128(initialCrc, 16 - len, &crc0, &crc1);
104 shiftRight128(data0, leadOutSize, &A, &B);
106 const uint64x2_t P = veorq_u64(A, crc0);
107 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)),
108 veorq_u64(mm_srli_si128(P, 8), mm_slli_si128(crc1, 8)));
110 else if(alignedLength == 2)
112 const uint64x2_t data1 = vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)(alignedData + 1)));
117 uint64x2_t crc0, crc1;
118 shiftRight128(initialCrc, 16 - len, &crc0, &crc1);
120 uint64x2_t A, B, C, D;
121 shiftRight128(data0, leadOutSize, &A, &B);
122 shiftRight128(data1, leadOutSize, &C, &D);
124 const uint64x2_t P = veorq_u64(veorq_u64(B, C), crc0);
125 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)),
126 veorq_u64(mm_srli_si128(P, 8), mm_slli_si128(crc1, 8)));
131 uint64x2_t crc0, crc1;
132 shiftRight128(initialCrc, leadInSize, &crc0, &crc1);
134 uint64x2_t A, B, C, D;
135 shiftRight128(veorq_u64(data0, crc0), leadOutSize, &A, &B);
136 shiftRight128(veorq_u64(data1, crc1), leadOutSize, &C, &D);
138 const uint64x2_t P = veorq_u64(fold(A, foldConstants1), veorq_u64(B, C));
139 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)), mm_srli_si128(P, 8));
145 len -= 16 - leadInSize;
148 uint64x2_t crc0, crc1;
149 shiftRight128(initialCrc, leadInSize, &crc0, &crc1);
151 uint64x2_t accumulator = veorq_u64(fold(veorq_u64(crc0, data0), foldConstants1), crc1);
155 accumulator = fold(veorq_u64(vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)alignedData)), accumulator),
164 P = veorq_u64(accumulator, vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)alignedData)));
168 const uint64x2_t end0 =
169 veorq_u64(accumulator, vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)alignedData)));
174 const uint8_t *nextBlockAddr = (
const uint8_t *)(alignedData + 1);
177 if(nextBlockAddr < end)
179 size_t available = (size_t)(end - nextBlockAddr);
180 if(available > 16) available = 16;
181 memcpy(temp, nextBlockAddr, available);
184 const uint64x2_t end1 = vreinterpretq_u64_u32(vld1q_u32((
const uint32_t *)temp));
186 uint64x2_t A, B, C, D;
187 shiftRight128(end0, leadOutSize, &A, &B);
188 shiftRight128(end1, leadOutSize, &C, &D);
190 P = veorq_u64(fold(A, foldConstants1),
191 vreinterpretq_u64_u32(vorrq_u32(vreinterpretq_u32_u64(B), vreinterpretq_u32_u64(C))));
194 R = veorq_u64(sse2neon_vmull_p64(vget_low_u64(P), vget_high_u64(foldConstants1)), mm_srli_si128(P, 8));
198 const uint64x2_t T1 = sse2neon_vmull_p64(vget_low_u64(R), vget_low_u64(foldConstants2));
199 const uint64x2_t T2 = veorq_u64(
200 veorq_u64(sse2neon_vmull_p64(vget_low_u64(T1), vget_high_u64(foldConstants2)), mm_slli_si128(T1, 8)), R);
202 TRACE(
"Exiting aaruf_crc64_vmull()");
204 return ~vgetq_lane_u64(T2, 1);
static __attribute__((always_inline))