MurmurHash3.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. int i;
  65. //----------
  66. // body
  67. const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
  68. for(i = -nblocks; i; i++)
  69. {
  70. uint32_t k1 = getblock32(blocks,i);
  71. k1 *= c1;
  72. k1 = ROTL32(k1,15);
  73. k1 *= c2;
  74. h1 ^= k1;
  75. h1 = ROTL32(h1,13);
  76. h1 = h1*5+0xe6546b64;
  77. }
  78. //----------
  79. // tail
  80. const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
  81. uint32_t k1 = 0;
  82. switch(len & 3)
  83. {
  84. case 3: k1 ^= tail[2] << 16;
  85. case 2: k1 ^= tail[1] << 8;
  86. case 1: k1 ^= tail[0];
  87. k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  88. };
  89. //----------
  90. // finalization
  91. h1 ^= len;
  92. h1 = fmix32(h1);
  93. *(uint32_t*)out = h1;
  94. }
  95. //-----------------------------------------------------------------------------
  96. void MurmurHash3_x86_128 ( const void * key, const int len,
  97. uint32_t seed, void * out )
  98. {
  99. const uint8_t * data = (const uint8_t*)key;
  100. const int nblocks = len / 16;
  101. uint32_t h1 = seed;
  102. uint32_t h2 = seed;
  103. uint32_t h3 = seed;
  104. uint32_t h4 = seed;
  105. const uint32_t c1 = 0x239b961b;
  106. const uint32_t c2 = 0xab0e9789;
  107. const uint32_t c3 = 0x38b34ae5;
  108. const uint32_t c4 = 0xa1e38b93;
  109. int i;
  110. //----------
  111. // body
  112. const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
  113. for(i = -nblocks; i; i++)
  114. {
  115. uint32_t k1 = getblock32(blocks,i*4+0);
  116. uint32_t k2 = getblock32(blocks,i*4+1);
  117. uint32_t k3 = getblock32(blocks,i*4+2);
  118. uint32_t k4 = getblock32(blocks,i*4+3);
  119. k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  120. h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
  121. k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
  122. h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
  123. k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
  124. h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
  125. k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
  126. h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
  127. }
  128. //----------
  129. // tail
  130. const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  131. uint32_t k1 = 0;
  132. uint32_t k2 = 0;
  133. uint32_t k3 = 0;
  134. uint32_t k4 = 0;
  135. switch(len & 15)
  136. {
  137. case 15: k4 ^= tail[14] << 16;
  138. case 14: k4 ^= tail[13] << 8;
  139. case 13: k4 ^= tail[12] << 0;
  140. k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
  141. case 12: k3 ^= tail[11] << 24;
  142. case 11: k3 ^= tail[10] << 16;
  143. case 10: k3 ^= tail[ 9] << 8;
  144. case 9: k3 ^= tail[ 8] << 0;
  145. k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
  146. case 8: k2 ^= tail[ 7] << 24;
  147. case 7: k2 ^= tail[ 6] << 16;
  148. case 6: k2 ^= tail[ 5] << 8;
  149. case 5: k2 ^= tail[ 4] << 0;
  150. k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
  151. case 4: k1 ^= tail[ 3] << 24;
  152. case 3: k1 ^= tail[ 2] << 16;
  153. case 2: k1 ^= tail[ 1] << 8;
  154. case 1: k1 ^= tail[ 0] << 0;
  155. k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
  156. };
  157. //----------
  158. // finalization
  159. h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
  160. h1 += h2; h1 += h3; h1 += h4;
  161. h2 += h1; h3 += h1; h4 += h1;
  162. h1 = fmix32(h1);
  163. h2 = fmix32(h2);
  164. h3 = fmix32(h3);
  165. h4 = fmix32(h4);
  166. h1 += h2; h1 += h3; h1 += h4;
  167. h2 += h1; h3 += h1; h4 += h1;
  168. ((uint32_t*)out)[0] = h1;
  169. ((uint32_t*)out)[1] = h2;
  170. ((uint32_t*)out)[2] = h3;
  171. ((uint32_t*)out)[3] = h4;
  172. }
  173. //-----------------------------------------------------------------------------
  174. void MurmurHash3_x64_128 ( const void * key, const int len,
  175. const uint32_t seed, void * out )
  176. {
  177. const uint8_t * data = (const uint8_t*)key;
  178. const int nblocks = len / 16;
  179. uint64_t h1 = seed;
  180. uint64_t h2 = seed;
  181. const uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
  182. const uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
  183. int i;
  184. //----------
  185. // body
  186. const uint64_t * blocks = (const uint64_t *)(data);
  187. for(i = 0; i < nblocks; i++)
  188. {
  189. uint64_t k1 = getblock64(blocks,i*2+0);
  190. uint64_t k2 = getblock64(blocks,i*2+1);
  191. k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  192. h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
  193. k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  194. h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
  195. }
  196. //----------
  197. // tail
  198. const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
  199. uint64_t k1 = 0;
  200. uint64_t k2 = 0;
  201. switch(len & 15)
  202. {
  203. case 15: k2 ^= ((uint64_t)tail[14]) << 48;
  204. case 14: k2 ^= ((uint64_t)tail[13]) << 40;
  205. case 13: k2 ^= ((uint64_t)tail[12]) << 32;
  206. case 12: k2 ^= ((uint64_t)tail[11]) << 24;
  207. case 11: k2 ^= ((uint64_t)tail[10]) << 16;
  208. case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
  209. case 9: k2 ^= ((uint64_t)tail[ 8]) << 0;
  210. k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
  211. case 8: k1 ^= ((uint64_t)tail[ 7]) << 56;
  212. case 7: k1 ^= ((uint64_t)tail[ 6]) << 48;
  213. case 6: k1 ^= ((uint64_t)tail[ 5]) << 40;
  214. case 5: k1 ^= ((uint64_t)tail[ 4]) << 32;
  215. case 4: k1 ^= ((uint64_t)tail[ 3]) << 24;
  216. case 3: k1 ^= ((uint64_t)tail[ 2]) << 16;
  217. case 2: k1 ^= ((uint64_t)tail[ 1]) << 8;
  218. case 1: k1 ^= ((uint64_t)tail[ 0]) << 0;
  219. k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
  220. };
  221. //----------
  222. // finalization
  223. h1 ^= len; h2 ^= len;
  224. h1 += h2;
  225. h2 += h1;
  226. h1 = fmix64(h1);
  227. h2 = fmix64(h2);
  228. h1 += h2;
  229. h2 += h1;
  230. ((uint64_t*)out)[0] = h1;
  231. ((uint64_t*)out)[1] = h2;
  232. }
  233. //-----------------------------------------------------------------------------