murmur3.c 7.4 KB

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