1 /*
2 * xxHash - Extremely Fast Hash algorithm
3 * Copyright (C) 2012-2016, Yann Collet.
4 *
5 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This program is free software; you can redistribute it and/or modify it under
31 * the terms of the GNU General Public License version 2 as published by the
32 * Free Software Foundation. This program is dual-licensed; you may select
33 * either version 2 of the GNU General Public License ("GPL") or BSD license
34 * ("BSD").
35 *
36 * You can contact the author at:
37 * - xxHash homepage: https://cyan4973.github.io/xxHash/
38 * - xxHash source repository: https://github.com/Cyan4973/xxHash
39 */
40
41 /*
42 * Notice extracted from xxHash homepage:
43 *
44 * xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
45 * It also successfully passes all tests from the SMHasher suite.
46 *
47 * Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2
48 * Duo @3GHz)
49 *
50 * Name Speed Q.Score Author
51 * xxHash 5.4 GB/s 10
52 * CrapWow 3.2 GB/s 2 Andrew
53 * MumurHash 3a 2.7 GB/s 10 Austin Appleby
54 * SpookyHash 2.0 GB/s 10 Bob Jenkins
55 * SBox 1.4 GB/s 9 Bret Mulvey
56 * Lookup3 1.2 GB/s 9 Bob Jenkins
57 * SuperFastHash 1.2 GB/s 1 Paul Hsieh
58 * CityHash64 1.05 GB/s 10 Pike & Alakuijala
59 * FNV 0.55 GB/s 5 Fowler, Noll, Vo
60 * CRC32 0.43 GB/s 9
61 * MD5-32 0.33 GB/s 10 Ronald L. Rivest
62 * SHA1-32 0.28 GB/s 10
63 *
64 * Q.Score is a measure of quality of the hash function.
65 * It depends on successfully passing SMHasher test set.
66 * 10 is a perfect score.
67 *
68 * A 64-bits version, named xxh64 offers much better speed,
69 * but for 64-bits applications only.
70 * Name Speed on 64 bits Speed on 32 bits
71 * xxh64 13.8 GB/s 1.9 GB/s
72 * xxh32 6.8 GB/s 6.0 GB/s
73 */
74
75 #ifndef XXHASH_H
76 #define XXHASH_H
77
78 #include <linux/types.h>
79
80 #define XXH_API static inline __attribute__((unused))
81 /*-****************************
82 * Simple Hash Functions
83 *****************************/
84
85 /**
86 * xxh32() - calculate the 32-bit hash of the input with a given seed.
87 *
88 * @input: The data to hash.
89 * @length: The length of the data to hash.
90 * @seed: The seed can be used to alter the result predictably.
91 *
92 * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
93 *
94 * Return: The 32-bit hash of the data.
95 */
96 XXH_API uint32_t xxh32(const void *input, size_t length, uint32_t seed);
97
98 /**
99 * xxh64() - calculate the 64-bit hash of the input with a given seed.
100 *
101 * @input: The data to hash.
102 * @length: The length of the data to hash.
103 * @seed: The seed can be used to alter the result predictably.
104 *
105 * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
106 *
107 * Return: The 64-bit hash of the data.
108 */
109 XXH_API uint64_t xxh64(const void *input, size_t length, uint64_t seed);
110
111 /**
112 * xxhash() - calculate wordsize hash of the input with a given seed
113 * @input: The data to hash.
114 * @length: The length of the data to hash.
115 * @seed: The seed can be used to alter the result predictably.
116 *
117 * If the hash does not need to be comparable between machines with
118 * different word sizes, this function will call whichever of xxh32()
119 * or xxh64() is faster.
120 *
121 * Return: wordsize hash of the data.
122 */
123
xxhash(const void * input,size_t length,uint64_t seed)124 static inline unsigned long xxhash(const void *input, size_t length,
125 uint64_t seed)
126 {
127 #if BITS_PER_LONG == 64
128 return xxh64(input, length, seed);
129 #else
130 return xxh32(input, length, seed);
131 #endif
132 }
133
134 /*-****************************
135 * Streaming Hash Functions
136 *****************************/
137
138 /*
139 * These definitions are only meant to allow allocation of XXH state
140 * statically, on stack, or in a struct for example.
141 * Do not use members directly.
142 */
143
144 /**
145 * struct xxh32_state - private xxh32 state, do not use members directly
146 */
147 struct xxh32_state {
148 uint32_t total_len_32;
149 uint32_t large_len;
150 uint32_t v1;
151 uint32_t v2;
152 uint32_t v3;
153 uint32_t v4;
154 uint32_t mem32[4];
155 uint32_t memsize;
156 };
157
158 /**
159 * struct xxh32_state - private xxh64 state, do not use members directly
160 */
161 struct xxh64_state {
162 uint64_t total_len;
163 uint64_t v1;
164 uint64_t v2;
165 uint64_t v3;
166 uint64_t v4;
167 uint64_t mem64[4];
168 uint32_t memsize;
169 };
170
171 /**
172 * xxh32_reset() - reset the xxh32 state to start a new hashing operation
173 *
174 * @state: The xxh32 state to reset.
175 * @seed: Initialize the hash state with this seed.
176 *
177 * Call this function on any xxh32_state to prepare for a new hashing operation.
178 */
179 XXH_API void xxh32_reset(struct xxh32_state *state, uint32_t seed);
180
181 /**
182 * xxh32_update() - hash the data given and update the xxh32 state
183 *
184 * @state: The xxh32 state to update.
185 * @input: The data to hash.
186 * @length: The length of the data to hash.
187 *
188 * After calling xxh32_reset() call xxh32_update() as many times as necessary.
189 *
190 * Return: Zero on success, otherwise an error code.
191 */
192 XXH_API int xxh32_update(struct xxh32_state *state, const void *input, size_t length);
193
194 /**
195 * xxh32_digest() - produce the current xxh32 hash
196 *
197 * @state: Produce the current xxh32 hash of this state.
198 *
199 * A hash value can be produced at any time. It is still possible to continue
200 * inserting input into the hash state after a call to xxh32_digest(), and
201 * generate new hashes later on, by calling xxh32_digest() again.
202 *
203 * Return: The xxh32 hash stored in the state.
204 */
205 XXH_API uint32_t xxh32_digest(const struct xxh32_state *state);
206
207 /**
208 * xxh64_reset() - reset the xxh64 state to start a new hashing operation
209 *
210 * @state: The xxh64 state to reset.
211 * @seed: Initialize the hash state with this seed.
212 */
213 XXH_API void xxh64_reset(struct xxh64_state *state, uint64_t seed);
214
215 /**
216 * xxh64_update() - hash the data given and update the xxh64 state
217 * @state: The xxh64 state to update.
218 * @input: The data to hash.
219 * @length: The length of the data to hash.
220 *
221 * After calling xxh64_reset() call xxh64_update() as many times as necessary.
222 *
223 * Return: Zero on success, otherwise an error code.
224 */
225 XXH_API int xxh64_update(struct xxh64_state *state, const void *input, size_t length);
226
227 /**
228 * xxh64_digest() - produce the current xxh64 hash
229 *
230 * @state: Produce the current xxh64 hash of this state.
231 *
232 * A hash value can be produced at any time. It is still possible to continue
233 * inserting input into the hash state after a call to xxh64_digest(), and
234 * generate new hashes later on, by calling xxh64_digest() again.
235 *
236 * Return: The xxh64 hash stored in the state.
237 */
238 XXH_API uint64_t xxh64_digest(const struct xxh64_state *state);
239
240 /*-**************************
241 * Utils
242 ***************************/
243
244 /**
245 * xxh32_copy_state() - copy the source state into the destination state
246 *
247 * @src: The source xxh32 state.
248 * @dst: The destination xxh32 state.
249 */
250 XXH_API void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src);
251
252 /**
253 * xxh64_copy_state() - copy the source state into the destination state
254 *
255 * @src: The source xxh64 state.
256 * @dst: The destination xxh64 state.
257 */
258 XXH_API void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src);
259
260 /*
261 * xxHash - Extremely Fast Hash algorithm
262 * Copyright (C) 2012-2016, Yann Collet.
263 *
264 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
265 *
266 * Redistribution and use in source and binary forms, with or without
267 * modification, are permitted provided that the following conditions are
268 * met:
269 *
270 * * Redistributions of source code must retain the above copyright
271 * notice, this list of conditions and the following disclaimer.
272 * * Redistributions in binary form must reproduce the above
273 * copyright notice, this list of conditions and the following disclaimer
274 * in the documentation and/or other materials provided with the
275 * distribution.
276 *
277 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
278 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
279 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
280 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
281 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
282 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
283 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
284 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
285 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
286 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
287 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288 *
289 * This program is free software; you can redistribute it and/or modify it under
290 * the terms of the GNU General Public License version 2 as published by the
291 * Free Software Foundation. This program is dual-licensed; you may select
292 * either version 2 of the GNU General Public License ("GPL") or BSD license
293 * ("BSD").
294 *
295 * You can contact the author at:
296 * - xxHash homepage: https://cyan4973.github.io/xxHash/
297 * - xxHash source repository: https://github.com/Cyan4973/xxHash
298 */
299
300 #include <asm/unaligned.h>
301 #include <linux/errno.h>
302 #include <linux/kernel.h>
303 #include <linux/module.h>
304 #include <linux/xxhash.h>
305
306 /*-*************************************
307 * Macros
308 **************************************/
309 #define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r)))
310 #define xxh_rotl64(x, r) ((x << r) | (x >> (64 - r)))
311
312 #ifdef __LITTLE_ENDIAN
313 # define XXH_CPU_LITTLE_ENDIAN 1
314 #else
315 # define XXH_CPU_LITTLE_ENDIAN 0
316 #endif
317
318 /*-*************************************
319 * Constants
320 **************************************/
321 static const uint32_t PRIME32_1 = 2654435761U;
322 static const uint32_t PRIME32_2 = 2246822519U;
323 static const uint32_t PRIME32_3 = 3266489917U;
324 static const uint32_t PRIME32_4 = 668265263U;
325 static const uint32_t PRIME32_5 = 374761393U;
326
327 static const uint64_t PRIME64_1 = 11400714785074694791ULL;
328 static const uint64_t PRIME64_2 = 14029467366897019727ULL;
329 static const uint64_t PRIME64_3 = 1609587929392839161ULL;
330 static const uint64_t PRIME64_4 = 9650029242287828579ULL;
331 static const uint64_t PRIME64_5 = 2870177450012600261ULL;
332
333 /*-**************************
334 * Utils
335 ***************************/
xxh32_copy_state(struct xxh32_state * dst,const struct xxh32_state * src)336 XXH_API void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src)
337 {
338 __builtin_memcpy(dst, src, sizeof(*dst));
339 }
340
xxh64_copy_state(struct xxh64_state * dst,const struct xxh64_state * src)341 XXH_API void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src)
342 {
343 __builtin_memcpy(dst, src, sizeof(*dst));
344 }
345
346 /*-***************************
347 * Simple Hash Functions
348 ****************************/
xxh32_round(uint32_t seed,const uint32_t input)349 static uint32_t xxh32_round(uint32_t seed, const uint32_t input)
350 {
351 seed += input * PRIME32_2;
352 seed = xxh_rotl32(seed, 13);
353 seed *= PRIME32_1;
354 return seed;
355 }
356
xxh32(const void * input,const size_t len,const uint32_t seed)357 XXH_API uint32_t xxh32(const void *input, const size_t len, const uint32_t seed)
358 {
359 const uint8_t *p = (const uint8_t *)input;
360 const uint8_t *b_end = p + len;
361 uint32_t h32;
362
363 if (len >= 16) {
364 const uint8_t *const limit = b_end - 16;
365 uint32_t v1 = seed + PRIME32_1 + PRIME32_2;
366 uint32_t v2 = seed + PRIME32_2;
367 uint32_t v3 = seed + 0;
368 uint32_t v4 = seed - PRIME32_1;
369
370 do {
371 v1 = xxh32_round(v1, get_unaligned_le32(p));
372 p += 4;
373 v2 = xxh32_round(v2, get_unaligned_le32(p));
374 p += 4;
375 v3 = xxh32_round(v3, get_unaligned_le32(p));
376 p += 4;
377 v4 = xxh32_round(v4, get_unaligned_le32(p));
378 p += 4;
379 } while (p <= limit);
380
381 h32 = xxh_rotl32(v1, 1) + xxh_rotl32(v2, 7) +
382 xxh_rotl32(v3, 12) + xxh_rotl32(v4, 18);
383 } else {
384 h32 = seed + PRIME32_5;
385 }
386
387 h32 += (uint32_t)len;
388
389 while (p + 4 <= b_end) {
390 h32 += get_unaligned_le32(p) * PRIME32_3;
391 h32 = xxh_rotl32(h32, 17) * PRIME32_4;
392 p += 4;
393 }
394
395 while (p < b_end) {
396 h32 += (*p) * PRIME32_5;
397 h32 = xxh_rotl32(h32, 11) * PRIME32_1;
398 p++;
399 }
400
401 h32 ^= h32 >> 15;
402 h32 *= PRIME32_2;
403 h32 ^= h32 >> 13;
404 h32 *= PRIME32_3;
405 h32 ^= h32 >> 16;
406
407 return h32;
408 }
409
xxh64_round(uint64_t acc,const uint64_t input)410 static uint64_t xxh64_round(uint64_t acc, const uint64_t input)
411 {
412 acc += input * PRIME64_2;
413 acc = xxh_rotl64(acc, 31);
414 acc *= PRIME64_1;
415 return acc;
416 }
417
xxh64_merge_round(uint64_t acc,uint64_t val)418 static uint64_t xxh64_merge_round(uint64_t acc, uint64_t val)
419 {
420 val = xxh64_round(0, val);
421 acc ^= val;
422 acc = acc * PRIME64_1 + PRIME64_4;
423 return acc;
424 }
425
xxh64(const void * input,const size_t len,const uint64_t seed)426 XXH_API uint64_t xxh64(const void *input, const size_t len, const uint64_t seed)
427 {
428 const uint8_t *p = (const uint8_t *)input;
429 const uint8_t *const b_end = p + len;
430 uint64_t h64;
431
432 if (len >= 32) {
433 const uint8_t *const limit = b_end - 32;
434 uint64_t v1 = seed + PRIME64_1 + PRIME64_2;
435 uint64_t v2 = seed + PRIME64_2;
436 uint64_t v3 = seed + 0;
437 uint64_t v4 = seed - PRIME64_1;
438
439 do {
440 v1 = xxh64_round(v1, get_unaligned_le64(p));
441 p += 8;
442 v2 = xxh64_round(v2, get_unaligned_le64(p));
443 p += 8;
444 v3 = xxh64_round(v3, get_unaligned_le64(p));
445 p += 8;
446 v4 = xxh64_round(v4, get_unaligned_le64(p));
447 p += 8;
448 } while (p <= limit);
449
450 h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
451 xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
452 h64 = xxh64_merge_round(h64, v1);
453 h64 = xxh64_merge_round(h64, v2);
454 h64 = xxh64_merge_round(h64, v3);
455 h64 = xxh64_merge_round(h64, v4);
456
457 } else {
458 h64 = seed + PRIME64_5;
459 }
460
461 h64 += (uint64_t)len;
462
463 while (p + 8 <= b_end) {
464 const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
465
466 h64 ^= k1;
467 h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
468 p += 8;
469 }
470
471 if (p + 4 <= b_end) {
472 h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
473 h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
474 p += 4;
475 }
476
477 while (p < b_end) {
478 h64 ^= (*p) * PRIME64_5;
479 h64 = xxh_rotl64(h64, 11) * PRIME64_1;
480 p++;
481 }
482
483 h64 ^= h64 >> 33;
484 h64 *= PRIME64_2;
485 h64 ^= h64 >> 29;
486 h64 *= PRIME64_3;
487 h64 ^= h64 >> 32;
488
489 return h64;
490 }
491
492 /*-**************************************************
493 * Advanced Hash Functions
494 ***************************************************/
xxh32_reset(struct xxh32_state * statePtr,const uint32_t seed)495 XXH_API void xxh32_reset(struct xxh32_state *statePtr, const uint32_t seed)
496 {
497 /* use a local state for memcpy() to avoid strict-aliasing warnings */
498 struct xxh32_state state;
499
500 __builtin_memset(&state, 0, sizeof(state));
501 state.v1 = seed + PRIME32_1 + PRIME32_2;
502 state.v2 = seed + PRIME32_2;
503 state.v3 = seed + 0;
504 state.v4 = seed - PRIME32_1;
505 __builtin_memcpy(statePtr, &state, sizeof(state));
506 }
507
xxh64_reset(struct xxh64_state * statePtr,const uint64_t seed)508 XXH_API void xxh64_reset(struct xxh64_state *statePtr, const uint64_t seed)
509 {
510 /* use a local state for memcpy() to avoid strict-aliasing warnings */
511 struct xxh64_state state;
512
513 __builtin_memset(&state, 0, sizeof(state));
514 state.v1 = seed + PRIME64_1 + PRIME64_2;
515 state.v2 = seed + PRIME64_2;
516 state.v3 = seed + 0;
517 state.v4 = seed - PRIME64_1;
518 __builtin_memcpy(statePtr, &state, sizeof(state));
519 }
520
xxh32_update(struct xxh32_state * state,const void * input,const size_t len)521 XXH_API int xxh32_update(struct xxh32_state *state, const void *input, const size_t len)
522 {
523 const uint8_t *p = (const uint8_t *)input;
524 const uint8_t *const b_end = p + len;
525
526 if (input == NULL)
527 return -EINVAL;
528
529 state->total_len_32 += (uint32_t)len;
530 state->large_len |= (len >= 16) | (state->total_len_32 >= 16);
531
532 if (state->memsize + len < 16) { /* fill in tmp buffer */
533 __builtin_memcpy((uint8_t *)(state->mem32) + state->memsize, input, len);
534 state->memsize += (uint32_t)len;
535 return 0;
536 }
537
538 if (state->memsize) { /* some data left from previous update */
539 const uint32_t *p32 = state->mem32;
540
541 __builtin_memcpy((uint8_t *)(state->mem32) + state->memsize, input,
542 16 - state->memsize);
543
544 state->v1 = xxh32_round(state->v1, get_unaligned_le32(p32));
545 p32++;
546 state->v2 = xxh32_round(state->v2, get_unaligned_le32(p32));
547 p32++;
548 state->v3 = xxh32_round(state->v3, get_unaligned_le32(p32));
549 p32++;
550 state->v4 = xxh32_round(state->v4, get_unaligned_le32(p32));
551 p32++;
552
553 p += 16-state->memsize;
554 state->memsize = 0;
555 }
556
557 if (p <= b_end - 16) {
558 const uint8_t *const limit = b_end - 16;
559 uint32_t v1 = state->v1;
560 uint32_t v2 = state->v2;
561 uint32_t v3 = state->v3;
562 uint32_t v4 = state->v4;
563
564 do {
565 v1 = xxh32_round(v1, get_unaligned_le32(p));
566 p += 4;
567 v2 = xxh32_round(v2, get_unaligned_le32(p));
568 p += 4;
569 v3 = xxh32_round(v3, get_unaligned_le32(p));
570 p += 4;
571 v4 = xxh32_round(v4, get_unaligned_le32(p));
572 p += 4;
573 } while (p <= limit);
574
575 state->v1 = v1;
576 state->v2 = v2;
577 state->v3 = v3;
578 state->v4 = v4;
579 }
580
581 if (p < b_end) {
582 __builtin_memcpy(state->mem32, p, (size_t)(b_end-p));
583 state->memsize = (uint32_t)(b_end-p);
584 }
585
586 return 0;
587 }
588
xxh32_digest(const struct xxh32_state * state)589 XXH_API uint32_t xxh32_digest(const struct xxh32_state *state)
590 {
591 const uint8_t *p = (const uint8_t *)state->mem32;
592 const uint8_t *const b_end = (const uint8_t *)(state->mem32) +
593 state->memsize;
594 uint32_t h32;
595
596 if (state->large_len) {
597 h32 = xxh_rotl32(state->v1, 1) + xxh_rotl32(state->v2, 7) +
598 xxh_rotl32(state->v3, 12) + xxh_rotl32(state->v4, 18);
599 } else {
600 h32 = state->v3 /* == seed */ + PRIME32_5;
601 }
602
603 h32 += state->total_len_32;
604
605 while (p + 4 <= b_end) {
606 h32 += get_unaligned_le32(p) * PRIME32_3;
607 h32 = xxh_rotl32(h32, 17) * PRIME32_4;
608 p += 4;
609 }
610
611 while (p < b_end) {
612 h32 += (*p) * PRIME32_5;
613 h32 = xxh_rotl32(h32, 11) * PRIME32_1;
614 p++;
615 }
616
617 h32 ^= h32 >> 15;
618 h32 *= PRIME32_2;
619 h32 ^= h32 >> 13;
620 h32 *= PRIME32_3;
621 h32 ^= h32 >> 16;
622
623 return h32;
624 }
625
xxh64_update(struct xxh64_state * state,const void * input,const size_t len)626 XXH_API int xxh64_update(struct xxh64_state *state, const void *input, const size_t len)
627 {
628 const uint8_t *p = (const uint8_t *)input;
629 const uint8_t *const b_end = p + len;
630
631 if (input == NULL)
632 return -EINVAL;
633
634 state->total_len += len;
635
636 if (state->memsize + len < 32) { /* fill in tmp buffer */
637 __builtin_memcpy(((uint8_t *)state->mem64) + state->memsize, input, len);
638 state->memsize += (uint32_t)len;
639 return 0;
640 }
641
642 if (state->memsize) { /* tmp buffer is full */
643 uint64_t *p64 = state->mem64;
644
645 __builtin_memcpy(((uint8_t *)p64) + state->memsize, input,
646 32 - state->memsize);
647
648 state->v1 = xxh64_round(state->v1, get_unaligned_le64(p64));
649 p64++;
650 state->v2 = xxh64_round(state->v2, get_unaligned_le64(p64));
651 p64++;
652 state->v3 = xxh64_round(state->v3, get_unaligned_le64(p64));
653 p64++;
654 state->v4 = xxh64_round(state->v4, get_unaligned_le64(p64));
655
656 p += 32 - state->memsize;
657 state->memsize = 0;
658 }
659
660 if (p + 32 <= b_end) {
661 const uint8_t *const limit = b_end - 32;
662 uint64_t v1 = state->v1;
663 uint64_t v2 = state->v2;
664 uint64_t v3 = state->v3;
665 uint64_t v4 = state->v4;
666
667 do {
668 v1 = xxh64_round(v1, get_unaligned_le64(p));
669 p += 8;
670 v2 = xxh64_round(v2, get_unaligned_le64(p));
671 p += 8;
672 v3 = xxh64_round(v3, get_unaligned_le64(p));
673 p += 8;
674 v4 = xxh64_round(v4, get_unaligned_le64(p));
675 p += 8;
676 } while (p <= limit);
677
678 state->v1 = v1;
679 state->v2 = v2;
680 state->v3 = v3;
681 state->v4 = v4;
682 }
683
684 if (p < b_end) {
685 __builtin_memcpy(state->mem64, p, (size_t)(b_end-p));
686 state->memsize = (uint32_t)(b_end - p);
687 }
688
689 return 0;
690 }
691
xxh64_digest(const struct xxh64_state * state)692 XXH_API uint64_t xxh64_digest(const struct xxh64_state *state)
693 {
694 const uint8_t *p = (const uint8_t *)state->mem64;
695 const uint8_t *const b_end = (const uint8_t *)state->mem64 +
696 state->memsize;
697 uint64_t h64;
698
699 if (state->total_len >= 32) {
700 const uint64_t v1 = state->v1;
701 const uint64_t v2 = state->v2;
702 const uint64_t v3 = state->v3;
703 const uint64_t v4 = state->v4;
704
705 h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
706 xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
707 h64 = xxh64_merge_round(h64, v1);
708 h64 = xxh64_merge_round(h64, v2);
709 h64 = xxh64_merge_round(h64, v3);
710 h64 = xxh64_merge_round(h64, v4);
711 } else {
712 h64 = state->v3 + PRIME64_5;
713 }
714
715 h64 += (uint64_t)state->total_len;
716
717 while (p + 8 <= b_end) {
718 const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
719
720 h64 ^= k1;
721 h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
722 p += 8;
723 }
724
725 if (p + 4 <= b_end) {
726 h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
727 h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
728 p += 4;
729 }
730
731 while (p < b_end) {
732 h64 ^= (*p) * PRIME64_5;
733 h64 = xxh_rotl64(h64, 11) * PRIME64_1;
734 p++;
735 }
736
737 h64 ^= h64 >> 33;
738 h64 *= PRIME64_2;
739 h64 ^= h64 >> 29;
740 h64 *= PRIME64_3;
741 h64 ^= h64 >> 32;
742
743 return h64;
744 }
745
746 #endif /* XXHASH_H */
747