xxhash.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * xxHash - Fast Hash algorithm
  3. * Copyright (C) 2012-2016, Yann Collet
  4. *
  5. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * You can contact the author at :
  31. * - xxHash homepage: http://www.xxhash.com
  32. * - xxHash source repository : https://github.com/Cyan4973/xxHash
  33. */
  34. /* *************************************
  35. * Tuning parameters
  36. ***************************************/
  37. /*!XXH_FORCE_MEMORY_ACCESS :
  38. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  39. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  40. * The below switch allow to select different access method for improved performance.
  41. * Method 0 (default) : use `memcpy()`. Safe and portable.
  42. * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
  43. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  44. * Method 2 : direct access. This method doesn't depend on compiler but violate C standard.
  45. * It can generate buggy code on targets which do not support unaligned memory accesses.
  46. * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
  47. * See http://stackoverflow.com/a/32095106/646947 for details.
  48. * Prefer these methods in priority order (0 > 1 > 2)
  49. */
  50. #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  51. # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
  52. # define XXH_FORCE_MEMORY_ACCESS 2
  53. # elif (defined(__INTEL_COMPILER) && !defined(WIN32)) || \
  54. (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) ))
  55. # define XXH_FORCE_MEMORY_ACCESS 1
  56. # endif
  57. #endif
  58. /*!XXH_ACCEPT_NULL_INPUT_POINTER :
  59. * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
  60. * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
  61. * By default, this option is disabled. To enable it, uncomment below define :
  62. */
  63. /* #define XXH_ACCEPT_NULL_INPUT_POINTER 1 */
  64. /*!XXH_FORCE_NATIVE_FORMAT :
  65. * By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
  66. * Results are therefore identical for little-endian and big-endian CPU.
  67. * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  68. * Should endian-independance be of no importance for your application, you may set the #define below to 1,
  69. * to improve speed for Big-endian CPU.
  70. * This option has no impact on Little_Endian CPU.
  71. */
  72. #ifndef XXH_FORCE_NATIVE_FORMAT /* can be defined externally */
  73. # define XXH_FORCE_NATIVE_FORMAT 0
  74. #endif
  75. /*!XXH_FORCE_ALIGN_CHECK :
  76. * This is a minor performance trick, only useful with lots of very small keys.
  77. * It means : check for aligned/unaligned input.
  78. * The check costs one initial branch per hash; set to 0 when the input data
  79. * is guaranteed to be aligned.
  80. */
  81. #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
  82. # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  83. # define XXH_FORCE_ALIGN_CHECK 0
  84. # else
  85. # define XXH_FORCE_ALIGN_CHECK 1
  86. # endif
  87. #endif
  88. /* *************************************
  89. * Includes & Memory related functions
  90. ***************************************/
  91. /* Modify the local functions below should you wish to use some other memory routines */
  92. /* for malloc(), free() */
  93. #include <stdlib.h>
  94. static void* XXH_malloc(size_t s) { return malloc(s); }
  95. static void XXH_free (void* p) { free(p); }
  96. /* for memcpy() */
  97. #include <string.h>
  98. static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
  99. #ifndef XXH_STATIC_LINKING_ONLY
  100. # define XXH_STATIC_LINKING_ONLY
  101. #endif
  102. #include "xxhash.h"
  103. /* *************************************
  104. * Compiler Specific Options
  105. ***************************************/
  106. #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  107. # define INLINE_KEYWORD inline
  108. #else
  109. # define INLINE_KEYWORD
  110. #endif
  111. #if defined(__GNUC__)
  112. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  113. #elif defined(_MSC_VER)
  114. # define FORCE_INLINE_ATTR __forceinline
  115. #else
  116. # define FORCE_INLINE_ATTR
  117. #endif
  118. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  119. #ifdef _MSC_VER
  120. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  121. #endif
  122. /* *************************************
  123. * Basic Types
  124. ***************************************/
  125. #ifndef MEM_MODULE
  126. # define MEM_MODULE
  127. # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  128. # include <stdint.h>
  129. typedef uint8_t BYTE;
  130. typedef uint16_t U16;
  131. typedef uint32_t U32;
  132. typedef int32_t S32;
  133. typedef uint64_t U64;
  134. # else
  135. typedef unsigned char BYTE;
  136. typedef unsigned short U16;
  137. typedef unsigned int U32;
  138. typedef signed int S32;
  139. typedef unsigned long long U64; /* if your compiler doesn't support unsigned long long, replace by another 64-bit type here. Note that xxhash.h will also need to be updated. */
  140. # endif
  141. #endif
  142. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  143. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  144. static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }
  145. static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }
  146. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  147. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  148. /* currently only defined for gcc and icc */
  149. typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign;
  150. static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  151. static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
  152. #else
  153. /* portable and safe solution. Generally efficient.
  154. * see : http://stackoverflow.com/a/32095106/646947
  155. */
  156. static U32 XXH_read32(const void* memPtr)
  157. {
  158. U32 val;
  159. memcpy(&val, memPtr, sizeof(val));
  160. return val;
  161. }
  162. static U64 XXH_read64(const void* memPtr)
  163. {
  164. U64 val;
  165. memcpy(&val, memPtr, sizeof(val));
  166. return val;
  167. }
  168. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  169. /* ****************************************
  170. * Compiler-specific Functions and Macros
  171. ******************************************/
  172. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  173. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  174. #if defined(_MSC_VER)
  175. # define XXH_rotl32(x,r) _rotl(x,r)
  176. # define XXH_rotl64(x,r) _rotl64(x,r)
  177. #else
  178. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  179. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  180. #endif
  181. #if defined(_MSC_VER) /* Visual Studio */
  182. # define XXH_swap32 _byteswap_ulong
  183. # define XXH_swap64 _byteswap_uint64
  184. #elif GCC_VERSION >= 403
  185. # define XXH_swap32 __builtin_bswap32
  186. # define XXH_swap64 __builtin_bswap64
  187. #else
  188. static U32 XXH_swap32 (U32 x)
  189. {
  190. return ((x << 24) & 0xff000000 ) |
  191. ((x << 8) & 0x00ff0000 ) |
  192. ((x >> 8) & 0x0000ff00 ) |
  193. ((x >> 24) & 0x000000ff );
  194. }
  195. static U64 XXH_swap64 (U64 x)
  196. {
  197. return ((x << 56) & 0xff00000000000000ULL) |
  198. ((x << 40) & 0x00ff000000000000ULL) |
  199. ((x << 24) & 0x0000ff0000000000ULL) |
  200. ((x << 8) & 0x000000ff00000000ULL) |
  201. ((x >> 8) & 0x00000000ff000000ULL) |
  202. ((x >> 24) & 0x0000000000ff0000ULL) |
  203. ((x >> 40) & 0x000000000000ff00ULL) |
  204. ((x >> 56) & 0x00000000000000ffULL);
  205. }
  206. #endif
  207. /* *************************************
  208. * Architecture Macros
  209. ***************************************/
  210. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  211. /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  212. #ifndef XXH_CPU_LITTLE_ENDIAN
  213. static const int g_one = 1;
  214. # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one))
  215. #endif
  216. /* ***************************
  217. * Memory reads
  218. *****************************/
  219. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  220. FORCE_INLINE_TEMPLATE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  221. {
  222. if (align==XXH_unaligned)
  223. return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  224. else
  225. return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
  226. }
  227. FORCE_INLINE_TEMPLATE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)
  228. {
  229. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  230. }
  231. static U32 XXH_readBE32(const void* ptr)
  232. {
  233. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  234. }
  235. FORCE_INLINE_TEMPLATE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  236. {
  237. if (align==XXH_unaligned)
  238. return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  239. else
  240. return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
  241. }
  242. FORCE_INLINE_TEMPLATE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)
  243. {
  244. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  245. }
  246. static U64 XXH_readBE64(const void* ptr)
  247. {
  248. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  249. }
  250. /* *************************************
  251. * Macros
  252. ***************************************/
  253. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  254. /* *************************************
  255. * Constants
  256. ***************************************/
  257. static const U32 PRIME32_1 = 2654435761U;
  258. static const U32 PRIME32_2 = 2246822519U;
  259. static const U32 PRIME32_3 = 3266489917U;
  260. static const U32 PRIME32_4 = 668265263U;
  261. static const U32 PRIME32_5 = 374761393U;
  262. static const U64 PRIME64_1 = 11400714785074694791ULL;
  263. static const U64 PRIME64_2 = 14029467366897019727ULL;
  264. static const U64 PRIME64_3 = 1609587929392839161ULL;
  265. static const U64 PRIME64_4 = 9650029242287828579ULL;
  266. static const U64 PRIME64_5 = 2870177450012600261ULL;
  267. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  268. /* **************************
  269. * Utils
  270. ****************************/
  271. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState)
  272. {
  273. memcpy(dstState, srcState, sizeof(*dstState));
  274. }
  275. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)
  276. {
  277. memcpy(dstState, srcState, sizeof(*dstState));
  278. }
  279. /* ***************************
  280. * Simple Hash Functions
  281. *****************************/
  282. static U32 XXH32_round(U32 seed, U32 input)
  283. {
  284. seed += input * PRIME32_2;
  285. seed = XXH_rotl32(seed, 13);
  286. seed *= PRIME32_1;
  287. return seed;
  288. }
  289. FORCE_INLINE_TEMPLATE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
  290. {
  291. const BYTE* p = (const BYTE*)input;
  292. const BYTE* bEnd = p + len;
  293. U32 h32;
  294. #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
  295. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  296. if (p==NULL) {
  297. len=0;
  298. bEnd=p=(const BYTE*)(size_t)16;
  299. }
  300. #endif
  301. if (len>=16) {
  302. const BYTE* const limit = bEnd - 16;
  303. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  304. U32 v2 = seed + PRIME32_2;
  305. U32 v3 = seed + 0;
  306. U32 v4 = seed - PRIME32_1;
  307. do {
  308. v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;
  309. v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;
  310. v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;
  311. v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;
  312. } while (p<=limit);
  313. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  314. } else {
  315. h32 = seed + PRIME32_5;
  316. }
  317. h32 += (U32) len;
  318. while (p+4<=bEnd) {
  319. h32 += XXH_get32bits(p) * PRIME32_3;
  320. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  321. p+=4;
  322. }
  323. while (p<bEnd) {
  324. h32 += (*p) * PRIME32_5;
  325. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  326. p++;
  327. }
  328. h32 ^= h32 >> 15;
  329. h32 *= PRIME32_2;
  330. h32 ^= h32 >> 13;
  331. h32 *= PRIME32_3;
  332. h32 ^= h32 >> 16;
  333. return h32;
  334. }
  335. XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed)
  336. {
  337. #if 0
  338. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  339. XXH32_CREATESTATE_STATIC(state);
  340. XXH32_reset(state, seed);
  341. XXH32_update(state, input, len);
  342. return XXH32_digest(state);
  343. #else
  344. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  345. if (XXH_FORCE_ALIGN_CHECK) {
  346. if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
  347. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  348. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  349. else
  350. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  351. } }
  352. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  353. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  354. else
  355. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  356. #endif
  357. }
  358. static U64 XXH64_round(U64 acc, U64 input)
  359. {
  360. acc += input * PRIME64_2;
  361. acc = XXH_rotl64(acc, 31);
  362. acc *= PRIME64_1;
  363. return acc;
  364. }
  365. static U64 XXH64_mergeRound(U64 acc, U64 val)
  366. {
  367. val = XXH64_round(0, val);
  368. acc ^= val;
  369. acc = acc * PRIME64_1 + PRIME64_4;
  370. return acc;
  371. }
  372. FORCE_INLINE_TEMPLATE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
  373. {
  374. const BYTE* p = (const BYTE*)input;
  375. const BYTE* const bEnd = p + len;
  376. U64 h64;
  377. #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
  378. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  379. if (p==NULL) {
  380. len=0;
  381. bEnd=p=(const BYTE*)(size_t)32;
  382. }
  383. #endif
  384. if (len>=32) {
  385. const BYTE* const limit = bEnd - 32;
  386. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  387. U64 v2 = seed + PRIME64_2;
  388. U64 v3 = seed + 0;
  389. U64 v4 = seed - PRIME64_1;
  390. do {
  391. v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
  392. v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
  393. v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
  394. v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
  395. } while (p<=limit);
  396. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  397. h64 = XXH64_mergeRound(h64, v1);
  398. h64 = XXH64_mergeRound(h64, v2);
  399. h64 = XXH64_mergeRound(h64, v3);
  400. h64 = XXH64_mergeRound(h64, v4);
  401. } else {
  402. h64 = seed + PRIME64_5;
  403. }
  404. h64 += (U64) len;
  405. while (p+8<=bEnd) {
  406. U64 const k1 = XXH64_round(0, XXH_get64bits(p));
  407. h64 ^= k1;
  408. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  409. p+=8;
  410. }
  411. if (p+4<=bEnd) {
  412. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
  413. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  414. p+=4;
  415. }
  416. while (p<bEnd) {
  417. h64 ^= (*p) * PRIME64_5;
  418. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  419. p++;
  420. }
  421. h64 ^= h64 >> 33;
  422. h64 *= PRIME64_2;
  423. h64 ^= h64 >> 29;
  424. h64 *= PRIME64_3;
  425. h64 ^= h64 >> 32;
  426. return h64;
  427. }
  428. XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  429. {
  430. #if 0
  431. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  432. XXH64_CREATESTATE_STATIC(state);
  433. XXH64_reset(state, seed);
  434. XXH64_update(state, input, len);
  435. return XXH64_digest(state);
  436. #else
  437. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  438. if (XXH_FORCE_ALIGN_CHECK) {
  439. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  440. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  441. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  442. else
  443. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  444. } }
  445. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  446. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  447. else
  448. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  449. #endif
  450. }
  451. /* **************************************************
  452. * Advanced Hash Functions
  453. ****************************************************/
  454. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  455. {
  456. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  457. }
  458. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  459. {
  460. XXH_free(statePtr);
  461. return XXH_OK;
  462. }
  463. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  464. {
  465. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  466. }
  467. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  468. {
  469. XXH_free(statePtr);
  470. return XXH_OK;
  471. }
  472. /*** Hash feed ***/
  473. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)
  474. {
  475. XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  476. memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
  477. state.v1 = seed + PRIME32_1 + PRIME32_2;
  478. state.v2 = seed + PRIME32_2;
  479. state.v3 = seed + 0;
  480. state.v4 = seed - PRIME32_1;
  481. memcpy(statePtr, &state, sizeof(state));
  482. return XXH_OK;
  483. }
  484. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
  485. {
  486. XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  487. memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
  488. state.v1 = seed + PRIME64_1 + PRIME64_2;
  489. state.v2 = seed + PRIME64_2;
  490. state.v3 = seed + 0;
  491. state.v4 = seed - PRIME64_1;
  492. memcpy(statePtr, &state, sizeof(state));
  493. return XXH_OK;
  494. }
  495. FORCE_INLINE_TEMPLATE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian)
  496. {
  497. const BYTE* p = (const BYTE*)input;
  498. const BYTE* const bEnd = p + len;
  499. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  500. if (input==NULL) return XXH_ERROR;
  501. #endif
  502. state->total_len_32 += (unsigned)len;
  503. state->large_len |= (len>=16) | (state->total_len_32>=16);
  504. if (state->memsize + len < 16) { /* fill in tmp buffer */
  505. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);
  506. state->memsize += (unsigned)len;
  507. return XXH_OK;
  508. }
  509. if (state->memsize) { /* some data left from previous update */
  510. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);
  511. { const U32* p32 = state->mem32;
  512. state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;
  513. state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;
  514. state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;
  515. state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++;
  516. }
  517. p += 16-state->memsize;
  518. state->memsize = 0;
  519. }
  520. if (p <= bEnd-16) {
  521. const BYTE* const limit = bEnd - 16;
  522. U32 v1 = state->v1;
  523. U32 v2 = state->v2;
  524. U32 v3 = state->v3;
  525. U32 v4 = state->v4;
  526. do {
  527. v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;
  528. v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;
  529. v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;
  530. v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;
  531. } while (p<=limit);
  532. state->v1 = v1;
  533. state->v2 = v2;
  534. state->v3 = v3;
  535. state->v4 = v4;
  536. }
  537. if (p < bEnd) {
  538. XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
  539. state->memsize = (unsigned)(bEnd-p);
  540. }
  541. return XXH_OK;
  542. }
  543. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
  544. {
  545. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  546. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  547. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  548. else
  549. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  550. }
  551. FORCE_INLINE_TEMPLATE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian)
  552. {
  553. const BYTE * p = (const BYTE*)state->mem32;
  554. const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize;
  555. U32 h32;
  556. if (state->large_len) {
  557. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  558. } else {
  559. h32 = state->v3 /* == seed */ + PRIME32_5;
  560. }
  561. h32 += state->total_len_32;
  562. while (p+4<=bEnd) {
  563. h32 += XXH_readLE32(p, endian) * PRIME32_3;
  564. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  565. p+=4;
  566. }
  567. while (p<bEnd) {
  568. h32 += (*p) * PRIME32_5;
  569. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  570. p++;
  571. }
  572. h32 ^= h32 >> 15;
  573. h32 *= PRIME32_2;
  574. h32 ^= h32 >> 13;
  575. h32 *= PRIME32_3;
  576. h32 ^= h32 >> 16;
  577. return h32;
  578. }
  579. XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)
  580. {
  581. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  582. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  583. return XXH32_digest_endian(state_in, XXH_littleEndian);
  584. else
  585. return XXH32_digest_endian(state_in, XXH_bigEndian);
  586. }
  587. /* **** XXH64 **** */
  588. FORCE_INLINE_TEMPLATE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)
  589. {
  590. const BYTE* p = (const BYTE*)input;
  591. const BYTE* const bEnd = p + len;
  592. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  593. if (input==NULL) return XXH_ERROR;
  594. #endif
  595. state->total_len += len;
  596. if (state->memsize + len < 32) { /* fill in tmp buffer */
  597. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
  598. state->memsize += (U32)len;
  599. return XXH_OK;
  600. }
  601. if (state->memsize) { /* tmp buffer is full */
  602. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
  603. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
  604. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
  605. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
  606. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
  607. p += 32-state->memsize;
  608. state->memsize = 0;
  609. }
  610. if (p+32 <= bEnd) {
  611. const BYTE* const limit = bEnd - 32;
  612. U64 v1 = state->v1;
  613. U64 v2 = state->v2;
  614. U64 v3 = state->v3;
  615. U64 v4 = state->v4;
  616. do {
  617. v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
  618. v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
  619. v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
  620. v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
  621. } while (p<=limit);
  622. state->v1 = v1;
  623. state->v2 = v2;
  624. state->v3 = v3;
  625. state->v4 = v4;
  626. }
  627. if (p < bEnd) {
  628. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  629. state->memsize = (unsigned)(bEnd-p);
  630. }
  631. return XXH_OK;
  632. }
  633. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  634. {
  635. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  636. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  637. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  638. else
  639. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  640. }
  641. FORCE_INLINE_TEMPLATE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)
  642. {
  643. const BYTE * p = (const BYTE*)state->mem64;
  644. const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;
  645. U64 h64;
  646. if (state->total_len >= 32) {
  647. U64 const v1 = state->v1;
  648. U64 const v2 = state->v2;
  649. U64 const v3 = state->v3;
  650. U64 const v4 = state->v4;
  651. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  652. h64 = XXH64_mergeRound(h64, v1);
  653. h64 = XXH64_mergeRound(h64, v2);
  654. h64 = XXH64_mergeRound(h64, v3);
  655. h64 = XXH64_mergeRound(h64, v4);
  656. } else {
  657. h64 = state->v3 + PRIME64_5;
  658. }
  659. h64 += (U64) state->total_len;
  660. while (p+8<=bEnd) {
  661. U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));
  662. h64 ^= k1;
  663. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  664. p+=8;
  665. }
  666. if (p+4<=bEnd) {
  667. h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;
  668. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  669. p+=4;
  670. }
  671. while (p<bEnd) {
  672. h64 ^= (*p) * PRIME64_5;
  673. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  674. p++;
  675. }
  676. h64 ^= h64 >> 33;
  677. h64 *= PRIME64_2;
  678. h64 ^= h64 >> 29;
  679. h64 *= PRIME64_3;
  680. h64 ^= h64 >> 32;
  681. return h64;
  682. }
  683. XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  684. {
  685. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  686. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  687. return XXH64_digest_endian(state_in, XXH_littleEndian);
  688. else
  689. return XXH64_digest_endian(state_in, XXH_bigEndian);
  690. }
  691. /* **************************
  692. * Canonical representation
  693. ****************************/
  694. /*! Default XXH result types are basic unsigned 32 and 64 bits.
  695. * The canonical representation follows human-readable write convention, aka big-endian (large digits first).
  696. * These functions allow transformation of hash result into and from its canonical format.
  697. * This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs.
  698. */
  699. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  700. {
  701. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  702. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  703. memcpy(dst, &hash, sizeof(*dst));
  704. }
  705. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  706. {
  707. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  708. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  709. memcpy(dst, &hash, sizeof(*dst));
  710. }
  711. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  712. {
  713. return XXH_readBE32(src);
  714. }
  715. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  716. {
  717. return XXH_readBE64(src);
  718. }