xxhash.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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__) \
  52. || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) \
  53. || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
  54. # define XXH_FORCE_MEMORY_ACCESS 2
  55. # elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || \
  56. (defined(__GNUC__) && ( defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
  57. || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
  58. || defined(__ARM_ARCH_7S__) ))
  59. # define XXH_FORCE_MEMORY_ACCESS 1
  60. # endif
  61. #endif
  62. /*!XXH_ACCEPT_NULL_INPUT_POINTER :
  63. * If input pointer is NULL, xxHash default behavior is to dereference it, triggering a segfault.
  64. * When this macro is enabled, xxHash actively checks input for null pointer.
  65. * It it is, result for null input pointers is the same as a null-length input.
  66. */
  67. #ifndef XXH_ACCEPT_NULL_INPUT_POINTER /* can be defined externally */
  68. # define XXH_ACCEPT_NULL_INPUT_POINTER 0
  69. #endif
  70. /*!XXH_FORCE_NATIVE_FORMAT :
  71. * By default, xxHash library provides endian-independent Hash values, based on little-endian convention.
  72. * Results are therefore identical for little-endian and big-endian CPU.
  73. * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  74. * Should endian-independence be of no importance for your application, you may set the #define below to 1,
  75. * to improve speed for Big-endian CPU.
  76. * This option has no impact on Little_Endian CPU.
  77. */
  78. #ifndef XXH_FORCE_NATIVE_FORMAT /* can be defined externally */
  79. # define XXH_FORCE_NATIVE_FORMAT 0
  80. #endif
  81. /*!XXH_FORCE_ALIGN_CHECK :
  82. * This is a minor performance trick, only useful with lots of very small keys.
  83. * It means : check for aligned/unaligned input.
  84. * The check costs one initial branch per hash;
  85. * set it to 0 when the input is guaranteed to be aligned,
  86. * or when alignment doesn't matter for performance.
  87. */
  88. #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
  89. # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  90. # define XXH_FORCE_ALIGN_CHECK 0
  91. # else
  92. # define XXH_FORCE_ALIGN_CHECK 1
  93. # endif
  94. #endif
  95. /* *************************************
  96. * Includes & Memory related functions
  97. ***************************************/
  98. /*! Modify the local functions below should you wish to use some other memory routines
  99. * for malloc(), free() */
  100. #include <stdlib.h>
  101. static void* XXH_malloc(size_t s) { return malloc(s); }
  102. static void XXH_free (void* p) { free(p); }
  103. /*! and for memcpy() */
  104. #include <string.h>
  105. static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
  106. #include <assert.h> /* assert */
  107. #define XXH_STATIC_LINKING_ONLY
  108. #include "xxhash.h"
  109. /* *************************************
  110. * Compiler Specific Options
  111. ***************************************/
  112. #ifdef _MSC_VER /* Visual Studio */
  113. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  114. # define FORCE_INLINE static __forceinline
  115. #else
  116. # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  117. # ifdef __GNUC__
  118. # define FORCE_INLINE static inline __attribute__((always_inline))
  119. # else
  120. # define FORCE_INLINE static inline
  121. # endif
  122. # else
  123. # define FORCE_INLINE static
  124. # endif /* __STDC_VERSION__ */
  125. #endif
  126. /* *************************************
  127. * Basic Types
  128. ***************************************/
  129. #ifndef MEM_MODULE
  130. # if !defined (__VMS) \
  131. && (defined (__cplusplus) \
  132. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  133. # include <stdint.h>
  134. typedef uint8_t BYTE;
  135. typedef uint16_t U16;
  136. typedef uint32_t U32;
  137. # else
  138. typedef unsigned char BYTE;
  139. typedef unsigned short U16;
  140. typedef unsigned int U32;
  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. #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; } __attribute__((packed)) unalign;
  150. static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  151. #else
  152. /* portable and safe solution. Generally efficient.
  153. * see : http://stackoverflow.com/a/32095106/646947
  154. */
  155. static U32 XXH_read32(const void* memPtr)
  156. {
  157. U32 val;
  158. memcpy(&val, memPtr, sizeof(val));
  159. return val;
  160. }
  161. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  162. /* ****************************************
  163. * Compiler-specific Functions and Macros
  164. ******************************************/
  165. #define XXH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  166. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  167. #if defined(_MSC_VER)
  168. # define XXH_rotl32(x,r) _rotl(x,r)
  169. # define XXH_rotl64(x,r) _rotl64(x,r)
  170. #else
  171. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  172. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  173. #endif
  174. #if defined(_MSC_VER) /* Visual Studio */
  175. # define XXH_swap32 _byteswap_ulong
  176. #elif XXH_GCC_VERSION >= 403
  177. # define XXH_swap32 __builtin_bswap32
  178. #else
  179. static U32 XXH_swap32 (U32 x)
  180. {
  181. return ((x << 24) & 0xff000000 ) |
  182. ((x << 8) & 0x00ff0000 ) |
  183. ((x >> 8) & 0x0000ff00 ) |
  184. ((x >> 24) & 0x000000ff );
  185. }
  186. #endif
  187. /* *************************************
  188. * Architecture Macros
  189. ***************************************/
  190. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  191. /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  192. #ifndef XXH_CPU_LITTLE_ENDIAN
  193. static int XXH_isLittleEndian(void)
  194. {
  195. const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  196. return one.c[0];
  197. }
  198. # define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian()
  199. #endif
  200. /* ***************************
  201. * Memory reads
  202. *****************************/
  203. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  204. FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  205. {
  206. if (align==XXH_unaligned)
  207. return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  208. else
  209. return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
  210. }
  211. FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)
  212. {
  213. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  214. }
  215. static U32 XXH_readBE32(const void* ptr)
  216. {
  217. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  218. }
  219. /* *************************************
  220. * Macros
  221. ***************************************/
  222. #define XXH_STATIC_ASSERT(c) { enum { XXH_sa = 1/(int)(!!(c)) }; } /* use after variable declarations */
  223. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  224. /* *******************************************************************
  225. * 32-bit hash functions
  226. *********************************************************************/
  227. static const U32 PRIME32_1 = 2654435761U;
  228. static const U32 PRIME32_2 = 2246822519U;
  229. static const U32 PRIME32_3 = 3266489917U;
  230. static const U32 PRIME32_4 = 668265263U;
  231. static const U32 PRIME32_5 = 374761393U;
  232. static U32 XXH32_round(U32 seed, U32 input)
  233. {
  234. seed += input * PRIME32_2;
  235. seed = XXH_rotl32(seed, 13);
  236. seed *= PRIME32_1;
  237. return seed;
  238. }
  239. /* mix all bits */
  240. static U32 XXH32_avalanche(U32 h32)
  241. {
  242. h32 ^= h32 >> 15;
  243. h32 *= PRIME32_2;
  244. h32 ^= h32 >> 13;
  245. h32 *= PRIME32_3;
  246. h32 ^= h32 >> 16;
  247. return(h32);
  248. }
  249. #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
  250. static U32
  251. XXH32_finalize(U32 h32, const void* ptr, size_t len,
  252. XXH_endianess endian, XXH_alignment align)
  253. {
  254. const BYTE* p = (const BYTE*)ptr;
  255. #define PROCESS1 \
  256. h32 += (*p++) * PRIME32_5; \
  257. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  258. #define PROCESS4 \
  259. h32 += XXH_get32bits(p) * PRIME32_3; \
  260. p+=4; \
  261. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  262. switch(len&15) /* or switch(bEnd - p) */
  263. {
  264. case 12: PROCESS4;
  265. /* fallthrough */
  266. case 8: PROCESS4;
  267. /* fallthrough */
  268. case 4: PROCESS4;
  269. return XXH32_avalanche(h32);
  270. case 13: PROCESS4;
  271. /* fallthrough */
  272. case 9: PROCESS4;
  273. /* fallthrough */
  274. case 5: PROCESS4;
  275. PROCESS1;
  276. return XXH32_avalanche(h32);
  277. case 14: PROCESS4;
  278. /* fallthrough */
  279. case 10: PROCESS4;
  280. /* fallthrough */
  281. case 6: PROCESS4;
  282. PROCESS1;
  283. PROCESS1;
  284. return XXH32_avalanche(h32);
  285. case 15: PROCESS4;
  286. /* fallthrough */
  287. case 11: PROCESS4;
  288. /* fallthrough */
  289. case 7: PROCESS4;
  290. /* fallthrough */
  291. case 3: PROCESS1;
  292. /* fallthrough */
  293. case 2: PROCESS1;
  294. /* fallthrough */
  295. case 1: PROCESS1;
  296. /* fallthrough */
  297. case 0: return XXH32_avalanche(h32);
  298. }
  299. assert(0);
  300. return h32; /* reaching this point is deemed impossible */
  301. }
  302. FORCE_INLINE U32
  303. XXH32_endian_align(const void* input, size_t len, U32 seed,
  304. XXH_endianess endian, XXH_alignment align)
  305. {
  306. const BYTE* p = (const BYTE*)input;
  307. const BYTE* bEnd = p + len;
  308. U32 h32;
  309. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  310. if (p==NULL) {
  311. len=0;
  312. bEnd=p=(const BYTE*)(size_t)16;
  313. }
  314. #endif
  315. if (len>=16) {
  316. const BYTE* const limit = bEnd - 15;
  317. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  318. U32 v2 = seed + PRIME32_2;
  319. U32 v3 = seed + 0;
  320. U32 v4 = seed - PRIME32_1;
  321. do {
  322. v1 = XXH32_round(v1, XXH_get32bits(p)); p+=4;
  323. v2 = XXH32_round(v2, XXH_get32bits(p)); p+=4;
  324. v3 = XXH32_round(v3, XXH_get32bits(p)); p+=4;
  325. v4 = XXH32_round(v4, XXH_get32bits(p)); p+=4;
  326. } while (p < limit);
  327. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7)
  328. + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  329. } else {
  330. h32 = seed + PRIME32_5;
  331. }
  332. h32 += (U32)len;
  333. return XXH32_finalize(h32, p, len&15, endian, align);
  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_state_t 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. /*====== Hash streaming ======*/
  359. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  360. {
  361. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  362. }
  363. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  364. {
  365. XXH_free(statePtr);
  366. return XXH_OK;
  367. }
  368. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t* srcState)
  369. {
  370. memcpy(dstState, srcState, sizeof(*dstState));
  371. }
  372. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int seed)
  373. {
  374. XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  375. memset(&state, 0, sizeof(state));
  376. state.v1 = seed + PRIME32_1 + PRIME32_2;
  377. state.v2 = seed + PRIME32_2;
  378. state.v3 = seed + 0;
  379. state.v4 = seed - PRIME32_1;
  380. /* do not write into reserved, planned to be removed in a future version */
  381. memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved));
  382. return XXH_OK;
  383. }
  384. FORCE_INLINE XXH_errorcode
  385. XXH32_update_endian(XXH32_state_t* state, const void* input, size_t len, XXH_endianess endian)
  386. {
  387. if (input==NULL)
  388. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  389. return XXH_OK;
  390. #else
  391. return XXH_ERROR;
  392. #endif
  393. { const BYTE* p = (const BYTE*)input;
  394. const BYTE* const bEnd = p + len;
  395. state->total_len_32 += (unsigned)len;
  396. state->large_len |= (len>=16) | (state->total_len_32>=16);
  397. if (state->memsize + len < 16) { /* fill in tmp buffer */
  398. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len);
  399. state->memsize += (unsigned)len;
  400. return XXH_OK;
  401. }
  402. if (state->memsize) { /* some data left from previous update */
  403. XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize);
  404. { const U32* p32 = state->mem32;
  405. state->v1 = XXH32_round(state->v1, XXH_readLE32(p32, endian)); p32++;
  406. state->v2 = XXH32_round(state->v2, XXH_readLE32(p32, endian)); p32++;
  407. state->v3 = XXH32_round(state->v3, XXH_readLE32(p32, endian)); p32++;
  408. state->v4 = XXH32_round(state->v4, XXH_readLE32(p32, endian));
  409. }
  410. p += 16-state->memsize;
  411. state->memsize = 0;
  412. }
  413. if (p <= bEnd-16) {
  414. const BYTE* const limit = bEnd - 16;
  415. U32 v1 = state->v1;
  416. U32 v2 = state->v2;
  417. U32 v3 = state->v3;
  418. U32 v4 = state->v4;
  419. do {
  420. v1 = XXH32_round(v1, XXH_readLE32(p, endian)); p+=4;
  421. v2 = XXH32_round(v2, XXH_readLE32(p, endian)); p+=4;
  422. v3 = XXH32_round(v3, XXH_readLE32(p, endian)); p+=4;
  423. v4 = XXH32_round(v4, XXH_readLE32(p, endian)); p+=4;
  424. } while (p<=limit);
  425. state->v1 = v1;
  426. state->v2 = v2;
  427. state->v3 = v3;
  428. state->v4 = v4;
  429. }
  430. if (p < bEnd) {
  431. XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
  432. state->memsize = (unsigned)(bEnd-p);
  433. }
  434. }
  435. return XXH_OK;
  436. }
  437. XXH_PUBLIC_API XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
  438. {
  439. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  440. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  441. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  442. else
  443. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  444. }
  445. FORCE_INLINE U32
  446. XXH32_digest_endian (const XXH32_state_t* state, XXH_endianess endian)
  447. {
  448. U32 h32;
  449. if (state->large_len) {
  450. h32 = XXH_rotl32(state->v1, 1)
  451. + XXH_rotl32(state->v2, 7)
  452. + XXH_rotl32(state->v3, 12)
  453. + XXH_rotl32(state->v4, 18);
  454. } else {
  455. h32 = state->v3 /* == seed */ + PRIME32_5;
  456. }
  457. h32 += state->total_len_32;
  458. return XXH32_finalize(h32, state->mem32, state->memsize, endian, XXH_aligned);
  459. }
  460. XXH_PUBLIC_API unsigned int XXH32_digest (const XXH32_state_t* state_in)
  461. {
  462. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  463. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  464. return XXH32_digest_endian(state_in, XXH_littleEndian);
  465. else
  466. return XXH32_digest_endian(state_in, XXH_bigEndian);
  467. }
  468. /*====== Canonical representation ======*/
  469. /*! Default XXH result types are basic unsigned 32 and 64 bits.
  470. * The canonical representation follows human-readable write convention, aka big-endian (large digits first).
  471. * These functions allow transformation of hash result into and from its canonical format.
  472. * This way, hash values can be written into a file or buffer, remaining comparable across different systems.
  473. */
  474. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  475. {
  476. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  477. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  478. memcpy(dst, &hash, sizeof(*dst));
  479. }
  480. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  481. {
  482. return XXH_readBE32(src);
  483. }
  484. #ifndef XXH_NO_LONG_LONG
  485. /* *******************************************************************
  486. * 64-bit hash functions
  487. *********************************************************************/
  488. /*====== Memory access ======*/
  489. #ifndef MEM_MODULE
  490. # define MEM_MODULE
  491. # if !defined (__VMS) \
  492. && (defined (__cplusplus) \
  493. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  494. # include <stdint.h>
  495. typedef uint64_t U64;
  496. # else
  497. /* if compiler doesn't support unsigned long long, replace by another 64-bit type */
  498. typedef unsigned long long U64;
  499. # endif
  500. #endif
  501. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  502. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  503. static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }
  504. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  505. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  506. /* currently only defined for gcc and icc */
  507. typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign64;
  508. static U64 XXH_read64(const void* ptr) { return ((const unalign64*)ptr)->u64; }
  509. #else
  510. /* portable and safe solution. Generally efficient.
  511. * see : http://stackoverflow.com/a/32095106/646947
  512. */
  513. static U64 XXH_read64(const void* memPtr)
  514. {
  515. U64 val;
  516. memcpy(&val, memPtr, sizeof(val));
  517. return val;
  518. }
  519. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  520. #if defined(_MSC_VER) /* Visual Studio */
  521. # define XXH_swap64 _byteswap_uint64
  522. #elif XXH_GCC_VERSION >= 403
  523. # define XXH_swap64 __builtin_bswap64
  524. #else
  525. static U64 XXH_swap64 (U64 x)
  526. {
  527. return ((x << 56) & 0xff00000000000000ULL) |
  528. ((x << 40) & 0x00ff000000000000ULL) |
  529. ((x << 24) & 0x0000ff0000000000ULL) |
  530. ((x << 8) & 0x000000ff00000000ULL) |
  531. ((x >> 8) & 0x00000000ff000000ULL) |
  532. ((x >> 24) & 0x0000000000ff0000ULL) |
  533. ((x >> 40) & 0x000000000000ff00ULL) |
  534. ((x >> 56) & 0x00000000000000ffULL);
  535. }
  536. #endif
  537. FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  538. {
  539. if (align==XXH_unaligned)
  540. return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  541. else
  542. return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
  543. }
  544. FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)
  545. {
  546. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  547. }
  548. static U64 XXH_readBE64(const void* ptr)
  549. {
  550. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  551. }
  552. /*====== xxh64 ======*/
  553. static const U64 PRIME64_1 = 11400714785074694791ULL;
  554. static const U64 PRIME64_2 = 14029467366897019727ULL;
  555. static const U64 PRIME64_3 = 1609587929392839161ULL;
  556. static const U64 PRIME64_4 = 9650029242287828579ULL;
  557. static const U64 PRIME64_5 = 2870177450012600261ULL;
  558. static U64 XXH64_round(U64 acc, U64 input)
  559. {
  560. acc += input * PRIME64_2;
  561. acc = XXH_rotl64(acc, 31);
  562. acc *= PRIME64_1;
  563. return acc;
  564. }
  565. static U64 XXH64_mergeRound(U64 acc, U64 val)
  566. {
  567. val = XXH64_round(0, val);
  568. acc ^= val;
  569. acc = acc * PRIME64_1 + PRIME64_4;
  570. return acc;
  571. }
  572. static U64 XXH64_avalanche(U64 h64)
  573. {
  574. h64 ^= h64 >> 33;
  575. h64 *= PRIME64_2;
  576. h64 ^= h64 >> 29;
  577. h64 *= PRIME64_3;
  578. h64 ^= h64 >> 32;
  579. return h64;
  580. }
  581. #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
  582. static U64
  583. XXH64_finalize(U64 h64, const void* ptr, size_t len,
  584. XXH_endianess endian, XXH_alignment align)
  585. {
  586. const BYTE* p = (const BYTE*)ptr;
  587. #define PROCESS1_64 \
  588. h64 ^= (*p++) * PRIME64_5; \
  589. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  590. #define PROCESS4_64 \
  591. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; \
  592. p+=4; \
  593. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  594. #define PROCESS8_64 { \
  595. U64 const k1 = XXH64_round(0, XXH_get64bits(p)); \
  596. p+=8; \
  597. h64 ^= k1; \
  598. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; \
  599. }
  600. switch(len&31) {
  601. case 24: PROCESS8_64;
  602. /* fallthrough */
  603. case 16: PROCESS8_64;
  604. /* fallthrough */
  605. case 8: PROCESS8_64;
  606. return XXH64_avalanche(h64);
  607. case 28: PROCESS8_64;
  608. /* fallthrough */
  609. case 20: PROCESS8_64;
  610. /* fallthrough */
  611. case 12: PROCESS8_64;
  612. /* fallthrough */
  613. case 4: PROCESS4_64;
  614. return XXH64_avalanche(h64);
  615. case 25: PROCESS8_64;
  616. /* fallthrough */
  617. case 17: PROCESS8_64;
  618. /* fallthrough */
  619. case 9: PROCESS8_64;
  620. PROCESS1_64;
  621. return XXH64_avalanche(h64);
  622. case 29: PROCESS8_64;
  623. /* fallthrough */
  624. case 21: PROCESS8_64;
  625. /* fallthrough */
  626. case 13: PROCESS8_64;
  627. /* fallthrough */
  628. case 5: PROCESS4_64;
  629. PROCESS1_64;
  630. return XXH64_avalanche(h64);
  631. case 26: PROCESS8_64;
  632. /* fallthrough */
  633. case 18: PROCESS8_64;
  634. /* fallthrough */
  635. case 10: PROCESS8_64;
  636. PROCESS1_64;
  637. PROCESS1_64;
  638. return XXH64_avalanche(h64);
  639. case 30: PROCESS8_64;
  640. /* fallthrough */
  641. case 22: PROCESS8_64;
  642. /* fallthrough */
  643. case 14: PROCESS8_64;
  644. /* fallthrough */
  645. case 6: PROCESS4_64;
  646. PROCESS1_64;
  647. PROCESS1_64;
  648. return XXH64_avalanche(h64);
  649. case 27: PROCESS8_64;
  650. /* fallthrough */
  651. case 19: PROCESS8_64;
  652. /* fallthrough */
  653. case 11: PROCESS8_64;
  654. PROCESS1_64;
  655. PROCESS1_64;
  656. PROCESS1_64;
  657. return XXH64_avalanche(h64);
  658. case 31: PROCESS8_64;
  659. /* fallthrough */
  660. case 23: PROCESS8_64;
  661. /* fallthrough */
  662. case 15: PROCESS8_64;
  663. /* fallthrough */
  664. case 7: PROCESS4_64;
  665. /* fallthrough */
  666. case 3: PROCESS1_64;
  667. /* fallthrough */
  668. case 2: PROCESS1_64;
  669. /* fallthrough */
  670. case 1: PROCESS1_64;
  671. /* fallthrough */
  672. case 0: return XXH64_avalanche(h64);
  673. }
  674. /* impossible to reach */
  675. assert(0);
  676. return 0; /* unreachable, but some compilers complain without it */
  677. }
  678. FORCE_INLINE U64
  679. XXH64_endian_align(const void* input, size_t len, U64 seed,
  680. XXH_endianess endian, XXH_alignment align)
  681. {
  682. const BYTE* p = (const BYTE*)input;
  683. const BYTE* bEnd = p + len;
  684. U64 h64;
  685. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  686. if (p==NULL) {
  687. len=0;
  688. bEnd=p=(const BYTE*)(size_t)32;
  689. }
  690. #endif
  691. if (len>=32) {
  692. const BYTE* const limit = bEnd - 32;
  693. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  694. U64 v2 = seed + PRIME64_2;
  695. U64 v3 = seed + 0;
  696. U64 v4 = seed - PRIME64_1;
  697. do {
  698. v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
  699. v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
  700. v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
  701. v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
  702. } while (p<=limit);
  703. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  704. h64 = XXH64_mergeRound(h64, v1);
  705. h64 = XXH64_mergeRound(h64, v2);
  706. h64 = XXH64_mergeRound(h64, v3);
  707. h64 = XXH64_mergeRound(h64, v4);
  708. } else {
  709. h64 = seed + PRIME64_5;
  710. }
  711. h64 += (U64) len;
  712. return XXH64_finalize(h64, p, len, endian, align);
  713. }
  714. XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  715. {
  716. #if 0
  717. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  718. XXH64_state_t state;
  719. XXH64_reset(&state, seed);
  720. XXH64_update(&state, input, len);
  721. return XXH64_digest(&state);
  722. #else
  723. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  724. if (XXH_FORCE_ALIGN_CHECK) {
  725. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  726. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  727. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  728. else
  729. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  730. } }
  731. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  732. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  733. else
  734. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  735. #endif
  736. }
  737. /*====== Hash Streaming ======*/
  738. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  739. {
  740. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  741. }
  742. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  743. {
  744. XXH_free(statePtr);
  745. return XXH_OK;
  746. }
  747. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dstState, const XXH64_state_t* srcState)
  748. {
  749. memcpy(dstState, srcState, sizeof(*dstState));
  750. }
  751. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
  752. {
  753. XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  754. memset(&state, 0, sizeof(state));
  755. state.v1 = seed + PRIME64_1 + PRIME64_2;
  756. state.v2 = seed + PRIME64_2;
  757. state.v3 = seed + 0;
  758. state.v4 = seed - PRIME64_1;
  759. /* do not write into reserved, planned to be removed in a future version */
  760. memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved));
  761. return XXH_OK;
  762. }
  763. FORCE_INLINE XXH_errorcode
  764. XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)
  765. {
  766. if (input==NULL)
  767. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  768. return XXH_OK;
  769. #else
  770. return XXH_ERROR;
  771. #endif
  772. { const BYTE* p = (const BYTE*)input;
  773. const BYTE* const bEnd = p + len;
  774. state->total_len += len;
  775. if (state->memsize + len < 32) { /* fill in tmp buffer */
  776. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
  777. state->memsize += (U32)len;
  778. return XXH_OK;
  779. }
  780. if (state->memsize) { /* tmp buffer is full */
  781. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
  782. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
  783. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
  784. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
  785. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
  786. p += 32-state->memsize;
  787. state->memsize = 0;
  788. }
  789. if (p+32 <= bEnd) {
  790. const BYTE* const limit = bEnd - 32;
  791. U64 v1 = state->v1;
  792. U64 v2 = state->v2;
  793. U64 v3 = state->v3;
  794. U64 v4 = state->v4;
  795. do {
  796. v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
  797. v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
  798. v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
  799. v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
  800. } while (p<=limit);
  801. state->v1 = v1;
  802. state->v2 = v2;
  803. state->v3 = v3;
  804. state->v4 = v4;
  805. }
  806. if (p < bEnd) {
  807. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  808. state->memsize = (unsigned)(bEnd-p);
  809. }
  810. }
  811. return XXH_OK;
  812. }
  813. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  814. {
  815. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  816. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  817. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  818. else
  819. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  820. }
  821. FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)
  822. {
  823. U64 h64;
  824. if (state->total_len >= 32) {
  825. U64 const v1 = state->v1;
  826. U64 const v2 = state->v2;
  827. U64 const v3 = state->v3;
  828. U64 const v4 = state->v4;
  829. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  830. h64 = XXH64_mergeRound(h64, v1);
  831. h64 = XXH64_mergeRound(h64, v2);
  832. h64 = XXH64_mergeRound(h64, v3);
  833. h64 = XXH64_mergeRound(h64, v4);
  834. } else {
  835. h64 = state->v3 /*seed*/ + PRIME64_5;
  836. }
  837. h64 += (U64) state->total_len;
  838. return XXH64_finalize(h64, state->mem64, (size_t)state->total_len, endian, XXH_aligned);
  839. }
  840. XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  841. {
  842. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  843. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  844. return XXH64_digest_endian(state_in, XXH_littleEndian);
  845. else
  846. return XXH64_digest_endian(state_in, XXH_bigEndian);
  847. }
  848. /*====== Canonical representation ======*/
  849. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  850. {
  851. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  852. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  853. memcpy(dst, &hash, sizeof(*dst));
  854. }
  855. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  856. {
  857. return XXH_readBE64(src);
  858. }
  859. #endif /* XXH_NO_LONG_LONG */