HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_XXHash.h
Go to the documentation of this file.
1 /*
2  * xxHash - Extremely Fast Hash algorithm
3  * Header File
4  * Copyright (C) 2012-2021 Yann Collet
5  *
6  * BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following disclaimer
16  * in the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * You can contact the author at:
32  * - xxHash homepage: https://www.xxhash.com
33  * - xxHash source repository: https://github.com/Cyan4973/xxHash
34  */
35 
36 #ifndef __UT_XXHash__
37 #define __UT_XXHash__
38 
39 /*!
40  *
41  * xxHash is an extremely fast non-cryptographic hash algorithm, working at RAM speed
42  * limits.
43  *
44  * It is proposed in four flavors, in three families:
45  * 1. @ref XXH32_family
46  * - Classic 32-bit hash function. Simple, compact, and runs on almost all
47  * 32-bit and 64-bit systems.
48  * 2. @ref XXH64_family
49  * - Classic 64-bit adaptation of XXH32. Just as simple, and runs well on most
50  * 64-bit systems (but _not_ 32-bit systems).
51  * 3. @ref XXH3_family
52  * - Modern 64-bit and 128-bit hash function family which features improved
53  * strength and performance across the board, especially on smaller data.
54  * It benefits greatly from SIMD and 64-bit without requiring it.
55  *
56  * Benchmarks
57  * ---
58  * The reference system uses an Intel i7-9700K CPU, and runs Ubuntu x64 20.04.
59  * The open source benchmark program is compiled with clang v10.0 using -O3 flag.
60  *
61  * | Hash Name | ISA ext | Width | Large Data Speed | Small Data Velocity |
62  * | -------------------- | ------- | ----: | ---------------: | ------------------: |
63  * | XXH3_64bits() | @b AVX2 | 64 | 59.4 GB/s | 133.1 |
64  * | MeowHash | AES-NI | 128 | 58.2 GB/s | 52.5 |
65  * | XXH3_128bits() | @b AVX2 | 128 | 57.9 GB/s | 118.1 |
66  * | CLHash | PCLMUL | 64 | 37.1 GB/s | 58.1 |
67  * | XXH3_64bits() | @b SSE2 | 64 | 31.5 GB/s | 133.1 |
68  * | XXH3_128bits() | @b SSE2 | 128 | 29.6 GB/s | 118.1 |
69  * | RAM sequential read | | N/A | 28.0 GB/s | N/A |
70  * | ahash | AES-NI | 64 | 22.5 GB/s | 107.2 |
71  * | City64 | | 64 | 22.0 GB/s | 76.6 |
72  * | T1ha2 | | 64 | 22.0 GB/s | 99.0 |
73  * | City128 | | 128 | 21.7 GB/s | 57.7 |
74  * | FarmHash | AES-NI | 64 | 21.3 GB/s | 71.9 |
75  * | XXH64() | | 64 | 19.4 GB/s | 71.0 |
76  * | SpookyHash | | 64 | 19.3 GB/s | 53.2 |
77  * | Mum | | 64 | 18.0 GB/s | 67.0 |
78  * | CRC32C | SSE4.2 | 32 | 13.0 GB/s | 57.9 |
79  * | XXH32() | | 32 | 9.7 GB/s | 71.9 |
80  * | City32 | | 32 | 9.1 GB/s | 66.0 |
81  * | Blake3* | @b AVX2 | 256 | 4.4 GB/s | 8.1 |
82  * | Murmur3 | | 32 | 3.9 GB/s | 56.1 |
83  * | SipHash* | | 64 | 3.0 GB/s | 43.2 |
84  * | Blake3* | @b SSE2 | 256 | 2.4 GB/s | 8.1 |
85  * | HighwayHash | | 64 | 1.4 GB/s | 6.0 |
86  * | FNV64 | | 64 | 1.2 GB/s | 62.7 |
87  * | Blake2* | | 256 | 1.1 GB/s | 5.1 |
88  * | SHA1* | | 160 | 0.8 GB/s | 5.6 |
89  * | MD5* | | 128 | 0.6 GB/s | 7.8 |
90  * @note
91  * - Hashes which require a specific ISA extension are noted. SSE2 is also noted,
92  * even though it is mandatory on x64.
93  * - Hashes with an asterisk are cryptographic. Note that MD5 is non-cryptographic
94  * by modern standards.
95  * - Small data velocity is a rough average of algorithm's efficiency for small
96  * data. For more accurate information, see the wiki.
97  * - More benchmarks and strength tests are found on the wiki:
98  * https://github.com/Cyan4973/xxHash/wiki
99  *
100  * Usage
101  * ------
102  * All xxHash variants use a similar API. Changing the algorithm is a trivial
103  * substitution.
104  *
105  * @pre
106  * For functions which take an input and length parameter, the following
107  * requirements are assumed:
108  * - The range from [`input`, `input + length`) is valid, readable memory.
109  * - The only exception is if the `length` is `0`, `input` may be `NULL`.
110  * - For C++, the objects must have the *TriviallyCopyable* property, as the
111  * functions access bytes directly as if it was an array of `unsigned char`.
112  *
113  * @anchor single_shot_example
114  * **Single Shot**
115  *
116  * These functions are stateless functions which hash a contiguous block of memory,
117  * immediately returning the result. They are the easiest and usually the fastest
118  * option.
119  *
120  * XXH32(), XXH64(), XXH3_64bits(), XXH3_128bits()
121  *
122  * @code{.c}
123  * #include <string.h>
124  * #include "xxhash.h"
125  *
126  * // Example for a function which hashes a null terminated string with XXH32().
127  * XXH32_hash_t hash_string(const char* string, XXH32_hash_t seed)
128  * {
129  * // NULL pointers are only valid if the length is zero
130  * size_t length = (string == NULL) ? 0 : strlen(string);
131  * return XXH32(string, length, seed);
132  * }
133  * @endcode
134  *
135  * @anchor streaming_example
136  * **Streaming**
137  *
138  * These groups of functions allow incremental hashing of unknown size, even
139  * more than what would fit in a size_t.
140  *
141  * XXH32_reset(), XXH64_reset(), XXH3_64bits_reset(), XXH3_128bits_reset()
142  *
143  * @code{.c}
144  * #include <stdio.h>
145  * #include <assert.h>
146  * #include "xxhash.h"
147  * // Example for a function which hashes a FILE incrementally with XXH3_64bits().
148  * XXH64_hash_t hashFile(FILE* f)
149  * {
150  * // Allocate a state struct. Do not just use malloc() or new.
151  * XXH3_state_t* state = XXH3_createState();
152  * assert(state != NULL && "Out of memory!");
153  * // Reset the state to start a new hashing session.
154  * XXH3_64bits_reset(state);
155  * char buffer[4096];
156  * size_t count;
157  * // Read the file in chunks
158  * while ((count = fread(buffer, 1, sizeof(buffer), f)) != 0) {
159  * // Run update() as many times as necessary to process the data
160  * XXH3_64bits_update(state, buffer, count);
161  * }
162  * // Retrieve the finalized hash. This will not change the state.
163  * XXH64_hash_t result = XXH3_64bits_digest(state);
164  * // Free the state. Do not use free().
165  * XXH3_freeState(state);
166  * return result;
167  * }
168  * @endcode
169  *
170  * @file xxhash.h
171  * xxHash prototypes and implementation
172  */
173 
174 #if defined (__cplusplus)
175 extern "C" {
176 #endif
177 
178 /* ****************************
179  * INLINE mode
180  ******************************/
181 /*!
182  * @defgroup public Public API
183  * Contains details on the public xxHash functions.
184  * @{
185  */
186 #include "UT_API.h" // SideFX modification
187 #define XXH_NAMESPACE UT_ // SideFX modification
188 
189 #ifdef XXH_DOXYGEN
190 /*!
191  * @brief Exposes the implementation and marks all functions as `inline`.
192  *
193  * Use these build macros to inline xxhash into the target unit.
194  * Inlining improves performance on small inputs, especially when the length is
195  * expressed as a compile-time constant:
196  *
197  * https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html
198  *
199  * It also keeps xxHash symbols private to the unit, so they are not exported.
200  *
201  * Usage:
202  * @code{.c}
203  * #define XXH_INLINE_ALL
204  * #include "xxhash.h"
205  * @endcode
206  * Do not compile and link xxhash.o as a separate object, as it is not useful.
207  */
208 # define XXH_INLINE_ALL
209 # undef XXH_INLINE_ALL
210 /*!
211  * @brief Exposes the implementation without marking functions as inline.
212  */
213 # define XXH_PRIVATE_API
214 # undef XXH_PRIVATE_API
215 /*!
216  * @brief Emulate a namespace by transparently prefixing all symbols.
217  *
218  * If you want to include _and expose_ xxHash functions from within your own
219  * library, but also want to avoid symbol collisions with other libraries which
220  * may also include xxHash, you can use @ref XXH_NAMESPACE to automatically prefix
221  * any public symbol from xxhash library with the value of @ref XXH_NAMESPACE
222  * (therefore, avoid empty or numeric values).
223  *
224  * Note that no change is required within the calling program as long as it
225  * includes `xxhash.h`: Regular symbol names will be automatically translated
226  * by this header.
227  */
228 # define XXH_NAMESPACE UT_ // SideFX modification
229 # undef XXH_NAMESPACE
230 #endif
231 
232 #if (defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)) \
233  && !defined(XXH_INLINE_ALL_31684351384)
234  /* this section should be traversed only once */
235 # define XXH_INLINE_ALL_31684351384
236  /* give access to the advanced API, required to compile implementations */
237 # undef XXH_STATIC_LINKING_ONLY /* avoid macro redef */
238 # define XXH_STATIC_LINKING_ONLY
239  /* make all functions private */
240 # undef XXH_PUBLIC_API
241 # if defined(__GNUC__)
242 # define XXH_PUBLIC_API static __inline __attribute__((unused))
243 # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
244 # define XXH_PUBLIC_API static inline
245 # elif defined(_MSC_VER)
246 # define XXH_PUBLIC_API static __inline
247 # else
248  /* note: this version may generate warnings for unused static functions */
249 # define XXH_PUBLIC_API static
250 # endif
251 
252  /*
253  * This part deals with the special case where a unit wants to inline xxHash,
254  * but "xxhash.h" has previously been included without XXH_INLINE_ALL,
255  * such as part of some previously included *.h header file.
256  * Without further action, the new include would just be ignored,
257  * and functions would effectively _not_ be inlined (silent failure).
258  * The following macros solve this situation by prefixing all inlined names,
259  * avoiding naming collision with previous inclusions.
260  */
261  /* Before that, we unconditionally #undef all symbols,
262  * in case they were already defined with XXH_NAMESPACE.
263  * They will then be redefined for XXH_INLINE_ALL
264  */
265 # undef XXH_versionNumber
266  /* XXH32 */
267 # undef XXH32
268 # undef XXH32_createState
269 # undef XXH32_freeState
270 # undef XXH32_reset
271 # undef XXH32_update
272 # undef XXH32_digest
273 # undef XXH32_copyState
274 # undef XXH32_canonicalFromHash
275 # undef XXH32_hashFromCanonical
276  /* XXH64 */
277 # undef XXH64
278 # undef XXH64_createState
279 # undef XXH64_freeState
280 # undef XXH64_reset
281 # undef XXH64_update
282 # undef XXH64_digest
283 # undef XXH64_copyState
284 # undef XXH64_canonicalFromHash
285 # undef XXH64_hashFromCanonical
286  /* XXH3_64bits */
287 # undef XXH3_64bits
288 # undef XXH3_64bits_withSecret
289 # undef XXH3_64bits_withSeed
290 # undef XXH3_64bits_withSecretandSeed
291 # undef XXH3_createState
292 # undef XXH3_freeState
293 # undef XXH3_copyState
294 # undef XXH3_64bits_reset
295 # undef XXH3_64bits_reset_withSeed
296 # undef XXH3_64bits_reset_withSecret
297 # undef XXH3_64bits_update
298 # undef XXH3_64bits_digest
299 # undef XXH3_generateSecret
300  /* XXH3_128bits */
301 # undef XXH128
302 # undef XXH3_128bits
303 # undef XXH3_128bits_withSeed
304 # undef XXH3_128bits_withSecret
305 # undef XXH3_128bits_reset
306 # undef XXH3_128bits_reset_withSeed
307 # undef XXH3_128bits_reset_withSecret
308 # undef XXH3_128bits_reset_withSecretandSeed
309 # undef XXH3_128bits_update
310 # undef XXH3_128bits_digest
311 # undef XXH128_isEqual
312 # undef XXH128_cmp
313 # undef XXH128_canonicalFromHash
314 # undef XXH128_hashFromCanonical
315  /* Finally, free the namespace itself */
316 # undef XXH_NAMESPACE
317 
318  /* employ the namespace for XXH_INLINE_ALL */
319 # define XXH_NAMESPACE XXH_INLINE_
320  /*
321  * Some identifiers (enums, type names) are not symbols,
322  * but they must nonetheless be renamed to avoid redeclaration.
323  * Alternative solution: do not redeclare them.
324  * However, this requires some #ifdefs, and has a more dispersed impact.
325  * Meanwhile, renaming can be achieved in a single place.
326  */
327 # define XXH_IPREF(Id) XXH_NAMESPACE ## Id
328 # define XXH_OK XXH_IPREF(XXH_OK)
329 # define XXH_ERROR XXH_IPREF(XXH_ERROR)
330 # define XXH_errorcode XXH_IPREF(XXH_errorcode)
331 # define XXH32_canonical_t XXH_IPREF(XXH32_canonical_t)
332 # define XXH64_canonical_t XXH_IPREF(XXH64_canonical_t)
333 # define XXH128_canonical_t XXH_IPREF(XXH128_canonical_t)
334 # define XXH32_state_s XXH_IPREF(XXH32_state_s)
335 # define XXH32_state_t XXH_IPREF(XXH32_state_t)
336 # define XXH64_state_s XXH_IPREF(XXH64_state_s)
337 # define XXH64_state_t XXH_IPREF(XXH64_state_t)
338 # define XXH3_state_s XXH_IPREF(XXH3_state_s)
339 # define XXH3_state_t XXH_IPREF(XXH3_state_t)
340 # define XXH128_hash_t XXH_IPREF(XXH128_hash_t)
341  /* Ensure the header is parsed again, even if it was previously included */
342 # undef XXHASH_H_5627135585666179
343 # undef XXHASH_H_STATIC_13879238742
344 #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
345 
346 /* ****************************************************************
347  * Stable API
348  *****************************************************************/
349 #ifndef XXHASH_H_5627135585666179
350 #define XXHASH_H_5627135585666179 1
351 
352 /*! @brief Marks a global symbol. */
353 #if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
354 # if defined(WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
355 # ifdef XXH_EXPORT
356 # define XXH_PUBLIC_API __declspec(dllexport)
357 # elif XXH_IMPORT
358 # define XXH_PUBLIC_API __declspec(dllimport)
359 # endif
360 # else
361 # define XXH_PUBLIC_API /* do nothing */
362 # endif
363 # undef XXH_PUBLIC_API // SideFX modification
364 # define XXH_PUBLIC_API UT_API // SideFX modification
365 #endif
366 
367 #ifdef XXH_NAMESPACE
368 # define XXH_CAT(A,B) A##B
369 # define XXH_NAME2(A,B) XXH_CAT(A,B)
370 # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
371 /* XXH32 */
372 # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
373 # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
374 # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
375 # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
376 # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
377 # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
378 # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
379 # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
380 # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
381 /* XXH64 */
382 # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
383 # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
384 # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
385 # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
386 # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
387 # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
388 # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
389 # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
390 # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
391 /* XXH3_64bits */
392 # define XXH3_64bits XXH_NAME2(XXH_NAMESPACE, XXH3_64bits)
393 # define XXH3_64bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecret)
394 # define XXH3_64bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSeed)
395 # define XXH3_64bits_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecretandSeed)
396 # define XXH3_createState XXH_NAME2(XXH_NAMESPACE, XXH3_createState)
397 # define XXH3_freeState XXH_NAME2(XXH_NAMESPACE, XXH3_freeState)
398 # define XXH3_copyState XXH_NAME2(XXH_NAMESPACE, XXH3_copyState)
399 # define XXH3_64bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset)
400 # define XXH3_64bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSeed)
401 # define XXH3_64bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecret)
402 # define XXH3_64bits_reset_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecretandSeed)
403 # define XXH3_64bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_update)
404 # define XXH3_64bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_digest)
405 # define XXH3_generateSecret XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret)
406 # define XXH3_generateSecret_fromSeed XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret_fromSeed)
407 /* XXH3_128bits */
408 # define XXH128 XXH_NAME2(XXH_NAMESPACE, XXH128)
409 # define XXH3_128bits XXH_NAME2(XXH_NAMESPACE, XXH3_128bits)
410 # define XXH3_128bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSeed)
411 # define XXH3_128bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecret)
412 # define XXH3_128bits_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecretandSeed)
413 # define XXH3_128bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset)
414 # define XXH3_128bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSeed)
415 # define XXH3_128bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecret)
416 # define XXH3_128bits_reset_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecretandSeed)
417 # define XXH3_128bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_update)
418 # define XXH3_128bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_digest)
419 # define XXH128_isEqual XXH_NAME2(XXH_NAMESPACE, XXH128_isEqual)
420 # define XXH128_cmp XXH_NAME2(XXH_NAMESPACE, XXH128_cmp)
421 # define XXH128_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH128_canonicalFromHash)
422 # define XXH128_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH128_hashFromCanonical)
423 #endif
424 
425 
426 /* *************************************
427 * Compiler specifics
428 ***************************************/
429 
430 /* specific declaration modes for Windows */
431 #if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
432 # undef XXH_PUBLIC_API // SideFX modification
433 # if defined(WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
434 # ifdef XXH_EXPORT
435 # define XXH_PUBLIC_API __declspec(dllexport)
436 # elif XXH_IMPORT
437 # define XXH_PUBLIC_API __declspec(dllimport)
438 # endif
439 # else
440 # define XXH_PUBLIC_API /* do nothing */
441 # endif
442 # undef XXH_PUBLIC_API // SideFX modification
443 # define XXH_PUBLIC_API UT_API // SideFX modification
444 #endif
445 
446 #if defined (__GNUC__)
447 # define XXH_CONSTF __attribute__((const))
448 # define XXH_PUREF __attribute__((pure))
449 # define XXH_MALLOCF __attribute__((malloc))
450 #else
451 # define XXH_CONSTF /* disable */
452 # define XXH_PUREF
453 # define XXH_MALLOCF
454 #endif
455 
456 /* *************************************
457 * Version
458 ***************************************/
459 #define XXH_VERSION_MAJOR 0
460 #define XXH_VERSION_MINOR 8
461 #define XXH_VERSION_RELEASE 1
462 /*! @brief Version number, encoded as two digits each */
463 #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
464 
465 /*!
466  * @brief Obtains the xxHash version.
467  *
468  * This is mostly useful when xxHash is compiled as a shared library,
469  * since the returned value comes from the library, as opposed to header file.
470  *
471  * @return @ref XXH_VERSION_NUMBER of the invoked library.
472  */
474 
475 
476 /* ****************************
477 * Common basic types
478 ******************************/
479 #include <stddef.h> /* size_t */
480 /*!
481  * @brief Exit code for the streaming API.
482  */
483 typedef enum {
484  XXH_OK = 0, /*!< OK */
485  XXH_ERROR /*!< Error */
486 } XXH_errorcode;
487 
488 
489 /*-**********************************************************************
490 * 32-bit hash
491 ************************************************************************/
492 #if defined(XXH_DOXYGEN) /* Don't show <stdint.h> include */
493 /*!
494  * @brief An unsigned 32-bit integer.
495  *
496  * Not necessarily defined to `uint32_t` but functionally equivalent.
497  */
498 typedef uint32_t XXH32_hash_t;
499 
500 #elif !defined (__VMS) \
501  && (defined (__cplusplus) \
502  || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
503 # include <stdint.h>
504  typedef uint32_t XXH32_hash_t;
505 
506 #else
507 # include <limits.h>
508 # if UINT_MAX == 0xFFFFFFFFUL
509  typedef unsigned int XXH32_hash_t;
510 # elif ULONG_MAX == 0xFFFFFFFFUL
511  typedef unsigned long XXH32_hash_t;
512 # else
513 # error "unsupported platform: need a 32-bit type"
514 # endif
515 #endif
516 
517 /*!
518  * @}
519  *
520  * @defgroup XXH32_family XXH32 family
521  * @ingroup public
522  * Contains functions used in the classic 32-bit xxHash algorithm.
523  *
524  * @note
525  * XXH32 is useful for older platforms, with no or poor 64-bit performance.
526  * Note that the @ref XXH3_family provides competitive speed for both 32-bit
527  * and 64-bit systems, and offers true 64/128 bit hash results.
528  *
529  * @see @ref XXH64_family, @ref XXH3_family : Other xxHash families
530  * @see @ref XXH32_impl for implementation details
531  * @{
532  */
533 
534 /*!
535  * @brief Calculates the 32-bit hash of @p input using xxHash32.
536  *
537  * Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark): 5.4 GB/s
538  *
539  * See @ref single_shot_example "Single Shot Example" for an example.
540  *
541  * @param input The block of data to be hashed, at least @p length bytes in size.
542  * @param length The length of @p input, in bytes.
543  * @param seed The 32-bit seed to alter the hash's output predictably.
544  *
545  * @pre
546  * The memory between @p input and @p input + @p length must be valid,
547  * readable, contiguous memory. However, if @p length is `0`, @p input may be
548  * `NULL`. In C++, this also must be *TriviallyCopyable*.
549  *
550  * @return The calculated 32-bit hash value.
551  *
552  * @see
553  * XXH64(), XXH3_64bits_withSeed(), XXH3_128bits_withSeed(), XXH128():
554  * Direct equivalents for the other variants of xxHash.
555  * @see
556  * XXH32_createState(), XXH32_update(), XXH32_digest(): Streaming version.
557  */
558 XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32 (const void* input, size_t length, XXH32_hash_t seed);
559 
560 #ifndef XXH_NO_STREAM
561 /*!
562  * Streaming functions generate the xxHash value from an incremental input.
563  * This method is slower than single-call functions, due to state management.
564  * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized.
565  *
566  * An XXH state must first be allocated using `XXH*_createState()`.
567  *
568  * Start a new hash by initializing the state with a seed using `XXH*_reset()`.
569  *
570  * Then, feed the hash state by calling `XXH*_update()` as many times as necessary.
571  *
572  * The function returns an error code, with 0 meaning OK, and any other value
573  * meaning there is an error.
574  *
575  * Finally, a hash value can be produced anytime, by using `XXH*_digest()`.
576  * This function returns the nn-bits hash as an int or long long.
577  *
578  * It's still possible to continue inserting input into the hash state after a
579  * digest, and generate new hash values later on by invoking `XXH*_digest()`.
580  *
581  * When done, release the state using `XXH*_freeState()`.
582  *
583  * @see streaming_example at the top of @ref xxhash.h for an example.
584  */
585 
586 /*!
587  * @typedef struct XXH32_state_s XXH32_state_t
588  * @brief The opaque state struct for the XXH32 streaming API.
589  *
590  * @see XXH32_state_s for details.
591  */
592 typedef struct XXH32_state_s XXH32_state_t;
593 
594 /*!
595  * @brief Allocates an @ref XXH32_state_t.
596  *
597  * Must be freed with XXH32_freeState().
598  * @return An allocated XXH32_state_t on success, `NULL` on failure.
599  */
601 /*!
602  * @brief Frees an @ref XXH32_state_t.
603  *
604  * Must be allocated with XXH32_createState().
605  * @param statePtr A pointer to an @ref XXH32_state_t allocated with @ref XXH32_createState().
606  * @return XXH_OK.
607  */
609 /*!
610  * @brief Copies one @ref XXH32_state_t to another.
611  *
612  * @param dst_state The state to copy to.
613  * @param src_state The state to copy from.
614  * @pre
615  * @p dst_state and @p src_state must not be `NULL` and must not overlap.
616  */
617 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
618 
619 /*!
620  * @brief Resets an @ref XXH32_state_t to begin a new hash.
621  *
622  * This function resets and seeds a state. Call it before @ref XXH32_update().
623  *
624  * @param statePtr The state struct to reset.
625  * @param seed The 32-bit seed to alter the hash result predictably.
626  *
627  * @pre
628  * @p statePtr must not be `NULL`.
629  *
630  * @return @ref XXH_OK on success, @ref XXH_ERROR on failure.
631  */
632 XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, XXH32_hash_t seed);
633 
634 /*!
635  * @brief Consumes a block of @p input to an @ref XXH32_state_t.
636  *
637  * Call this to incrementally consume blocks of data.
638  *
639  * @param statePtr The state struct to update.
640  * @param input The block of data to be hashed, at least @p length bytes in size.
641  * @param length The length of @p input, in bytes.
642  *
643  * @pre
644  * @p statePtr must not be `NULL`.
645  * @pre
646  * The memory between @p input and @p input + @p length must be valid,
647  * readable, contiguous memory. However, if @p length is `0`, @p input may be
648  * `NULL`. In C++, this also must be *TriviallyCopyable*.
649  *
650  * @return @ref XXH_OK on success, @ref XXH_ERROR on failure.
651  */
652 XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
653 
654 /*!
655  * @brief Returns the calculated hash value from an @ref XXH32_state_t.
656  *
657  * @note
658  * Calling XXH32_digest() will not affect @p statePtr, so you can update,
659  * digest, and update again.
660  *
661  * @param statePtr The state struct to calculate the hash from.
662  *
663  * @pre
664  * @p statePtr must not be `NULL`.
665  *
666  * @return The calculated xxHash32 value from that state.
667  */
668 XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
669 #endif /* !XXH_NO_STREAM */
670 
671 /******* Canonical representation *******/
672 
673 /*
674  * The default return values from XXH functions are unsigned 32 and 64 bit
675  * integers.
676  * This the simplest and fastest format for further post-processing.
677  *
678  * However, this leaves open the question of what is the order on the byte level,
679  * since little and big endian conventions will store the same number differently.
680  *
681  * The canonical representation settles this issue by mandating big-endian
682  * convention, the same convention as human-readable numbers (large digits first).
683  *
684  * When writing hash values to storage, sending them over a network, or printing
685  * them, it's highly recommended to use the canonical representation to ensure
686  * portability across a wider range of systems, present and future.
687  *
688  * The following functions allow transformation of hash values to and from
689  * canonical format.
690  */
691 
692 /*!
693  * @brief Canonical (big endian) representation of @ref XXH32_hash_t.
694  */
695 typedef struct {
696  unsigned char digest[4]; /*!< Hash bytes, big endian */
698 
699 /*!
700  * @brief Converts an @ref XXH32_hash_t to a big endian @ref XXH32_canonical_t.
701  *
702  * @param dst The @ref XXH32_canonical_t pointer to be stored to.
703  * @param hash The @ref XXH32_hash_t to be converted.
704  *
705  * @pre
706  * @p dst must not be `NULL`.
707  */
709 
710 /*!
711  * @brief Converts an @ref XXH32_canonical_t to a native @ref XXH32_hash_t.
712  *
713  * @param src The @ref XXH32_canonical_t to convert.
714  *
715  * @pre
716  * @p src must not be `NULL`.
717  *
718  * @return The converted hash.
719  */
721 
722 
723 #ifdef __has_attribute
724 # define XXH_HAS_ATTRIBUTE(x) __has_attribute(x)
725 #else
726 # define XXH_HAS_ATTRIBUTE(x) 0
727 #endif
728 
729 /* C-language Attributes are added in C23. */
730 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
731 # define XXH_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
732 #else
733 # define XXH_HAS_C_ATTRIBUTE(x) 0
734 #endif
735 
736 #if defined(__cplusplus) && defined(__has_cpp_attribute)
737 # define XXH_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
738 #else
739 # define XXH_HAS_CPP_ATTRIBUTE(x) 0
740 #endif
741 
742 /*
743  * Define XXH_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute
744  * introduced in CPP17 and C23.
745  * CPP17 : https://en.cppreference.com/w/cpp/language/attributes/fallthrough
746  * C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough
747  */
748 #if XXH_HAS_C_ATTRIBUTE(fallthrough) || XXH_HAS_CPP_ATTRIBUTE(fallthrough)
749 # define XXH_FALLTHROUGH [[fallthrough]]
750 #elif XXH_HAS_ATTRIBUTE(__fallthrough__)
751 # define XXH_FALLTHROUGH __attribute__ ((__fallthrough__))
752 #else
753 # define XXH_FALLTHROUGH /* fallthrough */
754 #endif
755 
756 /*!
757  * @}
758  * @ingroup public
759  * @{
760  */
761 
762 #ifndef XXH_NO_LONG_LONG
763 /*-**********************************************************************
764 * 64-bit hash
765 ************************************************************************/
766 #if defined(XXH_DOXYGEN) /* don't include <stdint.h> */
767 /*!
768  * @brief An unsigned 64-bit integer.
769  *
770  * Not necessarily defined to `uint64_t` but functionally equivalent.
771  */
772 typedef uint64_t XXH64_hash_t;
773 #elif !defined (__VMS) \
774  && (defined (__cplusplus) \
775  || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
776 # include <stdint.h>
777  typedef uint64_t XXH64_hash_t;
778 #else
779 # include <limits.h>
780 # if defined(__LP64__) && ULONG_MAX == 0xFFFFFFFFFFFFFFFFULL
781  /* LP64 ABI says uint64_t is unsigned long */
782  typedef unsigned long XXH64_hash_t;
783 # else
784  /* the following type must have a width of 64-bit */
785  typedef unsigned long long XXH64_hash_t;
786 # endif
787 #endif
788 
789 /*!
790  * @}
791  *
792  * @defgroup XXH64_family XXH64 family
793  * @ingroup public
794  * @{
795  * Contains functions used in the classic 64-bit xxHash algorithm.
796  *
797  * @note
798  * XXH3 provides competitive speed for both 32-bit and 64-bit systems,
799  * and offers true 64/128 bit hash results.
800  * It provides better speed for systems with vector processing capabilities.
801  */
802 
803 /*!
804  * @brief Calculates the 64-bit hash of @p input using xxHash64.
805  *
806  * This function usually runs faster on 64-bit systems, but slower on 32-bit
807  * systems (see benchmark).
808  *
809  * @param input The block of data to be hashed, at least @p length bytes in size.
810  * @param length The length of @p input, in bytes.
811  * @param seed The 64-bit seed to alter the hash's output predictably.
812  *
813  * @pre
814  * The memory between @p input and @p input + @p length must be valid,
815  * readable, contiguous memory. However, if @p length is `0`, @p input may be
816  * `NULL`. In C++, this also must be *TriviallyCopyable*.
817  *
818  * @return The calculated 64-bit hash.
819  *
820  * @see
821  * XXH32(), XXH3_64bits_withSeed(), XXH3_128bits_withSeed(), XXH128():
822  * Direct equivalents for the other variants of xxHash.
823  * @see
824  * XXH64_createState(), XXH64_update(), XXH64_digest(): Streaming version.
825  */
826 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64(const void* input, size_t length, XXH64_hash_t seed);
827 
828 /******* Streaming *******/
829 #ifndef XXH_NO_STREAM
830 /*!
831  * @brief The opaque state struct for the XXH64 streaming API.
832  *
833  * @see XXH64_state_s for details.
834  */
835 typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
838 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dst_state, const XXH64_state_t* src_state);
839 
840 XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, XXH64_hash_t seed);
841 XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
842 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
843 #endif /* !XXH_NO_STREAM */
844 /******* Canonical representation *******/
845 typedef struct { unsigned char digest[sizeof(XXH64_hash_t)]; } XXH64_canonical_t;
848 
849 #ifndef XXH_NO_XXH3
850 
851 /*!
852  * @}
853  * ************************************************************************
854  * @defgroup XXH3_family XXH3 family
855  * @ingroup public
856  * @{
857  *
858  * XXH3 is a more recent hash algorithm featuring:
859  * - Improved speed for both small and large inputs
860  * - True 64-bit and 128-bit outputs
861  * - SIMD acceleration
862  * - Improved 32-bit viability
863  *
864  * Speed analysis methodology is explained here:
865  *
866  * https://fastcompression.blogspot.com/2019/03/presenting-xxh3.html
867  *
868  * Compared to XXH64, expect XXH3 to run approximately
869  * ~2x faster on large inputs and >3x faster on small ones,
870  * exact differences vary depending on platform.
871  *
872  * XXH3's speed benefits greatly from SIMD and 64-bit arithmetic,
873  * but does not require it.
874  * Most 32-bit and 64-bit targets that can run XXH32 smoothly can run XXH3
875  * at competitive speeds, even without vector support. Further details are
876  * explained in the implementation.
877  *
878  * Optimized implementations are provided for AVX512, AVX2, SSE2, NEON, POWER8,
879  * ZVector and scalar targets. This can be controlled via the @ref XXH_VECTOR
880  * macro. For the x86 family, an automatic dispatcher is included separately
881  * in @ref xxh_x86dispatch.c.
882  *
883  * XXH3 implementation is portable:
884  * it has a generic C90 formulation that can be compiled on any platform,
885  * all implementations generage exactly the same hash value on all platforms.
886  * Starting from v0.8.0, it's also labelled "stable", meaning that
887  * any future version will also generate the same hash value.
888  *
889  * XXH3 offers 2 variants, _64bits and _128bits.
890  *
891  * When only 64 bits are needed, prefer invoking the _64bits variant, as it
892  * reduces the amount of mixing, resulting in faster speed on small inputs.
893  * It's also generally simpler to manipulate a scalar return type than a struct.
894  *
895  * The API supports one-shot hashing, streaming mode, and custom secrets.
896  */
897 /*-**********************************************************************
898 * XXH3 64-bit variant
899 ************************************************************************/
900 
901 /*!
902  * @brief 64-bit unseeded variant of XXH3.
903  *
904  * This is equivalent to @ref XXH3_64bits_withSeed() with a seed of 0, however
905  * it may have slightly better performance due to constant propagation of the
906  * defaults.
907  *
908  * @see
909  * XXH32(), XXH64(), XXH3_128bits(): equivalent for the other xxHash algorithms
910  * @see
911  * XXH3_64bits_withSeed(), XXH3_64bits_withSecret(): other seeding variants
912  * @see
913  * XXH3_64bits_reset(), XXH3_64bits_update(), XXH3_64bits_digest(): Streaming version.
914  */
915 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits(const void* input, size_t length);
916 
917 /*!
918  * @brief 64-bit seeded variant of XXH3
919  *
920  * This variant generates a custom secret on the fly based on default secret
921  * altered using the `seed` value.
922  *
923  * While this operation is decently fast, note that it's not completely free.
924  *
925  * @note
926  * seed == 0 produces the same results as @ref XXH3_64bits().
927  *
928  * @param input The data to hash
929  * @param length The length
930  * @param seed The 64-bit seed to alter the state.
931  */
932 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_withSeed(const void* input, size_t length, XXH64_hash_t seed);
933 
934 /*!
935  * The bare minimum size for a custom secret.
936  *
937  * @see
938  * XXH3_64bits_withSecret(), XXH3_64bits_reset_withSecret(),
939  * XXH3_128bits_withSecret(), XXH3_128bits_reset_withSecret().
940  */
941 #define XXH3_SECRET_SIZE_MIN 136
942 
943 /*!
944  * @brief 64-bit variant of XXH3 with a custom "secret".
945  *
946  * It's possible to provide any blob of bytes as a "secret" to generate the hash.
947  * This makes it more difficult for an external actor to prepare an intentional collision.
948  * The main condition is that secretSize *must* be large enough (>= XXH3_SECRET_SIZE_MIN).
949  * However, the quality of the secret impacts the dispersion of the hash algorithm.
950  * Therefore, the secret _must_ look like a bunch of random bytes.
951  * Avoid "trivial" or structured data such as repeated sequences or a text document.
952  * Whenever in doubt about the "randomness" of the blob of bytes,
953  * consider employing "XXH3_generateSecret()" instead (see below).
954  * It will generate a proper high entropy secret derived from the blob of bytes.
955  * Another advantage of using XXH3_generateSecret() is that
956  * it guarantees that all bits within the initial blob of bytes
957  * will impact every bit of the output.
958  * This is not necessarily the case when using the blob of bytes directly
959  * because, when hashing _small_ inputs, only a portion of the secret is employed.
960  */
961 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
962 
963 
964 /******* Streaming *******/
965 #ifndef XXH_NO_STREAM
966 /*
967  * Streaming requires state maintenance.
968  * This operation costs memory and CPU.
969  * As a consequence, streaming is slower than one-shot hashing.
970  * For better performance, prefer one-shot functions whenever applicable.
971  */
972 
973 /*!
974  * @brief The state struct for the XXH3 streaming API.
975  *
976  * @see XXH3_state_s for details.
977  */
978 typedef struct XXH3_state_s XXH3_state_t;
981 XXH_PUBLIC_API void XXH3_copyState(XXH3_state_t* dst_state, const XXH3_state_t* src_state);
982 
983 /*
984  * XXH3_64bits_reset():
985  * Initialize with default parameters.
986  * digest will be equivalent to `XXH3_64bits()`.
987  */
989 /*
990  * XXH3_64bits_reset_withSeed():
991  * Generate a custom secret from `seed`, and store it into `statePtr`.
992  * digest will be equivalent to `XXH3_64bits_withSeed()`.
993  */
995 /*!
996  * XXH3_64bits_reset_withSecret():
997  * `secret` is referenced, it _must outlive_ the hash streaming session.
998  * Similar to one-shot API, `secretSize` must be >= `XXH3_SECRET_SIZE_MIN`,
999  * and the quality of produced hash values depends on secret's entropy
1000  * (secret's content should look like a bunch of random bytes).
1001  * When in doubt about the randomness of a candidate `secret`,
1002  * consider employing `XXH3_generateSecret()` instead (see below).
1003  */
1004 XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize);
1005 
1006 XXH_PUBLIC_API XXH_errorcode XXH3_64bits_update (XXH3_state_t* statePtr, const void* input, size_t length);
1007 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_digest (const XXH3_state_t* statePtr);
1008 #endif /* !XXH_NO_STREAM */
1009 
1010 /* note : canonical representation of XXH3 is the same as XXH64
1011  * since they both produce XXH64_hash_t values */
1012 
1013 
1014 /*-**********************************************************************
1015 * XXH3 128-bit variant
1016 ************************************************************************/
1017 
1018 /*!
1019  * @brief The return value from 128-bit hashes.
1020  *
1021  * Stored in little endian order, although the fields themselves are in native
1022  * endianness.
1023  */
1024 typedef struct {
1025  XXH64_hash_t low64; /*!< `value & 0xFFFFFFFFFFFFFFFF` */
1026  XXH64_hash_t high64; /*!< `value >> 64` */
1027 } XXH128_hash_t;
1028 
1029 /*!
1030  * @brief Unseeded 128-bit variant of XXH3
1031  *
1032  * The 128-bit variant of XXH3 has more strength, but it has a bit of overhead
1033  * for shorter inputs.
1034  *
1035  * This is equivalent to @ref XXH3_128bits_withSeed() with a seed of 0, however
1036  * it may have slightly better performance due to constant propagation of the
1037  * defaults.
1038  *
1039  * @see
1040  * XXH32(), XXH64(), XXH3_64bits(): equivalent for the other xxHash algorithms
1041  * @see
1042  * XXH3_128bits_withSeed(), XXH3_128bits_withSecret(): other seeding variants
1043  * @see
1044  * XXH3_128bits_reset(), XXH3_128bits_update(), XXH3_128bits_digest(): Streaming version.
1045  */
1046 XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits(const void* data, size_t len);
1047 /*! @brief Seeded 128-bit variant of XXH3. @see XXH3_64bits_withSeed(). */
1048 XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_withSeed(const void* data, size_t len, XXH64_hash_t seed);
1049 /*! @brief Custom secret 128-bit variant of XXH3. @see XXH3_64bits_withSecret(). */
1050 XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
1051 
1052 /******* Streaming *******/
1053 #ifndef XXH_NO_STREAM
1054 /*
1055  * Streaming requires state maintenance.
1056  * This operation costs memory and CPU.
1057  * As a consequence, streaming is slower than one-shot hashing.
1058  * For better performance, prefer one-shot functions whenever applicable.
1059  *
1060  * XXH3_128bits uses the same XXH3_state_t as XXH3_64bits().
1061  * Use already declared XXH3_createState() and XXH3_freeState().
1062  *
1063  * All reset and streaming functions have same meaning as their 64-bit counterpart.
1064  */
1065 
1068 XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize);
1069 
1070 XXH_PUBLIC_API XXH_errorcode XXH3_128bits_update (XXH3_state_t* statePtr, const void* input, size_t length);
1072 #endif /* !XXH_NO_STREAM */
1073 
1074 /* Following helper functions make it possible to compare XXH128_hast_t values.
1075  * Since XXH128_hash_t is a structure, this capability is not offered by the language.
1076  * Note: For better performance, these functions can be inlined using XXH_INLINE_ALL */
1077 
1078 /*!
1079  * XXH128_isEqual():
1080  * Return: 1 if `h1` and `h2` are equal, 0 if they are not.
1081  */
1083 
1084 /*!
1085  * @brief Compares two @ref XXH128_hash_t
1086  * This comparator is compatible with stdlib's `qsort()`/`bsearch()`.
1087  *
1088  * @return: >0 if *h128_1 > *h128_2
1089  * =0 if *h128_1 == *h128_2
1090  * <0 if *h128_1 < *h128_2
1091  */
1092 XXH_PUBLIC_API XXH_PUREF int XXH128_cmp(const void* h128_1, const void* h128_2);
1093 
1094 
1095 /******* Canonical representation *******/
1096 typedef struct { unsigned char digest[sizeof(XXH128_hash_t)]; } XXH128_canonical_t;
1099 
1100 
1101 #endif /* !XXH_NO_XXH3 */
1102 #endif /* XXH_NO_LONG_LONG */
1103 
1104 /*!
1105  * @}
1106  */
1107 #endif /* XXHASH_H_5627135585666179 */
1108 
1109 
1110 
1111 #if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742)
1112 #define XXHASH_H_STATIC_13879238742
1113 /* ****************************************************************************
1114  * This section contains declarations which are not guaranteed to remain stable.
1115  * They may change in future versions, becoming incompatible with a different
1116  * version of the library.
1117  * These declarations should only be used with static linking.
1118  * Never use them in association with dynamic linking!
1119  ***************************************************************************** */
1120 
1121 /*
1122  * These definitions are only present to allow static allocation
1123  * of XXH states, on stack or in a struct, for example.
1124  * Never **ever** access their members directly.
1125  */
1126 
1127 /*!
1128  * @internal
1129  * @brief Structure for XXH32 streaming API.
1130  *
1131  * @note This is only defined when @ref XXH_STATIC_LINKING_ONLY,
1132  * @ref XXH_INLINE_ALL, or @ref XXH_IMPLEMENTATION is defined. Otherwise it is
1133  * an opaque type. This allows fields to safely be changed.
1134  *
1135  * Typedef'd to @ref XXH32_state_t.
1136  * Do not access the members of this struct directly.
1137  * @see XXH64_state_s, XXH3_state_s
1138  */
1139 struct XXH32_state_s {
1140  XXH32_hash_t total_len_32; /*!< Total length hashed, modulo 2^32 */
1141  XXH32_hash_t large_len; /*!< Whether the hash is >= 16 (handles @ref total_len_32 overflow) */
1142  XXH32_hash_t v[4]; /*!< Accumulator lanes */
1143  XXH32_hash_t mem32[4]; /*!< Internal buffer for partial reads. Treated as unsigned char[16]. */
1144  XXH32_hash_t memsize; /*!< Amount of data in @ref mem32 */
1145  XXH32_hash_t reserved; /*!< Reserved field. Do not read nor write to it. */
1146 }; /* typedef'd to XXH32_state_t */
1147 
1148 
1149 #ifndef XXH_NO_LONG_LONG /* defined when there is no 64-bit support */
1150 
1151 /*!
1152  * @internal
1153  * @brief Structure for XXH64 streaming API.
1154  *
1155  * @note This is only defined when @ref XXH_STATIC_LINKING_ONLY,
1156  * @ref XXH_INLINE_ALL, or @ref XXH_IMPLEMENTATION is defined. Otherwise it is
1157  * an opaque type. This allows fields to safely be changed.
1158  *
1159  * Typedef'd to @ref XXH64_state_t.
1160  * Do not access the members of this struct directly.
1161  * @see XXH32_state_s, XXH3_state_s
1162  */
1163 struct XXH64_state_s {
1164  XXH64_hash_t total_len; /*!< Total length hashed. This is always 64-bit. */
1165  XXH64_hash_t v[4]; /*!< Accumulator lanes */
1166  XXH64_hash_t mem64[4]; /*!< Internal buffer for partial reads. Treated as unsigned char[32]. */
1167  XXH32_hash_t memsize; /*!< Amount of data in @ref mem64 */
1168  XXH32_hash_t reserved32; /*!< Reserved field, needed for padding anyways*/
1169  XXH64_hash_t reserved64; /*!< Reserved field. Do not read or write to it. */
1170 }; /* typedef'd to XXH64_state_t */
1171 
1172 #ifndef XXH_NO_XXH3
1173 
1174 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* >= C11 */
1175 # include <stdalign.h>
1176 # define XXH_ALIGN(n) alignas(n)
1177 #elif defined(__cplusplus) && (__cplusplus >= 201103L) /* >= C++11 */
1178 /* In C++ alignas() is a keyword */
1179 # define XXH_ALIGN(n) alignas(n)
1180 #elif defined(__GNUC__)
1181 # define XXH_ALIGN(n) __attribute__ ((aligned(n)))
1182 #elif defined(_MSC_VER)
1183 # define XXH_ALIGN(n) __declspec(align(n))
1184 #else
1185 # define XXH_ALIGN(n) /* disabled */
1186 #endif
1187 
1188 /* Old GCC versions only accept the attribute after the type in structures. */
1189 #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) /* C11+ */ \
1190  && ! (defined(__cplusplus) && (__cplusplus >= 201103L)) /* >= C++11 */ \
1191  && defined(__GNUC__)
1192 # define XXH_ALIGN_MEMBER(align, type) type XXH_ALIGN(align)
1193 #else
1194 # define XXH_ALIGN_MEMBER(align, type) XXH_ALIGN(align) type
1195 #endif
1196 
1197 /*!
1198  * @brief The size of the internal XXH3 buffer.
1199  *
1200  * This is the optimal update size for incremental hashing.
1201  *
1202  * @see XXH3_64b_update(), XXH3_128b_update().
1203  */
1204 #define XXH3_INTERNALBUFFER_SIZE 256
1205 
1206 /*!
1207  * @brief Default size of the secret buffer (and @ref XXH3_kSecret).
1208  *
1209  * This is the size used in @ref XXH3_kSecret and the seeded functions.
1210  *
1211  * Not to be confused with @ref XXH3_SECRET_SIZE_MIN.
1212  */
1213 #define XXH3_SECRET_DEFAULT_SIZE 192
1214 
1215 /*!
1216  * @internal
1217  * @brief Structure for XXH3 streaming API.
1218  *
1219  * @note This is only defined when @ref XXH_STATIC_LINKING_ONLY,
1220  * @ref XXH_INLINE_ALL, or @ref XXH_IMPLEMENTATION is defined.
1221  * Otherwise it is an opaque type.
1222  * Never use this definition in combination with dynamic library.
1223  * This allows fields to safely be changed in the future.
1224  *
1225  * @note ** This structure has a strict alignment requirement of 64 bytes!! **
1226  * Do not allocate this with `malloc()` or `new`,
1227  * it will not be sufficiently aligned.
1228  * Use @ref XXH3_createState() and @ref XXH3_freeState(), or stack allocation.
1229  *
1230  * Typedef'd to @ref XXH3_state_t.
1231  * Do never access the members of this struct directly.
1232  *
1233  * @see XXH3_INITSTATE() for stack initialization.
1234  * @see XXH3_createState(), XXH3_freeState().
1235  * @see XXH32_state_s, XXH64_state_s
1236  */
1237 struct XXH3_state_s {
1238  XXH_ALIGN_MEMBER(64, XXH64_hash_t acc[8]);
1239  /*!< The 8 accumulators. See @ref XXH32_state_s::v and @ref XXH64_state_s::v */
1240  XXH_ALIGN_MEMBER(64, unsigned char customSecret[XXH3_SECRET_DEFAULT_SIZE]);
1241  /*!< Used to store a custom secret generated from a seed. */
1242  XXH_ALIGN_MEMBER(64, unsigned char buffer[XXH3_INTERNALBUFFER_SIZE]);
1243  /*!< The internal buffer. @see XXH32_state_s::mem32 */
1244  XXH32_hash_t bufferedSize;
1245  /*!< The amount of memory in @ref buffer, @see XXH32_state_s::memsize */
1246  XXH32_hash_t useSeed;
1247  /*!< Reserved field. Needed for padding on 64-bit. */
1248  size_t nbStripesSoFar;
1249  /*!< Number or stripes processed. */
1250  XXH64_hash_t totalLen;
1251  /*!< Total length hashed. 64-bit even on 32-bit targets. */
1252  size_t nbStripesPerBlock;
1253  /*!< Number of stripes per block. */
1254  size_t secretLimit;
1255  /*!< Size of @ref customSecret or @ref extSecret */
1256  XXH64_hash_t seed;
1257  /*!< Seed for _withSeed variants. Must be zero otherwise, @see XXH3_INITSTATE() */
1258  XXH64_hash_t reserved64;
1259  /*!< Reserved field. */
1260  const unsigned char* extSecret;
1261  /*!< Reference to an external secret for the _withSecret variants, NULL
1262  * for other variants. */
1263  /* note: there may be some padding at the end due to alignment on 64 bytes */
1264 }; /* typedef'd to XXH3_state_t */
1265 
1266 #undef XXH_ALIGN_MEMBER
1267 
1268 /*!
1269  * @brief Initializes a stack-allocated `XXH3_state_s`.
1270  *
1271  * When the @ref XXH3_state_t structure is merely emplaced on stack,
1272  * it should be initialized with XXH3_INITSTATE() or a memset()
1273  * in case its first reset uses XXH3_NNbits_reset_withSeed().
1274  * This init can be omitted if the first reset uses default or _withSecret mode.
1275  * This operation isn't necessary when the state is created with XXH3_createState().
1276  * Note that this doesn't prepare the state for a streaming operation,
1277  * it's still necessary to use XXH3_NNbits_reset*() afterwards.
1278  */
1279 #define XXH3_INITSTATE(XXH3_state_ptr) { (XXH3_state_ptr)->seed = 0; }
1280 
1281 
1282 /*!
1283  * simple alias to pre-selected XXH3_128bits variant
1284  */
1285 XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH128(const void* data, size_t len, XXH64_hash_t seed);
1286 
1287 
1288 /* === Experimental API === */
1289 /* Symbols defined below must be considered tied to a specific library version. */
1290 
1291 /*!
1292  * XXH3_generateSecret():
1293  *
1294  * Derive a high-entropy secret from any user-defined content, named customSeed.
1295  * The generated secret can be used in combination with `*_withSecret()` functions.
1296  * The `_withSecret()` variants are useful to provide a higher level of protection
1297  * than 64-bit seed, as it becomes much more difficult for an external actor to
1298  * guess how to impact the calculation logic.
1299  *
1300  * The function accepts as input a custom seed of any length and any content,
1301  * and derives from it a high-entropy secret of length @p secretSize into an
1302  * already allocated buffer @p secretBuffer.
1303  *
1304  * The generated secret can then be used with any `*_withSecret()` variant.
1305  * The functions @ref XXH3_128bits_withSecret(), @ref XXH3_64bits_withSecret(),
1306  * @ref XXH3_128bits_reset_withSecret() and @ref XXH3_64bits_reset_withSecret()
1307  * are part of this list. They all accept a `secret` parameter
1308  * which must be large enough for implementation reasons (>= @ref XXH3_SECRET_SIZE_MIN)
1309  * _and_ feature very high entropy (consist of random-looking bytes).
1310  * These conditions can be a high bar to meet, so @ref XXH3_generateSecret() can
1311  * be employed to ensure proper quality.
1312  *
1313  * @p customSeed can be anything. It can have any size, even small ones,
1314  * and its content can be anything, even "poor entropy" sources such as a bunch
1315  * of zeroes. The resulting `secret` will nonetheless provide all required qualities.
1316  *
1317  * @pre
1318  * - @p secretSize must be >= @ref XXH3_SECRET_SIZE_MIN
1319  * - When @p customSeedSize > 0, supplying NULL as customSeed is undefined behavior.
1320  *
1321  * Example code:
1322  * @code{.c}
1323  * #include <stdio.h>
1324  * #include <stdlib.h>
1325  * #include <string.h>
1326  * #define XXH_STATIC_LINKING_ONLY // expose unstable API
1327  * #include "xxhash.h"
1328  * // Hashes argv[2] using the entropy from argv[1].
1329  * int main(int argc, char* argv[])
1330  * {
1331  * char secret[XXH3_SECRET_SIZE_MIN];
1332  * if (argv != 3) { return 1; }
1333  * XXH3_generateSecret(secret, sizeof(secret), argv[1], strlen(argv[1]));
1334  * XXH64_hash_t h = XXH3_64bits_withSecret(
1335  * argv[2], strlen(argv[2]),
1336  * secret, sizeof(secret)
1337  * );
1338  * printf("%016llx\n", (unsigned long long) h);
1339  * }
1340  * @endcode
1341  */
1342 XXH_PUBLIC_API XXH_errorcode XXH3_generateSecret(void* secretBuffer, size_t secretSize, const void* customSeed, size_t customSeedSize);
1343 
1344 /*!
1345  * @brief Generate the same secret as the _withSeed() variants.
1346  *
1347  * The generated secret can be used in combination with
1348  *`*_withSecret()` and `_withSecretandSeed()` variants.
1349  *
1350  * Example C++ `std::string` hash class:
1351  * @code{.cpp}
1352  * #include <string>
1353  * #define XXH_STATIC_LINKING_ONLY // expose unstable API
1354  * #include "xxhash.h"
1355  * // Slow, seeds each time
1356  * class HashSlow {
1357  * XXH64_hash_t seed;
1358  * public:
1359  * HashSlow(XXH64_hash_t s) : seed{s} {}
1360  * size_t operator()(const std::string& x) const {
1361  * return size_t{XXH3_64bits_withSeed(x.c_str(), x.length(), seed)};
1362  * }
1363  * };
1364  * // Fast, caches the seeded secret for future uses.
1365  * class HashFast {
1366  * unsigned char secret[XXH3_SECRET_SIZE_MIN];
1367  * public:
1368  * HashFast(XXH64_hash_t s) {
1369  * XXH3_generateSecret_fromSeed(secret, seed);
1370  * }
1371  * size_t operator()(const std::string& x) const {
1372  * return size_t{
1373  * XXH3_64bits_withSecret(x.c_str(), x.length(), secret, sizeof(secret))
1374  * };
1375  * }
1376  * };
1377  * @endcode
1378  * @param secretBuffer A writable buffer of @ref XXH3_SECRET_SIZE_MIN bytes
1379  * @param seed The seed to seed the state.
1380  */
1381 XXH_PUBLIC_API void XXH3_generateSecret_fromSeed(void* secretBuffer, XXH64_hash_t seed);
1382 
1383 /*!
1384  * These variants generate hash values using either
1385  * @p seed for "short" keys (< XXH3_MIDSIZE_MAX = 240 bytes)
1386  * or @p secret for "large" keys (>= XXH3_MIDSIZE_MAX).
1387  *
1388  * This generally benefits speed, compared to `_withSeed()` or `_withSecret()`.
1389  * `_withSeed()` has to generate the secret on the fly for "large" keys.
1390  * It's fast, but can be perceptible for "not so large" keys (< 1 KB).
1391  * `_withSecret()` has to generate the masks on the fly for "small" keys,
1392  * which requires more instructions than _withSeed() variants.
1393  * Therefore, _withSecretandSeed variant combines the best of both worlds.
1394  *
1395  * When @p secret has been generated by XXH3_generateSecret_fromSeed(),
1396  * this variant produces *exactly* the same results as `_withSeed()` variant,
1397  * hence offering only a pure speed benefit on "large" input,
1398  * by skipping the need to regenerate the secret for every large input.
1399  *
1400  * Another usage scenario is to hash the secret to a 64-bit hash value,
1401  * for example with XXH3_64bits(), which then becomes the seed,
1402  * and then employ both the seed and the secret in _withSecretandSeed().
1403  * On top of speed, an added benefit is that each bit in the secret
1404  * has a 50% chance to swap each bit in the output, via its impact to the seed.
1405  *
1406  * This is not guaranteed when using the secret directly in "small data" scenarios,
1407  * because only portions of the secret are employed for small data.
1408  */
1409 XXH_PUBLIC_API XXH_PUREF XXH64_hash_t
1410 XXH3_64bits_withSecretandSeed(const void* data, size_t len,
1411  const void* secret, size_t secretSize,
1412  XXH64_hash_t seed);
1413 /*! @copydoc XXH3_64bits_withSecretandSeed() */
1415 XXH3_128bits_withSecretandSeed(const void* input, size_t length,
1416  const void* secret, size_t secretSize,
1417  XXH64_hash_t seed64);
1418 #ifndef XXH_NO_STREAM
1419 /*! @copydoc XXH3_64bits_withSecretandSeed() */
1422  const void* secret, size_t secretSize,
1423  XXH64_hash_t seed64);
1424 /*! @copydoc XXH3_64bits_withSecretandSeed() */
1427  const void* secret, size_t secretSize,
1428  XXH64_hash_t seed64);
1429 #endif /* !XXH_NO_STREAM */
1430 
1431 #endif /* !XXH_NO_XXH3 */
1432 #endif /* XXH_NO_LONG_LONG */
1433 #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
1434 # define XXH_IMPLEMENTATION
1435 #endif
1436 
1437 #endif /* defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742) */
1438 
1439 
1440 /* ======================================================================== */
1441 /* ======================================================================== */
1442 /* ======================================================================== */
1443 
1444 
1445 /*-**********************************************************************
1446  * xxHash implementation
1447  *-**********************************************************************
1448  * xxHash's implementation used to be hosted inside xxhash.c.
1449  *
1450  * However, inlining requires implementation to be visible to the compiler,
1451  * hence be included alongside the header.
1452  * Previously, implementation was hosted inside xxhash.c,
1453  * which was then #included when inlining was activated.
1454  * This construction created issues with a few build and install systems,
1455  * as it required xxhash.c to be stored in /include directory.
1456  *
1457  * xxHash implementation is now directly integrated within xxhash.h.
1458  * As a consequence, xxhash.c is no longer needed in /include.
1459  *
1460  * xxhash.c is still available and is still useful.
1461  * In a "normal" setup, when xxhash is not inlined,
1462  * xxhash.h only exposes the prototypes and public symbols,
1463  * while xxhash.c can be built into an object file xxhash.o
1464  * which can then be linked into the final binary.
1465  ************************************************************************/
1466 
1467 #if ( defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) \
1468  || defined(XXH_IMPLEMENTATION) ) && !defined(XXH_IMPLEM_13a8737387)
1469 # define XXH_IMPLEM_13a8737387
1470 
1471 /* *************************************
1472 * Tuning parameters
1473 ***************************************/
1474 
1475 /*!
1476  * @defgroup tuning Tuning parameters
1477  * @{
1478  *
1479  * Various macros to control xxHash's behavior.
1480  */
1481 #ifdef XXH_DOXYGEN
1482 /*!
1483  * @brief Define this to disable 64-bit code.
1484  *
1485  * Useful if only using the @ref XXH32_family and you have a strict C90 compiler.
1486  */
1487 # define XXH_NO_LONG_LONG
1488 # undef XXH_NO_LONG_LONG /* don't actually */
1489 /*!
1490  * @brief Controls how unaligned memory is accessed.
1491  *
1492  * By default, access to unaligned memory is controlled by `memcpy()`, which is
1493  * safe and portable.
1494  *
1495  * Unfortunately, on some target/compiler combinations, the generated assembly
1496  * is sub-optimal.
1497  *
1498  * The below switch allow selection of a different access method
1499  * in the search for improved performance.
1500  *
1501  * @par Possible options:
1502  *
1503  * - `XXH_FORCE_MEMORY_ACCESS=0` (default): `memcpy`
1504  * @par
1505  * Use `memcpy()`. Safe and portable. Note that most modern compilers will
1506  * eliminate the function call and treat it as an unaligned access.
1507  *
1508  * - `XXH_FORCE_MEMORY_ACCESS=1`: `__attribute__((aligned(1)))`
1509  * @par
1510  * Depends on compiler extensions and is therefore not portable.
1511  * This method is safe _if_ your compiler supports it,
1512  * and *generally* as fast or faster than `memcpy`.
1513  *
1514  * - `XXH_FORCE_MEMORY_ACCESS=2`: Direct cast
1515  * @par
1516  * Casts directly and dereferences. This method doesn't depend on the
1517  * compiler, but it violates the C standard as it directly dereferences an
1518  * unaligned pointer. It can generate buggy code on targets which do not
1519  * support unaligned memory accesses, but in some circumstances, it's the
1520  * only known way to get the most performance.
1521  *
1522  * - `XXH_FORCE_MEMORY_ACCESS=3`: Byteshift
1523  * @par
1524  * Also portable. This can generate the best code on old compilers which don't
1525  * inline small `memcpy()` calls, and it might also be faster on big-endian
1526  * systems which lack a native byteswap instruction. However, some compilers
1527  * will emit literal byteshifts even if the target supports unaligned access.
1528  * .
1529  *
1530  * @warning
1531  * Methods 1 and 2 rely on implementation-defined behavior. Use these with
1532  * care, as what works on one compiler/platform/optimization level may cause
1533  * another to read garbage data or even crash.
1534  *
1535  * See http://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html for details.
1536  *
1537  * Prefer these methods in priority order (0 > 3 > 1 > 2)
1538  */
1539 # define XXH_FORCE_MEMORY_ACCESS 0
1540 
1541 /*!
1542  * @def XXH_SIZE_OPT
1543  * @brief Controls how much xxHash optimizes for size.
1544  *
1545  * xxHash, when compiled, tends to result in a rather large binary size. This
1546  * is mostly due to heavy usage to forced inlining and constant folding of the
1547  * @ref XXH3_family to increase performance.
1548  *
1549  * However, some developers prefer size over speed. This option can
1550  * significantly reduce the size of the generated code. When using the `-Os`
1551  * or `-Oz` options on GCC or Clang, this is defined to 1 by default,
1552  * otherwise it is defined to 0.
1553  *
1554  * Most of these size optimizations can be controlled manually.
1555  *
1556  * This is a number from 0-2.
1557  * - `XXH_SIZE_OPT` == 0: Default. xxHash makes no size optimizations. Speed
1558  * comes first.
1559  * - `XXH_SIZE_OPT` == 1: Default for `-Os` and `-Oz`. xxHash is more
1560  * conservative and disables hacks that increase code size. It implies the
1561  * options @ref XXH_NO_INLINE_HINTS == 1, @ref XXH_FORCE_ALIGN_CHECK == 0,
1562  * and @ref XXH3_NEON_LANES == 8 if they are not already defined.
1563  * - `XXH_SIZE_OPT` == 2: xxHash tries to make itself as small as possible.
1564  * Performance may cry. For example, the single shot functions just use the
1565  * streaming API.
1566  */
1567 # define XXH_SIZE_OPT 0
1568 
1569 /*!
1570  * @def XXH_FORCE_ALIGN_CHECK
1571  * @brief If defined to non-zero, adds a special path for aligned inputs (XXH32()
1572  * and XXH64() only).
1573  *
1574  * This is an important performance trick for architectures without decent
1575  * unaligned memory access performance.
1576  *
1577  * It checks for input alignment, and when conditions are met, uses a "fast
1578  * path" employing direct 32-bit/64-bit reads, resulting in _dramatically
1579  * faster_ read speed.
1580  *
1581  * The check costs one initial branch per hash, which is generally negligible,
1582  * but not zero.
1583  *
1584  * Moreover, it's not useful to generate an additional code path if memory
1585  * access uses the same instruction for both aligned and unaligned
1586  * addresses (e.g. x86 and aarch64).
1587  *
1588  * In these cases, the alignment check can be removed by setting this macro to 0.
1589  * Then the code will always use unaligned memory access.
1590  * Align check is automatically disabled on x86, x64, ARM64, and some ARM chips
1591  * which are platforms known to offer good unaligned memory accesses performance.
1592  *
1593  * It is also disabled by default when @ref XXH_SIZE_OPT >= 1.
1594  *
1595  * This option does not affect XXH3 (only XXH32 and XXH64).
1596  */
1597 # define XXH_FORCE_ALIGN_CHECK 0
1598 
1599 /*!
1600  * @def XXH_NO_INLINE_HINTS
1601  * @brief When non-zero, sets all functions to `static`.
1602  *
1603  * By default, xxHash tries to force the compiler to inline almost all internal
1604  * functions.
1605  *
1606  * This can usually improve performance due to reduced jumping and improved
1607  * constant folding, but significantly increases the size of the binary which
1608  * might not be favorable.
1609  *
1610  * Additionally, sometimes the forced inlining can be detrimental to performance,
1611  * depending on the architecture.
1612  *
1613  * XXH_NO_INLINE_HINTS marks all internal functions as static, giving the
1614  * compiler full control on whether to inline or not.
1615  *
1616  * When not optimizing (-O0), using `-fno-inline` with GCC or Clang, or if
1617  * @ref XXH_SIZE_OPT >= 1, this will automatically be defined.
1618  */
1619 # define XXH_NO_INLINE_HINTS 0
1620 
1621 /*!
1622  * @def XXH32_ENDJMP
1623  * @brief Whether to use a jump for `XXH32_finalize`.
1624  *
1625  * For performance, `XXH32_finalize` uses multiple branches in the finalizer.
1626  * This is generally preferable for performance,
1627  * but depending on exact architecture, a jmp may be preferable.
1628  *
1629  * This setting is only possibly making a difference for very small inputs.
1630  */
1631 # define XXH32_ENDJMP 0
1632 
1633 /*!
1634  * @internal
1635  * @brief Redefines old internal names.
1636  *
1637  * For compatibility with code that uses xxHash's internals before the names
1638  * were changed to improve namespacing. There is no other reason to use this.
1639  */
1640 # define XXH_OLD_NAMES
1641 # undef XXH_OLD_NAMES /* don't actually use, it is ugly. */
1642 
1643 /*!
1644  * @def XXH_NO_STREAM
1645  * @brief Disables the streaming API.
1646  *
1647  * When xxHash is not inlined and the streaming functions are not used, disabling
1648  * the streaming functions can improve code size significantly, especially with
1649  * the @ref XXH3_family which tends to make constant folded copies of itself.
1650  */
1651 # define XXH_NO_STREAM
1652 # undef XXH_NO_STREAM /* don't actually */
1653 #endif /* XXH_DOXYGEN */
1654 /*!
1655  * @}
1656  */
1657 
1658 #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
1659  /* prefer __packed__ structures (method 1) for GCC
1660  * < ARMv7 with unaligned access (e.g. Raspbian armhf) still uses byte shifting, so we use memcpy
1661  * which for some reason does unaligned loads. */
1662 # if defined(__GNUC__) && !(defined(__ARM_ARCH) && __ARM_ARCH < 7 && defined(__ARM_FEATURE_UNALIGNED))
1663 # define XXH_FORCE_MEMORY_ACCESS 1
1664 # endif
1665 #endif
1666 
1667 #ifndef XXH_SIZE_OPT
1668  /* default to 1 for -Os or -Oz */
1669 # if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE_SIZE__)
1670 # define XXH_SIZE_OPT 1
1671 # else
1672 # define XXH_SIZE_OPT 0
1673 # endif
1674 #endif
1675 
1676 #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
1677  /* don't check on sizeopt, x86, aarch64, or arm when unaligned access is available */
1678 # if XXH_SIZE_OPT >= 1 || \
1679  defined(__i386) || defined(__x86_64__) || defined(__aarch64__) || defined(__ARM_FEATURE_UNALIGNED) \
1680  || defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64) || defined(_M_ARM) /* visual */
1681 # define XXH_FORCE_ALIGN_CHECK 0
1682 # else
1683 # define XXH_FORCE_ALIGN_CHECK 1
1684 # endif
1685 #endif
1686 
1687 #ifndef XXH_NO_INLINE_HINTS
1688 # if XXH_SIZE_OPT >= 1 || defined(__NO_INLINE__) /* -O0, -fno-inline */
1689 # define XXH_NO_INLINE_HINTS 1
1690 # else
1691 # define XXH_NO_INLINE_HINTS 0
1692 # endif
1693 #endif
1694 
1695 #ifndef XXH32_ENDJMP
1696 /* generally preferable for performance */
1697 # define XXH32_ENDJMP 0
1698 #endif
1699 
1700 /*!
1701  * @defgroup impl Implementation
1702  * @{
1703  */
1704 
1705 
1706 /* *************************************
1707 * Includes & Memory related functions
1708 ***************************************/
1709 #if defined(XXH_NO_STREAM)
1710 /* nothing */
1711 #elif defined(XXH_NO_STDLIB)
1712 
1713 /* When requesting to disable any mention of stdlib,
1714  * the library loses the ability to invoked malloc / free.
1715  * In practice, it means that functions like `XXH*_createState()`
1716  * will always fail, and return NULL.
1717  * This flag is useful in situations where
1718  * xxhash.h is integrated into some kernel, embedded or limited environment
1719  * without access to dynamic allocation.
1720  */
1721 
1722 static XXH_CONSTF void* XXH_malloc(size_t s) { (void)s; return NULL; }
1723 static void XXH_free(void* p) { (void)p; }
1724 
1725 #else
1726 
1727 /*
1728  * Modify the local functions below should you wish to use
1729  * different memory routines for malloc() and free()
1730  */
1731 #include <stdlib.h>
1732 
1733 /*!
1734  * @internal
1735  * @brief Modify this function to use a different routine than malloc().
1736  */
1737 static XXH_MALLOCF void* XXH_malloc(size_t s) { return malloc(s); }
1738 
1739 /*!
1740  * @internal
1741  * @brief Modify this function to use a different routine than free().
1742  */
1743 static void XXH_free(void* p) { free(p); }
1744 
1745 #endif /* XXH_NO_STDLIB */
1746 
1747 #include <string.h>
1748 
1749 /*!
1750  * @internal
1751  * @brief Modify this function to use a different routine than memcpy().
1752  */
1753 static void* XXH_memcpy(void* dest, const void* src, size_t size)
1754 {
1755  return memcpy(dest,src,size);
1756 }
1757 
1758 #include <limits.h> /* ULLONG_MAX */
1759 
1760 
1761 /* *************************************
1762 * Compiler Specific Options
1763 ***************************************/
1764 #if XXH_NO_INLINE_HINTS /* disable inlining hints */
1765 # if defined(__GNUC__) || defined(__clang__)
1766 # define XXH_FORCE_INLINE static __attribute__((unused))
1767 # else
1768 # define XXH_FORCE_INLINE static
1769 # endif
1770 # define XXH_NO_INLINE static
1771 /* enable inlining hints */
1772 #elif defined(__GNUC__) || defined(__clang__)
1773 # define XXH_FORCE_INLINE static __inline__ __attribute__((always_inline, unused))
1774 # define XXH_NO_INLINE static __attribute__((noinline))
1775 #elif defined(_MSC_VER) /* Visual Studio */
1776 # define XXH_FORCE_INLINE static __forceinline
1777 # define XXH_NO_INLINE static __declspec(noinline)
1778 #elif defined (__cplusplus) \
1779  || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* C99 */
1780 # define XXH_FORCE_INLINE static inline
1781 # define XXH_NO_INLINE static
1782 #else
1783 # define XXH_FORCE_INLINE static
1784 # define XXH_NO_INLINE static
1785 #endif
1786 
1787 
1788 
1789 /* *************************************
1790 * Debug
1791 ***************************************/
1792 /*!
1793  * @ingroup tuning
1794  * @def XXH_DEBUGLEVEL
1795  * @brief Sets the debugging level.
1796  *
1797  * XXH_DEBUGLEVEL is expected to be defined externally, typically via the
1798  * compiler's command line options. The value must be a number.
1799  */
1800 #ifndef XXH_DEBUGLEVEL
1801 # ifdef DEBUGLEVEL /* backwards compat */
1802 # define XXH_DEBUGLEVEL DEBUGLEVEL
1803 # else
1804 # define XXH_DEBUGLEVEL 0
1805 # endif
1806 #endif
1807 
1808 #if (XXH_DEBUGLEVEL>=1)
1809 # include <assert.h> /* note: can still be disabled with NDEBUG */
1810 # define XXH_ASSERT(c) assert(c)
1811 #else
1812 # define XXH_ASSERT(c) ((void)0)
1813 #endif
1814 
1815 /* note: use after variable declarations */
1816 #ifndef XXH_STATIC_ASSERT
1817 # if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */
1818 # define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { _Static_assert((c),m); } while(0)
1819 # elif defined(__cplusplus) && (__cplusplus >= 201103L) /* C++11 */
1820 # define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { static_assert((c),m); } while(0)
1821 # else
1822 # define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { struct xxh_sa { char x[(c) ? 1 : -1]; }; } while(0)
1823 # endif
1824 # define XXH_STATIC_ASSERT(c) XXH_STATIC_ASSERT_WITH_MESSAGE((c),#c)
1825 #endif
1826 
1827 /*!
1828  * @internal
1829  * @def XXH_COMPILER_GUARD(var)
1830  * @brief Used to prevent unwanted optimizations for @p var.
1831  *
1832  * It uses an empty GCC inline assembly statement with a register constraint
1833  * which forces @p var into a general purpose register (eg eax, ebx, ecx
1834  * on x86) and marks it as modified.
1835  *
1836  * This is used in a few places to avoid unwanted autovectorization (e.g.
1837  * XXH32_round()). All vectorization we want is explicit via intrinsics,
1838  * and _usually_ isn't wanted elsewhere.
1839  *
1840  * We also use it to prevent unwanted constant folding for AArch64 in
1841  * XXH3_initCustomSecret_scalar().
1842  */
1843 #if defined(__GNUC__) || defined(__clang__)
1844 # define XXH_COMPILER_GUARD(var) __asm__ __volatile__("" : "+r" (var))
1845 #else
1846 # define XXH_COMPILER_GUARD(var) ((void)0)
1847 #endif
1848 
1849 #if defined(__GNUC__) || defined(__clang__)
1850 # define XXH_COMPILER_GUARD_W(var) __asm__ __volatile__("" : "+w" (var))
1851 #else
1852 # define XXH_COMPILER_GUARD_W(var) ((void)0)
1853 #endif
1854 
1855 /* *************************************
1856 * Basic Types
1857 ***************************************/
1858 #if !defined (__VMS) \
1859  && (defined (__cplusplus) \
1860  || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
1861 # include <stdint.h>
1862  typedef uint8_t xxh_u8;
1863 #else
1864  typedef unsigned char xxh_u8;
1865 #endif
1866 typedef XXH32_hash_t xxh_u32;
1867 
1868 #ifdef XXH_OLD_NAMES
1869 # define BYTE xxh_u8
1870 # define U8 xxh_u8
1871 # define U32 xxh_u32
1872 #endif
1873 
1874 /* *** Memory access *** */
1875 
1876 /*!
1877  * @internal
1878  * @fn xxh_u32 XXH_read32(const void* ptr)
1879  * @brief Reads an unaligned 32-bit integer from @p ptr in native endianness.
1880  *
1881  * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
1882  *
1883  * @param ptr The pointer to read from.
1884  * @return The 32-bit native endian integer from the bytes at @p ptr.
1885  */
1886 
1887 /*!
1888  * @internal
1889  * @fn xxh_u32 XXH_readLE32(const void* ptr)
1890  * @brief Reads an unaligned 32-bit little endian integer from @p ptr.
1891  *
1892  * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
1893  *
1894  * @param ptr The pointer to read from.
1895  * @return The 32-bit little endian integer from the bytes at @p ptr.
1896  */
1897 
1898 /*!
1899  * @internal
1900  * @fn xxh_u32 XXH_readBE32(const void* ptr)
1901  * @brief Reads an unaligned 32-bit big endian integer from @p ptr.
1902  *
1903  * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
1904  *
1905  * @param ptr The pointer to read from.
1906  * @return The 32-bit big endian integer from the bytes at @p ptr.
1907  */
1908 
1909 /*!
1910  * @internal
1911  * @fn xxh_u32 XXH_readLE32_align(const void* ptr, XXH_alignment align)
1912  * @brief Like @ref XXH_readLE32(), but has an option for aligned reads.
1913  *
1914  * Affected by @ref XXH_FORCE_MEMORY_ACCESS.
1915  * Note that when @ref XXH_FORCE_ALIGN_CHECK == 0, the @p align parameter is
1916  * always @ref XXH_alignment::XXH_unaligned.
1917  *
1918  * @param ptr The pointer to read from.
1919  * @param align Whether @p ptr is aligned.
1920  * @pre
1921  * If @p align == @ref XXH_alignment::XXH_aligned, @p ptr must be 4 byte
1922  * aligned.
1923  * @return The 32-bit little endian integer from the bytes at @p ptr.
1924  */
1925 
1926 #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
1927 /*
1928  * Manual byteshift. Best for old compilers which don't inline memcpy.
1929  * We actually directly use XXH_readLE32 and XXH_readBE32.
1930  */
1931 #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
1932 
1933 /*
1934  * Force direct memory access. Only works on CPU which support unaligned memory
1935  * access in hardware.
1936  */
1937 static xxh_u32 XXH_read32(const void* memPtr) { return *(const xxh_u32*) memPtr; }
1938 
1939 #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
1940 
1941 /*
1942  * __attribute__((aligned(1))) is supported by gcc and clang. Originally the
1943  * documentation claimed that it only increased the alignment, but actually it
1944  * can decrease it on gcc, clang, and icc:
1945  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502,
1946  * https://gcc.godbolt.org/z/xYez1j67Y.
1947  */
1948 #ifdef XXH_OLD_NAMES
1949 typedef union { xxh_u32 u32; } __attribute__((packed)) unalign;
1950 #endif
1951 static xxh_u32 XXH_read32(const void* ptr)
1952 {
1953  typedef __attribute__((aligned(1))) xxh_u32 xxh_unalign32;
1954  return *((const xxh_unalign32*)ptr);
1955 }
1956 
1957 #else
1958 
1959 /*
1960  * Portable and safe solution. Generally efficient.
1961  * see: http://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html
1962  */
1963 static xxh_u32 XXH_read32(const void* memPtr)
1964 {
1965  xxh_u32 val;
1966  XXH_memcpy(&val, memPtr, sizeof(val));
1967  return val;
1968 }
1969 
1970 #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
1971 
1972 
1973 /* *** Endianness *** */
1974 
1975 /*!
1976  * @ingroup tuning
1977  * @def XXH_CPU_LITTLE_ENDIAN
1978  * @brief Whether the target is little endian.
1979  *
1980  * Defined to 1 if the target is little endian, or 0 if it is big endian.
1981  * It can be defined externally, for example on the compiler command line.
1982  *
1983  * If it is not defined,
1984  * a runtime check (which is usually constant folded) is used instead.
1985  *
1986  * @note
1987  * This is not necessarily defined to an integer constant.
1988  *
1989  * @see XXH_isLittleEndian() for the runtime check.
1990  */
1991 #ifndef XXH_CPU_LITTLE_ENDIAN
1992 /*
1993  * Try to detect endianness automatically, to avoid the nonstandard behavior
1994  * in `XXH_isLittleEndian()`
1995  */
1996 # if defined(_WIN32) /* Windows is always little endian */ \
1997  || defined(__LITTLE_ENDIAN__) \
1998  || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
1999 # define XXH_CPU_LITTLE_ENDIAN 1
2000 # elif defined(__BIG_ENDIAN__) \
2001  || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
2002 # define XXH_CPU_LITTLE_ENDIAN 0
2003 # else
2004 /*!
2005  * @internal
2006  * @brief Runtime check for @ref XXH_CPU_LITTLE_ENDIAN.
2007  *
2008  * Most compilers will constant fold this.
2009  */
2010 static int XXH_isLittleEndian(void)
2011 {
2012  /*
2013  * Portable and well-defined behavior.
2014  * Don't use static: it is detrimental to performance.
2015  */
2016  const union { xxh_u32 u; xxh_u8 c[4]; } one = { 1 };
2017  return one.c[0];
2018 }
2019 # define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian()
2020 # endif
2021 #endif
2022 
2023 
2024 
2025 
2026 /* ****************************************
2027 * Compiler-specific Functions and Macros
2028 ******************************************/
2029 #define XXH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2030 
2031 #ifdef __has_builtin
2032 # define XXH_HAS_BUILTIN(x) __has_builtin(x)
2033 #else
2034 # define XXH_HAS_BUILTIN(x) 0
2035 #endif
2036 
2037 /*!
2038  * @internal
2039  * @def XXH_rotl32(x,r)
2040  * @brief 32-bit rotate left.
2041  *
2042  * @param x The 32-bit integer to be rotated.
2043  * @param r The number of bits to rotate.
2044  * @pre
2045  * @p r > 0 && @p r < 32
2046  * @note
2047  * @p x and @p r may be evaluated multiple times.
2048  * @return The rotated result.
2049  */
2050 #if !defined(NO_CLANG_BUILTIN) && XXH_HAS_BUILTIN(__builtin_rotateleft32) \
2051  && XXH_HAS_BUILTIN(__builtin_rotateleft64)
2052 # define XXH_rotl32 __builtin_rotateleft32
2053 # define XXH_rotl64 __builtin_rotateleft64
2054 /* Note: although _rotl exists for minGW (GCC under windows), performance seems poor */
2055 #elif defined(_MSC_VER)
2056 # define XXH_rotl32(x,r) _rotl(x,r)
2057 # define XXH_rotl64(x,r) _rotl64(x,r)
2058 #else
2059 # define XXH_rotl32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
2060 # define XXH_rotl64(x,r) (((x) << (r)) | ((x) >> (64 - (r))))
2061 #endif
2062 
2063 /*!
2064  * @internal
2065  * @fn xxh_u32 XXH_swap32(xxh_u32 x)
2066  * @brief A 32-bit byteswap.
2067  *
2068  * @param x The 32-bit integer to byteswap.
2069  * @return @p x, byteswapped.
2070  */
2071 #if defined(_MSC_VER) /* Visual Studio */
2072 # define XXH_swap32 _byteswap_ulong
2073 #elif XXH_GCC_VERSION >= 403
2074 # define XXH_swap32 __builtin_bswap32
2075 #else
2076 static xxh_u32 XXH_swap32 (xxh_u32 x)
2077 {
2078  return ((x << 24) & 0xff000000 ) |
2079  ((x << 8) & 0x00ff0000 ) |
2080  ((x >> 8) & 0x0000ff00 ) |
2081  ((x >> 24) & 0x000000ff );
2082 }
2083 #endif
2084 
2085 
2086 /* ***************************
2087 * Memory reads
2088 *****************************/
2089 
2090 /*!
2091  * @internal
2092  * @brief Enum to indicate whether a pointer is aligned.
2093  */
2094 typedef enum {
2095  XXH_aligned, /*!< Aligned */
2096  XXH_unaligned /*!< Possibly unaligned */
2097 } XXH_alignment;
2098 
2099 /*
2100  * XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load.
2101  *
2102  * This is ideal for older compilers which don't inline memcpy.
2103  */
2104 #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
2105 
2106 XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* memPtr)
2107 {
2108  const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
2109  return bytePtr[0]
2110  | ((xxh_u32)bytePtr[1] << 8)
2111  | ((xxh_u32)bytePtr[2] << 16)
2112  | ((xxh_u32)bytePtr[3] << 24);
2113 }
2114 
2115 XXH_FORCE_INLINE xxh_u32 XXH_readBE32(const void* memPtr)
2116 {
2117  const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
2118  return bytePtr[3]
2119  | ((xxh_u32)bytePtr[2] << 8)
2120  | ((xxh_u32)bytePtr[1] << 16)
2121  | ((xxh_u32)bytePtr[0] << 24);
2122 }
2123 
2124 #else
2125 XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* ptr)
2126 {
2127  return XXH_CPU_LITTLE_ENDIAN ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
2128 }
2129 
2130 static xxh_u32 XXH_readBE32(const void* ptr)
2131 {
2132  return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
2133 }
2134 #endif
2135 
2136 XXH_FORCE_INLINE xxh_u32
2137 XXH_readLE32_align(const void* ptr, XXH_alignment align)
2138 {
2139  if (align==XXH_unaligned) {
2140  return XXH_readLE32(ptr);
2141  } else {
2142  return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u32*)ptr : XXH_swap32(*(const xxh_u32*)ptr);
2143  }
2144 }
2145 
2146 
2147 /* *************************************
2148 * Misc
2149 ***************************************/
2150 /*! @ingroup public */
2151 XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
2152 
2153 
2154 /* *******************************************************************
2155 * 32-bit hash functions
2156 *********************************************************************/
2157 /*!
2158  * @}
2159  * @defgroup XXH32_impl XXH32 implementation
2160  * @ingroup impl
2161  *
2162  * Details on the XXH32 implementation.
2163  * @{
2164  */
2165  /* #define instead of static const, to be used as initializers */
2166 #define XXH_PRIME32_1 0x9E3779B1U /*!< 0b10011110001101110111100110110001 */
2167 #define XXH_PRIME32_2 0x85EBCA77U /*!< 0b10000101111010111100101001110111 */
2168 #define XXH_PRIME32_3 0xC2B2AE3DU /*!< 0b11000010101100101010111000111101 */
2169 #define XXH_PRIME32_4 0x27D4EB2FU /*!< 0b00100111110101001110101100101111 */
2170 #define XXH_PRIME32_5 0x165667B1U /*!< 0b00010110010101100110011110110001 */
2171 
2172 #ifdef XXH_OLD_NAMES
2173 # define PRIME32_1 XXH_PRIME32_1
2174 # define PRIME32_2 XXH_PRIME32_2
2175 # define PRIME32_3 XXH_PRIME32_3
2176 # define PRIME32_4 XXH_PRIME32_4
2177 # define PRIME32_5 XXH_PRIME32_5
2178 #endif
2179 
2180 /*!
2181  * @internal
2182  * @brief Normal stripe processing routine.
2183  *
2184  * This shuffles the bits so that any bit from @p input impacts several bits in
2185  * @p acc.
2186  *
2187  * @param acc The accumulator lane.
2188  * @param input The stripe of input to mix.
2189  * @return The mixed accumulator lane.
2190  */
2191 static xxh_u32 XXH32_round(xxh_u32 acc, xxh_u32 input)
2192 {
2193  acc += input * XXH_PRIME32_2;
2194  acc = XXH_rotl32(acc, 13);
2195  acc *= XXH_PRIME32_1;
2196 #if (defined(__SSE4_1__) || defined(__aarch64__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
2197  /*
2198  * UGLY HACK:
2199  * A compiler fence is the only thing that prevents GCC and Clang from
2200  * autovectorizing the XXH32 loop (pragmas and attributes don't work for some
2201  * reason) without globally disabling SSE4.1.
2202  *
2203  * The reason we want to avoid vectorization is because despite working on
2204  * 4 integers at a time, there are multiple factors slowing XXH32 down on
2205  * SSE4:
2206  * - There's a ridiculous amount of lag from pmulld (10 cycles of latency on
2207  * newer chips!) making it slightly slower to multiply four integers at
2208  * once compared to four integers independently. Even when pmulld was
2209  * fastest, Sandy/Ivy Bridge, it is still not worth it to go into SSE
2210  * just to multiply unless doing a long operation.
2211  *
2212  * - Four instructions are required to rotate,
2213  * movqda tmp, v // not required with VEX encoding
2214  * pslld tmp, 13 // tmp <<= 13
2215  * psrld v, 19 // x >>= 19
2216  * por v, tmp // x |= tmp
2217  * compared to one for scalar:
2218  * roll v, 13 // reliably fast across the board
2219  * shldl v, v, 13 // Sandy Bridge and later prefer this for some reason
2220  *
2221  * - Instruction level parallelism is actually more beneficial here because
2222  * the SIMD actually serializes this operation: While v1 is rotating, v2
2223  * can load data, while v3 can multiply. SSE forces them to operate
2224  * together.
2225  *
2226  * This is also enabled on AArch64, as Clang autovectorizes it incorrectly
2227  * and it is pointless writing a NEON implementation that is basically the
2228  * same speed as scalar for XXH32.
2229  */
2230  XXH_COMPILER_GUARD(acc);
2231 #endif
2232  return acc;
2233 }
2234 
2235 /*!
2236  * @internal
2237  * @brief Mixes all bits to finalize the hash.
2238  *
2239  * The final mix ensures that all input bits have a chance to impact any bit in
2240  * the output digest, resulting in an unbiased distribution.
2241  *
2242  * @param hash The hash to avalanche.
2243  * @return The avalanched hash.
2244  */
2245 static xxh_u32 XXH32_avalanche(xxh_u32 hash)
2246 {
2247  hash ^= hash >> 15;
2248  hash *= XXH_PRIME32_2;
2249  hash ^= hash >> 13;
2250  hash *= XXH_PRIME32_3;
2251  hash ^= hash >> 16;
2252  return hash;
2253 }
2254 
2255 #define XXH_get32bits(p) XXH_readLE32_align(p, align)
2256 
2257 /*!
2258  * @internal
2259  * @brief Processes the last 0-15 bytes of @p ptr.
2260  *
2261  * There may be up to 15 bytes remaining to consume from the input.
2262  * This final stage will digest them to ensure that all input bytes are present
2263  * in the final mix.
2264  *
2265  * @param hash The hash to finalize.
2266  * @param ptr The pointer to the remaining input.
2267  * @param len The remaining length, modulo 16.
2268  * @param align Whether @p ptr is aligned.
2269  * @return The finalized hash.
2270  * @see XXH64_finalize().
2271  */
2272 static XXH_PUREF xxh_u32
2273 XXH32_finalize(xxh_u32 hash, const xxh_u8* ptr, size_t len, XXH_alignment align)
2274 {
2275 #define XXH_PROCESS1 do { \
2276  hash += (*ptr++) * XXH_PRIME32_5; \
2277  hash = XXH_rotl32(hash, 11) * XXH_PRIME32_1; \
2278 } while (0)
2279 
2280 #define XXH_PROCESS4 do { \
2281  hash += XXH_get32bits(ptr) * XXH_PRIME32_3; \
2282  ptr += 4; \
2283  hash = XXH_rotl32(hash, 17) * XXH_PRIME32_4; \
2284 } while (0)
2285 
2286  if (ptr==NULL) XXH_ASSERT(len == 0);
2287 
2288  /* Compact rerolled version; generally faster */
2289  if (!XXH32_ENDJMP) {
2290  len &= 15;
2291  while (len >= 4) {
2292  XXH_PROCESS4;
2293  len -= 4;
2294  }
2295  while (len > 0) {
2296  XXH_PROCESS1;
2297  --len;
2298  }
2299  return XXH32_avalanche(hash);
2300  } else {
2301  switch(len&15) /* or switch(bEnd - p) */ {
2302  case 12: XXH_PROCESS4;
2303  XXH_FALLTHROUGH; /* fallthrough */
2304  case 8: XXH_PROCESS4;
2305  XXH_FALLTHROUGH; /* fallthrough */
2306  case 4: XXH_PROCESS4;
2307  return XXH32_avalanche(hash);
2308 
2309  case 13: XXH_PROCESS4;
2310  XXH_FALLTHROUGH; /* fallthrough */
2311  case 9: XXH_PROCESS4;
2312  XXH_FALLTHROUGH; /* fallthrough */
2313  case 5: XXH_PROCESS4;
2314  XXH_PROCESS1;
2315  return XXH32_avalanche(hash);
2316 
2317  case 14: XXH_PROCESS4;
2318  XXH_FALLTHROUGH; /* fallthrough */
2319  case 10: XXH_PROCESS4;
2320  XXH_FALLTHROUGH; /* fallthrough */
2321  case 6: XXH_PROCESS4;
2322  XXH_PROCESS1;
2323  XXH_PROCESS1;
2324  return XXH32_avalanche(hash);
2325 
2326  case 15: XXH_PROCESS4;
2327  XXH_FALLTHROUGH; /* fallthrough */
2328  case 11: XXH_PROCESS4;
2329  XXH_FALLTHROUGH; /* fallthrough */
2330  case 7: XXH_PROCESS4;
2331  XXH_FALLTHROUGH; /* fallthrough */
2332  case 3: XXH_PROCESS1;
2333  XXH_FALLTHROUGH; /* fallthrough */
2334  case 2: XXH_PROCESS1;
2335  XXH_FALLTHROUGH; /* fallthrough */
2336  case 1: XXH_PROCESS1;
2337  XXH_FALLTHROUGH; /* fallthrough */
2338  case 0: return XXH32_avalanche(hash);
2339  }
2340  XXH_ASSERT(0);
2341  return hash; /* reaching this point is deemed impossible */
2342  }
2343 }
2344 
2345 #ifdef XXH_OLD_NAMES
2346 # define PROCESS1 XXH_PROCESS1
2347 # define PROCESS4 XXH_PROCESS4
2348 #else
2349 # undef XXH_PROCESS1
2350 # undef XXH_PROCESS4
2351 #endif
2352 
2353 /*!
2354  * @internal
2355  * @brief The implementation for @ref XXH32().
2356  *
2357  * @param input , len , seed Directly passed from @ref XXH32().
2358  * @param align Whether @p input is aligned.
2359  * @return The calculated hash.
2360  */
2361 XXH_FORCE_INLINE XXH_PUREF xxh_u32
2362 XXH32_endian_align(const xxh_u8* input, size_t len, xxh_u32 seed, XXH_alignment align)
2363 {
2364  xxh_u32 h32;
2365 
2366  if (input==NULL) XXH_ASSERT(len == 0);
2367 
2368  if (len>=16) {
2369  const xxh_u8* const bEnd = input + len;
2370  const xxh_u8* const limit = bEnd - 15;
2371  xxh_u32 v1 = seed + XXH_PRIME32_1 + XXH_PRIME32_2;
2372  xxh_u32 v2 = seed + XXH_PRIME32_2;
2373  xxh_u32 v3 = seed + 0;
2374  xxh_u32 v4 = seed - XXH_PRIME32_1;
2375 
2376  do {
2377  v1 = XXH32_round(v1, XXH_get32bits(input)); input += 4;
2378  v2 = XXH32_round(v2, XXH_get32bits(input)); input += 4;
2379  v3 = XXH32_round(v3, XXH_get32bits(input)); input += 4;
2380  v4 = XXH32_round(v4, XXH_get32bits(input)); input += 4;
2381  } while (input < limit);
2382 
2383  h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7)
2384  + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
2385  } else {
2386  h32 = seed + XXH_PRIME32_5;
2387  }
2388 
2389  h32 += (xxh_u32)len;
2390 
2391  return XXH32_finalize(h32, input, len&15, align);
2392 }
2393 
2394 /*! @ingroup XXH32_family */
2395 XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t len, XXH32_hash_t seed)
2396 {
2397 #if !defined(XXH_NO_STREAM) && XXH_SIZE_OPT >= 2
2398  /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
2399  XXH32_state_t state;
2400  XXH32_reset(&state, seed);
2401  XXH32_update(&state, (const xxh_u8*)input, len);
2402  return XXH32_digest(&state);
2403 #else
2404  if (XXH_FORCE_ALIGN_CHECK) {
2405  if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
2406  return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
2407  } }
2408 
2409  return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
2410 #endif
2411 }
2412 
2413 
2414 
2415 /******* Hash streaming *******/
2416 #ifndef XXH_NO_STREAM
2417 /*! @ingroup XXH32_family */
2419 {
2420  return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
2421 }
2422 /*! @ingroup XXH32_family */
2424 {
2425  XXH_free(statePtr);
2426  return XXH_OK;
2427 }
2428 
2429 /*! @ingroup XXH32_family */
2430 XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t* srcState)
2431 {
2432  XXH_memcpy(dstState, srcState, sizeof(*dstState));
2433 }
2434 
2435 /*! @ingroup XXH32_family */
2436 XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed)
2437 {
2438  XXH_ASSERT(statePtr != NULL);
2439  memset(statePtr, 0, sizeof(*statePtr));
2440  statePtr->v[0] = seed + XXH_PRIME32_1 + XXH_PRIME32_2;
2441  statePtr->v[1] = seed + XXH_PRIME32_2;
2442  statePtr->v[2] = seed + 0;
2443  statePtr->v[3] = seed - XXH_PRIME32_1;
2444  return XXH_OK;
2445 }
2446 
2447 
2448 /*! @ingroup XXH32_family */
2450 XXH32_update(XXH32_state_t* state, const void* input, size_t len)
2451 {
2452  if (input==NULL) {
2453  XXH_ASSERT(len == 0);
2454  return XXH_OK;
2455  }
2456 
2457  { const xxh_u8* p = (const xxh_u8*)input;
2458  const xxh_u8* const bEnd = p + len;
2459 
2460  state->total_len_32 += (XXH32_hash_t)len;
2461  state->large_len |= (XXH32_hash_t)((len>=16) | (state->total_len_32>=16));
2462 
2463  if (state->memsize + len < 16) { /* fill in tmp buffer */
2464  XXH_memcpy((xxh_u8*)(state->mem32) + state->memsize, input, len);
2465  state->memsize += (XXH32_hash_t)len;
2466  return XXH_OK;
2467  }
2468 
2469  if (state->memsize) { /* some data left from previous update */
2470  XXH_memcpy((xxh_u8*)(state->mem32) + state->memsize, input, 16-state->memsize);
2471  { const xxh_u32* p32 = state->mem32;
2472  state->v[0] = XXH32_round(state->v[0], XXH_readLE32(p32)); p32++;
2473  state->v[1] = XXH32_round(state->v[1], XXH_readLE32(p32)); p32++;
2474  state->v[2] = XXH32_round(state->v[2], XXH_readLE32(p32)); p32++;
2475  state->v[3] = XXH32_round(state->v[3], XXH_readLE32(p32));
2476  }
2477  p += 16-state->memsize;
2478  state->memsize = 0;
2479  }
2480 
2481  if (p <= bEnd-16) {
2482  const xxh_u8* const limit = bEnd - 16;
2483 
2484  do {
2485  state->v[0] = XXH32_round(state->v[0], XXH_readLE32(p)); p+=4;
2486  state->v[1] = XXH32_round(state->v[1], XXH_readLE32(p)); p+=4;
2487  state->v[2] = XXH32_round(state->v[2], XXH_readLE32(p)); p+=4;
2488  state->v[3] = XXH32_round(state->v[3], XXH_readLE32(p)); p+=4;
2489  } while (p<=limit);
2490 
2491  }
2492 
2493  if (p < bEnd) {
2494  XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
2495  state->memsize = (unsigned)(bEnd-p);
2496  }
2497  }
2498 
2499  return XXH_OK;
2500 }
2501 
2502 
2503 /*! @ingroup XXH32_family */
2504 XXH_PUBLIC_API XXH32_hash_t XXH32_digest(const XXH32_state_t* state)
2505 {
2506  xxh_u32 h32;
2507 
2508  if (state->large_len) {
2509  h32 = XXH_rotl32(state->v[0], 1)
2510  + XXH_rotl32(state->v[1], 7)
2511  + XXH_rotl32(state->v[2], 12)
2512  + XXH_rotl32(state->v[3], 18);
2513  } else {
2514  h32 = state->v[2] /* == seed */ + XXH_PRIME32_5;
2515  }
2516 
2517  h32 += state->total_len_32;
2518 
2519  return XXH32_finalize(h32, (const xxh_u8*)state->mem32, state->memsize, XXH_aligned);
2520 }
2521 #endif /* !XXH_NO_STREAM */
2522 
2523 /******* Canonical representation *******/
2524 
2525 /*!
2526  * @ingroup XXH32_family
2527  * The default return values from XXH functions are unsigned 32 and 64 bit
2528  * integers.
2529  *
2530  * The canonical representation uses big endian convention, the same convention
2531  * as human-readable numbers (large digits first).
2532  *
2533  * This way, hash values can be written into a file or buffer, remaining
2534  * comparable across different systems.
2535  *
2536  * The following functions allow transformation of hash values to and from their
2537  * canonical format.
2538  */
2540 {
2541  XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
2542  if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
2543  XXH_memcpy(dst, &hash, sizeof(*dst));
2544 }
2545 /*! @ingroup XXH32_family */
2547 {
2548  return XXH_readBE32(src);
2549 }
2550 
2551 
2552 #ifndef XXH_NO_LONG_LONG
2553 
2554 /* *******************************************************************
2555 * 64-bit hash functions
2556 *********************************************************************/
2557 /*!
2558  * @}
2559  * @ingroup impl
2560  * @{
2561  */
2562 /******* Memory access *******/
2563 
2564 typedef XXH64_hash_t xxh_u64;
2565 
2566 #ifdef XXH_OLD_NAMES
2567 # define U64 xxh_u64
2568 #endif
2569 
2570 #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
2571 /*
2572  * Manual byteshift. Best for old compilers which don't inline memcpy.
2573  * We actually directly use XXH_readLE64 and XXH_readBE64.
2574  */
2575 #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
2576 
2577 /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
2578 static xxh_u64 XXH_read64(const void* memPtr)
2579 {
2580  return *(const xxh_u64*) memPtr;
2581 }
2582 
2583 #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
2584 
2585 /*
2586  * __attribute__((aligned(1))) is supported by gcc and clang. Originally the
2587  * documentation claimed that it only increased the alignment, but actually it
2588  * can decrease it on gcc, clang, and icc:
2589  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502,
2590  * https://gcc.godbolt.org/z/xYez1j67Y.
2591  */
2592 #ifdef XXH_OLD_NAMES
2593 typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((packed)) unalign64;
2594 #endif
2595 static xxh_u64 XXH_read64(const void* ptr)
2596 {
2597  typedef __attribute__((aligned(1))) xxh_u64 xxh_unalign64;
2598  return *((const xxh_unalign64*)ptr);
2599 }
2600 
2601 #else
2602 
2603 /*
2604  * Portable and safe solution. Generally efficient.
2605  * see: http://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html
2606  */
2607 static xxh_u64 XXH_read64(const void* memPtr)
2608 {
2609  xxh_u64 val;
2610  XXH_memcpy(&val, memPtr, sizeof(val));
2611  return val;
2612 }
2613 
2614 #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
2615 
2616 #if defined(_MSC_VER) /* Visual Studio */
2617 # define XXH_swap64 _byteswap_uint64
2618 #elif XXH_GCC_VERSION >= 403
2619 # define XXH_swap64 __builtin_bswap64
2620 #else
2621 static xxh_u64 XXH_swap64(xxh_u64 x)
2622 {
2623  return ((x << 56) & 0xff00000000000000ULL) |
2624  ((x << 40) & 0x00ff000000000000ULL) |
2625  ((x << 24) & 0x0000ff0000000000ULL) |
2626  ((x << 8) & 0x000000ff00000000ULL) |
2627  ((x >> 8) & 0x00000000ff000000ULL) |
2628  ((x >> 24) & 0x0000000000ff0000ULL) |
2629  ((x >> 40) & 0x000000000000ff00ULL) |
2630  ((x >> 56) & 0x00000000000000ffULL);
2631 }
2632 #endif
2633 
2634 
2635 /* XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load. */
2636 #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
2637 
2638 XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* memPtr)
2639 {
2640  const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
2641  return bytePtr[0]
2642  | ((xxh_u64)bytePtr[1] << 8)
2643  | ((xxh_u64)bytePtr[2] << 16)
2644  | ((xxh_u64)bytePtr[3] << 24)
2645  | ((xxh_u64)bytePtr[4] << 32)
2646  | ((xxh_u64)bytePtr[5] << 40)
2647  | ((xxh_u64)bytePtr[6] << 48)
2648  | ((xxh_u64)bytePtr[7] << 56);
2649 }
2650 
2651 XXH_FORCE_INLINE xxh_u64 XXH_readBE64(const void* memPtr)
2652 {
2653  const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
2654  return bytePtr[7]
2655  | ((xxh_u64)bytePtr[6] << 8)
2656  | ((xxh_u64)bytePtr[5] << 16)
2657  | ((xxh_u64)bytePtr[4] << 24)
2658  | ((xxh_u64)bytePtr[3] << 32)
2659  | ((xxh_u64)bytePtr[2] << 40)
2660  | ((xxh_u64)bytePtr[1] << 48)
2661  | ((xxh_u64)bytePtr[0] << 56);
2662 }
2663 
2664 #else
2665 XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* ptr)
2666 {
2667  return XXH_CPU_LITTLE_ENDIAN ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
2668 }
2669 
2670 static xxh_u64 XXH_readBE64(const void* ptr)
2671 {
2672  return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
2673 }
2674 #endif
2675 
2676 XXH_FORCE_INLINE xxh_u64
2677 XXH_readLE64_align(const void* ptr, XXH_alignment align)
2678 {
2679  if (align==XXH_unaligned)
2680  return XXH_readLE64(ptr);
2681  else
2682  return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u64*)ptr : XXH_swap64(*(const xxh_u64*)ptr);
2683 }
2684 
2685 
2686 /******* xxh64 *******/
2687 /*!
2688  * @}
2689  * @defgroup XXH64_impl XXH64 implementation
2690  * @ingroup impl
2691  *
2692  * Details on the XXH64 implementation.
2693  * @{
2694  */
2695 /* #define rather that static const, to be used as initializers */
2696 #define XXH_PRIME64_1 0x9E3779B185EBCA87ULL /*!< 0b1001111000110111011110011011000110000101111010111100101010000111 */
2697 #define XXH_PRIME64_2 0xC2B2AE3D27D4EB4FULL /*!< 0b1100001010110010101011100011110100100111110101001110101101001111 */
2698 #define XXH_PRIME64_3 0x165667B19E3779F9ULL /*!< 0b0001011001010110011001111011000110011110001101110111100111111001 */
2699 #define XXH_PRIME64_4 0x85EBCA77C2B2AE63ULL /*!< 0b1000010111101011110010100111011111000010101100101010111001100011 */
2700 #define XXH_PRIME64_5 0x27D4EB2F165667C5ULL /*!< 0b0010011111010100111010110010111100010110010101100110011111000101 */
2701 
2702 #ifdef XXH_OLD_NAMES
2703 # define PRIME64_1 XXH_PRIME64_1
2704 # define PRIME64_2 XXH_PRIME64_2
2705 # define PRIME64_3 XXH_PRIME64_3
2706 # define PRIME64_4 XXH_PRIME64_4
2707 # define PRIME64_5 XXH_PRIME64_5
2708 #endif
2709 
2710 /*! @copydoc XXH32_round */
2711 static xxh_u64 XXH64_round(xxh_u64 acc, xxh_u64 input)
2712 {
2713  acc += input * XXH_PRIME64_2;
2714  acc = XXH_rotl64(acc, 31);
2715  acc *= XXH_PRIME64_1;
2716  return acc;
2717 }
2718 
2719 static xxh_u64 XXH64_mergeRound(xxh_u64 acc, xxh_u64 val)
2720 {
2721  val = XXH64_round(0, val);
2722  acc ^= val;
2723  acc = acc * XXH_PRIME64_1 + XXH_PRIME64_4;
2724  return acc;
2725 }
2726 
2727 /*! @copydoc XXH32_avalanche */
2728 static xxh_u64 XXH64_avalanche(xxh_u64 hash)
2729 {
2730  hash ^= hash >> 33;
2731  hash *= XXH_PRIME64_2;
2732  hash ^= hash >> 29;
2733  hash *= XXH_PRIME64_3;
2734  hash ^= hash >> 32;
2735  return hash;
2736 }
2737 
2738 
2739 #define XXH_get64bits(p) XXH_readLE64_align(p, align)
2740 
2741 /*!
2742  * @internal
2743  * @brief Processes the last 0-31 bytes of @p ptr.
2744  *
2745  * There may be up to 31 bytes remaining to consume from the input.
2746  * This final stage will digest them to ensure that all input bytes are present
2747  * in the final mix.
2748  *
2749  * @param hash The hash to finalize.
2750  * @param ptr The pointer to the remaining input.
2751  * @param len The remaining length, modulo 32.
2752  * @param align Whether @p ptr is aligned.
2753  * @return The finalized hash
2754  * @see XXH32_finalize().
2755  */
2756 static XXH_PUREF xxh_u64
2757 XXH64_finalize(xxh_u64 hash, const xxh_u8* ptr, size_t len, XXH_alignment align)
2758 {
2759  if (ptr==NULL) XXH_ASSERT(len == 0);
2760  len &= 31;
2761  while (len >= 8) {
2762  xxh_u64 const k1 = XXH64_round(0, XXH_get64bits(ptr));
2763  ptr += 8;
2764  hash ^= k1;
2765  hash = XXH_rotl64(hash,27) * XXH_PRIME64_1 + XXH_PRIME64_4;
2766  len -= 8;
2767  }
2768  if (len >= 4) {
2769  hash ^= (xxh_u64)(XXH_get32bits(ptr)) * XXH_PRIME64_1;
2770  ptr += 4;
2771  hash = XXH_rotl64(hash, 23) * XXH_PRIME64_2 + XXH_PRIME64_3;
2772  len -= 4;
2773  }
2774  while (len > 0) {
2775  hash ^= (*ptr++) * XXH_PRIME64_5;
2776  hash = XXH_rotl64(hash, 11) * XXH_PRIME64_1;
2777  --len;
2778  }
2779  return XXH64_avalanche(hash);
2780 }
2781 
2782 #ifdef XXH_OLD_NAMES
2783 # define PROCESS1_64 XXH_PROCESS1_64
2784 # define PROCESS4_64 XXH_PROCESS4_64
2785 # define PROCESS8_64 XXH_PROCESS8_64
2786 #else
2787 # undef XXH_PROCESS1_64
2788 # undef XXH_PROCESS4_64
2789 # undef XXH_PROCESS8_64
2790 #endif
2791 
2792 /*!
2793  * @internal
2794  * @brief The implementation for @ref XXH64().
2795  *
2796  * @param input , len , seed Directly passed from @ref XXH64().
2797  * @param align Whether @p input is aligned.
2798  * @return The calculated hash.
2799  */
2800 XXH_FORCE_INLINE XXH_PUREF xxh_u64
2801 XXH64_endian_align(const xxh_u8* input, size_t len, xxh_u64 seed, XXH_alignment align)
2802 {
2803  xxh_u64 h64;
2804  if (input==NULL) XXH_ASSERT(len == 0);
2805 
2806  if (len>=32) {
2807  const xxh_u8* const bEnd = input + len;
2808  const xxh_u8* const limit = bEnd - 31;
2809  xxh_u64 v1 = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
2810  xxh_u64 v2 = seed + XXH_PRIME64_2;
2811  xxh_u64 v3 = seed + 0;
2812  xxh_u64 v4 = seed - XXH_PRIME64_1;
2813 
2814  do {
2815  v1 = XXH64_round(v1, XXH_get64bits(input)); input+=8;
2816  v2 = XXH64_round(v2, XXH_get64bits(input)); input+=8;
2817  v3 = XXH64_round(v3, XXH_get64bits(input)); input+=8;
2818  v4 = XXH64_round(v4, XXH_get64bits(input)); input+=8;
2819  } while (input<limit);
2820 
2821  h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
2822  h64 = XXH64_mergeRound(h64, v1);
2823  h64 = XXH64_mergeRound(h64, v2);
2824  h64 = XXH64_mergeRound(h64, v3);
2825  h64 = XXH64_mergeRound(h64, v4);
2826 
2827  } else {
2828  h64 = seed + XXH_PRIME64_5;
2829  }
2830 
2831  h64 += (xxh_u64) len;
2832 
2833  return XXH64_finalize(h64, input, len, align);
2834 }
2835 
2836 
2837 /*! @ingroup XXH64_family */
2838 XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t len, XXH64_hash_t seed)
2839 {
2840 #if !defined(XXH_NO_STREAM) && XXH_SIZE_OPT >= 2
2841  /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
2842  XXH64_state_t state;
2843  XXH64_reset(&state, seed);
2844  XXH64_update(&state, (const xxh_u8*)input, len);
2845  return XXH64_digest(&state);
2846 #else
2847  if (XXH_FORCE_ALIGN_CHECK) {
2848  if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
2849  return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
2850  } }
2851 
2852  return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
2853 
2854 #endif
2855 }
2856 
2857 /******* Hash Streaming *******/
2858 #ifndef XXH_NO_STREAM
2859 /*! @ingroup XXH64_family*/
2861 {
2862  return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
2863 }
2864 /*! @ingroup XXH64_family */
2866 {
2867  XXH_free(statePtr);
2868  return XXH_OK;
2869 }
2870 
2871 /*! @ingroup XXH64_family */
2872 XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dstState, const XXH64_state_t* srcState)
2873 {
2874  XXH_memcpy(dstState, srcState, sizeof(*dstState));
2875 }
2876 
2877 /*! @ingroup XXH64_family */
2878 XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, XXH64_hash_t seed)
2879 {
2880  XXH_ASSERT(statePtr != NULL);
2881  memset(statePtr, 0, sizeof(*statePtr));
2882  statePtr->v[0] = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
2883  statePtr->v[1] = seed + XXH_PRIME64_2;
2884  statePtr->v[2] = seed + 0;
2885  statePtr->v[3] = seed - XXH_PRIME64_1;
2886  return XXH_OK;
2887 }
2888 
2889 /*! @ingroup XXH64_family */
2891 XXH64_update (XXH64_state_t* state, const void* input, size_t len)
2892 {
2893  if (input==NULL) {
2894  XXH_ASSERT(len == 0);
2895  return XXH_OK;
2896  }
2897 
2898  { const xxh_u8* p = (const xxh_u8*)input;
2899  const xxh_u8* const bEnd = p + len;
2900 
2901  state->total_len += len;
2902 
2903  if (state->memsize + len < 32) { /* fill in tmp buffer */
2904  XXH_memcpy(((xxh_u8*)state->mem64) + state->memsize, input, len);
2905  state->memsize += (xxh_u32)len;
2906  return XXH_OK;
2907  }
2908 
2909  if (state->memsize) { /* tmp buffer is full */
2910  XXH_memcpy(((xxh_u8*)state->mem64) + state->memsize, input, 32-state->memsize);
2911  state->v[0] = XXH64_round(state->v[0], XXH_readLE64(state->mem64+0));
2912  state->v[1] = XXH64_round(state->v[1], XXH_readLE64(state->mem64+1));
2913  state->v[2] = XXH64_round(state->v[2], XXH_readLE64(state->mem64+2));
2914  state->v[3] = XXH64_round(state->v[3], XXH_readLE64(state->mem64+3));
2915  p += 32 - state->memsize;
2916  state->memsize = 0;
2917  }
2918 
2919  if (p+32 <= bEnd) {
2920  const xxh_u8* const limit = bEnd - 32;
2921 
2922  do {
2923  state->v[0] = XXH64_round(state->v[0], XXH_readLE64(p)); p+=8;
2924  state->v[1] = XXH64_round(state->v[1], XXH_readLE64(p)); p+=8;
2925  state->v[2] = XXH64_round(state->v[2], XXH_readLE64(p)); p+=8;
2926  state->v[3] = XXH64_round(state->v[3], XXH_readLE64(p)); p+=8;
2927  } while (p<=limit);
2928 
2929  }
2930 
2931  if (p < bEnd) {
2932  XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
2933  state->memsize = (unsigned)(bEnd-p);
2934  }
2935  }
2936 
2937  return XXH_OK;
2938 }
2939 
2940 
2941 /*! @ingroup XXH64_family */
2942 XXH_PUBLIC_API XXH64_hash_t XXH64_digest(const XXH64_state_t* state)
2943 {
2944  xxh_u64 h64;
2945 
2946  if (state->total_len >= 32) {
2947  h64 = XXH_rotl64(state->v[0], 1) + XXH_rotl64(state->v[1], 7) + XXH_rotl64(state->v[2], 12) + XXH_rotl64(state->v[3], 18);
2948  h64 = XXH64_mergeRound(h64, state->v[0]);
2949  h64 = XXH64_mergeRound(h64, state->v[1]);
2950  h64 = XXH64_mergeRound(h64, state->v[2]);
2951  h64 = XXH64_mergeRound(h64, state->v[3]);
2952  } else {
2953  h64 = state->v[2] /*seed*/ + XXH_PRIME64_5;
2954  }
2955 
2956  h64 += (xxh_u64) state->total_len;
2957 
2958  return XXH64_finalize(h64, (const xxh_u8*)state->mem64, (size_t)state->total_len, XXH_aligned);
2959 }
2960 #endif /* !XXH_NO_STREAM */
2961 
2962 /******* Canonical representation *******/
2963 
2964 /*! @ingroup XXH64_family */
2965 XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
2966 {
2967  XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
2968  if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
2969  XXH_memcpy(dst, &hash, sizeof(*dst));
2970 }
2971 
2972 /*! @ingroup XXH64_family */
2974 {
2975  return XXH_readBE64(src);
2976 }
2977 
2978 #ifndef XXH_NO_XXH3
2979 
2980 /* *********************************************************************
2981 * XXH3
2982 * New generation hash designed for speed on small keys and vectorization
2983 ************************************************************************ */
2984 /*!
2985  * @}
2986  * @defgroup XXH3_impl XXH3 implementation
2987  * @ingroup impl
2988  * @{
2989  */
2990 
2991 /* === Compiler specifics === */
2992 
2993 #if ((defined(sun) || defined(__sun)) && __cplusplus) /* Solaris includes __STDC_VERSION__ with C++. Tested with GCC 5.5 */
2994 # define XXH_RESTRICT /* disable */
2995 #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* >= C99 */
2996 # define XXH_RESTRICT restrict
2997 #else
2998 /* Note: it might be useful to define __restrict or __restrict__ for some C++ compilers */
2999 # define XXH_RESTRICT /* disable */
3000 #endif
3001 
3002 #if (defined(__GNUC__) && (__GNUC__ >= 3)) \
3003  || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) \
3004  || defined(__clang__)
3005 # define XXH_likely(x) __builtin_expect(x, 1)
3006 # define XXH_unlikely(x) __builtin_expect(x, 0)
3007 #else
3008 # define XXH_likely(x) (x)
3009 # define XXH_unlikely(x) (x)
3010 #endif
3011 
3012 #if defined(__GNUC__) || defined(__clang__)
3013 # if defined(__ARM_FEATURE_SVE)
3014 # include <arm_sve.h>
3015 # elif defined(__ARM_NEON__) || defined(__ARM_NEON) \
3016  || defined(__aarch64__) || defined(_M_ARM) \
3017  || defined(_M_ARM64) || defined(_M_ARM64EC)
3018 # define inline __inline__ /* circumvent a clang bug */
3019 # include <arm_neon.h>
3020 # undef inline
3021 # elif defined(__AVX2__)
3022 # include <immintrin.h>
3023 # elif defined(__SSE2__)
3024 # include <emmintrin.h>
3025 # endif
3026 #endif
3027 
3028 #if defined(_MSC_VER)
3029 # include <intrin.h>
3030 #endif
3031 
3032 /*
3033  * One goal of XXH3 is to make it fast on both 32-bit and 64-bit, while
3034  * remaining a true 64-bit/128-bit hash function.
3035  *
3036  * This is done by prioritizing a subset of 64-bit operations that can be
3037  * emulated without too many steps on the average 32-bit machine.
3038  *
3039  * For example, these two lines seem similar, and run equally fast on 64-bit:
3040  *
3041  * xxh_u64 x;
3042  * x ^= (x >> 47); // good
3043  * x ^= (x >> 13); // bad
3044  *
3045  * However, to a 32-bit machine, there is a major difference.
3046  *
3047  * x ^= (x >> 47) looks like this:
3048  *
3049  * x.lo ^= (x.hi >> (47 - 32));
3050  *
3051  * while x ^= (x >> 13) looks like this:
3052  *
3053  * // note: funnel shifts are not usually cheap.
3054  * x.lo ^= (x.lo >> 13) | (x.hi << (32 - 13));
3055  * x.hi ^= (x.hi >> 13);
3056  *
3057  * The first one is significantly faster than the second, simply because the
3058  * shift is larger than 32. This means:
3059  * - All the bits we need are in the upper 32 bits, so we can ignore the lower
3060  * 32 bits in the shift.
3061  * - The shift result will always fit in the lower 32 bits, and therefore,
3062  * we can ignore the upper 32 bits in the xor.
3063  *
3064  * Thanks to this optimization, XXH3 only requires these features to be efficient:
3065  *
3066  * - Usable unaligned access
3067  * - A 32-bit or 64-bit ALU
3068  * - If 32-bit, a decent ADC instruction
3069  * - A 32 or 64-bit multiply with a 64-bit result
3070  * - For the 128-bit variant, a decent byteswap helps short inputs.
3071  *
3072  * The first two are already required by XXH32, and almost all 32-bit and 64-bit
3073  * platforms which can run XXH32 can run XXH3 efficiently.
3074  *
3075  * Thumb-1, the classic 16-bit only subset of ARM's instruction set, is one
3076  * notable exception.
3077  *
3078  * First of all, Thumb-1 lacks support for the UMULL instruction which
3079  * performs the important long multiply. This means numerous __aeabi_lmul
3080  * calls.
3081  *
3082  * Second of all, the 8 functional registers are just not enough.
3083  * Setup for __aeabi_lmul, byteshift loads, pointers, and all arithmetic need
3084  * Lo registers, and this shuffling results in thousands more MOVs than A32.
3085  *
3086  * A32 and T32 don't have this limitation. They can access all 14 registers,
3087  * do a 32->64 multiply with UMULL, and the flexible operand allowing free
3088  * shifts is helpful, too.
3089  *
3090  * Therefore, we do a quick sanity check.
3091  *
3092  * If compiling Thumb-1 for a target which supports ARM instructions, we will
3093  * emit a warning, as it is not a "sane" platform to compile for.
3094  *
3095  * Usually, if this happens, it is because of an accident and you probably need
3096  * to specify -march, as you likely meant to compile for a newer architecture.
3097  *
3098  * Credit: large sections of the vectorial and asm source code paths
3099  * have been contributed by @easyaspi314
3100  */
3101 #if defined(__thumb__) && !defined(__thumb2__) && defined(__ARM_ARCH_ISA_ARM)
3102 # warning "XXH3 is highly inefficient without ARM or Thumb-2."
3103 #endif
3104 
3105 /* ==========================================
3106  * Vectorization detection
3107  * ========================================== */
3108 
3109 #ifdef XXH_DOXYGEN
3110 /*!
3111  * @ingroup tuning
3112  * @brief Overrides the vectorization implementation chosen for XXH3.
3113  *
3114  * Can be defined to 0 to disable SIMD or any of the values mentioned in
3115  * @ref XXH_VECTOR_TYPE.
3116  *
3117  * If this is not defined, it uses predefined macros to determine the best
3118  * implementation.
3119  */
3120 # define XXH_VECTOR XXH_SCALAR
3121 /*!
3122  * @ingroup tuning
3123  * @brief Possible values for @ref XXH_VECTOR.
3124  *
3125  * Note that these are actually implemented as macros.
3126  *
3127  * If this is not defined, it is detected automatically.
3128  * @ref XXH_X86DISPATCH overrides this.
3129  */
3130 enum XXH_VECTOR_TYPE /* fake enum */ {
3131  XXH_SCALAR = 0, /*!< Portable scalar version */
3132  XXH_SSE2 = 1, /*!<
3133  * SSE2 for Pentium 4, Opteron, all x86_64.
3134  *
3135  * @note SSE2 is also guaranteed on Windows 10, macOS, and
3136  * Android x86.
3137  */
3138  XXH_AVX2 = 2, /*!< AVX2 for Haswell and Bulldozer */
3139  XXH_AVX512 = 3, /*!< AVX512 for Skylake and Icelake */
3140  XXH_NEON = 4, /*!< NEON for most ARMv7-A and all AArch64 */
3141  XXH_VSX = 5, /*!< VSX and ZVector for POWER8/z13 (64-bit) */
3142  XXH_SVE = 6, /*!< SVE for some ARMv8-A and ARMv9-A */
3143 };
3144 /*!
3145  * @ingroup tuning
3146  * @brief Selects the minimum alignment for XXH3's accumulators.
3147  *
3148  * When using SIMD, this should match the alignment required for said vector
3149  * type, so, for example, 32 for AVX2.
3150  *
3151  * Default: Auto detected.
3152  */
3153 # define XXH_ACC_ALIGN 8
3154 #endif
3155 
3156 /* Actual definition */
3157 #ifndef XXH_DOXYGEN
3158 # define XXH_SCALAR 0
3159 # define XXH_SSE2 1
3160 # define XXH_AVX2 2
3161 # define XXH_AVX512 3
3162 # define XXH_NEON 4
3163 # define XXH_VSX 5
3164 # define XXH_SVE 6
3165 #endif
3166 
3167 #ifndef XXH_VECTOR /* can be defined on command line */
3168 # if defined(__ARM_FEATURE_SVE)
3169 # define XXH_VECTOR XXH_SVE
3170 # elif ( \
3171  defined(__ARM_NEON__) || defined(__ARM_NEON) /* gcc */ \
3172  || defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC) /* msvc */ \
3173  ) && ( \
3174  defined(_WIN32) || defined(__LITTLE_ENDIAN__) /* little endian only */ \
3175  || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
3176  )
3177 # define XXH_VECTOR XXH_NEON
3178 # elif defined(__AVX512F__)
3179 # define XXH_VECTOR XXH_AVX512
3180 # elif defined(__AVX2__)
3181 # define XXH_VECTOR XXH_AVX2
3182 # elif defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP == 2))
3183 # define XXH_VECTOR XXH_SSE2
3184 # elif (defined(__PPC64__) && defined(__POWER8_VECTOR__)) \
3185  || (defined(__s390x__) && defined(__VEC__)) \
3186  && defined(__GNUC__) /* TODO: IBM XL */
3187 # define XXH_VECTOR XXH_VSX
3188 # else
3189 # define XXH_VECTOR XXH_SCALAR
3190 # endif
3191 #endif
3192 
3193 /* __ARM_FEATURE_SVE is only supported by GCC & Clang. */
3194 #if (XXH_VECTOR == XXH_SVE) && !defined(__ARM_FEATURE_SVE)
3195 # ifdef _MSC_VER
3196 # pragma warning(once : 4606)
3197 # else
3198 # warning "__ARM_FEATURE_SVE isn't supported. Use SCALAR instead."
3199 # endif
3200 # undef XXH_VECTOR
3201 # define XXH_VECTOR XXH_SCALAR
3202 #endif
3203 
3204 /*
3205  * Controls the alignment of the accumulator,
3206  * for compatibility with aligned vector loads, which are usually faster.
3207  */
3208 #ifndef XXH_ACC_ALIGN
3209 # if defined(XXH_X86DISPATCH)
3210 # define XXH_ACC_ALIGN 64 /* for compatibility with avx512 */
3211 # elif XXH_VECTOR == XXH_SCALAR /* scalar */
3212 # define XXH_ACC_ALIGN 8
3213 # elif XXH_VECTOR == XXH_SSE2 /* sse2 */
3214 # define XXH_ACC_ALIGN 16
3215 # elif XXH_VECTOR == XXH_AVX2 /* avx2 */
3216 # define XXH_ACC_ALIGN 32
3217 # elif XXH_VECTOR == XXH_NEON /* neon */
3218 # define XXH_ACC_ALIGN 16
3219 # elif XXH_VECTOR == XXH_VSX /* vsx */
3220 # define XXH_ACC_ALIGN 16
3221 # elif XXH_VECTOR == XXH_AVX512 /* avx512 */
3222 # define XXH_ACC_ALIGN 64
3223 # elif XXH_VECTOR == XXH_SVE /* sve */
3224 # define XXH_ACC_ALIGN 64
3225 # endif
3226 #endif
3227 
3228 #if defined(XXH_X86DISPATCH) || XXH_VECTOR == XXH_SSE2 \
3229  || XXH_VECTOR == XXH_AVX2 || XXH_VECTOR == XXH_AVX512
3230 # define XXH_SEC_ALIGN XXH_ACC_ALIGN
3231 #elif XXH_VECTOR == XXH_SVE
3232 # define XXH_SEC_ALIGN XXH_ACC_ALIGN
3233 #else
3234 # define XXH_SEC_ALIGN 8
3235 #endif
3236 
3237 /*
3238  * UGLY HACK:
3239  * GCC usually generates the best code with -O3 for xxHash.
3240  *
3241  * However, when targeting AVX2, it is overzealous in its unrolling resulting
3242  * in code roughly 3/4 the speed of Clang.
3243  *
3244  * There are other issues, such as GCC splitting _mm256_loadu_si256 into
3245  * _mm_loadu_si128 + _mm256_inserti128_si256. This is an optimization which
3246  * only applies to Sandy and Ivy Bridge... which don't even support AVX2.
3247  *
3248  * That is why when compiling the AVX2 version, it is recommended to use either
3249  * -O2 -mavx2 -march=haswell
3250  * or
3251  * -O2 -mavx2 -mno-avx256-split-unaligned-load
3252  * for decent performance, or to use Clang instead.
3253  *
3254  * Fortunately, we can control the first one with a pragma that forces GCC into
3255  * -O2, but the other one we can't control without "failed to inline always
3256  * inline function due to target mismatch" warnings.
3257  */
3258 #if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
3259  && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
3260  && defined(__OPTIMIZE__) && XXH_SIZE_OPT <= 0 /* respect -O0 and -Os */
3261 # pragma GCC push_options
3262 # pragma GCC optimize("-O2")
3263 #endif
3264 
3265 
3266 #if XXH_VECTOR == XXH_NEON
3267 /*
3268  * NEON's setup for vmlal_u32 is a little more complicated than it is on
3269  * SSE2, AVX2, and VSX.
3270  *
3271  * While PMULUDQ and VMULEUW both perform a mask, VMLAL.U32 performs an upcast.
3272  *
3273  * To do the same operation, the 128-bit 'Q' register needs to be split into
3274  * two 64-bit 'D' registers, performing this operation::
3275  *
3276  * [ a | b ]
3277  * | '---------. .--------' |
3278  * | x |
3279  * | .---------' '--------. |
3280  * [ a & 0xFFFFFFFF | b & 0xFFFFFFFF ],[ a >> 32 | b >> 32 ]
3281  *
3282  * Due to significant changes in aarch64, the fastest method for aarch64 is
3283  * completely different than the fastest method for ARMv7-A.
3284  *
3285  * ARMv7-A treats D registers as unions overlaying Q registers, so modifying
3286  * D11 will modify the high half of Q5. This is similar to how modifying AH
3287  * will only affect bits 8-15 of AX on x86.
3288  *
3289  * VZIP takes two registers, and puts even lanes in one register and odd lanes
3290  * in the other.
3291  *
3292  * On ARMv7-A, this strangely modifies both parameters in place instead of
3293  * taking the usual 3-operand form.
3294  *
3295  * Therefore, if we want to do this, we can simply use a D-form VZIP.32 on the
3296  * lower and upper halves of the Q register to end up with the high and low
3297  * halves where we want - all in one instruction.
3298  *
3299  * vzip.32 d10, d11 @ d10 = { d10[0], d11[0] }; d11 = { d10[1], d11[1] }
3300  *
3301  * Unfortunately we need inline assembly for this: Instructions modifying two
3302  * registers at once is not possible in GCC or Clang's IR, and they have to
3303  * create a copy.
3304  *
3305  * aarch64 requires a different approach.
3306  *
3307  * In order to make it easier to write a decent compiler for aarch64, many
3308  * quirks were removed, such as conditional execution.
3309  *
3310  * NEON was also affected by this.
3311  *
3312  * aarch64 cannot access the high bits of a Q-form register, and writes to a
3313  * D-form register zero the high bits, similar to how writes to W-form scalar
3314  * registers (or DWORD registers on x86_64) work.
3315  *
3316  * The formerly free vget_high intrinsics now require a vext (with a few
3317  * exceptions)
3318  *
3319  * Additionally, VZIP was replaced by ZIP1 and ZIP2, which are the equivalent
3320  * of PUNPCKL* and PUNPCKH* in SSE, respectively, in order to only modify one
3321  * operand.
3322  *
3323  * The equivalent of the VZIP.32 on the lower and upper halves would be this
3324  * mess:
3325  *
3326  * ext v2.4s, v0.4s, v0.4s, #2 // v2 = { v0[2], v0[3], v0[0], v0[1] }
3327  * zip1 v1.2s, v0.2s, v2.2s // v1 = { v0[0], v2[0] }
3328  * zip2 v0.2s, v0.2s, v1.2s // v0 = { v0[1], v2[1] }
3329  *
3330  * Instead, we use a literal downcast, vmovn_u64 (XTN), and vshrn_n_u64 (SHRN):
3331  *
3332  * shrn v1.2s, v0.2d, #32 // v1 = (uint32x2_t)(v0 >> 32);
3333  * xtn v0.2s, v0.2d // v0 = (uint32x2_t)(v0 & 0xFFFFFFFF);
3334  *
3335  * This is available on ARMv7-A, but is less efficient than a single VZIP.32.
3336  */
3337 
3338 /*!
3339  * Function-like macro:
3340  * void XXH_SPLIT_IN_PLACE(uint64x2_t &in, uint32x2_t &outLo, uint32x2_t &outHi)
3341  * {
3342  * outLo = (uint32x2_t)(in & 0xFFFFFFFF);
3343  * outHi = (uint32x2_t)(in >> 32);
3344  * in = UNDEFINED;
3345  * }
3346  */
3347 # if !defined(XXH_NO_VZIP_HACK) /* define to disable */ \
3348  && (defined(__GNUC__) || defined(__clang__)) \
3349  && (defined(__arm__) || defined(__thumb__) || defined(_M_ARM))
3350 # define XXH_SPLIT_IN_PLACE(in, outLo, outHi) \
3351  do { \
3352  /* Undocumented GCC/Clang operand modifier: %e0 = lower D half, %f0 = upper D half */ \
3353  /* https://github.com/gcc-mirror/gcc/blob/38cf91e5/gcc/config/arm/arm.c#L22486 */ \
3354  /* https://github.com/llvm-mirror/llvm/blob/2c4ca683/lib/Target/ARM/ARMAsmPrinter.cpp#L399 */ \
3355  __asm__("vzip.32 %e0, %f0" : "+w" (in)); \
3356  (outLo) = vget_low_u32 (vreinterpretq_u32_u64(in)); \
3357  (outHi) = vget_high_u32(vreinterpretq_u32_u64(in)); \
3358  } while (0)
3359 # else
3360 # define XXH_SPLIT_IN_PLACE(in, outLo, outHi) \
3361  do { \
3362  (outLo) = vmovn_u64 (in); \
3363  (outHi) = vshrn_n_u64 ((in), 32); \
3364  } while (0)
3365 # endif
3366 
3367 /*!
3368  * @internal
3369  * @brief `vld1q_u64` but faster and alignment-safe.
3370  *
3371  * On AArch64, unaligned access is always safe, but on ARMv7-a, it is only
3372  * *conditionally* safe (`vld1` has an alignment bit like `movdq[ua]` in x86).
3373  *
3374  * GCC for AArch64 sees `vld1q_u8` as an intrinsic instead of a load, so it
3375  * prohibits load-store optimizations. Therefore, a direct dereference is used.
3376  *
3377  * Otherwise, `vld1q_u8` is used with `vreinterpretq_u8_u64` to do a safe
3378  * unaligned load.
3379  */
3380 #if defined(__aarch64__) && defined(__GNUC__) && !defined(__clang__)
3381 XXH_FORCE_INLINE uint64x2_t XXH_vld1q_u64(void const* ptr) /* silence -Wcast-align */
3382 {
3383  return *(uint64x2_t const*)ptr;
3384 }
3385 #else
3386 XXH_FORCE_INLINE uint64x2_t XXH_vld1q_u64(void const* ptr)
3387 {
3388  return vreinterpretq_u64_u8(vld1q_u8((uint8_t const*)ptr));
3389 }
3390 #endif
3391 /*!
3392  * @ingroup tuning
3393  * @brief Controls the NEON to scalar ratio for XXH3
3394  *
3395  * On AArch64 when not optimizing for size, XXH3 will run 6 lanes using NEON and
3396  * 2 lanes on scalar by default (except on Apple platforms, as Apple CPUs benefit
3397  * from only using NEON).
3398  *
3399  * This can be set to 2, 4, 6, or 8. ARMv7 will default to all 8 NEON lanes, as the
3400  * emulated 64-bit arithmetic is too slow.
3401  *
3402  * Modern ARM CPUs are _very_ sensitive to how their pipelines are used.
3403  *
3404  * For example, the Cortex-A73 can dispatch 3 micro-ops per cycle, but it can't
3405  * have more than 2 NEON (F0/F1) micro-ops. If you are only using NEON instructions,
3406  * you are only using 2/3 of the CPU bandwidth.
3407  *
3408  * This is even more noticeable on the more advanced cores like the A76 which
3409  * can dispatch 8 micro-ops per cycle, but still only 2 NEON micro-ops at once.
3410  *
3411  * Therefore, @ref XXH3_NEON_LANES lanes will be processed using NEON, and the
3412  * remaining lanes will use scalar instructions. This improves the bandwidth
3413  * and also gives the integer pipelines something to do besides twiddling loop
3414  * counters and pointers.
3415  *
3416  * This change benefits CPUs with large micro-op buffers without negatively affecting
3417  * most other CPUs:
3418  *
3419  * | Chipset | Dispatch type | NEON only | 6:2 hybrid | Diff. |
3420  * |:----------------------|:--------------------|----------:|-----------:|------:|
3421  * | Snapdragon 730 (A76) | 2 NEON/8 micro-ops | 8.8 GB/s | 10.1 GB/s | ~16% |
3422  * | Snapdragon 835 (A73) | 2 NEON/3 micro-ops | 5.1 GB/s | 5.3 GB/s | ~5% |
3423  * | Marvell PXA1928 (A53) | In-order dual-issue | 1.9 GB/s | 1.9 GB/s | 0% |
3424  * | Apple M1 | 4 NEON/8 micro-ops | 37.3 GB/s | 36.1 GB/s | ~-3% |
3425  *
3426  * It also seems to fix some bad codegen on GCC, making it almost as fast as clang.
3427  *
3428  * @see XXH3_accumulate_512_neon()
3429  */
3430 # ifndef XXH3_NEON_LANES
3431 # if (defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) \
3432  && !defined(__APPLE__) && XXH_SIZE_OPT <= 0
3433 # define XXH3_NEON_LANES 6
3434 # else
3435 # define XXH3_NEON_LANES XXH_ACC_NB
3436 # endif
3437 # endif
3438 #endif /* XXH_VECTOR == XXH_NEON */
3439 
3440 /*
3441  * VSX and Z Vector helpers.
3442  *
3443  * This is very messy, and any pull requests to clean this up are welcome.
3444  *
3445  * There are a lot of problems with supporting VSX and s390x, due to
3446  * inconsistent intrinsics, spotty coverage, and multiple endiannesses.
3447  */
3448 #if XXH_VECTOR == XXH_VSX
3449 /* Annoyingly, these headers _may_ define three macros: `bool`, `vector`,
3450  * and `pixel`. This is a problem for obvious reasons.
3451  *
3452  * These keywords are unnecessary; the spec literally says they are
3453  * equivalent to `__bool`, `__vector`, and `__pixel` and may be undef'd
3454  * after including the header.
3455  *
3456  * We use pragma push_macro/pop_macro to keep the namespace clean. */
3457 # pragma push_macro("bool")
3458 # pragma push_macro("vector")
3459 # pragma push_macro("pixel")
3460 /* silence potential macro redefined warnings */
3461 # undef bool
3462 # undef vector
3463 # undef pixel
3464 
3465 # if defined(__s390x__)
3466 # include <s390intrin.h>
3467 # else
3468 # include <altivec.h>
3469 # endif
3470 
3471 /* Restore the original macro values, if applicable. */
3472 # pragma pop_macro("pixel")
3473 # pragma pop_macro("vector")
3474 # pragma pop_macro("bool")
3475 
3476 typedef __vector unsigned long long xxh_u64x2;
3477 typedef __vector unsigned char xxh_u8x16;
3478 typedef __vector unsigned xxh_u32x4;
3479 
3480 # ifndef XXH_VSX_BE
3481 # if defined(__BIG_ENDIAN__) \
3482  || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
3483 # define XXH_VSX_BE 1
3484 # elif defined(__VEC_ELEMENT_REG_ORDER__) && __VEC_ELEMENT_REG_ORDER__ == __ORDER_BIG_ENDIAN__
3485 # warning "-maltivec=be is not recommended. Please use native endianness."
3486 # define XXH_VSX_BE 1
3487 # else
3488 # define XXH_VSX_BE 0
3489 # endif
3490 # endif /* !defined(XXH_VSX_BE) */
3491 
3492 # if XXH_VSX_BE
3493 # if defined(__POWER9_VECTOR__) || (defined(__clang__) && defined(__s390x__))
3494 # define XXH_vec_revb vec_revb
3495 # else
3496 /*!
3497  * A polyfill for POWER9's vec_revb().
3498  */
3499 XXH_FORCE_INLINE xxh_u64x2 XXH_vec_revb(xxh_u64x2 val)
3500 {
3501  xxh_u8x16 const vByteSwap = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
3502  0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 };
3503  return vec_perm(val, val, vByteSwap);
3504 }
3505 # endif
3506 # endif /* XXH_VSX_BE */
3507 
3508 /*!
3509  * Performs an unaligned vector load and byte swaps it on big endian.
3510  */
3511 XXH_FORCE_INLINE xxh_u64x2 XXH_vec_loadu(const void *ptr)
3512 {
3513  xxh_u64x2 ret;
3514  XXH_memcpy(&ret, ptr, sizeof(xxh_u64x2));
3515 # if XXH_VSX_BE
3516  ret = XXH_vec_revb(ret);
3517 # endif
3518  return ret;
3519 }
3520 
3521 /*
3522  * vec_mulo and vec_mule are very problematic intrinsics on PowerPC
3523  *
3524  * These intrinsics weren't added until GCC 8, despite existing for a while,
3525  * and they are endian dependent. Also, their meaning swap depending on version.
3526  * */
3527 # if defined(__s390x__)
3528  /* s390x is always big endian, no issue on this platform */
3529 # define XXH_vec_mulo vec_mulo
3530 # define XXH_vec_mule vec_mule
3531 # elif defined(__clang__) && XXH_HAS_BUILTIN(__builtin_altivec_vmuleuw) && !defined(__ibmxl__)
3532 /* Clang has a better way to control this, we can just use the builtin which doesn't swap. */
3533  /* The IBM XL Compiler (which defined __clang__) only implements the vec_* operations */
3534 # define XXH_vec_mulo __builtin_altivec_vmulouw
3535 # define XXH_vec_mule __builtin_altivec_vmuleuw
3536 # else
3537 /* gcc needs inline assembly */
3538 /* Adapted from https://github.com/google/highwayhash/blob/master/highwayhash/hh_vsx.h. */
3539 XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mulo(xxh_u32x4 a, xxh_u32x4 b)
3540 {
3541  xxh_u64x2 result;
3542  __asm__("vmulouw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
3543  return result;
3544 }
3545 XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mule(xxh_u32x4 a, xxh_u32x4 b)
3546 {
3547  xxh_u64x2 result;
3548  __asm__("vmuleuw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
3549  return result;
3550 }
3551 # endif /* XXH_vec_mulo, XXH_vec_mule */
3552 #endif /* XXH_VECTOR == XXH_VSX */
3553 
3554 #if XXH_VECTOR == XXH_SVE
3555 #define ACCRND(acc, offset) \
3556 do { \
3557  svuint64_t input_vec = svld1_u64(mask, xinput + offset); \
3558  svuint64_t secret_vec = svld1_u64(mask, xsecret + offset); \
3559  svuint64_t mixed = sveor_u64_x(mask, secret_vec, input_vec); \
3560  svuint64_t swapped = svtbl_u64(input_vec, kSwap); \
3561  svuint64_t mixed_lo = svextw_u64_x(mask, mixed); \
3562  svuint64_t mixed_hi = svlsr_n_u64_x(mask, mixed, 32); \
3563  svuint64_t mul = svmad_u64_x(mask, mixed_lo, mixed_hi, swapped); \
3564  acc = svadd_u64_x(mask, acc, mul); \
3565 } while (0)
3566 #endif /* XXH_VECTOR == XXH_SVE */
3567 
3568 
3569 /* prefetch
3570  * can be disabled, by declaring XXH_NO_PREFETCH build macro */
3571 #if defined(XXH_NO_PREFETCH)
3572 # define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
3573 #else
3574 # if XXH_SIZE_OPT >= 1
3575 # define XXH_PREFETCH(ptr) (void)(ptr)
3576 # elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) /* _mm_prefetch() not defined outside of x86/x64 */
3577 # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
3578 # define XXH_PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
3579 # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
3580 # define XXH_PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
3581 # else
3582 # define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
3583 # endif
3584 #endif /* XXH_NO_PREFETCH */
3585 
3586 
3587 /* ==========================================
3588  * XXH3 default settings
3589  * ========================================== */
3590 
3591 #define XXH_SECRET_DEFAULT_SIZE 192 /* minimum XXH3_SECRET_SIZE_MIN */
3592 
3593 #if (XXH_SECRET_DEFAULT_SIZE < XXH3_SECRET_SIZE_MIN)
3594 # error "default keyset is not large enough"
3595 #endif
3596 
3597 /*! Pseudorandom secret taken directly from FARSH. */
3598 XXH_ALIGN(64) static const xxh_u8 XXH3_kSecret[XXH_SECRET_DEFAULT_SIZE] = {
3599  0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
3600  0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
3601  0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
3602  0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
3603  0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
3604  0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
3605  0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
3606  0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
3607  0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
3608  0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
3609  0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
3610  0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,
3611 };
3612 
3613 
3614 #ifdef XXH_OLD_NAMES
3615 # define kSecret XXH3_kSecret
3616 #endif
3617 
3618 #ifdef XXH_DOXYGEN
3619 /*!
3620  * @brief Calculates a 32-bit to 64-bit long multiply.
3621  *
3622  * Implemented as a macro.
3623  *
3624  * Wraps `__emulu` on MSVC x86 because it tends to call `__allmul` when it doesn't
3625  * need to (but it shouldn't need to anyways, it is about 7 instructions to do
3626  * a 64x64 multiply...). Since we know that this will _always_ emit `MULL`, we
3627  * use that instead of the normal method.
3628  *
3629  * If you are compiling for platforms like Thumb-1 and don't have a better option,
3630  * you may also want to write your own long multiply routine here.
3631  *
3632  * @param x, y Numbers to be multiplied
3633  * @return 64-bit product of the low 32 bits of @p x and @p y.
3634  */
3635 XXH_FORCE_INLINE xxh_u64
3636 XXH_mult32to64(xxh_u64 x, xxh_u64 y)
3637 {
3638  return (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF);
3639 }
3640 #elif defined(_MSC_VER) && defined(_M_IX86)
3641 # define XXH_mult32to64(x, y) __emulu((unsigned)(x), (unsigned)(y))
3642 #else
3643 /*
3644  * Downcast + upcast is usually better than masking on older compilers like
3645  * GCC 4.2 (especially 32-bit ones), all without affecting newer compilers.
3646  *
3647  * The other method, (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF), will AND both operands
3648  * and perform a full 64x64 multiply -- entirely redundant on 32-bit.
3649  */
3650 # define XXH_mult32to64(x, y) ((xxh_u64)(xxh_u32)(x) * (xxh_u64)(xxh_u32)(y))
3651 #endif
3652 
3653 /*!
3654  * @brief Calculates a 64->128-bit long multiply.
3655  *
3656  * Uses `__uint128_t` and `_umul128` if available, otherwise uses a scalar
3657  * version.
3658  *
3659  * @param lhs , rhs The 64-bit integers to be multiplied
3660  * @return The 128-bit result represented in an @ref XXH128_hash_t.
3661  */
3662 static XXH128_hash_t
3663 XXH_mult64to128(xxh_u64 lhs, xxh_u64 rhs)
3664 {
3665  /*
3666  * GCC/Clang __uint128_t method.
3667  *
3668  * On most 64-bit targets, GCC and Clang define a __uint128_t type.
3669  * This is usually the best way as it usually uses a native long 64-bit
3670  * multiply, such as MULQ on x86_64 or MUL + UMULH on aarch64.
3671  *
3672  * Usually.
3673  *
3674  * Despite being a 32-bit platform, Clang (and emscripten) define this type
3675  * despite not having the arithmetic for it. This results in a laggy
3676  * compiler builtin call which calculates a full 128-bit multiply.
3677  * In that case it is best to use the portable one.
3678  * https://github.com/Cyan4973/xxHash/issues/211#issuecomment-515575677
3679  */
3680 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__wasm__) \
3681  && defined(__SIZEOF_INT128__) \
3682  || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
3683 
3684  __uint128_t const product = (__uint128_t)lhs * (__uint128_t)rhs;
3685  XXH128_hash_t r128;
3686  r128.low64 = (xxh_u64)(product);
3687  r128.high64 = (xxh_u64)(product >> 64);
3688  return r128;
3689 
3690  /*
3691  * MSVC for x64's _umul128 method.
3692  *
3693  * xxh_u64 _umul128(xxh_u64 Multiplier, xxh_u64 Multiplicand, xxh_u64 *HighProduct);
3694  *
3695  * This compiles to single operand MUL on x64.
3696  */
3697 #elif (defined(_M_X64) || defined(_M_IA64)) && !defined(_M_ARM64EC)
3698 
3699 #ifndef _MSC_VER
3700 # pragma intrinsic(_umul128)
3701 #endif
3702  xxh_u64 product_high;
3703  xxh_u64 const product_low = _umul128(lhs, rhs, &product_high);
3704  XXH128_hash_t r128;
3705  r128.low64 = product_low;
3706  r128.high64 = product_high;
3707  return r128;
3708 
3709  /*
3710  * MSVC for ARM64's __umulh method.
3711  *
3712  * This compiles to the same MUL + UMULH as GCC/Clang's __uint128_t method.
3713  */
3714 #elif defined(_M_ARM64) || defined(_M_ARM64EC)
3715 
3716 #ifndef _MSC_VER
3717 # pragma intrinsic(__umulh)
3718 #endif
3719  XXH128_hash_t r128;
3720  r128.low64 = lhs * rhs;
3721  r128.high64 = __umulh(lhs, rhs);
3722  return r128;
3723 
3724 #else
3725  /*
3726  * Portable scalar method. Optimized for 32-bit and 64-bit ALUs.
3727  *
3728  * This is a fast and simple grade school multiply, which is shown below
3729  * with base 10 arithmetic instead of base 0x100000000.
3730  *
3731  * 9 3 // D2 lhs = 93
3732  * x 7 5 // D2 rhs = 75
3733  * ----------
3734  * 1 5 // D2 lo_lo = (93 % 10) * (75 % 10) = 15
3735  * 4 5 | // D2 hi_lo = (93 / 10) * (75 % 10) = 45
3736  * 2 1 | // D2 lo_hi = (93 % 10) * (75 / 10) = 21
3737  * + 6 3 | | // D2 hi_hi = (93 / 10) * (75 / 10) = 63
3738  * ---------
3739  * 2 7 | // D2 cross = (15 / 10) + (45 % 10) + 21 = 27
3740  * + 6 7 | | // D2 upper = (27 / 10) + (45 / 10) + 63 = 67
3741  * ---------
3742  * 6 9 7 5 // D4 res = (27 * 10) + (15 % 10) + (67 * 100) = 6975
3743  *
3744  * The reasons for adding the products like this are:
3745  * 1. It avoids manual carry tracking. Just like how
3746  * (9 * 9) + 9 + 9 = 99, the same applies with this for UINT64_MAX.
3747  * This avoids a lot of complexity.
3748  *
3749  * 2. It hints for, and on Clang, compiles to, the powerful UMAAL
3750  * instruction available in ARM's Digital Signal Processing extension
3751  * in 32-bit ARMv6 and later, which is shown below:
3752  *
3753  * void UMAAL(xxh_u32 *RdLo, xxh_u32 *RdHi, xxh_u32 Rn, xxh_u32 Rm)
3754  * {
3755  * xxh_u64 product = (xxh_u64)*RdLo * (xxh_u64)*RdHi + Rn + Rm;
3756  * *RdLo = (xxh_u32)(product & 0xFFFFFFFF);
3757  * *RdHi = (xxh_u32)(product >> 32);
3758  * }
3759  *
3760  * This instruction was designed for efficient long multiplication, and
3761  * allows this to be calculated in only 4 instructions at speeds
3762  * comparable to some 64-bit ALUs.
3763  *
3764  * 3. It isn't terrible on other platforms. Usually this will be a couple
3765  * of 32-bit ADD/ADCs.
3766  */
3767 
3768  /* First calculate all of the cross products. */
3769  xxh_u64 const lo_lo = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs & 0xFFFFFFFF);
3770  xxh_u64 const hi_lo = XXH_mult32to64(lhs >> 32, rhs & 0xFFFFFFFF);
3771  xxh_u64 const lo_hi = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs >> 32);
3772  xxh_u64 const hi_hi = XXH_mult32to64(lhs >> 32, rhs >> 32);
3773 
3774  /* Now add the products together. These will never overflow. */
3775  xxh_u64 const cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi;
3776  xxh_u64 const upper = (hi_lo >> 32) + (cross >> 32) + hi_hi;
3777  xxh_u64 const lower = (cross << 32) | (lo_lo & 0xFFFFFFFF);
3778 
3779  XXH128_hash_t r128;
3780  r128.low64 = lower;
3781  r128.high64 = upper;
3782  return r128;
3783 #endif
3784 }
3785 
3786 /*!
3787  * @brief Calculates a 64-bit to 128-bit multiply, then XOR folds it.
3788  *
3789  * The reason for the separate function is to prevent passing too many structs
3790  * around by value. This will hopefully inline the multiply, but we don't force it.
3791  *
3792  * @param lhs , rhs The 64-bit integers to multiply
3793  * @return The low 64 bits of the product XOR'd by the high 64 bits.
3794  * @see XXH_mult64to128()
3795  */
3796 static xxh_u64
3797 XXH3_mul128_fold64(xxh_u64 lhs, xxh_u64 rhs)
3798 {
3799  XXH128_hash_t product = XXH_mult64to128(lhs, rhs);
3800  return product.low64 ^ product.high64;
3801 }
3802 
3803 /*! Seems to produce slightly better code on GCC for some reason. */
3804 XXH_FORCE_INLINE XXH_CONSTF xxh_u64 XXH_xorshift64(xxh_u64 v64, int shift)
3805 {
3806  XXH_ASSERT(0 <= shift && shift < 64);
3807  return v64 ^ (v64 >> shift);
3808 }
3809 
3810 /*
3811  * This is a fast avalanche stage,
3812  * suitable when input bits are already partially mixed
3813  */
3814 static XXH64_hash_t XXH3_avalanche(xxh_u64 h64)
3815 {
3816  h64 = XXH_xorshift64(h64, 37);
3817  h64 *= 0x165667919E3779F9ULL;
3818  h64 = XXH_xorshift64(h64, 32);
3819  return h64;
3820 }
3821 
3822 /*
3823  * This is a stronger avalanche,
3824  * inspired by Pelle Evensen's rrmxmx
3825  * preferable when input has not been previously mixed
3826  */
3827 static XXH64_hash_t XXH3_rrmxmx(xxh_u64 h64, xxh_u64 len)
3828 {
3829  /* this mix is inspired by Pelle Evensen's rrmxmx */
3830  h64 ^= XXH_rotl64(h64, 49) ^ XXH_rotl64(h64, 24);
3831  h64 *= 0x9FB21C651E98DF25ULL;
3832  h64 ^= (h64 >> 35) + len ;
3833  h64 *= 0x9FB21C651E98DF25ULL;
3834  return XXH_xorshift64(h64, 28);
3835 }
3836 
3837 
3838 /* ==========================================
3839  * Short keys
3840  * ==========================================
3841  * One of the shortcomings of XXH32 and XXH64 was that their performance was
3842  * sub-optimal on short lengths. It used an iterative algorithm which strongly
3843  * favored lengths that were a multiple of 4 or 8.
3844  *
3845  * Instead of iterating over individual inputs, we use a set of single shot
3846  * functions which piece together a range of lengths and operate in constant time.
3847  *
3848  * Additionally, the number of multiplies has been significantly reduced. This
3849  * reduces latency, especially when emulating 64-bit multiplies on 32-bit.
3850  *
3851  * Depending on the platform, this may or may not be faster than XXH32, but it
3852  * is almost guaranteed to be faster than XXH64.
3853  */
3854 
3855 /*
3856  * At very short lengths, there isn't enough input to fully hide secrets, or use
3857  * the entire secret.
3858  *
3859  * There is also only a limited amount of mixing we can do before significantly
3860  * impacting performance.
3861  *
3862  * Therefore, we use different sections of the secret and always mix two secret
3863  * samples with an XOR. This should have no effect on performance on the
3864  * seedless or withSeed variants because everything _should_ be constant folded
3865  * by modern compilers.
3866  *
3867  * The XOR mixing hides individual parts of the secret and increases entropy.
3868  *
3869  * This adds an extra layer of strength for custom secrets.
3870  */
3871 XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
3872 XXH3_len_1to3_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
3873 {
3874  XXH_ASSERT(input != NULL);
3875  XXH_ASSERT(1 <= len && len <= 3);
3876  XXH_ASSERT(secret != NULL);
3877  /*
3878  * len = 1: combined = { input[0], 0x01, input[0], input[0] }
3879  * len = 2: combined = { input[1], 0x02, input[0], input[1] }
3880  * len = 3: combined = { input[2], 0x03, input[0], input[1] }
3881  */
3882  { xxh_u8 const c1 = input[0];
3883  xxh_u8 const c2 = input[len >> 1];
3884  xxh_u8 const c3 = input[len - 1];
3885  xxh_u32 const combined = ((xxh_u32)c1 << 16) | ((xxh_u32)c2 << 24)
3886  | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
3887  xxh_u64 const bitflip = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
3888  xxh_u64 const keyed = (xxh_u64)combined ^ bitflip;
3889  return XXH64_avalanche(keyed);
3890  }
3891 }
3892 
3893 XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
3894 XXH3_len_4to8_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
3895 {
3896  XXH_ASSERT(input != NULL);
3897  XXH_ASSERT(secret != NULL);
3898  XXH_ASSERT(4 <= len && len <= 8);
3899  seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
3900  { xxh_u32 const input1 = XXH_readLE32(input);
3901  xxh_u32 const input2 = XXH_readLE32(input + len - 4);
3902  xxh_u64 const bitflip = (XXH_readLE64(secret+8) ^ XXH_readLE64(secret+16)) - seed;
3903  xxh_u64 const input64 = input2 + (((xxh_u64)input1) << 32);
3904  xxh_u64 const keyed = input64 ^ bitflip;
3905  return XXH3_rrmxmx(keyed, len);
3906  }
3907 }
3908 
3909 XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
3910 XXH3_len_9to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
3911 {
3912  XXH_ASSERT(input != NULL);
3913  XXH_ASSERT(secret != NULL);
3914  XXH_ASSERT(9 <= len && len <= 16);
3915  { xxh_u64 const bitflip1 = (XXH_readLE64(secret+24) ^ XXH_readLE64(secret+32)) + seed;
3916  xxh_u64 const bitflip2 = (XXH_readLE64(secret+40) ^ XXH_readLE64(secret+48)) - seed;
3917  xxh_u64 const input_lo = XXH_readLE64(input) ^ bitflip1;
3918  xxh_u64 const input_hi = XXH_readLE64(input + len - 8) ^ bitflip2;
3919  xxh_u64 const acc = len
3920  + XXH_swap64(input_lo) + input_hi
3921  + XXH3_mul128_fold64(input_lo, input_hi);
3922  return XXH3_avalanche(acc);
3923  }
3924 }
3925 
3926 XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
3927 XXH3_len_0to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
3928 {
3929  XXH_ASSERT(len <= 16);
3930  { if (XXH_likely(len > 8)) return XXH3_len_9to16_64b(input, len, secret, seed);
3931  if (XXH_likely(len >= 4)) return XXH3_len_4to8_64b(input, len, secret, seed);
3932  if (len) return XXH3_len_1to3_64b(input, len, secret, seed);
3933  return XXH64_avalanche(seed ^ (XXH_readLE64(secret+56) ^ XXH_readLE64(secret+64)));
3934  }
3935 }
3936 
3937 /*
3938  * DISCLAIMER: There are known *seed-dependent* multicollisions here due to
3939  * multiplication by zero, affecting hashes of lengths 17 to 240.
3940  *
3941  * However, they are very unlikely.
3942  *
3943  * Keep this in mind when using the unseeded XXH3_64bits() variant: As with all
3944  * unseeded non-cryptographic hashes, it does not attempt to defend itself
3945  * against specially crafted inputs, only random inputs.
3946  *
3947  * Compared to classic UMAC where a 1 in 2^31 chance of 4 consecutive bytes
3948  * cancelling out the secret is taken an arbitrary number of times (addressed
3949  * in XXH3_accumulate_512), this collision is very unlikely with random inputs
3950  * and/or proper seeding:
3951  *
3952  * This only has a 1 in 2^63 chance of 8 consecutive bytes cancelling out, in a
3953  * function that is only called up to 16 times per hash with up to 240 bytes of
3954  * input.
3955  *
3956  * This is not too bad for a non-cryptographic hash function, especially with
3957  * only 64 bit outputs.
3958  *
3959  * The 128-bit variant (which trades some speed for strength) is NOT affected
3960  * by this, although it is always a good idea to use a proper seed if you care
3961  * about strength.
3962  */
3963 XXH_FORCE_INLINE xxh_u64 XXH3_mix16B(const xxh_u8* XXH_RESTRICT input,
3964  const xxh_u8* XXH_RESTRICT secret, xxh_u64 seed64)
3965 {
3966 #if defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
3967  && defined(__i386__) && defined(__SSE2__) /* x86 + SSE2 */ \
3968  && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable like XXH32 hack */
3969  /*
3970  * UGLY HACK:
3971  * GCC for x86 tends to autovectorize the 128-bit multiply, resulting in
3972  * slower code.
3973  *
3974  * By forcing seed64 into a register, we disrupt the cost model and
3975  * cause it to scalarize. See `XXH32_round()`
3976  *
3977  * FIXME: Clang's output is still _much_ faster -- On an AMD Ryzen 3600,
3978  * XXH3_64bits @ len=240 runs at 4.6 GB/s with Clang 9, but 3.3 GB/s on
3979  * GCC 9.2, despite both emitting scalar code.
3980  *
3981  * GCC generates much better scalar code than Clang for the rest of XXH3,
3982  * which is why finding a more optimal codepath is an interest.
3983  */
3984  XXH_COMPILER_GUARD(seed64);
3985 #endif
3986  { xxh_u64 const input_lo = XXH_readLE64(input);
3987  xxh_u64 const input_hi = XXH_readLE64(input+8);
3988  return XXH3_mul128_fold64(
3989  input_lo ^ (XXH_readLE64(secret) + seed64),
3990  input_hi ^ (XXH_readLE64(secret+8) - seed64)
3991  );
3992  }
3993 }
3994 
3995 /* For mid range keys, XXH3 uses a Mum-hash variant. */
3996 XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
3997 XXH3_len_17to128_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
3998  const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
3999  XXH64_hash_t seed)
4000 {
4001  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
4002  XXH_ASSERT(16 < len && len <= 128);
4003 
4004  { xxh_u64 acc = len * XXH_PRIME64_1;
4005 #if XXH_SIZE_OPT >= 1
4006  /* Smaller and cleaner, but slightly slower. */
4007  size_t i = (len - 1) / 32;
4008  do {
4009  acc += XXH3_mix16B(input+16 * i, secret+32*i, seed);
4010  acc += XXH3_mix16B(input+len-16*(i+1), secret+32*i+16, seed);
4011  } while (i-- != 0);
4012 #else
4013  if (len > 32) {
4014  if (len > 64) {
4015  if (len > 96) {
4016  acc += XXH3_mix16B(input+48, secret+96, seed);
4017  acc += XXH3_mix16B(input+len-64, secret+112, seed);
4018  }
4019  acc += XXH3_mix16B(input+32, secret+64, seed);
4020  acc += XXH3_mix16B(input+len-48, secret+80, seed);
4021  }
4022  acc += XXH3_mix16B(input+16, secret+32, seed);
4023  acc += XXH3_mix16B(input+len-32, secret+48, seed);
4024  }
4025  acc += XXH3_mix16B(input+0, secret+0, seed);
4026  acc += XXH3_mix16B(input+len-16, secret+16, seed);
4027 #endif
4028  return XXH3_avalanche(acc);
4029  }
4030 }
4031 
4032 #define XXH3_MIDSIZE_MAX 240
4033 
4034 XXH_NO_INLINE XXH_PUREF XXH64_hash_t
4035 XXH3_len_129to240_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
4036  const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
4037  XXH64_hash_t seed)
4038 {
4039  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
4040  XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
4041 
4042  #define XXH3_MIDSIZE_STARTOFFSET 3
4043  #define XXH3_MIDSIZE_LASTOFFSET 17
4044 
4045  { xxh_u64 acc = len * XXH_PRIME64_1;
4046  int const nbRounds = (int)len / 16;
4047  int i;
4048  for (i=0; i<8; i++) {
4049  acc += XXH3_mix16B(input+(16*i), secret+(16*i), seed);
4050  }
4051  acc = XXH3_avalanche(acc);
4052  XXH_ASSERT(nbRounds >= 8);
4053 #if defined(__clang__) /* Clang */ \
4054  && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
4055  && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
4056  /*
4057  * UGLY HACK:
4058  * Clang for ARMv7-A tries to vectorize this loop, similar to GCC x86.
4059  * In everywhere else, it uses scalar code.
4060  *
4061  * For 64->128-bit multiplies, even if the NEON was 100% optimal, it
4062  * would still be slower than UMAAL (see XXH_mult64to128).
4063  *
4064  * Unfortunately, Clang doesn't handle the long multiplies properly and
4065  * converts them to the nonexistent "vmulq_u64" intrinsic, which is then
4066  * scalarized into an ugly mess of VMOV.32 instructions.
4067  *
4068  * This mess is difficult to avoid without turning autovectorization
4069  * off completely, but they are usually relatively minor and/or not
4070  * worth it to fix.
4071  *
4072  * This loop is the easiest to fix, as unlike XXH32, this pragma
4073  * _actually works_ because it is a loop vectorization instead of an
4074  * SLP vectorization.
4075  */
4076  #pragma clang loop vectorize(disable)
4077 #endif
4078  for (i=8 ; i < nbRounds; i++) {
4079  acc += XXH3_mix16B(input+(16*i), secret+(16*(i-8)) + XXH3_MIDSIZE_STARTOFFSET, seed);
4080  }
4081  /* last bytes */
4082  acc += XXH3_mix16B(input + len - 16, secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET, seed);
4083  return XXH3_avalanche(acc);
4084  }
4085 }
4086 
4087 
4088 /* ======= Long Keys ======= */
4089 
4090 #define XXH_STRIPE_LEN 64
4091 #define XXH_SECRET_CONSUME_RATE 8 /* nb of secret bytes consumed at each accumulation */
4092 #define XXH_ACC_NB (XXH_STRIPE_LEN / sizeof(xxh_u64))
4093 
4094 #ifdef XXH_OLD_NAMES
4095 # define STRIPE_LEN XXH_STRIPE_LEN
4096 # define ACC_NB XXH_ACC_NB
4097 #endif
4098 
4099 XXH_FORCE_INLINE void XXH_writeLE64(void* dst, xxh_u64 v64)
4100 {
4101  if (!XXH_CPU_LITTLE_ENDIAN) v64 = XXH_swap64(v64);
4102  XXH_memcpy(dst, &v64, sizeof(v64));
4103 }
4104 
4105 /* Several intrinsic functions below are supposed to accept __int64 as argument,
4106  * as documented in https://software.intel.com/sites/landingpage/IntrinsicsGuide/ .
4107  * However, several environments do not define __int64 type,
4108  * requiring a workaround.
4109  */
4110 #if !defined (__VMS) \
4111  && (defined (__cplusplus) \
4112  || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
4113  typedef int64_t xxh_i64;
4114 #else
4115  /* the following type must have a width of 64-bit */
4116  typedef long long xxh_i64;
4117 #endif
4118 
4119 
4120 /*
4121  * XXH3_accumulate_512 is the tightest loop for long inputs, and it is the most optimized.
4122  *
4123  * It is a hardened version of UMAC, based off of FARSH's implementation.
4124  *
4125  * This was chosen because it adapts quite well to 32-bit, 64-bit, and SIMD
4126  * implementations, and it is ridiculously fast.
4127  *
4128  * We harden it by mixing the original input to the accumulators as well as the product.
4129  *
4130  * This means that in the (relatively likely) case of a multiply by zero, the
4131  * original input is preserved.
4132  *
4133  * On 128-bit inputs, we swap 64-bit pairs when we add the input to improve
4134  * cross-pollination, as otherwise the upper and lower halves would be
4135  * essentially independent.
4136  *
4137  * This doesn't matter on 64-bit hashes since they all get merged together in
4138  * the end, so we skip the extra step.
4139  *
4140  * Both XXH3_64bits and XXH3_128bits use this subroutine.
4141  */
4142 
4143 #if (XXH_VECTOR == XXH_AVX512) \
4144  || (defined(XXH_DISPATCH_AVX512) && XXH_DISPATCH_AVX512 != 0)
4145 
4146 #ifndef XXH_TARGET_AVX512
4147 # define XXH_TARGET_AVX512 /* disable attribute target */
4148 #endif
4149 
4150 XXH_FORCE_INLINE XXH_TARGET_AVX512 void
4151 XXH3_accumulate_512_avx512(void* XXH_RESTRICT acc,
4152  const void* XXH_RESTRICT input,
4153  const void* XXH_RESTRICT secret)
4154 {
4155  __m512i* const xacc = (__m512i *) acc;
4156  XXH_ASSERT((((size_t)acc) & 63) == 0);
4157  XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
4158 
4159  {
4160  /* data_vec = input[0]; */
4161  __m512i const data_vec = _mm512_loadu_si512 (input);
4162  /* key_vec = secret[0]; */
4163  __m512i const key_vec = _mm512_loadu_si512 (secret);
4164  /* data_key = data_vec ^ key_vec; */
4165  __m512i const data_key = _mm512_xor_si512 (data_vec, key_vec);
4166  /* data_key_lo = data_key >> 32; */
4167  __m512i const data_key_lo = _mm512_shuffle_epi32 (data_key, (_MM_PERM_ENUM)_MM_SHUFFLE(0, 3, 0, 1));
4168  /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
4169  __m512i const product = _mm512_mul_epu32 (data_key, data_key_lo);
4170  /* xacc[0] += swap(data_vec); */
4171  __m512i const data_swap = _mm512_shuffle_epi32(data_vec, (_MM_PERM_ENUM)_MM_SHUFFLE(1, 0, 3, 2));
4172  __m512i const sum = _mm512_add_epi64(*xacc, data_swap);
4173  /* xacc[0] += product; */
4174  *xacc = _mm512_add_epi64(product, sum);
4175  }
4176 }
4177 
4178 /*
4179  * XXH3_scrambleAcc: Scrambles the accumulators to improve mixing.
4180  *
4181  * Multiplication isn't perfect, as explained by Google in HighwayHash:
4182  *
4183  * // Multiplication mixes/scrambles bytes 0-7 of the 64-bit result to
4184  * // varying degrees. In descending order of goodness, bytes
4185  * // 3 4 2 5 1 6 0 7 have quality 228 224 164 160 100 96 36 32.
4186  * // As expected, the upper and lower bytes are much worse.
4187  *
4188  * Source: https://github.com/google/highwayhash/blob/0aaf66b/highwayhash/hh_avx2.h#L291
4189  *
4190  * Since our algorithm uses a pseudorandom secret to add some variance into the
4191  * mix, we don't need to (or want to) mix as often or as much as HighwayHash does.
4192  *
4193  * This isn't as tight as XXH3_accumulate, but still written in SIMD to avoid
4194  * extraction.
4195  *
4196  * Both XXH3_64bits and XXH3_128bits use this subroutine.
4197  */
4198 
4199 XXH_FORCE_INLINE XXH_TARGET_AVX512 void
4200 XXH3_scrambleAcc_avx512(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
4201 {
4202  XXH_ASSERT((((size_t)acc) & 63) == 0);
4203  XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
4204  { __m512i* const xacc = (__m512i*) acc;
4205  const __m512i prime32 = _mm512_set1_epi32((int)XXH_PRIME32_1);
4206 
4207  /* xacc[0] ^= (xacc[0] >> 47) */
4208  __m512i const acc_vec = *xacc;
4209  __m512i const shifted = _mm512_srli_epi64 (acc_vec, 47);
4210  __m512i const data_vec = _mm512_xor_si512 (acc_vec, shifted);
4211  /* xacc[0] ^= secret; */
4212  __m512i const key_vec = _mm512_loadu_si512 (secret);
4213  __m512i const data_key = _mm512_xor_si512 (data_vec, key_vec);
4214 
4215  /* xacc[0] *= XXH_PRIME32_1; */
4216  __m512i const data_key_hi = _mm512_shuffle_epi32 (data_key, (_MM_PERM_ENUM)_MM_SHUFFLE(0, 3, 0, 1));
4217  __m512i const prod_lo = _mm512_mul_epu32 (data_key, prime32);
4218  __m512i const prod_hi = _mm512_mul_epu32 (data_key_hi, prime32);
4219  *xacc = _mm512_add_epi64(prod_lo, _mm512_slli_epi64(prod_hi, 32));
4220  }
4221 }
4222 
4223 XXH_FORCE_INLINE XXH_TARGET_AVX512 void
4224 XXH3_initCustomSecret_avx512(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
4225 {
4226  XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 63) == 0);
4227  XXH_STATIC_ASSERT(XXH_SEC_ALIGN == 64);
4228  XXH_ASSERT(((size_t)customSecret & 63) == 0);
4229  (void)(&XXH_writeLE64);
4230  { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m512i);
4231  __m512i const seed_pos = _mm512_set1_epi64(seed64);
4232  __m512i const seed = _mm512_mask_sub_epi64(seed_pos, 0xAA, _mm512_set1_epi8(0), seed_pos);
4233 
4234  const __m512i* const src = (const __m512i*) ((const void*) XXH3_kSecret);
4235  __m512i* const dest = ( __m512i*) customSecret;
4236  int i;
4237  XXH_ASSERT(((size_t)src & 63) == 0); /* control alignment */
4238  XXH_ASSERT(((size_t)dest & 63) == 0);
4239  for (i=0; i < nbRounds; ++i) {
4240  dest[i] = _mm512_add_epi64(_mm512_load_si512(src + i), seed);
4241  } }
4242 }
4243 
4244 #endif
4245 
4246 #if (XXH_VECTOR == XXH_AVX2) \
4247  || (defined(XXH_DISPATCH_AVX2) && XXH_DISPATCH_AVX2 != 0)
4248 
4249 #ifndef XXH_TARGET_AVX2
4250 # define XXH_TARGET_AVX2 /* disable attribute target */
4251 #endif
4252 
4253 XXH_FORCE_INLINE XXH_TARGET_AVX2 void
4254 XXH3_accumulate_512_avx2( void* XXH_RESTRICT acc,
4255  const void* XXH_RESTRICT input,
4256  const void* XXH_RESTRICT secret)
4257 {
4258  XXH_ASSERT((((size_t)acc) & 31) == 0);
4259  { __m256i* const xacc = (__m256i *) acc;
4260  /* Unaligned. This is mainly for pointer arithmetic, and because
4261  * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
4262  const __m256i* const xinput = (const __m256i *) input;
4263  /* Unaligned. This is mainly for pointer arithmetic, and because
4264  * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
4265  const __m256i* const xsecret = (const __m256i *) secret;
4266 
4267  size_t i;
4268  for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
4269  /* data_vec = xinput[i]; */
4270  __m256i const data_vec = _mm256_loadu_si256 (xinput+i);
4271  /* key_vec = xsecret[i]; */
4272  __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
4273  /* data_key = data_vec ^ key_vec; */
4274  __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
4275  /* data_key_lo = data_key >> 32; */
4276  __m256i const data_key_lo = _mm256_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
4277  /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
4278  __m256i const product = _mm256_mul_epu32 (data_key, data_key_lo);
4279  /* xacc[i] += swap(data_vec); */
4280  __m256i const data_swap = _mm256_shuffle_epi32(data_vec, _MM_SHUFFLE(1, 0, 3, 2));
4281  __m256i const sum = _mm256_add_epi64(xacc[i], data_swap);
4282  /* xacc[i] += product; */
4283  xacc[i] = _mm256_add_epi64(product, sum);
4284  } }
4285 }
4286 
4287 XXH_FORCE_INLINE XXH_TARGET_AVX2 void
4288 XXH3_scrambleAcc_avx2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
4289 {
4290  XXH_ASSERT((((size_t)acc) & 31) == 0);
4291  { __m256i* const xacc = (__m256i*) acc;
4292  /* Unaligned. This is mainly for pointer arithmetic, and because
4293  * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
4294  const __m256i* const xsecret = (const __m256i *) secret;
4295  const __m256i prime32 = _mm256_set1_epi32((int)XXH_PRIME32_1);
4296 
4297  size_t i;
4298  for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
4299  /* xacc[i] ^= (xacc[i] >> 47) */
4300  __m256i const acc_vec = xacc[i];
4301  __m256i const shifted = _mm256_srli_epi64 (acc_vec, 47);
4302  __m256i const data_vec = _mm256_xor_si256 (acc_vec, shifted);
4303  /* xacc[i] ^= xsecret; */
4304  __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
4305  __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
4306 
4307  /* xacc[i] *= XXH_PRIME32_1; */
4308  __m256i const data_key_hi = _mm256_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
4309  __m256i const prod_lo = _mm256_mul_epu32 (data_key, prime32);
4310  __m256i const prod_hi = _mm256_mul_epu32 (data_key_hi, prime32);
4311  xacc[i] = _mm256_add_epi64(prod_lo, _mm256_slli_epi64(prod_hi, 32));
4312  }
4313  }
4314 }
4315 
4316 XXH_FORCE_INLINE XXH_TARGET_AVX2 void XXH3_initCustomSecret_avx2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
4317 {
4318  XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 31) == 0);
4319  XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE / sizeof(__m256i)) == 6);
4320  XXH_STATIC_ASSERT(XXH_SEC_ALIGN <= 64);
4321  (void)(&XXH_writeLE64);
4322  XXH_PREFETCH(customSecret);
4323  { __m256i const seed = _mm256_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64, (xxh_i64)(0U - seed64), (xxh_i64)seed64);
4324 
4325  const __m256i* const src = (const __m256i*) ((const void*) XXH3_kSecret);
4326  __m256i* dest = ( __m256i*) customSecret;
4327 
4328 # if defined(__GNUC__) || defined(__clang__)
4329  /*
4330  * On GCC & Clang, marking 'dest' as modified will cause the compiler:
4331  * - do not extract the secret from sse registers in the internal loop
4332  * - use less common registers, and avoid pushing these reg into stack
4333  */
4334  XXH_COMPILER_GUARD(dest);
4335 # endif
4336  XXH_ASSERT(((size_t)src & 31) == 0); /* control alignment */
4337  XXH_ASSERT(((size_t)dest & 31) == 0);
4338 
4339  /* GCC -O2 need unroll loop manually */
4340  dest[0] = _mm256_add_epi64(_mm256_load_si256(src+0), seed);
4341  dest[1] = _mm256_add_epi64(_mm256_load_si256(src+1), seed);
4342  dest[2] = _mm256_add_epi64(_mm256_load_si256(src+2), seed);
4343  dest[3] = _mm256_add_epi64(_mm256_load_si256(src+3), seed);
4344  dest[4] = _mm256_add_epi64(_mm256_load_si256(src+4), seed);
4345  dest[5] = _mm256_add_epi64(_mm256_load_si256(src+5), seed);
4346  }
4347 }
4348 
4349 #endif
4350 
4351 /* x86dispatch always generates SSE2 */
4352 #if (XXH_VECTOR == XXH_SSE2) || defined(XXH_X86DISPATCH)
4353 
4354 #ifndef XXH_TARGET_SSE2
4355 # define XXH_TARGET_SSE2 /* disable attribute target */
4356 #endif
4357 
4358 XXH_FORCE_INLINE XXH_TARGET_SSE2 void
4359 XXH3_accumulate_512_sse2( void* XXH_RESTRICT acc,
4360  const void* XXH_RESTRICT input,
4361  const void* XXH_RESTRICT secret)
4362 {
4363  /* SSE2 is just a half-scale version of the AVX2 version. */
4364  XXH_ASSERT((((size_t)acc) & 15) == 0);
4365  { __m128i* const xacc = (__m128i *) acc;
4366  /* Unaligned. This is mainly for pointer arithmetic, and because
4367  * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
4368  const __m128i* const xinput = (const __m128i *) input;
4369  /* Unaligned. This is mainly for pointer arithmetic, and because
4370  * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
4371  const __m128i* const xsecret = (const __m128i *) secret;
4372 
4373  size_t i;
4374  for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
4375  /* data_vec = xinput[i]; */
4376  __m128i const data_vec = _mm_loadu_si128 (xinput+i);
4377  /* key_vec = xsecret[i]; */
4378  __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
4379  /* data_key = data_vec ^ key_vec; */
4380  __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
4381  /* data_key_lo = data_key >> 32; */
4382  __m128i const data_key_lo = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
4383  /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
4384  __m128i const product = _mm_mul_epu32 (data_key, data_key_lo);
4385  /* xacc[i] += swap(data_vec); */
4386  __m128i const data_swap = _mm_shuffle_epi32(data_vec, _MM_SHUFFLE(1,0,3,2));
4387  __m128i const sum = _mm_add_epi64(xacc[i], data_swap);
4388  /* xacc[i] += product; */
4389  xacc[i] = _mm_add_epi64(product, sum);
4390  } }
4391 }
4392 
4393 XXH_FORCE_INLINE XXH_TARGET_SSE2 void
4394 XXH3_scrambleAcc_sse2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
4395 {
4396  XXH_ASSERT((((size_t)acc) & 15) == 0);
4397  { __m128i* const xacc = (__m128i*) acc;
4398  /* Unaligned. This is mainly for pointer arithmetic, and because
4399  * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
4400  const __m128i* const xsecret = (const __m128i *) secret;
4401  const __m128i prime32 = _mm_set1_epi32((int)XXH_PRIME32_1);
4402 
4403  size_t i;
4404  for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
4405  /* xacc[i] ^= (xacc[i] >> 47) */
4406  __m128i const acc_vec = xacc[i];
4407  __m128i const shifted = _mm_srli_epi64 (acc_vec, 47);
4408  __m128i const data_vec = _mm_xor_si128 (acc_vec, shifted);
4409  /* xacc[i] ^= xsecret[i]; */
4410  __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
4411  __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
4412 
4413  /* xacc[i] *= XXH_PRIME32_1; */
4414  __m128i const data_key_hi = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
4415  __m128i const prod_lo = _mm_mul_epu32 (data_key, prime32);
4416  __m128i const prod_hi = _mm_mul_epu32 (data_key_hi, prime32);
4417  xacc[i] = _mm_add_epi64(prod_lo, _mm_slli_epi64(prod_hi, 32));
4418  }
4419  }
4420 }
4421 
4422 XXH_FORCE_INLINE XXH_TARGET_SSE2 void XXH3_initCustomSecret_sse2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
4423 {
4424  XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
4425  (void)(&XXH_writeLE64);
4426  { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m128i);
4427 
4428 # if defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1900
4429  /* MSVC 32bit mode does not support _mm_set_epi64x before 2015 */
4430  XXH_ALIGN(16) const xxh_i64 seed64x2[2] = { (xxh_i64)seed64, (xxh_i64)(0U - seed64) };
4431  __m128i const seed = _mm_load_si128((__m128i const*)seed64x2);
4432 # else
4433  __m128i const seed = _mm_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64);
4434 # endif
4435  int i;
4436 
4437  const void* const src16 = XXH3_kSecret;
4438  __m128i* dst16 = (__m128i*) customSecret;
4439 # if defined(__GNUC__) || defined(__clang__)
4440  /*
4441  * On GCC & Clang, marking 'dest' as modified will cause the compiler:
4442  * - do not extract the secret from sse registers in the internal loop
4443  * - use less common registers, and avoid pushing these reg into stack
4444  */
4445  XXH_COMPILER_GUARD(dst16);
4446 # endif
4447  XXH_ASSERT(((size_t)src16 & 15) == 0); /* control alignment */
4448  XXH_ASSERT(((size_t)dst16 & 15) == 0);
4449 
4450  for (i=0; i < nbRounds; ++i) {
4451  dst16[i] = _mm_add_epi64(_mm_load_si128((const __m128i *)src16+i), seed);
4452  } }
4453 }
4454 
4455 #endif
4456 
4457 #if (XXH_VECTOR == XXH_NEON)
4458 
4459 /* forward declarations for the scalar routines */
4460 XXH_FORCE_INLINE void
4461 XXH3_scalarRound(void* XXH_RESTRICT acc, void const* XXH_RESTRICT input,
4462  void const* XXH_RESTRICT secret, size_t lane);
4463 
4464 XXH_FORCE_INLINE void
4465 XXH3_scalarScrambleRound(void* XXH_RESTRICT acc,
4466  void const* XXH_RESTRICT secret, size_t lane);
4467 
4468 /*!
4469  * @internal
4470  * @brief The bulk processing loop for NEON.
4471  *
4472  * The NEON code path is actually partially scalar when running on AArch64. This
4473  * is to optimize the pipelining and can have up to 15% speedup depending on the
4474  * CPU, and it also mitigates some GCC codegen issues.
4475  *
4476  * @see XXH3_NEON_LANES for configuring this and details about this optimization.
4477  */
4478 XXH_FORCE_INLINE void
4479 XXH3_accumulate_512_neon( void* XXH_RESTRICT acc,
4480  const void* XXH_RESTRICT input,
4481  const void* XXH_RESTRICT secret)
4482 {
4483  XXH_ASSERT((((size_t)acc) & 15) == 0);
4484  XXH_STATIC_ASSERT(XXH3_NEON_LANES > 0 && XXH3_NEON_LANES <= XXH_ACC_NB && XXH3_NEON_LANES % 2 == 0);
4485  {
4486  uint64x2_t* const xacc = (uint64x2_t *) acc;
4487  /* We don't use a uint32x4_t pointer because it causes bus errors on ARMv7. */
4488  uint8_t const* const xinput = (const uint8_t *) input;
4489  uint8_t const* const xsecret = (const uint8_t *) secret;
4490 
4491  size_t i;
4492  /* AArch64 uses both scalar and neon at the same time */
4493  for (i = XXH3_NEON_LANES; i < XXH_ACC_NB; i++) {
4494  XXH3_scalarRound(acc, input, secret, i);
4495  }
4496  i = 0;
4497  for (; i+1 < XXH3_NEON_LANES / 2; i+=2) {
4498  uint64x2_t acc_vec1 = xacc[i];
4499  /* data_vec = xinput[i]; */
4500  uint64x2_t data_vec1 = XXH_vld1q_u64(xinput + (i * 16));
4501  /* key_vec = xsecret[i]; */
4502  uint64x2_t key_vec1 = XXH_vld1q_u64(xsecret + (i * 16));
4503  /* acc_vec_2 = swap(data_vec) */
4504  uint64x2_t acc_vec_21 = vextq_u64(data_vec1, data_vec1, 1);
4505  /* data_key = data_vec ^ key_vec; */
4506  uint64x2_t data_key1 = veorq_u64(data_vec1, key_vec1);
4507 
4508  uint64x2_t acc_vec2 = xacc[i+1];
4509  /* data_vec = xinput[i]; */
4510  uint64x2_t data_vec2 = XXH_vld1q_u64(xinput + ((i+1) * 16));
4511  /* key_vec = xsecret[i]; */
4512  uint64x2_t key_vec2 = XXH_vld1q_u64(xsecret + ((i+1) * 16));
4513  /* acc_vec_2 = swap(data_vec) */
4514  uint64x2_t acc_vec_22 = vextq_u64(data_vec2, data_vec2, 1);
4515  /* data_key = data_vec ^ key_vec; */
4516  uint64x2_t data_key2 = veorq_u64(data_vec2, key_vec2);
4517 
4518  /* data_key_lo = {(data_key1 & 0xFFFFFFFF), (data_key2 & 0xFFFFFFFF)};
4519  * data_key_hi = {(data_key1 >> 32), (data_key2 >> 32)};
4520  */
4521  uint32x4x2_t zipped = vuzpq_u32(vreinterpretq_u32_u64(data_key1), vreinterpretq_u32_u64(data_key2));
4522  uint32x4_t data_key_lo = zipped.val[0];
4523  uint32x4_t data_key_hi = zipped.val[1];
4524 
4525  /* acc_vec_2 += (uint64x2_t) data_key_lo * (uint64x2_t) data_key_hi; */
4526  acc_vec_21 = vmlal_u32 (acc_vec_21, vget_low_u32(data_key_lo), vget_low_u32(data_key_hi));
4527  XXH_COMPILER_GUARD_W(acc_vec_21);
4528  /* xacc[i] += acc_vec_2; */
4529  acc_vec1 = vaddq_u64 (acc_vec1, acc_vec_21);
4530  xacc[i] = acc_vec1;
4531  /* acc_vec_2 += (uint64x2_t) data_key_lo * (uint64x2_t) data_key_hi; */
4532  acc_vec_22 = vmlal_u32 (acc_vec_22, vget_high_u32(data_key_lo), vget_high_u32(data_key_hi));
4533  XXH_COMPILER_GUARD_W(acc_vec_22);
4534  /* xacc[i] += acc_vec_2; */
4535  acc_vec2 = vaddq_u64 (acc_vec2, acc_vec_22);
4536  xacc[i+1] = acc_vec2;
4537  }
4538  for (; i < XXH3_NEON_LANES / 2; i++) {
4539  uint64x2_t acc_vec = xacc[i];
4540  /* data_vec = xinput[i]; */
4541  uint64x2_t data_vec = XXH_vld1q_u64(xinput + (i * 16));
4542  /* key_vec = xsecret[i]; */
4543  uint64x2_t key_vec = XXH_vld1q_u64(xsecret + (i * 16));
4544  uint64x2_t data_key;
4545  uint32x2_t data_key_lo, data_key_hi;
4546  /* acc_vec_2 = swap(data_vec) */
4547  uint64x2_t acc_vec_2 = vextq_u64(data_vec, data_vec, 1);
4548  /* data_key = data_vec ^ key_vec; */
4549  data_key = veorq_u64(data_vec, key_vec);
4550  /* data_key_lo = (uint32x2_t) (data_key & 0xFFFFFFFF);
4551  * data_key_hi = (uint32x2_t) (data_key >> 32);
4552  * data_key = UNDEFINED; */
4553  XXH_SPLIT_IN_PLACE(data_key, data_key_lo, data_key_hi);
4554  /* acc_vec_2 += (uint64x2_t) data_key_lo * (uint64x2_t) data_key_hi; */
4555  acc_vec_2 = vmlal_u32 (acc_vec_2, data_key_lo, data_key_hi);
4556  XXH_COMPILER_GUARD_W(acc_vec_2);
4557  /* xacc[i] += acc_vec_2; */
4558  acc_vec = vaddq_u64 (acc_vec, acc_vec_2);
4559  xacc[i] = acc_vec;
4560  }
4561 
4562  }
4563 }
4564 
4565 XXH_FORCE_INLINE void
4566 XXH3_scrambleAcc_neon(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
4567 {
4568  XXH_ASSERT((((size_t)acc) & 15) == 0);
4569 
4570  { uint64x2_t* xacc = (uint64x2_t*) acc;
4571  uint8_t const* xsecret = (uint8_t const*) secret;
4572  uint32x2_t prime = vdup_n_u32 (XXH_PRIME32_1);
4573 
4574  size_t i;
4575  /* AArch64 uses both scalar and neon at the same time */
4576  for (i = XXH3_NEON_LANES; i < XXH_ACC_NB; i++) {
4577  XXH3_scalarScrambleRound(acc, secret, i);
4578  }
4579  for (i=0; i < XXH3_NEON_LANES / 2; i++) {
4580  /* xacc[i] ^= (xacc[i] >> 47); */
4581  uint64x2_t acc_vec = xacc[i];
4582  uint64x2_t shifted = vshrq_n_u64 (acc_vec, 47);
4583  uint64x2_t data_vec = veorq_u64 (acc_vec, shifted);
4584 
4585  /* xacc[i] ^= xsecret[i]; */
4586  uint64x2_t key_vec = XXH_vld1q_u64 (xsecret + (i * 16));
4587  uint64x2_t data_key = veorq_u64 (data_vec, key_vec);
4588 
4589  /* xacc[i] *= XXH_PRIME32_1 */
4590  uint32x2_t data_key_lo, data_key_hi;
4591  /* data_key_lo = (uint32x2_t) (xacc[i] & 0xFFFFFFFF);
4592  * data_key_hi = (uint32x2_t) (xacc[i] >> 32);
4593  * xacc[i] = UNDEFINED; */
4594  XXH_SPLIT_IN_PLACE(data_key, data_key_lo, data_key_hi);
4595  { /*
4596  * prod_hi = (data_key >> 32) * XXH_PRIME32_1;
4597  *
4598  * Avoid vmul_u32 + vshll_n_u32 since Clang 6 and 7 will
4599  * incorrectly "optimize" this:
4600  * tmp = vmul_u32(vmovn_u64(a), vmovn_u64(b));
4601  * shifted = vshll_n_u32(tmp, 32);
4602  * to this:
4603  * tmp = "vmulq_u64"(a, b); // no such thing!
4604  * shifted = vshlq_n_u64(tmp, 32);
4605  *
4606  * However, unlike SSE, Clang lacks a 64-bit multiply routine
4607  * for NEON, and it scalarizes two 64-bit multiplies instead.
4608  *
4609  * vmull_u32 has the same timing as vmul_u32, and it avoids
4610  * this bug completely.
4611  * See https://bugs.llvm.org/show_bug.cgi?id=39967
4612  */
4613  uint64x2_t prod_hi = vmull_u32 (data_key_hi, prime);
4614  /* xacc[i] = prod_hi << 32; */
4615  prod_hi = vshlq_n_u64(prod_hi, 32);
4616  /* xacc[i] += (prod_hi & 0xFFFFFFFF) * XXH_PRIME32_1; */
4617  xacc[i] = vmlal_u32(prod_hi, data_key_lo, prime);
4618  }
4619  }
4620  }
4621 }
4622 
4623 #endif
4624 
4625 #if (XXH_VECTOR == XXH_VSX)
4626 
4627 XXH_FORCE_INLINE void
4628 XXH3_accumulate_512_vsx( void* XXH_RESTRICT acc,
4629  const void* XXH_RESTRICT input,
4630  const void* XXH_RESTRICT secret)
4631 {
4632  /* presumed aligned */
4633  unsigned int* const xacc = (unsigned int*) acc;
4634  xxh_u64x2 const* const xinput = (xxh_u64x2 const*) input; /* no alignment restriction */
4635  xxh_u64x2 const* const xsecret = (xxh_u64x2 const*) secret; /* no alignment restriction */
4636  xxh_u64x2 const v32 = { 32, 32 };
4637  size_t i;
4638  for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
4639  /* data_vec = xinput[i]; */
4640  xxh_u64x2 const data_vec = XXH_vec_loadu(xinput + i);
4641  /* key_vec = xsecret[i]; */
4642  xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + i);
4643  xxh_u64x2 const data_key = data_vec ^ key_vec;
4644  /* shuffled = (data_key << 32) | (data_key >> 32); */
4645  xxh_u32x4 const shuffled = (xxh_u32x4)vec_rl(data_key, v32);
4646  /* product = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)shuffled & 0xFFFFFFFF); */
4647  xxh_u64x2 const product = XXH_vec_mulo((xxh_u32x4)data_key, shuffled);
4648  /* acc_vec = xacc[i]; */
4649  xxh_u64x2 acc_vec = (xxh_u64x2)vec_xl(0, xacc + 4 * i);
4650  acc_vec += product;
4651 
4652  /* swap high and low halves */
4653 #ifdef __s390x__
4654  acc_vec += vec_permi(data_vec, data_vec, 2);
4655 #else
4656  acc_vec += vec_xxpermdi(data_vec, data_vec, 2);
4657 #endif
4658  /* xacc[i] = acc_vec; */
4659  vec_xst((xxh_u32x4)acc_vec, 0, xacc + 4 * i);
4660  }
4661 }
4662 
4663 XXH_FORCE_INLINE void
4664 XXH3_scrambleAcc_vsx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
4665 {
4666  XXH_ASSERT((((size_t)acc) & 15) == 0);
4667 
4668  { xxh_u64x2* const xacc = (xxh_u64x2*) acc;
4669  const xxh_u64x2* const xsecret = (const xxh_u64x2*) secret;
4670  /* constants */
4671  xxh_u64x2 const v32 = { 32, 32 };
4672  xxh_u64x2 const v47 = { 47, 47 };
4673  xxh_u32x4 const prime = { XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1 };
4674  size_t i;
4675  for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
4676  /* xacc[i] ^= (xacc[i] >> 47); */
4677  xxh_u64x2 const acc_vec = xacc[i];
4678  xxh_u64x2 const data_vec = acc_vec ^ (acc_vec >> v47);
4679 
4680  /* xacc[i] ^= xsecret[i]; */
4681  xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + i);
4682  xxh_u64x2 const data_key = data_vec ^ key_vec;
4683 
4684  /* xacc[i] *= XXH_PRIME32_1 */
4685  /* prod_lo = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)prime & 0xFFFFFFFF); */
4686  xxh_u64x2 const prod_even = XXH_vec_mule((xxh_u32x4)data_key, prime);
4687  /* prod_hi = ((xxh_u64x2)data_key >> 32) * ((xxh_u64x2)prime >> 32); */
4688  xxh_u64x2 const prod_odd = XXH_vec_mulo((xxh_u32x4)data_key, prime);
4689  xacc[i] = prod_odd + (prod_even << v32);
4690  } }
4691 }
4692 
4693 #endif
4694 
4695 #if (XXH_VECTOR == XXH_SVE)
4696 
4697 XXH_FORCE_INLINE void
4698 XXH3_accumulate_512_sve( void* XXH_RESTRICT acc,
4699  const void* XXH_RESTRICT input,
4700  const void* XXH_RESTRICT secret)
4701 {
4702  uint64_t *xacc = (uint64_t *)acc;
4703  const uint64_t *xinput = (const uint64_t *)(const void *)input;
4704  const uint64_t *xsecret = (const uint64_t *)(const void *)secret;
4705  svuint64_t kSwap = sveor_n_u64_z(svptrue_b64(), svindex_u64(0, 1), 1);
4706  uint64_t element_count = svcntd();
4707  if (element_count >= 8) {
4708  svbool_t mask = svptrue_pat_b64(SV_VL8);
4709  svuint64_t vacc = svld1_u64(mask, xacc);
4710  ACCRND(vacc, 0);
4711  svst1_u64(mask, xacc, vacc);
4712  } else if (element_count == 2) { /* sve128 */
4713  svbool_t mask = svptrue_pat_b64(SV_VL2);
4714  svuint64_t acc0 = svld1_u64(mask, xacc + 0);
4715  svuint64_t acc1 = svld1_u64(mask, xacc + 2);
4716  svuint64_t acc2 = svld1_u64(mask, xacc + 4);
4717  svuint64_t acc3 = svld1_u64(mask, xacc + 6);
4718  ACCRND(acc0, 0);
4719  ACCRND(acc1, 2);
4720  ACCRND(acc2, 4);
4721  ACCRND(acc3, 6);
4722  svst1_u64(mask, xacc + 0, acc0);
4723  svst1_u64(mask, xacc + 2, acc1);
4724  svst1_u64(mask, xacc + 4, acc2);
4725  svst1_u64(mask, xacc + 6, acc3);
4726  } else {
4727  svbool_t mask = svptrue_pat_b64(SV_VL4);
4728  svuint64_t acc0 = svld1_u64(mask, xacc + 0);
4729  svuint64_t acc1 = svld1_u64(mask, xacc + 4);
4730  ACCRND(acc0, 0);
4731  ACCRND(acc1, 4);
4732  svst1_u64(mask, xacc + 0, acc0);
4733  svst1_u64(mask, xacc + 4, acc1);
4734  }
4735 }
4736 
4737 #endif
4738 
4739 /* scalar variants - universal */
4740 
4741 /*!
4742  * @internal
4743  * @brief Scalar round for @ref XXH3_accumulate_512_scalar().
4744  *
4745  * This is extracted to its own function because the NEON path uses a combination
4746  * of NEON and scalar.
4747  */
4748 XXH_FORCE_INLINE void
4749 XXH3_scalarRound(void* XXH_RESTRICT acc,
4750  void const* XXH_RESTRICT input,
4751  void const* XXH_RESTRICT secret,
4752  size_t lane)
4753 {
4754  xxh_u64* xacc = (xxh_u64*) acc;
4755  xxh_u8 const* xinput = (xxh_u8 const*) input;
4756  xxh_u8 const* xsecret = (xxh_u8 const*) secret;
4757  XXH_ASSERT(lane < XXH_ACC_NB);
4758  XXH_ASSERT(((size_t)acc & (XXH_ACC_ALIGN-1)) == 0);
4759  {
4760  xxh_u64 const data_val = XXH_readLE64(xinput + lane * 8);
4761  xxh_u64 const data_key = data_val ^ XXH_readLE64(xsecret + lane * 8);
4762  xacc[lane ^ 1] += data_val; /* swap adjacent lanes */
4763  xacc[lane] += XXH_mult32to64(data_key & 0xFFFFFFFF, data_key >> 32);
4764  }
4765 }
4766 
4767 /*!
4768  * @internal
4769  * @brief Processes a 64 byte block of data using the scalar path.
4770  */
4771 XXH_FORCE_INLINE void
4772 XXH3_accumulate_512_scalar(void* XXH_RESTRICT acc,
4773  const void* XXH_RESTRICT input,
4774  const void* XXH_RESTRICT secret)
4775 {
4776  size_t i;
4777  /* ARM GCC refuses to unroll this loop, resulting in a 24% slowdown on ARMv6. */
4778 #if defined(__GNUC__) && !defined(__clang__) \
4779  && (defined(__arm__) || defined(__thumb2__)) \
4780  && defined(__ARM_FEATURE_UNALIGNED) /* no unaligned access just wastes bytes */ \
4781  && XXH_SIZE_OPT <= 0
4782 # pragma GCC unroll 8
4783 #endif
4784  for (i=0; i < XXH_ACC_NB; i++) {
4785  XXH3_scalarRound(acc, input, secret, i);
4786  }
4787 }
4788 
4789 /*!
4790  * @internal
4791  * @brief Scalar scramble step for @ref XXH3_scrambleAcc_scalar().
4792  *
4793  * This is extracted to its own function because the NEON path uses a combination
4794  * of NEON and scalar.
4795  */
4796 XXH_FORCE_INLINE void
4797 XXH3_scalarScrambleRound(void* XXH_RESTRICT acc,
4798  void const* XXH_RESTRICT secret,
4799  size_t lane)
4800 {
4801  xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned */
4802  const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
4803  XXH_ASSERT((((size_t)acc) & (XXH_ACC_ALIGN-1)) == 0);
4804  XXH_ASSERT(lane < XXH_ACC_NB);
4805  {
4806  xxh_u64 const key64 = XXH_readLE64(xsecret + lane * 8);
4807  xxh_u64 acc64 = xacc[lane];
4808  acc64 = XXH_xorshift64(acc64, 47);
4809  acc64 ^= key64;
4810  acc64 *= XXH_PRIME32_1;
4811  xacc[lane] = acc64;
4812  }
4813 }
4814 
4815 /*!
4816  * @internal
4817  * @brief Scrambles the accumulators after a large chunk has been read
4818  */
4819 XXH_FORCE_INLINE void
4820 XXH3_scrambleAcc_scalar(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
4821 {
4822  size_t i;
4823  for (i=0; i < XXH_ACC_NB; i++) {
4824  XXH3_scalarScrambleRound(acc, secret, i);
4825  }
4826 }
4827 
4828 XXH_FORCE_INLINE void
4829 XXH3_initCustomSecret_scalar(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
4830 {
4831  /*
4832  * We need a separate pointer for the hack below,
4833  * which requires a non-const pointer.
4834  * Any decent compiler will optimize this out otherwise.
4835  */
4836  const xxh_u8* kSecretPtr = XXH3_kSecret;
4837  XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
4838 
4839 #if defined(__clang__) && defined(__aarch64__)
4840  /*
4841  * UGLY HACK:
4842  * Clang generates a bunch of MOV/MOVK pairs for aarch64, and they are
4843  * placed sequentially, in order, at the top of the unrolled loop.
4844  *
4845  * While MOVK is great for generating constants (2 cycles for a 64-bit
4846  * constant compared to 4 cycles for LDR), it fights for bandwidth with
4847  * the arithmetic instructions.
4848  *
4849  * I L S
4850  * MOVK
4851  * MOVK
4852  * MOVK
4853  * MOVK
4854  * ADD
4855  * SUB STR
4856  * STR
4857  * By forcing loads from memory (as the asm line causes Clang to assume
4858  * that XXH3_kSecretPtr has been changed), the pipelines are used more
4859  * efficiently:
4860  * I L S
4861  * LDR
4862  * ADD LDR
4863  * SUB STR
4864  * STR
4865  *
4866  * See XXH3_NEON_LANES for details on the pipsline.
4867  *
4868  * XXH3_64bits_withSeed, len == 256, Snapdragon 835
4869  * without hack: 2654.4 MB/s
4870  * with hack: 3202.9 MB/s
4871  */
4872  XXH_COMPILER_GUARD(kSecretPtr);
4873 #endif
4874  /*
4875  * Note: in debug mode, this overrides the asm optimization
4876  * and Clang will emit MOVK chains again.
4877  */
4878  XXH_ASSERT(kSecretPtr == XXH3_kSecret);
4879 
4880  { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / 16;
4881  int i;
4882  for (i=0; i < nbRounds; i++) {
4883  /*
4884  * The asm hack causes Clang to assume that kSecretPtr aliases with
4885  * customSecret, and on aarch64, this prevented LDP from merging two
4886  * loads together for free. Putting the loads together before the stores
4887  * properly generates LDP.
4888  */
4889  xxh_u64 lo = XXH_readLE64(kSecretPtr + 16*i) + seed64;
4890  xxh_u64 hi = XXH_readLE64(kSecretPtr + 16*i + 8) - seed64;
4891  XXH_writeLE64((xxh_u8*)customSecret + 16*i, lo);
4892  XXH_writeLE64((xxh_u8*)customSecret + 16*i + 8, hi);
4893  } }
4894 }
4895 
4896 
4897 typedef void (*XXH3_f_accumulate_512)(void* XXH_RESTRICT, const void*, const void*);
4898 typedef void (*XXH3_f_scrambleAcc)(void* XXH_RESTRICT, const void*);
4899 typedef void (*XXH3_f_initCustomSecret)(void* XXH_RESTRICT, xxh_u64);
4900 
4901 
4902 #if (XXH_VECTOR == XXH_AVX512)
4903 
4904 #define XXH3_accumulate_512 XXH3_accumulate_512_avx512
4905 #define XXH3_scrambleAcc XXH3_scrambleAcc_avx512
4906 #define XXH3_initCustomSecret XXH3_initCustomSecret_avx512
4907 
4908 #elif (XXH_VECTOR == XXH_AVX2)
4909 
4910 #define XXH3_accumulate_512 XXH3_accumulate_512_avx2
4911 #define XXH3_scrambleAcc XXH3_scrambleAcc_avx2
4912 #define XXH3_initCustomSecret XXH3_initCustomSecret_avx2
4913 
4914 #elif (XXH_VECTOR == XXH_SSE2)
4915 
4916 #define XXH3_accumulate_512 XXH3_accumulate_512_sse2
4917 #define XXH3_scrambleAcc XXH3_scrambleAcc_sse2
4918 #define XXH3_initCustomSecret XXH3_initCustomSecret_sse2
4919 
4920 #elif (XXH_VECTOR == XXH_NEON)
4921 
4922 #define XXH3_accumulate_512 XXH3_accumulate_512_neon
4923 #define XXH3_scrambleAcc XXH3_scrambleAcc_neon
4924 #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
4925 
4926 #elif (XXH_VECTOR == XXH_VSX)
4927 
4928 #define XXH3_accumulate_512 XXH3_accumulate_512_vsx
4929 #define XXH3_scrambleAcc XXH3_scrambleAcc_vsx
4930 #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
4931 
4932 #elif (XXH_VECTOR == XXH_SVE)
4933 #define XXH3_accumulate_512 XXH3_accumulate_512_sve
4934 #define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
4935 #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
4936 
4937 #else /* scalar */
4938 
4939 #define XXH3_accumulate_512 XXH3_accumulate_512_scalar
4940 #define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
4941 #define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
4942 
4943 #endif
4944 
4945 #if XXH_SIZE_OPT >= 1 /* don't do SIMD for initialization */
4946 # undef XXH3_initCustomSecret
4947 # define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
4948 #endif
4949 
4950 #ifndef XXH_PREFETCH_DIST
4951 # ifdef __clang__
4952 # define XXH_PREFETCH_DIST 320
4953 # else
4954 # if (XXH_VECTOR == XXH_AVX512)
4955 # define XXH_PREFETCH_DIST 512
4956 # else
4957 # define XXH_PREFETCH_DIST 384
4958 # endif
4959 # endif /* __clang__ */
4960 #endif /* XXH_PREFETCH_DIST */
4961 
4962 /*
4963  * XXH3_accumulate()
4964  * Loops over XXH3_accumulate_512().
4965  * Assumption: nbStripes will not overflow the secret size
4966  */
4967 XXH_FORCE_INLINE void
4968 XXH3_accumulate( xxh_u64* XXH_RESTRICT acc,
4969  const xxh_u8* XXH_RESTRICT input,
4970  const xxh_u8* XXH_RESTRICT secret,
4971  size_t nbStripes,
4972  XXH3_f_accumulate_512 f_acc512)
4973 {
4974  size_t n;
4975  for (n = 0; n < nbStripes; n++ ) {
4976  const xxh_u8* const in = input + n*XXH_STRIPE_LEN;
4977  XXH_PREFETCH(in + XXH_PREFETCH_DIST);
4978  f_acc512(acc,
4979  in,
4980  secret + n*XXH_SECRET_CONSUME_RATE);
4981  }
4982 }
4983 
4984 XXH_FORCE_INLINE void
4985 XXH3_hashLong_internal_loop(xxh_u64* XXH_RESTRICT acc,
4986  const xxh_u8* XXH_RESTRICT input, size_t len,
4987  const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
4988  XXH3_f_accumulate_512 f_acc512,
4989  XXH3_f_scrambleAcc f_scramble)
4990 {
4991  size_t const nbStripesPerBlock = (secretSize - XXH_STRIPE_LEN) / XXH_SECRET_CONSUME_RATE;
4992  size_t const block_len = XXH_STRIPE_LEN * nbStripesPerBlock;
4993  size_t const nb_blocks = (len - 1) / block_len;
4994 
4995  size_t n;
4996 
4997  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
4998 
4999  for (n = 0; n < nb_blocks; n++) {
5000  XXH3_accumulate(acc, input + n*block_len, secret, nbStripesPerBlock, f_acc512);
5001  f_scramble(acc, secret + secretSize - XXH_STRIPE_LEN);
5002  }
5003 
5004  /* last partial block */
5005  XXH_ASSERT(len > XXH_STRIPE_LEN);
5006  { size_t const nbStripes = ((len - 1) - (block_len * nb_blocks)) / XXH_STRIPE_LEN;
5007  XXH_ASSERT(nbStripes <= (secretSize / XXH_SECRET_CONSUME_RATE));
5008  XXH3_accumulate(acc, input + nb_blocks*block_len, secret, nbStripes, f_acc512);
5009 
5010  /* last stripe */
5011  { const xxh_u8* const p = input + len - XXH_STRIPE_LEN;
5012 #define XXH_SECRET_LASTACC_START 7 /* not aligned on 8, last secret is different from acc & scrambler */
5013  f_acc512(acc, p, secret + secretSize - XXH_STRIPE_LEN - XXH_SECRET_LASTACC_START);
5014  } }
5015 }
5016 
5017 XXH_FORCE_INLINE xxh_u64
5018 XXH3_mix2Accs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret)
5019 {
5020  return XXH3_mul128_fold64(
5021  acc[0] ^ XXH_readLE64(secret),
5022  acc[1] ^ XXH_readLE64(secret+8) );
5023 }
5024 
5025 static XXH64_hash_t
5026 XXH3_mergeAccs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, xxh_u64 start)
5027 {
5028  xxh_u64 result64 = start;
5029  size_t i = 0;
5030 
5031  for (i = 0; i < 4; i++) {
5032  result64 += XXH3_mix2Accs(acc+2*i, secret + 16*i);
5033 #if defined(__clang__) /* Clang */ \
5034  && (defined(__arm__) || defined(__thumb__)) /* ARMv7 */ \
5035  && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
5036  && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
5037  /*
5038  * UGLY HACK:
5039  * Prevent autovectorization on Clang ARMv7-a. Exact same problem as
5040  * the one in XXH3_len_129to240_64b. Speeds up shorter keys > 240b.
5041  * XXH3_64bits, len == 256, Snapdragon 835:
5042  * without hack: 2063.7 MB/s
5043  * with hack: 2560.7 MB/s
5044  */
5045  XXH_COMPILER_GUARD(result64);
5046 #endif
5047  }
5048 
5049  return XXH3_avalanche(result64);
5050 }
5051 
5052 #define XXH3_INIT_ACC { XXH_PRIME32_3, XXH_PRIME64_1, XXH_PRIME64_2, XXH_PRIME64_3, \
5053  XXH_PRIME64_4, XXH_PRIME32_2, XXH_PRIME64_5, XXH_PRIME32_1 }
5054 
5055 XXH_FORCE_INLINE XXH64_hash_t
5056 XXH3_hashLong_64b_internal(const void* XXH_RESTRICT input, size_t len,
5057  const void* XXH_RESTRICT secret, size_t secretSize,
5058  XXH3_f_accumulate_512 f_acc512,
5059  XXH3_f_scrambleAcc f_scramble)
5060 {
5061  XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
5062 
5063  XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize, f_acc512, f_scramble);
5064 
5065  /* converge into final hash */
5066  XXH_STATIC_ASSERT(sizeof(acc) == 64);
5067  /* do not align on 8, so that the secret is different from the accumulator */
5068 #define XXH_SECRET_MERGEACCS_START 11
5069  XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
5070  return XXH3_mergeAccs(acc, (const xxh_u8*)secret + XXH_SECRET_MERGEACCS_START, (xxh_u64)len * XXH_PRIME64_1);
5071 }
5072 
5073 /*
5074  * It's important for performance to transmit secret's size (when it's static)
5075  * so that the compiler can properly optimize the vectorized loop.
5076  * This makes a big performance difference for "medium" keys (<1 KB) when using AVX instruction set.
5077  */
5078 XXH_FORCE_INLINE XXH64_hash_t
5079 XXH3_hashLong_64b_withSecret(const void* XXH_RESTRICT input, size_t len,
5080  XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
5081 {
5082  (void)seed64;
5083  return XXH3_hashLong_64b_internal(input, len, secret, secretLen, XXH3_accumulate_512, XXH3_scrambleAcc);
5084 }
5085 
5086 /*
5087  * It's preferable for performance that XXH3_hashLong is not inlined,
5088  * as it results in a smaller function for small data, easier to the instruction cache.
5089  * Note that inside this no_inline function, we do inline the internal loop,
5090  * and provide a statically defined secret size to allow optimization of vector loop.
5091  */
5092 XXH_NO_INLINE XXH_PUREF XXH64_hash_t
5093 XXH3_hashLong_64b_default(const void* XXH_RESTRICT input, size_t len,
5094  XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
5095 {
5096  (void)seed64; (void)secret; (void)secretLen;
5097  return XXH3_hashLong_64b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_accumulate_512, XXH3_scrambleAcc);
5098 }
5099 
5100 /*
5101  * XXH3_hashLong_64b_withSeed():
5102  * Generate a custom key based on alteration of default XXH3_kSecret with the seed,
5103  * and then use this key for long mode hashing.
5104  *
5105  * This operation is decently fast but nonetheless costs a little bit of time.
5106  * Try to avoid it whenever possible (typically when seed==0).
5107  *
5108  * It's important for performance that XXH3_hashLong is not inlined. Not sure
5109  * why (uop cache maybe?), but the difference is large and easily measurable.
5110  */
5111 XXH_FORCE_INLINE XXH64_hash_t
5112 XXH3_hashLong_64b_withSeed_internal(const void* input, size_t len,
5113  XXH64_hash_t seed,
5114  XXH3_f_accumulate_512 f_acc512,
5115  XXH3_f_scrambleAcc f_scramble,
5116  XXH3_f_initCustomSecret f_initSec)
5117 {
5118 #if XXH_SIZE_OPT <= 0
5119  if (seed == 0)
5120  return XXH3_hashLong_64b_internal(input, len,
5121  XXH3_kSecret, sizeof(XXH3_kSecret),
5122  f_acc512, f_scramble);
5123 #endif
5124  { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
5125  f_initSec(secret, seed);
5126  return XXH3_hashLong_64b_internal(input, len, secret, sizeof(secret),
5127  f_acc512, f_scramble);
5128  }
5129 }
5130 
5131 /*
5132  * It's important for performance that XXH3_hashLong is not inlined.
5133  */
5134 XXH_NO_INLINE XXH64_hash_t
5135 XXH3_hashLong_64b_withSeed(const void* XXH_RESTRICT input, size_t len,
5136  XXH64_hash_t seed, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
5137 {
5138  (void)secret; (void)secretLen;
5139  return XXH3_hashLong_64b_withSeed_internal(input, len, seed,
5140  XXH3_accumulate_512, XXH3_scrambleAcc, XXH3_initCustomSecret);
5141 }
5142 
5143 
5144 typedef XXH64_hash_t (*XXH3_hashLong64_f)(const void* XXH_RESTRICT, size_t,
5145  XXH64_hash_t, const xxh_u8* XXH_RESTRICT, size_t);
5146 
5147 XXH_FORCE_INLINE XXH64_hash_t
5148 XXH3_64bits_internal(const void* XXH_RESTRICT input, size_t len,
5149  XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
5150  XXH3_hashLong64_f f_hashLong)
5151 {
5152  XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
5153  /*
5154  * If an action is to be taken if `secretLen` condition is not respected,
5155  * it should be done here.
5156  * For now, it's a contract pre-condition.
5157  * Adding a check and a branch here would cost performance at every hash.
5158  * Also, note that function signature doesn't offer room to return an error.
5159  */
5160  if (len <= 16)
5161  return XXH3_len_0to16_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
5162  if (len <= 128)
5163  return XXH3_len_17to128_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
5164  if (len <= XXH3_MIDSIZE_MAX)
5165  return XXH3_len_129to240_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
5166  return f_hashLong(input, len, seed64, (const xxh_u8*)secret, secretLen);
5167 }
5168 
5169 
5170 /* === Public entry point === */
5171 
5172 /*! @ingroup XXH3_family */
5173 XXH_PUBLIC_API XXH64_hash_t XXH3_64bits(const void* input, size_t length)
5174 {
5175  return XXH3_64bits_internal(input, length, 0, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_default);
5176 }
5177 
5178 /*! @ingroup XXH3_family */
5179 XXH_PUBLIC_API XXH64_hash_t
5180 XXH3_64bits_withSecret(const void* input, size_t length, const void* secret, size_t secretSize)
5181 {
5182  return XXH3_64bits_internal(input, length, 0, secret, secretSize, XXH3_hashLong_64b_withSecret);
5183 }
5184 
5185 /*! @ingroup XXH3_family */
5186 XXH_PUBLIC_API XXH64_hash_t
5187 XXH3_64bits_withSeed(const void* input, size_t length, XXH64_hash_t seed)
5188 {
5189  return XXH3_64bits_internal(input, length, seed, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_withSeed);
5190 }
5191 
5192 XXH_PUBLIC_API XXH64_hash_t
5193 XXH3_64bits_withSecretandSeed(const void* input, size_t length, const void* secret, size_t secretSize, XXH64_hash_t seed)
5194 {
5195  if (length <= XXH3_MIDSIZE_MAX)
5196  return XXH3_64bits_internal(input, length, seed, XXH3_kSecret, sizeof(XXH3_kSecret), NULL);
5197  return XXH3_hashLong_64b_withSecret(input, length, seed, (const xxh_u8*)secret, secretSize);
5198 }
5199 
5200 
5201 /* === XXH3 streaming === */
5202 #ifndef XXH_NO_STREAM
5203 /*
5204  * Malloc's a pointer that is always aligned to align.
5205  *
5206  * This must be freed with `XXH_alignedFree()`.
5207  *
5208  * malloc typically guarantees 16 byte alignment on 64-bit systems and 8 byte
5209  * alignment on 32-bit. This isn't enough for the 32 byte aligned loads in AVX2
5210  * or on 32-bit, the 16 byte aligned loads in SSE2 and NEON.
5211  *
5212  * This underalignment previously caused a rather obvious crash which went
5213  * completely unnoticed due to XXH3_createState() not actually being tested.
5214  * Credit to RedSpah for noticing this bug.
5215  *
5216  * The alignment is done manually: Functions like posix_memalign or _mm_malloc
5217  * are avoided: To maintain portability, we would have to write a fallback
5218  * like this anyways, and besides, testing for the existence of library
5219  * functions without relying on external build tools is impossible.
5220  *
5221  * The method is simple: Overallocate, manually align, and store the offset
5222  * to the original behind the returned pointer.
5223  *
5224  * Align must be a power of 2 and 8 <= align <= 128.
5225  */
5226 static XXH_MALLOCF void* XXH_alignedMalloc(size_t s, size_t align)
5227 {
5228  XXH_ASSERT(align <= 128 && align >= 8); /* range check */
5229  XXH_ASSERT((align & (align-1)) == 0); /* power of 2 */
5230  XXH_ASSERT(s != 0 && s < (s + align)); /* empty/overflow */
5231  { /* Overallocate to make room for manual realignment and an offset byte */
5232  xxh_u8* base = (xxh_u8*)XXH_malloc(s + align);
5233  if (base != NULL) {
5234  /*
5235  * Get the offset needed to align this pointer.
5236  *
5237  * Even if the returned pointer is aligned, there will always be
5238  * at least one byte to store the offset to the original pointer.
5239  */
5240  size_t offset = align - ((size_t)base & (align - 1)); /* base % align */
5241  /* Add the offset for the now-aligned pointer */
5242  xxh_u8* ptr = base + offset;
5243 
5244  XXH_ASSERT((size_t)ptr % align == 0);
5245 
5246  /* Store the offset immediately before the returned pointer. */
5247  ptr[-1] = (xxh_u8)offset;
5248  return ptr;
5249  }
5250  return NULL;
5251  }
5252 }
5253 /*
5254  * Frees an aligned pointer allocated by XXH_alignedMalloc(). Don't pass
5255  * normal malloc'd pointers, XXH_alignedMalloc has a specific data layout.
5256  */
5257 static void XXH_alignedFree(void* p)
5258 {
5259  if (p != NULL) {
5260  xxh_u8* ptr = (xxh_u8*)p;
5261  /* Get the offset byte we added in XXH_malloc. */
5262  xxh_u8 offset = ptr[-1];
5263  /* Free the original malloc'd pointer */
5264  xxh_u8* base = ptr - offset;
5265  XXH_free(base);
5266  }
5267 }
5268 /*! @ingroup XXH3_family */
5270 {
5271  XXH3_state_t* const state = (XXH3_state_t*)XXH_alignedMalloc(sizeof(XXH3_state_t), 64);
5272  if (state==NULL) return NULL;
5273  XXH3_INITSTATE(state);
5274  return state;
5275 }
5276 
5277 /*! @ingroup XXH3_family */
5279 {
5280  XXH_alignedFree(statePtr);
5281  return XXH_OK;
5282 }
5283 
5284 /*! @ingroup XXH3_family */
5285 XXH_PUBLIC_API void
5286 XXH3_copyState(XXH3_state_t* dst_state, const XXH3_state_t* src_state)
5287 {
5288  XXH_memcpy(dst_state, src_state, sizeof(*dst_state));
5289 }
5290 
5291 static void
5292 XXH3_reset_internal(XXH3_state_t* statePtr,
5293  XXH64_hash_t seed,
5294  const void* secret, size_t secretSize)
5295 {
5296  size_t const initStart = offsetof(XXH3_state_t, bufferedSize);
5297  size_t const initLength = offsetof(XXH3_state_t, nbStripesPerBlock) - initStart;
5298  XXH_ASSERT(offsetof(XXH3_state_t, nbStripesPerBlock) > initStart);
5299  XXH_ASSERT(statePtr != NULL);
5300  /* set members from bufferedSize to nbStripesPerBlock (excluded) to 0 */
5301  memset((char*)statePtr + initStart, 0, initLength);
5302  statePtr->acc[0] = XXH_PRIME32_3;
5303  statePtr->acc[1] = XXH_PRIME64_1;
5304  statePtr->acc[2] = XXH_PRIME64_2;
5305  statePtr->acc[3] = XXH_PRIME64_3;
5306  statePtr->acc[4] = XXH_PRIME64_4;
5307  statePtr->acc[5] = XXH_PRIME32_2;
5308  statePtr->acc[6] = XXH_PRIME64_5;
5309  statePtr->acc[7] = XXH_PRIME32_1;
5310  statePtr->seed = seed;
5311  statePtr->useSeed = (seed != 0);
5312  statePtr->extSecret = (const unsigned char*)secret;
5313  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
5314  statePtr->secretLimit = secretSize - XXH_STRIPE_LEN;
5315  statePtr->nbStripesPerBlock = statePtr->secretLimit / XXH_SECRET_CONSUME_RATE;
5316 }
5317 
5318 /*! @ingroup XXH3_family */
5321 {
5322  if (statePtr == NULL) return XXH_ERROR;
5323  XXH3_reset_internal(statePtr, 0, XXH3_kSecret, XXH_SECRET_DEFAULT_SIZE);
5324  return XXH_OK;
5325 }
5326 
5327 /*! @ingroup XXH3_family */
5329 XXH3_64bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize)
5330 {
5331  if (statePtr == NULL) return XXH_ERROR;
5332  XXH3_reset_internal(statePtr, 0, secret, secretSize);
5333  if (secret == NULL) return XXH_ERROR;
5334  if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
5335  return XXH_OK;
5336 }
5337 
5338 /*! @ingroup XXH3_family */
5340 XXH3_64bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed)
5341 {
5342  if (statePtr == NULL) return XXH_ERROR;
5343  if (seed==0) return XXH3_64bits_reset(statePtr);
5344  if ((seed != statePtr->seed) || (statePtr->extSecret != NULL))
5345  XXH3_initCustomSecret(statePtr->customSecret, seed);
5346  XXH3_reset_internal(statePtr, seed, NULL, XXH_SECRET_DEFAULT_SIZE);
5347  return XXH_OK;
5348 }
5349 
5350 /*! @ingroup XXH3_family */
5352 XXH3_64bits_reset_withSecretandSeed(XXH3_state_t* statePtr, const void* secret, size_t secretSize, XXH64_hash_t seed64)
5353 {
5354  if (statePtr == NULL) return XXH_ERROR;
5355  if (secret == NULL) return XXH_ERROR;
5356  if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
5357  XXH3_reset_internal(statePtr, seed64, secret, secretSize);
5358  statePtr->useSeed = 1; /* always, even if seed64==0 */
5359  return XXH_OK;
5360 }
5361 
5362 /* Note : when XXH3_consumeStripes() is invoked,
5363  * there must be a guarantee that at least one more byte must be consumed from input
5364  * so that the function can blindly consume all stripes using the "normal" secret segment */
5365 XXH_FORCE_INLINE void
5366 XXH3_consumeStripes(xxh_u64* XXH_RESTRICT acc,
5367  size_t* XXH_RESTRICT nbStripesSoFarPtr, size_t nbStripesPerBlock,
5368  const xxh_u8* XXH_RESTRICT input, size_t nbStripes,
5369  const xxh_u8* XXH_RESTRICT secret, size_t secretLimit,
5370  XXH3_f_accumulate_512 f_acc512,
5371  XXH3_f_scrambleAcc f_scramble)
5372 {
5373  XXH_ASSERT(nbStripes <= nbStripesPerBlock); /* can handle max 1 scramble per invocation */
5374  XXH_ASSERT(*nbStripesSoFarPtr < nbStripesPerBlock);
5375  if (nbStripesPerBlock - *nbStripesSoFarPtr <= nbStripes) {
5376  /* need a scrambling operation */
5377  size_t const nbStripesToEndofBlock = nbStripesPerBlock - *nbStripesSoFarPtr;
5378  size_t const nbStripesAfterBlock = nbStripes - nbStripesToEndofBlock;
5379  XXH3_accumulate(acc, input, secret + nbStripesSoFarPtr[0] * XXH_SECRET_CONSUME_RATE, nbStripesToEndofBlock, f_acc512);
5380  f_scramble(acc, secret + secretLimit);
5381  XXH3_accumulate(acc, input + nbStripesToEndofBlock * XXH_STRIPE_LEN, secret, nbStripesAfterBlock, f_acc512);
5382  *nbStripesSoFarPtr = nbStripesAfterBlock;
5383  } else {
5384  XXH3_accumulate(acc, input, secret + nbStripesSoFarPtr[0] * XXH_SECRET_CONSUME_RATE, nbStripes, f_acc512);
5385  *nbStripesSoFarPtr += nbStripes;
5386  }
5387 }
5388 
5389 #ifndef XXH3_STREAM_USE_STACK
5390 # if XXH_SIZE_OPT <= 0 && !defined(__clang__) /* clang doesn't need additional stack space */
5391 # define XXH3_STREAM_USE_STACK 1
5392 # endif
5393 #endif
5394 /*
5395  * Both XXH3_64bits_update and XXH3_128bits_update use this routine.
5396  */
5397 XXH_FORCE_INLINE XXH_errorcode
5398 XXH3_update(XXH3_state_t* XXH_RESTRICT const state,
5399  const xxh_u8* XXH_RESTRICT input, size_t len,
5400  XXH3_f_accumulate_512 f_acc512,
5401  XXH3_f_scrambleAcc f_scramble)
5402 {
5403  if (input==NULL) {
5404  XXH_ASSERT(len == 0);
5405  return XXH_OK;
5406  }
5407 
5408  XXH_ASSERT(state != NULL);
5409  { const xxh_u8* const bEnd = input + len;
5410  const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
5411 #if defined(XXH3_STREAM_USE_STACK) && XXH3_STREAM_USE_STACK >= 1
5412  /* For some reason, gcc and MSVC seem to suffer greatly
5413  * when operating accumulators directly into state.
5414  * Operating into stack space seems to enable proper optimization.
5415  * clang, on the other hand, doesn't seem to need this trick */
5416  XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[8]; memcpy(acc, state->acc, sizeof(acc));
5417 #else
5418  xxh_u64* XXH_RESTRICT const acc = state->acc;
5419 #endif
5420  state->totalLen += len;
5421  XXH_ASSERT(state->bufferedSize <= XXH3_INTERNALBUFFER_SIZE);
5422 
5423  /* small input : just fill in tmp buffer */
5424  if (state->bufferedSize + len <= XXH3_INTERNALBUFFER_SIZE) {
5425  XXH_memcpy(state->buffer + state->bufferedSize, input, len);
5426  state->bufferedSize += (XXH32_hash_t)len;
5427  return XXH_OK;
5428  }
5429 
5430  /* total input is now > XXH3_INTERNALBUFFER_SIZE */
5431  #define XXH3_INTERNALBUFFER_STRIPES (XXH3_INTERNALBUFFER_SIZE / XXH_STRIPE_LEN)
5432  XXH_STATIC_ASSERT(XXH3_INTERNALBUFFER_SIZE % XXH_STRIPE_LEN == 0); /* clean multiple */
5433 
5434  /*
5435  * Internal buffer is partially filled (always, except at beginning)
5436  * Complete it, then consume it.
5437  */
5438  if (state->bufferedSize) {
5439  size_t const loadSize = XXH3_INTERNALBUFFER_SIZE - state->bufferedSize;
5440  XXH_memcpy(state->buffer + state->bufferedSize, input, loadSize);
5441  input += loadSize;
5442  XXH3_consumeStripes(acc,
5443  &state->nbStripesSoFar, state->nbStripesPerBlock,
5444  state->buffer, XXH3_INTERNALBUFFER_STRIPES,
5445  secret, state->secretLimit,
5446  f_acc512, f_scramble);
5447  state->bufferedSize = 0;
5448  }
5449  XXH_ASSERT(input < bEnd);
5450 
5451  /* large input to consume : ingest per full block */
5452  if ((size_t)(bEnd - input) > state->nbStripesPerBlock * XXH_STRIPE_LEN) {
5453  size_t nbStripes = (size_t)(bEnd - 1 - input) / XXH_STRIPE_LEN;
5454  XXH_ASSERT(state->nbStripesPerBlock >= state->nbStripesSoFar);
5455  /* join to current block's end */
5456  { size_t const nbStripesToEnd = state->nbStripesPerBlock - state->nbStripesSoFar;
5457  XXH_ASSERT(nbStripesToEnd <= nbStripes);
5458  XXH3_accumulate(acc, input, secret + state->nbStripesSoFar * XXH_SECRET_CONSUME_RATE, nbStripesToEnd, f_acc512);
5459  f_scramble(acc, secret + state->secretLimit);
5460  state->nbStripesSoFar = 0;
5461  input += nbStripesToEnd * XXH_STRIPE_LEN;
5462  nbStripes -= nbStripesToEnd;
5463  }
5464  /* consume per entire blocks */
5465  while(nbStripes >= state->nbStripesPerBlock) {
5466  XXH3_accumulate(acc, input, secret, state->nbStripesPerBlock, f_acc512);
5467  f_scramble(acc, secret + state->secretLimit);
5468  input += state->nbStripesPerBlock * XXH_STRIPE_LEN;
5469  nbStripes -= state->nbStripesPerBlock;
5470  }
5471  /* consume last partial block */
5472  XXH3_accumulate(acc, input, secret, nbStripes, f_acc512);
5473  input += nbStripes * XXH_STRIPE_LEN;
5474  XXH_ASSERT(input < bEnd); /* at least some bytes left */
5475  state->nbStripesSoFar = nbStripes;
5476  /* buffer predecessor of last partial stripe */
5477  XXH_memcpy(state->buffer + sizeof(state->buffer) - XXH_STRIPE_LEN, input - XXH_STRIPE_LEN, XXH_STRIPE_LEN);
5478  XXH_ASSERT(bEnd - input <= XXH_STRIPE_LEN);
5479  } else {
5480  /* content to consume <= block size */
5481  /* Consume input by a multiple of internal buffer size */
5482  if (bEnd - input > XXH3_INTERNALBUFFER_SIZE) {
5483  const xxh_u8* const limit = bEnd - XXH3_INTERNALBUFFER_SIZE;
5484  do {
5485  XXH3_consumeStripes(acc,
5486  &state->nbStripesSoFar, state->nbStripesPerBlock,
5487  input, XXH3_INTERNALBUFFER_STRIPES,
5488  secret, state->secretLimit,
5489  f_acc512, f_scramble);
5490  input += XXH3_INTERNALBUFFER_SIZE;
5491  } while (input<limit);
5492  /* buffer predecessor of last partial stripe */
5493  XXH_memcpy(state->buffer + sizeof(state->buffer) - XXH_STRIPE_LEN, input - XXH_STRIPE_LEN, XXH_STRIPE_LEN);
5494  }
5495  }
5496 
5497  /* Some remaining input (always) : buffer it */
5498  XXH_ASSERT(input < bEnd);
5499  XXH_ASSERT(bEnd - input <= XXH3_INTERNALBUFFER_SIZE);
5500  XXH_ASSERT(state->bufferedSize == 0);
5501  XXH_memcpy(state->buffer, input, (size_t)(bEnd-input));
5502  state->bufferedSize = (XXH32_hash_t)(bEnd-input);
5503 #if defined(XXH3_STREAM_USE_STACK) && XXH3_STREAM_USE_STACK >= 1
5504  /* save stack accumulators into state */
5505  memcpy(state->acc, acc, sizeof(acc));
5506 #endif
5507  }
5508 
5509  return XXH_OK;
5510 }
5511 
5512 /*! @ingroup XXH3_family */
5514 XXH3_64bits_update(XXH3_state_t* state, const void* input, size_t len)
5515 {
5516  return XXH3_update(state, (const xxh_u8*)input, len,
5517  XXH3_accumulate_512, XXH3_scrambleAcc);
5518 }
5519 
5520 
5521 XXH_FORCE_INLINE void
5522 XXH3_digest_long (XXH64_hash_t* acc,
5523  const XXH3_state_t* state,
5524  const unsigned char* secret)
5525 {
5526  /*
5527  * Digest on a local copy. This way, the state remains unaltered, and it can
5528  * continue ingesting more input afterwards.
5529  */
5530  XXH_memcpy(acc, state->acc, sizeof(state->acc));
5531  if (state->bufferedSize >= XXH_STRIPE_LEN) {
5532  size_t const nbStripes = (state->bufferedSize - 1) / XXH_STRIPE_LEN;
5533  size_t nbStripesSoFar = state->nbStripesSoFar;
5534  XXH3_consumeStripes(acc,
5535  &nbStripesSoFar, state->nbStripesPerBlock,
5536  state->buffer, nbStripes,
5537  secret, state->secretLimit,
5538  XXH3_accumulate_512, XXH3_scrambleAcc);
5539  /* last stripe */
5540  XXH3_accumulate_512(acc,
5541  state->buffer + state->bufferedSize - XXH_STRIPE_LEN,
5542  secret + state->secretLimit - XXH_SECRET_LASTACC_START);
5543  } else { /* bufferedSize < XXH_STRIPE_LEN */
5544  xxh_u8 lastStripe[XXH_STRIPE_LEN];
5545  size_t const catchupSize = XXH_STRIPE_LEN - state->bufferedSize;
5546  XXH_ASSERT(state->bufferedSize > 0); /* there is always some input buffered */
5547  XXH_memcpy(lastStripe, state->buffer + sizeof(state->buffer) - catchupSize, catchupSize);
5548  XXH_memcpy(lastStripe + catchupSize, state->buffer, state->bufferedSize);
5549  XXH3_accumulate_512(acc,
5550  lastStripe,
5551  secret + state->secretLimit - XXH_SECRET_LASTACC_START);
5552  }
5553 }
5554 
5555 /*! @ingroup XXH3_family */
5556 XXH_PUBLIC_API XXH64_hash_t XXH3_64bits_digest (const XXH3_state_t* state)
5557 {
5558  const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
5559  if (state->totalLen > XXH3_MIDSIZE_MAX) {
5560  XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
5561  XXH3_digest_long(acc, state, secret);
5562  return XXH3_mergeAccs(acc,
5563  secret + XXH_SECRET_MERGEACCS_START,
5564  (xxh_u64)state->totalLen * XXH_PRIME64_1);
5565  }
5566  /* totalLen <= XXH3_MIDSIZE_MAX: digesting a short input */
5567  if (state->useSeed)
5568  return XXH3_64bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
5569  return XXH3_64bits_withSecret(state->buffer, (size_t)(state->totalLen),
5570  secret, state->secretLimit + XXH_STRIPE_LEN);
5571 }
5572 #endif /* !XXH_NO_STREAM */
5573 
5574 
5575 /* ==========================================
5576  * XXH3 128 bits (a.k.a XXH128)
5577  * ==========================================
5578  * XXH3's 128-bit variant has better mixing and strength than the 64-bit variant,
5579  * even without counting the significantly larger output size.
5580  *
5581  * For example, extra steps are taken to avoid the seed-dependent collisions
5582  * in 17-240 byte inputs (See XXH3_mix16B and XXH128_mix32B).
5583  *
5584  * This strength naturally comes at the cost of some speed, especially on short
5585  * lengths. Note that longer hashes are about as fast as the 64-bit version
5586  * due to it using only a slight modification of the 64-bit loop.
5587  *
5588  * XXH128 is also more oriented towards 64-bit machines. It is still extremely
5589  * fast for a _128-bit_ hash on 32-bit (it usually clears XXH64).
5590  */
5591 
5592 XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
5593 XXH3_len_1to3_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
5594 {
5595  /* A doubled version of 1to3_64b with different constants. */
5596  XXH_ASSERT(input != NULL);
5597  XXH_ASSERT(1 <= len && len <= 3);
5598  XXH_ASSERT(secret != NULL);
5599  /*
5600  * len = 1: combinedl = { input[0], 0x01, input[0], input[0] }
5601  * len = 2: combinedl = { input[1], 0x02, input[0], input[1] }
5602  * len = 3: combinedl = { input[2], 0x03, input[0], input[1] }
5603  */
5604  { xxh_u8 const c1 = input[0];
5605  xxh_u8 const c2 = input[len >> 1];
5606  xxh_u8 const c3 = input[len - 1];
5607  xxh_u32 const combinedl = ((xxh_u32)c1 <<16) | ((xxh_u32)c2 << 24)
5608  | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
5609  xxh_u32 const combinedh = XXH_rotl32(XXH_swap32(combinedl), 13);
5610  xxh_u64 const bitflipl = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
5611  xxh_u64 const bitfliph = (XXH_readLE32(secret+8) ^ XXH_readLE32(secret+12)) - seed;
5612  xxh_u64 const keyed_lo = (xxh_u64)combinedl ^ bitflipl;
5613  xxh_u64 const keyed_hi = (xxh_u64)combinedh ^ bitfliph;
5614  XXH128_hash_t h128;
5615  h128.low64 = XXH64_avalanche(keyed_lo);
5616  h128.high64 = XXH64_avalanche(keyed_hi);
5617  return h128;
5618  }
5619 }
5620 
5621 XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
5622 XXH3_len_4to8_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
5623 {
5624  XXH_ASSERT(input != NULL);
5625  XXH_ASSERT(secret != NULL);
5626  XXH_ASSERT(4 <= len && len <= 8);
5627  seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
5628  { xxh_u32 const input_lo = XXH_readLE32(input);
5629  xxh_u32 const input_hi = XXH_readLE32(input + len - 4);
5630  xxh_u64 const input_64 = input_lo + ((xxh_u64)input_hi << 32);
5631  xxh_u64 const bitflip = (XXH_readLE64(secret+16) ^ XXH_readLE64(secret+24)) + seed;
5632  xxh_u64 const keyed = input_64 ^ bitflip;
5633 
5634  /* Shift len to the left to ensure it is even, this avoids even multiplies. */
5635  XXH128_hash_t m128 = XXH_mult64to128(keyed, XXH_PRIME64_1 + (len << 2));
5636 
5637  m128.high64 += (m128.low64 << 1);
5638  m128.low64 ^= (m128.high64 >> 3);
5639 
5640  m128.low64 = XXH_xorshift64(m128.low64, 35);
5641  m128.low64 *= 0x9FB21C651E98DF25ULL;
5642  m128.low64 = XXH_xorshift64(m128.low64, 28);
5643  m128.high64 = XXH3_avalanche(m128.high64);
5644  return m128;
5645  }
5646 }
5647 
5648 XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
5649 XXH3_len_9to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
5650 {
5651  XXH_ASSERT(input != NULL);
5652  XXH_ASSERT(secret != NULL);
5653  XXH_ASSERT(9 <= len && len <= 16);
5654  { xxh_u64 const bitflipl = (XXH_readLE64(secret+32) ^ XXH_readLE64(secret+40)) - seed;
5655  xxh_u64 const bitfliph = (XXH_readLE64(secret+48) ^ XXH_readLE64(secret+56)) + seed;
5656  xxh_u64 const input_lo = XXH_readLE64(input);
5657  xxh_u64 input_hi = XXH_readLE64(input + len - 8);
5658  XXH128_hash_t m128 = XXH_mult64to128(input_lo ^ input_hi ^ bitflipl, XXH_PRIME64_1);
5659  /*
5660  * Put len in the middle of m128 to ensure that the length gets mixed to
5661  * both the low and high bits in the 128x64 multiply below.
5662  */
5663  m128.low64 += (xxh_u64)(len - 1) << 54;
5664  input_hi ^= bitfliph;
5665  /*
5666  * Add the high 32 bits of input_hi to the high 32 bits of m128, then
5667  * add the long product of the low 32 bits of input_hi and XXH_PRIME32_2 to
5668  * the high 64 bits of m128.
5669  *
5670  * The best approach to this operation is different on 32-bit and 64-bit.
5671  */
5672  if (sizeof(void *) < sizeof(xxh_u64)) { /* 32-bit */
5673  /*
5674  * 32-bit optimized version, which is more readable.
5675  *
5676  * On 32-bit, it removes an ADC and delays a dependency between the two
5677  * halves of m128.high64, but it generates an extra mask on 64-bit.
5678  */
5679  m128.high64 += (input_hi & 0xFFFFFFFF00000000ULL) + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2);
5680  } else {
5681  /*
5682  * 64-bit optimized (albeit more confusing) version.
5683  *
5684  * Uses some properties of addition and multiplication to remove the mask:
5685  *
5686  * Let:
5687  * a = input_hi.lo = (input_hi & 0x00000000FFFFFFFF)
5688  * b = input_hi.hi = (input_hi & 0xFFFFFFFF00000000)
5689  * c = XXH_PRIME32_2
5690  *
5691  * a + (b * c)
5692  * Inverse Property: x + y - x == y
5693  * a + (b * (1 + c - 1))
5694  * Distributive Property: x * (y + z) == (x * y) + (x * z)
5695  * a + (b * 1) + (b * (c - 1))
5696  * Identity Property: x * 1 == x
5697  * a + b + (b * (c - 1))
5698  *
5699  * Substitute a, b, and c:
5700  * input_hi.hi + input_hi.lo + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
5701  *
5702  * Since input_hi.hi + input_hi.lo == input_hi, we get this:
5703  * input_hi + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
5704  */
5705  m128.high64 += input_hi + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2 - 1);
5706  }
5707  /* m128 ^= XXH_swap64(m128 >> 64); */
5708  m128.low64 ^= XXH_swap64(m128.high64);
5709 
5710  { /* 128x64 multiply: h128 = m128 * XXH_PRIME64_2; */
5711  XXH128_hash_t h128 = XXH_mult64to128(m128.low64, XXH_PRIME64_2);
5712  h128.high64 += m128.high64 * XXH_PRIME64_2;
5713 
5714  h128.low64 = XXH3_avalanche(h128.low64);
5715  h128.high64 = XXH3_avalanche(h128.high64);
5716  return h128;
5717  } }
5718 }
5719 
5720 /*
5721  * Assumption: `secret` size is >= XXH3_SECRET_SIZE_MIN
5722  */
5723 XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
5724 XXH3_len_0to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
5725 {
5726  XXH_ASSERT(len <= 16);
5727  { if (len > 8) return XXH3_len_9to16_128b(input, len, secret, seed);
5728  if (len >= 4) return XXH3_len_4to8_128b(input, len, secret, seed);
5729  if (len) return XXH3_len_1to3_128b(input, len, secret, seed);
5730  { XXH128_hash_t h128;
5731  xxh_u64 const bitflipl = XXH_readLE64(secret+64) ^ XXH_readLE64(secret+72);
5732  xxh_u64 const bitfliph = XXH_readLE64(secret+80) ^ XXH_readLE64(secret+88);
5733  h128.low64 = XXH64_avalanche(seed ^ bitflipl);
5734  h128.high64 = XXH64_avalanche( seed ^ bitfliph);
5735  return h128;
5736  } }
5737 }
5738 
5739 /*
5740  * A bit slower than XXH3_mix16B, but handles multiply by zero better.
5741  */
5742 XXH_FORCE_INLINE XXH128_hash_t
5743 XXH128_mix32B(XXH128_hash_t acc, const xxh_u8* input_1, const xxh_u8* input_2,
5744  const xxh_u8* secret, XXH64_hash_t seed)
5745 {
5746  acc.low64 += XXH3_mix16B (input_1, secret+0, seed);
5747  acc.low64 ^= XXH_readLE64(input_2) + XXH_readLE64(input_2 + 8);
5748  acc.high64 += XXH3_mix16B (input_2, secret+16, seed);
5749  acc.high64 ^= XXH_readLE64(input_1) + XXH_readLE64(input_1 + 8);
5750  return acc;
5751 }
5752 
5753 
5754 XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
5755 XXH3_len_17to128_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
5756  const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
5757  XXH64_hash_t seed)
5758 {
5759  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
5760  XXH_ASSERT(16 < len && len <= 128);
5761 
5762  { XXH128_hash_t acc;
5763  acc.low64 = len * XXH_PRIME64_1;
5764  acc.high64 = 0;
5765 
5766 #if XXH_SIZE_OPT >= 1
5767  {
5768  /* Smaller, but slightly slower. */
5769  size_t i = (len - 1) / 32;
5770  do {
5771  acc = XXH128_mix32B(acc, input+16*i, input+len-16*(i+1), secret+32*i, seed);
5772  } while (i-- != 0);
5773  }
5774 #else
5775  if (len > 32) {
5776  if (len > 64) {
5777  if (len > 96) {
5778  acc = XXH128_mix32B(acc, input+48, input+len-64, secret+96, seed);
5779  }
5780  acc = XXH128_mix32B(acc, input+32, input+len-48, secret+64, seed);
5781  }
5782  acc = XXH128_mix32B(acc, input+16, input+len-32, secret+32, seed);
5783  }
5784  acc = XXH128_mix32B(acc, input, input+len-16, secret, seed);
5785 #endif
5786  { XXH128_hash_t h128;
5787  h128.low64 = acc.low64 + acc.high64;
5788  h128.high64 = (acc.low64 * XXH_PRIME64_1)
5789  + (acc.high64 * XXH_PRIME64_4)
5790  + ((len - seed) * XXH_PRIME64_2);
5791  h128.low64 = XXH3_avalanche(h128.low64);
5792  h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
5793  return h128;
5794  }
5795  }
5796 }
5797 
5798 XXH_NO_INLINE XXH_PUREF XXH128_hash_t
5799 XXH3_len_129to240_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
5800  const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
5801  XXH64_hash_t seed)
5802 {
5803  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
5804  XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
5805 
5806  { XXH128_hash_t acc;
5807  int const nbRounds = (int)len / 32;
5808  int i;
5809  acc.low64 = len * XXH_PRIME64_1;
5810  acc.high64 = 0;
5811  for (i=0; i<4; i++) {
5812  acc = XXH128_mix32B(acc,
5813  input + (32 * i),
5814  input + (32 * i) + 16,
5815  secret + (32 * i),
5816  seed);
5817  }
5818  acc.low64 = XXH3_avalanche(acc.low64);
5819  acc.high64 = XXH3_avalanche(acc.high64);
5820  XXH_ASSERT(nbRounds >= 4);
5821  for (i=4 ; i < nbRounds; i++) {
5822  acc = XXH128_mix32B(acc,
5823  input + (32 * i),
5824  input + (32 * i) + 16,
5825  secret + XXH3_MIDSIZE_STARTOFFSET + (32 * (i - 4)),
5826  seed);
5827  }
5828  /* last bytes */
5829  acc = XXH128_mix32B(acc,
5830  input + len - 16,
5831  input + len - 32,
5832  secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET - 16,
5833  0ULL - seed);
5834 
5835  { XXH128_hash_t h128;
5836  h128.low64 = acc.low64 + acc.high64;
5837  h128.high64 = (acc.low64 * XXH_PRIME64_1)
5838  + (acc.high64 * XXH_PRIME64_4)
5839  + ((len - seed) * XXH_PRIME64_2);
5840  h128.low64 = XXH3_avalanche(h128.low64);
5841  h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
5842  return h128;
5843  }
5844  }
5845 }
5846 
5847 XXH_FORCE_INLINE XXH128_hash_t
5848 XXH3_hashLong_128b_internal(const void* XXH_RESTRICT input, size_t len,
5849  const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
5850  XXH3_f_accumulate_512 f_acc512,
5851  XXH3_f_scrambleAcc f_scramble)
5852 {
5853  XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
5854 
5855  XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, secret, secretSize, f_acc512, f_scramble);
5856 
5857  /* converge into final hash */
5858  XXH_STATIC_ASSERT(sizeof(acc) == 64);
5859  XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
5860  { XXH128_hash_t h128;
5861  h128.low64 = XXH3_mergeAccs(acc,
5862  secret + XXH_SECRET_MERGEACCS_START,
5863  (xxh_u64)len * XXH_PRIME64_1);
5864  h128.high64 = XXH3_mergeAccs(acc,
5865  secret + secretSize
5866  - sizeof(acc) - XXH_SECRET_MERGEACCS_START,
5867  ~((xxh_u64)len * XXH_PRIME64_2));
5868  return h128;
5869  }
5870 }
5871 
5872 /*
5873  * It's important for performance that XXH3_hashLong() is not inlined.
5874  */
5875 XXH_NO_INLINE XXH_PUREF XXH128_hash_t
5876 XXH3_hashLong_128b_default(const void* XXH_RESTRICT input, size_t len,
5877  XXH64_hash_t seed64,
5878  const void* XXH_RESTRICT secret, size_t secretLen)
5879 {
5880  (void)seed64; (void)secret; (void)secretLen;
5881  return XXH3_hashLong_128b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret),
5882  XXH3_accumulate_512, XXH3_scrambleAcc);
5883 }
5884 
5885 /*
5886  * It's important for performance to pass @p secretLen (when it's static)
5887  * to the compiler, so that it can properly optimize the vectorized loop.
5888  */
5889 XXH_FORCE_INLINE XXH128_hash_t
5890 XXH3_hashLong_128b_withSecret(const void* XXH_RESTRICT input, size_t len,
5891  XXH64_hash_t seed64,
5892  const void* XXH_RESTRICT secret, size_t secretLen)
5893 {
5894  (void)seed64;
5895  return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, secretLen,
5896  XXH3_accumulate_512, XXH3_scrambleAcc);
5897 }
5898 
5899 XXH_FORCE_INLINE XXH128_hash_t
5900 XXH3_hashLong_128b_withSeed_internal(const void* XXH_RESTRICT input, size_t len,
5901  XXH64_hash_t seed64,
5902  XXH3_f_accumulate_512 f_acc512,
5903  XXH3_f_scrambleAcc f_scramble,
5904  XXH3_f_initCustomSecret f_initSec)
5905 {
5906  if (seed64 == 0)
5907  return XXH3_hashLong_128b_internal(input, len,
5908  XXH3_kSecret, sizeof(XXH3_kSecret),
5909  f_acc512, f_scramble);
5910  { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
5911  f_initSec(secret, seed64);
5912  return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, sizeof(secret),
5913  f_acc512, f_scramble);
5914  }
5915 }
5916 
5917 /*
5918  * It's important for performance that XXH3_hashLong is not inlined.
5919  */
5920 XXH_NO_INLINE XXH128_hash_t
5921 XXH3_hashLong_128b_withSeed(const void* input, size_t len,
5922  XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen)
5923 {
5924  (void)secret; (void)secretLen;
5925  return XXH3_hashLong_128b_withSeed_internal(input, len, seed64,
5926  XXH3_accumulate_512, XXH3_scrambleAcc, XXH3_initCustomSecret);
5927 }
5928 
5929 typedef XXH128_hash_t (*XXH3_hashLong128_f)(const void* XXH_RESTRICT, size_t,
5930  XXH64_hash_t, const void* XXH_RESTRICT, size_t);
5931 
5932 XXH_FORCE_INLINE XXH128_hash_t
5933 XXH3_128bits_internal(const void* input, size_t len,
5934  XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
5935  XXH3_hashLong128_f f_hl128)
5936 {
5937  XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
5938  /*
5939  * If an action is to be taken if `secret` conditions are not respected,
5940  * it should be done here.
5941  * For now, it's a contract pre-condition.
5942  * Adding a check and a branch here would cost performance at every hash.
5943  */
5944  if (len <= 16)
5945  return XXH3_len_0to16_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
5946  if (len <= 128)
5947  return XXH3_len_17to128_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
5948  if (len <= XXH3_MIDSIZE_MAX)
5949  return XXH3_len_129to240_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
5950  return f_hl128(input, len, seed64, secret, secretLen);
5951 }
5952 
5953 
5954 /* === Public XXH128 API === */
5955 
5956 /*! @ingroup XXH3_family */
5957 XXH_PUBLIC_API XXH128_hash_t XXH3_128bits(const void* input, size_t len)
5958 {
5959  return XXH3_128bits_internal(input, len, 0,
5960  XXH3_kSecret, sizeof(XXH3_kSecret),
5961  XXH3_hashLong_128b_default);
5962 }
5963 
5964 /*! @ingroup XXH3_family */
5966 XXH3_128bits_withSecret(const void* input, size_t len, const void* secret, size_t secretSize)
5967 {
5968  return XXH3_128bits_internal(input, len, 0,
5969  (const xxh_u8*)secret, secretSize,
5970  XXH3_hashLong_128b_withSecret);
5971 }
5972 
5973 /*! @ingroup XXH3_family */
5975 XXH3_128bits_withSeed(const void* input, size_t len, XXH64_hash_t seed)
5976 {
5977  return XXH3_128bits_internal(input, len, seed,
5978  XXH3_kSecret, sizeof(XXH3_kSecret),
5979  XXH3_hashLong_128b_withSeed);
5980 }
5981 
5982 /*! @ingroup XXH3_family */
5984 XXH3_128bits_withSecretandSeed(const void* input, size_t len, const void* secret, size_t secretSize, XXH64_hash_t seed)
5985 {
5986  if (len <= XXH3_MIDSIZE_MAX)
5987  return XXH3_128bits_internal(input, len, seed, XXH3_kSecret, sizeof(XXH3_kSecret), NULL);
5988  return XXH3_hashLong_128b_withSecret(input, len, seed, secret, secretSize);
5989 }
5990 
5991 /*! @ingroup XXH3_family */
5993 XXH128(const void* input, size_t len, XXH64_hash_t seed)
5994 {
5995  return XXH3_128bits_withSeed(input, len, seed);
5996 }
5997 
5998 
5999 /* === XXH3 128-bit streaming === */
6000 #ifndef XXH_NO_STREAM
6001 /*
6002  * All initialization and update functions are identical to 64-bit streaming variant.
6003  * The only difference is the finalization routine.
6004  */
6005 
6006 /*! @ingroup XXH3_family */
6009 {
6010  return XXH3_64bits_reset(statePtr);
6011 }
6012 
6013 /*! @ingroup XXH3_family */
6015 XXH3_128bits_reset_withSecret(XXH3_state_t* statePtr, const void* secret, size_t secretSize)
6016 {
6017  return XXH3_64bits_reset_withSecret(statePtr, secret, secretSize);
6018 }
6019 
6020 /*! @ingroup XXH3_family */
6022 XXH3_128bits_reset_withSeed(XXH3_state_t* statePtr, XXH64_hash_t seed)
6023 {
6024  return XXH3_64bits_reset_withSeed(statePtr, seed);
6025 }
6026 
6027 /*! @ingroup XXH3_family */
6029 XXH3_128bits_reset_withSecretandSeed(XXH3_state_t* statePtr, const void* secret, size_t secretSize, XXH64_hash_t seed)
6030 {
6031  return XXH3_64bits_reset_withSecretandSeed(statePtr, secret, secretSize, seed);
6032 }
6033 
6034 /*! @ingroup XXH3_family */
6036 XXH3_128bits_update(XXH3_state_t* state, const void* input, size_t len)
6037 {
6038  return XXH3_update(state, (const xxh_u8*)input, len,
6039  XXH3_accumulate_512, XXH3_scrambleAcc);
6040 }
6041 
6042 /*! @ingroup XXH3_family */
6044 {
6045  const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
6046  if (state->totalLen > XXH3_MIDSIZE_MAX) {
6047  XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
6048  XXH3_digest_long(acc, state, secret);
6049  XXH_ASSERT(state->secretLimit + XXH_STRIPE_LEN >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
6050  { XXH128_hash_t h128;
6051  h128.low64 = XXH3_mergeAccs(acc,
6052  secret + XXH_SECRET_MERGEACCS_START,
6053  (xxh_u64)state->totalLen * XXH_PRIME64_1);
6054  h128.high64 = XXH3_mergeAccs(acc,
6055  secret + state->secretLimit + XXH_STRIPE_LEN
6056  - sizeof(acc) - XXH_SECRET_MERGEACCS_START,
6057  ~((xxh_u64)state->totalLen * XXH_PRIME64_2));
6058  return h128;
6059  }
6060  }
6061  /* len <= XXH3_MIDSIZE_MAX : short code */
6062  if (state->seed)
6063  return XXH3_128bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
6064  return XXH3_128bits_withSecret(state->buffer, (size_t)(state->totalLen),
6065  secret, state->secretLimit + XXH_STRIPE_LEN);
6066 }
6067 #endif /* !XXH_NO_STREAM */
6068 /* 128-bit utility functions */
6069 
6070 #include <string.h> /* memcmp, memcpy */
6071 
6072 /* return : 1 is equal, 0 if different */
6073 /*! @ingroup XXH3_family */
6075 {
6076  /* note : XXH128_hash_t is compact, it has no padding byte */
6077  return !(memcmp(&h1, &h2, sizeof(h1)));
6078 }
6079 
6080 /* This prototype is compatible with stdlib's qsort().
6081  * @return : >0 if *h128_1 > *h128_2
6082  * <0 if *h128_1 < *h128_2
6083  * =0 if *h128_1 == *h128_2 */
6084 /*! @ingroup XXH3_family */
6085 XXH_PUBLIC_API int XXH128_cmp(const void* h128_1, const void* h128_2)
6086 {
6087  XXH128_hash_t const h1 = *(const XXH128_hash_t*)h128_1;
6088  XXH128_hash_t const h2 = *(const XXH128_hash_t*)h128_2;
6089  int const hcmp = (h1.high64 > h2.high64) - (h2.high64 > h1.high64);
6090  /* note : bets that, in most cases, hash values are different */
6091  if (hcmp) return hcmp;
6092  return (h1.low64 > h2.low64) - (h2.low64 > h1.low64);
6093 }
6094 
6095 
6096 /*====== Canonical representation ======*/
6097 /*! @ingroup XXH3_family */
6098 XXH_PUBLIC_API void
6100 {
6101  XXH_STATIC_ASSERT(sizeof(XXH128_canonical_t) == sizeof(XXH128_hash_t));
6102  if (XXH_CPU_LITTLE_ENDIAN) {
6103  hash.high64 = XXH_swap64(hash.high64);
6104  hash.low64 = XXH_swap64(hash.low64);
6105  }
6106  XXH_memcpy(dst, &hash.high64, sizeof(hash.high64));
6107  XXH_memcpy((char*)dst + sizeof(hash.high64), &hash.low64, sizeof(hash.low64));
6108 }
6109 
6110 /*! @ingroup XXH3_family */
6113 {
6114  XXH128_hash_t h;
6115  h.high64 = XXH_readBE64(src);
6116  h.low64 = XXH_readBE64(src->digest + 8);
6117  return h;
6118 }
6119 
6120 
6121 
6122 /* ==========================================
6123  * Secret generators
6124  * ==========================================
6125  */
6126 #define XXH_MIN(x, y) (((x) > (y)) ? (y) : (x))
6127 
6128 XXH_FORCE_INLINE void XXH3_combine16(void* dst, XXH128_hash_t h128)
6129 {
6130  XXH_writeLE64( dst, XXH_readLE64(dst) ^ h128.low64 );
6131  XXH_writeLE64( (char*)dst+8, XXH_readLE64((char*)dst+8) ^ h128.high64 );
6132 }
6133 
6134 /*! @ingroup XXH3_family */
6136 XXH3_generateSecret(void* secretBuffer, size_t secretSize, const void* customSeed, size_t customSeedSize)
6137 {
6138 #if (XXH_DEBUGLEVEL >= 1)
6139  XXH_ASSERT(secretBuffer != NULL);
6140  XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
6141 #else
6142  /* production mode, assert() are disabled */
6143  if (secretBuffer == NULL) return XXH_ERROR;
6144  if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
6145 #endif
6146 
6147  if (customSeedSize == 0) {
6148  customSeed = XXH3_kSecret;
6149  customSeedSize = XXH_SECRET_DEFAULT_SIZE;
6150  }
6151 #if (XXH_DEBUGLEVEL >= 1)
6152  XXH_ASSERT(customSeed != NULL);
6153 #else
6154  if (customSeed == NULL) return XXH_ERROR;
6155 #endif
6156 
6157  /* Fill secretBuffer with a copy of customSeed - repeat as needed */
6158  { size_t pos = 0;
6159  while (pos < secretSize) {
6160  size_t const toCopy = XXH_MIN((secretSize - pos), customSeedSize);
6161  memcpy((char*)secretBuffer + pos, customSeed, toCopy);
6162  pos += toCopy;
6163  } }
6164 
6165  { size_t const nbSeg16 = secretSize / 16;
6166  size_t n;
6167  XXH128_canonical_t scrambler;
6168  XXH128_canonicalFromHash(&scrambler, XXH128(customSeed, customSeedSize, 0));
6169  for (n=0; n<nbSeg16; n++) {
6170  XXH128_hash_t const h128 = XXH128(&scrambler, sizeof(scrambler), n);
6171  XXH3_combine16((char*)secretBuffer + n*16, h128);
6172  }
6173  /* last segment */
6174  XXH3_combine16((char*)secretBuffer + secretSize - 16, XXH128_hashFromCanonical(&scrambler));
6175  }
6176  return XXH_OK;
6177 }
6178 
6179 /*! @ingroup XXH3_family */
6180 XXH_PUBLIC_API void
6181 XXH3_generateSecret_fromSeed(void* secretBuffer, XXH64_hash_t seed)
6182 {
6183  XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
6184  XXH3_initCustomSecret(secret, seed);
6185  XXH_ASSERT(secretBuffer != NULL);
6186  memcpy(secretBuffer, secret, XXH_SECRET_DEFAULT_SIZE);
6187 }
6188 
6189 
6190 
6191 /* Pop our optimization override from above */
6192 #if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
6193  && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
6194  && defined(__OPTIMIZE__) && XXH_SIZE_OPT <= 0 /* respect -O0 and -Os */
6195 # pragma GCC pop_options
6196 #endif
6197 
6198 #endif /* XXH_NO_LONG_LONG */
6199 
6200 #endif /* XXH_NO_XXH3 */
6201 
6202 /*!
6203  * @}
6204  */
6205 #endif /* XXH_IMPLEMENTATION */
6206 
6207 
6208 #if defined (__cplusplus)
6209 } /* extern "C" */
6210 #endif
6211 
6212 // Wrap the hash function. Instead of using XXH64, use XXH3_64bits which is
6213 // considerably more efficient.
6214 #include <SYS/SYS_TypeTraits.h>
6215 SYS_FORCE_INLINE UT_API XXH64_hash_t UTxxhash64(const void *data, size_t len)
6216 {
6217  return XXH3_64bits(data, len);
6218 }
6219 
6220 SYS_FORCE_INLINE UT_API XXH64_hash_t UTxxhash64(const void *data, size_t len, XXH64_hash_t seed)
6221 {
6222  return XXH3_64bits_withSeed(data, len, seed);
6223 }
6224 
6225 #endif // __UT_XXHash__
#define XXH3_generateSecret_fromSeed
Definition: UT_XXHash.h:406
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
#define XXH_FALLTHROUGH
Definition: UT_XXHash.h:753
std::string upper(string_view a)
Return an all-upper case version of a (locale-independent).
Definition: strutil.h:402
#define XXH128
Definition: UT_XXHash.h:408
#define XXH_PUREF
Definition: UT_XXHash.h:452
#define XXH64_copyState
Definition: UT_XXHash.h:388
unsigned char digest[sizeof(XXH128_hash_t)]
Definition: UT_XXHash.h:1096
#define XXH64_createState
Definition: UT_XXHash.h:383
#define XXH_PUBLIC_API
Marks a global symbol.
Definition: UT_XXHash.h:443
#define XXH3_64bits_withSecret
Definition: UT_XXHash.h:393
void
Definition: png.h:1083
const GLdouble * v
Definition: glcorearb.h:837
GLuint start
Definition: glcorearb.h:475
#define XXH32_digest
Definition: UT_XXHash.h:377
#define XXH32_reset
Definition: UT_XXHash.h:375
#define XXH3_freeState
Definition: UT_XXHash.h:397
SYS_FORCE_INLINE UT_API XXH64_hash_t UTxxhash64(const void *data, size_t len)
Definition: UT_XXHash.h:6215
#define XXH64_digest
Definition: UT_XXHash.h:387
#define XXH3_64bits_withSecretandSeed
Definition: UT_XXHash.h:395
#define XXH3_64bits
Definition: UT_XXHash.h:392
#define XXH64
Definition: UT_XXHash.h:382
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define XXH32_hashFromCanonical
Definition: UT_XXHash.h:380
GLdouble s
Definition: glad.h:3009
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
#define XXH3_copyState
Definition: UT_XXHash.h:398
struct XXH3_state_s XXH3_state_t
The state struct for the XXH3 streaming API.
Definition: UT_XXHash.h:978
#define UT_API
Definition: UT_API.h:14
#define XXH3_128bits_reset
Definition: UT_XXHash.h:413
GLint y
Definition: glcorearb.h:103
The return value from 128-bit hashes.
Definition: UT_XXHash.h:1024
**But if you need a result
Definition: thread.h:613
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:819
#define XXH32_freeState
Definition: UT_XXHash.h:374
XXH_errorcode
Exit code for the streaming API.
Definition: UT_XXHash.h:483
#define XXH64_freeState
Definition: UT_XXHash.h:384
#define XXH3_64bits_digest
Definition: UT_XXHash.h:404
#define XXH_versionNumber
Definition: UT_XXHash.h:370
#define XXH3_128bits_reset_withSeed
Definition: UT_XXHash.h:414
__attribute__((visibility("default")))
unsigned long long XXH64_hash_t
Definition: UT_XXHash.h:785
#define XXH3_128bits_reset_withSecretandSeed
Definition: UT_XXHash.h:416
GLdouble n
Definition: glcorearb.h:2008
#define XXH3_128bits_withSecret
Definition: UT_XXHash.h:411
#define XXH3_generateSecret
Definition: UT_XXHash.h:405
GLintptr offset
Definition: glcorearb.h:665
Definition: core.h:760
#define XXH3_64bits_reset_withSecretandSeed
Definition: UT_XXHash.h:402
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
struct XXH64_state_s XXH64_state_t
The opaque state struct for the XXH64 streaming API.
Definition: UT_XXHash.h:835
#define XXH3_128bits_update
Definition: UT_XXHash.h:417
GLint GLuint mask
Definition: glcorearb.h:124
#define XXH_VERSION_NUMBER
Version number, encoded as two digits each.
Definition: UT_XXHash.h:463
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR_14 uint8_t packed(VULKAN_HPP_NAMESPACE::Format format)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
#define XXH128_isEqual
Definition: UT_XXHash.h:419
#define XXH32_update
Definition: UT_XXHash.h:376
#define XXH3_64bits_update
Definition: UT_XXHash.h:403
#define XXH3_128bits_reset_withSecret
Definition: UT_XXHash.h:415
#define XXH3_128bits_withSecretandSeed
Definition: UT_XXHash.h:412
GLsizeiptr size
Definition: glcorearb.h:664
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLenum GLenum dst
Definition: glcorearb.h:1793
#define XXH64_canonicalFromHash
Definition: UT_XXHash.h:389
#define XXH128_cmp
Definition: UT_XXHash.h:420
#define XXH64_reset
Definition: UT_XXHash.h:385
#define XXH64_update
Definition: UT_XXHash.h:386
#define XXH_CONSTF
Definition: UT_XXHash.h:451
#define XXH_MALLOCF
Definition: UT_XXHash.h:453
#define XXH3_64bits_reset_withSecret
Definition: UT_XXHash.h:401
std::string lower(string_view a)
Return an all-upper case version of a (locale-independent).
Definition: strutil.h:395
Canonical (big endian) representation of XXH32_hash_t.
Definition: UT_XXHash.h:695
#define XXH128_hashFromCanonical
Definition: UT_XXHash.h:422
#define XXH3_SECRET_SIZE_MIN
Definition: UT_XXHash.h:941
#define XXH3_128bits
Definition: UT_XXHash.h:409
#define XXH3_64bits_reset_withSeed
Definition: UT_XXHash.h:400
GLfloat GLfloat v1
Definition: glcorearb.h:817
auto ptr(T p) -> const void *
Definition: format.h:2448
GLuint GLfloat * val
Definition: glcorearb.h:1608
XXH64_hash_t low64
Definition: UT_XXHash.h:1025
#define XXH32_copyState
Definition: UT_XXHash.h:378
if(num_boxed_items<=0)
Definition: UT_RTreeImpl.h:697
#define XXH3_64bits_reset
Definition: UT_XXHash.h:399
#define XXH32_createState
Definition: UT_XXHash.h:373
#define XXH32_canonicalFromHash
Definition: UT_XXHash.h:379
XXH64_hash_t high64
Definition: UT_XXHash.h:1026
#define XXH3_64bits_withSeed
Definition: UT_XXHash.h:394
#define const
Definition: zconf.h:214
#define XXH3_128bits_digest
Definition: UT_XXHash.h:418
#define XXH64_hashFromCanonical
Definition: UT_XXHash.h:390
#define XXH32
Definition: UT_XXHash.h:372
#define XXH128_canonicalFromHash
Definition: UT_XXHash.h:421
SIM_DerVector3 cross(const SIM_DerVector3 &lhs, const SIM_DerVector3 &rhs)
#define XXH3_128bits_withSeed
Definition: UT_XXHash.h:410
struct XXH32_state_s XXH32_state_t
The opaque state struct for the XXH32 streaming API.
Definition: UT_XXHash.h:592
Definition: format.h:895
#define XXH3_createState
Definition: UT_XXHash.h:396
GLenum src
Definition: glcorearb.h:1793