Line | Count | Source |
1 | | /** |
2 | | * An implementation of the XXH3 hash function. |
3 | | * |
4 | | * The reason some of these names do not look quite like the style of the rest |
5 | | * of the code in Prism is that this was adapted straight from the XXH3 |
6 | | * specification here: |
7 | | * |
8 | | * https://github.com/Cyan4973/xxHash/blob/release/doc/xxhash_spec.md |
9 | | * |
10 | | * To better understand this code it is recommended to read the XXH3 |
11 | | * specification linked above, where you should be able to follow along with |
12 | | * this implementation more easily. Note this only implements the XXH3_64 |
13 | | * variant with the default secret and a seed of 0. Additionally it does not |
14 | | * include a lot of the various CPU-specific optimizations that exist in the |
15 | | * reference implementation. |
16 | | */ |
17 | | |
18 | | #include "prism/internal/xxh3.h" |
19 | | |
20 | | #include "prism/compiler/accel.h" |
21 | | #include "prism/compiler/bswap.h" |
22 | | #include "prism/compiler/endianness.h" |
23 | | |
24 | | #include <string.h> |
25 | | |
26 | | #if defined(PRISM_HAS_NEON) && defined(PRISM_LITTLE_ENDIAN_COMPTIME) |
27 | | #include <arm_neon.h> |
28 | | #define XXH3_NEON |
29 | | #elif defined(PRISM_HAS_SSE2) |
30 | | #include <emmintrin.h> |
31 | | #define XXH3_SSE2 |
32 | | #endif |
33 | | |
34 | | #if defined(__SIZEOF_INT128__) |
35 | | static uint64_t |
36 | 100k | XXH3_mul128_fold64(uint64_t lhs, uint64_t rhs) { |
37 | 100k | __uint128_t x = (__uint128_t) lhs * (__uint128_t) rhs; |
38 | 100k | return ((uint64_t) (x & 0xFFFFFFFFFFFFFFFFULL)) ^ ((uint64_t) (x >> 64)); |
39 | 100k | } |
40 | | #else |
41 | | #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64)) |
42 | | #include <intrin.h> |
43 | | #endif |
44 | | |
45 | | static uint64_t |
46 | | XXH3_mul128_fold64(uint64_t lhs, uint64_t rhs) { |
47 | | uint64_t lo, hi; |
48 | | #if defined(_MSC_VER) && defined(_M_X64) |
49 | | lo = _umul128(lhs, rhs, &hi); |
50 | | #elif defined(_MSC_VER) && defined(_M_ARM64) |
51 | | hi = __umulh(lhs, rhs); |
52 | | lo = lhs * rhs; |
53 | | #else |
54 | | uint64_t lo_lo = (lhs & 0xFFFFFFFF) * (rhs & 0xFFFFFFFF); |
55 | | uint64_t hi_lo = (lhs >> 32) * (rhs & 0xFFFFFFFF); |
56 | | uint64_t lo_hi = (lhs & 0xFFFFFFFF) * (rhs >> 32); |
57 | | uint64_t hi_hi = (lhs >> 32) * (rhs >> 32); |
58 | | |
59 | | uint64_t cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi; |
60 | | hi = hi_hi + (hi_lo >> 32) + (cross >> 32); |
61 | | lo = (cross << 32) | (lo_lo & 0xFFFFFFFF); |
62 | | #endif |
63 | | return lo ^ hi; |
64 | | } |
65 | | #endif |
66 | | |
67 | | static uint32_t |
68 | 10.6k | read32(const void* ptr) { |
69 | 10.6k | uint32_t val; |
70 | 10.6k | memcpy(&val, ptr, sizeof(val)); |
71 | 10.6k | if (!PRISM_LITTLE_ENDIAN_RUNTIME) { Branch (71:9): [Folded, False: 10.6k]
|
72 | 0 | val = pm_bswap32(val); |
73 | 0 | } |
74 | 10.6k | return val; |
75 | 10.6k | } |
76 | | |
77 | | static uint64_t |
78 | 338k | read64(const void* ptr) { |
79 | 338k | uint64_t val; |
80 | 338k | memcpy(&val, ptr, sizeof(val)); |
81 | 338k | if (!PRISM_LITTLE_ENDIAN_RUNTIME) { Branch (81:9): [Folded, False: 338k]
|
82 | 0 | val = pm_bswap64(val); |
83 | 0 | } |
84 | 338k | return val; |
85 | 338k | } |
86 | | |
87 | | static const uint64_t PRIME32_1 = 0x9E3779B1U; |
88 | | static const uint64_t PRIME32_2 = 0x85EBCA77U; |
89 | | static const uint64_t PRIME32_3 = 0xC2B2AE3DU; |
90 | | static const uint64_t PRIME64_1 = 0x9E3779B185EBCA87ULL; |
91 | | static const uint64_t PRIME64_2 = 0xC2B2AE3D27D4EB4FULL; |
92 | | static const uint64_t PRIME64_3 = 0x165667B19E3779F9ULL; |
93 | | static const uint64_t PRIME64_4 = 0x85EBCA77C2B2AE63ULL; |
94 | | static const uint64_t PRIME64_5 = 0x27D4EB2F165667C5ULL; |
95 | | static const uint64_t PRIME_MX1 = 0x165667919E3779F9ULL; |
96 | | static const uint64_t PRIME_MX2 = 0x9FB21C651E98DF25ULL; |
97 | | |
98 | | static const uint8_t secret[192] = { |
99 | | 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c, |
100 | | 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f, |
101 | | 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21, |
102 | | 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c, |
103 | | 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3, |
104 | | 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8, |
105 | | 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d, |
106 | | 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64, |
107 | | 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb, |
108 | | 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e, |
109 | | 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce, |
110 | | 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e, |
111 | | }; |
112 | | |
113 | | static uint64_t |
114 | 27.2k | avalanche(uint64_t x) { |
115 | 27.2k | x ^= (x >> 37); |
116 | 27.2k | x *= PRIME_MX1; |
117 | 27.2k | x ^= (x >> 32); |
118 | 27.2k | return x; |
119 | 27.2k | } |
120 | | |
121 | | static uint64_t |
122 | 5.41k | avalanche_XXH64(uint64_t x) { |
123 | 5.41k | x ^= (x >> 33); |
124 | 5.41k | x *= PRIME64_2; |
125 | 5.41k | x ^= (x >> 29); |
126 | 5.41k | x *= PRIME64_3; |
127 | 5.41k | x ^= (x >> 32); |
128 | 5.41k | return x; |
129 | 5.41k | } |
130 | | |
131 | | static uint64_t |
132 | 161 | XXH3_64_empty(void) { |
133 | 161 | return avalanche_XXH64(read64(secret + 56) ^ read64(secret + 64)); |
134 | 161 | } |
135 | | |
136 | | static uint64_t |
137 | 5.25k | XXH3_64_1to3(const uint8_t *input, size_t length) { |
138 | 5.25k | uint32_t combined = (uint32_t) input[length - 1] | ((uint32_t) length << 8) | ((uint32_t) input[0] << 16) | ((uint32_t) input[length >> 1] << 24); |
139 | 5.25k | return avalanche_XXH64((uint64_t) (read32(secret) ^ read32(secret + 4)) ^ (uint64_t) combined); |
140 | 5.25k | } |
141 | | |
142 | | static uint64_t |
143 | 73 | XXH3_64_4to8(const uint8_t *input, size_t length) { |
144 | 146 | #define rot(x_, r_) (((x_) << (r_)) | ((x_) >> (64 - (r_)))) |
145 | | |
146 | 73 | uint64_t combined = (uint64_t) read32(input + length - 4) | ((uint64_t) read32(input) << 32); |
147 | 73 | uint64_t value = (read64(secret + 8) ^ read64(secret + 16)) ^ combined; |
148 | 73 | value ^= rot(value, 49) ^ rot(value, 24); |
149 | 73 | value *= PRIME_MX2; |
150 | 73 | value ^= ((value >> 35) + length); |
151 | 73 | value *= PRIME_MX2; |
152 | 73 | value ^= (value >> 28); |
153 | 73 | return value; |
154 | | |
155 | 73 | #undef rot |
156 | 73 | } |
157 | | |
158 | | static uint64_t |
159 | 146 | XXH3_64_9to16(const uint8_t *input, size_t length) { |
160 | 146 | uint64_t lo = (read64(secret + 24) ^ read64(secret + 32)) ^ read64(input); |
161 | 146 | uint64_t hi = (read64(secret + 40) ^ read64(secret + 48)) ^ read64(input + length - 8); |
162 | 146 | return avalanche(length + pm_bswap64(lo) + hi + XXH3_mul128_fold64(lo, hi)); |
163 | 146 | } |
164 | | |
165 | | static uint64_t |
166 | 16.8k | XXH3_64_17to240(const uint8_t *input, size_t length) { |
167 | 68.6k | #define mix(data_, offset_) XXH3_mul128_fold64(read64((data_)) ^ read64(secret + (offset_)), read64((data_) + 8) ^ read64(secret + (offset_) + 8)) |
168 | | |
169 | 16.8k | uint64_t acc = length * PRIME64_1; |
170 | | |
171 | 16.8k | if (length <= 128) { Branch (171:9): [True: 14.4k, False: 2.38k]
|
172 | 14.4k | size_t idx = (length - 1) / 32; |
173 | 19.8k | do { |
174 | 19.8k | acc += mix(input + idx * 16, idx * 32) + mix(input + length - idx * 16 - 16, idx * 32 + 16); |
175 | 19.8k | } while (idx--); Branch (175:18): [True: 5.41k, False: 14.4k]
|
176 | 14.4k | } else { |
177 | 21.4k | for (size_t idx = 0; idx < 8; idx++19.0k ) { Branch (177:30): [True: 19.0k, False: 2.38k]
|
178 | 19.0k | acc += mix(input + idx * 16, idx * 16); |
179 | 19.0k | } |
180 | 2.38k | acc = avalanche(acc); |
181 | 9.88k | for (size_t idx = 8; idx < length >> 4; idx++7.50k ) { Branch (181:30): [True: 7.50k, False: 2.38k]
|
182 | 7.50k | acc += mix(input + idx * 16, (idx - 8) * 16 + 3); |
183 | 7.50k | } |
184 | 2.38k | acc += mix(input + length - 16, 119); |
185 | 2.38k | } |
186 | | |
187 | 16.8k | return avalanche(acc); |
188 | | |
189 | 16.8k | #undef mix |
190 | 16.8k | } |
191 | | |
192 | | static void |
193 | 56.2k | accumulate(uint64_t acc[8], const uint8_t *stripe, size_t secret_offset) { |
194 | 56.2k | #if defined(XXH3_NEON) |
195 | 281k | for (int idx = 0; idx < 8; idx += 2225k ) { Branch (195:23): [True: 225k, False: 56.2k]
|
196 | 225k | uint64x2_t data = vreinterpretq_u64_u8(vld1q_u8(stripe + 8 * idx)); |
197 | 225k | uint64x2_t value = veorq_u64(data, vreinterpretq_u64_u8(vld1q_u8(secret + secret_offset + 8 * idx))); |
198 | 225k | uint64x2_t product = vmull_u32(vmovn_u64(value), vshrn_n_u64(value, 32)); |
199 | 225k | vst1q_u64(acc + idx, vaddq_u64(vld1q_u64(acc + idx), vaddq_u64(vextq_u64(data, data, 1), product))); |
200 | 225k | } |
201 | | #elif defined(XXH3_SSE2) |
202 | | for (int idx = 0; idx < 8; idx += 2) { |
203 | | __m128i data = _mm_loadu_si128((const __m128i *) (stripe + 8 * idx)); |
204 | | __m128i value = _mm_xor_si128(data, _mm_loadu_si128((const __m128i *) (secret + secret_offset + 8 * idx))); |
205 | | __m128i product = _mm_mul_epu32(value, _mm_shuffle_epi32(value, _MM_SHUFFLE(0, 3, 0, 1))); |
206 | | _mm_storeu_si128((__m128i *) (acc + idx), _mm_add_epi64(_mm_loadu_si128((const __m128i *) (acc + idx)), _mm_add_epi64(_mm_shuffle_epi32(data, _MM_SHUFFLE(1, 0, 3, 2)), product))); |
207 | | } |
208 | | #else |
209 | | for (int idx = 0; idx < 8; idx++) { |
210 | | uint64_t data = read64(stripe + 8 * idx); |
211 | | uint64_t value = data ^ read64(secret + secret_offset + 8 * idx); |
212 | | acc[idx ^ 1] = acc[idx ^ 1] + data; |
213 | | acc[idx] += (value & 0xFFFFFFFF) * (value >> 32); |
214 | | } |
215 | | #endif |
216 | 56.2k | } |
217 | | |
218 | | static uint64_t |
219 | 7.88k | XXH3_64_large(const uint8_t *input, size_t length) { |
220 | 7.88k | const size_t secret_length = sizeof(secret); |
221 | 7.88k | const size_t stripes_per_block = (secret_length - 64) / 8; |
222 | 7.88k | const size_t block_size = 64 * stripes_per_block; |
223 | 7.88k | const size_t block_count = (length - 1) / block_size; |
224 | 7.88k | const size_t last_offset = block_count * block_size; |
225 | | |
226 | 7.88k | uint64_t acc[8] = { |
227 | 7.88k | PRIME32_3, PRIME64_1, PRIME64_2, PRIME64_3, |
228 | 7.88k | PRIME64_4, PRIME32_2, PRIME64_5, PRIME32_1 |
229 | 7.88k | }; |
230 | | |
231 | 7.88k | for (size_t block_idx = 0; block_idx < block_count; block_idx++0 ) { Branch (231:32): [True: 0, False: 7.88k]
|
232 | 0 | const uint8_t *block = input + block_idx * block_size; |
233 | |
|
234 | 0 | for (size_t stripe_idx = 0; stripe_idx < stripes_per_block; stripe_idx++) { Branch (234:37): [True: 0, False: 0]
|
235 | 0 | accumulate(acc, block + stripe_idx * 64, stripe_idx * 8); |
236 | 0 | } |
237 | |
|
238 | 0 | #if defined(XXH3_NEON) |
239 | 0 | const uint32x2_t prime = vdup_n_u32((uint32_t) PRIME32_1); |
240 | 0 | for (size_t idx = 0; idx < 8; idx += 2) { Branch (240:30): [True: 0, False: 0]
|
241 | 0 | uint64x2_t value = vld1q_u64(acc + idx); |
242 | 0 | value = veorq_u64(value, vshrq_n_u64(value, 47)); |
243 | 0 | value = veorq_u64(value, vreinterpretq_u64_u8(vld1q_u8(secret + secret_length - 64 + idx * 8))); |
244 | 0 | vst1q_u64(acc + idx, vaddq_u64(vmull_u32(vmovn_u64(value), prime), vshlq_n_u64(vmull_u32(vshrn_n_u64(value, 32), prime), 32))); |
245 | 0 | } |
246 | | #elif defined(XXH3_SSE2) |
247 | | const __m128i prime = _mm_set1_epi32((int) (uint32_t) PRIME32_1); |
248 | | for (size_t idx = 0; idx < 8; idx += 2) { |
249 | | __m128i value = _mm_loadu_si128((const __m128i *) (acc + idx)); |
250 | | value = _mm_xor_si128(value, _mm_srli_epi64(value, 47)); |
251 | | value = _mm_xor_si128(value, _mm_loadu_si128((const __m128i *) (secret + secret_length - 64 + idx * 8))); |
252 | | _mm_storeu_si128((__m128i *) (acc + idx), _mm_add_epi64(_mm_mul_epu32(value, prime), _mm_slli_epi64(_mm_mul_epu32(_mm_shuffle_epi32(value, _MM_SHUFFLE(0, 3, 0, 1)), prime), 32))); |
253 | | } |
254 | | #else |
255 | | for (size_t idx = 0; idx < 8; idx++) { |
256 | | acc[idx] ^= (acc[idx] >> 47); |
257 | | acc[idx] ^= read64(secret + secret_length - 64 + idx * 8); |
258 | | acc[idx] *= PRIME32_1; |
259 | | } |
260 | | #endif |
261 | 0 | } |
262 | | |
263 | 56.2k | for (size_t stripe_idx = 0; stripe_idx < (length - last_offset - 1) / 64; stripe_idx++48.4k ) { Branch (263:33): [True: 48.4k, False: 7.88k]
|
264 | 48.4k | accumulate(acc, input + last_offset + stripe_idx * 64, stripe_idx * 8); |
265 | 48.4k | } |
266 | | |
267 | 7.88k | accumulate(acc, input + length - 64, secret_length - 71); |
268 | | |
269 | 7.88k | uint64_t result = (uint64_t) length * PRIME64_1; |
270 | 39.4k | for (size_t idx = 0; idx < 4; idx++31.5k ) { Branch (270:26): [True: 31.5k, False: 7.88k]
|
271 | 31.5k | result += XXH3_mul128_fold64(acc[idx * 2] ^ read64(secret + 11 + 16 * idx), acc[idx * 2 + 1] ^ read64(secret + 11 + 16 * idx + 8)); |
272 | 31.5k | } |
273 | | |
274 | 7.88k | return avalanche(result); |
275 | 7.88k | } |
276 | | |
277 | | uint64_t |
278 | 30.3k | pm_xxh3(const uint8_t *input, size_t length) { |
279 | 30.3k | if (length == 0) { Branch (279:9): [True: 161, False: 30.1k]
|
280 | 161 | return XXH3_64_empty(); |
281 | 30.1k | } else if (length <= 3) { Branch (281:16): [True: 5.25k, False: 24.9k]
|
282 | 5.25k | return XXH3_64_1to3(input, length); |
283 | 24.9k | } else if (length <= 8) { Branch (283:16): [True: 73, False: 24.8k]
|
284 | 73 | return XXH3_64_4to8(input, length); |
285 | 24.8k | } else if (length <= 16) { Branch (285:16): [True: 146, False: 24.7k]
|
286 | 146 | return XXH3_64_9to16(input, length); |
287 | 24.7k | } else if (length <= 240) { Branch (287:16): [True: 16.8k, False: 7.88k]
|
288 | 16.8k | return XXH3_64_17to240(input, length); |
289 | 16.8k | } else { |
290 | 7.88k | return XXH3_64_large(input, length); |
291 | 7.88k | } |
292 | 30.3k | } |