You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.4KB

  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. //-----------------------------------------------------------------------------
  5. uint32_t rotl32 ( uint32_t x, int8_t r )
  6. {
  7. return (x << r) | (x >> (32 - r));
  8. }
  9. uint32_t fmix32 ( uint32_t h )
  10. {
  11. h ^= h >> 16;
  12. h *= 0x85ebca6b;
  13. h ^= h >> 13;
  14. h *= 0xc2b2ae35;
  15. h ^= h >> 16;
  16. return h;
  17. }
  18. void MurmurHash3_x86_32 ( const void * key, int len,
  19. uint32_t seed, void * out )
  20. {
  21. const uint8_t * data = (const uint8_t*)key;
  22. const int nblocks = len / 4;
  23. uint32_t h1 = seed;
  24. const uint32_t c1 = 0xcc9e2d51;
  25. const uint32_t c2 = 0x1b873593;
  26. //----------
  27. // body
  28. const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
  29. for(int i = -nblocks; i; i++)
  30. {
  31. uint32_t k1 = blocks[i];
  32. k1 *= c1;
  33. k1 = rotl32(k1,15);
  34. k1 *= c2;
  35. h1 ^= k1;
  36. h1 = rotl32(h1,13);
  37. h1 = h1*5+0xe6546b64;
  38. }
  39. //----------
  40. // tail
  41. const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
  42. uint32_t k1 = 0;
  43. switch(len & 3)
  44. {
  45. case 3: k1 ^= tail[2] << 16;
  46. case 2: k1 ^= tail[1] << 8;
  47. case 1: k1 ^= tail[0];
  48. k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
  49. };
  50. //----------
  51. // finalization
  52. h1 ^= len;
  53. h1 = fmix32(h1);
  54. *(uint32_t*)out = h1;
  55. }