Hash.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. #include "Hash.h"
  2. #pragma warning(disable:4804)
  3. /*
  4. xxHash - Fast Hash algorithm
  5. Copyright (C) 2012-2014, Yann Collet.
  6. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  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. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the following disclaimer
  14. in the documentation and/or other materials provided with the
  15. distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. You can contact the author at :
  28. - xxHash source repository : http://code.google.com/p/xxhash/
  29. - public discussion board : https://groups.google.com/forum/#!forum/lz4c
  30. */
  31. //**************************************
  32. // Tuning parameters
  33. //**************************************
  34. // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
  35. // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected.
  36. // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance.
  37. // You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32).
  38. #if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  39. # define XXH_USE_UNALIGNED_ACCESS 1
  40. #endif
  41. // XXH_ACCEPT_NULL_INPUT_POINTER :
  42. // If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
  43. // When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
  44. // This option has a very small performance cost (only measurable on small inputs).
  45. // By default, this option is disabled. To enable it, uncomment below define :
  46. // #define XXH_ACCEPT_NULL_INPUT_POINTER 1
  47. // XXH_FORCE_NATIVE_FORMAT :
  48. // By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
  49. // Results are therefore identical for little-endian and big-endian CPU.
  50. // This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  51. // Should endian-independance be of no importance for your application, you may set the #define below to 1.
  52. // It will improve speed for Big-endian CPU.
  53. // This option has no impact on Little_Endian CPU.
  54. #define XXH_FORCE_NATIVE_FORMAT 0
  55. //**************************************
  56. // Compiler Specific Options
  57. //**************************************
  58. // Disable some Visual warning messages
  59. #ifdef _MSC_VER // Visual Studio
  60. # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
  61. #endif
  62. #ifdef _MSC_VER // Visual Studio
  63. # define FORCE_INLINE static __forceinline
  64. #else
  65. # ifdef __GNUC__
  66. # define FORCE_INLINE static inline __attribute__((always_inline))
  67. # else
  68. # define FORCE_INLINE static inline
  69. # endif
  70. #endif
  71. //**************************************
  72. // Includes & Memory related functions
  73. //**************************************
  74. typedef struct { long long ll[6]; } XXH32_state_t;
  75. typedef struct { long long ll[11]; } XXH64_state_t;
  76. typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode;
  77. // Modify the local functions below should you wish to use some other memory routines
  78. // for malloc(), free()
  79. #include <stdlib.h>
  80. FORCE_INLINE void* XXH_malloc(size_t s) { return malloc(s); }
  81. FORCE_INLINE void XXH_free (void* p) { free(p); }
  82. // for memcpy()
  83. #include <string.h>
  84. FORCE_INLINE void* XXH_memcpy(void* dest, const void* src, size_t size)
  85. {
  86. return memcpy(dest,src,size);
  87. }
  88. //**************************************
  89. // Basic Types
  90. //**************************************
  91. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
  92. # include <stdint.h>
  93. typedef uint8_t BYTE;
  94. typedef uint16_t U16;
  95. typedef uint32_t U32;
  96. typedef int32_t S32;
  97. typedef uint64_t U64;
  98. #else
  99. typedef unsigned char BYTE;
  100. typedef unsigned short U16;
  101. typedef unsigned int U32;
  102. typedef signed int S32;
  103. typedef unsigned long long U64;
  104. #endif
  105. #if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS)
  106. # define _PACKED __attribute__ ((packed))
  107. #else
  108. # define _PACKED
  109. #endif
  110. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  111. # ifdef __IBMC__
  112. # pragma pack(1)
  113. # else
  114. # pragma pack(push, 1)
  115. # endif
  116. #endif
  117. typedef struct _U32_S
  118. {
  119. U32 v;
  120. } _PACKED U32_S;
  121. typedef struct _U64_S
  122. {
  123. U64 v;
  124. } _PACKED U64_S;
  125. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  126. # pragma pack(pop)
  127. #endif
  128. #define A32(x) (((U32_S *)(x))->v)
  129. #define A64(x) (((U64_S *)(x))->v)
  130. //***************************************
  131. // Compiler-specific Functions and Macros
  132. //***************************************
  133. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  134. // Note : although _rotl exists for minGW (GCC under windows), performance seems poor
  135. #if defined(_MSC_VER)
  136. # define XXH_rotl32(x,r) _rotl(x,r)
  137. # define XXH_rotl64(x,r) _rotl64(x,r)
  138. #else
  139. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  140. # define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r)))
  141. #endif
  142. #if defined(_MSC_VER) // Visual Studio
  143. # define XXH_swap32 _byteswap_ulong
  144. # define XXH_swap64 _byteswap_uint64
  145. #elif GCC_VERSION >= 403
  146. # define XXH_swap32 __builtin_bswap32
  147. # define XXH_swap64 __builtin_bswap64
  148. #else
  149. static inline U32 XXH_swap32 (U32 x)
  150. {
  151. return ((x << 24) & 0xff000000 ) |
  152. ((x << 8) & 0x00ff0000 ) |
  153. ((x >> 8) & 0x0000ff00 ) |
  154. ((x >> 24) & 0x000000ff );
  155. }
  156. static inline U64 XXH_swap64 (U64 x)
  157. {
  158. return ((x << 56) & 0xff00000000000000ULL) |
  159. ((x << 40) & 0x00ff000000000000ULL) |
  160. ((x << 24) & 0x0000ff0000000000ULL) |
  161. ((x << 8) & 0x000000ff00000000ULL) |
  162. ((x >> 8) & 0x00000000ff000000ULL) |
  163. ((x >> 24) & 0x0000000000ff0000ULL) |
  164. ((x >> 40) & 0x000000000000ff00ULL) |
  165. ((x >> 56) & 0x00000000000000ffULL);
  166. }
  167. #endif
  168. //**************************************
  169. // Constants
  170. //**************************************
  171. #define PRIME32_1 2654435761U
  172. #define PRIME32_2 2246822519U
  173. #define PRIME32_3 3266489917U
  174. #define PRIME32_4 668265263U
  175. #define PRIME32_5 374761393U
  176. #define PRIME64_1 11400714785074694791ULL
  177. #define PRIME64_2 14029467366897019727ULL
  178. #define PRIME64_3 1609587929392839161ULL
  179. #define PRIME64_4 9650029242287828579ULL
  180. #define PRIME64_5 2870177450012600261ULL
  181. //**************************************
  182. // Architecture Macros
  183. //**************************************
  184. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  185. #ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch
  186. static const int one = 1;
  187. # define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one))
  188. #endif
  189. //**************************************
  190. // Macros
  191. //**************************************
  192. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations
  193. //****************************
  194. // Memory reads
  195. //****************************
  196. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  197. FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align)
  198. {
  199. if (align==XXH_unaligned)
  200. return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
  201. else
  202. return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
  203. }
  204. FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian)
  205. {
  206. return XXH_readLE32_align(ptr, endian, XXH_unaligned);
  207. }
  208. FORCE_INLINE U64 XXH_readLE64_align(const U64* ptr, XXH_endianess endian, XXH_alignment align)
  209. {
  210. if (align==XXH_unaligned)
  211. return endian==XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr));
  212. else
  213. return endian==XXH_littleEndian ? *ptr : XXH_swap64(*ptr);
  214. }
  215. FORCE_INLINE U64 XXH_readLE64(const U64* ptr, XXH_endianess endian)
  216. {
  217. return XXH_readLE64_align(ptr, endian, XXH_unaligned);
  218. }
  219. //****************************
  220. // Simple Hash Functions
  221. //****************************
  222. FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align)
  223. {
  224. const BYTE* p = (const BYTE*)input;
  225. const BYTE* bEnd = p + len;
  226. U32 h32;
  227. #define XXH_get32bits(p) XXH_readLE32_align((const U32*)p, endian, align)
  228. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  229. if (p==NULL)
  230. {
  231. len=0;
  232. bEnd=p=(const BYTE*)(size_t)16;
  233. }
  234. #endif
  235. if (len>=16)
  236. {
  237. const BYTE* const limit = bEnd - 16;
  238. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  239. U32 v2 = seed + PRIME32_2;
  240. U32 v3 = seed + 0;
  241. U32 v4 = seed - PRIME32_1;
  242. do
  243. {
  244. v1 += XXH_get32bits(p) * PRIME32_2;
  245. v1 = XXH_rotl32(v1, 13);
  246. v1 *= PRIME32_1;
  247. p+=4;
  248. v2 += XXH_get32bits(p) * PRIME32_2;
  249. v2 = XXH_rotl32(v2, 13);
  250. v2 *= PRIME32_1;
  251. p+=4;
  252. v3 += XXH_get32bits(p) * PRIME32_2;
  253. v3 = XXH_rotl32(v3, 13);
  254. v3 *= PRIME32_1;
  255. p+=4;
  256. v4 += XXH_get32bits(p) * PRIME32_2;
  257. v4 = XXH_rotl32(v4, 13);
  258. v4 *= PRIME32_1;
  259. p+=4;
  260. }
  261. while (p<=limit);
  262. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  263. }
  264. else
  265. {
  266. h32 = seed + PRIME32_5;
  267. }
  268. h32 += (U32) len;
  269. while (p+4<=bEnd)
  270. {
  271. h32 += XXH_get32bits(p) * PRIME32_3;
  272. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  273. p+=4;
  274. }
  275. while (p<bEnd)
  276. {
  277. h32 += (*p) * PRIME32_5;
  278. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  279. p++;
  280. }
  281. h32 ^= h32 >> 15;
  282. h32 *= PRIME32_2;
  283. h32 ^= h32 >> 13;
  284. h32 *= PRIME32_3;
  285. h32 ^= h32 >> 16;
  286. return h32;
  287. }
  288. unsigned int XXH32 (const void* input, size_t len, unsigned seed)
  289. {
  290. #if 0
  291. // Simple version, good for code maintenance, but unfortunately slow for small inputs
  292. XXH32_state_t state;
  293. XXH32_reset(&state, seed);
  294. XXH32_update(&state, input, len);
  295. return XXH32_digest(&state);
  296. #else
  297. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  298. # if !defined(XXH_USE_UNALIGNED_ACCESS)
  299. if ((((size_t)input) & 3) == 0) // Input is aligned, let's leverage the speed advantage
  300. {
  301. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  302. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  303. else
  304. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  305. }
  306. # endif
  307. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  308. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  309. else
  310. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  311. #endif
  312. }
  313. FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH_endianess endian, XXH_alignment align)
  314. {
  315. const BYTE* p = (const BYTE*)input;
  316. const BYTE* bEnd = p + len;
  317. U64 h64;
  318. #define XXH_get64bits(p) XXH_readLE64_align((const U64*)p, endian, align)
  319. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  320. if (p==NULL)
  321. {
  322. len=0;
  323. bEnd=p=(const BYTE*)(size_t)32;
  324. }
  325. #endif
  326. if (len>=32)
  327. {
  328. const BYTE* const limit = bEnd - 32;
  329. U64 v1 = seed + PRIME64_1 + PRIME64_2;
  330. U64 v2 = seed + PRIME64_2;
  331. U64 v3 = seed + 0;
  332. U64 v4 = seed - PRIME64_1;
  333. do
  334. {
  335. v1 += XXH_get64bits(p) * PRIME64_2;
  336. p+=8;
  337. v1 = XXH_rotl64(v1, 31);
  338. v1 *= PRIME64_1;
  339. v2 += XXH_get64bits(p) * PRIME64_2;
  340. p+=8;
  341. v2 = XXH_rotl64(v2, 31);
  342. v2 *= PRIME64_1;
  343. v3 += XXH_get64bits(p) * PRIME64_2;
  344. p+=8;
  345. v3 = XXH_rotl64(v3, 31);
  346. v3 *= PRIME64_1;
  347. v4 += XXH_get64bits(p) * PRIME64_2;
  348. p+=8;
  349. v4 = XXH_rotl64(v4, 31);
  350. v4 *= PRIME64_1;
  351. }
  352. while (p<=limit);
  353. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  354. v1 *= PRIME64_2;
  355. v1 = XXH_rotl64(v1, 31);
  356. v1 *= PRIME64_1;
  357. h64 ^= v1;
  358. h64 = h64 * PRIME64_1 + PRIME64_4;
  359. v2 *= PRIME64_2;
  360. v2 = XXH_rotl64(v2, 31);
  361. v2 *= PRIME64_1;
  362. h64 ^= v2;
  363. h64 = h64 * PRIME64_1 + PRIME64_4;
  364. v3 *= PRIME64_2;
  365. v3 = XXH_rotl64(v3, 31);
  366. v3 *= PRIME64_1;
  367. h64 ^= v3;
  368. h64 = h64 * PRIME64_1 + PRIME64_4;
  369. v4 *= PRIME64_2;
  370. v4 = XXH_rotl64(v4, 31);
  371. v4 *= PRIME64_1;
  372. h64 ^= v4;
  373. h64 = h64 * PRIME64_1 + PRIME64_4;
  374. }
  375. else
  376. {
  377. h64 = seed + PRIME64_5;
  378. }
  379. h64 += (U64) len;
  380. while (p+8<=bEnd)
  381. {
  382. U64 k1 = XXH_get64bits(p);
  383. k1 *= PRIME64_2;
  384. k1 = XXH_rotl64(k1,31);
  385. k1 *= PRIME64_1;
  386. h64 ^= k1;
  387. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  388. p+=8;
  389. }
  390. if (p+4<=bEnd)
  391. {
  392. h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1;
  393. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  394. p+=4;
  395. }
  396. while (p<bEnd)
  397. {
  398. h64 ^= (*p) * PRIME64_5;
  399. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  400. p++;
  401. }
  402. h64 ^= h64 >> 33;
  403. h64 *= PRIME64_2;
  404. h64 ^= h64 >> 29;
  405. h64 *= PRIME64_3;
  406. h64 ^= h64 >> 32;
  407. return h64;
  408. }
  409. unsigned long long XXH64 (const void* input, size_t len, unsigned long long seed)
  410. {
  411. #if 0
  412. // Simple version, good for code maintenance, but unfortunately slow for small inputs
  413. XXH64_state_t state;
  414. XXH64_reset(&state, seed);
  415. XXH64_update(&state, input, len);
  416. return XXH64_digest(&state);
  417. #else
  418. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  419. # if !defined(XXH_USE_UNALIGNED_ACCESS)
  420. if ((((size_t)input) & 7)==0) // Input is aligned, let's leverage the speed advantage
  421. {
  422. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  423. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  424. else
  425. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  426. }
  427. # endif
  428. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  429. return XXH64_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  430. else
  431. return XXH64_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  432. #endif
  433. }
  434. /****************************************************
  435. * Advanced Hash Functions
  436. ****************************************************/
  437. /*** Allocation ***/
  438. typedef struct
  439. {
  440. U64 total_len;
  441. U32 seed;
  442. U32 v1;
  443. U32 v2;
  444. U32 v3;
  445. U32 v4;
  446. U32 memsize;
  447. char memory[16];
  448. } XXH_istate32_t;
  449. typedef struct
  450. {
  451. U64 total_len;
  452. U64 seed;
  453. U64 v1;
  454. U64 v2;
  455. U64 v3;
  456. U64 v4;
  457. U32 memsize;
  458. char memory[32];
  459. } XXH_istate64_t;
  460. XXH32_state_t* XXH32_createState(void)
  461. {
  462. XXH_STATIC_ASSERT(sizeof(XXH32_state_t) >= sizeof(XXH_istate32_t)); // A compilation error here means XXH32_state_t is not large enough
  463. return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t));
  464. }
  465. XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr)
  466. {
  467. XXH_free(statePtr);
  468. return XXH_OK;
  469. };
  470. XXH64_state_t* XXH64_createState(void)
  471. {
  472. XXH_STATIC_ASSERT(sizeof(XXH64_state_t) >= sizeof(XXH_istate64_t)); // A compilation error here means XXH64_state_t is not large enough
  473. return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t));
  474. }
  475. XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr)
  476. {
  477. XXH_free(statePtr);
  478. return XXH_OK;
  479. };
  480. /*** Hash feed ***/
  481. XXH_errorcode XXH32_reset(XXH32_state_t* state_in, U32 seed)
  482. {
  483. XXH_istate32_t* state = (XXH_istate32_t*) state_in;
  484. state->seed = seed;
  485. state->v1 = seed + PRIME32_1 + PRIME32_2;
  486. state->v2 = seed + PRIME32_2;
  487. state->v3 = seed + 0;
  488. state->v4 = seed - PRIME32_1;
  489. state->total_len = 0;
  490. state->memsize = 0;
  491. return XXH_OK;
  492. }
  493. XXH_errorcode XXH64_reset(XXH64_state_t* state_in, unsigned long long seed)
  494. {
  495. XXH_istate64_t* state = (XXH_istate64_t*) state_in;
  496. state->seed = seed;
  497. state->v1 = seed + PRIME64_1 + PRIME64_2;
  498. state->v2 = seed + PRIME64_2;
  499. state->v3 = seed + 0;
  500. state->v4 = seed - PRIME64_1;
  501. state->total_len = 0;
  502. state->memsize = 0;
  503. return XXH_OK;
  504. }
  505. FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const void* input, size_t len, XXH_endianess endian)
  506. {
  507. XXH_istate32_t* state = (XXH_istate32_t *) state_in;
  508. const BYTE* p = (const BYTE*)input;
  509. const BYTE* const bEnd = p + len;
  510. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  511. if (input==NULL) return XXH_ERROR;
  512. #endif
  513. state->total_len += len;
  514. if (state->memsize + len < 16) // fill in tmp buffer
  515. {
  516. XXH_memcpy(state->memory + state->memsize, input, len);
  517. state->memsize += (U32)len;
  518. return XXH_OK;
  519. }
  520. if (state->memsize) // some data left from previous update
  521. {
  522. XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize);
  523. {
  524. const U32* p32 = (const U32*)state->memory;
  525. state->v1 += XXH_readLE32(p32, endian) * PRIME32_2;
  526. state->v1 = XXH_rotl32(state->v1, 13);
  527. state->v1 *= PRIME32_1;
  528. p32++;
  529. state->v2 += XXH_readLE32(p32, endian) * PRIME32_2;
  530. state->v2 = XXH_rotl32(state->v2, 13);
  531. state->v2 *= PRIME32_1;
  532. p32++;
  533. state->v3 += XXH_readLE32(p32, endian) * PRIME32_2;
  534. state->v3 = XXH_rotl32(state->v3, 13);
  535. state->v3 *= PRIME32_1;
  536. p32++;
  537. state->v4 += XXH_readLE32(p32, endian) * PRIME32_2;
  538. state->v4 = XXH_rotl32(state->v4, 13);
  539. state->v4 *= PRIME32_1;
  540. p32++;
  541. }
  542. p += 16-state->memsize;
  543. state->memsize = 0;
  544. }
  545. if (p <= bEnd-16)
  546. {
  547. const BYTE* const limit = bEnd - 16;
  548. U32 v1 = state->v1;
  549. U32 v2 = state->v2;
  550. U32 v3 = state->v3;
  551. U32 v4 = state->v4;
  552. do
  553. {
  554. v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  555. v1 = XXH_rotl32(v1, 13);
  556. v1 *= PRIME32_1;
  557. p+=4;
  558. v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  559. v2 = XXH_rotl32(v2, 13);
  560. v2 *= PRIME32_1;
  561. p+=4;
  562. v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  563. v3 = XXH_rotl32(v3, 13);
  564. v3 *= PRIME32_1;
  565. p+=4;
  566. v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2;
  567. v4 = XXH_rotl32(v4, 13);
  568. v4 *= PRIME32_1;
  569. p+=4;
  570. }
  571. while (p<=limit);
  572. state->v1 = v1;
  573. state->v2 = v2;
  574. state->v3 = v3;
  575. state->v4 = v4;
  576. }
  577. if (p < bEnd)
  578. {
  579. XXH_memcpy(state->memory, p, bEnd-p);
  580. state->memsize = (int)(bEnd-p);
  581. }
  582. return XXH_OK;
  583. }
  584. XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len)
  585. {
  586. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  587. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  588. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  589. else
  590. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  591. }
  592. FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state_in, XXH_endianess endian)
  593. {
  594. XXH_istate32_t* state = (XXH_istate32_t*) state_in;
  595. const BYTE * p = (const BYTE*)state->memory;
  596. BYTE* bEnd = (BYTE*)state->memory + state->memsize;
  597. U32 h32;
  598. if (state->total_len >= 16)
  599. {
  600. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  601. }
  602. else
  603. {
  604. h32 = state->seed + PRIME32_5;
  605. }
  606. h32 += (U32) state->total_len;
  607. while (p+4<=bEnd)
  608. {
  609. h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3;
  610. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  611. p+=4;
  612. }
  613. while (p<bEnd)
  614. {
  615. h32 += (*p) * PRIME32_5;
  616. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  617. p++;
  618. }
  619. h32 ^= h32 >> 15;
  620. h32 *= PRIME32_2;
  621. h32 ^= h32 >> 13;
  622. h32 *= PRIME32_3;
  623. h32 ^= h32 >> 16;
  624. return h32;
  625. }
  626. U32 XXH32_digest (const XXH32_state_t* state_in)
  627. {
  628. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  629. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  630. return XXH32_digest_endian(state_in, XXH_littleEndian);
  631. else
  632. return XXH32_digest_endian(state_in, XXH_bigEndian);
  633. }
  634. FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state_in, const void* input, size_t len, XXH_endianess endian)
  635. {
  636. XXH_istate64_t * state = (XXH_istate64_t *) state_in;
  637. const BYTE* p = (const BYTE*)input;
  638. const BYTE* const bEnd = p + len;
  639. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  640. if (input==NULL) return XXH_ERROR;
  641. #endif
  642. state->total_len += len;
  643. if (state->memsize + len < 32) // fill in tmp buffer
  644. {
  645. XXH_memcpy(state->memory + state->memsize, input, len);
  646. state->memsize += (U32)len;
  647. return XXH_OK;
  648. }
  649. if (state->memsize) // some data left from previous update
  650. {
  651. XXH_memcpy(state->memory + state->memsize, input, 32-state->memsize);
  652. {
  653. const U64* p64 = (const U64*)state->memory;
  654. state->v1 += XXH_readLE64(p64, endian) * PRIME64_2;
  655. state->v1 = XXH_rotl64(state->v1, 31);
  656. state->v1 *= PRIME64_1;
  657. p64++;
  658. state->v2 += XXH_readLE64(p64, endian) * PRIME64_2;
  659. state->v2 = XXH_rotl64(state->v2, 31);
  660. state->v2 *= PRIME64_1;
  661. p64++;
  662. state->v3 += XXH_readLE64(p64, endian) * PRIME64_2;
  663. state->v3 = XXH_rotl64(state->v3, 31);
  664. state->v3 *= PRIME64_1;
  665. p64++;
  666. state->v4 += XXH_readLE64(p64, endian) * PRIME64_2;
  667. state->v4 = XXH_rotl64(state->v4, 31);
  668. state->v4 *= PRIME64_1;
  669. p64++;
  670. }
  671. p += 32-state->memsize;
  672. state->memsize = 0;
  673. }
  674. if (p+32 <= bEnd)
  675. {
  676. const BYTE* const limit = bEnd - 32;
  677. U64 v1 = state->v1;
  678. U64 v2 = state->v2;
  679. U64 v3 = state->v3;
  680. U64 v4 = state->v4;
  681. do
  682. {
  683. v1 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  684. v1 = XXH_rotl64(v1, 31);
  685. v1 *= PRIME64_1;
  686. p+=8;
  687. v2 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  688. v2 = XXH_rotl64(v2, 31);
  689. v2 *= PRIME64_1;
  690. p+=8;
  691. v3 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  692. v3 = XXH_rotl64(v3, 31);
  693. v3 *= PRIME64_1;
  694. p+=8;
  695. v4 += XXH_readLE64((const U64*)p, endian) * PRIME64_2;
  696. v4 = XXH_rotl64(v4, 31);
  697. v4 *= PRIME64_1;
  698. p+=8;
  699. }
  700. while (p<=limit);
  701. state->v1 = v1;
  702. state->v2 = v2;
  703. state->v3 = v3;
  704. state->v4 = v4;
  705. }
  706. if (p < bEnd)
  707. {
  708. XXH_memcpy(state->memory, p, bEnd-p);
  709. state->memsize = (int)(bEnd-p);
  710. }
  711. return XXH_OK;
  712. }
  713. XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len)
  714. {
  715. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  716. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  717. return XXH64_update_endian(state_in, input, len, XXH_littleEndian);
  718. else
  719. return XXH64_update_endian(state_in, input, len, XXH_bigEndian);
  720. }
  721. FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state_in, XXH_endianess endian)
  722. {
  723. XXH_istate64_t * state = (XXH_istate64_t *) state_in;
  724. const BYTE * p = (const BYTE*)state->memory;
  725. BYTE* bEnd = (BYTE*)state->memory + state->memsize;
  726. U64 h64;
  727. if (state->total_len >= 32)
  728. {
  729. U64 v1 = state->v1;
  730. U64 v2 = state->v2;
  731. U64 v3 = state->v3;
  732. U64 v4 = state->v4;
  733. h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18);
  734. v1 *= PRIME64_2;
  735. v1 = XXH_rotl64(v1, 31);
  736. v1 *= PRIME64_1;
  737. h64 ^= v1;
  738. h64 = h64*PRIME64_1 + PRIME64_4;
  739. v2 *= PRIME64_2;
  740. v2 = XXH_rotl64(v2, 31);
  741. v2 *= PRIME64_1;
  742. h64 ^= v2;
  743. h64 = h64*PRIME64_1 + PRIME64_4;
  744. v3 *= PRIME64_2;
  745. v3 = XXH_rotl64(v3, 31);
  746. v3 *= PRIME64_1;
  747. h64 ^= v3;
  748. h64 = h64*PRIME64_1 + PRIME64_4;
  749. v4 *= PRIME64_2;
  750. v4 = XXH_rotl64(v4, 31);
  751. v4 *= PRIME64_1;
  752. h64 ^= v4;
  753. h64 = h64*PRIME64_1 + PRIME64_4;
  754. }
  755. else
  756. {
  757. h64 = state->seed + PRIME64_5;
  758. }
  759. h64 += (U64) state->total_len;
  760. while (p+8<=bEnd)
  761. {
  762. U64 k1 = XXH_readLE64((const U64*)p, endian);
  763. k1 *= PRIME64_2;
  764. k1 = XXH_rotl64(k1,31);
  765. k1 *= PRIME64_1;
  766. h64 ^= k1;
  767. h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4;
  768. p+=8;
  769. }
  770. if (p+4<=bEnd)
  771. {
  772. h64 ^= (U64)(XXH_readLE32((const U32*)p, endian)) * PRIME64_1;
  773. h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
  774. p+=4;
  775. }
  776. while (p<bEnd)
  777. {
  778. h64 ^= (*p) * PRIME64_5;
  779. h64 = XXH_rotl64(h64, 11) * PRIME64_1;
  780. p++;
  781. }
  782. h64 ^= h64 >> 33;
  783. h64 *= PRIME64_2;
  784. h64 ^= h64 >> 29;
  785. h64 *= PRIME64_3;
  786. h64 ^= h64 >> 32;
  787. return h64;
  788. }
  789. unsigned long long XXH64_digest (const XXH64_state_t* state_in)
  790. {
  791. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  792. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  793. return XXH64_digest_endian(state_in, XXH_littleEndian);
  794. else
  795. return XXH64_digest_endian(state_in, XXH_bigEndian);
  796. }
  797. //////////////////////////////////////////////////////////////////////////
  798. //-----------------------------------------------------------------------------
  799. // MurmurHash3 was written by Austin Appleby, and is placed in the public
  800. // domain. The author hereby disclaims copyright to this source code.
  801. // Note - The x86 and x64 versions do _not_ produce the same results, as the
  802. // algorithms are optimized for their respective platforms. You can still
  803. // compile and run any of them on any platform, but your performance with the
  804. // non-native version will be less than optimal.
  805. //-----------------------------------------------------------------------------
  806. // Platform-specific functions and macros
  807. // Microsoft Visual Studio
  808. #if defined(_MSC_VER)
  809. #ifndef FORCE_INLINE
  810. #define FORCE_INLINE __forceinline
  811. #endif
  812. #include <stdlib.h>
  813. #define ROTL32(x,y) _rotl(x,y)
  814. #define ROTL64(x,y) _rotl64(x,y)
  815. #define BIG_CONSTANT(x) (x)
  816. // Other compilers
  817. #else // defined(_MSC_VER)
  818. //#define FORCE_INLINE inline __attribute__((always_inline))
  819. inline uint32_t rotl32(uint32_t x, int8_t r)
  820. {
  821. return (x << r) | (x >> (32 - r));
  822. }
  823. inline uint64_t rotl64(uint64_t x, int8_t r)
  824. {
  825. return (x << r) | (x >> (64 - r));
  826. }
  827. #define ROTL32(x,y) rotl32(x,y)
  828. #define ROTL64(x,y) rotl64(x,y)
  829. #define BIG_CONSTANT(x) (x##LLU)
  830. #endif // !defined(_MSC_VER)
  831. //-----------------------------------------------------------------------------
  832. // Block read - if your platform needs to do endian-swapping or can only
  833. // handle aligned reads, do the conversion here
  834. FORCE_INLINE uint32_t getblock32(const uint32_t * p, int i)
  835. {
  836. return p[i];
  837. }
  838. FORCE_INLINE uint64_t getblock64(const uint64_t * p, int i)
  839. {
  840. return p[i];
  841. }
  842. //-----------------------------------------------------------------------------
  843. // Finalization mix - force all bits of a hash block to avalanche
  844. FORCE_INLINE uint32_t fmix32(uint32_t h)
  845. {
  846. h ^= h >> 16;
  847. h *= 0x85ebca6b;
  848. h ^= h >> 13;
  849. h *= 0xc2b2ae35;
  850. h ^= h >> 16;
  851. return h;
  852. }
  853. //----------
  854. FORCE_INLINE uint64_t fmix64(uint64_t k)
  855. {
  856. k ^= k >> 33;
  857. k *= BIG_CONSTANT(0xff51afd7ed558ccd);
  858. k ^= k >> 33;
  859. k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
  860. k ^= k >> 33;
  861. return k;
  862. }
  863. //-----------------------------------------------------------------------------
  864. void MurmurHash3_x86_32(const void * key, int len,
  865. uint32_t seed, void * out)
  866. {
  867. const uint8_t * data = (const uint8_t*) key;
  868. const int nblocks = len / 4;
  869. uint32_t h1 = seed;
  870. const uint32_t c1 = 0xcc9e2d51;
  871. const uint32_t c2 = 0x1b873593;
  872. //----------
  873. // body
  874. const uint32_t * blocks = (const uint32_t *) (data + nblocks * 4);
  875. for (int i = -nblocks; i; i++)
  876. {
  877. uint32_t k1 = getblock32(blocks, i);
  878. k1 *= c1;
  879. k1 = ROTL32(k1, 15);
  880. k1 *= c2;
  881. h1 ^= k1;
  882. h1 = ROTL32(h1, 13);
  883. h1 = h1 * 5 + 0xe6546b64;
  884. }
  885. //----------
  886. // tail
  887. const uint8_t * tail = (const uint8_t*) (data + nblocks * 4);
  888. uint32_t k1 = 0;
  889. switch (len & 3)
  890. {
  891. case 3: k1 ^= tail[2] << 16;
  892. case 2: k1 ^= tail[1] << 8;
  893. case 1: k1 ^= tail[0];
  894. k1 *= c1; k1 = ROTL32(k1, 15); k1 *= c2; h1 ^= k1;
  895. };
  896. //----------
  897. // finalization
  898. h1 ^= len;
  899. h1 = fmix32(h1);
  900. *(uint32_t*) out = h1;
  901. }
  902. //-----------------------------------------------------------------------------
  903. void MurmurHash3_x86_128(const void * key, const int len,
  904. uint32_t seed, void * out)
  905. {
  906. const uint8_t * data = (const uint8_t*) key;
  907. const int nblocks = len / 16;
  908. uint32_t h1 = seed;
  909. uint32_t h2 = seed;
  910. uint32_t h3 = seed;
  911. uint32_t h4 = seed;
  912. const uint32_t c1 = 0x239b961b;
  913. const uint32_t c2 = 0xab0e9789;
  914. const uint32_t c3 = 0x38b34ae5;
  915. const uint32_t c4 = 0xa1e38b93;
  916. //----------
  917. // body
  918. const uint32_t * blocks = (const uint32_t *) (data + nblocks * 16);
  919. for (int i = -nblocks; i; i++)
  920. {
  921. uint32_t k1 = getblock32(blocks, i * 4 + 0);
  922. uint32_t k2 = getblock32(blocks, i * 4 + 1);
  923. uint32_t k3 = getblock32(blocks, i * 4 + 2);
  924. uint32_t k4 = getblock32(blocks, i * 4 + 3);
  925. k1 *= c1; k1 = ROTL32(k1, 15); k1 *= c2; h1 ^= k1;
  926. h1 = ROTL32(h1, 19); h1 += h2; h1 = h1 * 5 + 0x561ccd1b;
  927. k2 *= c2; k2 = ROTL32(k2, 16); k2 *= c3; h2 ^= k2;
  928. h2 = ROTL32(h2, 17); h2 += h3; h2 = h2 * 5 + 0x0bcaa747;
  929. k3 *= c3; k3 = ROTL32(k3, 17); k3 *= c4; h3 ^= k3;
  930. h3 = ROTL32(h3, 15); h3 += h4; h3 = h3 * 5 + 0x96cd1c35;
  931. k4 *= c4; k4 = ROTL32(k4, 18); k4 *= c1; h4 ^= k4;
  932. h4 = ROTL32(h4, 13); h4 += h1; h4 = h4 * 5 + 0x32ac3b17;
  933. }
  934. //----------
  935. // tail
  936. const uint8_t * tail = (const uint8_t*) (data + nblocks * 16);
  937. uint32_t k1 = 0;
  938. uint32_t k2 = 0;
  939. uint32_t k3 = 0;
  940. uint32_t k4 = 0;
  941. switch (len & 15)
  942. {
  943. case 15: k4 ^= tail[14] << 16;
  944. case 14: k4 ^= tail[13] << 8;
  945. case 13: k4 ^= tail[12] << 0;
  946. k4 *= c4; k4 = ROTL32(k4, 18); k4 *= c1; h4 ^= k4;
  947. case 12: k3 ^= tail[11] << 24;
  948. case 11: k3 ^= tail[10] << 16;
  949. case 10: k3 ^= tail[9] << 8;
  950. case 9: k3 ^= tail[8] << 0;
  951. k3 *= c3; k3 = ROTL32(k3, 17); k3 *= c4; h3 ^= k3;
  952. case 8: k2 ^= tail[7] << 24;
  953. case 7: k2 ^= tail[6] << 16;
  954. case 6: k2 ^= tail[5] << 8;
  955. case 5: k2 ^= tail[4] << 0;
  956. k2 *= c2; k2 = ROTL32(k2, 16); k2 *= c3; h2 ^= k2;
  957. case 4: k1 ^= tail[3] << 24;
  958. case 3: k1 ^= tail[2] << 16;
  959. case 2: k1 ^= tail[1] << 8;
  960. case 1: k1 ^= tail[0] << 0;
  961. k1 *= c1; k1 = ROTL32(k1, 15); k1 *= c2; h1 ^= k1;
  962. };
  963. //----------
  964. // finalization
  965. h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
  966. h1 += h2; h1 += h3; h1 += h4;
  967. h2 += h1; h3 += h1; h4 += h1;
  968. h1 = fmix32(h1);
  969. h2 = fmix32(h2);
  970. h3 = fmix32(h3);
  971. h4 = fmix32(h4);
  972. h1 += h2; h1 += h3; h1 += h4;
  973. h2 += h1; h3 += h1; h4 += h1;
  974. ((uint32_t*) out)[0] = h1;
  975. ((uint32_t*) out)[1] = h2;
  976. ((uint32_t*) out)[2] = h3;
  977. ((uint32_t*) out)[3] = h4;
  978. }
  979. //-----------------------------------------------------------------------------
  980. void MurmurHash3_x64_128(const void * key, const int len,
  981. const uint32_t seed, void * out)
  982. {
  983. const uint8_t * data = (const uint8_t*) key;
  984. const int nblocks = len / 16;
  985. uint64_t h1 = seed;
  986. uint64_t h2 = seed;
  987. const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
  988. const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
  989. //----------
  990. // body
  991. const uint64_t * blocks = (const uint64_t *) (data);
  992. for (int i = 0; i < nblocks; i++)
  993. {
  994. uint64_t k1 = getblock64(blocks, i * 2 + 0);
  995. uint64_t k2 = getblock64(blocks, i * 2 + 1);
  996. k1 *= c1; k1 = ROTL64(k1, 31); k1 *= c2; h1 ^= k1;
  997. h1 = ROTL64(h1, 27); h1 += h2; h1 = h1 * 5 + 0x52dce729;
  998. k2 *= c2; k2 = ROTL64(k2, 33); k2 *= c1; h2 ^= k2;
  999. h2 = ROTL64(h2, 31); h2 += h1; h2 = h2 * 5 + 0x38495ab5;
  1000. }
  1001. //----------
  1002. // tail
  1003. const uint8_t * tail = (const uint8_t*) (data + nblocks * 16);
  1004. uint64_t k1 = 0;
  1005. uint64_t k2 = 0;
  1006. switch (len & 15)
  1007. {
  1008. case 15: k2 ^= ((uint64_t) tail[14]) << 48;
  1009. case 14: k2 ^= ((uint64_t) tail[13]) << 40;
  1010. case 13: k2 ^= ((uint64_t) tail[12]) << 32;
  1011. case 12: k2 ^= ((uint64_t) tail[11]) << 24;
  1012. case 11: k2 ^= ((uint64_t) tail[10]) << 16;
  1013. case 10: k2 ^= ((uint64_t) tail[9]) << 8;
  1014. case 9: k2 ^= ((uint64_t) tail[8]) << 0;
  1015. k2 *= c2; k2 = ROTL64(k2, 33); k2 *= c1; h2 ^= k2;
  1016. case 8: k1 ^= ((uint64_t) tail[7]) << 56;
  1017. case 7: k1 ^= ((uint64_t) tail[6]) << 48;
  1018. case 6: k1 ^= ((uint64_t) tail[5]) << 40;
  1019. case 5: k1 ^= ((uint64_t) tail[4]) << 32;
  1020. case 4: k1 ^= ((uint64_t) tail[3]) << 24;
  1021. case 3: k1 ^= ((uint64_t) tail[2]) << 16;
  1022. case 2: k1 ^= ((uint64_t) tail[1]) << 8;
  1023. case 1: k1 ^= ((uint64_t) tail[0]) << 0;
  1024. k1 *= c1; k1 = ROTL64(k1, 31); k1 *= c2; h1 ^= k1;
  1025. };
  1026. //----------
  1027. // finalization
  1028. h1 ^= len; h2 ^= len;
  1029. h1 += h2;
  1030. h2 += h1;
  1031. h1 = fmix64(h1);
  1032. h2 = fmix64(h2);
  1033. h1 += h2;
  1034. h2 += h1;
  1035. ((uint64_t*) out)[0] = h1;
  1036. ((uint64_t*) out)[1] = h2;
  1037. }
  1038. //////////////////////////////////////////////////////////////////////////
  1039. USING_NS_BF;
  1040. uint64 Beefy::Hash64(const void* data, int length, uint64 seed)
  1041. {
  1042. return XXH64(data, length, seed);
  1043. }
  1044. uint64 Beefy::Hash64(uint64 hash, uint64 seed)
  1045. {
  1046. //return XXH64((const void*)&hash, sizeof(uint64), seed);
  1047. return fmix64(seed) ^ hash;
  1048. }
  1049. Val128 Beefy::Hash128(const void* data, int length)
  1050. {
  1051. Val128 hashVal;
  1052. MurmurHash3_x64_128(data, length, 0, &hashVal);
  1053. return hashVal;
  1054. }
  1055. Val128 Beefy::Hash128(const void* data, int length, const Val128& seed)
  1056. {
  1057. if (length < 8)
  1058. {
  1059. uint64 iVal = 0;
  1060. memcpy(&iVal, data, length);
  1061. Val128 val;
  1062. val.mLow = fmix64(seed.mLow) ^ iVal;
  1063. val.mHigh = val.mLow ^ seed.mHigh;
  1064. return val;
  1065. }
  1066. Val128 hashVal;
  1067. MurmurHash3_x64_128(data, length, (uint32)seed.mLow, &hashVal);
  1068. hashVal.mHigh ^= fmix64(seed.mHigh);
  1069. hashVal.mLow ^= fmix64(seed.mLow);
  1070. return hashVal;
  1071. }
  1072. //////////////////////////////////////////////////////////////////////////
  1073. // Only 63 chars - skip zero
  1074. static const char cHash64bToChar[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  1075. 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
  1076. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  1077. 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' };
  1078. String Beefy::HashEncode64(uint64 val)
  1079. {
  1080. String outStr;
  1081. if ((int64)val < 0)
  1082. {
  1083. uint64 flippedNum = (uint64)-(int64)val;
  1084. // Only flip if the encoded result would actually be shorter
  1085. if (flippedNum <= 0x00FFFFFFFFFFFFFFLL)
  1086. {
  1087. val = flippedNum;
  1088. outStr.Append('_');
  1089. }
  1090. }
  1091. for (int i = 0; i < 10; i++)
  1092. {
  1093. int charIdx = (int)((val >> (i * 6)) & 0x3F) - 1;
  1094. if (charIdx != -1)
  1095. outStr.Append(cHash64bToChar[charIdx]);
  1096. }
  1097. return outStr;
  1098. }
  1099. StringT<21> Beefy::HashEncode128(Val128 val)
  1100. {
  1101. StringT<21> str;
  1102. str += HashEncode64(val.mHigh);
  1103. str += HashEncode64(val.mLow);
  1104. return str;
  1105. }
  1106. //////////////////////////////////////////////////////////////////////////
  1107. static int gDbgVizIdx = 0;
  1108. HashContext::~HashContext()
  1109. {
  1110. #ifdef BF_PLATFORM_WINDOWS
  1111. delete mDbgVizStream;
  1112. #endif
  1113. }
  1114. void HashContext::Reset()
  1115. {
  1116. mBufSize = 0;
  1117. mBufOffset = 0;
  1118. }
  1119. void HashContext::Mixin(const void* data, int size)
  1120. {
  1121. if (size >= 1024)
  1122. {
  1123. Val128 hashVal = Hash128(data, size);
  1124. Mixin(&hashVal, 16);
  1125. return;
  1126. }
  1127. while (size > 0)
  1128. {
  1129. int addBytes = std::min(size, 1024 - mBufSize);
  1130. #ifdef BF_PLATFORM_WINDOWS
  1131. if (mDbgViz)
  1132. {
  1133. int findIdx = 0x2cc159;
  1134. if ((mBufOffset + mBufSize <= findIdx) && (mBufOffset + mBufSize + addBytes > findIdx))
  1135. {
  1136. NOP;
  1137. }
  1138. }
  1139. #endif
  1140. memcpy(&mBuf[mBufSize], data, addBytes);
  1141. mBufSize += addBytes;
  1142. size -= addBytes;
  1143. data = (uint8*)data + addBytes;
  1144. if (mBufSize == 1024)
  1145. {
  1146. Val128 val = Finish128();
  1147. *(Val128*)mBuf = val;
  1148. mBufSize = 16;
  1149. }
  1150. }
  1151. }
  1152. void HashContext::MixinHashContext(HashContext& ctx)
  1153. {
  1154. Mixin(ctx.mBuf, ctx.mBufSize);
  1155. }
  1156. void HashContext::MixinStr(const char* str)
  1157. {
  1158. Mixin(str, (int)strlen(str));
  1159. }
  1160. void HashContext::MixinStr(const StringImpl& str)
  1161. {
  1162. Mixin(str.c_str(), (int)str.length());
  1163. }
  1164. Val128 HashContext::Finish128()
  1165. {
  1166. #ifdef BF_PLATFORM_WINDOWS
  1167. if (mDbgViz)
  1168. {
  1169. // String dbg = "HashContext Dbg";
  1170. // for (int i = 0; i < mBufSize; i++)
  1171. // dbg += StrFormat(" %X:%02X", i, mBuf[i]);
  1172. // dbg += "\n";
  1173. // OutputDebugStr(dbg);
  1174. if (mDbgVizStream == NULL)
  1175. {
  1176. String filePath = StrFormat("c:\\temp\\hash%d.bin", gDbgVizIdx++);
  1177. mDbgVizStream = new FileStream();
  1178. if (mDbgVizStream->Open(filePath, "wb"))
  1179. {
  1180. OutputDebugStrF("Creating dbg hash: %s\n", filePath.c_str());
  1181. }
  1182. else
  1183. {
  1184. OutputDebugStrF("FAILED creating dbg hash: %s\n", filePath.c_str());
  1185. }
  1186. }
  1187. if ((mDbgVizStream != NULL) && (mDbgVizStream->IsOpen()))
  1188. mDbgVizStream->Write(mBuf, mBufSize);
  1189. }
  1190. #endif
  1191. if (mBufSize <= 16)
  1192. {
  1193. // We do this copy because 'result' gets zero-initialized and then we copy in any applicable data
  1194. Val128 result;
  1195. memcpy(&result, mBuf, mBufSize);
  1196. mBufOffset += mBufSize;
  1197. return result;
  1198. }
  1199. Val128 val = Hash128(mBuf, mBufSize);
  1200. mBufOffset += mBufSize;
  1201. mBufSize = 0;
  1202. return val;
  1203. }
  1204. uint64 HashContext::Finish64()
  1205. {
  1206. #ifdef BF_PLATFORM_WINDOWS
  1207. if (mDbgViz)
  1208. {
  1209. // String dbg = "HashContext Dbg";
  1210. // for (int i = 0; i < mBufSize; i++)
  1211. // dbg += StrFormat(" %X:%02X", i, mBuf[i]);
  1212. // dbg += "\n";
  1213. // OutputDebugStr(dbg);
  1214. if (mDbgVizStream == NULL)
  1215. {
  1216. String filePath = StrFormat("c:\\temp\\hash%d.bin", gDbgVizIdx++);
  1217. mDbgVizStream = new FileStream();
  1218. mDbgVizStream->Open(filePath, "wb");
  1219. OutputDebugStrF("Creating dbg hash: %s\n", filePath.c_str());
  1220. }
  1221. mDbgVizStream->Write(mBuf, mBufSize);
  1222. }
  1223. #endif
  1224. if (mBufSize <= 8)
  1225. {
  1226. // We do this copy because 'result' gets zero-initialized and then we copy in any applicable data
  1227. uint64 result = 0;
  1228. memcpy(&result, mBuf, mBufSize);
  1229. mBufOffset += mBufSize;
  1230. return result;
  1231. }
  1232. uint64 val = Hash64(mBuf, mBufSize);
  1233. mBufOffset += mBufSize;
  1234. mBufSize = 0;
  1235. return val;
  1236. }