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