xxhash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. xxHash - Fast Hash algorithm
  3. Copyright (C) 2012-2014, Yann Collet.
  4. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above
  11. copyright notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other materials provided with the
  13. distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. You can contact the author at :
  26. - xxHash source repository : http://code.google.com/p/xxhash/
  27. */
  28. //**************************************
  29. // Tuning parameters
  30. //**************************************
  31. // Unaligned memory access is automatically enabled for "common" CPU, such as x86.
  32. // For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected.
  33. // If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance.
  34. // You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32).
  35. #if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
  36. # define XXH_USE_UNALIGNED_ACCESS 1
  37. #endif
  38. // XXH_ACCEPT_NULL_INPUT_POINTER :
  39. // If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
  40. // When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
  41. // This option has a very small performance cost (only measurable on small inputs).
  42. // By default, this option is disabled. To enable it, uncomment below define :
  43. //#define XXH_ACCEPT_NULL_INPUT_POINTER 1
  44. // XXH_FORCE_NATIVE_FORMAT :
  45. // By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
  46. // Results are therefore identical for little-endian and big-endian CPU.
  47. // This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
  48. // Should endian-independance be of no importance for your application, you may set the #define below to 1.
  49. // It will improve speed for Big-endian CPU.
  50. // This option has no impact on Little_Endian CPU.
  51. #define XXH_FORCE_NATIVE_FORMAT 0
  52. //**************************************
  53. // Compiler Specific Options
  54. //**************************************
  55. // Disable some Visual warning messages
  56. #ifdef _MSC_VER // Visual Studio
  57. # pragma warning(disable : 4127) // disable: C4127: conditional expression is constant
  58. #endif
  59. #ifdef _MSC_VER // Visual Studio
  60. # define FORCE_INLINE static __forceinline
  61. #else
  62. # ifdef __GNUC__
  63. # define FORCE_INLINE static inline __attribute__((always_inline))
  64. # else
  65. # define FORCE_INLINE static inline
  66. # endif
  67. #endif
  68. //**************************************
  69. // Includes & Memory related functions
  70. //**************************************
  71. #include "xxhash.h"
  72. // Modify the local functions below should you wish to use some other memory related routines
  73. // for malloc(), free()
  74. #include <stdlib.h>
  75. FORCE_INLINE void* XXH_malloc(size_t s) { return malloc(s); }
  76. FORCE_INLINE void XXH_free (void* p) { free(p); }
  77. // for memcpy()
  78. #include <string.h>
  79. FORCE_INLINE void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
  80. //**************************************
  81. // Basic Types
  82. //**************************************
  83. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99
  84. # include <stdint.h>
  85. typedef uint8_t BYTE;
  86. typedef uint16_t U16;
  87. typedef uint32_t U32;
  88. typedef int32_t S32;
  89. typedef uint64_t U64;
  90. #else
  91. typedef unsigned char BYTE;
  92. typedef unsigned short U16;
  93. typedef unsigned int U32;
  94. typedef signed int S32;
  95. typedef unsigned long long U64;
  96. #endif
  97. #if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS)
  98. # define _PACKED __attribute__ ((packed))
  99. #else
  100. # define _PACKED
  101. #endif
  102. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  103. # ifdef __IBMC__
  104. # pragma pack(1)
  105. # else
  106. # pragma pack(push, 1)
  107. # endif
  108. #endif
  109. typedef struct _U32_S { U32 v; } _PACKED U32_S;
  110. #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__)
  111. # pragma pack(pop)
  112. #endif
  113. #define A32(x) (((U32_S *)(x))->v)
  114. //***************************************
  115. // Compiler-specific Functions and Macros
  116. //***************************************
  117. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  118. // Note : although _rotl exists for minGW (GCC under windows), performance seems poor
  119. #if defined(_MSC_VER)
  120. # define XXH_rotl32(x,r) _rotl(x,r)
  121. #else
  122. # define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
  123. #endif
  124. #if defined(_MSC_VER) // Visual Studio
  125. # define XXH_swap32 _byteswap_ulong
  126. #elif GCC_VERSION >= 403
  127. # define XXH_swap32 __builtin_bswap32
  128. #else
  129. static inline U32 XXH_swap32 (U32 x) {
  130. return ((x << 24) & 0xff000000 ) |
  131. ((x << 8) & 0x00ff0000 ) |
  132. ((x >> 8) & 0x0000ff00 ) |
  133. ((x >> 24) & 0x000000ff );}
  134. #endif
  135. //**************************************
  136. // Constants
  137. //**************************************
  138. #define PRIME32_1 2654435761U
  139. #define PRIME32_2 2246822519U
  140. #define PRIME32_3 3266489917U
  141. #define PRIME32_4 668265263U
  142. #define PRIME32_5 374761393U
  143. //**************************************
  144. // Architecture Macros
  145. //**************************************
  146. typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
  147. #ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch
  148. static const int one = 1;
  149. # define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one))
  150. #endif
  151. //**************************************
  152. // Macros
  153. //**************************************
  154. #define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations
  155. //****************************
  156. // Memory reads
  157. //****************************
  158. typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment;
  159. FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align)
  160. {
  161. if (align==XXH_unaligned)
  162. return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
  163. else
  164. return endian==XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
  165. }
  166. FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); }
  167. //****************************
  168. // Simple Hash Functions
  169. //****************************
  170. FORCE_INLINE U32 XXH32_endian_align(const void* input, int len, U32 seed, XXH_endianess endian, XXH_alignment align)
  171. {
  172. const BYTE* p = (const BYTE*)input;
  173. const BYTE* const bEnd = p + len;
  174. U32 h32;
  175. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  176. if (p==NULL) { len=0; p=(const BYTE*)(size_t)16; }
  177. #endif
  178. if (len>=16)
  179. {
  180. const BYTE* const limit = bEnd - 16;
  181. U32 v1 = seed + PRIME32_1 + PRIME32_2;
  182. U32 v2 = seed + PRIME32_2;
  183. U32 v3 = seed + 0;
  184. U32 v4 = seed - PRIME32_1;
  185. do
  186. {
  187. v1 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
  188. v2 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
  189. v3 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
  190. v4 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
  191. } while (p<=limit);
  192. h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
  193. }
  194. else
  195. {
  196. h32 = seed + PRIME32_5;
  197. }
  198. h32 += (U32) len;
  199. while (p<=bEnd-4)
  200. {
  201. h32 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_3;
  202. h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
  203. p+=4;
  204. }
  205. while (p<bEnd)
  206. {
  207. h32 += (*p) * PRIME32_5;
  208. h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
  209. p++;
  210. }
  211. h32 ^= h32 >> 15;
  212. h32 *= PRIME32_2;
  213. h32 ^= h32 >> 13;
  214. h32 *= PRIME32_3;
  215. h32 ^= h32 >> 16;
  216. return h32;
  217. }
  218. U32 XXH32(const void* input, int len, U32 seed)
  219. {
  220. #if 0
  221. // Simple version, good for code maintenance, but unfortunately slow for small inputs
  222. void* state = XXH32_init(seed);
  223. XXH32_update(state, input, len);
  224. return XXH32_digest(state);
  225. #else
  226. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  227. # if !defined(XXH_USE_UNALIGNED_ACCESS)
  228. if ((((size_t)input) & 3)) // Input is aligned, let's leverage the speed advantage
  229. {
  230. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  231. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
  232. else
  233. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
  234. }
  235. # endif
  236. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  237. return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
  238. else
  239. return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
  240. #endif
  241. }
  242. //****************************
  243. // Advanced Hash Functions
  244. //****************************
  245. struct XXH_state32_t
  246. {
  247. U64 total_len;
  248. U32 seed;
  249. U32 v1;
  250. U32 v2;
  251. U32 v3;
  252. U32 v4;
  253. int memsize;
  254. char memory[16];
  255. };
  256. int XXH32_sizeofState()
  257. {
  258. XXH_STATIC_ASSERT(XXH32_SIZEOFSTATE >= sizeof(struct XXH_state32_t)); // A compilation error here means XXH32_SIZEOFSTATE is not large enough
  259. return sizeof(struct XXH_state32_t);
  260. }
  261. XXH_errorcode XXH32_resetState(void* state_in, U32 seed)
  262. {
  263. struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
  264. state->seed = seed;
  265. state->v1 = seed + PRIME32_1 + PRIME32_2;
  266. state->v2 = seed + PRIME32_2;
  267. state->v3 = seed + 0;
  268. state->v4 = seed - PRIME32_1;
  269. state->total_len = 0;
  270. state->memsize = 0;
  271. return XXH_OK;
  272. }
  273. void* XXH32_init (U32 seed)
  274. {
  275. void* state = XXH_malloc (sizeof(struct XXH_state32_t));
  276. XXH32_resetState(state, seed);
  277. return state;
  278. }
  279. FORCE_INLINE XXH_errorcode XXH32_update_endian (void* state_in, const void* input, int len, XXH_endianess endian)
  280. {
  281. struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
  282. const BYTE* p = (const BYTE*)input;
  283. const BYTE* const bEnd = p + len;
  284. #ifdef XXH_ACCEPT_NULL_INPUT_POINTER
  285. if (input==NULL) return XXH_ERROR;
  286. #endif
  287. state->total_len += len;
  288. if (state->memsize + len < 16) // fill in tmp buffer
  289. {
  290. XXH_memcpy(state->memory + state->memsize, input, len);
  291. state->memsize += len;
  292. return XXH_OK;
  293. }
  294. if (state->memsize) // some data left from previous update
  295. {
  296. XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize);
  297. {
  298. const U32* p32 = (const U32*)state->memory;
  299. state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++;
  300. state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++;
  301. state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++;
  302. state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++;
  303. }
  304. p += 16-state->memsize;
  305. state->memsize = 0;
  306. }
  307. if (p <= bEnd-16)
  308. {
  309. const BYTE* const limit = bEnd - 16;
  310. U32 v1 = state->v1;
  311. U32 v2 = state->v2;
  312. U32 v3 = state->v3;
  313. U32 v4 = state->v4;
  314. do
  315. {
  316. v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
  317. v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
  318. v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
  319. v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
  320. } while (p<=limit);
  321. state->v1 = v1;
  322. state->v2 = v2;
  323. state->v3 = v3;
  324. state->v4 = v4;
  325. }
  326. if (p < bEnd)
  327. {
  328. XXH_memcpy(state->memory, p, bEnd-p);
  329. state->memsize = (int)(bEnd-p);
  330. }
  331. return XXH_OK;
  332. }
  333. XXH_errorcode XXH32_update (void* state_in, const void* input, int len)
  334. {
  335. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  336. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  337. return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
  338. else
  339. return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
  340. }
  341. FORCE_INLINE U32 XXH32_intermediateDigest_endian (void* state_in, XXH_endianess endian)
  342. {
  343. struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
  344. const BYTE * p = (const BYTE*)state->memory;
  345. BYTE* bEnd = (BYTE*)state->memory + state->memsize;
  346. U32 h32;
  347. if (state->total_len >= 16)
  348. {
  349. h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
  350. }
  351. else
  352. {
  353. h32 = state->seed + PRIME32_5;
  354. }
  355. h32 += (U32) state->total_len;
  356. while (p<=bEnd-4)
  357. {
  358. h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3;
  359. h32 = XXH_rotl32(h32, 17) * PRIME32_4;
  360. p+=4;
  361. }
  362. while (p<bEnd)
  363. {
  364. h32 += (*p) * PRIME32_5;
  365. h32 = XXH_rotl32(h32, 11) * PRIME32_1;
  366. p++;
  367. }
  368. h32 ^= h32 >> 15;
  369. h32 *= PRIME32_2;
  370. h32 ^= h32 >> 13;
  371. h32 *= PRIME32_3;
  372. h32 ^= h32 >> 16;
  373. return h32;
  374. }
  375. U32 XXH32_intermediateDigest (void* state_in)
  376. {
  377. XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
  378. if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
  379. return XXH32_intermediateDigest_endian(state_in, XXH_littleEndian);
  380. else
  381. return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian);
  382. }
  383. U32 XXH32_digest (void* state_in)
  384. {
  385. U32 h32 = XXH32_intermediateDigest(state_in);
  386. XXH_free(state_in);
  387. return h32;
  388. }