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