xxhash.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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-bits version, named XXH64, is available since r35.
  50. It offers much better speed, but for 64-bits 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. #if defined (__cplusplus)
  56. extern "C" {
  57. #endif
  58. #ifndef XXHASH_H_5627135585666179
  59. #define XXHASH_H_5627135585666179 1
  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_PRIVATE_API
  69. * This is useful if you want to include xxhash functions in `static` mode
  70. * in order to inline them, and remove their symbol from the public list.
  71. * Methodology :
  72. * #define XXH_PRIVATE_API
  73. * #include "xxhash.h"
  74. * `xxhash.c` is automatically included.
  75. * It's not useful to compile and link it as a separate module anymore.
  76. */
  77. #ifdef XXH_PRIVATE_API
  78. # ifndef XXH_STATIC_LINKING_ONLY
  79. # define XXH_STATIC_LINKING_ONLY
  80. # endif
  81. # if defined(__GNUC__)
  82. # define XXH_PUBLIC_API static __inline __attribute__((unused))
  83. # elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  84. # define XXH_PUBLIC_API static inline
  85. # elif defined(_MSC_VER)
  86. # define XXH_PUBLIC_API static __inline
  87. # else
  88. # define XXH_PUBLIC_API static /* this version may generate warnings for unused static functions; disable the relevant warning */
  89. # endif
  90. #else
  91. # define XXH_PUBLIC_API /* do nothing */
  92. #endif /* XXH_PRIVATE_API */
  93. /*!XXH_NAMESPACE, aka Namespace Emulation :
  94. If you want to include _and expose_ xxHash functions from within your own library,
  95. but also want to avoid symbol collisions with another library which also includes xxHash,
  96. you can use XXH_NAMESPACE, to automatically prefix any public symbol from xxhash library
  97. with the value of XXH_NAMESPACE (so avoid to keep it NULL and avoid numeric values).
  98. Note that no change is required within the calling program as long as it includes `xxhash.h` :
  99. regular symbol name will be automatically translated by this header.
  100. */
  101. #ifdef XXH_NAMESPACE
  102. # define XXH_CAT(A,B) A##B
  103. # define XXH_NAME2(A,B) XXH_CAT(A,B)
  104. # define XXH32 XXH_NAME2(XXH_NAMESPACE, XXH32)
  105. # define XXH64 XXH_NAME2(XXH_NAMESPACE, XXH64)
  106. # define XXH_versionNumber XXH_NAME2(XXH_NAMESPACE, XXH_versionNumber)
  107. # define XXH32_createState XXH_NAME2(XXH_NAMESPACE, XXH32_createState)
  108. # define XXH64_createState XXH_NAME2(XXH_NAMESPACE, XXH64_createState)
  109. # define XXH32_freeState XXH_NAME2(XXH_NAMESPACE, XXH32_freeState)
  110. # define XXH64_freeState XXH_NAME2(XXH_NAMESPACE, XXH64_freeState)
  111. # define XXH32_reset XXH_NAME2(XXH_NAMESPACE, XXH32_reset)
  112. # define XXH64_reset XXH_NAME2(XXH_NAMESPACE, XXH64_reset)
  113. # define XXH32_update XXH_NAME2(XXH_NAMESPACE, XXH32_update)
  114. # define XXH64_update XXH_NAME2(XXH_NAMESPACE, XXH64_update)
  115. # define XXH32_digest XXH_NAME2(XXH_NAMESPACE, XXH32_digest)
  116. # define XXH64_digest XXH_NAME2(XXH_NAMESPACE, XXH64_digest)
  117. # define XXH32_copyState XXH_NAME2(XXH_NAMESPACE, XXH32_copyState)
  118. # define XXH64_copyState XXH_NAME2(XXH_NAMESPACE, XXH64_copyState)
  119. # define XXH32_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH32_canonicalFromHash)
  120. # define XXH64_canonicalFromHash XXH_NAME2(XXH_NAMESPACE, XXH64_canonicalFromHash)
  121. # define XXH32_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH32_hashFromCanonical)
  122. # define XXH64_hashFromCanonical XXH_NAME2(XXH_NAMESPACE, XXH64_hashFromCanonical)
  123. #endif
  124. /* *************************************
  125. * Version
  126. ***************************************/
  127. #define XXH_VERSION_MAJOR 0
  128. #define XXH_VERSION_MINOR 6
  129. #define XXH_VERSION_RELEASE 2
  130. #define XXH_VERSION_NUMBER (XXH_VERSION_MAJOR *100*100 + XXH_VERSION_MINOR *100 + XXH_VERSION_RELEASE)
  131. XXH_PUBLIC_API unsigned XXH_versionNumber (void);
  132. /* ****************************
  133. * Simple Hash Functions
  134. ******************************/
  135. typedef unsigned int XXH32_hash_t;
  136. typedef unsigned long long XXH64_hash_t;
  137. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t length, unsigned int seed);
  138. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t length, unsigned long long seed);
  139. /*!
  140. XXH32() :
  141. Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input".
  142. The memory between input & input+length must be valid (allocated and read-accessible).
  143. "seed" can be used to alter the result predictably.
  144. Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
  145. XXH64() :
  146. Calculate the 64-bits hash of sequence of length "len" stored at memory address "input".
  147. "seed" can be used to alter the result predictably.
  148. This function runs 2x faster on 64-bits systems, but slower on 32-bits systems (see benchmark).
  149. */
  150. /* ****************************
  151. * Streaming Hash Functions
  152. ******************************/
  153. typedef struct XXH32_state_s XXH32_state_t; /* incomplete type */
  154. typedef struct XXH64_state_s XXH64_state_t; /* incomplete type */
  155. /*! State allocation, compatible with dynamic libraries */
  156. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void);
  157. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr);
  158. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void);
  159. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr);
  160. /* hash streaming */
  161. XXH_PUBLIC_API XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned int seed);
  162. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length);
  163. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* statePtr);
  164. XXH_PUBLIC_API XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed);
  165. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length);
  166. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* statePtr);
  167. /*
  168. These functions generate the xxHash of an input provided in multiple segments.
  169. Note that, for small input, they are slower than single-call functions, due to state management.
  170. For small input, prefer `XXH32()` and `XXH64()` .
  171. XXH state must first be allocated, using XXH*_createState() .
  172. Start a new hash by initializing state with a seed, using XXH*_reset().
  173. Then, feed the hash state by calling XXH*_update() as many times as necessary.
  174. Obviously, input must be allocated and read accessible.
  175. The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
  176. Finally, a hash value can be produced anytime, by using XXH*_digest().
  177. This function returns the nn-bits hash as an int or long long.
  178. It's still possible to continue inserting input into the hash state after a digest,
  179. and generate some new hashes later on, by calling again XXH*_digest().
  180. When done, free XXH state space if it was allocated dynamically.
  181. */
  182. /* **************************
  183. * Utils
  184. ****************************/
  185. #if !(defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) /* ! C99 */
  186. # define restrict /* disable restrict */
  187. #endif
  188. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dst_state, const XXH32_state_t* restrict src_state);
  189. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dst_state, const XXH64_state_t* restrict src_state);
  190. /* **************************
  191. * Canonical representation
  192. ****************************/
  193. /* Default result type for XXH functions are primitive unsigned 32 and 64 bits.
  194. * The canonical representation uses human-readable write convention, aka big-endian (large digits first).
  195. * These functions allow transformation of hash result into and from its canonical format.
  196. * This way, hash values can be written into a file / memory, and remain comparable on different systems and programs.
  197. */
  198. typedef struct { unsigned char digest[4]; } XXH32_canonical_t;
  199. typedef struct { unsigned char digest[8]; } XXH64_canonical_t;
  200. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash);
  201. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash);
  202. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src);
  203. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src);
  204. #endif /* XXHASH_H_5627135585666179 */
  205. /* ================================================================================================
  206. This section contains definitions which are not guaranteed to remain stable.
  207. They may change in future versions, becoming incompatible with a different version of the library.
  208. They shall only be used with static linking.
  209. Never use these definitions in association with dynamic linking !
  210. =================================================================================================== */
  211. #if defined(XXH_STATIC_LINKING_ONLY) && !defined(XXH_STATIC_H_3543687687345)
  212. #define XXH_STATIC_H_3543687687345
  213. /* These definitions are only meant to allow allocation of XXH state
  214. statically, on stack, or in a struct for example.
  215. Do not use members directly. */
  216. struct XXH32_state_s {
  217. unsigned total_len_32;
  218. unsigned large_len;
  219. unsigned v1;
  220. unsigned v2;
  221. unsigned v3;
  222. unsigned v4;
  223. unsigned mem32[4]; /* buffer defined as U32 for alignment */
  224. unsigned memsize;
  225. unsigned reserved; /* never read nor write, will be removed in a future version */
  226. }; /* typedef'd to XXH32_state_t */
  227. struct XXH64_state_s {
  228. unsigned long long total_len;
  229. unsigned long long v1;
  230. unsigned long long v2;
  231. unsigned long long v3;
  232. unsigned long long v4;
  233. unsigned long long mem64[4]; /* buffer defined as U64 for alignment */
  234. unsigned memsize;
  235. unsigned reserved[2]; /* never read nor write, will be removed in a future version */
  236. }; /* typedef'd to XXH64_state_t */
  237. # ifdef XXH_PRIVATE_API
  238. # include "xxhash.c" /* include xxhash functions as `static`, for inlining */
  239. # endif
  240. #endif /* XXH_STATIC_LINKING_ONLY && XXH_STATIC_H_3543687687345 */
  241. #if defined (__cplusplus)
  242. }
  243. #endif