xxhash.cc 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. /*
  6. * xxHash - Fast Hash algorithm
  7. * Copyright (C) 2012-2016, Yann Collet
  8. *
  9. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are
  13. * met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following disclaimer
  19. * in the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * You can contact the author at :
  35. * - xxHash homepage: http://www.xxhash.com
  36. * - xxHash source repository : https://github.com/Cyan4973/xxHash
  37. */
  38. /* since xxhash.c can be included (via XXH_INLINE_ALL),
  39. * it's good practice to protect it with guard
  40. * in case of multiples inclusions */
  41. #ifndef XXHASH_C_01393879
  42. #define XXHASH_C_01393879
  43. /* *************************************
  44. * Tuning parameters
  45. ***************************************/
  46. /*!XXH_FORCE_MEMORY_ACCESS :
  47. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  48. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  49. * The below switch allow to select different access method for improved performance.
  50. * Method 0 (default) : use `memcpy()`. Safe and portable.
  51. * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable).
  52. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  53. * Method 2 : direct access. This method doesn't depend on compiler but violate C standard.
  54. * It can generate buggy code on targets which do not support unaligned memory accesses.
  55. * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6)
  56. * See http://stackoverflow.com/a/32095106/646947 for details.
  57. * Prefer these methods in priority order (0 > 1 > 2)
  58. */
  59. #ifndef XXH_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  60. # if !defined(__clang__) && defined(__GNUC__) && defined(__ARM_FEATURE_UNALIGNED) && defined(__ARM_ARCH) && (__ARM_ARCH == 6)
  61. # define XXH_FORCE_MEMORY_ACCESS 2
  62. # elif !defined(__clang__) && ((defined(__INTEL_COMPILER) && !defined(_WIN32)) || \
  63. (defined(__GNUC__) && (defined(__ARM_ARCH) && __ARM_ARCH >= 7)))
  64. # define XXH_FORCE_MEMORY_ACCESS 1
  65. # endif
  66. #endif
  67. /*!XXH_ACCEPT_NULL_INPUT_POINTER :
  68. * If input pointer is NULL, xxHash default behavior is to dereference it, triggering a segfault.
  69. * When this macro is enabled, xxHash actively checks input for null pointer.
  70. * It it is, result for null input pointers is the same as a null-length input.
  71. */
  72. #ifndef XXH_ACCEPT_NULL_INPUT_POINTER /* can be defined externally */
  73. # define XXH_ACCEPT_NULL_INPUT_POINTER 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;
  79. * set it to 0 when the input is guaranteed to be aligned,
  80. * or when alignment doesn't matter for performance.
  81. */
  82. #ifndef XXH_FORCE_ALIGN_CHECK /* can be defined externally */
  83. # if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  84. # define XXH_FORCE_ALIGN_CHECK 0
  85. # else
  86. # define XXH_FORCE_ALIGN_CHECK 1
  87. # endif
  88. #endif
  89. /*!XXH_REROLL:
  90. * Whether to reroll XXH32_finalize, and XXH64_finalize,
  91. * instead of using an unrolled jump table/if statement loop.
  92. *
  93. * This is automatically defined on -Os/-Oz on GCC and Clang. */
  94. #ifndef XXH_REROLL
  95. # if defined(__OPTIMIZE_SIZE__)
  96. # define XXH_REROLL 1
  97. # else
  98. # define XXH_REROLL 0
  99. # endif
  100. #endif
  101. /* *************************************
  102. * Includes & Memory related functions
  103. ***************************************/
  104. /*! Modify the local functions below should you wish to use some other memory routines
  105. * for malloc(), free() */
  106. #include <stdlib.h>
  107. static void* XXH_malloc(size_t s) { return malloc(s); }
  108. static void XXH_free (void* p) { free(p); }
  109. /*! and for memcpy() */
  110. #include <string.h>
  111. static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
  112. #include <limits.h> /* ULLONG_MAX */
  113. #ifndef XXH_STATIC_LINKING_ONLY
  114. #define XXH_STATIC_LINKING_ONLY
  115. #endif
  116. #include "xxhash.h"
  117. /* BEGIN RocksDB customizations */
  118. #include "util/util.h" /* for FALLTHROUGH_INTENDED, inserted as appropriate */
  119. /* END RocksDB customizations */
  120. /* *************************************
  121. * Compiler Specific Options
  122. ***************************************/
  123. #ifdef _MSC_VER /* Visual Studio */
  124. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  125. # define XXH_FORCE_INLINE static __forceinline
  126. # define XXH_NO_INLINE static __declspec(noinline)
  127. #else
  128. # if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  129. # ifdef __GNUC__
  130. # define XXH_FORCE_INLINE static inline __attribute__((always_inline))
  131. # define XXH_NO_INLINE static __attribute__((noinline))
  132. # else
  133. # define XXH_FORCE_INLINE static inline
  134. # define XXH_NO_INLINE static
  135. # endif
  136. # else
  137. # define XXH_FORCE_INLINE static
  138. # define XXH_NO_INLINE static
  139. # endif /* __STDC_VERSION__ */
  140. #endif
  141. /* *************************************
  142. * Debug
  143. ***************************************/
  144. /* DEBUGLEVEL is expected to be defined externally,
  145. * typically through compiler command line.
  146. * Value must be a number. */
  147. #ifndef DEBUGLEVEL
  148. # define DEBUGLEVEL 0
  149. #endif
  150. #if (DEBUGLEVEL>=1)
  151. # include <assert.h> /* note : can still be disabled with NDEBUG */
  152. # define XXH_ASSERT(c) assert(c)
  153. #else
  154. # define XXH_ASSERT(c) ((void)0)
  155. #endif
  156. /* note : use after variable declarations */
  157. #define XXH_STATIC_ASSERT(c) { enum { XXH_sa = 1/(int)(!!(c)) }; }
  158. /* *************************************
  159. * Basic Types
  160. ***************************************/
  161. #if !defined (__VMS) \
  162. && (defined (__cplusplus) \
  163. || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  164. # include <stdint.h>
  165. typedef uint8_t xxh_u8;
  166. #else
  167. typedef unsigned char xxh_u8;
  168. #endif
  169. typedef XXH32_hash_t xxh_u32;
  170. /* === Memory access === */
  171. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  172. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  173. static xxh_u32 XXH_read32(const void* memPtr) { return *(const xxh_u32*) memPtr; }
  174. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  175. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  176. /* currently only defined for gcc and icc */
  177. typedef union { xxh_u32 u32; } __attribute__((packed)) unalign;
  178. static xxh_u32 XXH_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  179. #else
  180. /* portable and safe solution. Generally efficient.
  181. * see : http://stackoverflow.com/a/32095106/646947
  182. */
  183. static xxh_u32 XXH_read32(const void* memPtr)
  184. {
  185. xxh_u32 val;
  186. memcpy(&val, memPtr, sizeof(val));
  187. return val;
  188. }
  189. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  190. /* === Endianess === */
  191. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  192. /* XXH_CPU_LITTLE_ENDIAN can be defined externally, for example on the compiler command line */
  193. #ifndef XXH_CPU_LITTLE_ENDIAN
  194. # if defined(_WIN32) /* Windows is always little endian */ \
  195. || defined(__LITTLE_ENDIAN__) \
  196. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  197. # define XXH_CPU_LITTLE_ENDIAN 1
  198. # elif defined(__BIG_ENDIAN__) \
  199. || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  200. # define XXH_CPU_LITTLE_ENDIAN 0
  201. # else
  202. static int XXH_isLittleEndian(void)
  203. {
  204. const union { xxh_u32 u; xxh_u8 c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  205. return one.c[0];
  206. }
  207. # define XXH_CPU_LITTLE_ENDIAN XXH_isLittleEndian()
  208. # endif
  209. #endif
  210. /* ****************************************
  211. * Compiler-specific Functions and Macros
  212. ******************************************/
  213. #define XXH_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  214. #ifndef __has_builtin
  215. # define __has_builtin(x) 0
  216. #endif
  217. #if !defined(NO_CLANG_BUILTIN) && __has_builtin(__builtin_rotateleft32) && __has_builtin(__builtin_rotateleft64)
  218. # define XXH_rotl32 __builtin_rotateleft32
  219. # define XXH_rotl64 __builtin_rotateleft64
  220. /* Note : although _rotl exists for minGW (GCC under windows), performance seems poor */
  221. #elif defined(_MSC_VER)
  222. # define XXH_rotl32(x,r) _rotl(x,r)
  223. # define XXH_rotl64(x,r) _rotl64(x,r)
  224. #else
  225. # define XXH_rotl32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
  226. # define XXH_rotl64(x,r) (((x) << (r)) | ((x) >> (64 - (r))))
  227. #endif
  228. #if defined(_MSC_VER) /* Visual Studio */
  229. # define XXH_swap32 _byteswap_ulong
  230. #elif XXH_GCC_VERSION >= 403
  231. # define XXH_swap32 __builtin_bswap32
  232. #else
  233. static xxh_u32 XXH_swap32 (xxh_u32 x)
  234. {
  235. return ((x << 24) & 0xff000000 ) |
  236. ((x << 8) & 0x00ff0000 ) |
  237. ((x >> 8) & 0x0000ff00 ) |
  238. ((x >> 24) & 0x000000ff );
  239. }
  240. #endif
  241. /* ***************************
  242. * Memory reads
  243. *****************************/
  244. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  245. XXH_FORCE_INLINE xxh_u32 XXH_readLE32(const void* ptr)
  246. {
  247. return XXH_CPU_LITTLE_ENDIAN ? XXH_read32(ptr) : XXH_swap32(XXH_read32(ptr));
  248. }
  249. static xxh_u32 XXH_readBE32(const void* ptr)
  250. {
  251. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap32(XXH_read32(ptr)) : XXH_read32(ptr);
  252. }
  253. XXH_FORCE_INLINE xxh_u32
  254. XXH_readLE32_align(const void* ptr, XXH_alignment align)
  255. {
  256. if (align==XXH_unaligned) {
  257. return XXH_readLE32(ptr);
  258. } else {
  259. return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u32*)ptr : XXH_swap32(*(const xxh_u32*)ptr);
  260. }
  261. }
  262. /* *************************************
  263. * Misc
  264. ***************************************/
  265. XXH_PUBLIC_API unsigned XXH_versionNumber (void) { return XXH_VERSION_NUMBER; }
  266. /* *******************************************************************
  267. * 32-bit hash functions
  268. *********************************************************************/
  269. static const xxh_u32 PRIME32_1 = 0x9E3779B1U; /* 0b10011110001101110111100110110001 */
  270. static const xxh_u32 PRIME32_2 = 0x85EBCA77U; /* 0b10000101111010111100101001110111 */
  271. static const xxh_u32 PRIME32_3 = 0xC2B2AE3DU; /* 0b11000010101100101010111000111101 */
  272. static const xxh_u32 PRIME32_4 = 0x27D4EB2FU; /* 0b00100111110101001110101100101111 */
  273. static const xxh_u32 PRIME32_5 = 0x165667B1U; /* 0b00010110010101100110011110110001 */
  274. static xxh_u32 XXH32_round(xxh_u32 acc, xxh_u32 input)
  275. {
  276. acc += input * PRIME32_2;
  277. acc = XXH_rotl32(acc, 13);
  278. acc *= PRIME32_1;
  279. #if defined(__GNUC__) && defined(__SSE4_1__) && !defined(XXH_ENABLE_AUTOVECTORIZE)
  280. /* UGLY HACK:
  281. * This inline assembly hack forces acc into a normal register. This is the
  282. * only thing that prevents GCC and Clang from autovectorizing the XXH32 loop
  283. * (pragmas and attributes don't work for some resason) without globally
  284. * disabling SSE4.1.
  285. *
  286. * The reason we want to avoid vectorization is because despite working on
  287. * 4 integers at a time, there are multiple factors slowing XXH32 down on
  288. * SSE4:
  289. * - There's a ridiculous amount of lag from pmulld (10 cycles of latency on newer chips!)
  290. * making it slightly slower to multiply four integers at once compared to four
  291. * integers independently. Even when pmulld was fastest, Sandy/Ivy Bridge, it is
  292. * still not worth it to go into SSE just to multiply unless doing a long operation.
  293. *
  294. * - Four instructions are required to rotate,
  295. * movqda tmp, v // not required with VEX encoding
  296. * pslld tmp, 13 // tmp <<= 13
  297. * psrld v, 19 // x >>= 19
  298. * por v, tmp // x |= tmp
  299. * compared to one for scalar:
  300. * roll v, 13 // reliably fast across the board
  301. * shldl v, v, 13 // Sandy Bridge and later prefer this for some reason
  302. *
  303. * - Instruction level parallelism is actually more beneficial here because the
  304. * SIMD actually serializes this operation: While v1 is rotating, v2 can load data,
  305. * while v3 can multiply. SSE forces them to operate together.
  306. *
  307. * How this hack works:
  308. * __asm__("" // Declare an assembly block but don't declare any instructions
  309. * : // However, as an Input/Output Operand,
  310. * "+r" // constrain a read/write operand (+) as a general purpose register (r).
  311. * (acc) // and set acc as the operand
  312. * );
  313. *
  314. * Because of the 'r', the compiler has promised that seed will be in a
  315. * general purpose register and the '+' says that it will be 'read/write',
  316. * so it has to assume it has changed. It is like volatile without all the
  317. * loads and stores.
  318. *
  319. * Since the argument has to be in a normal register (not an SSE register),
  320. * each time XXH32_round is called, it is impossible to vectorize. */
  321. __asm__("" : "+r" (acc));
  322. #endif
  323. return acc;
  324. }
  325. /* mix all bits */
  326. static xxh_u32 XXH32_avalanche(xxh_u32 h32)
  327. {
  328. h32 ^= h32 >> 15;
  329. h32 *= PRIME32_2;
  330. h32 ^= h32 >> 13;
  331. h32 *= PRIME32_3;
  332. h32 ^= h32 >> 16;
  333. return(h32);
  334. }
  335. #define XXH_get32bits(p) XXH_readLE32_align(p, align)
  336. static xxh_u32
  337. XXH32_finalize(xxh_u32 h32, const xxh_u8* ptr, size_t len, XXH_alignment align)
  338. {
  339. #define PROCESS1 \
  340. h32 += (*ptr++) * PRIME32_5; \
  341. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  342. #define PROCESS4 \
  343. h32 += XXH_get32bits(ptr) * PRIME32_3; \
  344. ptr+=4; \
  345. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  346. /* Compact rerolled version */
  347. if (XXH_REROLL) {
  348. len &= 15;
  349. while (len >= 4) {
  350. PROCESS4;
  351. len -= 4;
  352. }
  353. while (len > 0) {
  354. PROCESS1;
  355. --len;
  356. }
  357. return XXH32_avalanche(h32);
  358. } else {
  359. switch(len&15) /* or switch(bEnd - p) */ {
  360. case 12: PROCESS4;
  361. FALLTHROUGH_INTENDED;
  362. /* fallthrough */
  363. case 8: PROCESS4;
  364. FALLTHROUGH_INTENDED;
  365. /* fallthrough */
  366. case 4: PROCESS4;
  367. return XXH32_avalanche(h32);
  368. case 13: PROCESS4;
  369. FALLTHROUGH_INTENDED;
  370. /* fallthrough */
  371. case 9: PROCESS4;
  372. FALLTHROUGH_INTENDED;
  373. /* fallthrough */
  374. case 5: PROCESS4;
  375. PROCESS1;
  376. return XXH32_avalanche(h32);
  377. case 14: PROCESS4;
  378. FALLTHROUGH_INTENDED;
  379. /* fallthrough */
  380. case 10: PROCESS4;
  381. FALLTHROUGH_INTENDED;
  382. /* fallthrough */
  383. case 6: PROCESS4;
  384. PROCESS1;
  385. PROCESS1;
  386. return XXH32_avalanche(h32);
  387. case 15: PROCESS4;
  388. FALLTHROUGH_INTENDED;
  389. /* fallthrough */
  390. case 11: PROCESS4;
  391. FALLTHROUGH_INTENDED;
  392. /* fallthrough */
  393. case 7: PROCESS4;
  394. FALLTHROUGH_INTENDED;
  395. /* fallthrough */
  396. case 3: PROCESS1;
  397. FALLTHROUGH_INTENDED;
  398. /* fallthrough */
  399. case 2: PROCESS1;
  400. FALLTHROUGH_INTENDED;
  401. /* fallthrough */
  402. case 1: PROCESS1;
  403. FALLTHROUGH_INTENDED;
  404. /* fallthrough */
  405. case 0: return XXH32_avalanche(h32);
  406. }
  407. XXH_ASSERT(0);
  408. return h32; /* reaching this point is deemed impossible */
  409. }
  410. }
  411. XXH_FORCE_INLINE xxh_u32
  412. XXH32_endian_align(const xxh_u8* input, size_t len, xxh_u32 seed, XXH_alignment align)
  413. {
  414. const xxh_u8* bEnd = input + len;
  415. xxh_u32 h32;
  416. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  417. if (input==NULL) {
  418. len=0;
  419. bEnd=input=(const xxh_u8*)(size_t)16;
  420. }
  421. #endif
  422. if (len>=16) {
  423. const xxh_u8* const limit = bEnd - 15;
  424. xxh_u32 v1 = seed + PRIME32_1 + PRIME32_2;
  425. xxh_u32 v2 = seed + PRIME32_2;
  426. xxh_u32 v3 = seed + 0;
  427. xxh_u32 v4 = seed - PRIME32_1;
  428. do {
  429. v1 = XXH32_round(v1, XXH_get32bits(input)); input += 4;
  430. v2 = XXH32_round(v2, XXH_get32bits(input)); input += 4;
  431. v3 = XXH32_round(v3, XXH_get32bits(input)); input += 4;
  432. v4 = XXH32_round(v4, XXH_get32bits(input)); input += 4;
  433. } while (input < limit);
  434. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7)
  435. + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  436. } else {
  437. h32 = seed + PRIME32_5;
  438. }
  439. h32 += (xxh_u32)len;
  440. return XXH32_finalize(h32, input, len&15, align);
  441. }
  442. XXH_PUBLIC_API XXH32_hash_t XXH32 (const void* input, size_t len, XXH32_hash_t seed)
  443. {
  444. #if 0
  445. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  446. XXH32_state_t state;
  447. XXH32_reset(&state, seed);
  448. XXH32_update(&state, (const xxh_u8*)input, len);
  449. return XXH32_digest(&state);
  450. #else
  451. if (XXH_FORCE_ALIGN_CHECK) {
  452. if ((((size_t)input) & 3) == 0) { /* Input is 4-bytes aligned, leverage the speed benefit */
  453. return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
  454. } }
  455. return XXH32_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
  456. #endif
  457. }
  458. /*====== Hash streaming ======*/
  459. XXH_PUBLIC_API XXH32_state_t* XXH32_createState(void)
  460. {
  461. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  462. }
  463. XXH_PUBLIC_API XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  464. {
  465. XXH_free(statePtr);
  466. return XXH_OK;
  467. }
  468. XXH_PUBLIC_API void XXH32_copyState(XXH32_state_t* dstState, const XXH32_state_t* srcState)
  469. {
  470. memcpy(dstState, srcState, sizeof(*dstState));
  471. }
  472. XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, XXH32_hash_t seed)
  473. {
  474. XXH32_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  475. memset(&state, 0, sizeof(state));
  476. state.v1 = seed + PRIME32_1 + PRIME32_2;
  477. state.v2 = seed + PRIME32_2;
  478. state.v3 = seed + 0;
  479. state.v4 = seed - PRIME32_1;
  480. /* do not write into reserved, planned to be removed in a future version */
  481. memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved));
  482. return XXH_OK;
  483. }
  484. XXH_PUBLIC_API XXH_errorcode
  485. XXH32_update(XXH32_state_t* state, const void* input, size_t len)
  486. {
  487. if (input==NULL)
  488. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  489. return XXH_OK;
  490. #else
  491. return XXH_ERROR;
  492. #endif
  493. { const xxh_u8* p = (const xxh_u8*)input;
  494. const xxh_u8* const bEnd = p + len;
  495. state->total_len_32 += (XXH32_hash_t)len;
  496. state->large_len |= (XXH32_hash_t)((len>=16) | (state->total_len_32>=16));
  497. if (state->memsize + len < 16) { /* fill in tmp buffer */
  498. XXH_memcpy((xxh_u8*)(state->mem32) + state->memsize, input, len);
  499. state->memsize += (XXH32_hash_t)len;
  500. return XXH_OK;
  501. }
  502. if (state->memsize) { /* some data left from previous update */
  503. XXH_memcpy((xxh_u8*)(state->mem32) + state->memsize, input, 16-state->memsize);
  504. { const xxh_u32* p32 = state->mem32;
  505. state->v1 = XXH32_round(state->v1, XXH_readLE32(p32)); p32++;
  506. state->v2 = XXH32_round(state->v2, XXH_readLE32(p32)); p32++;
  507. state->v3 = XXH32_round(state->v3, XXH_readLE32(p32)); p32++;
  508. state->v4 = XXH32_round(state->v4, XXH_readLE32(p32));
  509. }
  510. p += 16-state->memsize;
  511. state->memsize = 0;
  512. }
  513. // uintptr_t casts added to avoid array-bounds error on
  514. // some inlined calls
  515. if ((uintptr_t)p <= (uintptr_t)bEnd - 16) {
  516. const uintptr_t limit = (uintptr_t)bEnd - 16;
  517. xxh_u32 v1 = state->v1;
  518. xxh_u32 v2 = state->v2;
  519. xxh_u32 v3 = state->v3;
  520. xxh_u32 v4 = state->v4;
  521. do {
  522. v1 = XXH32_round(v1, XXH_readLE32(p)); p+=4;
  523. v2 = XXH32_round(v2, XXH_readLE32(p)); p+=4;
  524. v3 = XXH32_round(v3, XXH_readLE32(p)); p+=4;
  525. v4 = XXH32_round(v4, XXH_readLE32(p)); p+=4;
  526. } while ((uintptr_t)p <= limit);
  527. state->v1 = v1;
  528. state->v2 = v2;
  529. state->v3 = v3;
  530. state->v4 = v4;
  531. }
  532. if (p < bEnd) {
  533. XXH_memcpy(state->mem32, p, (size_t)(bEnd-p));
  534. state->memsize = (unsigned)(bEnd-p);
  535. }
  536. }
  537. return XXH_OK;
  538. }
  539. XXH_PUBLIC_API XXH32_hash_t XXH32_digest (const XXH32_state_t* state)
  540. {
  541. xxh_u32 h32;
  542. if (state->large_len) {
  543. h32 = XXH_rotl32(state->v1, 1)
  544. + XXH_rotl32(state->v2, 7)
  545. + XXH_rotl32(state->v3, 12)
  546. + XXH_rotl32(state->v4, 18);
  547. } else {
  548. h32 = state->v3 /* == seed */ + PRIME32_5;
  549. }
  550. h32 += state->total_len_32;
  551. return XXH32_finalize(h32, (const xxh_u8*)state->mem32, state->memsize, XXH_aligned);
  552. }
  553. /*====== Canonical representation ======*/
  554. /*! Default XXH result types are basic unsigned 32 and 64 bits.
  555. * The canonical representation follows human-readable write convention, aka big-endian (large digits first).
  556. * These functions allow transformation of hash result into and from its canonical format.
  557. * This way, hash values can be written into a file or buffer, remaining comparable across different systems.
  558. */
  559. XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t hash)
  560. {
  561. XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
  562. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
  563. memcpy(dst, &hash, sizeof(*dst));
  564. }
  565. XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)
  566. {
  567. return XXH_readBE32(src);
  568. }
  569. #ifndef XXH_NO_LONG_LONG
  570. /* *******************************************************************
  571. * 64-bit hash functions
  572. *********************************************************************/
  573. /*====== Memory access ======*/
  574. typedef XXH64_hash_t xxh_u64;
  575. /*! XXH_REROLL_XXH64:
  576. * Whether to reroll the XXH64_finalize() loop.
  577. *
  578. * Just like XXH32, we can unroll the XXH64_finalize() loop. This can be a performance gain
  579. * on 64-bit hosts, as only one jump is required.
  580. *
  581. * However, on 32-bit hosts, because arithmetic needs to be done with two 32-bit registers,
  582. * and 64-bit arithmetic needs to be simulated, it isn't beneficial to unroll. The code becomes
  583. * ridiculously large (the largest function in the binary on i386!), and rerolling it saves
  584. * anywhere from 3kB to 20kB. It is also slightly faster because it fits into cache better
  585. * and is more likely to be inlined by the compiler.
  586. *
  587. * If XXH_REROLL is defined, this is ignored and the loop is always rerolled. */
  588. #ifndef XXH_REROLL_XXH64
  589. # if (defined(__ILP32__) || defined(_ILP32)) /* ILP32 is often defined on 32-bit GCC family */ \
  590. || !(defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) /* x86-64 */ \
  591. || defined(_M_ARM64) || defined(__aarch64__) || defined(__arm64__) /* aarch64 */ \
  592. || defined(__PPC64__) || defined(__PPC64LE__) || defined(__ppc64__) || defined(__powerpc64__) /* ppc64 */ \
  593. || defined(__mips64__) || defined(__mips64)) /* mips64 */ \
  594. || (!defined(SIZE_MAX) || SIZE_MAX < ULLONG_MAX) /* check limits */
  595. # define XXH_REROLL_XXH64 1
  596. # else
  597. # define XXH_REROLL_XXH64 0
  598. # endif
  599. #endif /* !defined(XXH_REROLL_XXH64) */
  600. #if (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==2))
  601. /* Force direct memory access. Only works on CPU which support unaligned memory access in hardware */
  602. static xxh_u64 XXH_read64(const void* memPtr) { return *(const xxh_u64*) memPtr; }
  603. #elif (defined(XXH_FORCE_MEMORY_ACCESS) && (XXH_FORCE_MEMORY_ACCESS==1))
  604. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  605. /* currently only defined for gcc and icc */
  606. typedef union { xxh_u32 u32; xxh_u64 u64; } __attribute__((packed)) unalign64;
  607. static xxh_u64 XXH_read64(const void* ptr) { return ((const unalign64*)ptr)->u64; }
  608. #else
  609. /* portable and safe solution. Generally efficient.
  610. * see : http://stackoverflow.com/a/32095106/646947
  611. */
  612. static xxh_u64 XXH_read64(const void* memPtr)
  613. {
  614. xxh_u64 val;
  615. memcpy(&val, memPtr, sizeof(val));
  616. return val;
  617. }
  618. #endif /* XXH_FORCE_DIRECT_MEMORY_ACCESS */
  619. #if defined(_MSC_VER) /* Visual Studio */
  620. # define XXH_swap64 _byteswap_uint64
  621. #elif XXH_GCC_VERSION >= 403
  622. # define XXH_swap64 __builtin_bswap64
  623. #else
  624. static xxh_u64 XXH_swap64 (xxh_u64 x)
  625. {
  626. return ((x << 56) & 0xff00000000000000ULL) |
  627. ((x << 40) & 0x00ff000000000000ULL) |
  628. ((x << 24) & 0x0000ff0000000000ULL) |
  629. ((x << 8) & 0x000000ff00000000ULL) |
  630. ((x >> 8) & 0x00000000ff000000ULL) |
  631. ((x >> 24) & 0x0000000000ff0000ULL) |
  632. ((x >> 40) & 0x000000000000ff00ULL) |
  633. ((x >> 56) & 0x00000000000000ffULL);
  634. }
  635. #endif
  636. XXH_FORCE_INLINE xxh_u64 XXH_readLE64(const void* ptr)
  637. {
  638. return XXH_CPU_LITTLE_ENDIAN ? XXH_read64(ptr) : XXH_swap64(XXH_read64(ptr));
  639. }
  640. static xxh_u64 XXH_readBE64(const void* ptr)
  641. {
  642. return XXH_CPU_LITTLE_ENDIAN ? XXH_swap64(XXH_read64(ptr)) : XXH_read64(ptr);
  643. }
  644. XXH_FORCE_INLINE xxh_u64
  645. XXH_readLE64_align(const void* ptr, XXH_alignment align)
  646. {
  647. if (align==XXH_unaligned)
  648. return XXH_readLE64(ptr);
  649. else
  650. return XXH_CPU_LITTLE_ENDIAN ? *(const xxh_u64*)ptr : XXH_swap64(*(const xxh_u64*)ptr);
  651. }
  652. /*====== xxh64 ======*/
  653. static const xxh_u64 PRIME64_1 = 0x9E3779B185EBCA87ULL; /* 0b1001111000110111011110011011000110000101111010111100101010000111 */
  654. static const xxh_u64 PRIME64_2 = 0xC2B2AE3D27D4EB4FULL; /* 0b1100001010110010101011100011110100100111110101001110101101001111 */
  655. static const xxh_u64 PRIME64_3 = 0x165667B19E3779F9ULL; /* 0b0001011001010110011001111011000110011110001101110111100111111001 */
  656. static const xxh_u64 PRIME64_4 = 0x85EBCA77C2B2AE63ULL; /* 0b1000010111101011110010100111011111000010101100101010111001100011 */
  657. static const xxh_u64 PRIME64_5 = 0x27D4EB2F165667C5ULL; /* 0b0010011111010100111010110010111100010110010101100110011111000101 */
  658. static xxh_u64 XXH64_round(xxh_u64 acc, xxh_u64 input)
  659. {
  660. acc += input * PRIME64_2;
  661. acc = XXH_rotl64(acc, 31);
  662. acc *= PRIME64_1;
  663. return acc;
  664. }
  665. static xxh_u64 XXH64_mergeRound(xxh_u64 acc, xxh_u64 val)
  666. {
  667. val = XXH64_round(0, val);
  668. acc ^= val;
  669. acc = acc * PRIME64_1 + PRIME64_4;
  670. return acc;
  671. }
  672. static xxh_u64 XXH64_avalanche(xxh_u64 h64)
  673. {
  674. h64 ^= h64 >> 33;
  675. h64 *= PRIME64_2;
  676. h64 ^= h64 >> 29;
  677. h64 *= PRIME64_3;
  678. h64 ^= h64 >> 32;
  679. return h64;
  680. }
  681. #define XXH_get64bits(p) XXH_readLE64_align(p, align)
  682. static xxh_u64
  683. XXH64_finalize(xxh_u64 h64, const xxh_u8* ptr, size_t len, XXH_alignment align)
  684. {
  685. #define PROCESS1_64 \
  686. h64 ^= (*ptr++) * PRIME64_5; \
  687. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  688. #define PROCESS4_64 \
  689. h64 ^= (xxh_u64)(XXH_get32bits(ptr)) * PRIME64_1; \
  690. ptr+=4; \
  691. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  692. #define PROCESS8_64 { \
  693. xxh_u64 const k1 = XXH64_round(0, XXH_get64bits(ptr)); \
  694. ptr+=8; \
  695. h64 ^= k1; \
  696. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; \
  697. }
  698. /* Rerolled version for 32-bit targets is faster and much smaller. */
  699. if (XXH_REROLL || XXH_REROLL_XXH64) {
  700. len &= 31;
  701. while (len >= 8) {
  702. PROCESS8_64;
  703. len -= 8;
  704. }
  705. if (len >= 4) {
  706. PROCESS4_64;
  707. len -= 4;
  708. }
  709. while (len > 0) {
  710. PROCESS1_64;
  711. --len;
  712. }
  713. return XXH64_avalanche(h64);
  714. } else {
  715. switch(len & 31) {
  716. case 24: PROCESS8_64;
  717. FALLTHROUGH_INTENDED;
  718. /* fallthrough */
  719. case 16: PROCESS8_64;
  720. FALLTHROUGH_INTENDED;
  721. /* fallthrough */
  722. case 8: PROCESS8_64;
  723. return XXH64_avalanche(h64);
  724. case 28: PROCESS8_64;
  725. FALLTHROUGH_INTENDED;
  726. /* fallthrough */
  727. case 20: PROCESS8_64;
  728. FALLTHROUGH_INTENDED;
  729. /* fallthrough */
  730. case 12: PROCESS8_64;
  731. FALLTHROUGH_INTENDED;
  732. /* fallthrough */
  733. case 4: PROCESS4_64;
  734. return XXH64_avalanche(h64);
  735. case 25: PROCESS8_64;
  736. FALLTHROUGH_INTENDED;
  737. /* fallthrough */
  738. case 17: PROCESS8_64;
  739. FALLTHROUGH_INTENDED;
  740. /* fallthrough */
  741. case 9: PROCESS8_64;
  742. PROCESS1_64;
  743. return XXH64_avalanche(h64);
  744. case 29: PROCESS8_64;
  745. FALLTHROUGH_INTENDED;
  746. /* fallthrough */
  747. case 21: PROCESS8_64;
  748. FALLTHROUGH_INTENDED;
  749. /* fallthrough */
  750. case 13: PROCESS8_64;
  751. FALLTHROUGH_INTENDED;
  752. /* fallthrough */
  753. case 5: PROCESS4_64;
  754. PROCESS1_64;
  755. return XXH64_avalanche(h64);
  756. case 26: PROCESS8_64;
  757. FALLTHROUGH_INTENDED;
  758. /* fallthrough */
  759. case 18: PROCESS8_64;
  760. FALLTHROUGH_INTENDED;
  761. /* fallthrough */
  762. case 10: PROCESS8_64;
  763. PROCESS1_64;
  764. PROCESS1_64;
  765. return XXH64_avalanche(h64);
  766. case 30: PROCESS8_64;
  767. FALLTHROUGH_INTENDED;
  768. /* fallthrough */
  769. case 22: PROCESS8_64;
  770. FALLTHROUGH_INTENDED;
  771. /* fallthrough */
  772. case 14: PROCESS8_64;
  773. FALLTHROUGH_INTENDED;
  774. /* fallthrough */
  775. case 6: PROCESS4_64;
  776. PROCESS1_64;
  777. PROCESS1_64;
  778. return XXH64_avalanche(h64);
  779. case 27: PROCESS8_64;
  780. FALLTHROUGH_INTENDED;
  781. /* fallthrough */
  782. case 19: PROCESS8_64;
  783. FALLTHROUGH_INTENDED;
  784. /* fallthrough */
  785. case 11: PROCESS8_64;
  786. PROCESS1_64;
  787. PROCESS1_64;
  788. PROCESS1_64;
  789. return XXH64_avalanche(h64);
  790. case 31: PROCESS8_64;
  791. FALLTHROUGH_INTENDED;
  792. /* fallthrough */
  793. case 23: PROCESS8_64;
  794. FALLTHROUGH_INTENDED;
  795. /* fallthrough */
  796. case 15: PROCESS8_64;
  797. FALLTHROUGH_INTENDED;
  798. /* fallthrough */
  799. case 7: PROCESS4_64;
  800. FALLTHROUGH_INTENDED;
  801. /* fallthrough */
  802. case 3: PROCESS1_64;
  803. FALLTHROUGH_INTENDED;
  804. /* fallthrough */
  805. case 2: PROCESS1_64;
  806. FALLTHROUGH_INTENDED;
  807. /* fallthrough */
  808. case 1: PROCESS1_64;
  809. FALLTHROUGH_INTENDED;
  810. /* fallthrough */
  811. case 0: return XXH64_avalanche(h64);
  812. }
  813. }
  814. /* impossible to reach */
  815. XXH_ASSERT(0);
  816. return 0; /* unreachable, but some compilers complain without it */
  817. }
  818. XXH_FORCE_INLINE xxh_u64
  819. XXH64_endian_align(const xxh_u8* input, size_t len, xxh_u64 seed, XXH_alignment align)
  820. {
  821. const xxh_u8* bEnd = input + len;
  822. xxh_u64 h64;
  823. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  824. if (input==NULL) {
  825. len=0;
  826. bEnd=input=(const xxh_u8*)(size_t)32;
  827. }
  828. #endif
  829. if (len>=32) {
  830. const xxh_u8* const limit = bEnd - 32;
  831. xxh_u64 v1 = seed + PRIME64_1 + PRIME64_2;
  832. xxh_u64 v2 = seed + PRIME64_2;
  833. xxh_u64 v3 = seed + 0;
  834. xxh_u64 v4 = seed - PRIME64_1;
  835. do {
  836. v1 = XXH64_round(v1, XXH_get64bits(input)); input+=8;
  837. v2 = XXH64_round(v2, XXH_get64bits(input)); input+=8;
  838. v3 = XXH64_round(v3, XXH_get64bits(input)); input+=8;
  839. v4 = XXH64_round(v4, XXH_get64bits(input)); input+=8;
  840. } while (input<=limit);
  841. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  842. h64 = XXH64_mergeRound(h64, v1);
  843. h64 = XXH64_mergeRound(h64, v2);
  844. h64 = XXH64_mergeRound(h64, v3);
  845. h64 = XXH64_mergeRound(h64, v4);
  846. } else {
  847. h64 = seed + PRIME64_5;
  848. }
  849. h64 += (xxh_u64) len;
  850. return XXH64_finalize(h64, input, len, align);
  851. }
  852. XXH_PUBLIC_API XXH64_hash_t XXH64 (const void* input, size_t len, XXH64_hash_t seed)
  853. {
  854. #if 0
  855. /* Simple version, good for code maintenance, but unfortunately slow for small inputs */
  856. XXH64_state_t state;
  857. XXH64_reset(&state, seed);
  858. XXH64_update(&state, (const xxh_u8*)input, len);
  859. return XXH64_digest(&state);
  860. #else
  861. if (XXH_FORCE_ALIGN_CHECK) {
  862. if ((((size_t)input) & 7)==0) { /* Input is aligned, let's leverage the speed advantage */
  863. return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_aligned);
  864. } }
  865. return XXH64_endian_align((const xxh_u8*)input, len, seed, XXH_unaligned);
  866. #endif
  867. }
  868. /*====== Hash Streaming ======*/
  869. XXH_PUBLIC_API XXH64_state_t* XXH64_createState(void)
  870. {
  871. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  872. }
  873. XXH_PUBLIC_API XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  874. {
  875. XXH_free(statePtr);
  876. return XXH_OK;
  877. }
  878. XXH_PUBLIC_API void XXH64_copyState(XXH64_state_t* dstState, const XXH64_state_t* srcState)
  879. {
  880. memcpy(dstState, srcState, sizeof(*dstState));
  881. }
  882. XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, XXH64_hash_t seed)
  883. {
  884. XXH64_state_t state; /* using a local state to memcpy() in order to avoid strict-aliasing warnings */
  885. memset(&state, 0, sizeof(state));
  886. state.v1 = seed + PRIME64_1 + PRIME64_2;
  887. state.v2 = seed + PRIME64_2;
  888. state.v3 = seed + 0;
  889. state.v4 = seed - PRIME64_1;
  890. /* do not write into reserved64, might be removed in a future version */
  891. memcpy(statePtr, &state, sizeof(state) - sizeof(state.reserved64));
  892. return XXH_OK;
  893. }
  894. XXH_PUBLIC_API XXH_errorcode
  895. XXH64_update (XXH64_state_t* state, const void* input, size_t len)
  896. {
  897. if (input==NULL)
  898. #if defined(XXH_ACCEPT_NULL_INPUT_POINTER) && (XXH_ACCEPT_NULL_INPUT_POINTER>=1)
  899. return XXH_OK;
  900. #else
  901. return XXH_ERROR;
  902. #endif
  903. { const xxh_u8* p = (const xxh_u8*)input;
  904. const xxh_u8* const bEnd = p + len;
  905. state->total_len += len;
  906. if (state->memsize + len < 32) { /* fill in tmp buffer */
  907. XXH_memcpy(((xxh_u8*)state->mem64) + state->memsize, input, len);
  908. state->memsize += (xxh_u32)len;
  909. return XXH_OK;
  910. }
  911. if (state->memsize) { /* tmp buffer is full */
  912. XXH_memcpy(((xxh_u8*)state->mem64) + state->memsize, input, 32-state->memsize);
  913. state->v1 = XXH64_round(state->v1, XXH_readLE64(state->mem64+0));
  914. state->v2 = XXH64_round(state->v2, XXH_readLE64(state->mem64+1));
  915. state->v3 = XXH64_round(state->v3, XXH_readLE64(state->mem64+2));
  916. state->v4 = XXH64_round(state->v4, XXH_readLE64(state->mem64+3));
  917. p += 32-state->memsize;
  918. state->memsize = 0;
  919. }
  920. // uintptr_t casts added to avoid array-bounds error on
  921. // some inlined calls
  922. if ((uintptr_t)p + 32 <= (uintptr_t)bEnd) {
  923. const uintptr_t limit = (uintptr_t)bEnd - 32;
  924. xxh_u64 v1 = state->v1;
  925. xxh_u64 v2 = state->v2;
  926. xxh_u64 v3 = state->v3;
  927. xxh_u64 v4 = state->v4;
  928. do {
  929. v1 = XXH64_round(v1, XXH_readLE64(p)); p+=8;
  930. v2 = XXH64_round(v2, XXH_readLE64(p)); p+=8;
  931. v3 = XXH64_round(v3, XXH_readLE64(p)); p+=8;
  932. v4 = XXH64_round(v4, XXH_readLE64(p)); p+=8;
  933. } while ((uintptr_t)p <= limit);
  934. state->v1 = v1;
  935. state->v2 = v2;
  936. state->v3 = v3;
  937. state->v4 = v4;
  938. }
  939. if (p < bEnd) {
  940. XXH_memcpy(state->mem64, p, (size_t)(bEnd-p));
  941. state->memsize = (unsigned)(bEnd-p);
  942. }
  943. }
  944. return XXH_OK;
  945. }
  946. XXH_PUBLIC_API XXH64_hash_t XXH64_digest (const XXH64_state_t* state)
  947. {
  948. xxh_u64 h64;
  949. if (state->total_len >= 32) {
  950. xxh_u64 const v1 = state->v1;
  951. xxh_u64 const v2 = state->v2;
  952. xxh_u64 const v3 = state->v3;
  953. xxh_u64 const v4 = state->v4;
  954. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  955. h64 = XXH64_mergeRound(h64, v1);
  956. h64 = XXH64_mergeRound(h64, v2);
  957. h64 = XXH64_mergeRound(h64, v3);
  958. h64 = XXH64_mergeRound(h64, v4);
  959. } else {
  960. h64 = state->v3 /*seed*/ + PRIME64_5;
  961. }
  962. h64 += (xxh_u64) state->total_len;
  963. return XXH64_finalize(h64, (const xxh_u8*)state->mem64, (size_t)state->total_len, XXH_aligned);
  964. }
  965. /*====== Canonical representation ======*/
  966. XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
  967. {
  968. XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
  969. if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
  970. memcpy(dst, &hash, sizeof(*dst));
  971. }
  972. XXH_PUBLIC_API XXH64_hash_t XXH64_hashFromCanonical(const XXH64_canonical_t* src)
  973. {
  974. return XXH_readBE64(src);
  975. }
  976. /* *********************************************************************
  977. * XXH3
  978. * New generation hash designed for speed on small keys and vectorization
  979. ************************************************************************ */
  980. #include "xxh3p.h" /* XXH3 preview for RocksDB */
  981. #endif /* XXH_NO_LONG_LONG */
  982. #endif /* XXHASH_C_01393879 */