murmur3.cc 8.3 KB

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