MurmurHash3.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //-----------------------------------------------------------------------------
  2. // MurmurHash3 was written by Austin Appleby, and is placed in the public
  3. // domain. The author hereby disclaims copyright to this source code.
  4. // Note - The x86 and x64 versions do _not_ produce the same results, as the
  5. // algorithms are optimized for their respective platforms. You can still
  6. // compile and run any of them on any platform, but your performance with the
  7. // non-native version will be less than optimal.
  8. #include "MurmurHash3.h"
  9. //-----------------------------------------------------------------------------
  10. // Platform-specific functions and macros
  11. // Microsoft Visual Studio
  12. #if defined(_MSC_VER)
  13. #define FORCE_INLINE __forceinline
  14. #include <stdlib.h>
  15. #define ROTL32(x,y) _rotl(x,y)
  16. #define ROTL64(x,y) _rotl64(x,y)
  17. #define BIG_CONSTANT(x) (x)
  18. // Other compilers
  19. #else // defined(_MSC_VER)
  20. #define FORCE_INLINE inline __attribute__((always_inline))
  21. inline uint32_t rotl32 ( uint32_t x, int8_t r )
  22. {
  23. return (x << r) | (x >> (32 - r));
  24. }
  25. inline uint64_t rotl64 ( uint64_t x, int8_t r )
  26. {
  27. return (x << r) | (x >> (64 - r));
  28. }
  29. #define ROTL32(x,y) rotl32(x,y)
  30. #define ROTL64(x,y) rotl64(x,y)
  31. #define BIG_CONSTANT(x) (x##LLU)
  32. #endif // !defined(_MSC_VER)
  33. //-----------------------------------------------------------------------------
  34. // Block read - if your platform needs to do endian-swapping or can only
  35. // handle aligned reads, do the conversion here
  36. FORCE_INLINE uint32_t getblock32 ( const uint32_t * p, int i )
  37. {
  38. return p[i];
  39. }
  40. FORCE_INLINE uint64_t getblock64 ( const uint64_t * p, int i )
  41. {
  42. return p[i];
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Finalization mix - force all bits of a hash block to avalanche
  46. FORCE_INLINE uint32_t fmix32 ( uint32_t h )
  47. {
  48. h ^= h >> 16;
  49. h *= 0x85ebca6b;
  50. h ^= h >> 13;
  51. h *= 0xc2b2ae35;
  52. h ^= h >> 16;
  53. return h;
  54. }
  55. //----------
  56. FORCE_INLINE uint64_t fmix64 ( uint64_t k )
  57. {
  58. k ^= k >> 33;
  59. k *= BIG_CONSTANT(0xff51afd7ed558ccd);
  60. k ^= k >> 33;
  61. k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
  62. k ^= k >> 33;
  63. return k;
  64. }
  65. //-----------------------------------------------------------------------------
  66. void MurmurHash3_x86_32 ( const void * key, int len,
  67. uint32_t seed, void * out )
  68. {
  69. const uint8_t * data = (const uint8_t*)key;
  70. const int nblocks = len / 4;
  71. uint32_t h1 = seed;
  72. const uint32_t c1 = 0xcc9e2d51;
  73. const uint32_t c2 = 0x1b873593;
  74. //----------
  75. // body
  76. const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
  77. for(int i = -nblocks; i; i++)
  78. {
  79. uint32_t k1 = getblock32(blocks,i);
  80. k1 *= c1;
  81. k1 = ROTL32(k1,15);
  82. k1 *= c2;
  83. h1 ^= k1;
  84. h1 = ROTL32(h1,13);
  85. h1 = h1*5+0xe6546b64;
  86. }
  87. //----------
  88. // tail
  89. const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
  90. uint32_t k1 = 0;
  91. switch(len & 3)
  92. {
  93. case 3: k1 ^= tail[2] << 16;
  94. case 2: k1 ^= tail[1] << 8;
  95. case 1: k1 ^= tail[0];
  96. k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  97. };
  98. //----------
  99. // finalization
  100. h1 ^= len;
  101. h1 = fmix32(h1);
  102. *(uint32_t*)out = h1;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void MurmurHash3_x86_128 ( const void * key, const int len,
  106. uint32_t seed, void * out )
  107. {
  108. const uint8_t * data = (const uint8_t*)key;
  109. const int nblocks = len / 16;
  110. uint32_t h1 = seed;
  111. uint32_t h2 = seed;
  112. uint32_t h3 = seed;
  113. uint32_t h4 = seed;
  114. const uint32_t c1 = 0x239b961b;
  115. const uint32_t c2 = 0xab0e9789;
  116. const uint32_t c3 = 0x38b34ae5;
  117. const uint32_t c4 = 0xa1e38b93;
  118. //----------
  119. // body
  120. const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
  121. for(int i = -nblocks; i; i++)
  122. {
  123. uint32_t k1 = getblock32(blocks,i*4+0);
  124. uint32_t k2 = getblock32(blocks,i*4+1);
  125. uint32_t k3 = getblock32(blocks,i*4+2);
  126. uint32_t k4 = getblock32(blocks,i*4+3);
  127. k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  128. h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
  129. k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
  130. h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
  131. k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
  132. h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
  133. k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
  134. h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
  135. }
  136. //----------
  137. // tail
  138. const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  139. uint32_t k1 = 0;
  140. uint32_t k2 = 0;
  141. uint32_t k3 = 0;
  142. uint32_t k4 = 0;
  143. switch(len & 15)
  144. {
  145. case 15: k4 ^= tail[14] << 16;
  146. case 14: k4 ^= tail[13] << 8;
  147. case 13: k4 ^= tail[12] << 0;
  148. k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
  149. case 12: k3 ^= tail[11] << 24;
  150. case 11: k3 ^= tail[10] << 16;
  151. case 10: k3 ^= tail[ 9] << 8;
  152. case 9: k3 ^= tail[ 8] << 0;
  153. k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
  154. case 8: k2 ^= tail[ 7] << 24;
  155. case 7: k2 ^= tail[ 6] << 16;
  156. case 6: k2 ^= tail[ 5] << 8;
  157. case 5: k2 ^= tail[ 4] << 0;
  158. k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
  159. case 4: k1 ^= tail[ 3] << 24;
  160. case 3: k1 ^= tail[ 2] << 16;
  161. case 2: k1 ^= tail[ 1] << 8;
  162. case 1: k1 ^= tail[ 0] << 0;
  163. k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  164. };
  165. //----------
  166. // finalization
  167. h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
  168. h1 += h2; h1 += h3; h1 += h4;
  169. h2 += h1; h3 += h1; h4 += h1;
  170. h1 = fmix32(h1);
  171. h2 = fmix32(h2);
  172. h3 = fmix32(h3);
  173. h4 = fmix32(h4);
  174. h1 += h2; h1 += h3; h1 += h4;
  175. h2 += h1; h3 += h1; h4 += h1;
  176. ((uint32_t*)out)[0] = h1;
  177. ((uint32_t*)out)[1] = h2;
  178. ((uint32_t*)out)[2] = h3;
  179. ((uint32_t*)out)[3] = h4;
  180. }
  181. //-----------------------------------------------------------------------------
  182. void MurmurHash3_x64_128 ( const void * key, const int len,
  183. const uint32_t seed, void * out )
  184. {
  185. const uint8_t * data = (const uint8_t*)key;
  186. const int nblocks = len / 16;
  187. uint64_t h1 = seed;
  188. uint64_t h2 = seed;
  189. const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
  190. const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
  191. //----------
  192. // body
  193. const uint64_t * blocks = (const uint64_t *)(data);
  194. for(int i = 0; i < nblocks; i++)
  195. {
  196. uint64_t k1 = getblock64(blocks,i*2+0);
  197. uint64_t k2 = getblock64(blocks,i*2+1);
  198. k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  199. h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
  200. k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  201. h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
  202. }
  203. //----------
  204. // tail
  205. const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  206. uint64_t k1 = 0;
  207. uint64_t k2 = 0;
  208. switch(len & 15)
  209. {
  210. case 15: k2 ^= ((uint64_t)tail[14]) << 48;
  211. case 14: k2 ^= ((uint64_t)tail[13]) << 40;
  212. case 13: k2 ^= ((uint64_t)tail[12]) << 32;
  213. case 12: k2 ^= ((uint64_t)tail[11]) << 24;
  214. case 11: k2 ^= ((uint64_t)tail[10]) << 16;
  215. case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
  216. case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
  217. k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  218. case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
  219. case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
  220. case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
  221. case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
  222. case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
  223. case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
  224. case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
  225. case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
  226. k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  227. };
  228. //----------
  229. // finalization
  230. h1 ^= len; h2 ^= len;
  231. h1 += h2;
  232. h2 += h1;
  233. h1 = fmix64(h1);
  234. h2 = fmix64(h2);
  235. h1 += h2;
  236. h2 += h1;
  237. ((uint64_t*)out)[0] = h1;
  238. ((uint64_t*)out)[1] = h2;
  239. }
  240. //-----------------------------------------------------------------------------