xxHash 0.8.3
Extremely fast non-cryptographic hash function
 
Loading...
Searching...
No Matches
xxhash.h
Go to the documentation of this file.
1/*
2 * xxHash - Extremely Fast Hash algorithm
3 * Header File
4 * Copyright (C) 2012-2023 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
243
244#if defined (__cplusplus)
245extern "C" {
246#endif
247
248/* ****************************
249 * INLINE mode
250 ******************************/
256#ifdef XXH_DOXYGEN
268# define XXH_STATIC_LINKING_ONLY
269/* Do not undef XXH_STATIC_LINKING_ONLY for Doxygen */
270
281# define XXH_IMPLEMENTATION
282/* Do not undef XXH_IMPLEMENTATION for Doxygen */
283
302# define XXH_INLINE_ALL
303# undef XXH_INLINE_ALL
307# define XXH_PRIVATE_API
308# undef XXH_PRIVATE_API
322# define XXH_NAMESPACE /* YOUR NAME HERE */
323# undef XXH_NAMESPACE
324#endif
325
326#if (defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)) \
327 && !defined(XXH_INLINE_ALL_31684351384)
328 /* this section should be traversed only once */
329# define XXH_INLINE_ALL_31684351384
330 /* give access to the advanced API, required to compile implementations */
331# undef XXH_STATIC_LINKING_ONLY /* avoid macro redef */
332# define XXH_STATIC_LINKING_ONLY
333 /* make all functions private */
334# undef XXH_PUBLIC_API
335# if defined(__GNUC__)
336# define XXH_PUBLIC_API static __inline __attribute__((__unused__))
337# elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
338# define XXH_PUBLIC_API static inline
339# elif defined(_MSC_VER)
340# define XXH_PUBLIC_API static __inline
341# else
342 /* note: this version may generate warnings for unused static functions */
343# define XXH_PUBLIC_API static
344# endif
345
346 /*
347 * This part deals with the special case where a unit wants to inline xxHash,
348 * but "xxhash.h" has previously been included without XXH_INLINE_ALL,
349 * such as part of some previously included *.h header file.
350 * Without further action, the new include would just be ignored,
351 * and functions would effectively _not_ be inlined (silent failure).
352 * The following macros solve this situation by prefixing all inlined names,
353 * avoiding naming collision with previous inclusions.
354 */
355 /* Before that, we unconditionally #undef all symbols,
356 * in case they were already defined with XXH_NAMESPACE.
357 * They will then be redefined for XXH_INLINE_ALL
358 */
359# undef XXH_versionNumber
360 /* XXH32 */
361# undef XXH32
362# undef XXH32_createState
363# undef XXH32_freeState
364# undef XXH32_reset
365# undef XXH32_update
366# undef XXH32_digest
367# undef XXH32_copyState
368# undef XXH32_canonicalFromHash
369# undef XXH32_hashFromCanonical
370 /* XXH64 */
371# undef XXH64
372# undef XXH64_createState
373# undef XXH64_freeState
374# undef XXH64_reset
375# undef XXH64_update
376# undef XXH64_digest
377# undef XXH64_copyState
378# undef XXH64_canonicalFromHash
379# undef XXH64_hashFromCanonical
380 /* XXH3_64bits */
381# undef XXH3_64bits
382# undef XXH3_64bits_withSecret
383# undef XXH3_64bits_withSeed
384# undef XXH3_64bits_withSecretandSeed
385# undef XXH3_createState
386# undef XXH3_freeState
387# undef XXH3_copyState
388# undef XXH3_64bits_reset
389# undef XXH3_64bits_reset_withSeed
390# undef XXH3_64bits_reset_withSecret
391# undef XXH3_64bits_update
392# undef XXH3_64bits_digest
393# undef XXH3_generateSecret
394 /* XXH3_128bits */
395# undef XXH128
396# undef XXH3_128bits
397# undef XXH3_128bits_withSeed
398# undef XXH3_128bits_withSecret
399# undef XXH3_128bits_reset
400# undef XXH3_128bits_reset_withSeed
401# undef XXH3_128bits_reset_withSecret
402# undef XXH3_128bits_reset_withSecretandSeed
403# undef XXH3_128bits_update
404# undef XXH3_128bits_digest
405# undef XXH128_isEqual
406# undef XXH128_cmp
407# undef XXH128_canonicalFromHash
408# undef XXH128_hashFromCanonical
409 /* Finally, free the namespace itself */
410# undef XXH_NAMESPACE
411
412 /* employ the namespace for XXH_INLINE_ALL */
413# define XXH_NAMESPACE XXH_INLINE_
414 /*
415 * Some identifiers (enums, type names) are not symbols,
416 * but they must nonetheless be renamed to avoid redeclaration.
417 * Alternative solution: do not redeclare them.
418 * However, this requires some #ifdefs, and has a more dispersed impact.
419 * Meanwhile, renaming can be achieved in a single place.
420 */
421# define XXH_IPREF(Id) XXH_NAMESPACE ## Id
422# define XXH_OK XXH_IPREF(XXH_OK)
423# define XXH_ERROR XXH_IPREF(XXH_ERROR)
424# define XXH_errorcode XXH_IPREF(XXH_errorcode)
425# define XXH32_canonical_t XXH_IPREF(XXH32_canonical_t)
426# define XXH64_canonical_t XXH_IPREF(XXH64_canonical_t)
427# define XXH128_canonical_t XXH_IPREF(XXH128_canonical_t)
428# define XXH32_state_s XXH_IPREF(XXH32_state_s)
429# define XXH32_state_t XXH_IPREF(XXH32_state_t)
430# define XXH64_state_s XXH_IPREF(XXH64_state_s)
431# define XXH64_state_t XXH_IPREF(XXH64_state_t)
432# define XXH3_state_s XXH_IPREF(XXH3_state_s)
433# define XXH3_state_t XXH_IPREF(XXH3_state_t)
434# define XXH128_hash_t XXH_IPREF(XXH128_hash_t)
435 /* Ensure the header is parsed again, even if it was previously included */
436# undef XXHASH_H_5627135585666179
437# undef XXHASH_H_STATIC_13879238742
438#endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
439
440/* ****************************************************************
441 * Stable API
442 *****************************************************************/
443#ifndef XXHASH_H_5627135585666179
444#define XXHASH_H_5627135585666179 1
445
447#if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
448# if defined(_WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
449# ifdef XXH_EXPORT
450# define XXH_PUBLIC_API __declspec(dllexport)
451# elif XXH_IMPORT
452# define XXH_PUBLIC_API __declspec(dllimport)
453# endif
454# else
455# define XXH_PUBLIC_API /* do nothing */
456# endif
457#endif
458
459#ifdef XXH_NAMESPACE
460# define XXH_CAT(A,B) A##B
461# define XXH_NAME2(A,B) XXH_CAT(A,B)
462# define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
463/* XXH32 */
464# define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
465# define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
466# define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
467# define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
468# define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
469# define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
470# define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
471# define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
472# define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
473/* XXH64 */
474# define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
475# define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
476# define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
477# define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
478# define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
479# define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
480# define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
481# define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
482# define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
483/* XXH3_64bits */
484# define XXH3_64bits XXH_NAME2(XXH_NAMESPACE, XXH3_64bits)
485# define XXH3_64bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecret)
486# define XXH3_64bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSeed)
487# define XXH3_64bits_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_withSecretandSeed)
488# define XXH3_createState XXH_NAME2(XXH_NAMESPACE, XXH3_createState)
489# define XXH3_freeState XXH_NAME2(XXH_NAMESPACE, XXH3_freeState)
490# define XXH3_copyState XXH_NAME2(XXH_NAMESPACE, XXH3_copyState)
491# define XXH3_64bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset)
492# define XXH3_64bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSeed)
493# define XXH3_64bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecret)
494# define XXH3_64bits_reset_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_reset_withSecretandSeed)
495# define XXH3_64bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_update)
496# define XXH3_64bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_64bits_digest)
497# define XXH3_generateSecret XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret)
498# define XXH3_generateSecret_fromSeed XXH_NAME2(XXH_NAMESPACE, XXH3_generateSecret_fromSeed)
499/* XXH3_128bits */
500# define XXH128 XXH_NAME2(XXH_NAMESPACE, XXH128)
501# define XXH3_128bits XXH_NAME2(XXH_NAMESPACE, XXH3_128bits)
502# define XXH3_128bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSeed)
503# define XXH3_128bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecret)
504# define XXH3_128bits_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_withSecretandSeed)
505# define XXH3_128bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset)
506# define XXH3_128bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSeed)
507# define XXH3_128bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecret)
508# define XXH3_128bits_reset_withSecretandSeed XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_reset_withSecretandSeed)
509# define XXH3_128bits_update XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_update)
510# define XXH3_128bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3_128bits_digest)
511# define XXH128_isEqual XXH_NAME2(XXH_NAMESPACE, XXH128_isEqual)
512# define XXH128_cmp XXH_NAME2(XXH_NAMESPACE, XXH128_cmp)
513# define XXH128_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH128_canonicalFromHash)
514# define XXH128_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH128_hashFromCanonical)
515#endif
516
517
518/* *************************************
519* Compiler specifics
520***************************************/
521
522/* specific declaration modes for Windows */
523#if !defined(XXH_INLINE_ALL) && !defined(XXH_PRIVATE_API)
524# if defined(_WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
525# ifdef XXH_EXPORT
526# define XXH_PUBLIC_API __declspec(dllexport)
527# elif XXH_IMPORT
528# define XXH_PUBLIC_API __declspec(dllimport)
529# endif
530# else
531# define XXH_PUBLIC_API /* do nothing */
532# endif
533#endif
534
535#if defined (__GNUC__)
536# define XXH_CONSTF __attribute__((__const__))
537# define XXH_PUREF __attribute__((__pure__))
538# define XXH_MALLOCF __attribute__((__malloc__))
539#else
540# define XXH_CONSTF /* disable */
541# define XXH_PUREF
542# define XXH_MALLOCF
543#endif
544
545/* *************************************
546* Version
547***************************************/
548#define XXH_VERSION_MAJOR 0
549#define XXH_VERSION_MINOR 8
550#define XXH_VERSION_RELEASE 3
552#define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
553
562XXH_PUBLIC_API XXH_CONSTF unsigned XXH_versionNumber (void);
563
564
565/* ****************************
566* Common basic types
567******************************/
568#include <stddef.h> /* size_t */
572typedef enum {
573 XXH_OK = 0,
576
577
578/*-**********************************************************************
579* 32-bit hash
580************************************************************************/
581#if defined(XXH_DOXYGEN) /* Don't show <stdint.h> include */
587typedef uint32_t XXH32_hash_t;
588
589#elif !defined (__VMS) \
590 && (defined (__cplusplus) \
591 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
592# ifdef _AIX
593# include <inttypes.h>
594# else
595# include <stdint.h>
596# endif
597 typedef uint32_t XXH32_hash_t;
598
599#else
600# include <limits.h>
601# if UINT_MAX == 0xFFFFFFFFUL
602 typedef unsigned int XXH32_hash_t;
603# elif ULONG_MAX == 0xFFFFFFFFUL
604 typedef unsigned long XXH32_hash_t;
605# else
606# error "unsupported platform: need a 32-bit type"
607# endif
608#endif
609
626
643XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32 (const void* input, size_t length, XXH32_hash_t seed);
644
645#ifndef XXH_NO_STREAM
654
687XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
688
706
728XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
729
746XXH_PUBLIC_API XXH_PUREF XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
747#endif /* !XXH_NO_STREAM */
748
749/******* Canonical representation *******/
750
754typedef struct {
755 unsigned char digest[4];
757
770
784
785
787#ifdef __has_attribute
788# define XXH_HAS_ATTRIBUTE(x) __has_attribute(x)
789#else
790# define XXH_HAS_ATTRIBUTE(x) 0
791#endif
793
795/*
796 * C23 __STDC_VERSION__ number hasn't been specified yet. For now
797 * leave as `201711L` (C17 + 1).
798 * TODO: Update to correct value when its been specified.
799 */
800#define XXH_C23_VN 201711L
802
804/* C-language Attributes are added in C23. */
805#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= XXH_C23_VN) && defined(__has_c_attribute)
806# define XXH_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
807#else
808# define XXH_HAS_C_ATTRIBUTE(x) 0
809#endif
811
813#if defined(__cplusplus) && defined(__has_cpp_attribute)
814# define XXH_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
815#else
816# define XXH_HAS_CPP_ATTRIBUTE(x) 0
817#endif
819
821/*
822 * Define XXH_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute
823 * introduced in CPP17 and C23.
824 * CPP17 : https://en.cppreference.com/w/cpp/language/attributes/fallthrough
825 * C23 : https://en.cppreference.com/w/c/language/attributes/fallthrough
826 */
827#if XXH_HAS_C_ATTRIBUTE(fallthrough) || XXH_HAS_CPP_ATTRIBUTE(fallthrough)
828# define XXH_FALLTHROUGH [[fallthrough]]
829#elif XXH_HAS_ATTRIBUTE(__fallthrough__)
830# define XXH_FALLTHROUGH __attribute__ ((__fallthrough__))
831#else
832# define XXH_FALLTHROUGH /* fallthrough */
833#endif
835
837/*
838 * Define XXH_NOESCAPE for annotated pointers in public API.
839 * https://clang.llvm.org/docs/AttributeReference.html#noescape
840 * As of writing this, only supported by clang.
841 */
842#if XXH_HAS_ATTRIBUTE(noescape)
843# define XXH_NOESCAPE __attribute__((__noescape__))
844#else
845# define XXH_NOESCAPE
846#endif
848
849
855
856#ifndef XXH_NO_LONG_LONG
857/*-**********************************************************************
858* 64-bit hash
859************************************************************************/
860#if defined(XXH_DOXYGEN) /* don't include <stdint.h> */
866typedef uint64_t XXH64_hash_t;
867#elif !defined (__VMS) \
868 && (defined (__cplusplus) \
869 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
870# ifdef _AIX
871# include <inttypes.h>
872# else
873# include <stdint.h>
874# endif
875 typedef uint64_t XXH64_hash_t;
876#else
877# include <limits.h>
878# if defined(__LP64__) && ULONG_MAX == 0xFFFFFFFFFFFFFFFFULL
879 /* LP64 ABI says uint64_t is unsigned long */
880 typedef unsigned long XXH64_hash_t;
881# else
882 /* the following type must have a width of 64-bit */
883 typedef unsigned long long XXH64_hash_t;
884# endif
885#endif
886
900
917XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64(XXH_NOESCAPE const void* input, size_t length, XXH64_hash_t seed);
918
919/******* Streaming *******/
920#ifndef XXH_NO_STREAM
927typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
928
940
953
962XXH_PUBLIC_API void XXH64_copyState(XXH_NOESCAPE XXH64_state_t* dst_state, const XXH64_state_t* src_state);
963
981
1003XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH_NOESCAPE XXH64_state_t* statePtr, XXH_NOESCAPE const void* input, size_t length);
1004
1021XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64_digest (XXH_NOESCAPE const XXH64_state_t* statePtr);
1022#endif /* !XXH_NO_STREAM */
1023/******* Canonical representation *******/
1024
1028typedef struct { unsigned char digest[sizeof(XXH64_hash_t)]; } XXH64_canonical_t;
1029
1042
1055XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH64_hashFromCanonical(XXH_NOESCAPE const XXH64_canonical_t* src);
1056
1057#ifndef XXH_NO_XXH3
1058
1113
1120# define XXH_SCALAR 0
1121# define XXH_SSE2 1
1122# define XXH_AVX2 2
1123# define XXH_AVX512 3
1124# define XXH_NEON 4
1125# define XXH_VSX 5
1126# define XXH_SVE 6
1127# define XXH_LSX 7
1128# define XXH_LASX 8
1129
1130
1131/*-**********************************************************************
1132* XXH3 64-bit variant
1133************************************************************************/
1134
1157XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits(XXH_NOESCAPE const void* input, size_t length);
1158
1183XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_withSeed(XXH_NOESCAPE const void* input, size_t length, XXH64_hash_t seed);
1184
1192#define XXH3_SECRET_SIZE_MIN 136
1193
1226XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_withSecret(XXH_NOESCAPE const void* data, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize);
1227
1228
1229/******* Streaming *******/
1230#ifndef XXH_NO_STREAM
1231/*
1232 * Streaming requires state maintenance.
1233 * This operation costs memory and CPU.
1234 * As a consequence, streaming is slower than one-shot hashing.
1235 * For better performance, prefer one-shot functions whenever applicable.
1236 */
1237
1247
1256XXH_PUBLIC_API void XXH3_copyState(XXH_NOESCAPE XXH3_state_t* dst_state, XXH_NOESCAPE const XXH3_state_t* src_state);
1257
1278
1300
1325XXH_PUBLIC_API XXH_errorcode XXH3_64bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize);
1326
1348XXH_PUBLIC_API XXH_errorcode XXH3_64bits_update (XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* input, size_t length);
1349
1366XXH_PUBLIC_API XXH_PUREF XXH64_hash_t XXH3_64bits_digest (XXH_NOESCAPE const XXH3_state_t* statePtr);
1367#endif /* !XXH_NO_STREAM */
1368
1369/* note : canonical representation of XXH3 is the same as XXH64
1370 * since they both produce XXH64_hash_t values */
1371
1372
1373/*-**********************************************************************
1374* XXH3 128-bit variant
1375************************************************************************/
1376
1387
1406XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits(XXH_NOESCAPE const void* data, size_t len);
1426XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_withSeed(XXH_NOESCAPE const void* data, size_t len, XXH64_hash_t seed);
1454XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_withSecret(XXH_NOESCAPE const void* data, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize);
1455
1456/******* Streaming *******/
1457#ifndef XXH_NO_STREAM
1458/*
1459 * Streaming requires state maintenance.
1460 * This operation costs memory and CPU.
1461 * As a consequence, streaming is slower than one-shot hashing.
1462 * For better performance, prefer one-shot functions whenever applicable.
1463 *
1464 * XXH3_128bits uses the same XXH3_state_t as XXH3_64bits().
1465 * Use already declared XXH3_createState() and XXH3_freeState().
1466 *
1467 * All reset and streaming functions have same meaning as their 64-bit counterpart.
1468 */
1469
1489
1532XXH_PUBLIC_API XXH_errorcode XXH3_128bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize);
1533
1555XXH_PUBLIC_API XXH_errorcode XXH3_128bits_update (XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* input, size_t length);
1556
1572XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH3_128bits_digest (XXH_NOESCAPE const XXH3_state_t* statePtr);
1573#endif /* !XXH_NO_STREAM */
1574
1575/* Following helper functions make it possible to compare XXH128_hast_t values.
1576 * Since XXH128_hash_t is a structure, this capability is not offered by the language.
1577 * Note: For better performance, these functions can be inlined using XXH_INLINE_ALL */
1578
1589
1602XXH_PUBLIC_API XXH_PUREF int XXH128_cmp(XXH_NOESCAPE const void* h128_1, XXH_NOESCAPE const void* h128_2);
1603
1604
1605/******* Canonical representation *******/
1606typedef struct { unsigned char digest[sizeof(XXH128_hash_t)]; } XXH128_canonical_t;
1607
1608
1620
1633
1634
1635#endif /* !XXH_NO_XXH3 */
1636#endif /* XXH_NO_LONG_LONG */
1637
1641#endif /* XXHASH_H_5627135585666179 */
1642
1643
1644
1645#if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742)
1646#define XXHASH_H_STATIC_13879238742
1647/* ****************************************************************************
1648 * This section contains declarations which are not guaranteed to remain stable.
1649 * They may change in future versions, becoming incompatible with a different
1650 * version of the library.
1651 * These declarations should only be used with static linking.
1652 * Never use them in association with dynamic linking!
1653 ***************************************************************************** */
1654
1655/*
1656 * These definitions are only present to allow static allocation
1657 * of XXH states, on stack or in a struct, for example.
1658 * Never **ever** access their members directly.
1659 */
1660
1681
1682
1683#ifndef XXH_NO_LONG_LONG /* defined when there is no 64-bit support */
1684
1705
1706#ifndef XXH_NO_XXH3
1707
1708#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* >= C11 */
1709# define XXH_ALIGN(n) _Alignas(n)
1710#elif defined(__cplusplus) && (__cplusplus >= 201103L) /* >= C++11 */
1711/* In C++ alignas() is a keyword */
1712# define XXH_ALIGN(n) alignas(n)
1713#elif defined(__GNUC__)
1714# define XXH_ALIGN(n) __attribute__ ((aligned(n)))
1715#elif defined(_MSC_VER)
1716# define XXH_ALIGN(n) __declspec(align(n))
1717#else
1718# define XXH_ALIGN(n) /* disabled */
1719#endif
1720
1721/* Old GCC versions only accept the attribute after the type in structures. */
1722#if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) /* C11+ */ \
1723 && ! (defined(__cplusplus) && (__cplusplus >= 201103L)) /* >= C++11 */ \
1724 && defined(__GNUC__)
1725# define XXH_ALIGN_MEMBER(align, type) type XXH_ALIGN(align)
1726#else
1727# define XXH_ALIGN_MEMBER(align, type) XXH_ALIGN(align) type
1728#endif
1729
1738#define XXH3_INTERNALBUFFER_SIZE 256
1739
1749#define XXH3_SECRET_DEFAULT_SIZE 192
1750
1774 XXH_ALIGN_MEMBER(64, XXH64_hash_t acc[8]);
1776 XXH_ALIGN_MEMBER(64, unsigned char customSecret[XXH3_SECRET_DEFAULT_SIZE]);
1778 XXH_ALIGN_MEMBER(64, unsigned char buffer[XXH3_INTERNALBUFFER_SIZE]);
1796 const unsigned char* extSecret;
1799 /* note: there may be some padding at the end due to alignment on 64 bytes */
1800}; /* typedef'd to XXH3_state_t */
1801
1802#undef XXH_ALIGN_MEMBER
1803
1815#define XXH3_INITSTATE(XXH3_state_ptr) \
1816 do { \
1817 XXH3_state_t* tmp_xxh3_state_ptr = (XXH3_state_ptr); \
1818 tmp_xxh3_state_ptr->seed = 0; \
1819 tmp_xxh3_state_ptr->extSecret = NULL; \
1820 } while(0)
1821
1822
1839XXH_PUBLIC_API XXH_PUREF XXH128_hash_t XXH128(XXH_NOESCAPE const void* data, size_t len, XXH64_hash_t seed);
1840
1841
1842/* === Experimental API === */
1843/* Symbols defined below must be considered tied to a specific library version. */
1844
1903XXH_PUBLIC_API XXH_errorcode XXH3_generateSecret(XXH_NOESCAPE void* secretBuffer, size_t secretSize, XXH_NOESCAPE const void* customSeed, size_t customSeedSize);
1904
1943XXH_PUBLIC_API void XXH3_generateSecret_fromSeed(XXH_NOESCAPE void* secretBuffer, XXH64_hash_t seed);
1944
1948#define XXH3_MIDSIZE_MAX 240
1949
1985XXH3_64bits_withSecretandSeed(XXH_NOESCAPE const void* data, size_t len,
1986 XXH_NOESCAPE const void* secret, size_t secretSize,
1987 XXH64_hash_t seed);
1988
2004XXH3_128bits_withSecretandSeed(XXH_NOESCAPE const void* input, size_t length,
2005 XXH_NOESCAPE const void* secret, size_t secretSize,
2006 XXH64_hash_t seed64);
2007
2008#ifndef XXH_NO_STREAM
2024 XXH_NOESCAPE const void* secret, size_t secretSize,
2025 XXH64_hash_t seed64);
2026
2050 XXH_NOESCAPE const void* secret, size_t secretSize,
2051 XXH64_hash_t seed64);
2052
2053#endif /* !XXH_NO_STREAM */
2054
2055#endif /* !XXH_NO_XXH3 */
2056#endif /* XXH_NO_LONG_LONG */
2057#if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
2058# define XXH_IMPLEMENTATION
2059#endif
2060
2061#endif /* defined(XXH_STATIC_LINKING_ONLY) && !defined(XXHASH_H_STATIC_13879238742) */
2062
2063
2064/* ======================================================================== */
2065/* ======================================================================== */
2066/* ======================================================================== */
2067
2068
2069/*-**********************************************************************
2070 * xxHash implementation
2071 *-**********************************************************************
2072 * xxHash's implementation used to be hosted inside xxhash.c.
2073 *
2074 * However, inlining requires implementation to be visible to the compiler,
2075 * hence be included alongside the header.
2076 * Previously, implementation was hosted inside xxhash.c,
2077 * which was then #included when inlining was activated.
2078 * This construction created issues with a few build and install systems,
2079 * as it required xxhash.c to be stored in /include directory.
2080 *
2081 * xxHash implementation is now directly integrated within xxhash.h.
2082 * As a consequence, xxhash.c is no longer needed in /include.
2083 *
2084 * xxhash.c is still available and is still useful.
2085 * In a "normal" setup, when xxhash is not inlined,
2086 * xxhash.h only exposes the prototypes and public symbols,
2087 * while xxhash.c can be built into an object file xxhash.o
2088 * which can then be linked into the final binary.
2089 ************************************************************************/
2090
2091#if ( defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API) \
2092 || defined(XXH_IMPLEMENTATION) ) && !defined(XXH_IMPLEM_13a8737387)
2093# define XXH_IMPLEM_13a8737387
2094
2095/* *************************************
2096* Tuning parameters
2097***************************************/
2098
2105#ifdef XXH_DOXYGEN
2111# define XXH_NO_LONG_LONG
2112# undef XXH_NO_LONG_LONG /* don't actually */
2163# define XXH_FORCE_MEMORY_ACCESS 0
2164
2191# define XXH_SIZE_OPT 0
2192
2221# define XXH_FORCE_ALIGN_CHECK 0
2222
2243# define XXH_NO_INLINE_HINTS 0
2244
2260# define XXH3_INLINE_SECRET 0
2261
2272# define XXH32_ENDJMP 0
2273
2281# define XXH_OLD_NAMES
2282# undef XXH_OLD_NAMES /* don't actually use, it is ugly. */
2283
2292# define XXH_NO_STREAM
2293# undef XXH_NO_STREAM /* don't actually */
2294#endif /* XXH_DOXYGEN */
2298
2299#ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
2300 /* prefer __packed__ structures (method 1) for GCC
2301 * < ARMv7 with unaligned access (e.g. Raspbian armhf) still uses byte shifting, so we use memcpy
2302 * which for some reason does unaligned loads. */
2303# if defined(__GNUC__) && !(defined(__ARM_ARCH) && __ARM_ARCH < 7 && defined(__ARM_FEATURE_UNALIGNED))
2304# define XXH_FORCE_MEMORY_ACCESS 1
2305# endif
2306#endif
2307
2308#ifndef XXH_SIZE_OPT
2309 /* default to 1 for -Os or -Oz */
2310# if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE_SIZE__)
2311# define XXH_SIZE_OPT 1
2312# else
2313# define XXH_SIZE_OPT 0
2314# endif
2315#endif
2316
2317#ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
2318 /* don't check on sizeopt, x86, aarch64, or arm when unaligned access is available */
2319# if XXH_SIZE_OPT >= 1 || \
2320 defined(__i386) || defined(__x86_64__) || defined(__aarch64__) || defined(__ARM_FEATURE_UNALIGNED) \
2321 || defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64) || defined(_M_ARM) /* visual */
2322# define XXH_FORCE_ALIGN_CHECK 0
2323# else
2324# define XXH_FORCE_ALIGN_CHECK 1
2325# endif
2326#endif
2327
2328#ifndef XXH_NO_INLINE_HINTS
2329# if XXH_SIZE_OPT >= 1 || defined(__NO_INLINE__) /* -O0, -fno-inline */
2330# define XXH_NO_INLINE_HINTS 1
2331# else
2332# define XXH_NO_INLINE_HINTS 0
2333# endif
2334#endif
2335
2336#ifndef XXH3_INLINE_SECRET
2337# if (defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12) \
2338 || !defined(XXH_INLINE_ALL)
2339# define XXH3_INLINE_SECRET 0
2340# else
2341# define XXH3_INLINE_SECRET 1
2342# endif
2343#endif
2344
2345#ifndef XXH32_ENDJMP
2346/* generally preferable for performance */
2347# define XXH32_ENDJMP 0
2348#endif
2349
2354
2355
2356/* *************************************
2357* Includes & Memory related functions
2358***************************************/
2359#if defined(XXH_NO_STREAM)
2360/* nothing */
2361#elif defined(XXH_NO_STDLIB)
2362
2363/* When requesting to disable any mention of stdlib,
2364 * the library loses the ability to invoked malloc / free.
2365 * In practice, it means that functions like `XXH*_createState()`
2366 * will always fail, and return NULL.
2367 * This flag is useful in situations where
2368 * xxhash.h is integrated into some kernel, embedded or limited environment
2369 * without access to dynamic allocation.
2370 */
2371
2372static XXH_CONSTF void* XXH_malloc(size_t s) { (void)s; return NULL; }
2373static void XXH_free(void* p) { (void)p; }
2374
2375#else
2376
2377/*
2378 * Modify the local functions below should you wish to use
2379 * different memory routines for malloc() and free()
2380 */
2381#include <stdlib.h>
2382
2387static XXH_MALLOCF void* XXH_malloc(size_t s) { return malloc(s); }
2388
2393static void XXH_free(void* p) { free(p); }
2394
2395#endif /* XXH_NO_STDLIB */
2396
2397#include <string.h>
2398
2403static void* XXH_memcpy(void* dest, const void* src, size_t size)
2404{
2405 return memcpy(dest,src,size);
2406}
2407
2408#include <limits.h> /* ULLONG_MAX */
2409
2410
2411/* *************************************
2412* Compiler Specific Options
2413***************************************/
2414#ifdef _MSC_VER /* Visual Studio warning fix */
2415# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
2416#endif
2417
2418#if XXH_NO_INLINE_HINTS /* disable inlining hints */
2419# if defined(__GNUC__) || defined(__clang__)
2420# define XXH_FORCE_INLINE static __attribute__((__unused__))
2421# else
2422# define XXH_FORCE_INLINE static
2423# endif
2424# define XXH_NO_INLINE static
2425/* enable inlining hints */
2426#elif defined(__GNUC__) || defined(__clang__)
2427# define XXH_FORCE_INLINE static __inline__ __attribute__((__always_inline__, __unused__))
2428# define XXH_NO_INLINE static __attribute__((__noinline__))
2429#elif defined(_MSC_VER) /* Visual Studio */
2430# define XXH_FORCE_INLINE static __forceinline
2431# define XXH_NO_INLINE static __declspec(noinline)
2432#elif defined (__cplusplus) \
2433 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* C99 */
2434# define XXH_FORCE_INLINE static inline
2435# define XXH_NO_INLINE static
2436#else
2437# define XXH_FORCE_INLINE static
2438# define XXH_NO_INLINE static
2439#endif
2440
2441#if defined(XXH_INLINE_ALL)
2442# define XXH_STATIC XXH_FORCE_INLINE
2443#else
2444# define XXH_STATIC static
2445#endif
2446
2447#if XXH3_INLINE_SECRET
2448# define XXH3_WITH_SECRET_INLINE XXH_FORCE_INLINE
2449#else
2450# define XXH3_WITH_SECRET_INLINE XXH_NO_INLINE
2451#endif
2452
2453#if ((defined(sun) || defined(__sun)) && __cplusplus) /* Solaris includes __STDC_VERSION__ with C++. Tested with GCC 5.5 */
2454# define XXH_RESTRICT /* disable */
2455#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* >= C99 */
2456# define XXH_RESTRICT restrict
2457#elif (defined (__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))) \
2458 || (defined (__clang__)) \
2459 || (defined (_MSC_VER) && (_MSC_VER >= 1400)) \
2460 || (defined (__INTEL_COMPILER) && (__INTEL_COMPILER >= 1300))
2461/*
2462 * There are a LOT more compilers that recognize __restrict but this
2463 * covers the major ones.
2464 */
2465# define XXH_RESTRICT __restrict
2466#else
2467# define XXH_RESTRICT /* disable */
2468#endif
2469
2470/* *************************************
2471* Debug
2472***************************************/
2481#ifndef XXH_DEBUGLEVEL
2482# ifdef DEBUGLEVEL /* backwards compat */
2483# define XXH_DEBUGLEVEL DEBUGLEVEL
2484# else
2485# define XXH_DEBUGLEVEL 0
2486# endif
2487#endif
2488
2489#if (XXH_DEBUGLEVEL>=1)
2490# include <assert.h> /* note: can still be disabled with NDEBUG */
2491# define XXH_ASSERT(c) assert(c)
2492#else
2493# if defined(__INTEL_COMPILER)
2494# define XXH_ASSERT(c) XXH_ASSUME((unsigned char) (c))
2495# else
2496# define XXH_ASSERT(c) XXH_ASSUME(c)
2497# endif
2498#endif
2499
2500/* note: use after variable declarations */
2501#ifndef XXH_STATIC_ASSERT
2502# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */
2503# define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { _Static_assert((c),m); } while(0)
2504# elif defined(__cplusplus) && (__cplusplus >= 201103L) /* C++11 */
2505# define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { static_assert((c),m); } while(0)
2506# else
2507# define XXH_STATIC_ASSERT_WITH_MESSAGE(c,m) do { struct xxh_sa { char x[(c) ? 1 : -1]; }; } while(0)
2508# endif
2509# define XXH_STATIC_ASSERT(c) XXH_STATIC_ASSERT_WITH_MESSAGE((c),#c)
2510#endif
2511
2528#if defined(__GNUC__) || defined(__clang__)
2529# define XXH_COMPILER_GUARD(var) __asm__("" : "+r" (var))
2530#else
2531# define XXH_COMPILER_GUARD(var) ((void)0)
2532#endif
2533
2534/* Specifically for NEON vectors which use the "w" constraint, on
2535 * Clang. */
2536#if defined(__clang__) && defined(__ARM_ARCH) && !defined(__wasm__)
2537# define XXH_COMPILER_GUARD_CLANG_NEON(var) __asm__("" : "+w" (var))
2538#else
2539# define XXH_COMPILER_GUARD_CLANG_NEON(var) ((void)0)
2540#endif
2541
2542/* *************************************
2543* Basic Types
2544***************************************/
2545#if !defined (__VMS) \
2546 && (defined (__cplusplus) \
2547 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
2548# ifdef _AIX
2549# include <inttypes.h>
2550# else
2551# include <stdint.h>
2552# endif
2553 typedef uint8_t xxh_u8;
2554#else
2555 typedef unsigned char xxh_u8;
2556#endif
2557typedef XXH32_hash_t xxh_u32;
2558
2559#ifdef XXH_OLD_NAMES
2560# warning "XXH_OLD_NAMES is planned to be removed starting v0.9. If the program depends on it, consider moving away from it by employing newer type names directly"
2561# define BYTE xxh_u8
2562# define U8 xxh_u8
2563# define U32 xxh_u32
2564#endif
2565
2566/* *** Memory access *** */
2567
2578
2589
2600
2617
2618#if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
2619/*
2620 * Manual byteshift. Best for old compilers which don't inline memcpy.
2621 * We actually directly use XXH_readLE32 and XXH_readBE32.
2622 */
2623#elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
2624
2625/*
2626 * Force direct memory access. Only works on CPU which support unaligned memory
2627 * access in hardware.
2628 */
2629static xxh_u32 XXH_read32(const void* memPtr) { return *(const xxh_u32*) memPtr; }
2630
2631#elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
2632
2633/*
2634 * __attribute__((aligned(1))) is supported by gcc and clang. Originally the
2635 * documentation claimed that it only increased the alignment, but actually it
2636 * can decrease it on gcc, clang, and icc:
2637 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502,
2638 * https://gcc.godbolt.org/z/xYez1j67Y.
2639 */
2640#ifdef XXH_OLD_NAMES
2641typedef union { xxh_u32 u32; } __attribute__((__packed__)) unalign;
2642#endif
2643static xxh_u32 XXH_read32(const void* ptr)
2644{
2645 typedef __attribute__((__aligned__(1))) xxh_u32 xxh_unalign32;
2646 return *((const xxh_unalign32*)ptr);
2647}
2648
2649#else
2650
2651/*
2652 * Portable and safe solution. Generally efficient.
2653 * see: https://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html
2654 */
2655static xxh_u32 XXH_read32(const void* memPtr)
2656{
2657 xxh_u32 val;
2658 XXH_memcpy(&val, memPtr, sizeof(val));
2659 return val;
2660}
2661
2662#endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
2663
2664
2665/* *** Endianness *** */
2666
2683#ifndef XXH_CPU_LITTLE_ENDIAN
2684/*
2685 * Try to detect endianness automatically, to avoid the nonstandard behavior
2686 * in `XXH_isLittleEndian()`
2687 */
2688# if defined(_WIN32) /* Windows is always little endian */ \
2689 || defined(__LITTLE_ENDIAN__) \
2690 || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
2691# define XXH_CPU_LITTLE_ENDIAN 1
2692# elif defined(__BIG_ENDIAN__) \
2693 || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
2694# define XXH_CPU_LITTLE_ENDIAN 0
2695# else
2702static int XXH_isLittleEndian(void)
2703{
2704 /*
2705 * Portable and well-defined behavior.
2706 * Don't use static: it is detrimental to performance.
2707 */
2708 const union { xxh_u32 u; xxh_u8 c[4]; } one = { 1 };
2709 return one.c[0];
2710}
2711# define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian()
2712# endif
2713#endif
2714
2715
2716
2717
2718/* ****************************************
2719* Compiler-specific Functions and Macros
2720******************************************/
2721#define XXH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
2722
2723#ifdef __has_builtin
2724# define XXH_HAS_BUILTIN(x) __has_builtin(x)
2725#else
2726# define XXH_HAS_BUILTIN(x) 0
2727#endif
2728
2729
2730
2731/*
2732 * C23 and future versions have standard "unreachable()".
2733 * Once it has been implemented reliably we can add it as an
2734 * additional case:
2735 *
2736 * ```
2737 * #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= XXH_C23_VN)
2738 * # include <stddef.h>
2739 * # ifdef unreachable
2740 * # define XXH_UNREACHABLE() unreachable()
2741 * # endif
2742 * #endif
2743 * ```
2744 *
2745 * Note C++23 also has std::unreachable() which can be detected
2746 * as follows:
2747 * ```
2748 * #if defined(__cpp_lib_unreachable) && (__cpp_lib_unreachable >= 202202L)
2749 * # include <utility>
2750 * # define XXH_UNREACHABLE() std::unreachable()
2751 * #endif
2752 * ```
2753 * NB: `__cpp_lib_unreachable` is defined in the `<version>` header.
2754 * We don't use that as including `<utility>` in `extern "C"` blocks
2755 * doesn't work on GCC12
2756 */
2757
2758#if XXH_HAS_BUILTIN(__builtin_unreachable)
2759# define XXH_UNREACHABLE() __builtin_unreachable()
2760
2761#elif defined(_MSC_VER)
2762# define XXH_UNREACHABLE() __assume(0)
2763
2764#else
2765# define XXH_UNREACHABLE()
2766#endif
2767
2768#if XXH_HAS_BUILTIN(__builtin_assume)
2769# define XXH_ASSUME(c) __builtin_assume(c)
2770#else
2771# define XXH_ASSUME(c) if (!(c)) { XXH_UNREACHABLE(); }
2772#endif
2773
2787#if !defined(NO_CLANG_BUILTIN) && XXH_HAS_BUILTIN(__builtin_rotateleft32) \
2788 && XXH_HAS_BUILTIN(__builtin_rotateleft64)
2789# define XXH_rotl32 __builtin_rotateleft32
2790# define XXH_rotl64 __builtin_rotateleft64
2791#elif XXH_HAS_BUILTIN(__builtin_stdc_rotate_left)
2792# define XXH_rotl32 __builtin_stdc_rotate_left
2793# define XXH_rotl64 __builtin_stdc_rotate_left
2794/* Note: although _rotl exists for minGW (GCC under windows), performance seems poor */
2795#elif defined(_MSC_VER)
2796# define XXH_rotl32(x,r) _rotl(x,r)
2797# define XXH_rotl64(x,r) _rotl64(x,r)
2798#else
2799# define XXH_rotl32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
2800# define XXH_rotl64(x,r) (((x) << (r)) | ((x) >> (64 - (r))))
2801#endif
2802
2811#if defined(_MSC_VER) /* Visual Studio */
2812# define XXH_swap32 _byteswap_ulong
2813#elif XXH_GCC_VERSION >= 403
2814# define XXH_swap32 __builtin_bswap32
2815#else
2816static xxh_u32 XXH_swap32 (xxh_u32 x)
2817{
2818 return ((x << 24) & 0xff000000 ) |
2819 ((x << 8) & 0x00ff0000 ) |
2820 ((x >> 8) & 0x0000ff00 ) |
2821 ((x >> 24) & 0x000000ff );
2822}
2823#endif
2824
2825
2826/* ***************************
2827* Memory reads
2828*****************************/
2829
2838
2839/*
2840 * XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load.
2841 *
2842 * This is ideal for older compilers which don't inline memcpy.
2843 */
2844#if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
2845
2846XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* memPtr)
2847{
2848 const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
2849 return bytePtr[0]
2850 | ((xxh_u32)bytePtr[1] << 8)
2851 | ((xxh_u32)bytePtr[2] << 16)
2852 | ((xxh_u32)bytePtr[3] << 24);
2853}
2854
2855XXH_FORCE_INLINE xxh_u32 XXH_readBE32(const void* memPtr)
2856{
2857 const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
2858 return bytePtr[3]
2859 | ((xxh_u32)bytePtr[2] << 8)
2860 | ((xxh_u32)bytePtr[1] << 16)
2861 | ((xxh_u32)bytePtr[0] << 24);
2862}
2863
2864#else
2865XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* ptr)
2866{
2867 return XXH_CPU_LITTLE_ENDIAN ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
2868}
2869
2870static xxh_u32 XXH_readBE32(const void* ptr)
2871{
2872 return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
2873}
2874#endif
2875
2876XXH_FORCE_INLINE xxh_u32
2877XXH_readLE32_align(const void* ptr, XXH_alignment align)
2878{
2879 if (align==XXH_unaligned) {
2880 return XXH_readLE32(ptr);
2881 } else {
2882 return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u32*)ptr : XXH_swap32(*(const xxh_u32*)ptr);
2883 }
2884}
2885
2886
2887/* *************************************
2888* Misc
2889***************************************/
2892
2893
2894/* *******************************************************************
2895* 32-bit hash functions
2896*********************************************************************/
2905 /* #define instead of static const, to be used as initializers */
2906#define XXH_PRIME32_1 0x9E3779B1U
2907#define XXH_PRIME32_2 0x85EBCA77U
2908#define XXH_PRIME32_3 0xC2B2AE3DU
2909#define XXH_PRIME32_4 0x27D4EB2FU
2910#define XXH_PRIME32_5 0x165667B1U
2911
2912#ifdef XXH_OLD_NAMES
2913# define PRIME32_1 XXH_PRIME32_1
2914# define PRIME32_2 XXH_PRIME32_2
2915# define PRIME32_3 XXH_PRIME32_3
2916# define PRIME32_4 XXH_PRIME32_4
2917# define PRIME32_5 XXH_PRIME32_5
2918#endif
2919
2931static xxh_u32 XXH32_round(xxh_u32 acc, xxh_u32 input)
2932{
2933 acc += input * XXH_PRIME32_2;
2934 acc = XXH_rotl32(acc, 13);
2935 acc *= XXH_PRIME32_1;
2936#if (defined(__SSE4_1__) || defined(__aarch64__) || defined(__wasm_simd128__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
2937 /*
2938 * UGLY HACK:
2939 * A compiler fence is used to prevent GCC and Clang from
2940 * autovectorizing the XXH32 loop (pragmas and attributes don't work for some
2941 * reason) without globally disabling SSE4.1.
2942 *
2943 * The reason we want to avoid vectorization is because despite working on
2944 * 4 integers at a time, there are multiple factors slowing XXH32 down on
2945 * SSE4:
2946 * - There's a ridiculous amount of lag from pmulld (10 cycles of latency on
2947 * newer chips!) making it slightly slower to multiply four integers at
2948 * once compared to four integers independently. Even when pmulld was
2949 * fastest, Sandy/Ivy Bridge, it is still not worth it to go into SSE
2950 * just to multiply unless doing a long operation.
2951 *
2952 * - Four instructions are required to rotate,
2953 * movqda tmp, v // not required with VEX encoding
2954 * pslld tmp, 13 // tmp <<= 13
2955 * psrld v, 19 // x >>= 19
2956 * por v, tmp // x |= tmp
2957 * compared to one for scalar:
2958 * roll v, 13 // reliably fast across the board
2959 * shldl v, v, 13 // Sandy Bridge and later prefer this for some reason
2960 *
2961 * - Instruction level parallelism is actually more beneficial here because
2962 * the SIMD actually serializes this operation: While v1 is rotating, v2
2963 * can load data, while v3 can multiply. SSE forces them to operate
2964 * together.
2965 *
2966 * This is also enabled on AArch64, as Clang is *very aggressive* in vectorizing
2967 * the loop. NEON is only faster on the A53, and with the newer cores, it is less
2968 * than half the speed.
2969 *
2970 * Additionally, this is used on WASM SIMD128 because it JITs to the same
2971 * SIMD instructions and has the same issue.
2972 */
2973 XXH_COMPILER_GUARD(acc);
2974#endif
2975 return acc;
2976}
2977
2988static xxh_u32 XXH32_avalanche(xxh_u32 hash)
2989{
2990 hash ^= hash >> 15;
2991 hash *= XXH_PRIME32_2;
2992 hash ^= hash >> 13;
2993 hash *= XXH_PRIME32_3;
2994 hash ^= hash >> 16;
2995 return hash;
2996}
2997
2998#define XXH_get32bits(p) XXH_readLE32_align(p, align)
2999
3004XXH_FORCE_INLINE void
3005XXH32_initAccs(xxh_u32 *acc, xxh_u32 seed)
3006{
3007 XXH_ASSERT(acc != NULL);
3008 acc[0] = seed + XXH_PRIME32_1 + XXH_PRIME32_2;
3009 acc[1] = seed + XXH_PRIME32_2;
3010 acc[2] = seed + 0;
3011 acc[3] = seed - XXH_PRIME32_1;
3012}
3013
3020XXH_FORCE_INLINE const xxh_u8 *
3021XXH32_consumeLong(
3022 xxh_u32 *XXH_RESTRICT acc,
3023 xxh_u8 const *XXH_RESTRICT input,
3024 size_t len,
3025 XXH_alignment align
3026)
3027{
3028 const xxh_u8* const bEnd = input + len;
3029 const xxh_u8* const limit = bEnd - 15;
3030 XXH_ASSERT(acc != NULL);
3031 XXH_ASSERT(input != NULL);
3032 XXH_ASSERT(len >= 16);
3033 do {
3034 acc[0] = XXH32_round(acc[0], XXH_get32bits(input)); input += 4;
3035 acc[1] = XXH32_round(acc[1], XXH_get32bits(input)); input += 4;
3036 acc[2] = XXH32_round(acc[2], XXH_get32bits(input)); input += 4;
3037 acc[3] = XXH32_round(acc[3], XXH_get32bits(input)); input += 4;
3038 } while (input < limit);
3039
3040 return input;
3041}
3042
3047XXH_FORCE_INLINE XXH_PUREF xxh_u32
3048XXH32_mergeAccs(const xxh_u32 *acc)
3049{
3050 XXH_ASSERT(acc != NULL);
3051 return XXH_rotl32(acc[0], 1) + XXH_rotl32(acc[1], 7)
3052 + XXH_rotl32(acc[2], 12) + XXH_rotl32(acc[3], 18);
3053}
3054
3070static XXH_PUREF xxh_u32
3071XXH32_finalize(xxh_u32 hash, const xxh_u8* ptr, size_t len, XXH_alignment align)
3072{
3073#define XXH_PROCESS1 do { \
3074 hash += (*ptr++) * XXH_PRIME32_5; \
3075 hash = XXH_rotl32(hash, 11) * XXH_PRIME32_1; \
3076} while (0)
3077
3078#define XXH_PROCESS4 do { \
3079 hash += XXH_get32bits(ptr) * XXH_PRIME32_3; \
3080 ptr += 4; \
3081 hash = XXH_rotl32(hash, 17) * XXH_PRIME32_4; \
3082} while (0)
3083
3084 if (ptr==NULL) XXH_ASSERT(len == 0);
3085
3086 /* Compact rerolled version; generally faster */
3087 if (!XXH32_ENDJMP) {
3088 len &= 15;
3089 while (len >= 4) {
3090 XXH_PROCESS4;
3091 len -= 4;
3092 }
3093 while (len > 0) {
3094 XXH_PROCESS1;
3095 --len;
3096 }
3097 return XXH32_avalanche(hash);
3098 } else {
3099 switch(len&15) /* or switch(bEnd - p) */ {
3100 case 12: XXH_PROCESS4;
3101 XXH_FALLTHROUGH; /* fallthrough */
3102 case 8: XXH_PROCESS4;
3103 XXH_FALLTHROUGH; /* fallthrough */
3104 case 4: XXH_PROCESS4;
3105 return XXH32_avalanche(hash);
3106
3107 case 13: XXH_PROCESS4;
3108 XXH_FALLTHROUGH; /* fallthrough */
3109 case 9: XXH_PROCESS4;
3110 XXH_FALLTHROUGH; /* fallthrough */
3111 case 5: XXH_PROCESS4;
3112 XXH_PROCESS1;
3113 return XXH32_avalanche(hash);
3114
3115 case 14: XXH_PROCESS4;
3116 XXH_FALLTHROUGH; /* fallthrough */
3117 case 10: XXH_PROCESS4;
3118 XXH_FALLTHROUGH; /* fallthrough */
3119 case 6: XXH_PROCESS4;
3120 XXH_PROCESS1;
3121 XXH_PROCESS1;
3122 return XXH32_avalanche(hash);
3123
3124 case 15: XXH_PROCESS4;
3125 XXH_FALLTHROUGH; /* fallthrough */
3126 case 11: XXH_PROCESS4;
3127 XXH_FALLTHROUGH; /* fallthrough */
3128 case 7: XXH_PROCESS4;
3129 XXH_FALLTHROUGH; /* fallthrough */
3130 case 3: XXH_PROCESS1;
3131 XXH_FALLTHROUGH; /* fallthrough */
3132 case 2: XXH_PROCESS1;
3133 XXH_FALLTHROUGH; /* fallthrough */
3134 case 1: XXH_PROCESS1;
3135 XXH_FALLTHROUGH; /* fallthrough */
3136 case 0: return XXH32_avalanche(hash);
3137 }
3138 XXH_ASSERT(0);
3139 return hash; /* reaching this point is deemed impossible */
3140 }
3141}
3142
3143#ifdef XXH_OLD_NAMES
3144# define PROCESS1 XXH_PROCESS1
3145# define PROCESS4 XXH_PROCESS4
3146#else
3147# undef XXH_PROCESS1
3148# undef XXH_PROCESS4
3149#endif
3150
3159XXH_FORCE_INLINE XXH_PUREF xxh_u32
3160XXH32_endian_align(const xxh_u8* input, size_t len, xxh_u32 seed, XXH_alignment align)
3161{
3162 xxh_u32 h32;
3163
3164 if (input==NULL) XXH_ASSERT(len == 0);
3165
3166 if (len>=16) {
3167 xxh_u32 acc[4];
3168 XXH32_initAccs(acc, seed);
3169
3170 input = XXH32_consumeLong(acc, input, len, align);
3171
3172 h32 = XXH32_mergeAccs(acc);
3173 } else {
3174 h32 = seed + XXH_PRIME32_5;
3175 }
3176
3177 h32 += (xxh_u32)len;
3178
3179 return XXH32_finalize(h32, input, len&15, align);
3180}
3181
3183XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t len, XXH32_hash_t seed)
3184{
3185#if !defined(XXH_NO_STREAM) && XXH_SIZE_OPT >= 2
3186 /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
3187 XXH32_state_t state;
3188 XXH32_reset(&state, seed);
3189 XXH32_update(&state, (const xxh_u8*)input, len);
3190 return XXH32_digest(&state);
3191#else
3193 if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
3194 return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
3195 } }
3196
3197 return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
3198#endif
3199}
3200
3201
3202
3203/******* Hash streaming *******/
3204#ifndef XXH_NO_STREAM
3207{
3208 return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
3209}
3210
3212{
3213 XXH_free(statePtr);
3214 return XXH_OK;
3215}
3216
3219{
3220 XXH_memcpy(dstState, srcState, sizeof(*dstState));
3221}
3222
3225{
3226 XXH_ASSERT(statePtr != NULL);
3227 memset(statePtr, 0, sizeof(*statePtr));
3228 XXH32_initAccs(statePtr->acc, seed);
3229 return XXH_OK;
3230}
3231
3232
3235XXH32_update(XXH32_state_t* state, const void* input, size_t len)
3236{
3237 if (input==NULL) {
3238 XXH_ASSERT(len == 0);
3239 return XXH_OK;
3240 }
3241
3242 state->total_len_32 += (XXH32_hash_t)len;
3243 state->large_len |= (XXH32_hash_t)((len>=16) | (state->total_len_32>=16));
3244
3245 XXH_ASSERT(state->bufferedSize < sizeof(state->buffer));
3246 if (len < sizeof(state->buffer) - state->bufferedSize) { /* fill in tmp buffer */
3247 XXH_memcpy(state->buffer + state->bufferedSize, input, len);
3248 state->bufferedSize += (XXH32_hash_t)len;
3249 return XXH_OK;
3250 }
3251
3252 { const xxh_u8* xinput = (const xxh_u8*)input;
3253 const xxh_u8* const bEnd = xinput + len;
3254
3255 if (state->bufferedSize) { /* non-empty buffer: complete first */
3256 XXH_memcpy(state->buffer + state->bufferedSize, xinput, sizeof(state->buffer) - state->bufferedSize);
3257 xinput += sizeof(state->buffer) - state->bufferedSize;
3258 /* then process one round */
3259 (void)XXH32_consumeLong(state->acc, state->buffer, sizeof(state->buffer), XXH_aligned);
3260 state->bufferedSize = 0;
3261 }
3262
3263 XXH_ASSERT(xinput <= bEnd);
3264 if ((size_t)(bEnd - xinput) >= sizeof(state->buffer)) {
3265 /* Process the remaining data */
3266 xinput = XXH32_consumeLong(state->acc, xinput, (size_t)(bEnd - xinput), XXH_unaligned);
3267 }
3268
3269 if (xinput < bEnd) {
3270 /* Copy the leftover to the tmp buffer */
3271 XXH_memcpy(state->buffer, xinput, (size_t)(bEnd-xinput));
3272 state->bufferedSize = (unsigned)(bEnd-xinput);
3273 }
3274 }
3275
3276 return XXH_OK;
3277}
3278
3279
3282{
3283 xxh_u32 h32;
3284
3285 if (state->large_len) {
3286 h32 = XXH32_mergeAccs(state->acc);
3287 } else {
3288 h32 = state->acc[2] /* == seed */ + XXH_PRIME32_5;
3289 }
3290
3291 h32 += state->total_len_32;
3292
3293 return XXH32_finalize(h32, state->buffer, state->bufferedSize, XXH_aligned);
3294}
3295#endif /* !XXH_NO_STREAM */
3296
3297/******* Canonical representation *******/
3298
3301{
3302 XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
3303 if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
3304 XXH_memcpy(dst, &hash, sizeof(*dst));
3305}
3306
3308{
3309 return XXH_readBE32(src);
3310}
3311
3312
3313#ifndef XXH_NO_LONG_LONG
3314
3315/* *******************************************************************
3316* 64-bit hash functions
3317*********************************************************************/
3323/******* Memory access *******/
3324
3325typedef XXH64_hash_t xxh_u64;
3326
3327#ifdef XXH_OLD_NAMES
3328# define U64 xxh_u64
3329#endif
3330
3331#if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
3332/*
3333 * Manual byteshift. Best for old compilers which don't inline memcpy.
3334 * We actually directly use XXH_readLE64 and XXH_readBE64.
3335 */
3336#elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
3337
3338/* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
3339static xxh_u64 XXH_read64(const void* memPtr)
3340{
3341 return *(const xxh_u64*) memPtr;
3342}
3343
3344#elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
3345
3346/*
3347 * __attribute__((aligned(1))) is supported by gcc and clang. Originally the
3348 * documentation claimed that it only increased the alignment, but actually it
3349 * can decrease it on gcc, clang, and icc:
3350 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69502,
3351 * https://gcc.godbolt.org/z/xYez1j67Y.
3352 */
3353#ifdef XXH_OLD_NAMES
3354typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((__packed__)) unalign64;
3355#endif
3356static xxh_u64 XXH_read64(const void* ptr)
3357{
3358 typedef __attribute__((__aligned__(1))) xxh_u64 xxh_unalign64;
3359 return *((const xxh_unalign64*)ptr);
3360}
3361
3362#else
3363
3364/*
3365 * Portable and safe solution. Generally efficient.
3366 * see: https://fastcompression.blogspot.com/2015/08/accessing-unaligned-memory.html
3367 */
3368static xxh_u64 XXH_read64(const void* memPtr)
3369{
3370 xxh_u64 val;
3371 XXH_memcpy(&val, memPtr, sizeof(val));
3372 return val;
3373}
3374
3375#endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
3376
3377#if defined(_MSC_VER) /* Visual Studio */
3378# define XXH_swap64 _byteswap_uint64
3379#elif XXH_GCC_VERSION >= 403
3380# define XXH_swap64 __builtin_bswap64
3381#else
3382static xxh_u64 XXH_swap64(xxh_u64 x)
3383{
3384 return ((x << 56) & 0xff00000000000000ULL) |
3385 ((x << 40) & 0x00ff000000000000ULL) |
3386 ((x << 24) & 0x0000ff0000000000ULL) |
3387 ((x << 8) & 0x000000ff00000000ULL) |
3388 ((x >> 8) & 0x00000000ff000000ULL) |
3389 ((x >> 24) & 0x0000000000ff0000ULL) |
3390 ((x >> 40) & 0x000000000000ff00ULL) |
3391 ((x >> 56) & 0x00000000000000ffULL);
3392}
3393#endif
3394
3395
3396/* XXH_FORCE_MEMORY_ACCESS==3 is an endian-independent byteshift load. */
3397#if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==3))
3398
3399XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* memPtr)
3400{
3401 const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
3402 return bytePtr[0]
3403 | ((xxh_u64)bytePtr[1] << 8)
3404 | ((xxh_u64)bytePtr[2] << 16)
3405 | ((xxh_u64)bytePtr[3] << 24)
3406 | ((xxh_u64)bytePtr[4] << 32)
3407 | ((xxh_u64)bytePtr[5] << 40)
3408 | ((xxh_u64)bytePtr[6] << 48)
3409 | ((xxh_u64)bytePtr[7] << 56);
3410}
3411
3412XXH_FORCE_INLINE xxh_u64 XXH_readBE64(const void* memPtr)
3413{
3414 const xxh_u8* bytePtr = (const xxh_u8 *)memPtr;
3415 return bytePtr[7]
3416 | ((xxh_u64)bytePtr[6] << 8)
3417 | ((xxh_u64)bytePtr[5] << 16)
3418 | ((xxh_u64)bytePtr[4] << 24)
3419 | ((xxh_u64)bytePtr[3] << 32)
3420 | ((xxh_u64)bytePtr[2] << 40)
3421 | ((xxh_u64)bytePtr[1] << 48)
3422 | ((xxh_u64)bytePtr[0] << 56);
3423}
3424
3425#else
3426XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* ptr)
3427{
3428 return XXH_CPU_LITTLE_ENDIAN ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
3429}
3430
3431static xxh_u64 XXH_readBE64(const void* ptr)
3432{
3433 return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
3434}
3435#endif
3436
3437XXH_FORCE_INLINE xxh_u64
3438XXH_readLE64_align(const void* ptr, XXH_alignment align)
3439{
3440 if (align==XXH_unaligned)
3441 return XXH_readLE64(ptr);
3442 else
3443 return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u64*)ptr : XXH_swap64(*(const xxh_u64*)ptr);
3444}
3445
3446
3447/******* xxh64 *******/
3456/* #define rather that static const, to be used as initializers */
3457#define XXH_PRIME64_1 0x9E3779B185EBCA87ULL
3458#define XXH_PRIME64_2 0xC2B2AE3D27D4EB4FULL
3459#define XXH_PRIME64_3 0x165667B19E3779F9ULL
3460#define XXH_PRIME64_4 0x85EBCA77C2B2AE63ULL
3461#define XXH_PRIME64_5 0x27D4EB2F165667C5ULL
3462
3463#ifdef XXH_OLD_NAMES
3464# define PRIME64_1 XXH_PRIME64_1
3465# define PRIME64_2 XXH_PRIME64_2
3466# define PRIME64_3 XXH_PRIME64_3
3467# define PRIME64_4 XXH_PRIME64_4
3468# define PRIME64_5 XXH_PRIME64_5
3469#endif
3470
3472static xxh_u64 XXH64_round(xxh_u64 acc, xxh_u64 input)
3473{
3474 acc += input * XXH_PRIME64_2;
3475 acc = XXH_rotl64(acc, 31);
3476 acc *= XXH_PRIME64_1;
3477#if (defined(__AVX512F__)) && !defined(XXH_ENABLE_AUTOVECTORIZE)
3478 /*
3479 * DISABLE AUTOVECTORIZATION:
3480 * A compiler fence is used to prevent GCC and Clang from
3481 * autovectorizing the XXH64 loop (pragmas and attributes don't work for some
3482 * reason) without globally disabling AVX512.
3483 *
3484 * Autovectorization of XXH64 tends to be detrimental,
3485 * though the exact outcome may change depending on exact cpu and compiler version.
3486 * For information, it has been reported as detrimental for Skylake-X,
3487 * but possibly beneficial for Zen4.
3488 *
3489 * The default is to disable auto-vectorization,
3490 * but you can select to enable it instead using `XXH_ENABLE_AUTOVECTORIZE` build variable.
3491 */
3492 XXH_COMPILER_GUARD(acc);
3493#endif
3494 return acc;
3495}
3496
3497static xxh_u64 XXH64_mergeRound(xxh_u64 acc, xxh_u64 val)
3498{
3499 val = XXH64_round(0, val);
3500 acc ^= val;
3501 acc = acc * XXH_PRIME64_1 + XXH_PRIME64_4;
3502 return acc;
3503}
3504
3506static xxh_u64 XXH64_avalanche(xxh_u64 hash)
3507{
3508 hash ^= hash >> 33;
3509 hash *= XXH_PRIME64_2;
3510 hash ^= hash >> 29;
3511 hash *= XXH_PRIME64_3;
3512 hash ^= hash >> 32;
3513 return hash;
3514}
3515
3516
3517#define XXH_get64bits(p) XXH_readLE64_align(p, align)
3518
3523XXH_FORCE_INLINE void
3524XXH64_initAccs(xxh_u64 *acc, xxh_u64 seed)
3525{
3526 XXH_ASSERT(acc != NULL);
3527 acc[0] = seed + XXH_PRIME64_1 + XXH_PRIME64_2;
3528 acc[1] = seed + XXH_PRIME64_2;
3529 acc[2] = seed + 0;
3530 acc[3] = seed - XXH_PRIME64_1;
3531}
3532
3539XXH_FORCE_INLINE const xxh_u8 *
3540XXH64_consumeLong(
3541 xxh_u64 *XXH_RESTRICT acc,
3542 xxh_u8 const *XXH_RESTRICT input,
3543 size_t len,
3544 XXH_alignment align
3545)
3546{
3547 const xxh_u8* const bEnd = input + len;
3548 const xxh_u8* const limit = bEnd - 31;
3549 XXH_ASSERT(acc != NULL);
3550 XXH_ASSERT(input != NULL);
3551 XXH_ASSERT(len >= 32);
3552 do {
3553 /* reroll on 32-bit */
3554 if (sizeof(void *) < sizeof(xxh_u64)) {
3555 size_t i;
3556 for (i = 0; i < 4; i++) {
3557 acc[i] = XXH64_round(acc[i], XXH_get64bits(input));
3558 input += 8;
3559 }
3560 } else {
3561 acc[0] = XXH64_round(acc[0], XXH_get64bits(input)); input += 8;
3562 acc[1] = XXH64_round(acc[1], XXH_get64bits(input)); input += 8;
3563 acc[2] = XXH64_round(acc[2], XXH_get64bits(input)); input += 8;
3564 acc[3] = XXH64_round(acc[3], XXH_get64bits(input)); input += 8;
3565 }
3566 } while (input < limit);
3567
3568 return input;
3569}
3570
3575XXH_FORCE_INLINE XXH_PUREF xxh_u64
3576XXH64_mergeAccs(const xxh_u64 *acc)
3577{
3578 XXH_ASSERT(acc != NULL);
3579 {
3580 xxh_u64 h64 = XXH_rotl64(acc[0], 1) + XXH_rotl64(acc[1], 7)
3581 + XXH_rotl64(acc[2], 12) + XXH_rotl64(acc[3], 18);
3582 /* reroll on 32-bit */
3583 if (sizeof(void *) < sizeof(xxh_u64)) {
3584 size_t i;
3585 for (i = 0; i < 4; i++) {
3586 h64 = XXH64_mergeRound(h64, acc[i]);
3587 }
3588 } else {
3589 h64 = XXH64_mergeRound(h64, acc[0]);
3590 h64 = XXH64_mergeRound(h64, acc[1]);
3591 h64 = XXH64_mergeRound(h64, acc[2]);
3592 h64 = XXH64_mergeRound(h64, acc[3]);
3593 }
3594 return h64;
3595 }
3596}
3597
3613XXH_STATIC XXH_PUREF xxh_u64
3614XXH64_finalize(xxh_u64 hash, const xxh_u8* ptr, size_t len, XXH_alignment align)
3615{
3616 if (ptr==NULL) XXH_ASSERT(len == 0);
3617 len &= 31;
3618 while (len >= 8) {
3619 xxh_u64 const k1 = XXH64_round(0, XXH_get64bits(ptr));
3620 ptr += 8;
3621 hash ^= k1;
3622 hash = XXH_rotl64(hash,27) * XXH_PRIME64_1 + XXH_PRIME64_4;
3623 len -= 8;
3624 }
3625 if (len >= 4) {
3626 hash ^= (xxh_u64)(XXH_get32bits(ptr)) * XXH_PRIME64_1;
3627 ptr += 4;
3628 hash = XXH_rotl64(hash, 23) * XXH_PRIME64_2 + XXH_PRIME64_3;
3629 len -= 4;
3630 }
3631 while (len > 0) {
3632 hash ^= (*ptr++) * XXH_PRIME64_5;
3633 hash = XXH_rotl64(hash, 11) * XXH_PRIME64_1;
3634 --len;
3635 }
3636 return XXH64_avalanche(hash);
3637}
3638
3639#ifdef XXH_OLD_NAMES
3640# define PROCESS1_64 XXH_PROCESS1_64
3641# define PROCESS4_64 XXH_PROCESS4_64
3642# define PROCESS8_64 XXH_PROCESS8_64
3643#else
3644# undef XXH_PROCESS1_64
3645# undef XXH_PROCESS4_64
3646# undef XXH_PROCESS8_64
3647#endif
3648
3657XXH_FORCE_INLINE XXH_PUREF xxh_u64
3658XXH64_endian_align(const xxh_u8* input, size_t len, xxh_u64 seed, XXH_alignment align)
3659{
3660 xxh_u64 h64;
3661 if (input==NULL) XXH_ASSERT(len == 0);
3662
3663 if (len>=32) { /* Process a large block of data */
3664 xxh_u64 acc[4];
3665 XXH64_initAccs(acc, seed);
3666
3667 input = XXH64_consumeLong(acc, input, len, align);
3668
3669 h64 = XXH64_mergeAccs(acc);
3670 } else {
3671 h64 = seed + XXH_PRIME64_5;
3672 }
3673
3674 h64 += (xxh_u64) len;
3675
3676 return XXH64_finalize(h64, input, len, align);
3677}
3678
3679
3681XXH_PUBLIC_API XXH64_hash_t XXH64 (XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed)
3682{
3683#if !defined(XXH_NO_STREAM) && XXH_SIZE_OPT >= 2
3684 /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
3685 XXH64_state_t state;
3686 XXH64_reset(&state, seed);
3687 XXH64_update(&state, (const xxh_u8*)input, len);
3688 return XXH64_digest(&state);
3689#else
3691 if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
3692 return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
3693 } }
3694
3695 return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
3696
3697#endif
3698}
3699
3700/******* Hash Streaming *******/
3701#ifndef XXH_NO_STREAM
3704{
3705 return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
3706}
3707
3709{
3710 XXH_free(statePtr);
3711 return XXH_OK;
3712}
3713
3715XXH_PUBLIC_API void XXH64_copyState(XXH_NOESCAPE XXH64_state_t* dstState, const XXH64_state_t* srcState)
3716{
3717 XXH_memcpy(dstState, srcState, sizeof(*dstState));
3718}
3719
3722{
3723 XXH_ASSERT(statePtr != NULL);
3724 memset(statePtr, 0, sizeof(*statePtr));
3725 XXH64_initAccs(statePtr->acc, seed);
3726 return XXH_OK;
3727}
3728
3731XXH64_update (XXH_NOESCAPE XXH64_state_t* state, XXH_NOESCAPE const void* input, size_t len)
3732{
3733 if (input==NULL) {
3734 XXH_ASSERT(len == 0);
3735 return XXH_OK;
3736 }
3737
3738 state->total_len += len;
3739
3740 XXH_ASSERT(state->bufferedSize <= sizeof(state->buffer));
3741 if (len < sizeof(state->buffer) - state->bufferedSize) { /* fill in tmp buffer */
3742 XXH_memcpy(state->buffer + state->bufferedSize, input, len);
3743 state->bufferedSize += (XXH32_hash_t)len;
3744 return XXH_OK;
3745 }
3746
3747 { const xxh_u8* xinput = (const xxh_u8*)input;
3748 const xxh_u8* const bEnd = xinput + len;
3749
3750 if (state->bufferedSize) { /* non-empty buffer => complete first */
3751 XXH_memcpy(state->buffer + state->bufferedSize, xinput, sizeof(state->buffer) - state->bufferedSize);
3752 xinput += sizeof(state->buffer) - state->bufferedSize;
3753 /* and process one round */
3754 (void)XXH64_consumeLong(state->acc, state->buffer, sizeof(state->buffer), XXH_aligned);
3755 state->bufferedSize = 0;
3756 }
3757
3758 XXH_ASSERT(xinput <= bEnd);
3759 if ((size_t)(bEnd - xinput) >= sizeof(state->buffer)) {
3760 /* Process the remaining data */
3761 xinput = XXH64_consumeLong(state->acc, xinput, (size_t)(bEnd - xinput), XXH_unaligned);
3762 }
3763
3764 if (xinput < bEnd) {
3765 /* Copy the leftover to the tmp buffer */
3766 XXH_memcpy(state->buffer, xinput, (size_t)(bEnd-xinput));
3767 state->bufferedSize = (unsigned)(bEnd-xinput);
3768 }
3769 }
3770
3771 return XXH_OK;
3772}
3773
3774
3777{
3778 xxh_u64 h64;
3779
3780 if (state->total_len >= 32) {
3781 h64 = XXH64_mergeAccs(state->acc);
3782 } else {
3783 h64 = state->acc[2] /*seed*/ + XXH_PRIME64_5;
3784 }
3785
3786 h64 += (xxh_u64) state->total_len;
3787
3788 return XXH64_finalize(h64, state->buffer, (size_t)state->total_len, XXH_aligned);
3789}
3790#endif /* !XXH_NO_STREAM */
3791
3792/******* Canonical representation *******/
3793
3796{
3797 XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
3798 if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
3799 XXH_memcpy(dst, &hash, sizeof(*dst));
3800}
3801
3804{
3805 return XXH_readBE64(src);
3806}
3807
3808#ifndef XXH_NO_XXH3
3809
3810/* *********************************************************************
3811* XXH3
3812* New generation hash designed for speed on small keys and vectorization
3813************************************************************************ */
3820
3821/* === Compiler specifics === */
3822
3823
3824#if (defined(__GNUC__) && (__GNUC__ >= 3)) \
3825 || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) \
3826 || defined(__clang__)
3827# define XXH_likely(x) __builtin_expect(x, 1)
3828# define XXH_unlikely(x) __builtin_expect(x, 0)
3829#else
3830# define XXH_likely(x) (x)
3831# define XXH_unlikely(x) (x)
3832#endif
3833
3834#ifndef XXH_HAS_INCLUDE
3835# ifdef __has_include
3836/*
3837 * Not defined as XXH_HAS_INCLUDE(x) (function-like) because
3838 * this causes segfaults in Apple Clang 4.2 (on Mac OS X 10.7 Lion)
3839 */
3840# define XXH_HAS_INCLUDE __has_include
3841# else
3842# define XXH_HAS_INCLUDE(x) 0
3843# endif
3844#endif
3845
3846#if defined(__GNUC__) || defined(__clang__)
3847# if defined(__ARM_FEATURE_SVE)
3848# include <arm_sve.h>
3849# endif
3850# if defined(__ARM_NEON__) || defined(__ARM_NEON) \
3851 || (defined(_M_ARM) && _M_ARM >= 7) \
3852 || defined(_M_ARM64) || defined(_M_ARM64EC) \
3853 || (defined(__wasm_simd128__) && XXH_HAS_INCLUDE(<arm_neon.h>)) /* WASM SIMD128 via SIMDe */
3854# define inline __inline__ /* circumvent a clang bug */
3855# include <arm_neon.h>
3856# undef inline
3857# elif defined(__AVX2__)
3858# include <immintrin.h>
3859# elif defined(__SSE2__)
3860# include <emmintrin.h>
3861# elif defined(__loongarch_asx)
3862# include <lasxintrin.h>
3863# include <lsxintrin.h>
3864# elif defined(__loongarch_sx)
3865# include <lsxintrin.h>
3866# endif
3867#endif
3868
3869#if defined(_MSC_VER)
3870# include <intrin.h>
3871#endif
3872
3873/*
3874 * One goal of XXH3 is to make it fast on both 32-bit and 64-bit, while
3875 * remaining a true 64-bit/128-bit hash function.
3876 *
3877 * This is done by prioritizing a subset of 64-bit operations that can be
3878 * emulated without too many steps on the average 32-bit machine.
3879 *
3880 * For example, these two lines seem similar, and run equally fast on 64-bit:
3881 *
3882 * xxh_u64 x;
3883 * x ^= (x >> 47); // good
3884 * x ^= (x >> 13); // bad
3885 *
3886 * However, to a 32-bit machine, there is a major difference.
3887 *
3888 * x ^= (x >> 47) looks like this:
3889 *
3890 * x.lo ^= (x.hi >> (47 - 32));
3891 *
3892 * while x ^= (x >> 13) looks like this:
3893 *
3894 * // note: funnel shifts are not usually cheap.
3895 * x.lo ^= (x.lo >> 13) | (x.hi << (32 - 13));
3896 * x.hi ^= (x.hi >> 13);
3897 *
3898 * The first one is significantly faster than the second, simply because the
3899 * shift is larger than 32. This means:
3900 * - All the bits we need are in the upper 32 bits, so we can ignore the lower
3901 * 32 bits in the shift.
3902 * - The shift result will always fit in the lower 32 bits, and therefore,
3903 * we can ignore the upper 32 bits in the xor.
3904 *
3905 * Thanks to this optimization, XXH3 only requires these features to be efficient:
3906 *
3907 * - Usable unaligned access
3908 * - A 32-bit or 64-bit ALU
3909 * - If 32-bit, a decent ADC instruction
3910 * - A 32 or 64-bit multiply with a 64-bit result
3911 * - For the 128-bit variant, a decent byteswap helps short inputs.
3912 *
3913 * The first two are already required by XXH32, and almost all 32-bit and 64-bit
3914 * platforms which can run XXH32 can run XXH3 efficiently.
3915 *
3916 * Thumb-1, the classic 16-bit only subset of ARM's instruction set, is one
3917 * notable exception.
3918 *
3919 * First of all, Thumb-1 lacks support for the UMULL instruction which
3920 * performs the important long multiply. This means numerous __aeabi_lmul
3921 * calls.
3922 *
3923 * Second of all, the 8 functional registers are just not enough.
3924 * Setup for __aeabi_lmul, byteshift loads, pointers, and all arithmetic need
3925 * Lo registers, and this shuffling results in thousands more MOVs than A32.
3926 *
3927 * A32 and T32 don't have this limitation. They can access all 14 registers,
3928 * do a 32->64 multiply with UMULL, and the flexible operand allowing free
3929 * shifts is helpful, too.
3930 *
3931 * Therefore, we do a quick sanity check.
3932 *
3933 * If compiling Thumb-1 for a target which supports ARM instructions, we will
3934 * emit a warning, as it is not a "sane" platform to compile for.
3935 *
3936 * Usually, if this happens, it is because of an accident and you probably need
3937 * to specify -march, as you likely meant to compile for a newer architecture.
3938 *
3939 * Credit: large sections of the vectorial and asm source code paths
3940 * have been contributed by @easyaspi314
3941 */
3942#if defined(__thumb__) && !defined(__thumb2__) && defined(__ARM_ARCH_ISA_ARM)
3943# warning "XXH3 is highly inefficient without ARM or Thumb-2."
3944#endif
3945
3946/* ==========================================
3947 * Vectorization detection
3948 * ========================================== */
3949
3950#ifdef XXH_DOXYGEN
3961# define XXH_VECTOR XXH_SCALAR
3971# define XXH_ACC_ALIGN 8
3972#endif
3973
3974/* Actual definition */
3975#ifndef XXH_DOXYGEN
3976#endif
3977
3978#ifndef XXH_VECTOR /* can be defined on command line */
3979# if defined(__ARM_FEATURE_SVE)
3980# define XXH_VECTOR XXH_SVE
3981# elif ( \
3982 defined(__ARM_NEON__) || defined(__ARM_NEON) /* gcc */ \
3983 || defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC) /* msvc */ \
3984 || (defined(__wasm_simd128__) && XXH_HAS_INCLUDE(<arm_neon.h>)) /* wasm simd128 via SIMDe */ \
3985 ) && ( \
3986 defined(_WIN32) || defined(__LITTLE_ENDIAN__) /* little endian only */ \
3987 || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
3988 )
3989# define XXH_VECTOR XXH_NEON
3990# elif defined(__AVX512F__)
3991# define XXH_VECTOR XXH_AVX512
3992# elif defined(__AVX2__)
3993# define XXH_VECTOR XXH_AVX2
3994# elif defined(__SSE2__) || defined(_M_AMD64) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP == 2))
3995# define XXH_VECTOR XXH_SSE2
3996# elif (defined(__PPC64__) && defined(__POWER8_VECTOR__)) \
3997 || (defined(__s390x__) && defined(__VEC__)) \
3998 && defined(__GNUC__) /* TODO: IBM XL */
3999# define XXH_VECTOR XXH_VSX
4000# elif defined(__loongarch_asx)
4001# define XXH_VECTOR XXH_LASX
4002# elif defined(__loongarch_sx)
4003# define XXH_VECTOR XXH_LSX
4004# else
4005# define XXH_VECTOR XXH_SCALAR
4006# endif
4007#endif
4008
4009/* __ARM_FEATURE_SVE is only supported by GCC & Clang. */
4010#if (XXH_VECTOR == XXH_SVE) && !defined(__ARM_FEATURE_SVE)
4011# ifdef _MSC_VER
4012# pragma warning(once : 4606)
4013# else
4014# warning "__ARM_FEATURE_SVE isn't supported. Use SCALAR instead."
4015# endif
4016# undef XXH_VECTOR
4017# define XXH_VECTOR XXH_SCALAR
4018#endif
4019
4020/*
4021 * Controls the alignment of the accumulator,
4022 * for compatibility with aligned vector loads, which are usually faster.
4023 */
4024#ifndef XXH_ACC_ALIGN
4025# if defined(XXH_X86DISPATCH)
4026# define XXH_ACC_ALIGN 64 /* for compatibility with avx512 */
4027# elif XXH_VECTOR == XXH_SCALAR /* scalar */
4028# define XXH_ACC_ALIGN 8
4029# elif XXH_VECTOR == XXH_SSE2 /* sse2 */
4030# define XXH_ACC_ALIGN 16
4031# elif XXH_VECTOR == XXH_AVX2 /* avx2 */
4032# define XXH_ACC_ALIGN 32
4033# elif XXH_VECTOR == XXH_NEON /* neon */
4034# define XXH_ACC_ALIGN 16
4035# elif XXH_VECTOR == XXH_VSX /* vsx */
4036# define XXH_ACC_ALIGN 16
4037# elif XXH_VECTOR == XXH_AVX512 /* avx512 */
4038# define XXH_ACC_ALIGN 64
4039# elif XXH_VECTOR == XXH_SVE /* sve */
4040# define XXH_ACC_ALIGN 64
4041# elif XXH_VECTOR == XXH_LASX /* lasx */
4042# define XXH_ACC_ALIGN 64
4043# elif XXH_VECTOR == XXH_LSX /* lsx */
4044# define XXH_ACC_ALIGN 64
4045# endif
4046#endif
4047
4048#if defined(XXH_X86DISPATCH) || XXH_VECTOR == XXH_SSE2 \
4049 || XXH_VECTOR == XXH_AVX2 || XXH_VECTOR == XXH_AVX512
4050# define XXH_SEC_ALIGN XXH_ACC_ALIGN
4051#elif XXH_VECTOR == XXH_SVE
4052# define XXH_SEC_ALIGN XXH_ACC_ALIGN
4053#else
4054# define XXH_SEC_ALIGN 8
4055#endif
4056
4057#if defined(__GNUC__) || defined(__clang__)
4058# define XXH_ALIASING __attribute__((__may_alias__))
4059#else
4060# define XXH_ALIASING /* nothing */
4061#endif
4062
4063/*
4064 * UGLY HACK:
4065 * GCC usually generates the best code with -O3 for xxHash.
4066 *
4067 * However, when targeting AVX2, it is overzealous in its unrolling resulting
4068 * in code roughly 3/4 the speed of Clang.
4069 *
4070 * There are other issues, such as GCC splitting _mm256_loadu_si256 into
4071 * _mm_loadu_si128 + _mm256_inserti128_si256. This is an optimization which
4072 * only applies to Sandy and Ivy Bridge... which don't even support AVX2.
4073 *
4074 * That is why when compiling the AVX2 version, it is recommended to use either
4075 * -O2 -mavx2 -march=haswell
4076 * or
4077 * -O2 -mavx2 -mno-avx256-split-unaligned-load
4078 * for decent performance, or to use Clang instead.
4079 *
4080 * Fortunately, we can control the first one with a pragma that forces GCC into
4081 * -O2, but the other one we can't control without "failed to inline always
4082 * inline function due to target mismatch" warnings.
4083 */
4084#if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
4085 && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
4086 && defined(__OPTIMIZE__) && XXH_SIZE_OPT <= 0 /* respect -O0 and -Os */
4087# pragma GCC push_options
4088# pragma GCC optimize("-O2")
4089#endif
4090
4091#if XXH_VECTOR == XXH_NEON
4092
4093/*
4094 * UGLY HACK: While AArch64 GCC on Linux does not seem to care, on macOS, GCC -O3
4095 * optimizes out the entire hashLong loop because of the aliasing violation.
4096 *
4097 * However, GCC is also inefficient at load-store optimization with vld1q/vst1q,
4098 * so the only option is to mark it as aliasing.
4099 */
4100typedef uint64x2_t xxh_aliasing_uint64x2_t XXH_ALIASING;
4101
4115#if defined(__aarch64__) && defined(__GNUC__) && !defined(__clang__)
4116XXH_FORCE_INLINE uint64x2_t XXH_vld1q_u64(void const* ptr) /* silence -Wcast-align */
4117{
4118 return *(xxh_aliasing_uint64x2_t const *)ptr;
4119}
4120#else
4121XXH_FORCE_INLINE uint64x2_t XXH_vld1q_u64(void const* ptr)
4122{
4123 return vreinterpretq_u64_u8(vld1q_u8((uint8_t const*)ptr));
4124}
4125#endif
4126
4135#if defined(__aarch64__) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 11
4136XXH_FORCE_INLINE uint64x2_t
4137XXH_vmlal_low_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
4138{
4139 /* Inline assembly is the only way */
4140 __asm__("umlal %0.2d, %1.2s, %2.2s" : "+w" (acc) : "w" (lhs), "w" (rhs));
4141 return acc;
4142}
4143XXH_FORCE_INLINE uint64x2_t
4144XXH_vmlal_high_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
4145{
4146 /* This intrinsic works as expected */
4147 return vmlal_high_u32(acc, lhs, rhs);
4148}
4149#else
4150/* Portable intrinsic versions */
4151XXH_FORCE_INLINE uint64x2_t
4152XXH_vmlal_low_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
4153{
4154 return vmlal_u32(acc, vget_low_u32(lhs), vget_low_u32(rhs));
4155}
4158XXH_FORCE_INLINE uint64x2_t
4159XXH_vmlal_high_u32(uint64x2_t acc, uint32x4_t lhs, uint32x4_t rhs)
4160{
4161 return vmlal_u32(acc, vget_high_u32(lhs), vget_high_u32(rhs));
4162}
4163#endif
4164
4203# ifndef XXH3_NEON_LANES
4204# if (defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC)) \
4205 && !defined(__APPLE__) && XXH_SIZE_OPT <= 0
4206# define XXH3_NEON_LANES 6
4207# else
4208# define XXH3_NEON_LANES XXH_ACC_NB
4209# endif
4210# endif
4211#endif /* XXH_VECTOR == XXH_NEON */
4212
4213/*
4214 * VSX and Z Vector helpers.
4215 *
4216 * This is very messy, and any pull requests to clean this up are welcome.
4217 *
4218 * There are a lot of problems with supporting VSX and s390x, due to
4219 * inconsistent intrinsics, spotty coverage, and multiple endiannesses.
4220 */
4221#if XXH_VECTOR == XXH_VSX
4222/* Annoyingly, these headers _may_ define three macros: `bool`, `vector`,
4223 * and `pixel`. This is a problem for obvious reasons.
4224 *
4225 * These keywords are unnecessary; the spec literally says they are
4226 * equivalent to `__bool`, `__vector`, and `__pixel` and may be undef'd
4227 * after including the header.
4228 *
4229 * We use pragma push_macro/pop_macro to keep the namespace clean. */
4230# pragma push_macro("bool")
4231# pragma push_macro("vector")
4232# pragma push_macro("pixel")
4233/* silence potential macro redefined warnings */
4234# undef bool
4235# undef vector
4236# undef pixel
4237
4238# if defined(__s390x__)
4239# include <s390intrin.h>
4240# else
4241# include <altivec.h>
4242# endif
4243
4244/* Restore the original macro values, if applicable. */
4245# pragma pop_macro("pixel")
4246# pragma pop_macro("vector")
4247# pragma pop_macro("bool")
4248
4249typedef __vector unsigned long long xxh_u64x2;
4250typedef __vector unsigned char xxh_u8x16;
4251typedef __vector unsigned xxh_u32x4;
4252
4253/*
4254 * UGLY HACK: Similar to aarch64 macOS GCC, s390x GCC has the same aliasing issue.
4255 */
4256typedef xxh_u64x2 xxh_aliasing_u64x2 XXH_ALIASING;
4257
4258# ifndef XXH_VSX_BE
4259# if defined(__BIG_ENDIAN__) \
4260 || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
4261# define XXH_VSX_BE 1
4262# elif defined(__VEC_ELEMENT_REG_ORDER__) && __VEC_ELEMENT_REG_ORDER__ == __ORDER_BIG_ENDIAN__
4263# warning "-maltivec=be is not recommended. Please use native endianness."
4264# define XXH_VSX_BE 1
4265# else
4266# define XXH_VSX_BE 0
4267# endif
4268# endif /* !defined(XXH_VSX_BE) */
4269
4270# if XXH_VSX_BE
4271# if defined(__POWER9_VECTOR__) || (defined(__clang__) && defined(__s390x__))
4272# define XXH_vec_revb vec_revb
4273# else
4277XXH_FORCE_INLINE xxh_u64x2 XXH_vec_revb(xxh_u64x2 val)
4278{
4279 xxh_u8x16 const vByteSwap = { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
4280 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08 };
4281 return vec_perm(val, val, vByteSwap);
4282}
4283# endif
4284# endif /* XXH_VSX_BE */
4285
4289XXH_FORCE_INLINE xxh_u64x2 XXH_vec_loadu(const void *ptr)
4290{
4291 xxh_u64x2 ret;
4292 XXH_memcpy(&ret, ptr, sizeof(xxh_u64x2));
4293# if XXH_VSX_BE
4294 ret = XXH_vec_revb(ret);
4295# endif
4296 return ret;
4297}
4298
4299/*
4300 * vec_mulo and vec_mule are very problematic intrinsics on PowerPC
4301 *
4302 * These intrinsics weren't added until GCC 8, despite existing for a while,
4303 * and they are endian dependent. Also, their meaning swap depending on version.
4304 * */
4305# if defined(__s390x__)
4306 /* s390x is always big endian, no issue on this platform */
4307# define XXH_vec_mulo vec_mulo
4308# define XXH_vec_mule vec_mule
4309# elif defined(__clang__) && XXH_HAS_BUILTIN(__builtin_altivec_vmuleuw) && !defined(__ibmxl__)
4310/* Clang has a better way to control this, we can just use the builtin which doesn't swap. */
4311 /* The IBM XL Compiler (which defined __clang__) only implements the vec_* operations */
4312# define XXH_vec_mulo __builtin_altivec_vmulouw
4313# define XXH_vec_mule __builtin_altivec_vmuleuw
4314# else
4315/* gcc needs inline assembly */
4316/* Adapted from https://github.com/google/highwayhash/blob/master/highwayhash/hh_vsx.h. */
4317XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mulo(xxh_u32x4 a, xxh_u32x4 b)
4318{
4319 xxh_u64x2 result;
4320 __asm__("vmulouw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
4321 return result;
4322}
4323XXH_FORCE_INLINE xxh_u64x2 XXH_vec_mule(xxh_u32x4 a, xxh_u32x4 b)
4324{
4325 xxh_u64x2 result;
4326 __asm__("vmuleuw %0, %1, %2" : "=v" (result) : "v" (a), "v" (b));
4327 return result;
4328}
4329# endif /* XXH_vec_mulo, XXH_vec_mule */
4330#endif /* XXH_VECTOR == XXH_VSX */
4331
4332#if XXH_VECTOR == XXH_SVE
4333#define ACCRND(acc, offset) \
4334do { \
4335 svuint64_t input_vec = svld1_u64(mask, xinput + offset); \
4336 svuint64_t secret_vec = svld1_u64(mask, xsecret + offset); \
4337 svuint64_t mixed = sveor_u64_x(mask, secret_vec, input_vec); \
4338 svuint64_t swapped = svtbl_u64(input_vec, kSwap); \
4339 svuint64_t mixed_lo = svextw_u64_x(mask, mixed); \
4340 svuint64_t mixed_hi = svlsr_n_u64_x(mask, mixed, 32); \
4341 svuint64_t mul = svmad_u64_x(mask, mixed_lo, mixed_hi, swapped); \
4342 acc = svadd_u64_x(mask, acc, mul); \
4343} while (0)
4344#endif /* XXH_VECTOR == XXH_SVE */
4345
4346/* prefetch
4347 * can be disabled, by declaring XXH_NO_PREFETCH build macro */
4348#if defined(XXH_NO_PREFETCH)
4349# define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
4350#else
4351# if XXH_SIZE_OPT >= 1
4352# define XXH_PREFETCH(ptr) (void)(ptr)
4353# elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)) /* _mm_prefetch() not defined outside of x86/x64 */
4354# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
4355# define XXH_PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
4356# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
4357# define XXH_PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
4358# else
4359# define XXH_PREFETCH(ptr) (void)(ptr) /* disabled */
4360# endif
4361#endif /* XXH_NO_PREFETCH */
4362
4363
4364/* ==========================================
4365 * XXH3 default settings
4366 * ========================================== */
4367
4368#define XXH_SECRET_DEFAULT_SIZE 192 /* minimum XXH3_SECRET_SIZE_MIN */
4369
4370#if (XXH_SECRET_DEFAULT_SIZE < XXH3_SECRET_SIZE_MIN)
4371# error "default keyset is not large enough"
4372#endif
4373
4378XXH_ALIGN(64) static const xxh_u8 XXH3_kSecret[XXH_SECRET_DEFAULT_SIZE] = {
4379 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c,
4380 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f,
4381 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
4382 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c,
4383 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3,
4384 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
4385 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d,
4386 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64,
4387 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
4388 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e,
4389 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce,
4390 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,
4391};
4392
4393static const xxh_u64 PRIME_MX1 = 0x165667919E3779F9ULL;
4394static const xxh_u64 PRIME_MX2 = 0x9FB21C651E98DF25ULL;
4395
4396#ifdef XXH_OLD_NAMES
4397# define kSecret XXH3_kSecret
4398#endif
4399
4400#ifdef XXH_DOXYGEN
4417XXH_FORCE_INLINE xxh_u64
4418XXH_mult32to64(xxh_u64 x, xxh_u64 y)
4419{
4420 return (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF);
4421}
4422#elif defined(_MSC_VER) && defined(_M_IX86)
4423# define XXH_mult32to64(x, y) __emulu((unsigned)(x), (unsigned)(y))
4424#else
4425/*
4426 * Downcast + upcast is usually better than masking on older compilers like
4427 * GCC 4.2 (especially 32-bit ones), all without affecting newer compilers.
4428 *
4429 * The other method, (x & 0xFFFFFFFF) * (y & 0xFFFFFFFF), will AND both operands
4430 * and perform a full 64x64 multiply -- entirely redundant on 32-bit.
4431 */
4432# define XXH_mult32to64(x, y) ((xxh_u64)(xxh_u32)(x) * (xxh_u64)(xxh_u32)(y))
4433#endif
4434
4444static XXH128_hash_t
4445XXH_mult64to128(xxh_u64 lhs, xxh_u64 rhs)
4446{
4447 /*
4448 * GCC/Clang __uint128_t method.
4449 *
4450 * On most 64-bit targets, GCC and Clang define a __uint128_t type.
4451 * This is usually the best way as it usually uses a native long 64-bit
4452 * multiply, such as MULQ on x86_64 or MUL + UMULH on aarch64.
4453 *
4454 * Usually.
4455 *
4456 * Despite being a 32-bit platform, Clang (and emscripten) define this type
4457 * despite not having the arithmetic for it. This results in a laggy
4458 * compiler builtin call which calculates a full 128-bit multiply.
4459 * In that case it is best to use the portable one.
4460 * https://github.com/Cyan4973/xxHash/issues/211#issuecomment-515575677
4461 */
4462#if (defined(__GNUC__) || defined(__clang__)) && !defined(__wasm__) \
4463 && defined(__SIZEOF_INT128__) \
4464 || (defined(_INTEGRAL_MAX_BITS) && _INTEGRAL_MAX_BITS >= 128)
4465
4466 __uint128_t const product = (__uint128_t)lhs * (__uint128_t)rhs;
4467 XXH128_hash_t r128;
4468 r128.low64 = (xxh_u64)(product);
4469 r128.high64 = (xxh_u64)(product >> 64);
4470 return r128;
4471
4472 /*
4473 * MSVC for x64's _umul128 method.
4474 *
4475 * xxh_u64 _umul128(xxh_u64 Multiplier, xxh_u64 Multiplicand, xxh_u64 *HighProduct);
4476 *
4477 * This compiles to single operand MUL on x64.
4478 */
4479#elif (defined(_M_X64) || defined(_M_IA64)) && !defined(_M_ARM64EC)
4480
4481#ifndef _MSC_VER
4482# pragma intrinsic(_umul128)
4483#endif
4484 xxh_u64 product_high;
4485 xxh_u64 const product_low = _umul128(lhs, rhs, &product_high);
4486 XXH128_hash_t r128;
4487 r128.low64 = product_low;
4488 r128.high64 = product_high;
4489 return r128;
4490
4491 /*
4492 * MSVC for ARM64's __umulh method.
4493 *
4494 * This compiles to the same MUL + UMULH as GCC/Clang's __uint128_t method.
4495 */
4496#elif defined(_M_ARM64) || defined(_M_ARM64EC)
4497
4498#ifndef _MSC_VER
4499# pragma intrinsic(__umulh)
4500#endif
4501 XXH128_hash_t r128;
4502 r128.low64 = lhs * rhs;
4503 r128.high64 = __umulh(lhs, rhs);
4504 return r128;
4505
4506#else
4507 /*
4508 * Portable scalar method. Optimized for 32-bit and 64-bit ALUs.
4509 *
4510 * This is a fast and simple grade school multiply, which is shown below
4511 * with base 10 arithmetic instead of base 0x100000000.
4512 *
4513 * 9 3 // D2 lhs = 93
4514 * x 7 5 // D2 rhs = 75
4515 * ----------
4516 * 1 5 // D2 lo_lo = (93 % 10) * (75 % 10) = 15
4517 * 4 5 | // D2 hi_lo = (93 / 10) * (75 % 10) = 45
4518 * 2 1 | // D2 lo_hi = (93 % 10) * (75 / 10) = 21
4519 * + 6 3 | | // D2 hi_hi = (93 / 10) * (75 / 10) = 63
4520 * ---------
4521 * 2 7 | // D2 cross = (15 / 10) + (45 % 10) + 21 = 27
4522 * + 6 7 | | // D2 upper = (27 / 10) + (45 / 10) + 63 = 67
4523 * ---------
4524 * 6 9 7 5 // D4 res = (27 * 10) + (15 % 10) + (67 * 100) = 6975
4525 *
4526 * The reasons for adding the products like this are:
4527 * 1. It avoids manual carry tracking. Just like how
4528 * (9 * 9) + 9 + 9 = 99, the same applies with this for UINT64_MAX.
4529 * This avoids a lot of complexity.
4530 *
4531 * 2. It hints for, and on Clang, compiles to, the powerful UMAAL
4532 * instruction available in ARM's Digital Signal Processing extension
4533 * in 32-bit ARMv6 and later, which is shown below:
4534 *
4535 * void UMAAL(xxh_u32 *RdLo, xxh_u32 *RdHi, xxh_u32 Rn, xxh_u32 Rm)
4536 * {
4537 * xxh_u64 product = (xxh_u64)*RdLo * (xxh_u64)*RdHi + Rn + Rm;
4538 * *RdLo = (xxh_u32)(product & 0xFFFFFFFF);
4539 * *RdHi = (xxh_u32)(product >> 32);
4540 * }
4541 *
4542 * This instruction was designed for efficient long multiplication, and
4543 * allows this to be calculated in only 4 instructions at speeds
4544 * comparable to some 64-bit ALUs.
4545 *
4546 * 3. It isn't terrible on other platforms. Usually this will be a couple
4547 * of 32-bit ADD/ADCs.
4548 */
4549
4550 /* First calculate all of the cross products. */
4551 xxh_u64 const lo_lo = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs & 0xFFFFFFFF);
4552 xxh_u64 const hi_lo = XXH_mult32to64(lhs >> 32, rhs & 0xFFFFFFFF);
4553 xxh_u64 const lo_hi = XXH_mult32to64(lhs & 0xFFFFFFFF, rhs >> 32);
4554 xxh_u64 const hi_hi = XXH_mult32to64(lhs >> 32, rhs >> 32);
4555
4556 /* Now add the products together. These will never overflow. */
4557 xxh_u64 const cross = (lo_lo >> 32) + (hi_lo & 0xFFFFFFFF) + lo_hi;
4558 xxh_u64 const upper = (hi_lo >> 32) + (cross >> 32) + hi_hi;
4559 xxh_u64 const lower = (cross << 32) | (lo_lo & 0xFFFFFFFF);
4560
4561 XXH128_hash_t r128;
4562 r128.low64 = lower;
4563 r128.high64 = upper;
4564 return r128;
4565#endif
4566}
4567
4578static xxh_u64
4579XXH3_mul128_fold64(xxh_u64 lhs, xxh_u64 rhs)
4580{
4581 XXH128_hash_t product = XXH_mult64to128(lhs, rhs);
4582 return product.low64 ^ product.high64;
4583}
4584
4586XXH_FORCE_INLINE XXH_CONSTF xxh_u64 XXH_xorshift64(xxh_u64 v64, int shift)
4587{
4588 XXH_ASSERT(0 <= shift && shift < 64);
4589 return v64 ^ (v64 >> shift);
4590}
4591
4592/*
4593 * This is a fast avalanche stage,
4594 * suitable when input bits are already partially mixed
4595 */
4596static XXH64_hash_t XXH3_avalanche(xxh_u64 h64)
4597{
4598 h64 = XXH_xorshift64(h64, 37);
4599 h64 *= PRIME_MX1;
4600 h64 = XXH_xorshift64(h64, 32);
4601 return h64;
4602}
4603
4604/*
4605 * This is a stronger avalanche,
4606 * inspired by Pelle Evensen's rrmxmx
4607 * preferable when input has not been previously mixed
4608 */
4609static XXH64_hash_t XXH3_rrmxmx(xxh_u64 h64, xxh_u64 len)
4610{
4611 /* this mix is inspired by Pelle Evensen's rrmxmx */
4612 h64 ^= XXH_rotl64(h64, 49) ^ XXH_rotl64(h64, 24);
4613 h64 *= PRIME_MX2;
4614 h64 ^= (h64 >> 35) + len ;
4615 h64 *= PRIME_MX2;
4616 return XXH_xorshift64(h64, 28);
4617}
4618
4619
4620/* ==========================================
4621 * Short keys
4622 * ==========================================
4623 * One of the shortcomings of XXH32 and XXH64 was that their performance was
4624 * sub-optimal on short lengths. It used an iterative algorithm which strongly
4625 * favored lengths that were a multiple of 4 or 8.
4626 *
4627 * Instead of iterating over individual inputs, we use a set of single shot
4628 * functions which piece together a range of lengths and operate in constant time.
4629 *
4630 * Additionally, the number of multiplies has been significantly reduced. This
4631 * reduces latency, especially when emulating 64-bit multiplies on 32-bit.
4632 *
4633 * Depending on the platform, this may or may not be faster than XXH32, but it
4634 * is almost guaranteed to be faster than XXH64.
4635 */
4636
4637/*
4638 * At very short lengths, there isn't enough input to fully hide secrets, or use
4639 * the entire secret.
4640 *
4641 * There is also only a limited amount of mixing we can do before significantly
4642 * impacting performance.
4643 *
4644 * Therefore, we use different sections of the secret and always mix two secret
4645 * samples with an XOR. This should have no effect on performance on the
4646 * seedless or withSeed variants because everything _should_ be constant folded
4647 * by modern compilers.
4648 *
4649 * The XOR mixing hides individual parts of the secret and increases entropy.
4650 *
4651 * This adds an extra layer of strength for custom secrets.
4652 */
4653XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
4654XXH3_len_1to3_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
4655{
4656 XXH_ASSERT(input != NULL);
4657 XXH_ASSERT(1 <= len && len <= 3);
4658 XXH_ASSERT(secret != NULL);
4659 /*
4660 * len = 1: combined = { input[0], 0x01, input[0], input[0] }
4661 * len = 2: combined = { input[1], 0x02, input[0], input[1] }
4662 * len = 3: combined = { input[2], 0x03, input[0], input[1] }
4663 */
4664 { xxh_u8 const c1 = input[0];
4665 xxh_u8 const c2 = input[len >> 1];
4666 xxh_u8 const c3 = input[len - 1];
4667 xxh_u32 const combined = ((xxh_u32)c1 << 16) | ((xxh_u32)c2 << 24)
4668 | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
4669 xxh_u64 const bitflip = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
4670 xxh_u64 const keyed = (xxh_u64)combined ^ bitflip;
4671 return XXH64_avalanche(keyed);
4672 }
4673}
4674
4675XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
4676XXH3_len_4to8_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
4677{
4678 XXH_ASSERT(input != NULL);
4679 XXH_ASSERT(secret != NULL);
4680 XXH_ASSERT(4 <= len && len <= 8);
4681 seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
4682 { xxh_u32 const input1 = XXH_readLE32(input);
4683 xxh_u32 const input2 = XXH_readLE32(input + len - 4);
4684 xxh_u64 const bitflip = (XXH_readLE64(secret+8) ^ XXH_readLE64(secret+16)) - seed;
4685 xxh_u64 const input64 = input2 + (((xxh_u64)input1) << 32);
4686 xxh_u64 const keyed = input64 ^ bitflip;
4687 return XXH3_rrmxmx(keyed, len);
4688 }
4689}
4690
4691XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
4692XXH3_len_9to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
4693{
4694 XXH_ASSERT(input != NULL);
4695 XXH_ASSERT(secret != NULL);
4696 XXH_ASSERT(9 <= len && len <= 16);
4697 { xxh_u64 const bitflip1 = (XXH_readLE64(secret+24) ^ XXH_readLE64(secret+32)) + seed;
4698 xxh_u64 const bitflip2 = (XXH_readLE64(secret+40) ^ XXH_readLE64(secret+48)) - seed;
4699 xxh_u64 const input_lo = XXH_readLE64(input) ^ bitflip1;
4700 xxh_u64 const input_hi = XXH_readLE64(input + len - 8) ^ bitflip2;
4701 xxh_u64 const acc = len
4702 + XXH_swap64(input_lo) + input_hi
4703 + XXH3_mul128_fold64(input_lo, input_hi);
4704 return XXH3_avalanche(acc);
4705 }
4706}
4707
4708XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
4709XXH3_len_0to16_64b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
4710{
4711 XXH_ASSERT(len <= 16);
4712 { if (XXH_likely(len > 8)) return XXH3_len_9to16_64b(input, len, secret, seed);
4713 if (XXH_likely(len >= 4)) return XXH3_len_4to8_64b(input, len, secret, seed);
4714 if (len) return XXH3_len_1to3_64b(input, len, secret, seed);
4715 return XXH64_avalanche(seed ^ (XXH_readLE64(secret+56) ^ XXH_readLE64(secret+64)));
4716 }
4717}
4718
4719/*
4720 * DISCLAIMER: There are known *seed-dependent* multicollisions here due to
4721 * multiplication by zero, affecting hashes of lengths 17 to 240.
4722 *
4723 * However, they are very unlikely.
4724 *
4725 * Keep this in mind when using the unseeded XXH3_64bits() variant: As with all
4726 * unseeded non-cryptographic hashes, it does not attempt to defend itself
4727 * against specially crafted inputs, only random inputs.
4728 *
4729 * Compared to classic UMAC where a 1 in 2^31 chance of 4 consecutive bytes
4730 * cancelling out the secret is taken an arbitrary number of times (addressed
4731 * in XXH3_accumulate_512), this collision is very unlikely with random inputs
4732 * and/or proper seeding:
4733 *
4734 * This only has a 1 in 2^63 chance of 8 consecutive bytes cancelling out, in a
4735 * function that is only called up to 16 times per hash with up to 240 bytes of
4736 * input.
4737 *
4738 * This is not too bad for a non-cryptographic hash function, especially with
4739 * only 64 bit outputs.
4740 *
4741 * The 128-bit variant (which trades some speed for strength) is NOT affected
4742 * by this, although it is always a good idea to use a proper seed if you care
4743 * about strength.
4744 */
4745XXH_FORCE_INLINE xxh_u64 XXH3_mix16B(const xxh_u8* XXH_RESTRICT input,
4746 const xxh_u8* XXH_RESTRICT secret, xxh_u64 seed64)
4747{
4748#if defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
4749 && defined(__i386__) && defined(__SSE2__) /* x86 + SSE2 */ \
4750 && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable like XXH32 hack */
4751 /*
4752 * UGLY HACK:
4753 * GCC for x86 tends to autovectorize the 128-bit multiply, resulting in
4754 * slower code.
4755 *
4756 * By forcing seed64 into a register, we disrupt the cost model and
4757 * cause it to scalarize. See `XXH32_round()`
4758 *
4759 * FIXME: Clang's output is still _much_ faster -- On an AMD Ryzen 3600,
4760 * XXH3_64bits @ len=240 runs at 4.6 GB/s with Clang 9, but 3.3 GB/s on
4761 * GCC 9.2, despite both emitting scalar code.
4762 *
4763 * GCC generates much better scalar code than Clang for the rest of XXH3,
4764 * which is why finding a more optimal codepath is an interest.
4765 */
4766 XXH_COMPILER_GUARD(seed64);
4767#endif
4768 { xxh_u64 const input_lo = XXH_readLE64(input);
4769 xxh_u64 const input_hi = XXH_readLE64(input+8);
4770 return XXH3_mul128_fold64(
4771 input_lo ^ (XXH_readLE64(secret) + seed64),
4772 input_hi ^ (XXH_readLE64(secret+8) - seed64)
4773 );
4774 }
4775}
4776
4777/* For mid range keys, XXH3 uses a Mum-hash variant. */
4778XXH_FORCE_INLINE XXH_PUREF XXH64_hash_t
4779XXH3_len_17to128_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
4780 const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
4781 XXH64_hash_t seed)
4782{
4783 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
4784 XXH_ASSERT(16 < len && len <= 128);
4785
4786 { xxh_u64 acc = len * XXH_PRIME64_1;
4787#if XXH_SIZE_OPT >= 1
4788 /* Smaller and cleaner, but slightly slower. */
4789 unsigned int i = (unsigned int)(len - 1) / 32;
4790 do {
4791 acc += XXH3_mix16B(input+16 * i, secret+32*i, seed);
4792 acc += XXH3_mix16B(input+len-16*(i+1), secret+32*i+16, seed);
4793 } while (i-- != 0);
4794#else
4795 if (len > 32) {
4796 if (len > 64) {
4797 if (len > 96) {
4798 acc += XXH3_mix16B(input+48, secret+96, seed);
4799 acc += XXH3_mix16B(input+len-64, secret+112, seed);
4800 }
4801 acc += XXH3_mix16B(input+32, secret+64, seed);
4802 acc += XXH3_mix16B(input+len-48, secret+80, seed);
4803 }
4804 acc += XXH3_mix16B(input+16, secret+32, seed);
4805 acc += XXH3_mix16B(input+len-32, secret+48, seed);
4806 }
4807 acc += XXH3_mix16B(input+0, secret+0, seed);
4808 acc += XXH3_mix16B(input+len-16, secret+16, seed);
4809#endif
4810 return XXH3_avalanche(acc);
4811 }
4812}
4813
4814XXH_NO_INLINE XXH_PUREF XXH64_hash_t
4815XXH3_len_129to240_64b(const xxh_u8* XXH_RESTRICT input, size_t len,
4816 const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
4817 XXH64_hash_t seed)
4818{
4819 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
4820 XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
4821
4822 #define XXH3_MIDSIZE_STARTOFFSET 3
4823 #define XXH3_MIDSIZE_LASTOFFSET 17
4824
4825 { xxh_u64 acc = len * XXH_PRIME64_1;
4826 xxh_u64 acc_end;
4827 unsigned int const nbRounds = (unsigned int)len / 16;
4828 unsigned int i;
4829 XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
4830 for (i=0; i<8; i++) {
4831 acc += XXH3_mix16B(input+(16*i), secret+(16*i), seed);
4832 }
4833 /* last bytes */
4834 acc_end = XXH3_mix16B(input + len - 16, secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET, seed);
4835 XXH_ASSERT(nbRounds >= 8);
4836 acc = XXH3_avalanche(acc);
4837#if defined(__clang__) /* Clang */ \
4838 && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
4839 && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
4840 /*
4841 * UGLY HACK:
4842 * Clang for ARMv7-A tries to vectorize this loop, similar to GCC x86.
4843 * In everywhere else, it uses scalar code.
4844 *
4845 * For 64->128-bit multiplies, even if the NEON was 100% optimal, it
4846 * would still be slower than UMAAL (see XXH_mult64to128).
4847 *
4848 * Unfortunately, Clang doesn't handle the long multiplies properly and
4849 * converts them to the nonexistent "vmulq_u64" intrinsic, which is then
4850 * scalarized into an ugly mess of VMOV.32 instructions.
4851 *
4852 * This mess is difficult to avoid without turning autovectorization
4853 * off completely, but they are usually relatively minor and/or not
4854 * worth it to fix.
4855 *
4856 * This loop is the easiest to fix, as unlike XXH32, this pragma
4857 * _actually works_ because it is a loop vectorization instead of an
4858 * SLP vectorization.
4859 */
4860 #pragma clang loop vectorize(disable)
4861#endif
4862 for (i=8 ; i < nbRounds; i++) {
4863 /*
4864 * Prevents clang for unrolling the acc loop and interleaving with this one.
4865 */
4866 XXH_COMPILER_GUARD(acc);
4867 acc_end += XXH3_mix16B(input+(16*i), secret+(16*(i-8)) + XXH3_MIDSIZE_STARTOFFSET, seed);
4868 }
4869 return XXH3_avalanche(acc + acc_end);
4870 }
4871}
4872
4873
4874/* ======= Long Keys ======= */
4875
4876#define XXH_STRIPE_LEN 64
4877#define XXH_SECRET_CONSUME_RATE 8 /* nb of secret bytes consumed at each accumulation */
4878#define XXH_ACC_NB (XXH_STRIPE_LEN / sizeof(xxh_u64))
4879
4880#ifdef XXH_OLD_NAMES
4881# define STRIPE_LEN XXH_STRIPE_LEN
4882# define ACC_NB XXH_ACC_NB
4883#endif
4884
4885#ifndef XXH_PREFETCH_DIST
4886# ifdef __clang__
4887# define XXH_PREFETCH_DIST 320
4888# else
4889# if (XXH_VECTOR == XXH_AVX512)
4890# define XXH_PREFETCH_DIST 512
4891# else
4892# define XXH_PREFETCH_DIST 384
4893# endif
4894# endif /* __clang__ */
4895#endif /* XXH_PREFETCH_DIST */
4896
4897/*
4898 * These macros are to generate an XXH3_accumulate() function.
4899 * The two arguments select the name suffix and target attribute.
4900 *
4901 * The name of this symbol is XXH3_accumulate_<name>() and it calls
4902 * XXH3_accumulate_512_<name>().
4903 *
4904 * It may be useful to hand implement this function if the compiler fails to
4905 * optimize the inline function.
4906 */
4907#define XXH3_ACCUMULATE_TEMPLATE(name) \
4908void \
4909XXH3_accumulate_##name(xxh_u64* XXH_RESTRICT acc, \
4910 const xxh_u8* XXH_RESTRICT input, \
4911 const xxh_u8* XXH_RESTRICT secret, \
4912 size_t nbStripes) \
4913{ \
4914 size_t n; \
4915 for (n = 0; n < nbStripes; n++ ) { \
4916 const xxh_u8* const in = input + n*XXH_STRIPE_LEN; \
4917 XXH_PREFETCH(in + XXH_PREFETCH_DIST); \
4918 XXH3_accumulate_512_##name( \
4919 acc, \
4920 in, \
4921 secret + n*XXH_SECRET_CONSUME_RATE); \
4922 } \
4923}
4924
4925
4926XXH_FORCE_INLINE void XXH_writeLE64(void* dst, xxh_u64 v64)
4927{
4928 if (!XXH_CPU_LITTLE_ENDIAN) v64 = XXH_swap64(v64);
4929 XXH_memcpy(dst, &v64, sizeof(v64));
4930}
4931
4932/* Several intrinsic functions below are supposed to accept __int64 as argument,
4933 * as documented in https://software.intel.com/sites/landingpage/IntrinsicsGuide/ .
4934 * However, several environments do not define __int64 type,
4935 * requiring a workaround.
4936 */
4937#if !defined (__VMS) \
4938 && (defined (__cplusplus) \
4939 || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
4940 typedef int64_t xxh_i64;
4941#else
4942 /* the following type must have a width of 64-bit */
4943 typedef long long xxh_i64;
4944#endif
4945
4946
4947/*
4948 * XXH3_accumulate_512 is the tightest loop for long inputs, and it is the most optimized.
4949 *
4950 * It is a hardened version of UMAC, based off of FARSH's implementation.
4951 *
4952 * This was chosen because it adapts quite well to 32-bit, 64-bit, and SIMD
4953 * implementations, and it is ridiculously fast.
4954 *
4955 * We harden it by mixing the original input to the accumulators as well as the product.
4956 *
4957 * This means that in the (relatively likely) case of a multiply by zero, the
4958 * original input is preserved.
4959 *
4960 * On 128-bit inputs, we swap 64-bit pairs when we add the input to improve
4961 * cross-pollination, as otherwise the upper and lower halves would be
4962 * essentially independent.
4963 *
4964 * This doesn't matter on 64-bit hashes since they all get merged together in
4965 * the end, so we skip the extra step.
4966 *
4967 * Both XXH3_64bits and XXH3_128bits use this subroutine.
4968 */
4969
4970#if (XXH_VECTOR == XXH_AVX512) \
4971 || (defined(XXH_DISPATCH_AVX512) && XXH_DISPATCH_AVX512 != 0)
4972
4973#ifndef XXH_TARGET_AVX512
4974# define XXH_TARGET_AVX512 /* disable attribute target */
4975#endif
4976
4977XXH_FORCE_INLINE XXH_TARGET_AVX512 void
4978XXH3_accumulate_512_avx512(void* XXH_RESTRICT acc,
4979 const void* XXH_RESTRICT input,
4980 const void* XXH_RESTRICT secret)
4981{
4982 __m512i* const xacc = (__m512i *) acc;
4983 XXH_ASSERT((((size_t)acc) & 63) == 0);
4984 XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
4985
4986 {
4987 /* data_vec = input[0]; */
4988 __m512i const data_vec = _mm512_loadu_si512 (input);
4989 /* key_vec = secret[0]; */
4990 __m512i const key_vec = _mm512_loadu_si512 (secret);
4991 /* data_key = data_vec ^ key_vec; */
4992 __m512i const data_key = _mm512_xor_si512 (data_vec, key_vec);
4993 /* data_key_lo = data_key >> 32; */
4994 __m512i const data_key_lo = _mm512_srli_epi64 (data_key, 32);
4995 /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
4996 __m512i const product = _mm512_mul_epu32 (data_key, data_key_lo);
4997 /* xacc[0] += swap(data_vec); */
4998 __m512i const data_swap = _mm512_shuffle_epi32(data_vec, (_MM_PERM_ENUM)_MM_SHUFFLE(1, 0, 3, 2));
4999 __m512i const sum = _mm512_add_epi64(*xacc, data_swap);
5000 /* xacc[0] += product; */
5001 *xacc = _mm512_add_epi64(product, sum);
5002 }
5003}
5004XXH_FORCE_INLINE XXH_TARGET_AVX512 XXH3_ACCUMULATE_TEMPLATE(avx512)
5005
5006/*
5007 * XXH3_scrambleAcc: Scrambles the accumulators to improve mixing.
5008 *
5009 * Multiplication isn't perfect, as explained by Google in HighwayHash:
5010 *
5011 * // Multiplication mixes/scrambles bytes 0-7 of the 64-bit result to
5012 * // varying degrees. In descending order of goodness, bytes
5013 * // 3 4 2 5 1 6 0 7 have quality 228 224 164 160 100 96 36 32.
5014 * // As expected, the upper and lower bytes are much worse.
5015 *
5016 * Source: https://github.com/google/highwayhash/blob/0aaf66b/highwayhash/hh_avx2.h#L291
5017 *
5018 * Since our algorithm uses a pseudorandom secret to add some variance into the
5019 * mix, we don't need to (or want to) mix as often or as much as HighwayHash does.
5020 *
5021 * This isn't as tight as XXH3_accumulate, but still written in SIMD to avoid
5022 * extraction.
5023 *
5024 * Both XXH3_64bits and XXH3_128bits use this subroutine.
5025 */
5026
5027XXH_FORCE_INLINE XXH_TARGET_AVX512 void
5028XXH3_scrambleAcc_avx512(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5029{
5030 XXH_ASSERT((((size_t)acc) & 63) == 0);
5031 XXH_STATIC_ASSERT(XXH_STRIPE_LEN == sizeof(__m512i));
5032 { __m512i* const xacc = (__m512i*) acc;
5033 const __m512i prime32 = _mm512_set1_epi32((int)XXH_PRIME32_1);
5034
5035 /* xacc[0] ^= (xacc[0] >> 47) */
5036 __m512i const acc_vec = *xacc;
5037 __m512i const shifted = _mm512_srli_epi64 (acc_vec, 47);
5038 /* xacc[0] ^= secret; */
5039 __m512i const key_vec = _mm512_loadu_si512 (secret);
5040 __m512i const data_key = _mm512_ternarylogic_epi32(key_vec, acc_vec, shifted, 0x96 /* key_vec ^ acc_vec ^ shifted */);
5041
5042 /* xacc[0] *= XXH_PRIME32_1; */
5043 __m512i const data_key_hi = _mm512_srli_epi64 (data_key, 32);
5044 __m512i const prod_lo = _mm512_mul_epu32 (data_key, prime32);
5045 __m512i const prod_hi = _mm512_mul_epu32 (data_key_hi, prime32);
5046 *xacc = _mm512_add_epi64(prod_lo, _mm512_slli_epi64(prod_hi, 32));
5047 }
5048}
5049
5050XXH_FORCE_INLINE XXH_TARGET_AVX512 void
5051XXH3_initCustomSecret_avx512(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
5052{
5053 XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 63) == 0);
5054 XXH_STATIC_ASSERT(XXH_SEC_ALIGN == 64);
5055 XXH_ASSERT(((size_t)customSecret & 63) == 0);
5056 (void)(&XXH_writeLE64);
5057 { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m512i);
5058 __m512i const seed_pos = _mm512_set1_epi64((xxh_i64)seed64);
5059 __m512i const seed = _mm512_mask_sub_epi64(seed_pos, 0xAA, _mm512_set1_epi8(0), seed_pos);
5060
5061 const __m512i* const src = (const __m512i*) ((const void*) XXH3_kSecret);
5062 __m512i* const dest = ( __m512i*) customSecret;
5063 int i;
5064 XXH_ASSERT(((size_t)src & 63) == 0); /* control alignment */
5065 XXH_ASSERT(((size_t)dest & 63) == 0);
5066 for (i=0; i < nbRounds; ++i) {
5067 dest[i] = _mm512_add_epi64(_mm512_load_si512(src + i), seed);
5068 } }
5069}
5070
5071#endif
5072
5073#if (XXH_VECTOR == XXH_AVX2) \
5074 || (defined(XXH_DISPATCH_AVX2) && XXH_DISPATCH_AVX2 != 0)
5075
5076#ifndef XXH_TARGET_AVX2
5077# define XXH_TARGET_AVX2 /* disable attribute target */
5078#endif
5079
5080XXH_FORCE_INLINE XXH_TARGET_AVX2 void
5081XXH3_accumulate_512_avx2( void* XXH_RESTRICT acc,
5082 const void* XXH_RESTRICT input,
5083 const void* XXH_RESTRICT secret)
5084{
5085 XXH_ASSERT((((size_t)acc) & 31) == 0);
5086 { __m256i* const xacc = (__m256i *) acc;
5087 /* Unaligned. This is mainly for pointer arithmetic, and because
5088 * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
5089 const __m256i* const xinput = (const __m256i *) input;
5090 /* Unaligned. This is mainly for pointer arithmetic, and because
5091 * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
5092 const __m256i* const xsecret = (const __m256i *) secret;
5093
5094 size_t i;
5095 for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
5096 /* data_vec = xinput[i]; */
5097 __m256i const data_vec = _mm256_loadu_si256 (xinput+i);
5098 /* key_vec = xsecret[i]; */
5099 __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
5100 /* data_key = data_vec ^ key_vec; */
5101 __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
5102 /* data_key_lo = data_key >> 32; */
5103 __m256i const data_key_lo = _mm256_srli_epi64 (data_key, 32);
5104 /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
5105 __m256i const product = _mm256_mul_epu32 (data_key, data_key_lo);
5106 /* xacc[i] += swap(data_vec); */
5107 __m256i const data_swap = _mm256_shuffle_epi32(data_vec, _MM_SHUFFLE(1, 0, 3, 2));
5108 __m256i const sum = _mm256_add_epi64(xacc[i], data_swap);
5109 /* xacc[i] += product; */
5110 xacc[i] = _mm256_add_epi64(product, sum);
5111 } }
5112}
5113XXH_FORCE_INLINE XXH_TARGET_AVX2 XXH3_ACCUMULATE_TEMPLATE(avx2)
5114
5115XXH_FORCE_INLINE XXH_TARGET_AVX2 void
5116XXH3_scrambleAcc_avx2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5117{
5118 XXH_ASSERT((((size_t)acc) & 31) == 0);
5119 { __m256i* const xacc = (__m256i*) acc;
5120 /* Unaligned. This is mainly for pointer arithmetic, and because
5121 * _mm256_loadu_si256 requires a const __m256i * pointer for some reason. */
5122 const __m256i* const xsecret = (const __m256i *) secret;
5123 const __m256i prime32 = _mm256_set1_epi32((int)XXH_PRIME32_1);
5124
5125 size_t i;
5126 for (i=0; i < XXH_STRIPE_LEN/sizeof(__m256i); i++) {
5127 /* xacc[i] ^= (xacc[i] >> 47) */
5128 __m256i const acc_vec = xacc[i];
5129 __m256i const shifted = _mm256_srli_epi64 (acc_vec, 47);
5130 __m256i const data_vec = _mm256_xor_si256 (acc_vec, shifted);
5131 /* xacc[i] ^= xsecret; */
5132 __m256i const key_vec = _mm256_loadu_si256 (xsecret+i);
5133 __m256i const data_key = _mm256_xor_si256 (data_vec, key_vec);
5134
5135 /* xacc[i] *= XXH_PRIME32_1; */
5136 __m256i const data_key_hi = _mm256_srli_epi64 (data_key, 32);
5137 __m256i const prod_lo = _mm256_mul_epu32 (data_key, prime32);
5138 __m256i const prod_hi = _mm256_mul_epu32 (data_key_hi, prime32);
5139 xacc[i] = _mm256_add_epi64(prod_lo, _mm256_slli_epi64(prod_hi, 32));
5140 }
5141 }
5142}
5143
5144XXH_FORCE_INLINE XXH_TARGET_AVX2 void XXH3_initCustomSecret_avx2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
5145{
5146 XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 31) == 0);
5147 XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE / sizeof(__m256i)) == 6);
5148 XXH_STATIC_ASSERT(XXH_SEC_ALIGN <= 64);
5149 (void)(&XXH_writeLE64);
5150 XXH_PREFETCH(customSecret);
5151 { __m256i const seed = _mm256_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64, (xxh_i64)(0U - seed64), (xxh_i64)seed64);
5152
5153 const __m256i* const src = (const __m256i*) ((const void*) XXH3_kSecret);
5154 __m256i* dest = ( __m256i*) customSecret;
5155
5156# if defined(__GNUC__) || defined(__clang__)
5157 /*
5158 * On GCC & Clang, marking 'dest' as modified will cause the compiler:
5159 * - do not extract the secret from sse registers in the internal loop
5160 * - use less common registers, and avoid pushing these reg into stack
5161 */
5162 XXH_COMPILER_GUARD(dest);
5163# endif
5164 XXH_ASSERT(((size_t)src & 31) == 0); /* control alignment */
5165 XXH_ASSERT(((size_t)dest & 31) == 0);
5166
5167 /* GCC -O2 need unroll loop manually */
5168 dest[0] = _mm256_add_epi64(_mm256_load_si256(src+0), seed);
5169 dest[1] = _mm256_add_epi64(_mm256_load_si256(src+1), seed);
5170 dest[2] = _mm256_add_epi64(_mm256_load_si256(src+2), seed);
5171 dest[3] = _mm256_add_epi64(_mm256_load_si256(src+3), seed);
5172 dest[4] = _mm256_add_epi64(_mm256_load_si256(src+4), seed);
5173 dest[5] = _mm256_add_epi64(_mm256_load_si256(src+5), seed);
5174 }
5175}
5176
5177#endif
5178
5179/* x86dispatch always generates SSE2 */
5180#if (XXH_VECTOR == XXH_SSE2) || defined(XXH_X86DISPATCH)
5181
5182#ifndef XXH_TARGET_SSE2
5183# define XXH_TARGET_SSE2 /* disable attribute target */
5184#endif
5185
5186XXH_FORCE_INLINE XXH_TARGET_SSE2 void
5187XXH3_accumulate_512_sse2( void* XXH_RESTRICT acc,
5188 const void* XXH_RESTRICT input,
5189 const void* XXH_RESTRICT secret)
5190{
5191 /* SSE2 is just a half-scale version of the AVX2 version. */
5192 XXH_ASSERT((((size_t)acc) & 15) == 0);
5193 { __m128i* const xacc = (__m128i *) acc;
5194 /* Unaligned. This is mainly for pointer arithmetic, and because
5195 * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
5196 const __m128i* const xinput = (const __m128i *) input;
5197 /* Unaligned. This is mainly for pointer arithmetic, and because
5198 * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
5199 const __m128i* const xsecret = (const __m128i *) secret;
5200
5201 size_t i;
5202 for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
5203 /* data_vec = xinput[i]; */
5204 __m128i const data_vec = _mm_loadu_si128 (xinput+i);
5205 /* key_vec = xsecret[i]; */
5206 __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
5207 /* data_key = data_vec ^ key_vec; */
5208 __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
5209 /* data_key_lo = data_key >> 32; */
5210 __m128i const data_key_lo = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
5211 /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
5212 __m128i const product = _mm_mul_epu32 (data_key, data_key_lo);
5213 /* xacc[i] += swap(data_vec); */
5214 __m128i const data_swap = _mm_shuffle_epi32(data_vec, _MM_SHUFFLE(1,0,3,2));
5215 __m128i const sum = _mm_add_epi64(xacc[i], data_swap);
5216 /* xacc[i] += product; */
5217 xacc[i] = _mm_add_epi64(product, sum);
5218 } }
5219}
5220XXH_FORCE_INLINE XXH_TARGET_SSE2 XXH3_ACCUMULATE_TEMPLATE(sse2)
5221
5222XXH_FORCE_INLINE XXH_TARGET_SSE2 void
5223XXH3_scrambleAcc_sse2(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5224{
5225 XXH_ASSERT((((size_t)acc) & 15) == 0);
5226 { __m128i* const xacc = (__m128i*) acc;
5227 /* Unaligned. This is mainly for pointer arithmetic, and because
5228 * _mm_loadu_si128 requires a const __m128i * pointer for some reason. */
5229 const __m128i* const xsecret = (const __m128i *) secret;
5230 const __m128i prime32 = _mm_set1_epi32((int)XXH_PRIME32_1);
5231
5232 size_t i;
5233 for (i=0; i < XXH_STRIPE_LEN/sizeof(__m128i); i++) {
5234 /* xacc[i] ^= (xacc[i] >> 47) */
5235 __m128i const acc_vec = xacc[i];
5236 __m128i const shifted = _mm_srli_epi64 (acc_vec, 47);
5237 __m128i const data_vec = _mm_xor_si128 (acc_vec, shifted);
5238 /* xacc[i] ^= xsecret[i]; */
5239 __m128i const key_vec = _mm_loadu_si128 (xsecret+i);
5240 __m128i const data_key = _mm_xor_si128 (data_vec, key_vec);
5241
5242 /* xacc[i] *= XXH_PRIME32_1; */
5243 __m128i const data_key_hi = _mm_shuffle_epi32 (data_key, _MM_SHUFFLE(0, 3, 0, 1));
5244 __m128i const prod_lo = _mm_mul_epu32 (data_key, prime32);
5245 __m128i const prod_hi = _mm_mul_epu32 (data_key_hi, prime32);
5246 xacc[i] = _mm_add_epi64(prod_lo, _mm_slli_epi64(prod_hi, 32));
5247 }
5248 }
5249}
5250
5251XXH_FORCE_INLINE XXH_TARGET_SSE2 void XXH3_initCustomSecret_sse2(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
5252{
5253 XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
5254 (void)(&XXH_writeLE64);
5255 { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / sizeof(__m128i);
5256
5257# if defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1900
5258 /* MSVC 32bit mode does not support _mm_set_epi64x before 2015 */
5259 XXH_ALIGN(16) const xxh_i64 seed64x2[2] = { (xxh_i64)seed64, (xxh_i64)(0U - seed64) };
5260 __m128i const seed = _mm_load_si128((__m128i const*)seed64x2);
5261# else
5262 __m128i const seed = _mm_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64);
5263# endif
5264 int i;
5265
5266 const void* const src16 = XXH3_kSecret;
5267 __m128i* dst16 = (__m128i*) customSecret;
5268# if defined(__GNUC__) || defined(__clang__)
5269 /*
5270 * On GCC & Clang, marking 'dest' as modified will cause the compiler:
5271 * - do not extract the secret from sse registers in the internal loop
5272 * - use less common registers, and avoid pushing these reg into stack
5273 */
5274 XXH_COMPILER_GUARD(dst16);
5275# endif
5276 XXH_ASSERT(((size_t)src16 & 15) == 0); /* control alignment */
5277 XXH_ASSERT(((size_t)dst16 & 15) == 0);
5278
5279 for (i=0; i < nbRounds; ++i) {
5280 dst16[i] = _mm_add_epi64(_mm_load_si128((const __m128i *)src16+i), seed);
5281 } }
5282}
5283
5284#endif
5285
5286#if (XXH_VECTOR == XXH_NEON)
5287
5288/* forward declarations for the scalar routines */
5289XXH_FORCE_INLINE void
5290XXH3_scalarRound(void* XXH_RESTRICT acc, void const* XXH_RESTRICT input,
5291 void const* XXH_RESTRICT secret, size_t lane);
5292
5293XXH_FORCE_INLINE void
5294XXH3_scalarScrambleRound(void* XXH_RESTRICT acc,
5295 void const* XXH_RESTRICT secret, size_t lane);
5296
5320
5321XXH_FORCE_INLINE void
5322XXH3_accumulate_512_neon( void* XXH_RESTRICT acc,
5323 const void* XXH_RESTRICT input,
5324 const void* XXH_RESTRICT secret)
5325{
5326 XXH_ASSERT((((size_t)acc) & 15) == 0);
5327 XXH_STATIC_ASSERT(XXH3_NEON_LANES > 0 && XXH3_NEON_LANES <= XXH_ACC_NB && XXH3_NEON_LANES % 2 == 0);
5328 { /* GCC for darwin arm64 does not like aliasing here */
5329 xxh_aliasing_uint64x2_t* const xacc = (xxh_aliasing_uint64x2_t*) acc;
5330 /* We don't use a uint32x4_t pointer because it causes bus errors on ARMv7. */
5331 uint8_t const* xinput = (const uint8_t *) input;
5332 uint8_t const* xsecret = (const uint8_t *) secret;
5333
5334 size_t i;
5335#ifdef __wasm_simd128__
5336 /*
5337 * On WASM SIMD128, Clang emits direct address loads when XXH3_kSecret
5338 * is constant propagated, which results in it converting it to this
5339 * inside the loop:
5340 *
5341 * a = v128.load(XXH3_kSecret + 0 + $secret_offset, offset = 0)
5342 * b = v128.load(XXH3_kSecret + 16 + $secret_offset, offset = 0)
5343 * ...
5344 *
5345 * This requires a full 32-bit address immediate (and therefore a 6 byte
5346 * instruction) as well as an add for each offset.
5347 *
5348 * Putting an asm guard prevents it from folding (at the cost of losing
5349 * the alignment hint), and uses the free offset in `v128.load` instead
5350 * of adding secret_offset each time which overall reduces code size by
5351 * about a kilobyte and improves performance.
5352 */
5353 XXH_COMPILER_GUARD(xsecret);
5354#endif
5355 /* Scalar lanes use the normal scalarRound routine */
5356 for (i = XXH3_NEON_LANES; i < XXH_ACC_NB; i++) {
5357 XXH3_scalarRound(acc, input, secret, i);
5358 }
5359 i = 0;
5360 /* 4 NEON lanes at a time. */
5361 for (; i+1 < XXH3_NEON_LANES / 2; i+=2) {
5362 /* data_vec = xinput[i]; */
5363 uint64x2_t data_vec_1 = XXH_vld1q_u64(xinput + (i * 16));
5364 uint64x2_t data_vec_2 = XXH_vld1q_u64(xinput + ((i+1) * 16));
5365 /* key_vec = xsecret[i]; */
5366 uint64x2_t key_vec_1 = XXH_vld1q_u64(xsecret + (i * 16));
5367 uint64x2_t key_vec_2 = XXH_vld1q_u64(xsecret + ((i+1) * 16));
5368 /* data_swap = swap(data_vec) */
5369 uint64x2_t data_swap_1 = vextq_u64(data_vec_1, data_vec_1, 1);
5370 uint64x2_t data_swap_2 = vextq_u64(data_vec_2, data_vec_2, 1);
5371 /* data_key = data_vec ^ key_vec; */
5372 uint64x2_t data_key_1 = veorq_u64(data_vec_1, key_vec_1);
5373 uint64x2_t data_key_2 = veorq_u64(data_vec_2, key_vec_2);
5374
5375 /*
5376 * If we reinterpret the 64x2 vectors as 32x4 vectors, we can use a
5377 * de-interleave operation for 4 lanes in 1 step with `vuzpq_u32` to
5378 * get one vector with the low 32 bits of each lane, and one vector
5379 * with the high 32 bits of each lane.
5380 *
5381 * The intrinsic returns a double vector because the original ARMv7-a
5382 * instruction modified both arguments in place. AArch64 and SIMD128 emit
5383 * two instructions from this intrinsic.
5384 *
5385 * [ dk11L | dk11H | dk12L | dk12H ] -> [ dk11L | dk12L | dk21L | dk22L ]
5386 * [ dk21L | dk21H | dk22L | dk22H ] -> [ dk11H | dk12H | dk21H | dk22H ]
5387 */
5388 uint32x4x2_t unzipped = vuzpq_u32(
5389 vreinterpretq_u32_u64(data_key_1),
5390 vreinterpretq_u32_u64(data_key_2)
5391 );
5392 /* data_key_lo = data_key & 0xFFFFFFFF */
5393 uint32x4_t data_key_lo = unzipped.val[0];
5394 /* data_key_hi = data_key >> 32 */
5395 uint32x4_t data_key_hi = unzipped.val[1];
5396 /*
5397 * Then, we can split the vectors horizontally and multiply which, as for most
5398 * widening intrinsics, have a variant that works on both high half vectors
5399 * for free on AArch64. A similar instruction is available on SIMD128.
5400 *
5401 * sum = data_swap + (u64x2) data_key_lo * (u64x2) data_key_hi
5402 */
5403 uint64x2_t sum_1 = XXH_vmlal_low_u32(data_swap_1, data_key_lo, data_key_hi);
5404 uint64x2_t sum_2 = XXH_vmlal_high_u32(data_swap_2, data_key_lo, data_key_hi);
5405 /*
5406 * Clang reorders
5407 * a += b * c; // umlal swap.2d, dkl.2s, dkh.2s
5408 * c += a; // add acc.2d, acc.2d, swap.2d
5409 * to
5410 * c += a; // add acc.2d, acc.2d, swap.2d
5411 * c += b * c; // umlal acc.2d, dkl.2s, dkh.2s
5412 *
5413 * While it would make sense in theory since the addition is faster,
5414 * for reasons likely related to umlal being limited to certain NEON
5415 * pipelines, this is worse. A compiler guard fixes this.
5416 */
5417 XXH_COMPILER_GUARD_CLANG_NEON(sum_1);
5418 XXH_COMPILER_GUARD_CLANG_NEON(sum_2);
5419 /* xacc[i] = acc_vec + sum; */
5420 xacc[i] = vaddq_u64(xacc[i], sum_1);
5421 xacc[i+1] = vaddq_u64(xacc[i+1], sum_2);
5422 }
5423 /* Operate on the remaining NEON lanes 2 at a time. */
5424 for (; i < XXH3_NEON_LANES / 2; i++) {
5425 /* data_vec = xinput[i]; */
5426 uint64x2_t data_vec = XXH_vld1q_u64(xinput + (i * 16));
5427 /* key_vec = xsecret[i]; */
5428 uint64x2_t key_vec = XXH_vld1q_u64(xsecret + (i * 16));
5429 /* acc_vec_2 = swap(data_vec) */
5430 uint64x2_t data_swap = vextq_u64(data_vec, data_vec, 1);
5431 /* data_key = data_vec ^ key_vec; */
5432 uint64x2_t data_key = veorq_u64(data_vec, key_vec);
5433 /* For two lanes, just use VMOVN and VSHRN. */
5434 /* data_key_lo = data_key & 0xFFFFFFFF; */
5435 uint32x2_t data_key_lo = vmovn_u64(data_key);
5436 /* data_key_hi = data_key >> 32; */
5437 uint32x2_t data_key_hi = vshrn_n_u64(data_key, 32);
5438 /* sum = data_swap + (u64x2) data_key_lo * (u64x2) data_key_hi; */
5439 uint64x2_t sum = vmlal_u32(data_swap, data_key_lo, data_key_hi);
5440 /* Same Clang workaround as before */
5441 XXH_COMPILER_GUARD_CLANG_NEON(sum);
5442 /* xacc[i] = acc_vec + sum; */
5443 xacc[i] = vaddq_u64 (xacc[i], sum);
5444 }
5445 }
5446}
5447XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(neon)
5448
5449XXH_FORCE_INLINE void
5450XXH3_scrambleAcc_neon(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5451{
5452 XXH_ASSERT((((size_t)acc) & 15) == 0);
5453
5454 { xxh_aliasing_uint64x2_t* xacc = (xxh_aliasing_uint64x2_t*) acc;
5455 uint8_t const* xsecret = (uint8_t const*) secret;
5456
5457 size_t i;
5458 /* WASM uses operator overloads and doesn't need these. */
5459#ifndef __wasm_simd128__
5460 /* { prime32_1, prime32_1 } */
5461 uint32x2_t const kPrimeLo = vdup_n_u32(XXH_PRIME32_1);
5462 /* { 0, prime32_1, 0, prime32_1 } */
5463 uint32x4_t const kPrimeHi = vreinterpretq_u32_u64(vdupq_n_u64((xxh_u64)XXH_PRIME32_1 << 32));
5464#endif
5465
5466 /* AArch64 uses both scalar and neon at the same time */
5467 for (i = XXH3_NEON_LANES; i < XXH_ACC_NB; i++) {
5468 XXH3_scalarScrambleRound(acc, secret, i);
5469 }
5470 for (i=0; i < XXH3_NEON_LANES / 2; i++) {
5471 /* xacc[i] ^= (xacc[i] >> 47); */
5472 uint64x2_t acc_vec = xacc[i];
5473 uint64x2_t shifted = vshrq_n_u64(acc_vec, 47);
5474 uint64x2_t data_vec = veorq_u64(acc_vec, shifted);
5475
5476 /* xacc[i] ^= xsecret[i]; */
5477 uint64x2_t key_vec = XXH_vld1q_u64(xsecret + (i * 16));
5478 uint64x2_t data_key = veorq_u64(data_vec, key_vec);
5479 /* xacc[i] *= XXH_PRIME32_1 */
5480#ifdef __wasm_simd128__
5481 /* SIMD128 has multiply by u64x2, use it instead of expanding and scalarizing */
5482 xacc[i] = data_key * XXH_PRIME32_1;
5483#else
5484 /*
5485 * Expanded version with portable NEON intrinsics
5486 *
5487 * lo(x) * lo(y) + (hi(x) * lo(y) << 32)
5488 *
5489 * prod_hi = hi(data_key) * lo(prime) << 32
5490 *
5491 * Since we only need 32 bits of this multiply a trick can be used, reinterpreting the vector
5492 * as a uint32x4_t and multiplying by { 0, prime, 0, prime } to cancel out the unwanted bits
5493 * and avoid the shift.
5494 */
5495 uint32x4_t prod_hi = vmulq_u32 (vreinterpretq_u32_u64(data_key), kPrimeHi);
5496 /* Extract low bits for vmlal_u32 */
5497 uint32x2_t data_key_lo = vmovn_u64(data_key);
5498 /* xacc[i] = prod_hi + lo(data_key) * XXH_PRIME32_1; */
5499 xacc[i] = vmlal_u32(vreinterpretq_u64_u32(prod_hi), data_key_lo, kPrimeLo);
5500#endif
5501 }
5502 }
5503}
5504#endif
5505
5506#if (XXH_VECTOR == XXH_VSX)
5507
5508XXH_FORCE_INLINE void
5509XXH3_accumulate_512_vsx( void* XXH_RESTRICT acc,
5510 const void* XXH_RESTRICT input,
5511 const void* XXH_RESTRICT secret)
5512{
5513 /* presumed aligned */
5514 xxh_aliasing_u64x2* const xacc = (xxh_aliasing_u64x2*) acc;
5515 xxh_u8 const* const xinput = (xxh_u8 const*) input; /* no alignment restriction */
5516 xxh_u8 const* const xsecret = (xxh_u8 const*) secret; /* no alignment restriction */
5517 xxh_u64x2 const v32 = { 32, 32 };
5518 size_t i;
5519 for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
5520 /* data_vec = xinput[i]; */
5521 xxh_u64x2 const data_vec = XXH_vec_loadu(xinput + 16*i);
5522 /* key_vec = xsecret[i]; */
5523 xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + 16*i);
5524 xxh_u64x2 const data_key = data_vec ^ key_vec;
5525 /* shuffled = (data_key << 32) | (data_key >> 32); */
5526 xxh_u32x4 const shuffled = (xxh_u32x4)vec_rl(data_key, v32);
5527 /* product = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)shuffled & 0xFFFFFFFF); */
5528 xxh_u64x2 const product = XXH_vec_mulo((xxh_u32x4)data_key, shuffled);
5529 /* acc_vec = xacc[i]; */
5530 xxh_u64x2 acc_vec = xacc[i];
5531 acc_vec += product;
5532
5533 /* swap high and low halves */
5534#ifdef __s390x__
5535 acc_vec += vec_permi(data_vec, data_vec, 2);
5536#else
5537 acc_vec += vec_xxpermdi(data_vec, data_vec, 2);
5538#endif
5539 xacc[i] = acc_vec;
5540 }
5541}
5542XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(vsx)
5543
5544XXH_FORCE_INLINE void
5545XXH3_scrambleAcc_vsx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5546{
5547 XXH_ASSERT((((size_t)acc) & 15) == 0);
5548
5549 { xxh_aliasing_u64x2* const xacc = (xxh_aliasing_u64x2*) acc;
5550 const xxh_u8* const xsecret = (const xxh_u8*) secret;
5551 /* constants */
5552 xxh_u64x2 const v32 = { 32, 32 };
5553 xxh_u64x2 const v47 = { 47, 47 };
5554 xxh_u32x4 const prime = { XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1, XXH_PRIME32_1 };
5555 size_t i;
5556 for (i = 0; i < XXH_STRIPE_LEN / sizeof(xxh_u64x2); i++) {
5557 /* xacc[i] ^= (xacc[i] >> 47); */
5558 xxh_u64x2 const acc_vec = xacc[i];
5559 xxh_u64x2 const data_vec = acc_vec ^ (acc_vec >> v47);
5560
5561 /* xacc[i] ^= xsecret[i]; */
5562 xxh_u64x2 const key_vec = XXH_vec_loadu(xsecret + 16*i);
5563 xxh_u64x2 const data_key = data_vec ^ key_vec;
5564
5565 /* xacc[i] *= XXH_PRIME32_1 */
5566 /* prod_lo = ((xxh_u64x2)data_key & 0xFFFFFFFF) * ((xxh_u64x2)prime & 0xFFFFFFFF); */
5567 xxh_u64x2 const prod_even = XXH_vec_mule((xxh_u32x4)data_key, prime);
5568 /* prod_hi = ((xxh_u64x2)data_key >> 32) * ((xxh_u64x2)prime >> 32); */
5569 xxh_u64x2 const prod_odd = XXH_vec_mulo((xxh_u32x4)data_key, prime);
5570 xacc[i] = prod_odd + (prod_even << v32);
5571 } }
5572}
5573
5574#endif
5575
5576#if (XXH_VECTOR == XXH_SVE)
5577
5578XXH_FORCE_INLINE void
5579XXH3_accumulate_512_sve( void* XXH_RESTRICT acc,
5580 const void* XXH_RESTRICT input,
5581 const void* XXH_RESTRICT secret)
5582{
5583 uint64_t *xacc = (uint64_t *)acc;
5584 const uint64_t *xinput = (const uint64_t *)(const void *)input;
5585 const uint64_t *xsecret = (const uint64_t *)(const void *)secret;
5586 svuint64_t kSwap = sveor_n_u64_z(svptrue_b64(), svindex_u64(0, 1), 1);
5587 uint64_t element_count = svcntd();
5588 if (element_count >= 8) {
5589 svbool_t mask = svptrue_pat_b64(SV_VL8);
5590 svuint64_t vacc = svld1_u64(mask, xacc);
5591 ACCRND(vacc, 0);
5592 svst1_u64(mask, xacc, vacc);
5593 } else if (element_count == 2) { /* sve128 */
5594 svbool_t mask = svptrue_pat_b64(SV_VL2);
5595 svuint64_t acc0 = svld1_u64(mask, xacc + 0);
5596 svuint64_t acc1 = svld1_u64(mask, xacc + 2);
5597 svuint64_t acc2 = svld1_u64(mask, xacc + 4);
5598 svuint64_t acc3 = svld1_u64(mask, xacc + 6);
5599 ACCRND(acc0, 0);
5600 ACCRND(acc1, 2);
5601 ACCRND(acc2, 4);
5602 ACCRND(acc3, 6);
5603 svst1_u64(mask, xacc + 0, acc0);
5604 svst1_u64(mask, xacc + 2, acc1);
5605 svst1_u64(mask, xacc + 4, acc2);
5606 svst1_u64(mask, xacc + 6, acc3);
5607 } else {
5608 svbool_t mask = svptrue_pat_b64(SV_VL4);
5609 svuint64_t acc0 = svld1_u64(mask, xacc + 0);
5610 svuint64_t acc1 = svld1_u64(mask, xacc + 4);
5611 ACCRND(acc0, 0);
5612 ACCRND(acc1, 4);
5613 svst1_u64(mask, xacc + 0, acc0);
5614 svst1_u64(mask, xacc + 4, acc1);
5615 }
5616}
5617
5618XXH_FORCE_INLINE void
5619XXH3_accumulate_sve(xxh_u64* XXH_RESTRICT acc,
5620 const xxh_u8* XXH_RESTRICT input,
5621 const xxh_u8* XXH_RESTRICT secret,
5622 size_t nbStripes)
5623{
5624 if (nbStripes != 0) {
5625 uint64_t *xacc = (uint64_t *)acc;
5626 const uint64_t *xinput = (const uint64_t *)(const void *)input;
5627 const uint64_t *xsecret = (const uint64_t *)(const void *)secret;
5628 svuint64_t kSwap = sveor_n_u64_z(svptrue_b64(), svindex_u64(0, 1), 1);
5629 uint64_t element_count = svcntd();
5630 if (element_count >= 8) {
5631 svbool_t mask = svptrue_pat_b64(SV_VL8);
5632 svuint64_t vacc = svld1_u64(mask, xacc + 0);
5633 do {
5634 /* svprfd(svbool_t, void *, enum svfprop); */
5635 svprfd(mask, xinput + 128, SV_PLDL1STRM);
5636 ACCRND(vacc, 0);
5637 xinput += 8;
5638 xsecret += 1;
5639 nbStripes--;
5640 } while (nbStripes != 0);
5641
5642 svst1_u64(mask, xacc + 0, vacc);
5643 } else if (element_count == 2) { /* sve128 */
5644 svbool_t mask = svptrue_pat_b64(SV_VL2);
5645 svuint64_t acc0 = svld1_u64(mask, xacc + 0);
5646 svuint64_t acc1 = svld1_u64(mask, xacc + 2);
5647 svuint64_t acc2 = svld1_u64(mask, xacc + 4);
5648 svuint64_t acc3 = svld1_u64(mask, xacc + 6);
5649 do {
5650 svprfd(mask, xinput + 128, SV_PLDL1STRM);
5651 ACCRND(acc0, 0);
5652 ACCRND(acc1, 2);
5653 ACCRND(acc2, 4);
5654 ACCRND(acc3, 6);
5655 xinput += 8;
5656 xsecret += 1;
5657 nbStripes--;
5658 } while (nbStripes != 0);
5659
5660 svst1_u64(mask, xacc + 0, acc0);
5661 svst1_u64(mask, xacc + 2, acc1);
5662 svst1_u64(mask, xacc + 4, acc2);
5663 svst1_u64(mask, xacc + 6, acc3);
5664 } else {
5665 svbool_t mask = svptrue_pat_b64(SV_VL4);
5666 svuint64_t acc0 = svld1_u64(mask, xacc + 0);
5667 svuint64_t acc1 = svld1_u64(mask, xacc + 4);
5668 do {
5669 svprfd(mask, xinput + 128, SV_PLDL1STRM);
5670 ACCRND(acc0, 0);
5671 ACCRND(acc1, 4);
5672 xinput += 8;
5673 xsecret += 1;
5674 nbStripes--;
5675 } while (nbStripes != 0);
5676
5677 svst1_u64(mask, xacc + 0, acc0);
5678 svst1_u64(mask, xacc + 4, acc1);
5679 }
5680 }
5681}
5682
5683#endif
5684
5685#if (XXH_VECTOR == XXH_LSX)
5686#define _LSX_SHUFFLE(z, y, x, w) (((z) << 6) | ((y) << 4) | ((x) << 2) | (w))
5687
5688XXH_FORCE_INLINE void
5689XXH3_accumulate_512_lsx( void* XXH_RESTRICT acc,
5690 const void* XXH_RESTRICT input,
5691 const void* XXH_RESTRICT secret)
5692{
5693 XXH_ASSERT((((size_t)acc) & 15) == 0);
5694 {
5695 __m128i* const xacc = (__m128i *) acc;
5696 const __m128i* const xinput = (const __m128i *) input;
5697 const __m128i* const xsecret = (const __m128i *) secret;
5698
5699 for (size_t i = 0; i < XXH_STRIPE_LEN / sizeof(__m128i); i++) {
5700 /* data_vec = xinput[i]; */
5701 __m128i const data_vec = __lsx_vld(xinput + i, 0);
5702 /* key_vec = xsecret[i]; */
5703 __m128i const key_vec = __lsx_vld(xsecret + i, 0);
5704 /* data_key = data_vec ^ key_vec; */
5705 __m128i const data_key = __lsx_vxor_v(data_vec, key_vec);
5706 /* data_key_lo = data_key >> 32; */
5707 __m128i const data_key_lo = __lsx_vsrli_d(data_key, 32);
5708 // __m128i const data_key_lo = __lsx_vsrli_d(data_key, 32);
5709 /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
5710 __m128i const product = __lsx_vmulwev_d_wu(data_key, data_key_lo);
5711 /* xacc[i] += swap(data_vec); */
5712 __m128i const data_swap = __lsx_vshuf4i_w(data_vec, _LSX_SHUFFLE(1, 0, 3, 2));
5713 __m128i const sum = __lsx_vadd_d(xacc[i], data_swap);
5714 /* xacc[i] += product; */
5715 xacc[i] = __lsx_vadd_d(product, sum);
5716 }
5717 }
5718}
5719XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(lsx)
5720
5721XXH_FORCE_INLINE void
5722XXH3_scrambleAcc_lsx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5723{
5724 XXH_ASSERT((((size_t)acc) & 15) == 0);
5725 {
5726 __m128i* const xacc = (__m128i*) acc;
5727 const __m128i* const xsecret = (const __m128i *) secret;
5728 const __m128i prime32 = __lsx_vreplgr2vr_d(XXH_PRIME32_1);
5729
5730 for (size_t i = 0; i < XXH_STRIPE_LEN / sizeof(__m128i); i++) {
5731 /* xacc[i] ^= (xacc[i] >> 47) */
5732 __m128i const acc_vec = xacc[i];
5733 __m128i const shifted = __lsx_vsrli_d(acc_vec, 47);
5734 __m128i const data_vec = __lsx_vxor_v(acc_vec, shifted);
5735 /* xacc[i] ^= xsecret[i]; */
5736 __m128i const key_vec = __lsx_vld(xsecret + i, 0);
5737 __m128i const data_key = __lsx_vxor_v(data_vec, key_vec);
5738
5739 /* xacc[i] *= XXH_PRIME32_1; */
5740 xacc[i] = __lsx_vmul_d(data_key, prime32);
5741 }
5742 }
5743}
5744
5745#endif
5746
5747#if (XXH_VECTOR == XXH_LASX)
5748#define _LASX_SHUFFLE(z, y, x, w) (((z) << 6) | ((y) << 4) | ((x) << 2) | (w))
5749
5750XXH_FORCE_INLINE void
5751XXH3_accumulate_512_lasx( void* XXH_RESTRICT acc,
5752 const void* XXH_RESTRICT input,
5753 const void* XXH_RESTRICT secret)
5754{
5755 XXH_ASSERT((((size_t)acc) & 31) == 0);
5756 {
5757 __m256i* const xacc = (__m256i *) acc;
5758 const __m256i* const xinput = (const __m256i *) input;
5759 const __m256i* const xsecret = (const __m256i *) secret;
5760
5761 for (size_t i = 0; i < XXH_STRIPE_LEN / sizeof(__m256i); i++) {
5762 /* data_vec = xinput[i]; */
5763 __m256i const data_vec = __lasx_xvld(xinput + i, 0);
5764 /* key_vec = xsecret[i]; */
5765 __m256i const key_vec = __lasx_xvld(xsecret + i, 0);
5766 /* data_key = data_vec ^ key_vec; */
5767 __m256i const data_key = __lasx_xvxor_v(data_vec, key_vec);
5768 /* data_key_lo = data_key >> 32; */
5769 __m256i const data_key_lo = __lasx_xvsrli_d(data_key, 32);
5770 // __m256i const data_key_lo = __lasx_xvsrli_d(data_key, 32);
5771 /* product = (data_key & 0xffffffff) * (data_key_lo & 0xffffffff); */
5772 __m256i const product = __lasx_xvmulwev_d_wu(data_key, data_key_lo);
5773 /* xacc[i] += swap(data_vec); */
5774 __m256i const data_swap = __lasx_xvshuf4i_w(data_vec, _LASX_SHUFFLE(1, 0, 3, 2));
5775 __m256i const sum = __lasx_xvadd_d(xacc[i], data_swap);
5776 /* xacc[i] += product; */
5777 xacc[i] = __lasx_xvadd_d(product, sum);
5778 }
5779 }
5780}
5781XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(lasx)
5782
5783XXH_FORCE_INLINE void
5784XXH3_scrambleAcc_lasx(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5785{
5786 XXH_ASSERT((((size_t)acc) & 31) == 0);
5787 {
5788 __m256i* const xacc = (__m256i*) acc;
5789 const __m256i* const xsecret = (const __m256i *) secret;
5790 const __m256i prime32 = __lasx_xvreplgr2vr_d(XXH_PRIME32_1);
5791
5792 for (size_t i = 0; i < XXH_STRIPE_LEN / sizeof(__m256i); i++) {
5793 /* xacc[i] ^= (xacc[i] >> 47) */
5794 __m256i const acc_vec = xacc[i];
5795 __m256i const shifted = __lasx_xvsrli_d(acc_vec, 47);
5796 __m256i const data_vec = __lasx_xvxor_v(acc_vec, shifted);
5797 /* xacc[i] ^= xsecret[i]; */
5798 __m256i const key_vec = __lasx_xvld(xsecret + i, 0);
5799 __m256i const data_key = __lasx_xvxor_v(data_vec, key_vec);
5800
5801 /* xacc[i] *= XXH_PRIME32_1; */
5802 xacc[i] = __lasx_xvmul_d(data_key, prime32);
5803 }
5804 }
5805}
5806
5807#endif
5808
5809/* scalar variants - universal */
5810
5811#if defined(__aarch64__) && (defined(__GNUC__) || defined(__clang__))
5812/*
5813 * In XXH3_scalarRound(), GCC and Clang have a similar codegen issue, where they
5814 * emit an excess mask and a full 64-bit multiply-add (MADD X-form).
5815 *
5816 * While this might not seem like much, as AArch64 is a 64-bit architecture, only
5817 * big Cortex designs have a full 64-bit multiplier.
5818 *
5819 * On the little cores, the smaller 32-bit multiplier is used, and full 64-bit
5820 * multiplies expand to 2-3 multiplies in microcode. This has a major penalty
5821 * of up to 4 latency cycles and 2 stall cycles in the multiply pipeline.
5822 *
5823 * Thankfully, AArch64 still provides the 32-bit long multiply-add (UMADDL) which does
5824 * not have this penalty and does the mask automatically.
5825 */
5826XXH_FORCE_INLINE xxh_u64
5827XXH_mult32to64_add64(xxh_u64 lhs, xxh_u64 rhs, xxh_u64 acc)
5828{
5829 xxh_u64 ret;
5830 /* note: %x = 64-bit register, %w = 32-bit register */
5831 __asm__("umaddl %x0, %w1, %w2, %x3" : "=r" (ret) : "r" (lhs), "r" (rhs), "r" (acc));
5832 return ret;
5833}
5834#else
5835XXH_FORCE_INLINE xxh_u64
5836XXH_mult32to64_add64(xxh_u64 lhs, xxh_u64 rhs, xxh_u64 acc)
5837{
5838 return XXH_mult32to64((xxh_u32)lhs, (xxh_u32)rhs) + acc;
5839}
5840#endif
5841
5849XXH_FORCE_INLINE void
5850XXH3_scalarRound(void* XXH_RESTRICT acc,
5851 void const* XXH_RESTRICT input,
5852 void const* XXH_RESTRICT secret,
5853 size_t lane)
5854{
5855 xxh_u64* xacc = (xxh_u64*) acc;
5856 xxh_u8 const* xinput = (xxh_u8 const*) input;
5857 xxh_u8 const* xsecret = (xxh_u8 const*) secret;
5858 XXH_ASSERT(lane < XXH_ACC_NB);
5859 XXH_ASSERT(((size_t)acc & (XXH_ACC_ALIGN-1)) == 0);
5860 {
5861 xxh_u64 const data_val = XXH_readLE64(xinput + lane * 8);
5862 xxh_u64 const data_key = data_val ^ XXH_readLE64(xsecret + lane * 8);
5863 xacc[lane ^ 1] += data_val; /* swap adjacent lanes */
5864 xacc[lane] = XXH_mult32to64_add64(data_key /* & 0xFFFFFFFF */, data_key >> 32, xacc[lane]);
5865 }
5866}
5867
5872XXH_FORCE_INLINE void
5873XXH3_accumulate_512_scalar(void* XXH_RESTRICT acc,
5874 const void* XXH_RESTRICT input,
5875 const void* XXH_RESTRICT secret)
5876{
5877 size_t i;
5878 /* ARM GCC refuses to unroll this loop, resulting in a 24% slowdown on ARMv6. */
5879#if defined(__GNUC__) && !defined(__clang__) \
5880 && (defined(__arm__) || defined(__thumb2__)) \
5881 && defined(__ARM_FEATURE_UNALIGNED) /* no unaligned access just wastes bytes */ \
5882 && XXH_SIZE_OPT <= 0
5883# pragma GCC unroll 8
5884#endif
5885 for (i=0; i < XXH_ACC_NB; i++) {
5886 XXH3_scalarRound(acc, input, secret, i);
5887 }
5888}
5889XXH_FORCE_INLINE XXH3_ACCUMULATE_TEMPLATE(scalar)
5890
5891
5898XXH_FORCE_INLINE void
5899XXH3_scalarScrambleRound(void* XXH_RESTRICT acc,
5900 void const* XXH_RESTRICT secret,
5901 size_t lane)
5902{
5903 xxh_u64* const xacc = (xxh_u64*) acc; /* presumed aligned */
5904 const xxh_u8* const xsecret = (const xxh_u8*) secret; /* no alignment restriction */
5905 XXH_ASSERT((((size_t)acc) & (XXH_ACC_ALIGN-1)) == 0);
5906 XXH_ASSERT(lane < XXH_ACC_NB);
5907 {
5908 xxh_u64 const key64 = XXH_readLE64(xsecret + lane * 8);
5909 xxh_u64 acc64 = xacc[lane];
5910 acc64 = XXH_xorshift64(acc64, 47);
5911 acc64 ^= key64;
5912 acc64 *= XXH_PRIME32_1;
5913 xacc[lane] = acc64;
5914 }
5915}
5916
5921XXH_FORCE_INLINE void
5922XXH3_scrambleAcc_scalar(void* XXH_RESTRICT acc, const void* XXH_RESTRICT secret)
5923{
5924 size_t i;
5925 for (i=0; i < XXH_ACC_NB; i++) {
5926 XXH3_scalarScrambleRound(acc, secret, i);
5927 }
5928}
5929
5930XXH_FORCE_INLINE void
5931XXH3_initCustomSecret_scalar(void* XXH_RESTRICT customSecret, xxh_u64 seed64)
5932{
5933 /*
5934 * We need a separate pointer for the hack below,
5935 * which requires a non-const pointer.
5936 * Any decent compiler will optimize this out otherwise.
5937 */
5938 const xxh_u8* kSecretPtr = XXH3_kSecret;
5939 XXH_STATIC_ASSERT((XXH_SECRET_DEFAULT_SIZE & 15) == 0);
5940
5941#if defined(__GNUC__) && defined(__aarch64__)
5942 /*
5943 * UGLY HACK:
5944 * GCC and Clang generate a bunch of MOV/MOVK pairs for aarch64, and they are
5945 * placed sequentially, in order, at the top of the unrolled loop.
5946 *
5947 * While MOVK is great for generating constants (2 cycles for a 64-bit
5948 * constant compared to 4 cycles for LDR), it fights for bandwidth with
5949 * the arithmetic instructions.
5950 *
5951 * I L S
5952 * MOVK
5953 * MOVK
5954 * MOVK
5955 * MOVK
5956 * ADD
5957 * SUB STR
5958 * STR
5959 * By forcing loads from memory (as the asm line causes the compiler to assume
5960 * that XXH3_kSecretPtr has been changed), the pipelines are used more
5961 * efficiently:
5962 * I L S
5963 * LDR
5964 * ADD LDR
5965 * SUB STR
5966 * STR
5967 *
5968 * See XXH3_NEON_LANES for details on the pipsline.
5969 *
5970 * XXH3_64bits_withSeed, len == 256, Snapdragon 835
5971 * without hack: 2654.4 MB/s
5972 * with hack: 3202.9 MB/s
5973 */
5974 XXH_COMPILER_GUARD(kSecretPtr);
5975#endif
5976 { int const nbRounds = XXH_SECRET_DEFAULT_SIZE / 16;
5977 int i;
5978 for (i=0; i < nbRounds; i++) {
5979 /*
5980 * The asm hack causes the compiler to assume that kSecretPtr aliases with
5981 * customSecret, and on aarch64, this prevented LDP from merging two
5982 * loads together for free. Putting the loads together before the stores
5983 * properly generates LDP.
5984 */
5985 xxh_u64 lo = XXH_readLE64(kSecretPtr + 16*i) + seed64;
5986 xxh_u64 hi = XXH_readLE64(kSecretPtr + 16*i + 8) - seed64;
5987 XXH_writeLE64((xxh_u8*)customSecret + 16*i, lo);
5988 XXH_writeLE64((xxh_u8*)customSecret + 16*i + 8, hi);
5989 } }
5990}
5991
5992
5993typedef void (*XXH3_f_accumulate)(xxh_u64* XXH_RESTRICT, const xxh_u8* XXH_RESTRICT, const xxh_u8* XXH_RESTRICT, size_t);
5994typedef void (*XXH3_f_scrambleAcc)(void* XXH_RESTRICT, const void*);
5995typedef void (*XXH3_f_initCustomSecret)(void* XXH_RESTRICT, xxh_u64);
5996
5997
5998#if (XXH_VECTOR == XXH_AVX512)
5999
6000#define XXH3_accumulate_512 XXH3_accumulate_512_avx512
6001#define XXH3_accumulate XXH3_accumulate_avx512
6002#define XXH3_scrambleAcc XXH3_scrambleAcc_avx512
6003#define XXH3_initCustomSecret XXH3_initCustomSecret_avx512
6004
6005#elif (XXH_VECTOR == XXH_AVX2)
6006
6007#define XXH3_accumulate_512 XXH3_accumulate_512_avx2
6008#define XXH3_accumulate XXH3_accumulate_avx2
6009#define XXH3_scrambleAcc XXH3_scrambleAcc_avx2
6010#define XXH3_initCustomSecret XXH3_initCustomSecret_avx2
6011
6012#elif (XXH_VECTOR == XXH_SSE2)
6013
6014#define XXH3_accumulate_512 XXH3_accumulate_512_sse2
6015#define XXH3_accumulate XXH3_accumulate_sse2
6016#define XXH3_scrambleAcc XXH3_scrambleAcc_sse2
6017#define XXH3_initCustomSecret XXH3_initCustomSecret_sse2
6018
6019#elif (XXH_VECTOR == XXH_NEON)
6020
6021#define XXH3_accumulate_512 XXH3_accumulate_512_neon
6022#define XXH3_accumulate XXH3_accumulate_neon
6023#define XXH3_scrambleAcc XXH3_scrambleAcc_neon
6024#define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6025
6026#elif (XXH_VECTOR == XXH_VSX)
6027
6028#define XXH3_accumulate_512 XXH3_accumulate_512_vsx
6029#define XXH3_accumulate XXH3_accumulate_vsx
6030#define XXH3_scrambleAcc XXH3_scrambleAcc_vsx
6031#define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6032
6033#elif (XXH_VECTOR == XXH_SVE)
6034#define XXH3_accumulate_512 XXH3_accumulate_512_sve
6035#define XXH3_accumulate XXH3_accumulate_sve
6036#define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
6037#define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6038
6039#elif (XXH_VECTOR == XXH_LASX)
6040#define XXH3_accumulate_512 XXH3_accumulate_512_lasx
6041#define XXH3_accumulate XXH3_accumulate_lasx
6042#define XXH3_scrambleAcc XXH3_scrambleAcc_lasx
6043#define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6044
6045#elif (XXH_VECTOR == XXH_LSX)
6046#define XXH3_accumulate_512 XXH3_accumulate_512_lsx
6047#define XXH3_accumulate XXH3_accumulate_lsx
6048#define XXH3_scrambleAcc XXH3_scrambleAcc_lsx
6049#define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6050
6051#else /* scalar */
6052
6053#define XXH3_accumulate_512 XXH3_accumulate_512_scalar
6054#define XXH3_accumulate XXH3_accumulate_scalar
6055#define XXH3_scrambleAcc XXH3_scrambleAcc_scalar
6056#define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6057
6058#endif
6059
6060#if XXH_SIZE_OPT >= 1 /* don't do SIMD for initialization */
6061# undef XXH3_initCustomSecret
6062# define XXH3_initCustomSecret XXH3_initCustomSecret_scalar
6063#endif
6064
6065XXH_FORCE_INLINE void
6066XXH3_hashLong_internal_loop(xxh_u64* XXH_RESTRICT acc,
6067 const xxh_u8* XXH_RESTRICT input, size_t len,
6068 const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
6069 XXH3_f_accumulate f_acc,
6070 XXH3_f_scrambleAcc f_scramble)
6071{
6072 size_t const nbStripesPerBlock = (secretSize - XXH_STRIPE_LEN) / XXH_SECRET_CONSUME_RATE;
6073 size_t const block_len = XXH_STRIPE_LEN * nbStripesPerBlock;
6074 size_t const nb_blocks = (len - 1) / block_len;
6075
6076 size_t n;
6077
6078 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
6079
6080 for (n = 0; n < nb_blocks; n++) {
6081 f_acc(acc, input + n*block_len, secret, nbStripesPerBlock);
6082 f_scramble(acc, secret + secretSize - XXH_STRIPE_LEN);
6083 }
6084
6085 /* last partial block */
6086 XXH_ASSERT(len > XXH_STRIPE_LEN);
6087 { size_t const nbStripes = ((len - 1) - (block_len * nb_blocks)) / XXH_STRIPE_LEN;
6088 XXH_ASSERT(nbStripes <= (secretSize / XXH_SECRET_CONSUME_RATE));
6089 f_acc(acc, input + nb_blocks*block_len, secret, nbStripes);
6090
6091 /* last stripe */
6092 { const xxh_u8* const p = input + len - XXH_STRIPE_LEN;
6093#define XXH_SECRET_LASTACC_START 7 /* not aligned on 8, last secret is different from acc & scrambler */
6094 XXH3_accumulate_512(acc, p, secret + secretSize - XXH_STRIPE_LEN - XXH_SECRET_LASTACC_START);
6095 } }
6096}
6097
6098XXH_FORCE_INLINE xxh_u64
6099XXH3_mix2Accs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret)
6100{
6101 return XXH3_mul128_fold64(
6102 acc[0] ^ XXH_readLE64(secret),
6103 acc[1] ^ XXH_readLE64(secret+8) );
6104}
6105
6106static XXH_PUREF XXH64_hash_t
6107XXH3_mergeAccs(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, xxh_u64 start)
6108{
6109 xxh_u64 result64 = start;
6110 size_t i = 0;
6111
6112 for (i = 0; i < 4; i++) {
6113 result64 += XXH3_mix2Accs(acc+2*i, secret + 16*i);
6114#if defined(__clang__) /* Clang */ \
6115 && (defined(__arm__) || defined(__thumb__)) /* ARMv7 */ \
6116 && (defined(__ARM_NEON) || defined(__ARM_NEON__)) /* NEON */ \
6117 && !defined(XXH_ENABLE_AUTOVECTORIZE) /* Define to disable */
6118 /*
6119 * UGLY HACK:
6120 * Prevent autovectorization on Clang ARMv7-a. Exact same problem as
6121 * the one in XXH3_len_129to240_64b. Speeds up shorter keys > 240b.
6122 * XXH3_64bits, len == 256, Snapdragon 835:
6123 * without hack: 2063.7 MB/s
6124 * with hack: 2560.7 MB/s
6125 */
6126 XXH_COMPILER_GUARD(result64);
6127#endif
6128 }
6129
6130 return XXH3_avalanche(result64);
6131}
6132
6133/* do not align on 8, so that the secret is different from the accumulator */
6134#define XXH_SECRET_MERGEACCS_START 11
6135
6136static XXH_PUREF XXH64_hash_t
6137XXH3_finalizeLong_64b(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, xxh_u64 len)
6138{
6139 return XXH3_mergeAccs(acc, secret + XXH_SECRET_MERGEACCS_START, len * XXH_PRIME64_1);
6140}
6141
6142#define XXH3_INIT_ACC { XXH_PRIME32_3, XXH_PRIME64_1, XXH_PRIME64_2, XXH_PRIME64_3, \
6143 XXH_PRIME64_4, XXH_PRIME32_2, XXH_PRIME64_5, XXH_PRIME32_1 }
6144
6145XXH_FORCE_INLINE XXH64_hash_t
6146XXH3_hashLong_64b_internal(const void* XXH_RESTRICT input, size_t len,
6147 const void* XXH_RESTRICT secret, size_t secretSize,
6148 XXH3_f_accumulate f_acc,
6149 XXH3_f_scrambleAcc f_scramble)
6150{
6151 XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
6152
6153 XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, (const xxh_u8*)secret, secretSize, f_acc, f_scramble);
6154
6155 /* converge into final hash */
6156 XXH_STATIC_ASSERT(sizeof(acc) == 64);
6157 XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
6158 return XXH3_finalizeLong_64b(acc, (const xxh_u8*)secret, (xxh_u64)len);
6159}
6160
6161/*
6162 * It's important for performance to transmit secret's size (when it's static)
6163 * so that the compiler can properly optimize the vectorized loop.
6164 * This makes a big performance difference for "medium" keys (<1 KB) when using AVX instruction set.
6165 * When the secret size is unknown, or on GCC 12 where the mix of NO_INLINE and FORCE_INLINE
6166 * breaks -Og, this is XXH_NO_INLINE.
6167 */
6168XXH3_WITH_SECRET_INLINE XXH64_hash_t
6169XXH3_hashLong_64b_withSecret(const void* XXH_RESTRICT input, size_t len,
6170 XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
6171{
6172 (void)seed64;
6173 return XXH3_hashLong_64b_internal(input, len, secret, secretLen, XXH3_accumulate, XXH3_scrambleAcc);
6174}
6175
6176/*
6177 * It's preferable for performance that XXH3_hashLong is not inlined,
6178 * as it results in a smaller function for small data, easier to the instruction cache.
6179 * Note that inside this no_inline function, we do inline the internal loop,
6180 * and provide a statically defined secret size to allow optimization of vector loop.
6181 */
6182XXH_NO_INLINE XXH_PUREF XXH64_hash_t
6183XXH3_hashLong_64b_default(const void* XXH_RESTRICT input, size_t len,
6184 XXH64_hash_t seed64, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
6185{
6186 (void)seed64; (void)secret; (void)secretLen;
6187 return XXH3_hashLong_64b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_accumulate, XXH3_scrambleAcc);
6188}
6189
6190/*
6191 * XXH3_hashLong_64b_withSeed():
6192 * Generate a custom key based on alteration of default XXH3_kSecret with the seed,
6193 * and then use this key for long mode hashing.
6194 *
6195 * This operation is decently fast but nonetheless costs a little bit of time.
6196 * Try to avoid it whenever possible (typically when seed==0).
6197 *
6198 * It's important for performance that XXH3_hashLong is not inlined. Not sure
6199 * why (uop cache maybe?), but the difference is large and easily measurable.
6200 */
6201XXH_FORCE_INLINE XXH64_hash_t
6202XXH3_hashLong_64b_withSeed_internal(const void* input, size_t len,
6203 XXH64_hash_t seed,
6204 XXH3_f_accumulate f_acc,
6205 XXH3_f_scrambleAcc f_scramble,
6206 XXH3_f_initCustomSecret f_initSec)
6207{
6208#if XXH_SIZE_OPT <= 0
6209 if (seed == 0)
6210 return XXH3_hashLong_64b_internal(input, len,
6211 XXH3_kSecret, sizeof(XXH3_kSecret),
6212 f_acc, f_scramble);
6213#endif
6214 { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
6215 f_initSec(secret, seed);
6216 return XXH3_hashLong_64b_internal(input, len, secret, sizeof(secret),
6217 f_acc, f_scramble);
6218 }
6219}
6220
6221/*
6222 * It's important for performance that XXH3_hashLong is not inlined.
6223 */
6224XXH_NO_INLINE XXH64_hash_t
6225XXH3_hashLong_64b_withSeed(const void* XXH_RESTRICT input, size_t len,
6226 XXH64_hash_t seed, const xxh_u8* XXH_RESTRICT secret, size_t secretLen)
6227{
6228 (void)secret; (void)secretLen;
6229 return XXH3_hashLong_64b_withSeed_internal(input, len, seed,
6230 XXH3_accumulate, XXH3_scrambleAcc, XXH3_initCustomSecret);
6231}
6232
6233
6234typedef XXH64_hash_t (*XXH3_hashLong64_f)(const void* XXH_RESTRICT, size_t,
6235 XXH64_hash_t, const xxh_u8* XXH_RESTRICT, size_t);
6236
6237XXH_FORCE_INLINE XXH64_hash_t
6238XXH3_64bits_internal(const void* XXH_RESTRICT input, size_t len,
6239 XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
6240 XXH3_hashLong64_f f_hashLong)
6241{
6242 XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
6243 /*
6244 * If an action is to be taken if `secretLen` condition is not respected,
6245 * it should be done here.
6246 * For now, it's a contract pre-condition.
6247 * Adding a check and a branch here would cost performance at every hash.
6248 * Also, note that function signature doesn't offer room to return an error.
6249 */
6250 if (len <= 16)
6251 return XXH3_len_0to16_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
6252 if (len <= 128)
6253 return XXH3_len_17to128_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
6254 if (len <= XXH3_MIDSIZE_MAX)
6255 return XXH3_len_129to240_64b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
6256 return f_hashLong(input, len, seed64, (const xxh_u8*)secret, secretLen);
6257}
6258
6259
6260/* === Public entry point === */
6261
6263XXH_PUBLIC_API XXH64_hash_t XXH3_64bits(XXH_NOESCAPE const void* input, size_t length)
6264{
6265 return XXH3_64bits_internal(input, length, 0, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_default);
6266}
6267
6270XXH3_64bits_withSecret(XXH_NOESCAPE const void* input, size_t length, XXH_NOESCAPE const void* secret, size_t secretSize)
6271{
6272 return XXH3_64bits_internal(input, length, 0, secret, secretSize, XXH3_hashLong_64b_withSecret);
6273}
6274
6277XXH3_64bits_withSeed(XXH_NOESCAPE const void* input, size_t length, XXH64_hash_t seed)
6278{
6279 return XXH3_64bits_internal(input, length, seed, XXH3_kSecret, sizeof(XXH3_kSecret), XXH3_hashLong_64b_withSeed);
6280}
6281
6283XXH3_64bits_withSecretandSeed(XXH_NOESCAPE const void* input, size_t length, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed)
6284{
6285 if (length <= XXH3_MIDSIZE_MAX)
6286 return XXH3_64bits_internal(input, length, seed, XXH3_kSecret, sizeof(XXH3_kSecret), NULL);
6287 return XXH3_hashLong_64b_withSecret(input, length, seed, (const xxh_u8*)secret, secretSize);
6288}
6289
6290
6291/* === XXH3 streaming === */
6292#ifndef XXH_NO_STREAM
6293/*
6294 * Malloc's a pointer that is always aligned to @align.
6295 *
6296 * This must be freed with `XXH_alignedFree()`.
6297 *
6298 * malloc typically guarantees 16 byte alignment on 64-bit systems and 8 byte
6299 * alignment on 32-bit. This isn't enough for the 32 byte aligned loads in AVX2
6300 * or on 32-bit, the 16 byte aligned loads in SSE2 and NEON.
6301 *
6302 * This underalignment previously caused a rather obvious crash which went
6303 * completely unnoticed due to XXH3_createState() not actually being tested.
6304 * Credit to RedSpah for noticing this bug.
6305 *
6306 * The alignment is done manually: Functions like posix_memalign or _mm_malloc
6307 * are avoided: To maintain portability, we would have to write a fallback
6308 * like this anyways, and besides, testing for the existence of library
6309 * functions without relying on external build tools is impossible.
6310 *
6311 * The method is simple: Overallocate, manually align, and store the offset
6312 * to the original behind the returned pointer.
6313 *
6314 * Align must be a power of 2 and 8 <= align <= 128.
6315 */
6316static XXH_MALLOCF void* XXH_alignedMalloc(size_t s, size_t align)
6317{
6318 XXH_ASSERT(align <= 128 && align >= 8); /* range check */
6319 XXH_ASSERT((align & (align-1)) == 0); /* power of 2 */
6320 XXH_ASSERT(s != 0 && s < (s + align)); /* empty/overflow */
6321 { /* Overallocate to make room for manual realignment and an offset byte */
6322 xxh_u8* base = (xxh_u8*)XXH_malloc(s + align);
6323 if (base != NULL) {
6324 /*
6325 * Get the offset needed to align this pointer.
6326 *
6327 * Even if the returned pointer is aligned, there will always be
6328 * at least one byte to store the offset to the original pointer.
6329 */
6330 size_t offset = align - ((size_t)base & (align - 1)); /* base % align */
6331 /* Add the offset for the now-aligned pointer */
6332 xxh_u8* ptr = base + offset;
6333
6334 XXH_ASSERT((size_t)ptr % align == 0);
6335
6336 /* Store the offset immediately before the returned pointer. */
6337 ptr[-1] = (xxh_u8)offset;
6338 return ptr;
6339 }
6340 return NULL;
6341 }
6342}
6343/*
6344 * Frees an aligned pointer allocated by XXH_alignedMalloc(). Don't pass
6345 * normal malloc'd pointers, XXH_alignedMalloc has a specific data layout.
6346 */
6347static void XXH_alignedFree(void* p)
6348{
6349 if (p != NULL) {
6350 xxh_u8* ptr = (xxh_u8*)p;
6351 /* Get the offset byte we added in XXH_malloc. */
6352 xxh_u8 offset = ptr[-1];
6353 /* Free the original malloc'd pointer */
6354 xxh_u8* base = ptr - offset;
6355 XXH_free(base);
6356 }
6357}
6370{
6371 XXH3_state_t* const state = (XXH3_state_t*)XXH_alignedMalloc(sizeof(XXH3_state_t), 64);
6372 if (state==NULL) return NULL;
6373 XXH3_INITSTATE(state);
6374 return state;
6375}
6376
6390{
6391 XXH_alignedFree(statePtr);
6392 return XXH_OK;
6393}
6394
6396XXH_PUBLIC_API void
6397XXH3_copyState(XXH_NOESCAPE XXH3_state_t* dst_state, XXH_NOESCAPE const XXH3_state_t* src_state)
6398{
6399 XXH_memcpy(dst_state, src_state, sizeof(*dst_state));
6400}
6401
6402static void
6403XXH3_reset_internal(XXH3_state_t* statePtr,
6404 XXH64_hash_t seed,
6405 const void* secret, size_t secretSize)
6406{
6407 size_t const initStart = offsetof(XXH3_state_t, bufferedSize);
6408 size_t const initLength = offsetof(XXH3_state_t, nbStripesPerBlock) - initStart;
6409 XXH_ASSERT(offsetof(XXH3_state_t, nbStripesPerBlock) > initStart);
6410 XXH_ASSERT(statePtr != NULL);
6411 /* set members from bufferedSize to nbStripesPerBlock (excluded) to 0 */
6412 memset((char*)statePtr + initStart, 0, initLength);
6413 statePtr->acc[0] = XXH_PRIME32_3;
6414 statePtr->acc[1] = XXH_PRIME64_1;
6415 statePtr->acc[2] = XXH_PRIME64_2;
6416 statePtr->acc[3] = XXH_PRIME64_3;
6417 statePtr->acc[4] = XXH_PRIME64_4;
6418 statePtr->acc[5] = XXH_PRIME32_2;
6419 statePtr->acc[6] = XXH_PRIME64_5;
6420 statePtr->acc[7] = XXH_PRIME32_1;
6421 statePtr->seed = seed;
6422 statePtr->useSeed = (seed != 0);
6423 statePtr->extSecret = (const unsigned char*)secret;
6424 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
6425 statePtr->secretLimit = secretSize - XXH_STRIPE_LEN;
6426 statePtr->nbStripesPerBlock = statePtr->secretLimit / XXH_SECRET_CONSUME_RATE;
6427}
6428
6431XXH3_64bits_reset(XXH_NOESCAPE XXH3_state_t* statePtr)
6432{
6433 if (statePtr == NULL) return XXH_ERROR;
6434 XXH3_reset_internal(statePtr, 0, XXH3_kSecret, XXH_SECRET_DEFAULT_SIZE);
6435 return XXH_OK;
6436}
6437
6440XXH3_64bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize)
6441{
6442 if (statePtr == NULL) return XXH_ERROR;
6443 XXH3_reset_internal(statePtr, 0, secret, secretSize);
6444 if (secret == NULL) return XXH_ERROR;
6445 if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
6446 return XXH_OK;
6447}
6448
6452{
6453 if (statePtr == NULL) return XXH_ERROR;
6454 if (seed==0) return XXH3_64bits_reset(statePtr);
6455 if ((seed != statePtr->seed) || (statePtr->extSecret != NULL))
6456 XXH3_initCustomSecret(statePtr->customSecret, seed);
6457 XXH3_reset_internal(statePtr, seed, NULL, XXH_SECRET_DEFAULT_SIZE);
6458 return XXH_OK;
6459}
6460
6463XXH3_64bits_reset_withSecretandSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed64)
6464{
6465 if (statePtr == NULL) return XXH_ERROR;
6466 if (secret == NULL) return XXH_ERROR;
6467 if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
6468 XXH3_reset_internal(statePtr, seed64, secret, secretSize);
6469 statePtr->useSeed = 1; /* always, even if seed64==0 */
6470 return XXH_OK;
6471}
6472
6490XXH_FORCE_INLINE const xxh_u8 *
6491XXH3_consumeStripes(xxh_u64* XXH_RESTRICT acc,
6492 size_t* XXH_RESTRICT nbStripesSoFarPtr, size_t nbStripesPerBlock,
6493 const xxh_u8* XXH_RESTRICT input, size_t nbStripes,
6494 const xxh_u8* XXH_RESTRICT secret, size_t secretLimit,
6495 XXH3_f_accumulate f_acc,
6496 XXH3_f_scrambleAcc f_scramble)
6497{
6498 const xxh_u8* initialSecret = secret + *nbStripesSoFarPtr * XXH_SECRET_CONSUME_RATE;
6499 /* Process full blocks */
6500 if (nbStripes >= (nbStripesPerBlock - *nbStripesSoFarPtr)) {
6501 /* Process the initial partial block... */
6502 size_t nbStripesThisIter = nbStripesPerBlock - *nbStripesSoFarPtr;
6503
6504 do {
6505 /* Accumulate and scramble */
6506 f_acc(acc, input, initialSecret, nbStripesThisIter);
6507 f_scramble(acc, secret + secretLimit);
6508 input += nbStripesThisIter * XXH_STRIPE_LEN;
6509 nbStripes -= nbStripesThisIter;
6510 /* Then continue the loop with the full block size */
6511 nbStripesThisIter = nbStripesPerBlock;
6512 initialSecret = secret;
6513 } while (nbStripes >= nbStripesPerBlock);
6514 *nbStripesSoFarPtr = 0;
6515 }
6516 /* Process a partial block */
6517 if (nbStripes > 0) {
6518 f_acc(acc, input, initialSecret, nbStripes);
6519 input += nbStripes * XXH_STRIPE_LEN;
6520 *nbStripesSoFarPtr += nbStripes;
6521 }
6522 /* Return end pointer */
6523 return input;
6524}
6525
6526#ifndef XXH3_STREAM_USE_STACK
6527# if XXH_SIZE_OPT <= 0 && !defined(__clang__) /* clang doesn't need additional stack space */
6528# define XXH3_STREAM_USE_STACK 1
6529# endif
6530#endif
6531/*
6532 * Both XXH3_64bits_update and XXH3_128bits_update use this routine.
6533 */
6534XXH_FORCE_INLINE XXH_errorcode
6535XXH3_update(XXH3_state_t* XXH_RESTRICT const state,
6536 const xxh_u8* XXH_RESTRICT input, size_t len,
6537 XXH3_f_accumulate f_acc,
6538 XXH3_f_scrambleAcc f_scramble)
6539{
6540 if (input==NULL) {
6541 XXH_ASSERT(len == 0);
6542 return XXH_OK;
6543 }
6544
6545 XXH_ASSERT(state != NULL);
6546 { const xxh_u8* const bEnd = input + len;
6547 const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
6548#if defined(XXH3_STREAM_USE_STACK) && XXH3_STREAM_USE_STACK >= 1
6549 /* For some reason, gcc and MSVC seem to suffer greatly
6550 * when operating accumulators directly into state.
6551 * Operating into stack space seems to enable proper optimization.
6552 * clang, on the other hand, doesn't seem to need this trick */
6553 XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[8];
6554 XXH_memcpy(acc, state->acc, sizeof(acc));
6555#else
6556 xxh_u64* XXH_RESTRICT const acc = state->acc;
6557#endif
6558 state->totalLen += len;
6559 XXH_ASSERT(state->bufferedSize <= XXH3_INTERNALBUFFER_SIZE);
6560
6561 /* small input : just fill in tmp buffer */
6562 if (len <= XXH3_INTERNALBUFFER_SIZE - state->bufferedSize) {
6563 XXH_memcpy(state->buffer + state->bufferedSize, input, len);
6564 state->bufferedSize += (XXH32_hash_t)len;
6565 return XXH_OK;
6566 }
6567
6568 /* total input is now > XXH3_INTERNALBUFFER_SIZE */
6569 #define XXH3_INTERNALBUFFER_STRIPES (XXH3_INTERNALBUFFER_SIZE / XXH_STRIPE_LEN)
6570 XXH_STATIC_ASSERT(XXH3_INTERNALBUFFER_SIZE % XXH_STRIPE_LEN == 0); /* clean multiple */
6571
6572 /*
6573 * Internal buffer is partially filled (always, except at beginning)
6574 * Complete it, then consume it.
6575 */
6576 if (state->bufferedSize) {
6577 size_t const loadSize = XXH3_INTERNALBUFFER_SIZE - state->bufferedSize;
6578 XXH_memcpy(state->buffer + state->bufferedSize, input, loadSize);
6579 input += loadSize;
6580 XXH3_consumeStripes(acc,
6581 &state->nbStripesSoFar, state->nbStripesPerBlock,
6582 state->buffer, XXH3_INTERNALBUFFER_STRIPES,
6583 secret, state->secretLimit,
6584 f_acc, f_scramble);
6585 state->bufferedSize = 0;
6586 }
6587 XXH_ASSERT(input < bEnd);
6588 if (bEnd - input > XXH3_INTERNALBUFFER_SIZE) {
6589 size_t nbStripes = (size_t)(bEnd - 1 - input) / XXH_STRIPE_LEN;
6590 input = XXH3_consumeStripes(acc,
6591 &state->nbStripesSoFar, state->nbStripesPerBlock,
6592 input, nbStripes,
6593 secret, state->secretLimit,
6594 f_acc, f_scramble);
6595 XXH_memcpy(state->buffer + sizeof(state->buffer) - XXH_STRIPE_LEN, input - XXH_STRIPE_LEN, XXH_STRIPE_LEN);
6596
6597 }
6598 /* Some remaining input (always) : buffer it */
6599 XXH_ASSERT(input < bEnd);
6600 XXH_ASSERT(bEnd - input <= XXH3_INTERNALBUFFER_SIZE);
6601 XXH_ASSERT(state->bufferedSize == 0);
6602 XXH_memcpy(state->buffer, input, (size_t)(bEnd-input));
6603 state->bufferedSize = (XXH32_hash_t)(bEnd-input);
6604#if defined(XXH3_STREAM_USE_STACK) && XXH3_STREAM_USE_STACK >= 1
6605 /* save stack accumulators into state */
6606 XXH_memcpy(state->acc, acc, sizeof(acc));
6607#endif
6608 }
6609
6610 return XXH_OK;
6611}
6612
6615XXH3_64bits_update(XXH_NOESCAPE XXH3_state_t* state, XXH_NOESCAPE const void* input, size_t len)
6616{
6617 return XXH3_update(state, (const xxh_u8*)input, len,
6618 XXH3_accumulate, XXH3_scrambleAcc);
6619}
6620
6621
6622XXH_FORCE_INLINE void
6623XXH3_digest_long (XXH64_hash_t* acc,
6624 const XXH3_state_t* state,
6625 const unsigned char* secret)
6626{
6627 xxh_u8 lastStripe[XXH_STRIPE_LEN];
6628 const xxh_u8* lastStripePtr;
6629
6630 /*
6631 * Digest on a local copy. This way, the state remains unaltered, and it can
6632 * continue ingesting more input afterwards.
6633 */
6634 XXH_memcpy(acc, state->acc, sizeof(state->acc));
6635 if (state->bufferedSize >= XXH_STRIPE_LEN) {
6636 /* Consume remaining stripes then point to remaining data in buffer */
6637 size_t const nbStripes = (state->bufferedSize - 1) / XXH_STRIPE_LEN;
6638 size_t nbStripesSoFar = state->nbStripesSoFar;
6639 XXH3_consumeStripes(acc,
6640 &nbStripesSoFar, state->nbStripesPerBlock,
6641 state->buffer, nbStripes,
6642 secret, state->secretLimit,
6643 XXH3_accumulate, XXH3_scrambleAcc);
6644 lastStripePtr = state->buffer + state->bufferedSize - XXH_STRIPE_LEN;
6645 } else { /* bufferedSize < XXH_STRIPE_LEN */
6646 /* Copy to temp buffer */
6647 size_t const catchupSize = XXH_STRIPE_LEN - state->bufferedSize;
6648 XXH_ASSERT(state->bufferedSize > 0); /* there is always some input buffered */
6649 XXH_memcpy(lastStripe, state->buffer + sizeof(state->buffer) - catchupSize, catchupSize);
6650 XXH_memcpy(lastStripe + catchupSize, state->buffer, state->bufferedSize);
6651 lastStripePtr = lastStripe;
6652 }
6653 /* Last stripe */
6654 XXH3_accumulate_512(acc,
6655 lastStripePtr,
6656 secret + state->secretLimit - XXH_SECRET_LASTACC_START);
6657}
6658
6661{
6662 const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
6663 if (state->totalLen > XXH3_MIDSIZE_MAX) {
6664 XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
6665 XXH3_digest_long(acc, state, secret);
6666 return XXH3_finalizeLong_64b(acc, secret, (xxh_u64)state->totalLen);
6667 }
6668 /* totalLen <= XXH3_MIDSIZE_MAX: digesting a short input */
6669 if (state->useSeed)
6670 return XXH3_64bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
6671 return XXH3_64bits_withSecret(state->buffer, (size_t)(state->totalLen),
6672 secret, state->secretLimit + XXH_STRIPE_LEN);
6673}
6674#endif /* !XXH_NO_STREAM */
6675
6676
6677/* ==========================================
6678 * XXH3 128 bits (a.k.a XXH128)
6679 * ==========================================
6680 * XXH3's 128-bit variant has better mixing and strength than the 64-bit variant,
6681 * even without counting the significantly larger output size.
6682 *
6683 * For example, extra steps are taken to avoid the seed-dependent collisions
6684 * in 17-240 byte inputs (See XXH3_mix16B and XXH128_mix32B).
6685 *
6686 * This strength naturally comes at the cost of some speed, especially on short
6687 * lengths. Note that longer hashes are about as fast as the 64-bit version
6688 * due to it using only a slight modification of the 64-bit loop.
6689 *
6690 * XXH128 is also more oriented towards 64-bit machines. It is still extremely
6691 * fast for a _128-bit_ hash on 32-bit (it usually clears XXH64).
6692 */
6693
6694XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
6695XXH3_len_1to3_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
6696{
6697 /* A doubled version of 1to3_64b with different constants. */
6698 XXH_ASSERT(input != NULL);
6699 XXH_ASSERT(1 <= len && len <= 3);
6700 XXH_ASSERT(secret != NULL);
6701 /*
6702 * len = 1: combinedl = { input[0], 0x01, input[0], input[0] }
6703 * len = 2: combinedl = { input[1], 0x02, input[0], input[1] }
6704 * len = 3: combinedl = { input[2], 0x03, input[0], input[1] }
6705 */
6706 { xxh_u8 const c1 = input[0];
6707 xxh_u8 const c2 = input[len >> 1];
6708 xxh_u8 const c3 = input[len - 1];
6709 xxh_u32 const combinedl = ((xxh_u32)c1 <<16) | ((xxh_u32)c2 << 24)
6710 | ((xxh_u32)c3 << 0) | ((xxh_u32)len << 8);
6711 xxh_u32 const combinedh = XXH_rotl32(XXH_swap32(combinedl), 13);
6712 xxh_u64 const bitflipl = (XXH_readLE32(secret) ^ XXH_readLE32(secret+4)) + seed;
6713 xxh_u64 const bitfliph = (XXH_readLE32(secret+8) ^ XXH_readLE32(secret+12)) - seed;
6714 xxh_u64 const keyed_lo = (xxh_u64)combinedl ^ bitflipl;
6715 xxh_u64 const keyed_hi = (xxh_u64)combinedh ^ bitfliph;
6716 XXH128_hash_t h128;
6717 h128.low64 = XXH64_avalanche(keyed_lo);
6718 h128.high64 = XXH64_avalanche(keyed_hi);
6719 return h128;
6720 }
6721}
6722
6723XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
6724XXH3_len_4to8_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
6725{
6726 XXH_ASSERT(input != NULL);
6727 XXH_ASSERT(secret != NULL);
6728 XXH_ASSERT(4 <= len && len <= 8);
6729 seed ^= (xxh_u64)XXH_swap32((xxh_u32)seed) << 32;
6730 { xxh_u32 const input_lo = XXH_readLE32(input);
6731 xxh_u32 const input_hi = XXH_readLE32(input + len - 4);
6732 xxh_u64 const input_64 = input_lo + ((xxh_u64)input_hi << 32);
6733 xxh_u64 const bitflip = (XXH_readLE64(secret+16) ^ XXH_readLE64(secret+24)) + seed;
6734 xxh_u64 const keyed = input_64 ^ bitflip;
6735
6736 /* Shift len to the left to ensure it is even, this avoids even multiplies. */
6737 XXH128_hash_t m128 = XXH_mult64to128(keyed, XXH_PRIME64_1 + (len << 2));
6738
6739 m128.high64 += (m128.low64 << 1);
6740 m128.low64 ^= (m128.high64 >> 3);
6741
6742 m128.low64 = XXH_xorshift64(m128.low64, 35);
6743 m128.low64 *= PRIME_MX2;
6744 m128.low64 = XXH_xorshift64(m128.low64, 28);
6745 m128.high64 = XXH3_avalanche(m128.high64);
6746 return m128;
6747 }
6748}
6749
6750XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
6751XXH3_len_9to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
6752{
6753 XXH_ASSERT(input != NULL);
6754 XXH_ASSERT(secret != NULL);
6755 XXH_ASSERT(9 <= len && len <= 16);
6756 { xxh_u64 const bitflipl = (XXH_readLE64(secret+32) ^ XXH_readLE64(secret+40)) - seed;
6757 xxh_u64 const bitfliph = (XXH_readLE64(secret+48) ^ XXH_readLE64(secret+56)) + seed;
6758 xxh_u64 const input_lo = XXH_readLE64(input);
6759 xxh_u64 input_hi = XXH_readLE64(input + len - 8);
6760 XXH128_hash_t m128 = XXH_mult64to128(input_lo ^ input_hi ^ bitflipl, XXH_PRIME64_1);
6761 /*
6762 * Put len in the middle of m128 to ensure that the length gets mixed to
6763 * both the low and high bits in the 128x64 multiply below.
6764 */
6765 m128.low64 += (xxh_u64)(len - 1) << 54;
6766 input_hi ^= bitfliph;
6767 /*
6768 * Add the high 32 bits of input_hi to the high 32 bits of m128, then
6769 * add the long product of the low 32 bits of input_hi and XXH_PRIME32_2 to
6770 * the high 64 bits of m128.
6771 *
6772 * The best approach to this operation is different on 32-bit and 64-bit.
6773 */
6774 if (sizeof(void *) < sizeof(xxh_u64)) { /* 32-bit */
6775 /*
6776 * 32-bit optimized version, which is more readable.
6777 *
6778 * On 32-bit, it removes an ADC and delays a dependency between the two
6779 * halves of m128.high64, but it generates an extra mask on 64-bit.
6780 */
6781 m128.high64 += (input_hi & 0xFFFFFFFF00000000ULL) + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2);
6782 } else {
6783 /*
6784 * 64-bit optimized (albeit more confusing) version.
6785 *
6786 * Uses some properties of addition and multiplication to remove the mask:
6787 *
6788 * Let:
6789 * a = input_hi.lo = (input_hi & 0x00000000FFFFFFFF)
6790 * b = input_hi.hi = (input_hi & 0xFFFFFFFF00000000)
6791 * c = XXH_PRIME32_2
6792 *
6793 * a + (b * c)
6794 * Inverse Property: x + y - x == y
6795 * a + (b * (1 + c - 1))
6796 * Distributive Property: x * (y + z) == (x * y) + (x * z)
6797 * a + (b * 1) + (b * (c - 1))
6798 * Identity Property: x * 1 == x
6799 * a + b + (b * (c - 1))
6800 *
6801 * Substitute a, b, and c:
6802 * input_hi.hi + input_hi.lo + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
6803 *
6804 * Since input_hi.hi + input_hi.lo == input_hi, we get this:
6805 * input_hi + ((xxh_u64)input_hi.lo * (XXH_PRIME32_2 - 1))
6806 */
6807 m128.high64 += input_hi + XXH_mult32to64((xxh_u32)input_hi, XXH_PRIME32_2 - 1);
6808 }
6809 /* m128 ^= XXH_swap64(m128 >> 64); */
6810 m128.low64 ^= XXH_swap64(m128.high64);
6811
6812 { /* 128x64 multiply: h128 = m128 * XXH_PRIME64_2; */
6813 XXH128_hash_t h128 = XXH_mult64to128(m128.low64, XXH_PRIME64_2);
6814 h128.high64 += m128.high64 * XXH_PRIME64_2;
6815
6816 h128.low64 = XXH3_avalanche(h128.low64);
6817 h128.high64 = XXH3_avalanche(h128.high64);
6818 return h128;
6819 } }
6820}
6821
6822/*
6823 * Assumption: `secret` size is >= XXH3_SECRET_SIZE_MIN
6824 */
6825XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
6826XXH3_len_0to16_128b(const xxh_u8* input, size_t len, const xxh_u8* secret, XXH64_hash_t seed)
6827{
6828 XXH_ASSERT(len <= 16);
6829 { if (len > 8) return XXH3_len_9to16_128b(input, len, secret, seed);
6830 if (len >= 4) return XXH3_len_4to8_128b(input, len, secret, seed);
6831 if (len) return XXH3_len_1to3_128b(input, len, secret, seed);
6832 { XXH128_hash_t h128;
6833 xxh_u64 const bitflipl = XXH_readLE64(secret+64) ^ XXH_readLE64(secret+72);
6834 xxh_u64 const bitfliph = XXH_readLE64(secret+80) ^ XXH_readLE64(secret+88);
6835 h128.low64 = XXH64_avalanche(seed ^ bitflipl);
6836 h128.high64 = XXH64_avalanche( seed ^ bitfliph);
6837 return h128;
6838 } }
6839}
6840
6841/*
6842 * A bit slower than XXH3_mix16B, but handles multiply by zero better.
6843 */
6844XXH_FORCE_INLINE XXH128_hash_t
6845XXH128_mix32B(XXH128_hash_t acc, const xxh_u8* input_1, const xxh_u8* input_2,
6846 const xxh_u8* secret, XXH64_hash_t seed)
6847{
6848 acc.low64 += XXH3_mix16B (input_1, secret+0, seed);
6849 acc.low64 ^= XXH_readLE64(input_2) + XXH_readLE64(input_2 + 8);
6850 acc.high64 += XXH3_mix16B (input_2, secret+16, seed);
6851 acc.high64 ^= XXH_readLE64(input_1) + XXH_readLE64(input_1 + 8);
6852 return acc;
6853}
6854
6855
6856XXH_FORCE_INLINE XXH_PUREF XXH128_hash_t
6857XXH3_len_17to128_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
6858 const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
6859 XXH64_hash_t seed)
6860{
6861 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
6862 XXH_ASSERT(16 < len && len <= 128);
6863
6864 { XXH128_hash_t acc;
6865 acc.low64 = len * XXH_PRIME64_1;
6866 acc.high64 = 0;
6867
6868#if XXH_SIZE_OPT >= 1
6869 {
6870 /* Smaller, but slightly slower. */
6871 unsigned int i = (unsigned int)(len - 1) / 32;
6872 do {
6873 acc = XXH128_mix32B(acc, input+16*i, input+len-16*(i+1), secret+32*i, seed);
6874 } while (i-- != 0);
6875 }
6876#else
6877 if (len > 32) {
6878 if (len > 64) {
6879 if (len > 96) {
6880 acc = XXH128_mix32B(acc, input+48, input+len-64, secret+96, seed);
6881 }
6882 acc = XXH128_mix32B(acc, input+32, input+len-48, secret+64, seed);
6883 }
6884 acc = XXH128_mix32B(acc, input+16, input+len-32, secret+32, seed);
6885 }
6886 acc = XXH128_mix32B(acc, input, input+len-16, secret, seed);
6887#endif
6888 { XXH128_hash_t h128;
6889 h128.low64 = acc.low64 + acc.high64;
6890 h128.high64 = (acc.low64 * XXH_PRIME64_1)
6891 + (acc.high64 * XXH_PRIME64_4)
6892 + ((len - seed) * XXH_PRIME64_2);
6893 h128.low64 = XXH3_avalanche(h128.low64);
6894 h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
6895 return h128;
6896 }
6897 }
6898}
6899
6900XXH_NO_INLINE XXH_PUREF XXH128_hash_t
6901XXH3_len_129to240_128b(const xxh_u8* XXH_RESTRICT input, size_t len,
6902 const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
6903 XXH64_hash_t seed)
6904{
6905 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN); (void)secretSize;
6906 XXH_ASSERT(128 < len && len <= XXH3_MIDSIZE_MAX);
6907
6908 { XXH128_hash_t acc;
6909 unsigned i;
6910 acc.low64 = len * XXH_PRIME64_1;
6911 acc.high64 = 0;
6912 /*
6913 * We set as `i` as offset + 32. We do this so that unchanged
6914 * `len` can be used as upper bound. This reaches a sweet spot
6915 * where both x86 and aarch64 get simple agen and good codegen
6916 * for the loop.
6917 */
6918 for (i = 32; i < 160; i += 32) {
6919 acc = XXH128_mix32B(acc,
6920 input + i - 32,
6921 input + i - 16,
6922 secret + i - 32,
6923 seed);
6924 }
6925 acc.low64 = XXH3_avalanche(acc.low64);
6926 acc.high64 = XXH3_avalanche(acc.high64);
6927 /*
6928 * NB: `i <= len` will duplicate the last 32-bytes if
6929 * len % 32 was zero. This is an unfortunate necessity to keep
6930 * the hash result stable.
6931 */
6932 for (i=160; i <= len; i += 32) {
6933 acc = XXH128_mix32B(acc,
6934 input + i - 32,
6935 input + i - 16,
6936 secret + XXH3_MIDSIZE_STARTOFFSET + i - 160,
6937 seed);
6938 }
6939 /* last bytes */
6940 acc = XXH128_mix32B(acc,
6941 input + len - 16,
6942 input + len - 32,
6943 secret + XXH3_SECRET_SIZE_MIN - XXH3_MIDSIZE_LASTOFFSET - 16,
6944 (XXH64_hash_t)0 - seed);
6945
6946 { XXH128_hash_t h128;
6947 h128.low64 = acc.low64 + acc.high64;
6948 h128.high64 = (acc.low64 * XXH_PRIME64_1)
6949 + (acc.high64 * XXH_PRIME64_4)
6950 + ((len - seed) * XXH_PRIME64_2);
6951 h128.low64 = XXH3_avalanche(h128.low64);
6952 h128.high64 = (XXH64_hash_t)0 - XXH3_avalanche(h128.high64);
6953 return h128;
6954 }
6955 }
6956}
6957
6958static XXH_PUREF XXH128_hash_t
6959XXH3_finalizeLong_128b(const xxh_u64* XXH_RESTRICT acc, const xxh_u8* XXH_RESTRICT secret, size_t secretSize, xxh_u64 len)
6960{
6961 XXH128_hash_t h128;
6962 h128.low64 = XXH3_finalizeLong_64b(acc, secret, len);
6963 h128.high64 = XXH3_mergeAccs(acc, secret + secretSize
6964 - XXH_STRIPE_LEN - XXH_SECRET_MERGEACCS_START,
6965 ~(len * XXH_PRIME64_2));
6966 return h128;
6967}
6968
6969XXH_FORCE_INLINE XXH128_hash_t
6970XXH3_hashLong_128b_internal(const void* XXH_RESTRICT input, size_t len,
6971 const xxh_u8* XXH_RESTRICT secret, size_t secretSize,
6972 XXH3_f_accumulate f_acc,
6973 XXH3_f_scrambleAcc f_scramble)
6974{
6975 XXH_ALIGN(XXH_ACC_ALIGN) xxh_u64 acc[XXH_ACC_NB] = XXH3_INIT_ACC;
6976
6977 XXH3_hashLong_internal_loop(acc, (const xxh_u8*)input, len, secret, secretSize, f_acc, f_scramble);
6978
6979 /* converge into final hash */
6980 XXH_STATIC_ASSERT(sizeof(acc) == 64);
6981 XXH_ASSERT(secretSize >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
6982 return XXH3_finalizeLong_128b(acc, secret, secretSize, (xxh_u64)len);
6983}
6984
6985/*
6986 * It's important for performance that XXH3_hashLong() is not inlined.
6987 */
6988XXH_NO_INLINE XXH_PUREF XXH128_hash_t
6989XXH3_hashLong_128b_default(const void* XXH_RESTRICT input, size_t len,
6990 XXH64_hash_t seed64,
6991 const void* XXH_RESTRICT secret, size_t secretLen)
6992{
6993 (void)seed64; (void)secret; (void)secretLen;
6994 return XXH3_hashLong_128b_internal(input, len, XXH3_kSecret, sizeof(XXH3_kSecret),
6995 XXH3_accumulate, XXH3_scrambleAcc);
6996}
6997
6998/*
6999 * It's important for performance to pass @p secretLen (when it's static)
7000 * to the compiler, so that it can properly optimize the vectorized loop.
7001 *
7002 * When the secret size is unknown, or on GCC 12 where the mix of NO_INLINE and FORCE_INLINE
7003 * breaks -Og, this is XXH_NO_INLINE.
7004 */
7005XXH3_WITH_SECRET_INLINE XXH128_hash_t
7006XXH3_hashLong_128b_withSecret(const void* XXH_RESTRICT input, size_t len,
7007 XXH64_hash_t seed64,
7008 const void* XXH_RESTRICT secret, size_t secretLen)
7009{
7010 (void)seed64;
7011 return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, secretLen,
7012 XXH3_accumulate, XXH3_scrambleAcc);
7013}
7014
7015XXH_FORCE_INLINE XXH128_hash_t
7016XXH3_hashLong_128b_withSeed_internal(const void* XXH_RESTRICT input, size_t len,
7017 XXH64_hash_t seed64,
7018 XXH3_f_accumulate f_acc,
7019 XXH3_f_scrambleAcc f_scramble,
7020 XXH3_f_initCustomSecret f_initSec)
7021{
7022 if (seed64 == 0)
7023 return XXH3_hashLong_128b_internal(input, len,
7024 XXH3_kSecret, sizeof(XXH3_kSecret),
7025 f_acc, f_scramble);
7026 { XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
7027 f_initSec(secret, seed64);
7028 return XXH3_hashLong_128b_internal(input, len, (const xxh_u8*)secret, sizeof(secret),
7029 f_acc, f_scramble);
7030 }
7031}
7032
7033/*
7034 * It's important for performance that XXH3_hashLong is not inlined.
7035 */
7036XXH_NO_INLINE XXH128_hash_t
7037XXH3_hashLong_128b_withSeed(const void* input, size_t len,
7038 XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen)
7039{
7040 (void)secret; (void)secretLen;
7041 return XXH3_hashLong_128b_withSeed_internal(input, len, seed64,
7042 XXH3_accumulate, XXH3_scrambleAcc, XXH3_initCustomSecret);
7043}
7044
7045typedef XXH128_hash_t (*XXH3_hashLong128_f)(const void* XXH_RESTRICT, size_t,
7046 XXH64_hash_t, const void* XXH_RESTRICT, size_t);
7047
7048XXH_FORCE_INLINE XXH128_hash_t
7049XXH3_128bits_internal(const void* input, size_t len,
7050 XXH64_hash_t seed64, const void* XXH_RESTRICT secret, size_t secretLen,
7051 XXH3_hashLong128_f f_hl128)
7052{
7053 XXH_ASSERT(secretLen >= XXH3_SECRET_SIZE_MIN);
7054 /*
7055 * If an action is to be taken if `secret` conditions are not respected,
7056 * it should be done here.
7057 * For now, it's a contract pre-condition.
7058 * Adding a check and a branch here would cost performance at every hash.
7059 */
7060 if (len <= 16)
7061 return XXH3_len_0to16_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, seed64);
7062 if (len <= 128)
7063 return XXH3_len_17to128_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
7064 if (len <= XXH3_MIDSIZE_MAX)
7065 return XXH3_len_129to240_128b((const xxh_u8*)input, len, (const xxh_u8*)secret, secretLen, seed64);
7066 return f_hl128(input, len, seed64, secret, secretLen);
7067}
7068
7069
7070/* === Public XXH128 API === */
7071
7073XXH_PUBLIC_API XXH128_hash_t XXH3_128bits(XXH_NOESCAPE const void* input, size_t len)
7074{
7075 return XXH3_128bits_internal(input, len, 0,
7076 XXH3_kSecret, sizeof(XXH3_kSecret),
7077 XXH3_hashLong_128b_default);
7078}
7079
7082XXH3_128bits_withSecret(XXH_NOESCAPE const void* input, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize)
7083{
7084 return XXH3_128bits_internal(input, len, 0,
7085 (const xxh_u8*)secret, secretSize,
7086 XXH3_hashLong_128b_withSecret);
7087}
7088
7091XXH3_128bits_withSeed(XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed)
7092{
7093 return XXH3_128bits_internal(input, len, seed,
7094 XXH3_kSecret, sizeof(XXH3_kSecret),
7095 XXH3_hashLong_128b_withSeed);
7096}
7097
7100XXH3_128bits_withSecretandSeed(XXH_NOESCAPE const void* input, size_t len, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed)
7101{
7102 if (len <= XXH3_MIDSIZE_MAX)
7103 return XXH3_128bits_internal(input, len, seed, XXH3_kSecret, sizeof(XXH3_kSecret), NULL);
7104 return XXH3_hashLong_128b_withSecret(input, len, seed, secret, secretSize);
7105}
7106
7109XXH128(XXH_NOESCAPE const void* input, size_t len, XXH64_hash_t seed)
7110{
7111 return XXH3_128bits_withSeed(input, len, seed);
7112}
7113
7114
7115/* === XXH3 128-bit streaming === */
7116#ifndef XXH_NO_STREAM
7117/*
7118 * All initialization and update functions are identical to 64-bit streaming variant.
7119 * The only difference is the finalization routine.
7120 */
7121
7124XXH3_128bits_reset(XXH_NOESCAPE XXH3_state_t* statePtr)
7125{
7126 return XXH3_64bits_reset(statePtr);
7127}
7128
7131XXH3_128bits_reset_withSecret(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize)
7132{
7133 return XXH3_64bits_reset_withSecret(statePtr, secret, secretSize);
7134}
7135
7139{
7140 return XXH3_64bits_reset_withSeed(statePtr, seed);
7141}
7142
7145XXH3_128bits_reset_withSecretandSeed(XXH_NOESCAPE XXH3_state_t* statePtr, XXH_NOESCAPE const void* secret, size_t secretSize, XXH64_hash_t seed)
7146{
7147 return XXH3_64bits_reset_withSecretandSeed(statePtr, secret, secretSize, seed);
7148}
7149
7152XXH3_128bits_update(XXH_NOESCAPE XXH3_state_t* state, XXH_NOESCAPE const void* input, size_t len)
7153{
7154 return XXH3_64bits_update(state, input, len);
7155}
7156
7159{
7160 const unsigned char* const secret = (state->extSecret == NULL) ? state->customSecret : state->extSecret;
7161 if (state->totalLen > XXH3_MIDSIZE_MAX) {
7162 XXH_ALIGN(XXH_ACC_ALIGN) XXH64_hash_t acc[XXH_ACC_NB];
7163 XXH3_digest_long(acc, state, secret);
7164 XXH_ASSERT(state->secretLimit + XXH_STRIPE_LEN >= sizeof(acc) + XXH_SECRET_MERGEACCS_START);
7165 return XXH3_finalizeLong_128b(acc, secret, state->secretLimit + XXH_STRIPE_LEN, (xxh_u64)state->totalLen);
7166 }
7167 /* len <= XXH3_MIDSIZE_MAX : short code */
7168 if (state->useSeed)
7169 return XXH3_128bits_withSeed(state->buffer, (size_t)state->totalLen, state->seed);
7170 return XXH3_128bits_withSecret(state->buffer, (size_t)(state->totalLen),
7171 secret, state->secretLimit + XXH_STRIPE_LEN);
7172}
7173#endif /* !XXH_NO_STREAM */
7174/* 128-bit utility functions */
7175
7176#include <string.h> /* memcmp, memcpy */
7177
7178/* return : 1 is equal, 0 if different */
7181{
7182 /* note : XXH128_hash_t is compact, it has no padding byte */
7183 return !(memcmp(&h1, &h2, sizeof(h1)));
7184}
7185
7186/* This prototype is compatible with stdlib's qsort().
7187 * @return : >0 if *h128_1 > *h128_2
7188 * <0 if *h128_1 < *h128_2
7189 * =0 if *h128_1 == *h128_2 */
7191XXH_PUBLIC_API int XXH128_cmp(XXH_NOESCAPE const void* h128_1, XXH_NOESCAPE const void* h128_2)
7192{
7193 XXH128_hash_t const h1 = *(const XXH128_hash_t*)h128_1;
7194 XXH128_hash_t const h2 = *(const XXH128_hash_t*)h128_2;
7195 int const hcmp = (h1.high64 > h2.high64) - (h2.high64 > h1.high64);
7196 /* note : bets that, in most cases, hash values are different */
7197 if (hcmp) return hcmp;
7198 return (h1.low64 > h2.low64) - (h2.low64 > h1.low64);
7199}
7200
7201
7202/*====== Canonical representation ======*/
7204XXH_PUBLIC_API void
7206{
7207 XXH_STATIC_ASSERT(sizeof(XXH128_canonical_t) == sizeof(XXH128_hash_t));
7209 hash.high64 = XXH_swap64(hash.high64);
7210 hash.low64 = XXH_swap64(hash.low64);
7211 }
7212 XXH_memcpy(dst, &hash.high64, sizeof(hash.high64));
7213 XXH_memcpy((char*)dst + sizeof(hash.high64), &hash.low64, sizeof(hash.low64));
7214}
7215
7219{
7220 XXH128_hash_t h;
7221 h.high64 = XXH_readBE64(src);
7222 h.low64 = XXH_readBE64(src->digest + 8);
7223 return h;
7224}
7225
7226
7227
7228/* ==========================================
7229 * Secret generators
7230 * ==========================================
7231 */
7232#define XXH_MIN(x, y) (((x) > (y)) ? (y) : (x))
7233
7234XXH_FORCE_INLINE void XXH3_combine16(void* dst, XXH128_hash_t h128)
7235{
7236 XXH_writeLE64( dst, XXH_readLE64(dst) ^ h128.low64 );
7237 XXH_writeLE64( (char*)dst+8, XXH_readLE64((char*)dst+8) ^ h128.high64 );
7238}
7239
7242XXH3_generateSecret(XXH_NOESCAPE void* secretBuffer, size_t secretSize, XXH_NOESCAPE const void* customSeed, size_t customSeedSize)
7243{
7244#if (XXH_DEBUGLEVEL >= 1)
7245 XXH_ASSERT(secretBuffer != NULL);
7246 XXH_ASSERT(secretSize >= XXH3_SECRET_SIZE_MIN);
7247#else
7248 /* production mode, assert() are disabled */
7249 if (secretBuffer == NULL) return XXH_ERROR;
7250 if (secretSize < XXH3_SECRET_SIZE_MIN) return XXH_ERROR;
7251#endif
7252
7253 if (customSeedSize == 0) {
7254 customSeed = XXH3_kSecret;
7255 customSeedSize = XXH_SECRET_DEFAULT_SIZE;
7256 }
7257#if (XXH_DEBUGLEVEL >= 1)
7258 XXH_ASSERT(customSeed != NULL);
7259#else
7260 if (customSeed == NULL) return XXH_ERROR;
7261#endif
7262
7263 /* Fill secretBuffer with a copy of customSeed - repeat as needed */
7264 { size_t pos = 0;
7265 while (pos < secretSize) {
7266 size_t const toCopy = XXH_MIN((secretSize - pos), customSeedSize);
7267 memcpy((char*)secretBuffer + pos, customSeed, toCopy);
7268 pos += toCopy;
7269 } }
7270
7271 { size_t const nbSeg16 = secretSize / 16;
7272 size_t n;
7273 XXH128_canonical_t scrambler;
7274 XXH128_canonicalFromHash(&scrambler, XXH128(customSeed, customSeedSize, 0));
7275 for (n=0; n<nbSeg16; n++) {
7276 XXH128_hash_t const h128 = XXH128(&scrambler, sizeof(scrambler), n);
7277 XXH3_combine16((char*)secretBuffer + n*16, h128);
7278 }
7279 /* last segment */
7280 XXH3_combine16((char*)secretBuffer + secretSize - 16, XXH128_hashFromCanonical(&scrambler));
7281 }
7282 return XXH_OK;
7283}
7284
7286XXH_PUBLIC_API void
7287XXH3_generateSecret_fromSeed(XXH_NOESCAPE void* secretBuffer, XXH64_hash_t seed)
7288{
7289 XXH_ALIGN(XXH_SEC_ALIGN) xxh_u8 secret[XXH_SECRET_DEFAULT_SIZE];
7290 XXH3_initCustomSecret(secret, seed);
7291 XXH_ASSERT(secretBuffer != NULL);
7292 memcpy(secretBuffer, secret, XXH_SECRET_DEFAULT_SIZE);
7293}
7294
7295
7296
7297/* Pop our optimization override from above */
7298#if XXH_VECTOR == XXH_AVX2 /* AVX2 */ \
7299 && defined(__GNUC__) && !defined(__clang__) /* GCC, not Clang */ \
7300 && defined(__OPTIMIZE__) && XXH_SIZE_OPT <= 0 /* respect -O0 and -Os */
7301# pragma GCC pop_options
7302#endif
7303
7304#endif /* XXH_NO_LONG_LONG */
7305
7306#endif /* XXH_NO_XXH3 */
7307
7311#endif /* XXH_IMPLEMENTATION */
7312
7313
7314#if defined (__cplusplus)
7315} /* extern "C" */
7316#endif
struct XXH32_state_s XXH32_state_t
The opaque state struct for the XXH32 streaming API.
Definition xxhash.h:653
XXH_errorcode XXH32_reset(XXH32_state_t *statePtr, XXH32_hash_t seed)
Resets an XXH32_state_t to begin a new hash.
Definition xxhash.h:3224
XXH32_hash_t XXH32(const void *input, size_t length, XXH32_hash_t seed)
Calculates the 32-bit hash of input using xxHash32.
Definition xxhash.h:3183
XXH_errorcode XXH32_update(XXH32_state_t *statePtr, const void *input, size_t length)
Consumes a block of input to an XXH32_state_t.
Definition xxhash.h:3235
XXH32_state_t * XXH32_createState(void)
Allocates an XXH32_state_t.
Definition xxhash.h:3206
XXH_errorcode XXH32_freeState(XXH32_state_t *statePtr)
Frees an XXH32_state_t.
Definition xxhash.h:3211
void XXH32_canonicalFromHash(XXH32_canonical_t *dst, XXH32_hash_t hash)
Converts an XXH32_hash_t to a big endian XXH32_canonical_t.
Definition xxhash.h:3300
XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t *src)
Converts an XXH32_canonical_t to a native XXH32_hash_t.
Definition xxhash.h:3307
XXH32_hash_t XXH32_digest(const XXH32_state_t *statePtr)
Returns the calculated hash value from an XXH32_state_t.
Definition xxhash.h:3281
void XXH32_copyState(XXH32_state_t *dst_state, const XXH32_state_t *src_state)
Copies one XXH32_state_t to another.
Definition xxhash.h:3218
#define XXH_PRIME32_2
Definition xxhash.h:2907
#define XXH_PRIME32_1
Definition xxhash.h:2906
#define XXH_PRIME32_5
Definition xxhash.h:2910
#define XXH_PRIME32_3
Definition xxhash.h:2908
XXH64_hash_t XXH3_64bits_digest(const XXH3_state_t *statePtr)
Returns the calculated XXH3 64-bit hash value from an XXH3_state_t.
Definition xxhash.h:6660
XXH_errorcode XXH3_128bits_update(XXH3_state_t *statePtr, const void *input, size_t length)
Consumes a block of input to an XXH3_state_t.
Definition xxhash.h:7152
void XXH3_generateSecret_fromSeed(void *secretBuffer, XXH64_hash_t seed)
Generate the same secret as the _withSeed() variants.
Definition xxhash.h:7287
void XXH128_canonicalFromHash(XXH128_canonical_t *dst, XXH128_hash_t hash)
Converts an XXH128_hash_t to a big endian XXH128_canonical_t.
Definition xxhash.h:7205
XXH64_hash_t XXH3_64bits_withSeed(const void *input, size_t length, XXH64_hash_t seed)
Calculates 64-bit seeded variant of XXH3 hash of input.
Definition xxhash.h:6277
int XXH128_cmp(const void *h128_1, const void *h128_2)
Compares two XXH128_hash_t.
Definition xxhash.h:7191
XXH128_hash_t XXH3_128bits_withSeed(const void *data, size_t len, XXH64_hash_t seed)
Calculates 128-bit seeded variant of XXH3 hash of data.
Definition xxhash.h:7091
XXH128_hash_t XXH128(const void *data, size_t len, XXH64_hash_t seed)
Calculates the 128-bit hash of data using XXH3.
Definition xxhash.h:7109
XXH_errorcode XXH3_generateSecret(void *secretBuffer, size_t secretSize, const void *customSeed, size_t customSeedSize)
Derive a high-entropy secret from any user-defined content, named customSeed.
Definition xxhash.h:7242
XXH_errorcode XXH3_64bits_reset_withSecretandSeed(XXH3_state_t *statePtr, const void *secret, size_t secretSize, XXH64_hash_t seed64)
Resets an XXH3_state_t with secret data to begin a new hash.
Definition xxhash.h:6463
XXH_errorcode XXH3_64bits_reset_withSeed(XXH3_state_t *statePtr, XXH64_hash_t seed)
Resets an XXH3_state_t with 64-bit seed to begin a new hash.
Definition xxhash.h:6451
struct XXH3_state_s XXH3_state_t
The opaque state struct for the XXH3 streaming API.
Definition xxhash.h:1244
XXH128_hash_t XXH3_128bits_digest(const XXH3_state_t *statePtr)
Returns the calculated XXH3 128-bit hash value from an XXH3_state_t.
Definition xxhash.h:7158
XXH_errorcode XXH3_64bits_reset_withSecret(XXH3_state_t *statePtr, const void *secret, size_t secretSize)
Resets an XXH3_state_t with secret data to begin a new hash.
Definition xxhash.h:6440
XXH3_state_t * XXH3_createState(void)
Allocate an XXH3_state_t.
Definition xxhash.h:6369
XXH_errorcode XXH3_128bits_reset_withSeed(XXH3_state_t *statePtr, XXH64_hash_t seed)
Resets an XXH3_state_t with 64-bit seed to begin a new hash.
Definition xxhash.h:7138
XXH128_hash_t XXH3_128bits(const void *data, size_t len)
Calculates 128-bit unseeded variant of XXH3 of data.
Definition xxhash.h:7073
XXH128_hash_t XXH3_128bits_withSecret(const void *data, size_t len, const void *secret, size_t secretSize)
Calculates 128-bit variant of XXH3 with a custom "secret".
Definition xxhash.h:7082
XXH_errorcode XXH3_128bits_reset_withSecretandSeed(XXH3_state_t *statePtr, const void *secret, size_t secretSize, XXH64_hash_t seed64)
Resets an XXH3_state_t with secret data to begin a new hash.
Definition xxhash.h:7145
XXH_errorcode XXH3_128bits_reset(XXH3_state_t *statePtr)
Resets an XXH3_state_t to begin a new hash.
Definition xxhash.h:7124
XXH128_hash_t XXH128_hashFromCanonical(const XXH128_canonical_t *src)
Converts an XXH128_canonical_t to a native XXH128_hash_t.
Definition xxhash.h:7218
void XXH3_copyState(XXH3_state_t *dst_state, const XXH3_state_t *src_state)
Copies one XXH3_state_t to another.
Definition xxhash.h:6397
XXH64_hash_t XXH3_64bits_withSecret(const void *data, size_t len, const void *secret, size_t secretSize)
Calculates 64-bit variant of XXH3 with a custom "secret".
Definition xxhash.h:6270
XXH64_hash_t XXH3_64bits(const void *input, size_t length)
Calculates 64-bit unseeded variant of XXH3 hash of input.
Definition xxhash.h:6263
XXH_errorcode XXH3_64bits_update(XXH3_state_t *statePtr, const void *input, size_t length)
Consumes a block of input to an XXH3_state_t.
Definition xxhash.h:6615
XXH128_hash_t XXH3_128bits_withSecretandSeed(const void *input, size_t length, const void *secret, size_t secretSize, XXH64_hash_t seed64)
Calculates 128-bit seeded variant of XXH3 hash of data.
Definition xxhash.h:7100
XXH_errorcode XXH3_64bits_reset(XXH3_state_t *statePtr)
Resets an XXH3_state_t to begin a new hash.
Definition xxhash.h:6431
int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2)
Check equality of two XXH128_hash_t values.
Definition xxhash.h:7180
#define XXH3_SECRET_SIZE_MIN
Definition xxhash.h:1192
XXH_errorcode XXH3_freeState(XXH3_state_t *statePtr)
Frees an XXH3_state_t.
Definition xxhash.h:6389
XXH_errorcode XXH3_128bits_reset_withSecret(XXH3_state_t *statePtr, const void *secret, size_t secretSize)
Resets an XXH3_state_t with secret data to begin a new hash.
Definition xxhash.h:7131
XXH64_hash_t XXH3_64bits_withSecretandSeed(const void *data, size_t len, const void *secret, size_t secretSize, XXH64_hash_t seed)
Calculates 64/128-bit seeded variant of XXH3 hash of data.
Definition xxhash.h:6283
struct XXH64_state_s XXH64_state_t
The opaque state struct for the XXH64 streaming API.
Definition xxhash.h:927
void XXH64_canonicalFromHash(XXH64_canonical_t *dst, XXH64_hash_t hash)
Converts an XXH64_hash_t to a big endian XXH64_canonical_t.
Definition xxhash.h:3795
void XXH64_copyState(XXH64_state_t *dst_state, const XXH64_state_t *src_state)
Copies one XXH64_state_t to another.
Definition xxhash.h:3715
XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t *src)
Converts an XXH64_canonical_t to a native XXH64_hash_t.
Definition xxhash.h:3803
XXH64_hash_t XXH64(const void *input, size_t length, XXH64_hash_t seed)
Calculates the 64-bit hash of input using xxHash64.
Definition xxhash.h:3681
XXH_errorcode XXH64_freeState(XXH64_state_t *statePtr)
Frees an XXH64_state_t.
Definition xxhash.h:3708
XXH64_state_t * XXH64_createState(void)
Allocates an XXH64_state_t.
Definition xxhash.h:3703
XXH_errorcode XXH64_update(XXH64_state_t *statePtr, const void *input, size_t length)
Consumes a block of input to an XXH64_state_t.
Definition xxhash.h:3731
XXH64_hash_t XXH64_digest(const XXH64_state_t *statePtr)
Returns the calculated hash value from an XXH64_state_t.
Definition xxhash.h:3776
XXH_errorcode XXH64_reset(XXH64_state_t *statePtr, XXH64_hash_t seed)
Resets an XXH64_state_t to begin a new hash.
Definition xxhash.h:3721
#define XXH_PRIME64_1
Definition xxhash.h:3457
#define XXH_PRIME64_2
Definition xxhash.h:3458
#define XXH_PRIME64_4
Definition xxhash.h:3460
#define XXH_PRIME64_3
Definition xxhash.h:3459
#define XXH_PRIME64_5
Definition xxhash.h:3461
#define XXH_TARGET_SSE2
Allows a function to be compiled with SSE2 intrinsics.
Definition xxhash.h:5183
#define XXH_TARGET_AVX512
Like XXH_TARGET_SSE2, but for AVX512.
Definition xxhash.h:4974
#define XXH_TARGET_AVX2
Like XXH_TARGET_SSE2, but for AVX2.
Definition xxhash.h:5077
XXH_alignment
Definition xxhash.h:2834
@ XXH_aligned
Definition xxhash.h:2835
@ XXH_unaligned
Definition xxhash.h:2836
uint32_t XXH32_hash_t
An unsigned 32-bit integer.
Definition xxhash.h:587
XXH_errorcode
Exit code for the streaming API.
Definition xxhash.h:572
uint64_t XXH64_hash_t
An unsigned 64-bit integer.
Definition xxhash.h:866
#define XXH_PUBLIC_API
Marks a global symbol.
Definition xxhash.h:455
unsigned XXH_versionNumber(void)
Obtains the xxHash version.
Definition xxhash.h:2891
#define XXH_VERSION_NUMBER
Version number, encoded as two digits each.
Definition xxhash.h:552
@ XXH_ERROR
Definition xxhash.h:574
@ XXH_OK
Definition xxhash.h:573
#define XXH_ACC_ALIGN
Selects the minimum alignment for XXH3's accumulators.
Definition xxhash.h:3971
#define XXH_CPU_LITTLE_ENDIAN
Whether the target is little endian.
Definition xxhash.h:2711
#define XXH3_NEON_LANES
Controls the NEON to scalar ratio for XXH3.
Definition xxhash.h:4208
#define XXH32_ENDJMP
Whether to use a jump for XXH32_finalize.
Definition xxhash.h:2272
#define XXH_FORCE_ALIGN_CHECK
If defined to non-zero, adds a special path for aligned inputs (XXH32() and XXH64() only).
Definition xxhash.h:2221
Definition xxhash.h:1606
The return value from 128-bit hashes.
Definition xxhash.h:1383
XXH64_hash_t low64
Definition xxhash.h:1384
XXH64_hash_t high64
Definition xxhash.h:1385
Canonical (big endian) representation of XXH32_hash_t.
Definition xxhash.h:754
unsigned char digest[4]
Definition xxhash.h:755
Definition xxhash.h:1673
XXH32_hash_t bufferedSize
Definition xxhash.h:1678
XXH32_hash_t total_len_32
Definition xxhash.h:1674
XXH32_hash_t large_len
Definition xxhash.h:1675
XXH32_hash_t reserved
Definition xxhash.h:1679
XXH32_hash_t acc[4]
Definition xxhash.h:1676
unsigned char buffer[16]
Definition xxhash.h:1677
Definition xxhash.h:1773
const unsigned char * extSecret
Definition xxhash.h:1796
XXH32_hash_t bufferedSize
Definition xxhash.h:1780
XXH64_hash_t reserved64
Definition xxhash.h:1794
XXH64_hash_t totalLen
Definition xxhash.h:1786
size_t nbStripesSoFar
Definition xxhash.h:1784
XXH32_hash_t useSeed
Definition xxhash.h:1782
size_t secretLimit
Definition xxhash.h:1790
size_t nbStripesPerBlock
Definition xxhash.h:1788
XXH64_hash_t seed
Definition xxhash.h:1792
unsigned char buffer[XXH3_INTERNALBUFFER_SIZE]
Definition xxhash.h:1778
unsigned char customSecret[XXH3_SECRET_DEFAULT_SIZE]
Definition xxhash.h:1776
XXH64_hash_t acc[8]
Definition xxhash.h:1774
Canonical (big endian) representation of XXH64_hash_t.
Definition xxhash.h:1028
Definition xxhash.h:1697
unsigned char buffer[32]
Definition xxhash.h:1700
XXH32_hash_t reserved32
Definition xxhash.h:1702
XXH64_hash_t acc[4]
Definition xxhash.h:1699
XXH64_hash_t reserved64
Definition xxhash.h:1703
XXH32_hash_t bufferedSize
Definition xxhash.h:1701
XXH64_hash_t total_len
Definition xxhash.h:1698
#define XXH3_MIDSIZE_MAX
Maximum size of "short" key in bytes.
Definition xxhash.h:1948
#define XXH3_SECRET_DEFAULT_SIZE
Default Secret's size.
Definition xxhash.h:1749
#define XXH3_INITSTATE(XXH3_state_ptr)
Initializes a stack-allocated XXH3_state_s.
Definition xxhash.h:1815