xxhash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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) || \
  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. #define XXH_STATIC_LINKING_ONLY
  100. #include "xxhash.h"
  101. /* *************************************
  102. * Compiler Specific Options
  103. ***************************************/
  104. #ifdef _MSC_VER /* Visual Studio */
  105. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  106. # define FORCE_INLINE static __forceinline
  107. #else
  108. # if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  109. # ifdef __GNUC__
  110. # define FORCE_INLINE static inline __attribute__((always_inline))
  111. # else
  112. # define FORCE_INLINE static inline
  113. # endif
  114. # else
  115. # define FORCE_INLINE static
  116. # endif /* __STDC_VERSION__ */
  117. #endif
  118. /* *************************************
  119. * Basic Types
  120. ***************************************/
  121. #ifndef MEM_MODULE
  122. # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  123. # include <stdint.h>
  124. typedef uint8_t BYTE;
  125. typedef uint16_t U16;
  126. typedef uint32_t U32;
  127. typedef int32_t S32;
  128. # else
  129. typedef unsigned char BYTE;
  130. typedef unsigned short U16;
  131. typedef unsigned int U32;
  132. typedef signed int S32;
  133. # endif
  134. #endif
  135. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  136. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  137. static U32 XXH_read32(const void* memPtr) { return *(const U32*) memPtr; }
  138. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  139. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  140. /* currently only defined for gcc and icc */
  141. typedef union { U32 u32; } __attribute__((packed)) unalign;
  142. static U32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  143. #else
  144. /* portable and safe solution. Generally efficient.
  145. * see : http://stackoverflow.com/a/32095106/646947
  146. */
  147. static U32 XXH_read32(const void* memPtr)
  148. {
  149. U32 val;
  150. memcpy(&val, memPtr, sizeof(val));
  151. return val;
  152. }
  153. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  154. /* ****************************************
  155. * Compiler-specific Functions and Macros
  156. ******************************************/
  157. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  158. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  159. #if defined(_MSC_VER)
  160. # define XXH_rotl32(x,r) _rotl(x,r)
  161. # define XXH_rotl64(x,r) _rotl64(x,r)
  162. #else
  163. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  164. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  165. #endif
  166. #if defined(_MSC_VER) /* Visual Studio */
  167. # define XXH_swap32 _byteswap_ulong
  168. #elif GCC_VERSION >= 403
  169. # define XXH_swap32 __builtin_bswap32
  170. #else
  171. static U32 XXH_swap32 (U32 x)
  172. {
  173. return ((x << 24) & 0xff000000 ) |
  174. ((x << 8) & 0x00ff0000 ) |
  175. ((x >> 8) & 0x0000ff00 ) |
  176. ((x >> 24) & 0x000000ff );
  177. }
  178. #endif
  179. /* *************************************
  180. * Architecture Macros
  181. ***************************************/
  182. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  183. /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  184. #ifndef XXH_CPU_LITTLE_ENDIAN
  185. static const int g_one = 1;
  186. # define XXH_CPU_LITTLE_ENDIAN (*(const char*)(&g_one))
  187. #endif
  188. /* ***************************
  189. * Memory reads
  190. *****************************/
  191. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  192. FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  193. {
  194. if (align==XXH_unaligned)
  195. return endian==XXH_littleEndian ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  196. else
  197. return endian==XXH_littleEndian ? *(const U32*)ptr : XXH_swap32(*(const U32*)ptr);
  198. }
  199. FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian)
  200. {
  201. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  202. }
  203. /* *************************************
  204. * Macros
  205. ***************************************/
  206. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  207. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  208. #ifndef XXH_NO_LONG_LONG
  209. /* *******************************************************************
  210. * 64-bits hash functions
  211. *********************************************************************/
  212. #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align)
  213. /*====== Memory access ======*/
  214. #ifndef MEM_MODULE
  215. # define MEM_MODULE
  216. # if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  217. # include <stdint.h>
  218. typedef uint64_t U64;
  219. # else
  220. 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. */
  221. # endif
  222. #endif
  223. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  224. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  225. static U64 XXH_read64(const void* memPtr) { return *(const U64*) memPtr; }
  226. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  227. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  228. /* currently only defined for gcc and icc */
  229. typedef union { U32 u32; U64 u64; } __attribute__((packed)) unalign64;
  230. static U64 XXH_read64(const void* ptr) { return ((const unalign64*)ptr)->u64; }
  231. #else
  232. /* portable and safe solution. Generally efficient.
  233. * see : http://stackoverflow.com/a/32095106/646947
  234. */
  235. static U64 XXH_read64(const void* memPtr)
  236. {
  237. U64 val;
  238. memcpy(&val, memPtr, sizeof(val));
  239. return val;
  240. }
  241. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  242. #if defined(_MSC_VER) /* Visual Studio */
  243. # define XXH_swap64 _byteswap_uint64
  244. #elif GCC_VERSION >= 403
  245. # define XXH_swap64 __builtin_bswap64
  246. #else
  247. static U64 XXH_swap64 (U64 x)
  248. {
  249. return ((x << 56) & 0xff00000000000000ULL) |
  250. ((x << 40) & 0x00ff000000000000ULL) |
  251. ((x << 24) & 0x0000ff0000000000ULL) |
  252. ((x << 8) & 0x000000ff00000000ULL) |
  253. ((x >> 8) & 0x00000000ff000000ULL) |
  254. ((x >> 24) & 0x0000000000ff0000ULL) |
  255. ((x >> 40) & 0x000000000000ff00ULL) |
  256. ((x >> 56) & 0x00000000000000ffULL);
  257. }
  258. #endif
  259. FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align)
  260. {
  261. if (align==XXH_unaligned)
  262. return endian==XXH_littleEndian ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  263. else
  264. return endian==XXH_littleEndian ? *(const U64*)ptr : XXH_swap64(*(const U64*)ptr);
  265. }
  266. FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian)
  267. {
  268. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  269. }
  270. static U64 XXH_readBE64(const void* ptr)
  271. {
  272. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  273. }
  274. /*====== xxh64 ======*/
  275. static const U64 PRIME64_1 = 11400714785074694791ULL;
  276. static const U64 PRIME64_2 = 14029467366897019727ULL;
  277. static const U64 PRIME64_3 = 1609587929392839161ULL;
  278. static const U64 PRIME64_4 = 9650029242287828579ULL;
  279. static const U64 PRIME64_5 = 2870177450012600261ULL;
  280. static U64 XXH64_round(U64 acc, U64 input)
  281. {
  282. acc += input * PRIME64_2;
  283. acc = XXH_rotl64(acc, 31);
  284. acc *= PRIME64_1;
  285. return acc;
  286. }
  287. static U64 XXH64_mergeRound(U64 acc, U64 val)
  288. {
  289. val = XXH64_round(0, val);
  290. acc ^= val;
  291. acc = acc * PRIME64_1 + PRIME64_4;
  292. return acc;
  293. }
  294. FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
  295. {
  296. const BYTE* p = (const BYTE*)input;
  297. const BYTE* const bEnd = p + len;
  298. U64 h64;
  299. #define XXH_get64bits(p) XXH_readLE64_align(p, endian, align)
  300. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  301. if (p==NULL) {
  302. len=0;
  303. bEnd=p=(const BYTE*)(size_t)32;
  304. }
  305. #endif
  306. if (len>=32) {
  307. const BYTE* const limit = bEnd - 32;
  308. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  309. U64 v2 = seed + PRIME64_2;
  310. U64 v3 = seed + 0;
  311. U64 v4 = seed - PRIME64_1;
  312. do {
  313. v1 = XXH64_round(v1, XXH_get64bits(p)); p+=8;
  314. v2 = XXH64_round(v2, XXH_get64bits(p)); p+=8;
  315. v3 = XXH64_round(v3, XXH_get64bits(p)); p+=8;
  316. v4 = XXH64_round(v4, XXH_get64bits(p)); p+=8;
  317. } while (p<=limit);
  318. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  319. h64 = XXH64_mergeRound(h64, v1);
  320. h64 = XXH64_mergeRound(h64, v2);
  321. h64 = XXH64_mergeRound(h64, v3);
  322. h64 = XXH64_mergeRound(h64, v4);
  323. } else {
  324. h64 = seed + PRIME64_5;
  325. }
  326. h64 += (U64) len;
  327. while (p+8<=bEnd) {
  328. U64 const k1 = XXH64_round(0, XXH_get64bits(p));
  329. h64 ^= k1;
  330. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  331. p+=8;
  332. }
  333. if (p+4<=bEnd) {
  334. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
  335. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  336. p+=4;
  337. }
  338. while (p<bEnd) {
  339. h64 ^= (*p) * PRIME64_5;
  340. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  341. p++;
  342. }
  343. h64 ^= h64 >> 33;
  344. h64 *= PRIME64_2;
  345. h64 ^= h64 >> 29;
  346. h64 *= PRIME64_3;
  347. h64 ^= h64 >> 32;
  348. return h64;
  349. }
  350. XXH_PUBLIC_API unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  351. {
  352. #if 0
  353. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  354. XXH64_CREATESTATE_STATIC(state);
  355. XXH64_reset(state, seed);
  356. XXH64_update(state, input, len);
  357. return XXH64_digest(state);
  358. #else
  359. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  360. if (XXH_FORCE_ALIGN_CHECK) {
  361. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  362. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  363. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  364. else
  365. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  366. } }
  367. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  368. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  369. else
  370. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  371. #endif
  372. }
  373. /*====== Hash Streaming ======*/
  374. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  375. {
  376. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  377. }
  378. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  379. {
  380. XXH_free(statePtr);
  381. return XXH_OK;
  382. }
  383. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* restrict dstState, const XXH64_state_t* restrict srcState)
  384. {
  385. memcpy(dstState, srcState, sizeof(*dstState));
  386. }
  387. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed)
  388. {
  389. XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  390. memset(&state, 0, sizeof(state)-8); /* do not write into reserved, for future removal */
  391. state.v1 = seed + PRIME64_1 + PRIME64_2;
  392. state.v2 = seed + PRIME64_2;
  393. state.v3 = seed + 0;
  394. state.v4 = seed - PRIME64_1;
  395. memcpy(statePtr, &state, sizeof(state));
  396. return XXH_OK;
  397. }
  398. FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state, const void* input, size_t len, XXH_endianess endian)
  399. {
  400. const BYTE* p = (const BYTE*)input;
  401. const BYTE* const bEnd = p + len;
  402. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  403. if (input==NULL) return XXH_ERROR;
  404. #endif
  405. state->total_len += len;
  406. if (state->memsize + len < 32) { /* fill in tmp buffer */
  407. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len);
  408. state->memsize += (U32)len;
  409. return XXH_OK;
  410. }
  411. if (state->memsize) { /* tmp buffer is full */
  412. XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize);
  413. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0, endian));
  414. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1, endian));
  415. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2, endian));
  416. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3, endian));
  417. p += 32-state->memsize;
  418. state->memsize = 0;
  419. }
  420. if (p+32 <= bEnd) {
  421. const BYTE* const limit = bEnd - 32;
  422. U64 v1 = state->v1;
  423. U64 v2 = state->v2;
  424. U64 v3 = state->v3;
  425. U64 v4 = state->v4;
  426. do {
  427. v1 = XXH64_round(v1, XXH_readLE64(p, endian)); p+=8;
  428. v2 = XXH64_round(v2, XXH_readLE64(p, endian)); p+=8;
  429. v3 = XXH64_round(v3, XXH_readLE64(p, endian)); p+=8;
  430. v4 = XXH64_round(v4, XXH_readLE64(p, endian)); p+=8;
  431. } while (p<=limit);
  432. state->v1 = v1;
  433. state->v2 = v2;
  434. state->v3 = v3;
  435. state->v4 = v4;
  436. }
  437. if (p < bEnd) {
  438. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  439. state->memsize = (unsigned)(bEnd-p);
  440. }
  441. return XXH_OK;
  442. }
  443. XXH_PUBLIC_API XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  444. {
  445. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  446. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  447. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  448. else
  449. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  450. }
  451. FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state, XXH_endianess endian)
  452. {
  453. const BYTE * p = (const BYTE*)state->mem64;
  454. const BYTE* const bEnd = (const BYTE*)state->mem64 + state->memsize;
  455. U64 h64;
  456. if (state->total_len >= 32) {
  457. U64 const v1 = state->v1;
  458. U64 const v2 = state->v2;
  459. U64 const v3 = state->v3;
  460. U64 const v4 = state->v4;
  461. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  462. h64 = XXH64_mergeRound(h64, v1);
  463. h64 = XXH64_mergeRound(h64, v2);
  464. h64 = XXH64_mergeRound(h64, v3);
  465. h64 = XXH64_mergeRound(h64, v4);
  466. } else {
  467. h64 = state->v3 + PRIME64_5;
  468. }
  469. h64 += (U64) state->total_len;
  470. while (p+8<=bEnd) {
  471. U64 const k1 = XXH64_round(0, XXH_readLE64(p, endian));
  472. h64 ^= k1;
  473. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  474. p+=8;
  475. }
  476. if (p+4<=bEnd) {
  477. h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1;
  478. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  479. p+=4;
  480. }
  481. while (p<bEnd) {
  482. h64 ^= (*p) * PRIME64_5;
  483. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  484. p++;
  485. }
  486. h64 ^= h64 >> 33;
  487. h64 *= PRIME64_2;
  488. h64 ^= h64 >> 29;
  489. h64 *= PRIME64_3;
  490. h64 ^= h64 >> 32;
  491. return h64;
  492. }
  493. XXH_PUBLIC_API unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  494. {
  495. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  496. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  497. return XXH64_digest_endian(state_in, XXH_littleEndian);
  498. else
  499. return XXH64_digest_endian(state_in, XXH_bigEndian);
  500. }
  501. /*====== Canonical representation ======*/
  502. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  503. {
  504. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  505. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  506. memcpy(dst, &hash, sizeof(*dst));
  507. }
  508. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  509. {
  510. return XXH_readBE64(src);
  511. }
  512. #endif /* XXH_NO_LONG_LONG */