xxhash.c 27 KB

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