2 * xxHash - Extremely Fast Hash algorithm
3 * Copyright (C) 2012-2016, Yann Collet.
5 * BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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
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.
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
36 * You can contact the author at:
37 * - xxHash homepage: https://cyan4973.github.io/xxHash/
38 * - xxHash source repository: https://github.com/Cyan4973/xxHash
42 * Notice extracted from xxHash homepage:
44 * xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
45 * It also successfully passes all tests from the SMHasher suite.
47 * Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2
50 * Name Speed Q.Score Author
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
61 * MD5-32 0.33 GB/s 10 Ronald L. Rivest
62 * SHA1-32 0.28 GB/s 10
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.
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
78 #include <linux/types.h>
80 #define XXH_API static inline __attribute__((unused))
81 /*-****************************
82 * Simple Hash Functions
83 *****************************/
86 * xxh32() - calculate the 32-bit hash of the input with a given seed.
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.
92 * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
94 * Return: The 32-bit hash of the data.
96 XXH_API uint32_t xxh32(const void *input, size_t length, uint32_t seed);
99 * xxh64() - calculate the 64-bit hash of the input with a given seed.
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.
105 * This function runs 2x faster on 64-bit systems, but slower on 32-bit systems.
107 * Return: The 64-bit hash of the data.
109 XXH_API uint64_t xxh64(const void *input, size_t length, uint64_t seed);
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.
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.
121 * Return: wordsize hash of the data.
124 static inline unsigned long xxhash(const void *input, size_t length,
127 if (sizeof(size_t) == 8)
128 return xxh64(input, length, seed);
130 return xxh32(input, length, seed);
133 /*-****************************
134 * Streaming Hash Functions
135 *****************************/
138 * These definitions are only meant to allow allocation of XXH state
139 * statically, on stack, or in a struct for example.
140 * Do not use members directly.
144 * struct xxh32_state - private xxh32 state, do not use members directly
147 uint32_t total_len_32;
158 * struct xxh32_state - private xxh64 state, do not use members directly
171 * xxh32_reset() - reset the xxh32 state to start a new hashing operation
173 * @state: The xxh32 state to reset.
174 * @seed: Initialize the hash state with this seed.
176 * Call this function on any xxh32_state to prepare for a new hashing operation.
178 XXH_API void xxh32_reset(struct xxh32_state *state, uint32_t seed);
181 * xxh32_update() - hash the data given and update the xxh32 state
183 * @state: The xxh32 state to update.
184 * @input: The data to hash.
185 * @length: The length of the data to hash.
187 * After calling xxh32_reset() call xxh32_update() as many times as necessary.
189 * Return: Zero on success, otherwise an error code.
191 XXH_API int xxh32_update(struct xxh32_state *state, const void *input, size_t length);
194 * xxh32_digest() - produce the current xxh32 hash
196 * @state: Produce the current xxh32 hash of this state.
198 * A hash value can be produced at any time. It is still possible to continue
199 * inserting input into the hash state after a call to xxh32_digest(), and
200 * generate new hashes later on, by calling xxh32_digest() again.
202 * Return: The xxh32 hash stored in the state.
204 XXH_API uint32_t xxh32_digest(const struct xxh32_state *state);
207 * xxh64_reset() - reset the xxh64 state to start a new hashing operation
209 * @state: The xxh64 state to reset.
210 * @seed: Initialize the hash state with this seed.
212 XXH_API void xxh64_reset(struct xxh64_state *state, uint64_t seed);
215 * xxh64_update() - hash the data given and update the xxh64 state
216 * @state: The xxh64 state to update.
217 * @input: The data to hash.
218 * @length: The length of the data to hash.
220 * After calling xxh64_reset() call xxh64_update() as many times as necessary.
222 * Return: Zero on success, otherwise an error code.
224 XXH_API int xxh64_update(struct xxh64_state *state, const void *input, size_t length);
227 * xxh64_digest() - produce the current xxh64 hash
229 * @state: Produce the current xxh64 hash of this state.
231 * A hash value can be produced at any time. It is still possible to continue
232 * inserting input into the hash state after a call to xxh64_digest(), and
233 * generate new hashes later on, by calling xxh64_digest() again.
235 * Return: The xxh64 hash stored in the state.
237 XXH_API uint64_t xxh64_digest(const struct xxh64_state *state);
239 /*-**************************
241 ***************************/
244 * xxh32_copy_state() - copy the source state into the destination state
246 * @src: The source xxh32 state.
247 * @dst: The destination xxh32 state.
249 XXH_API void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src);
252 * xxh64_copy_state() - copy the source state into the destination state
254 * @src: The source xxh64 state.
255 * @dst: The destination xxh64 state.
257 XXH_API void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src);
260 * xxHash - Extremely Fast Hash algorithm
261 * Copyright (C) 2012-2016, Yann Collet.
263 * BSD 2-Clause License (https://opensource.org/licenses/bsd-license.php)
265 * Redistribution and use in source and binary forms, with or without
266 * modification, are permitted provided that the following conditions are
269 * * Redistributions of source code must retain the above copyright
270 * notice, this list of conditions and the following disclaimer.
271 * * Redistributions in binary form must reproduce the above
272 * copyright notice, this list of conditions and the following disclaimer
273 * in the documentation and/or other materials provided with the
276 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
277 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
278 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
279 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
280 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
281 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
282 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
283 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
284 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
286 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288 * This program is free software; you can redistribute it and/or modify it under
289 * the terms of the GNU General Public License version 2 as published by the
290 * Free Software Foundation. This program is dual-licensed; you may select
291 * either version 2 of the GNU General Public License ("GPL") or BSD license
294 * You can contact the author at:
295 * - xxHash homepage: https://cyan4973.github.io/xxHash/
296 * - xxHash source repository: https://github.com/Cyan4973/xxHash
299 #include <asm/unaligned.h>
300 #include <linux/errno.h>
301 #include <linux/kernel.h>
302 #include <linux/module.h>
303 #include <linux/xxhash.h>
305 /*-*************************************
307 **************************************/
308 #define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r)))
309 #define xxh_rotl64(x, r) ((x << r) | (x >> (64 - r)))
311 #ifdef __LITTLE_ENDIAN
312 # define XXH_CPU_LITTLE_ENDIAN 1
314 # define XXH_CPU_LITTLE_ENDIAN 0
317 /*-*************************************
319 **************************************/
320 static const uint32_t PRIME32_1 = 2654435761U;
321 static const uint32_t PRIME32_2 = 2246822519U;
322 static const uint32_t PRIME32_3 = 3266489917U;
323 static const uint32_t PRIME32_4 = 668265263U;
324 static const uint32_t PRIME32_5 = 374761393U;
326 static const uint64_t PRIME64_1 = 11400714785074694791ULL;
327 static const uint64_t PRIME64_2 = 14029467366897019727ULL;
328 static const uint64_t PRIME64_3 = 1609587929392839161ULL;
329 static const uint64_t PRIME64_4 = 9650029242287828579ULL;
330 static const uint64_t PRIME64_5 = 2870177450012600261ULL;
332 /*-**************************
334 ***************************/
335 XXH_API void xxh32_copy_state(struct xxh32_state *dst, const struct xxh32_state *src)
337 __builtin_memcpy(dst, src, sizeof(*dst));
340 XXH_API void xxh64_copy_state(struct xxh64_state *dst, const struct xxh64_state *src)
342 __builtin_memcpy(dst, src, sizeof(*dst));
345 /*-***************************
346 * Simple Hash Functions
347 ****************************/
348 static uint32_t xxh32_round(uint32_t seed, const uint32_t input)
350 seed += input * PRIME32_2;
351 seed = xxh_rotl32(seed, 13);
356 XXH_API uint32_t xxh32(const void *input, const size_t len, const uint32_t seed)
358 const uint8_t *p = (const uint8_t *)input;
359 const uint8_t *b_end = p + len;
363 const uint8_t *const limit = b_end - 16;
364 uint32_t v1 = seed + PRIME32_1 + PRIME32_2;
365 uint32_t v2 = seed + PRIME32_2;
366 uint32_t v3 = seed + 0;
367 uint32_t v4 = seed - PRIME32_1;
370 v1 = xxh32_round(v1, get_unaligned_le32(p));
372 v2 = xxh32_round(v2, get_unaligned_le32(p));
374 v3 = xxh32_round(v3, get_unaligned_le32(p));
376 v4 = xxh32_round(v4, get_unaligned_le32(p));
378 } while (p <= limit);
380 h32 = xxh_rotl32(v1, 1) + xxh_rotl32(v2, 7) +
381 xxh_rotl32(v3, 12) + xxh_rotl32(v4, 18);
383 h32 = seed + PRIME32_5;
386 h32 += (uint32_t)len;
388 while (p + 4 <= b_end) {
389 h32 += get_unaligned_le32(p) * PRIME32_3;
390 h32 = xxh_rotl32(h32, 17) * PRIME32_4;
395 h32 += (*p) * PRIME32_5;
396 h32 = xxh_rotl32(h32, 11) * PRIME32_1;
409 static uint64_t xxh64_round(uint64_t acc, const uint64_t input)
411 acc += input * PRIME64_2;
412 acc = xxh_rotl64(acc, 31);
417 static uint64_t xxh64_merge_round(uint64_t acc, uint64_t val)
419 val = xxh64_round(0, val);
421 acc = acc * PRIME64_1 + PRIME64_4;
425 XXH_API uint64_t xxh64(const void *input, const size_t len, const uint64_t seed)
427 const uint8_t *p = (const uint8_t *)input;
428 const uint8_t *const b_end = p + len;
432 const uint8_t *const limit = b_end - 32;
433 uint64_t v1 = seed + PRIME64_1 + PRIME64_2;
434 uint64_t v2 = seed + PRIME64_2;
435 uint64_t v3 = seed + 0;
436 uint64_t v4 = seed - PRIME64_1;
439 v1 = xxh64_round(v1, get_unaligned_le64(p));
441 v2 = xxh64_round(v2, get_unaligned_le64(p));
443 v3 = xxh64_round(v3, get_unaligned_le64(p));
445 v4 = xxh64_round(v4, get_unaligned_le64(p));
447 } while (p <= limit);
449 h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
450 xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
451 h64 = xxh64_merge_round(h64, v1);
452 h64 = xxh64_merge_round(h64, v2);
453 h64 = xxh64_merge_round(h64, v3);
454 h64 = xxh64_merge_round(h64, v4);
457 h64 = seed + PRIME64_5;
460 h64 += (uint64_t)len;
462 while (p + 8 <= b_end) {
463 const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
466 h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
470 if (p + 4 <= b_end) {
471 h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
472 h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
477 h64 ^= (*p) * PRIME64_5;
478 h64 = xxh_rotl64(h64, 11) * PRIME64_1;
491 /*-**************************************************
492 * Advanced Hash Functions
493 ***************************************************/
494 XXH_API void xxh32_reset(struct xxh32_state *statePtr, const uint32_t seed)
496 /* use a local state for memcpy() to avoid strict-aliasing warnings */
497 struct xxh32_state state;
499 __builtin_memset(&state, 0, sizeof(state));
500 state.v1 = seed + PRIME32_1 + PRIME32_2;
501 state.v2 = seed + PRIME32_2;
503 state.v4 = seed - PRIME32_1;
504 __builtin_memcpy(statePtr, &state, sizeof(state));
507 XXH_API void xxh64_reset(struct xxh64_state *statePtr, const uint64_t seed)
509 /* use a local state for memcpy() to avoid strict-aliasing warnings */
510 struct xxh64_state state;
512 __builtin_memset(&state, 0, sizeof(state));
513 state.v1 = seed + PRIME64_1 + PRIME64_2;
514 state.v2 = seed + PRIME64_2;
516 state.v4 = seed - PRIME64_1;
517 __builtin_memcpy(statePtr, &state, sizeof(state));
520 XXH_API int xxh32_update(struct xxh32_state *state, const void *input, const size_t len)
522 const uint8_t *p = (const uint8_t *)input;
523 const uint8_t *const b_end = p + len;
528 state->total_len_32 += (uint32_t)len;
529 state->large_len |= (len >= 16) | (state->total_len_32 >= 16);
531 if (state->memsize + len < 16) { /* fill in tmp buffer */
532 __builtin_memcpy((uint8_t *)(state->mem32) + state->memsize, input, len);
533 state->memsize += (uint32_t)len;
537 if (state->memsize) { /* some data left from previous update */
538 const uint32_t *p32 = state->mem32;
540 __builtin_memcpy((uint8_t *)(state->mem32) + state->memsize, input,
541 16 - state->memsize);
543 state->v1 = xxh32_round(state->v1, get_unaligned_le32(p32));
545 state->v2 = xxh32_round(state->v2, get_unaligned_le32(p32));
547 state->v3 = xxh32_round(state->v3, get_unaligned_le32(p32));
549 state->v4 = xxh32_round(state->v4, get_unaligned_le32(p32));
552 p += 16-state->memsize;
556 if (p <= b_end - 16) {
557 const uint8_t *const limit = b_end - 16;
558 uint32_t v1 = state->v1;
559 uint32_t v2 = state->v2;
560 uint32_t v3 = state->v3;
561 uint32_t v4 = state->v4;
564 v1 = xxh32_round(v1, get_unaligned_le32(p));
566 v2 = xxh32_round(v2, get_unaligned_le32(p));
568 v3 = xxh32_round(v3, get_unaligned_le32(p));
570 v4 = xxh32_round(v4, get_unaligned_le32(p));
572 } while (p <= limit);
581 __builtin_memcpy(state->mem32, p, (size_t)(b_end-p));
582 state->memsize = (uint32_t)(b_end-p);
588 XXH_API uint32_t xxh32_digest(const struct xxh32_state *state)
590 const uint8_t *p = (const uint8_t *)state->mem32;
591 const uint8_t *const b_end = (const uint8_t *)(state->mem32) +
595 if (state->large_len) {
596 h32 = xxh_rotl32(state->v1, 1) + xxh_rotl32(state->v2, 7) +
597 xxh_rotl32(state->v3, 12) + xxh_rotl32(state->v4, 18);
599 h32 = state->v3 /* == seed */ + PRIME32_5;
602 h32 += state->total_len_32;
604 while (p + 4 <= b_end) {
605 h32 += get_unaligned_le32(p) * PRIME32_3;
606 h32 = xxh_rotl32(h32, 17) * PRIME32_4;
611 h32 += (*p) * PRIME32_5;
612 h32 = xxh_rotl32(h32, 11) * PRIME32_1;
625 XXH_API int xxh64_update(struct xxh64_state *state, const void *input, const size_t len)
627 const uint8_t *p = (const uint8_t *)input;
628 const uint8_t *const b_end = p + len;
633 state->total_len += len;
635 if (state->memsize + len < 32) { /* fill in tmp buffer */
636 __builtin_memcpy(((uint8_t *)state->mem64) + state->memsize, input, len);
637 state->memsize += (uint32_t)len;
641 if (state->memsize) { /* tmp buffer is full */
642 uint64_t *p64 = state->mem64;
644 __builtin_memcpy(((uint8_t *)p64) + state->memsize, input,
645 32 - state->memsize);
647 state->v1 = xxh64_round(state->v1, get_unaligned_le64(p64));
649 state->v2 = xxh64_round(state->v2, get_unaligned_le64(p64));
651 state->v3 = xxh64_round(state->v3, get_unaligned_le64(p64));
653 state->v4 = xxh64_round(state->v4, get_unaligned_le64(p64));
655 p += 32 - state->memsize;
659 if (p + 32 <= b_end) {
660 const uint8_t *const limit = b_end - 32;
661 uint64_t v1 = state->v1;
662 uint64_t v2 = state->v2;
663 uint64_t v3 = state->v3;
664 uint64_t v4 = state->v4;
667 v1 = xxh64_round(v1, get_unaligned_le64(p));
669 v2 = xxh64_round(v2, get_unaligned_le64(p));
671 v3 = xxh64_round(v3, get_unaligned_le64(p));
673 v4 = xxh64_round(v4, get_unaligned_le64(p));
675 } while (p <= limit);
684 __builtin_memcpy(state->mem64, p, (size_t)(b_end-p));
685 state->memsize = (uint32_t)(b_end - p);
691 XXH_API uint64_t xxh64_digest(const struct xxh64_state *state)
693 const uint8_t *p = (const uint8_t *)state->mem64;
694 const uint8_t *const b_end = (const uint8_t *)state->mem64 +
698 if (state->total_len >= 32) {
699 const uint64_t v1 = state->v1;
700 const uint64_t v2 = state->v2;
701 const uint64_t v3 = state->v3;
702 const uint64_t v4 = state->v4;
704 h64 = xxh_rotl64(v1, 1) + xxh_rotl64(v2, 7) +
705 xxh_rotl64(v3, 12) + xxh_rotl64(v4, 18);
706 h64 = xxh64_merge_round(h64, v1);
707 h64 = xxh64_merge_round(h64, v2);
708 h64 = xxh64_merge_round(h64, v3);
709 h64 = xxh64_merge_round(h64, v4);
711 h64 = state->v3 + PRIME64_5;
714 h64 += (uint64_t)state->total_len;
716 while (p + 8 <= b_end) {
717 const uint64_t k1 = xxh64_round(0, get_unaligned_le64(p));
720 h64 = xxh_rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
724 if (p + 4 <= b_end) {
725 h64 ^= (uint64_t)(get_unaligned_le32(p)) * PRIME64_1;
726 h64 = xxh_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
731 h64 ^= (*p) * PRIME64_5;
732 h64 = xxh_rotl64(h64, 11) * PRIME64_1;
745 #endif /* XXHASH_H */