xxhash.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. /*
  6. xxHash - Extremely Fast Hash algorithm
  7. Header File
  8. Copyright (C) 2012-2016, Yann Collet.
  9. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above
  16. copyright notice, this list of conditions and the following disclaimer
  17. in the documentation and/or other materials provided with the
  18. distribution.
  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. You can contact the author at :
  31. - xxHash source repository : https://github.com/Cyan4973/xxHash
  32. */
  33. /* Notice extracted from xxHash homepage :
  34. xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
  35. It also successfully passes all tests from the SMHasher suite.
  36. Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
  37. Name Speed Q.Score Author
  38. xxHash 5.4 GB/s 10
  39. CrapWow 3.2 GB/s 2 Andrew
  40. MumurHash 3a 2.7 GB/s 10 Austin Appleby
  41. SpookyHash 2.0 GB/s 10 Bob Jenkins
  42. SBox 1.4 GB/s 9 Bret Mulvey
  43. Lookup3 1.2 GB/s 9 Bob Jenkins
  44. SuperFastHash 1.2 GB/s 1 Paul Hsieh
  45. CityHash64 1.05 GB/s 10 Pike & Alakuijala
  46. FNV 0.55 GB/s 5 Fowler, Noll, Vo
  47. CRC32 0.43 GB/s # 9
  48. MD5-32 0.33 GB/s 10 Ronald L. Rivest
  49. SHA1-32 0.28 GB/s 10
  50. Note #: other CRC32 implementations can be over 40x faster than SMHasher's:
  51. http://fastcompression.blogspot.com/2019/03/presenting-xxh3.html?showComment=1552696407071#c3490092340461170735
  52. Q.Score is a measure of quality of the hash function.
  53. It depends on successfully passing SMHasher test set.
  54. 10 is a perfect score.
  55. A 64-bit version, named XXH64, is available since r35.
  56. It offers much better speed, but for 64-bit applications only.
  57. Name Speed on 64 bits Speed on 32 bits
  58. XXH64 13.8 GB/s 1.9 GB/s
  59. XXH32 6.8 GB/s 6.0 GB/s
  60. */
  61. #ifndef XXHASH_H_5627135585666179
  62. #define XXHASH_H_5627135585666179 1
  63. /* BEGIN RocksDB customizations */
  64. #ifndef XXH_STATIC_LINKING_ONLY
  65. #define XXH_STATIC_LINKING_ONLY 1 /* access experimental APIs like XXH3 */
  66. #endif
  67. #define XXH_NAMESPACE ROCKSDB_
  68. /* END RocksDB customizations */
  69. #if defined (__cplusplus)
  70. extern "C" {
  71. #endif
  72. /* ****************************
  73. * Definitions
  74. ******************************/
  75. #include <stddef.h> /* size_t */
  76. typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
  77. /* ****************************
  78. * API modifier
  79. ******************************/
  80. /** XXH_INLINE_ALL (and XXH_PRIVATE_API)
  81. * This build macro includes xxhash functions in `static` mode
  82. * in order to inline them, and remove their symbol from the public list.
  83. * Inlining offers great performance improvement on small keys,
  84. * and dramatic ones when length is expressed as a compile-time constant.
  85. * See https://fastcompression.blogspot.com/2018/03/xxhash-for-small-keys-impressive-power.html .
  86. * Methodology :
  87. * #define XXH_INLINE_ALL
  88. * #include "xxhash.h"
  89. * `xxhash.c` is automatically included.
  90. * It's not useful to compile and link it as a separate object.
  91. */
  92. #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
  93. # ifndef XXH_STATIC_LINKING_ONLY
  94. # define XXH_STATIC_LINKING_ONLY
  95. # endif
  96. # if defined(__GNUC__)
  97. # define XXH_PUBLIC_API static __inline __attribute__((unused))
  98. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  99. # define XXH_PUBLIC_API static inline
  100. # elif defined(_MSC_VER)
  101. # define XXH_PUBLIC_API static __inline
  102. # else
  103. /* this version may generate warnings for unused static functions */
  104. # define XXH_PUBLIC_API static
  105. # endif
  106. #else
  107. # if defined(WIN32) && defined(_MSC_VER) && (defined(XXH_IMPORT) || defined(XXH_EXPORT))
  108. # ifdef XXH_EXPORT
  109. # define XXH_PUBLIC_API __declspec(dllexport)
  110. # elif XXH_IMPORT
  111. # define XXH_PUBLIC_API __declspec(dllimport)
  112. # endif
  113. # else
  114. # define XXH_PUBLIC_API /* do nothing */
  115. # endif
  116. #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
  117. /*! XXH_NAMESPACE, aka Namespace Emulation :
  118. *
  119. * If you want to include _and expose_ xxHash functions from within your own library,
  120. * but also want to avoid symbol collisions with other libraries which may also include xxHash,
  121. *
  122. * you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library
  123. * with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values).
  124. *
  125. * Note that no change is required within the calling program as long as it includes `xxhash.h` :
  126. * regular symbol name will be automatically translated by this header.
  127. */
  128. #ifdef XXH_NAMESPACE
  129. # define XXH_CAT(A,B) A##B
  130. # define XXH_NAME2(A,B) XXH_CAT(A,B)
  131. # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
  132. # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
  133. # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
  134. # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
  135. # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
  136. # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
  137. # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
  138. # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
  139. # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
  140. # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
  141. # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
  142. # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
  143. # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
  144. # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
  145. # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
  146. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
  147. # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
  148. # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
  149. # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
  150. #endif
  151. /* *************************************
  152. * Version
  153. ***************************************/
  154. #define XXH_VERSION_MAJOR 0
  155. #define XXH_VERSION_MINOR 7
  156. #define XXH_VERSION_RELEASE 2
  157. #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
  158. XXH_PUBLIC_API unsigned XXH_versionNumber (void);
  159. /*-**********************************************************************
  160. * 32-bit hash
  161. ************************************************************************/
  162. #if !defined (__VMS) \
  163. && (defined (__cplusplus) \
  164. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  165. # include <stdint.h>
  166. typedef uint32_t XXH32_hash_t;
  167. #else
  168. # include <limits.h>
  169. # if UINT_MAX == 0xFFFFFFFFUL
  170. typedef unsigned int XXH32_hash_t;
  171. # else
  172. # if ULONG_MAX == 0xFFFFFFFFUL
  173. typedef unsigned long XXH32_hash_t;
  174. # else
  175. # error "unsupported platform : need a 32-bit type"
  176. # endif
  177. # endif
  178. #endif
  179. /*! XXH32() :
  180. Calculate the 32-bit hash of sequence "length" bytes stored at memory address "input".
  181. The memory between input & input+length must be valid (allocated and read-accessible).
  182. "seed" can be used to alter the result predictably.
  183. Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s */
  184. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, XXH32_hash_t seed);
  185. /*====== Streaming ======*/
  186. /*
  187. * Streaming functions generate the xxHash value from an incrememtal input.
  188. * This method is slower than single-call functions, due to state management.
  189. * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized.
  190. *
  191. * XXH state must first be allocated, using XXH*_createState() .
  192. *
  193. * Start a new hash by initializing state with a seed, using XXH*_reset().
  194. *
  195. * Then, feed the hash state by calling XXH*_update() as many times as necessary.
  196. * The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
  197. *
  198. * Finally, a hash value can be produced anytime, by using XXH*_digest().
  199. * This function returns the nn-bits hash as an int or long long.
  200. *
  201. * It's still possible to continue inserting input into the hash state after a digest,
  202. * and generate some new hash values later on, by invoking again XXH*_digest().
  203. *
  204. * When done, release the state, using XXH*_freeState().
  205. */
  206. typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
  207. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
  208. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
  209. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
  210. XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, XXH32_hash_t seed);
  211. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
  212. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
  213. /*====== Canonical representation ======*/
  214. /* Default return values from XXH functions are basic unsigned 32 and 64 bits.
  215. * This the simplest and fastest format for further post-processing.
  216. * However, this leaves open the question of what is the order of bytes,
  217. * since little and big endian conventions will write the same number differently.
  218. *
  219. * The canonical representation settles this issue,
  220. * by mandating big-endian convention,
  221. * aka, the same convention as human-readable numbers (large digits first).
  222. * When writing hash values to storage, sending them over a network, or printing them,
  223. * it's highly recommended to use the canonical representation,
  224. * to ensure portability across a wider range of systems, present and future.
  225. *
  226. * The following functions allow transformation of hash values into and from canonical format.
  227. */
  228. typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
  229. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
  230. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
  231. #ifndef XXH_NO_LONG_LONG
  232. /*-**********************************************************************
  233. * 64-bit hash
  234. ************************************************************************/
  235. #if !defined (__VMS) \
  236. && (defined (__cplusplus) \
  237. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  238. # include <stdint.h>
  239. typedef uint64_t XXH64_hash_t;
  240. #else
  241. /* the following type must have a width of 64-bit */
  242. typedef unsigned long long XXH64_hash_t;
  243. #endif
  244. /*! XXH64() :
  245. Calculate the 64-bit hash of sequence of length "len" stored at memory address "input".
  246. "seed" can be used to alter the result predictably.
  247. This function runs faster on 64-bit systems, but slower on 32-bit systems (see benchmark).
  248. */
  249. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, XXH64_hash_t seed);
  250. /*====== Streaming ======*/
  251. typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
  252. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
  253. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
  254. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dst_state, const XXH64_state_t* src_state);
  255. XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, XXH64_hash_t seed);
  256. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
  257. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
  258. /*====== Canonical representation ======*/
  259. typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
  260. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
  261. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
  262. #endif /* XXH_NO_LONG_LONG */
  263. #ifdef XXH_STATIC_LINKING_ONLY
  264. /* ================================================================================================
  265. This section contains declarations which are not guaranteed to remain stable.
  266. They may change in future versions, becoming incompatible with a different version of the library.
  267. These declarations should only be used with static linking.
  268. Never use them in association with dynamic linking !
  269. =================================================================================================== */
  270. /* These definitions are only present to allow
  271. * static allocation of XXH state, on stack or in a struct for example.
  272. * Never **ever** use members directly. */
  273. struct XXH32_state_s {
  274. XXH32_hash_t total_len_32;
  275. XXH32_hash_t large_len;
  276. XXH32_hash_t v1;
  277. XXH32_hash_t v2;
  278. XXH32_hash_t v3;
  279. XXH32_hash_t v4;
  280. XXH32_hash_t mem32[4];
  281. XXH32_hash_t memsize;
  282. XXH32_hash_t reserved; /* never read nor write, might be removed in a future version */
  283. }; /* typedef'd to XXH32_state_t */
  284. #ifndef XXH_NO_LONG_LONG /* remove 64-bit support */
  285. struct XXH64_state_s {
  286. XXH64_hash_t total_len;
  287. XXH64_hash_t v1;
  288. XXH64_hash_t v2;
  289. XXH64_hash_t v3;
  290. XXH64_hash_t v4;
  291. XXH64_hash_t mem64[4];
  292. XXH32_hash_t memsize;
  293. XXH32_hash_t reserved32; /* required for padding anyway */
  294. XXH64_hash_t reserved64; /* never read nor write, might be removed in a future version */
  295. }; /* typedef'd to XXH64_state_t */
  296. #endif /* XXH_NO_LONG_LONG */
  297. /*-**********************************************************************
  298. * XXH3
  299. * New experimental hash
  300. ************************************************************************/
  301. #ifndef XXH_NO_LONG_LONG
  302. /* ============================================
  303. * XXH3 is a new hash algorithm,
  304. * featuring improved speed performance for both small and large inputs.
  305. * See full speed analysis at : http://fastcompression.blogspot.com/2019/03/presenting-xxh3.html
  306. * In general, expect XXH3 to run about ~2x faster on large inputs,
  307. * and >3x faster on small ones, though exact differences depend on platform.
  308. *
  309. * The algorithm is portable, will generate the same hash on all platforms.
  310. * It benefits greatly from vectorization units, but does not require it.
  311. *
  312. * XXH3 offers 2 variants, _64bits and _128bits.
  313. * When only 64 bits are needed, prefer calling the _64bits variant :
  314. * it reduces the amount of mixing, resulting in faster speed on small inputs.
  315. * It's also generally simpler to manipulate a scalar return type than a struct.
  316. *
  317. * The XXH3 algorithm is still considered experimental.
  318. * Produced results can still change between versions.
  319. * Results produced by v0.7.x are not comparable with results from v0.7.y .
  320. * It's nonetheless possible to use XXH3 for ephemeral data (local sessions),
  321. * but avoid storing values in long-term storage for later reads.
  322. *
  323. * The API supports one-shot hashing, streaming mode, and custom secrets.
  324. *
  325. * There are still a number of opened questions that community can influence during the experimental period.
  326. * I'm trying to list a few of them below, though don't consider this list as complete.
  327. *
  328. * - 128-bits output type : currently defined as a structure of two 64-bits fields.
  329. * That's because 128-bit values do not exist in C standard.
  330. * Note that it means that, at byte level, result is not identical depending on endianess.
  331. * However, at field level, they are identical on all platforms.
  332. * The canonical representation solves the issue of identical byte-level representation across platforms,
  333. * which is necessary for serialization.
  334. * Q1 : Would there be a better representation for a 128-bit hash result ?
  335. * Q2 : Are the names of the inner 64-bit fields important ? Should they be changed ?
  336. *
  337. * - Prototype XXH128() : XXH128() uses the same arguments as XXH64(), for consistency.
  338. * It means it maps to XXH3p_128bits_withSeed().
  339. * This variant is slightly slower than XXH3p_128bits(),
  340. * because the seed is now part of the algorithm, and can't be simplified.
  341. * Is that a good idea ?
  342. *
  343. * - Seed type for XXH128() : currently, it's a single 64-bit value, like the 64-bit variant.
  344. * It could be argued that it's more logical to offer a 128-bit seed input parameter for a 128-bit hash.
  345. * But 128-bit seed is more difficult to use, since it requires to pass a structure instead of a scalar value.
  346. * Such a variant could either replace current one, or become an additional one.
  347. * Farmhash, for example, offers both variants (the 128-bits seed variant is called `doubleSeed`).
  348. * Follow up question : if both 64-bit and 128-bit seeds are allowed, which variant should be called XXH128 ?
  349. *
  350. * - Result for len==0 : Currently, the result of hashing a zero-length input is always `0`.
  351. * It seems okay as a return value when using "default" secret and seed.
  352. * But is it still fine to return `0` when secret or seed are non-default ?
  353. * Are there use cases which could depend on generating a different hash result for zero-length input when the secret is different ?
  354. *
  355. * - Consistency (1) : Streaming XXH128 uses an XXH3 state, which is the same state as XXH3p_64bits().
  356. * It means a 128bit streaming loop must invoke the following symbols :
  357. * XXH3p_createState(), XXH3p_128bits_reset(), XXH3p_128bits_update() (loop), XXH3p_128bits_digest(), XXH3p_freeState().
  358. * Is that consistent enough ?
  359. *
  360. * - Consistency (2) : The canonical representation of `XXH3p_64bits` is provided by existing functions
  361. * XXH64_canonicalFromHash(), and reverse operation XXH64_hashFromCanonical().
  362. * As a mirror, canonical functions for XXH128_hash_t results generated by `XXH3p_128bits`
  363. * are XXH128_canonicalFromHash() and XXH128_hashFromCanonical().
  364. * Which means, `XXH3` doesn't appear in the names, because canonical functions operate on a type,
  365. * independently of which algorithm was used to generate that type.
  366. * Is that consistent enough ?
  367. */
  368. #ifdef XXH_NAMESPACE
  369. # define XXH3p_64bits XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits)
  370. # define XXH3p_64bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_withSecret)
  371. # define XXH3p_64bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_withSeed)
  372. # define XXH3p_createState XXH_NAME2(XXH_NAMESPACE, XXH3p_createState)
  373. # define XXH3p_freeState XXH_NAME2(XXH_NAMESPACE, XXH3p_freeState)
  374. # define XXH3p_copyState XXH_NAME2(XXH_NAMESPACE, XXH3p_copyState)
  375. # define XXH3p_64bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_reset)
  376. # define XXH3p_64bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_reset_withSeed)
  377. # define XXH3p_64bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_reset_withSecret)
  378. # define XXH3p_64bits_update XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_update)
  379. # define XXH3p_64bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3p_64bits_digest)
  380. #endif
  381. /* XXH3p_64bits() :
  382. * default 64-bit variant, using default secret and default seed of 0.
  383. * It's the fastest variant. */
  384. XXH_PUBLIC_API XXH64_hash_t XXH3p_64bits(const void* data, size_t len);
  385. /* XXH3p_64bits_withSecret() :
  386. * It's possible to provide any blob of bytes as a "secret" to generate the hash.
  387. * This makes it more difficult for an external actor to prepare an intentional collision.
  388. * The secret *must* be large enough (>= XXH3p_SECRET_SIZE_MIN).
  389. * It should consist of random bytes.
  390. * Avoid repeating same character, or sequences of bytes,
  391. * and especially avoid swathes of \0.
  392. * Failure to respect these conditions will result in a poor quality hash.
  393. */
  394. #define XXH3p_SECRET_SIZE_MIN 136
  395. XXH_PUBLIC_API XXH64_hash_t XXH3p_64bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
  396. /* XXH3p_64bits_withSeed() :
  397. * This variant generates on the fly a custom secret,
  398. * based on the default secret, altered using the `seed` value.
  399. * While this operation is decently fast, note that it's not completely free.
  400. * note : seed==0 produces same results as XXH3p_64bits() */
  401. XXH_PUBLIC_API XXH64_hash_t XXH3p_64bits_withSeed(const void* data, size_t len, XXH64_hash_t seed);
  402. /* streaming 64-bit */
  403. #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11+ */
  404. # include <stdalign.h>
  405. # define XXH_ALIGN(n) alignas(n)
  406. #elif defined(__GNUC__)
  407. # define XXH_ALIGN(n) __attribute__ ((aligned(n)))
  408. #elif defined(_MSC_VER)
  409. # define XXH_ALIGN(n) __declspec(align(n))
  410. #else
  411. # define XXH_ALIGN(n) /* disabled */
  412. #endif
  413. typedef struct XXH3p_state_s XXH3p_state_t;
  414. #define XXH3p_SECRET_DEFAULT_SIZE 192 /* minimum XXH3p_SECRET_SIZE_MIN */
  415. #define XXH3p_INTERNALBUFFER_SIZE 256
  416. struct XXH3p_state_s {
  417. XXH_ALIGN(64) XXH64_hash_t acc[8];
  418. XXH_ALIGN(64) unsigned char customSecret[XXH3p_SECRET_DEFAULT_SIZE]; /* used to store a custom secret generated from the seed. Makes state larger. Design might change */
  419. XXH_ALIGN(64) unsigned char buffer[XXH3p_INTERNALBUFFER_SIZE];
  420. XXH32_hash_t bufferedSize;
  421. XXH32_hash_t nbStripesPerBlock;
  422. XXH32_hash_t nbStripesSoFar;
  423. XXH32_hash_t secretLimit;
  424. XXH32_hash_t reserved32;
  425. XXH32_hash_t reserved32_2;
  426. XXH64_hash_t totalLen;
  427. XXH64_hash_t seed;
  428. XXH64_hash_t reserved64;
  429. const unsigned char* secret; /* note : there is some padding after, due to alignment on 64 bytes */
  430. }; /* typedef'd to XXH3p_state_t */
  431. /* Streaming requires state maintenance.
  432. * This operation costs memory and cpu.
  433. * As a consequence, streaming is slower than one-shot hashing.
  434. * For better performance, prefer using one-shot functions whenever possible. */
  435. XXH_PUBLIC_API XXH3p_state_t* XXH3p_createState(void);
  436. XXH_PUBLIC_API XXH_errorcode XXH3p_freeState(XXH3p_state_t* statePtr);
  437. XXH_PUBLIC_API void XXH3p_copyState(XXH3p_state_t* dst_state, const XXH3p_state_t* src_state);
  438. /* XXH3p_64bits_reset() :
  439. * initialize with default parameters.
  440. * result will be equivalent to `XXH3p_64bits()`. */
  441. XXH_PUBLIC_API XXH_errorcode XXH3p_64bits_reset(XXH3p_state_t* statePtr);
  442. /* XXH3p_64bits_reset_withSeed() :
  443. * generate a custom secret from `seed`, and store it into state.
  444. * digest will be equivalent to `XXH3p_64bits_withSeed()`. */
  445. XXH_PUBLIC_API XXH_errorcode XXH3p_64bits_reset_withSeed(XXH3p_state_t* statePtr, XXH64_hash_t seed);
  446. /* XXH3p_64bits_reset_withSecret() :
  447. * `secret` is referenced, and must outlive the hash streaming session.
  448. * secretSize must be >= XXH3p_SECRET_SIZE_MIN.
  449. */
  450. XXH_PUBLIC_API XXH_errorcode XXH3p_64bits_reset_withSecret(XXH3p_state_t* statePtr, const void* secret, size_t secretSize);
  451. XXH_PUBLIC_API XXH_errorcode XXH3p_64bits_update (XXH3p_state_t* statePtr, const void* input, size_t length);
  452. XXH_PUBLIC_API XXH64_hash_t XXH3p_64bits_digest (const XXH3p_state_t* statePtr);
  453. /* 128-bit */
  454. #ifdef XXH_NAMESPACE
  455. # define XXH128 XXH_NAME2(XXH_NAMESPACE, XXH128)
  456. # define XXH3p_128bits XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits)
  457. # define XXH3p_128bits_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_withSeed)
  458. # define XXH3p_128bits_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_withSecret)
  459. # define XXH3p_128bits_reset XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_reset)
  460. # define XXH3p_128bits_reset_withSeed XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_reset_withSeed)
  461. # define XXH3p_128bits_reset_withSecret XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_reset_withSecret)
  462. # define XXH3p_128bits_update XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_update)
  463. # define XXH3p_128bits_digest XXH_NAME2(XXH_NAMESPACE, XXH3p_128bits_digest)
  464. # define XXH128_isEqual XXH_NAME2(XXH_NAMESPACE, XXH128_isEqual)
  465. # define XXH128_cmp XXH_NAME2(XXH_NAMESPACE, XXH128_cmp)
  466. # define XXH128_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH128_canonicalFromHash)
  467. # define XXH128_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH128_hashFromCanonical)
  468. #endif
  469. typedef struct {
  470. XXH64_hash_t low64;
  471. XXH64_hash_t high64;
  472. } XXH128_hash_t;
  473. XXH_PUBLIC_API XXH128_hash_t XXH128(const void* data, size_t len, XXH64_hash_t seed);
  474. XXH_PUBLIC_API XXH128_hash_t XXH3p_128bits(const void* data, size_t len);
  475. XXH_PUBLIC_API XXH128_hash_t XXH3p_128bits_withSeed(const void* data, size_t len, XXH64_hash_t seed); /* == XXH128() */
  476. XXH_PUBLIC_API XXH128_hash_t XXH3p_128bits_withSecret(const void* data, size_t len, const void* secret, size_t secretSize);
  477. XXH_PUBLIC_API XXH_errorcode XXH3p_128bits_reset(XXH3p_state_t* statePtr);
  478. XXH_PUBLIC_API XXH_errorcode XXH3p_128bits_reset_withSeed(XXH3p_state_t* statePtr, XXH64_hash_t seed);
  479. XXH_PUBLIC_API XXH_errorcode XXH3p_128bits_reset_withSecret(XXH3p_state_t* statePtr, const void* secret, size_t secretSize);
  480. XXH_PUBLIC_API XXH_errorcode XXH3p_128bits_update (XXH3p_state_t* statePtr, const void* input, size_t length);
  481. XXH_PUBLIC_API XXH128_hash_t XXH3p_128bits_digest (const XXH3p_state_t* statePtr);
  482. /* Note : for better performance, following functions can be inlined,
  483. * using XXH_INLINE_ALL */
  484. /* return : 1 is equal, 0 if different */
  485. XXH_PUBLIC_API int XXH128_isEqual(XXH128_hash_t h1, XXH128_hash_t h2);
  486. /* This comparator is compatible with stdlib's qsort().
  487. * return : >0 if *h128_1 > *h128_2
  488. * <0 if *h128_1 < *h128_2
  489. * =0 if *h128_1 == *h128_2 */
  490. XXH_PUBLIC_API int XXH128_cmp(const void* h128_1, const void* h128_2);
  491. /*====== Canonical representation ======*/
  492. typedef struct { unsigned char digest[16]; } XXH128_canonical_t;
  493. XXH_PUBLIC_API void XXH128_canonicalFromHash(XXH128_canonical_t* dst, XXH128_hash_t hash);
  494. XXH_PUBLIC_API XXH128_hash_t XXH128_hashFromCanonical(const XXH128_canonical_t* src);
  495. #endif /* XXH_NO_LONG_LONG */
  496. /*-**********************************************************************
  497. * XXH_INLINE_ALL
  498. ************************************************************************/
  499. #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
  500. # include "xxhash.cc" /* include xxhash function bodies as `static`, for inlining */
  501. #endif
  502. #endif /* XXH_STATIC_LINKING_ONLY */
  503. #if defined (__cplusplus)
  504. }
  505. #endif
  506. #endif /* XXHASH_H_5627135585666179 */