xxhash.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. #include <stddef.h> /* size_t */
  95. static void* XXH_malloc(size_t s) { return malloc(s); }
  96. static void XXH_free (void* p) { free(p); }
  97. /* for memcpy() */
  98. #include <string.h>
  99. static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
  100. #ifndef XXH_STATIC_LINKING_ONLY
  101. # define XXH_STATIC_LINKING_ONLY
  102. #endif
  103. #include "xxhash.h"
  104. /* *************************************
  105. * Compiler Specific Options
  106. ***************************************/
  107. #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  108. # define INLINE_KEYWORD inline
  109. #else
  110. # define INLINE_KEYWORD
  111. #endif
  112. #if defined(__GNUC__)
  113. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  114. #elif defined(_MSC_VER)
  115. # define FORCE_INLINE_ATTR __forceinline
  116. #else
  117. # define FORCE_INLINE_ATTR
  118. #endif
  119. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  120. #ifdef _MSC_VER
  121. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  122. #endif
  123. /* *************************************
  124. * Basic Types
  125. ***************************************/
  126. #ifndef MEM_MODULE
  127. # define MEM_MODULE
  128. # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  129. # include <stdint.h>
  130. typedef uint8_t BYTE;
  131. typedef uint16_t U16;
  132. typedef uint32_t U32;
  133. typedef int32_t S32;
  134. typedef uint64_t U64;
  135. # else
  136. typedef unsigned char BYTE;
  137. typedef unsigned short U16;
  138. typedef unsigned int U32;
  139. typedef signed int S32;
  140. 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. */
  141. # endif
  142. #endif
  143. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  144. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  145. static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }
  146. static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }
  147. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  148. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  149. /* currently only defined for gcc and icc */
  150. typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign;
  151. static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  152. static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
  153. #else
  154. /* portable and safe solution. Generally efficient.
  155. * see : http://stackoverflow.com/a/32095106/646947
  156. */
  157. static U32 XXH_read32(const void* memPtr)
  158. {
  159. U32 val;
  160. memcpy(&val, memPtr, sizeof(val));
  161. return val;
  162. }
  163. static U64 XXH_read64(const void* memPtr)
  164. {
  165. U64 val;
  166. memcpy(&val, memPtr, sizeof(val));
  167. return val;
  168. }
  169. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  170. /* ****************************************
  171. * Compiler-specific Functions and Macros
  172. ******************************************/
  173. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  174. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  175. #if defined(_MSC_VER)
  176. # define XXH_rotl32(x,r) _rotl(x,r)
  177. # define XXH_rotl64(x,r) _rotl64(x,r)
  178. #else
  179. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  180. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  181. #endif
  182. #if defined(_MSC_VER) /* Visual Studio */
  183. # define XXH_swap32 _byteswap_ulong
  184. # define XXH_swap64 _byteswap_uint64
  185. #elif GCC_VERSION >= 403
  186. # define XXH_swap32 __builtin_bswap32
  187. # define XXH_swap64 __builtin_bswap64
  188. #else
  189. static U32 XXH_swap32 (U32 x)
  190. {
  191. return ((x << 24) & 0xff000000 ) |
  192. ((x << 8) & 0x00ff0000 ) |
  193. ((x >> 8) & 0x0000ff00 ) |
  194. ((x >> 24) & 0x000000ff );
  195. }
  196. static U64 XXH_swap64 (U64 x)
  197. {
  198. return ((x << 56) & 0xff00000000000000ULL) |
  199. ((x << 40) & 0x00ff000000000000ULL) |
  200. ((x << 24) & 0x0000ff0000000000ULL) |
  201. ((x << 8) & 0x000000ff00000000ULL) |
  202. ((x >> 8) & 0x00000000ff000000ULL) |
  203. ((x >> 24) & 0x0000000000ff0000ULL) |
  204. ((x >> 40) & 0x000000000000ff00ULL) |
  205. ((x >> 56) & 0x00000000000000ffULL);
  206. }
  207. #endif
  208. /* *************************************
  209. * Architecture Macros
  210. ***************************************/
  211. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  212. /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  213. #ifndef XXH_CPU_LITTLE_ENDIAN
  214. static const int g_one = 1;
  215. # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one))
  216. #endif
  217. /* ***************************
  218. * Memory reads
  219. *****************************/
  220. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  221. FORCE_INLINE_TEMPLATE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  222. {
  223. if (align==XXH_unaligned)
  224. return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  225. else
  226. return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
  227. }
  228. FORCE_INLINE_TEMPLATE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)
  229. {
  230. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  231. }
  232. static U32 XXH_readBE32(const void* ptr)
  233. {
  234. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  235. }
  236. FORCE_INLINE_TEMPLATE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  237. {
  238. if (align==XXH_unaligned)
  239. return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  240. else
  241. return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
  242. }
  243. FORCE_INLINE_TEMPLATE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)
  244. {
  245. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  246. }
  247. static U64 XXH_readBE64(const void* ptr)
  248. {
  249. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  250. }
  251. /* *************************************
  252. * Macros
  253. ***************************************/
  254. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  255. /* *************************************
  256. * Constants
  257. ***************************************/
  258. static const U32 PRIME32_1 = 2654435761U;
  259. static const U32 PRIME32_2 = 2246822519U;
  260. static const U32 PRIME32_3 = 3266489917U;
  261. static const U32 PRIME32_4 = 668265263U;
  262. static const U32 PRIME32_5 = 374761393U;
  263. static const U64 PRIME64_1 = 11400714785074694791ULL;
  264. static const U64 PRIME64_2 = 14029467366897019727ULL;
  265. static const U64 PRIME64_3 = 1609587929392839161ULL;
  266. static const U64 PRIME64_4 = 9650029242287828579ULL;
  267. static const U64 PRIME64_5 = 2870177450012600261ULL;
  268. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  269. /* **************************
  270. * Utils
  271. ****************************/
  272. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* restrict dstState, const XXH32_state_t* restrict srcState)
  273. {
  274. memcpy(dstState, srcState, sizeof(*dstState));
  275. }
  276. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)
  277. {
  278. memcpy(dstState, srcState, sizeof(*dstState));
  279. }
  280. /* ***************************
  281. * Simple Hash Functions
  282. *****************************/
  283. static U32 XXH32_round(U32 seed, U32 input)
  284. {
  285. seed += input * PRIME32_2;
  286. seed = XXH_rotl32(seed, 13);
  287. seed *= PRIME32_1;
  288. return seed;
  289. }
  290. FORCE_INLINE_TEMPLATE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
  291. {
  292. const BYTE* p = (const BYTE*)input;
  293. const BYTE* bEnd = p + len;
  294. U32 h32;
  295. #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
  296. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  297. if (p==NULL) {
  298. len=0;
  299. bEnd=p=(const BYTE*)(size_t)16;
  300. }
  301. #endif
  302. if (len>=16) {
  303. const BYTE* const limit = bEnd - 16;
  304. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  305. U32 v2 = seed + PRIME32_2;
  306. U32 v3 = seed + 0;
  307. U32 v4 = seed - PRIME32_1;
  308. do {
  309. v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;
  310. v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;
  311. v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;
  312. v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;
  313. } while (p<=limit);
  314. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  315. } else {
  316. h32 = seed + PRIME32_5;
  317. }
  318. h32 += (U32) len;
  319. while (p+4<=bEnd) {
  320. h32 += XXH_get32bits(p) * PRIME32_3;
  321. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  322. p+=4;
  323. }
  324. while (p<bEnd) {
  325. h32 += (*p) * PRIME32_5;
  326. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  327. p++;
  328. }
  329. h32 ^= h32 >> 15;
  330. h32 *= PRIME32_2;
  331. h32 ^= h32 >> 13;
  332. h32 *= PRIME32_3;
  333. h32 ^= h32 >> 16;
  334. return h32;
  335. }
  336. XXH_PUBLIC_API unsigned int XXH32 (const void* input, size_t len, unsigned int seed)
  337. {
  338. #if 0
  339. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  340. XXH32_CREATESTATE_STATIC(state);
  341. XXH32_reset(state, seed);
  342. XXH32_update(state, input, len);
  343. return XXH32_digest(state);
  344. #else
  345. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  346. if (XXH_FORCE_ALIGN_CHECK) {
  347. if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
  348. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  349. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  350. else
  351. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  352. } }
  353. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  354. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  355. else
  356. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  357. #endif
  358. }
  359. static U64 XXH64_round(U64 acc, U64 input)
  360. {
  361. acc += input * PRIME64_2;
  362. acc = XXH_rotl64(acc, 31);
  363. acc *= PRIME64_1;
  364. return acc;
  365. }
  366. static U64 XXH64_mergeRound(U64 acc, U64 val)
  367. {
  368. val = XXH64_round(0, val);
  369. acc ^= val;
  370. acc = acc * PRIME64_1 + PRIME64_4;
  371. return acc;
  372. }
  373. FORCE_INLINE_TEMPLATE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
  374. {
  375. const BYTE* p = (const BYTE*)input;
  376. const BYTE* const bEnd = p + len;
  377. U64 h64;
  378. #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
  379. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  380. if (p==NULL) {
  381. len=0;
  382. bEnd=p=(const BYTE*)(size_t)32;
  383. }
  384. #endif
  385. if (len>=32) {
  386. const BYTE* const limit = bEnd - 32;
  387. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  388. U64 v2 = seed + PRIME64_2;
  389. U64 v3 = seed + 0;
  390. U64 v4 = seed - PRIME64_1;
  391. do {
  392. v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
  393. v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
  394. v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
  395. v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
  396. } while (p<=limit);
  397. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  398. h64 = XXH64_mergeRound(h64, v1);
  399. h64 = XXH64_mergeRound(h64, v2);
  400. h64 = XXH64_mergeRound(h64, v3);
  401. h64 = XXH64_mergeRound(h64, v4);
  402. } else {
  403. h64 = seed + PRIME64_5;
  404. }
  405. h64 += (U64) len;
  406. while (p+8<=bEnd) {
  407. U64 const k1 = XXH64_round(0, XXH_get64bits(p));
  408. h64 ^= k1;
  409. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  410. p+=8;
  411. }
  412. if (p+4<=bEnd) {
  413. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
  414. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  415. p+=4;
  416. }
  417. while (p<bEnd) {
  418. h64 ^= (*p) * PRIME64_5;
  419. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  420. p++;
  421. }
  422. h64 ^= h64 >> 33;
  423. h64 *= PRIME64_2;
  424. h64 ^= h64 >> 29;
  425. h64 *= PRIME64_3;
  426. h64 ^= h64 >> 32;
  427. return h64;
  428. }
  429. XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  430. {
  431. #if 0
  432. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  433. XXH64_CREATESTATE_STATIC(state);
  434. XXH64_reset(state, seed);
  435. XXH64_update(state, input, len);
  436. return XXH64_digest(state);
  437. #else
  438. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  439. if (XXH_FORCE_ALIGN_CHECK) {
  440. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  441. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  442. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  443. else
  444. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  445. } }
  446. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  447. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  448. else
  449. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  450. #endif
  451. }
  452. /* **************************************************
  453. * Advanced Hash Functions
  454. ****************************************************/
  455. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  456. {
  457. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  458. }
  459. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  460. {
  461. XXH_free(statePtr);
  462. return XXH_OK;
  463. }
  464. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  465. {
  466. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  467. }
  468. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  469. {
  470. XXH_free(statePtr);
  471. return XXH_OK;
  472. }
  473. /*** Hash feed ***/
  474. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)
  475. {
  476. XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  477. memset(&state, 0, sizeof(state)-4); /* do not write into reserved, for future removal */
  478. state.v1 = seed + PRIME32_1 + PRIME32_2;
  479. state.v2 = seed + PRIME32_2;
  480. state.v3 = seed + 0;
  481. state.v4 = seed - PRIME32_1;
  482. memcpy(statePtr, &state, sizeof(state));
  483. return XXH_OK;
  484. }
  485. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
  486. {
  487. XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  488. memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
  489. state.v1 = seed + PRIME64_1 + PRIME64_2;
  490. state.v2 = seed + PRIME64_2;
  491. state.v3 = seed + 0;
  492. state.v4 = seed - PRIME64_1;
  493. memcpy(statePtr, &state, sizeof(state));
  494. return XXH_OK;
  495. }
  496. FORCE_INLINE_TEMPLATE XXH_errorcode XXH32_update_endian (XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian)
  497. {
  498. const BYTE* p = (const BYTE*)input;
  499. const BYTE* const bEnd = p + len;
  500. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  501. if (input==NULL) return XXH_ERROR;
  502. #endif
  503. state->total_len_32 += (unsigned)len;
  504. state->large_len |= (len>=16) | (state->total_len_32>=16);
  505. if (state->memsize + len < 16) { /* fill in tmp buffer */
  506. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);
  507. state->memsize += (unsigned)len;
  508. return XXH_OK;
  509. }
  510. if (state->memsize) { /* some data left from previous update */
  511. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);
  512. { const U32* p32 = state->mem32;
  513. state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;
  514. state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;
  515. state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;
  516. state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian)); p32++;
  517. }
  518. p += 16-state->memsize;
  519. state->memsize = 0;
  520. }
  521. if (p <= bEnd-16) {
  522. const BYTE* const limit = bEnd - 16;
  523. U32 v1 = state->v1;
  524. U32 v2 = state->v2;
  525. U32 v3 = state->v3;
  526. U32 v4 = state->v4;
  527. do {
  528. v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;
  529. v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;
  530. v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;
  531. v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;
  532. } while (p<=limit);
  533. state->v1 = v1;
  534. state->v2 = v2;
  535. state->v3 = v3;
  536. state->v4 = v4;
  537. }
  538. if (p < bEnd) {
  539. XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
  540. state->memsize = (unsigned)(bEnd-p);
  541. }
  542. return XXH_OK;
  543. }
  544. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
  545. {
  546. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  547. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  548. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  549. else
  550. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  551. }
  552. FORCE_INLINE_TEMPLATE U32 XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian)
  553. {
  554. const BYTE * p = (const BYTE*)state->mem32;
  555. const BYTE* const bEnd = (const BYTE*)(state->mem32) + state->memsize;
  556. U32 h32;
  557. if (state->large_len) {
  558. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  559. } else {
  560. h32 = state->v3 /* == seed */ + PRIME32_5;
  561. }
  562. h32 += state->total_len_32;
  563. while (p+4<=bEnd) {
  564. h32 += XXH_readLE32(p, endian) * PRIME32_3;
  565. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  566. p+=4;
  567. }
  568. while (p<bEnd) {
  569. h32 += (*p) * PRIME32_5;
  570. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  571. p++;
  572. }
  573. h32 ^= h32 >> 15;
  574. h32 *= PRIME32_2;
  575. h32 ^= h32 >> 13;
  576. h32 *= PRIME32_3;
  577. h32 ^= h32 >> 16;
  578. return h32;
  579. }
  580. XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)
  581. {
  582. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  583. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  584. return XXH32_digest_endian(state_in, XXH_littleEndian);
  585. else
  586. return XXH32_digest_endian(state_in, XXH_bigEndian);
  587. }
  588. /* **** XXH64 **** */
  589. FORCE_INLINE_TEMPLATE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)
  590. {
  591. const BYTE* p = (const BYTE*)input;
  592. const BYTE* const bEnd = p + len;
  593. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  594. if (input==NULL) return XXH_ERROR;
  595. #endif
  596. state->total_len += len;
  597. if (state->memsize + len < 32) { /* fill in tmp buffer */
  598. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
  599. state->memsize += (U32)len;
  600. return XXH_OK;
  601. }
  602. if (state->memsize) { /* tmp buffer is full */
  603. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
  604. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
  605. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
  606. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
  607. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
  608. p += 32-state->memsize;
  609. state->memsize = 0;
  610. }
  611. if (p+32 <= bEnd) {
  612. const BYTE* const limit = bEnd - 32;
  613. U64 v1 = state->v1;
  614. U64 v2 = state->v2;
  615. U64 v3 = state->v3;
  616. U64 v4 = state->v4;
  617. do {
  618. v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
  619. v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
  620. v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
  621. v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
  622. } while (p<=limit);
  623. state->v1 = v1;
  624. state->v2 = v2;
  625. state->v3 = v3;
  626. state->v4 = v4;
  627. }
  628. if (p < bEnd) {
  629. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  630. state->memsize = (unsigned)(bEnd-p);
  631. }
  632. return XXH_OK;
  633. }
  634. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  635. {
  636. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  637. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  638. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  639. else
  640. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  641. }
  642. FORCE_INLINE_TEMPLATE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)
  643. {
  644. const BYTE * p = (const BYTE*)state->mem64;
  645. const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;
  646. U64 h64;
  647. if (state->total_len >= 32) {
  648. U64 const v1 = state->v1;
  649. U64 const v2 = state->v2;
  650. U64 const v3 = state->v3;
  651. U64 const v4 = state->v4;
  652. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  653. h64 = XXH64_mergeRound(h64, v1);
  654. h64 = XXH64_mergeRound(h64, v2);
  655. h64 = XXH64_mergeRound(h64, v3);
  656. h64 = XXH64_mergeRound(h64, v4);
  657. } else {
  658. h64 = state->v3 + PRIME64_5;
  659. }
  660. h64 += (U64) state->total_len;
  661. while (p+8<=bEnd) {
  662. U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));
  663. h64 ^= k1;
  664. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  665. p+=8;
  666. }
  667. if (p+4<=bEnd) {
  668. h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;
  669. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  670. p+=4;
  671. }
  672. while (p<bEnd) {
  673. h64 ^= (*p) * PRIME64_5;
  674. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  675. p++;
  676. }
  677. h64 ^= h64 >> 33;
  678. h64 *= PRIME64_2;
  679. h64 ^= h64 >> 29;
  680. h64 *= PRIME64_3;
  681. h64 ^= h64 >> 32;
  682. return h64;
  683. }
  684. XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  685. {
  686. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  687. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  688. return XXH64_digest_endian(state_in, XXH_littleEndian);
  689. else
  690. return XXH64_digest_endian(state_in, XXH_bigEndian);
  691. }
  692. /* **************************
  693. * Canonical representation
  694. ****************************/
  695. /*! Default XXH result types are basic unsigned 32 and 64 bits.
  696. * The canonical representation follows human-readable write convention, aka big-endian (large digits first).
  697. * These functions allow transformation of hash result into and from its canonical format.
  698. * This way, hash values can be written into a file or buffer, and remain comparable across different systems and programs.
  699. */
  700. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  701. {
  702. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  703. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  704. memcpy(dst, &hash, sizeof(*dst));
  705. }
  706. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  707. {
  708. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  709. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  710. memcpy(dst, &hash, sizeof(*dst));
  711. }
  712. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  713. {
  714. return XXH_readBE32(src);
  715. }
  716. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  717. {
  718. return XXH_readBE64(src);
  719. }