MurmurHash3.c 7.4 KB

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