MurmurHash2.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //-----------------------------------------------------------------------------
  2. // MurmurHash2 was written by Austin Appleby, and is placed in the public
  3. // domain. The author hereby disclaims copyright to this source code.
  4. // Note - This code makes a few assumptions about how your machine behaves -
  5. // 1. We can read a 4-byte value from any address without crashing
  6. // 2. sizeof(int) == 4
  7. // And it has a few limitations -
  8. // 1. It will not work incrementally.
  9. // 2. It will not produce the same results on little-endian and big-endian
  10. // machines.
  11. #include "MurmurHash2.h"
  12. //-----------------------------------------------------------------------------
  13. // Platform-specific functions and macros
  14. // Microsoft Visual Studio
  15. #if defined(_MSC_VER)
  16. #define BIG_CONSTANT(x) (x)
  17. // Other compilers
  18. #else // defined(_MSC_VER)
  19. #define BIG_CONSTANT(x) (x##LLU)
  20. #endif // !defined(_MSC_VER)
  21. //-----------------------------------------------------------------------------
  22. uint32_t MurmurHash2 ( const void * key, int len, uint32_t seed )
  23. {
  24. // 'm' and 'r' are mixing constants generated offline.
  25. // They're not really 'magic', they just happen to work well.
  26. const uint32_t m = 0x5bd1e995;
  27. const int r = 24;
  28. // Initialize the hash to a 'random' value
  29. uint32_t h = seed ^ len;
  30. // Mix 4 bytes at a time into the hash
  31. const unsigned char * data = (const unsigned char *)key;
  32. while(len >= 4)
  33. {
  34. uint32_t k = *(uint32_t*)data;
  35. k *= m;
  36. k ^= k >> r;
  37. k *= m;
  38. h *= m;
  39. h ^= k;
  40. data += 4;
  41. len -= 4;
  42. }
  43. // Handle the last few bytes of the input array
  44. switch(len)
  45. {
  46. case 3: h ^= data[2] << 16;
  47. case 2: h ^= data[1] << 8;
  48. case 1: h ^= data[0];
  49. h *= m;
  50. };
  51. // Do a few final mixes of the hash to ensure the last few
  52. // bytes are well-incorporated.
  53. h ^= h >> 13;
  54. h *= m;
  55. h ^= h >> 15;
  56. return h;
  57. }
  58. //-----------------------------------------------------------------------------
  59. // MurmurHash2, 64-bit versions, by Austin Appleby
  60. // The same caveats as 32-bit MurmurHash2 apply here - beware of alignment
  61. // and endian-ness issues if used across multiple platforms.
  62. // 64-bit hash for 64-bit platforms
  63. uint64_t MurmurHash64A ( const void * key, int len, uint64_t seed )
  64. {
  65. const uint64_t m = BIG_CONSTANT(0xc6a4a7935bd1e995);
  66. const int r = 47;
  67. uint64_t h = seed ^ (len * m);
  68. const uint64_t * data = (const uint64_t *)key;
  69. const uint64_t * end = data + (len/8);
  70. while(data != end)
  71. {
  72. uint64_t k = *data++;
  73. k *= m;
  74. k ^= k >> r;
  75. k *= m;
  76. h ^= k;
  77. h *= m;
  78. }
  79. const unsigned char * data2 = (const unsigned char*)data;
  80. switch(len & 7)
  81. {
  82. case 7: h ^= uint64_t(data2[6]) << 48;
  83. case 6: h ^= uint64_t(data2[5]) << 40;
  84. case 5: h ^= uint64_t(data2[4]) << 32;
  85. case 4: h ^= uint64_t(data2[3]) << 24;
  86. case 3: h ^= uint64_t(data2[2]) << 16;
  87. case 2: h ^= uint64_t(data2[1]) << 8;
  88. case 1: h ^= uint64_t(data2[0]);
  89. h *= m;
  90. };
  91. h ^= h >> r;
  92. h *= m;
  93. h ^= h >> r;
  94. return h;
  95. }
  96. // 64-bit hash for 32-bit platforms
  97. uint64_t MurmurHash64B ( const void * key, int len, uint64_t seed )
  98. {
  99. const uint32_t m = 0x5bd1e995;
  100. const int r = 24;
  101. uint32_t h1 = uint32_t(seed) ^ len;
  102. uint32_t h2 = uint32_t(seed >> 32);
  103. const uint32_t * data = (const uint32_t *)key;
  104. while(len >= 8)
  105. {
  106. uint32_t k1 = *data++;
  107. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  108. h1 *= m; h1 ^= k1;
  109. len -= 4;
  110. uint32_t k2 = *data++;
  111. k2 *= m; k2 ^= k2 >> r; k2 *= m;
  112. h2 *= m; h2 ^= k2;
  113. len -= 4;
  114. }
  115. if(len >= 4)
  116. {
  117. uint32_t k1 = *data++;
  118. k1 *= m; k1 ^= k1 >> r; k1 *= m;
  119. h1 *= m; h1 ^= k1;
  120. len -= 4;
  121. }
  122. switch(len)
  123. {
  124. case 3: h2 ^= ((unsigned char*)data)[2] << 16;
  125. case 2: h2 ^= ((unsigned char*)data)[1] << 8;
  126. case 1: h2 ^= ((unsigned char*)data)[0];
  127. h2 *= m;
  128. };
  129. h1 ^= h2 >> 18; h1 *= m;
  130. h2 ^= h1 >> 22; h2 *= m;
  131. h1 ^= h2 >> 17; h1 *= m;
  132. h2 ^= h1 >> 19; h2 *= m;
  133. uint64_t h = h1;
  134. h = (h << 32) | h2;
  135. return h;
  136. }
  137. //-----------------------------------------------------------------------------
  138. // MurmurHash2A, by Austin Appleby
  139. // This is a variant of MurmurHash2 modified to use the Merkle-Damgard
  140. // construction. Bulk speed should be identical to Murmur2, small-key speed
  141. // will be 10%-20% slower due to the added overhead at the end of the hash.
  142. // This variant fixes a minor issue where null keys were more likely to
  143. // collide with each other than expected, and also makes the function
  144. // more amenable to incremental implementations.
  145. #define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  146. uint32_t MurmurHash2A ( const void * key, int len, uint32_t seed )
  147. {
  148. const uint32_t m = 0x5bd1e995;
  149. const int r = 24;
  150. uint32_t l = len;
  151. const unsigned char * data = (const unsigned char *)key;
  152. uint32_t h = seed;
  153. while(len >= 4)
  154. {
  155. uint32_t k = *(uint32_t*)data;
  156. mmix(h,k);
  157. data += 4;
  158. len -= 4;
  159. }
  160. uint32_t t = 0;
  161. switch(len)
  162. {
  163. case 3: t ^= data[2] << 16;
  164. case 2: t ^= data[1] << 8;
  165. case 1: t ^= data[0];
  166. };
  167. mmix(h,t);
  168. mmix(h,l);
  169. h ^= h >> 13;
  170. h *= m;
  171. h ^= h >> 15;
  172. return h;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // CMurmurHash2A, by Austin Appleby
  176. // This is a sample implementation of MurmurHash2A designed to work
  177. // incrementally.
  178. // Usage -
  179. // CMurmurHash2A hasher
  180. // hasher.Begin(seed);
  181. // hasher.Add(data1,size1);
  182. // hasher.Add(data2,size2);
  183. // ...
  184. // hasher.Add(dataN,sizeN);
  185. // uint32_t hash = hasher.End()
  186. class CMurmurHash2A
  187. {
  188. public:
  189. void Begin ( uint32_t seed = 0 )
  190. {
  191. m_hash = seed;
  192. m_tail = 0;
  193. m_count = 0;
  194. m_size = 0;
  195. }
  196. void Add ( const unsigned char * data, int len )
  197. {
  198. m_size += len;
  199. MixTail(data,len);
  200. while(len >= 4)
  201. {
  202. uint32_t k = *(uint32_t*)data;
  203. mmix(m_hash,k);
  204. data += 4;
  205. len -= 4;
  206. }
  207. MixTail(data,len);
  208. }
  209. uint32_t End ( void )
  210. {
  211. mmix(m_hash,m_tail);
  212. mmix(m_hash,m_size);
  213. m_hash ^= m_hash >> 13;
  214. m_hash *= m;
  215. m_hash ^= m_hash >> 15;
  216. return m_hash;
  217. }
  218. private:
  219. static const uint32_t m = 0x5bd1e995;
  220. static const int r = 24;
  221. void MixTail ( const unsigned char * & data, int & len )
  222. {
  223. while( len && ((len<4) || m_count) )
  224. {
  225. m_tail |= (*data++) << (m_count * 8);
  226. m_count++;
  227. len--;
  228. if(m_count == 4)
  229. {
  230. mmix(m_hash,m_tail);
  231. m_tail = 0;
  232. m_count = 0;
  233. }
  234. }
  235. }
  236. uint32_t m_hash;
  237. uint32_t m_tail;
  238. uint32_t m_count;
  239. uint32_t m_size;
  240. };
  241. //-----------------------------------------------------------------------------
  242. // MurmurHashNeutral2, by Austin Appleby
  243. // Same as MurmurHash2, but endian- and alignment-neutral.
  244. // Half the speed though, alas.
  245. uint32_t MurmurHashNeutral2 ( const void * key, int len, uint32_t seed )
  246. {
  247. const uint32_t m = 0x5bd1e995;
  248. const int r = 24;
  249. uint32_t h = seed ^ len;
  250. const unsigned char * data = (const unsigned char *)key;
  251. while(len >= 4)
  252. {
  253. uint32_t k;
  254. k = data[0];
  255. k |= data[1] << 8;
  256. k |= data[2] << 16;
  257. k |= data[3] << 24;
  258. k *= m;
  259. k ^= k >> r;
  260. k *= m;
  261. h *= m;
  262. h ^= k;
  263. data += 4;
  264. len -= 4;
  265. }
  266. switch(len)
  267. {
  268. case 3: h ^= data[2] << 16;
  269. case 2: h ^= data[1] << 8;
  270. case 1: h ^= data[0];
  271. h *= m;
  272. };
  273. h ^= h >> 13;
  274. h *= m;
  275. h ^= h >> 15;
  276. return h;
  277. }
  278. //-----------------------------------------------------------------------------
  279. // MurmurHashAligned2, by Austin Appleby
  280. // Same algorithm as MurmurHash2, but only does aligned reads - should be safer
  281. // on certain platforms.
  282. // Performance will be lower than MurmurHash2
  283. #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  284. uint32_t MurmurHashAligned2 ( const void * key, int len, uint32_t seed )
  285. {
  286. const uint32_t m = 0x5bd1e995;
  287. const int r = 24;
  288. const unsigned char * data = (const unsigned char *)key;
  289. uint32_t h = seed ^ len;
  290. int align = (uint64_t)data & 3;
  291. if(align && (len >= 4))
  292. {
  293. // Pre-load the temp registers
  294. uint32_t t = 0, d = 0;
  295. switch(align)
  296. {
  297. case 1: t |= data[2] << 16;
  298. case 2: t |= data[1] << 8;
  299. case 3: t |= data[0];
  300. }
  301. t <<= (8 * align);
  302. data += 4-align;
  303. len -= 4-align;
  304. int sl = 8 * (4-align);
  305. int sr = 8 * align;
  306. // Mix
  307. while(len >= 4)
  308. {
  309. d = *(uint32_t *)data;
  310. t = (t >> sr) | (d << sl);
  311. uint32_t k = t;
  312. MIX(h,k,m);
  313. t = d;
  314. data += 4;
  315. len -= 4;
  316. }
  317. // Handle leftover data in temp registers
  318. d = 0;
  319. if(len >= align)
  320. {
  321. switch(align)
  322. {
  323. case 3: d |= data[2] << 16;
  324. case 2: d |= data[1] << 8;
  325. case 1: d |= data[0];
  326. }
  327. uint32_t k = (t >> sr) | (d << sl);
  328. MIX(h,k,m);
  329. data += align;
  330. len -= align;
  331. //----------
  332. // Handle tail bytes
  333. switch(len)
  334. {
  335. case 3: h ^= data[2] << 16;
  336. case 2: h ^= data[1] << 8;
  337. case 1: h ^= data[0];
  338. h *= m;
  339. };
  340. }
  341. else
  342. {
  343. switch(len)
  344. {
  345. case 3: d |= data[2] << 16;
  346. case 2: d |= data[1] << 8;
  347. case 1: d |= data[0];
  348. case 0: h ^= (t >> sr) | (d << sl);
  349. h *= m;
  350. }
  351. }
  352. h ^= h >> 13;
  353. h *= m;
  354. h ^= h >> 15;
  355. return h;
  356. }
  357. else
  358. {
  359. while(len >= 4)
  360. {
  361. uint32_t k = *(uint32_t *)data;
  362. MIX(h,k,m);
  363. data += 4;
  364. len -= 4;
  365. }
  366. //----------
  367. // Handle tail bytes
  368. switch(len)
  369. {
  370. case 3: h ^= data[2] << 16;
  371. case 2: h ^= data[1] << 8;
  372. case 1: h ^= data[0];
  373. h *= m;
  374. };
  375. h ^= h >> 13;
  376. h *= m;
  377. h ^= h >> 15;
  378. return h;
  379. }
  380. }
  381. //-----------------------------------------------------------------------------