xxhash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. xxHash - Extremely Fast Hash algorithm
  3. Header File
  4. Copyright (C) 2012-2016, Yann Collet.
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - xxHash source repository : https://github.com/Cyan4973/xxHash
  28. */
  29. /* Notice extracted from xxHash homepage :
  30. xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
  31. It also successfully passes all tests from the SMHasher suite.
  32. Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
  33. Name Speed Q.Score Author
  34. xxHash 5.4 GB/s 10
  35. CrapWow 3.2 GB/s 2 Andrew
  36. MumurHash 3a 2.7 GB/s 10 Austin Appleby
  37. SpookyHash 2.0 GB/s 10 Bob Jenkins
  38. SBox 1.4 GB/s 9 Bret Mulvey
  39. Lookup3 1.2 GB/s 9 Bob Jenkins
  40. SuperFastHash 1.2 GB/s 1 Paul Hsieh
  41. CityHash64 1.05 GB/s 10 Pike & Alakuijala
  42. FNV 0.55 GB/s 5 Fowler, Noll, Vo
  43. CRC32 0.43 GB/s 9
  44. MD5-32 0.33 GB/s 10 Ronald L. Rivest
  45. SHA1-32 0.28 GB/s 10
  46. Q.Score is a measure of quality of the hash function.
  47. It depends on successfully passing SMHasher test set.
  48. 10 is a perfect score.
  49. A 64-bit version, named XXH64, is available since r35.
  50. It offers much better speed, but for 64-bit applications only.
  51. Name Speed on 64 bits Speed on 32 bits
  52. XXH64 13.8 GB/s 1.9 GB/s
  53. XXH32 6.8 GB/s 6.0 GB/s
  54. */
  55. #ifndef XXHASH_H_5627135585666179
  56. #define XXHASH_H_5627135585666179 1
  57. #if defined (__cplusplus)
  58. extern "C" {
  59. #endif
  60. /* ****************************
  61. * Definitions
  62. ******************************/
  63. #include <stddef.h> /* size_t */
  64. typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
  65. /* ****************************
  66. * API modifier
  67. ******************************/
  68. /** XXH_INLINE_ALL (and XXH_PRIVATE_API)
  69. * This is useful to include xxhash functions in `static` mode
  70. * in order to inline them, and remove their symbol from the public list.
  71. * Inlining can offer dramatic performance improvement on small keys.
  72. * Methodology :
  73. * #define XXH_INLINE_ALL
  74. * #include "xxhash.h"
  75. * `xxhash.c` is automatically included.
  76. * It's not useful to compile and link it as a separate module.
  77. */
  78. #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
  79. # ifndef XXH_STATIC_LINKING_ONLY
  80. # define XXH_STATIC_LINKING_ONLY
  81. # endif
  82. # if defined(__GNUC__)
  83. # define XXH_PUBLIC_API static __inline __attribute__((unused))
  84. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  85. # define XXH_PUBLIC_API static inline
  86. # elif defined(_MSC_VER)
  87. # define XXH_PUBLIC_API static __inline
  88. # else
  89. /* this version may generate warnings for unused static functions */
  90. # define XXH_PUBLIC_API static
  91. # endif
  92. #else
  93. # define XXH_PUBLIC_API /* do nothing */
  94. #endif /* XXH_INLINE_ALL || XXH_PRIVATE_API */
  95. /*! XXH_NAMESPACE, aka Namespace Emulation :
  96. *
  97. * If you want to include _and expose_ xxHash functions from within your own library,
  98. * but also want to avoid symbol collisions with other libraries which may also include xxHash,
  99. *
  100. * you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library
  101. * with the value of XXH_NAMESPACE (therefore, avoid NULL and numeric values).
  102. *
  103. * Note that no change is required within the calling program as long as it includes `xxhash.h` :
  104. * regular symbol name will be automatically translated by this header.
  105. */
  106. #ifdef XXH_NAMESPACE
  107. # define XXH_CAT(A,B) A##B
  108. # define XXH_NAME2(A,B) XXH_CAT(A,B)
  109. # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
  110. # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
  111. # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
  112. # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
  113. # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
  114. # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
  115. # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
  116. # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
  117. # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
  118. # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
  119. # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
  120. # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
  121. # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
  122. # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
  123. # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
  124. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
  125. # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
  126. # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
  127. # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
  128. #endif
  129. /* *************************************
  130. * Version
  131. ***************************************/
  132. #define XXH_VERSION_MAJOR 0
  133. #define XXH_VERSION_MINOR 6
  134. #define XXH_VERSION_RELEASE 5
  135. #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
  136. XXH_PUBLIC_API unsigned XXH_versionNumber (void);
  137. /*-**********************************************************************
  138. * 32-bit hash
  139. ************************************************************************/
  140. typedef unsigned int XXH32_hash_t;
  141. /*! XXH32() :
  142. Calculate the 32-bit hash of sequence "length" bytes stored at memory address "input".
  143. The memory between input & input+length must be valid (allocated and read-accessible).
  144. "seed" can be used to alter the result predictably.
  145. Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s */
  146. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);
  147. /*====== Streaming ======*/
  148. typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
  149. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
  150. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
  151. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dst_state, const XXH32_state_t* src_state);
  152. XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed);
  153. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
  154. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
  155. /*
  156. * Streaming functions generate the xxHash of an input provided in multiple segments.
  157. * Note that, for small input, they are slower than single-call functions, due to state management.
  158. * For small inputs, prefer `XXH32()` and `XXH64()`, which are better optimized.
  159. *
  160. * XXH state must first be allocated, using XXH*_createState() .
  161. *
  162. * Start a new hash by initializing state with a seed, using XXH*_reset().
  163. *
  164. * Then, feed the hash state by calling XXH*_update() as many times as necessary.
  165. * The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
  166. *
  167. * Finally, a hash value can be produced anytime, by using XXH*_digest().
  168. * This function returns the nn-bits hash as an int or long long.
  169. *
  170. * It's still possible to continue inserting input into the hash state after a digest,
  171. * and generate some new hashes later on, by calling again XXH*_digest().
  172. *
  173. * When done, free XXH state space if it was allocated dynamically.
  174. */
  175. /*====== Canonical representation ======*/
  176. typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
  177. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
  178. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
  179. /* Default result type for XXH functions are primitive unsigned 32 and 64 bits.
  180. * The canonical representation uses human-readable write convention, aka big-endian (large digits first).
  181. * These functions allow transformation of hash result into and from its canonical format.
  182. * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
  183. */
  184. #ifndef XXH_NO_LONG_LONG
  185. /*-**********************************************************************
  186. * 64-bit hash
  187. ************************************************************************/
  188. typedef unsigned long long XXH64_hash_t;
  189. /*! XXH64() :
  190. Calculate the 64-bit hash of sequence of length "len" stored at memory address "input".
  191. "seed" can be used to alter the result predictably.
  192. This function runs faster on 64-bit systems, but slower on 32-bit systems (see benchmark).
  193. */
  194. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);
  195. /*====== Streaming ======*/
  196. typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
  197. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
  198. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
  199. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dst_state, const XXH64_state_t* src_state);
  200. XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed);
  201. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
  202. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
  203. /*====== Canonical representation ======*/
  204. typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
  205. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
  206. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
  207. #endif /* XXH_NO_LONG_LONG */
  208. #ifdef XXH_STATIC_LINKING_ONLY
  209. /* ================================================================================================
  210. This section contains declarations which are not guaranteed to remain stable.
  211. They may change in future versions, becoming incompatible with a different version of the library.
  212. These declarations should only be used with static linking.
  213. Never use them in association with dynamic linking !
  214. =================================================================================================== */
  215. /* These definitions are only present to allow
  216. * static allocation of XXH state, on stack or in a struct for example.
  217. * Never **ever** use members directly. */
  218. #if !defined (__VMS) \
  219. && (defined (__cplusplus) \
  220. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  221. # include <stdint.h>
  222. struct XXH32_state_s {
  223. uint32_t total_len_32;
  224. uint32_t large_len;
  225. uint32_t v1;
  226. uint32_t v2;
  227. uint32_t v3;
  228. uint32_t v4;
  229. uint32_t mem32[4];
  230. uint32_t memsize;
  231. uint32_t reserved; /* never read nor write, might be removed in a future version */
  232. }; /* typedef'd to XXH32_state_t */
  233. struct XXH64_state_s {
  234. uint64_t total_len;
  235. uint64_t v1;
  236. uint64_t v2;
  237. uint64_t v3;
  238. uint64_t v4;
  239. uint64_t mem64[4];
  240. uint32_t memsize;
  241. uint32_t reserved[2]; /* never read nor write, might be removed in a future version */
  242. }; /* typedef'd to XXH64_state_t */
  243. # else
  244. struct XXH32_state_s {
  245. unsigned total_len_32;
  246. unsigned large_len;
  247. unsigned v1;
  248. unsigned v2;
  249. unsigned v3;
  250. unsigned v4;
  251. unsigned mem32[4];
  252. unsigned memsize;
  253. unsigned reserved; /* never read nor write, might be removed in a future version */
  254. }; /* typedef'd to XXH32_state_t */
  255. # ifndef XXH_NO_LONG_LONG /* remove 64-bit support */
  256. struct XXH64_state_s {
  257. unsigned long long total_len;
  258. unsigned long long v1;
  259. unsigned long long v2;
  260. unsigned long long v3;
  261. unsigned long long v4;
  262. unsigned long long mem64[4];
  263. unsigned memsize;
  264. unsigned reserved[2]; /* never read nor write, might be removed in a future version */
  265. }; /* typedef'd to XXH64_state_t */
  266. # endif
  267. # endif
  268. #if defined(XXH_INLINE_ALL) || defined(XXH_PRIVATE_API)
  269. # include "xxhash.c" /* include xxhash function bodies as `static`, for inlining */
  270. #endif
  271. #endif /* XXH_STATIC_LINKING_ONLY */
  272. #if defined (__cplusplus)
  273. }
  274. #endif
  275. #endif /* XXHASH_H_5627135585666179 */