murmur.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "murmur.h"
  2. #include <util/system/unaligned_mem.h>
  3. namespace NMurmurPrivate {
  4. //-----------------------------------------------------------------------------
  5. // MurmurHash2, by Austin Appleby
  6. // Note - This code makes a few assumptions about how your machine behaves -
  7. // 1. We can read a 4-byte value from any address without crashing
  8. // 2. sizeof(int) == 4
  9. // And it has a few limitations -
  10. // 1. It will not work incrementally.
  11. // 2. It will not produce the same results on little-endian and big-endian
  12. // machines.
  13. Y_NO_INLINE ui32 MurmurHash32(const void* key, size_t len, ui32 seed) noexcept {
  14. const ui32 m = 0x5bd1e995;
  15. const int r = 24;
  16. ui32 h = ui32(seed ^ len);
  17. TUnalignedMemoryIterator<ui32> iter(key, len);
  18. while (!iter.AtEnd()) {
  19. ui32 k = iter.Next();
  20. k *= m;
  21. k ^= k >> r;
  22. k *= m;
  23. h *= m;
  24. h ^= k;
  25. }
  26. const unsigned char* data = iter.Last();
  27. switch (iter.Left()) {
  28. case 3:
  29. h ^= data[2] << 16;
  30. [[fallthrough]];
  31. case 2:
  32. h ^= data[1] << 8;
  33. [[fallthrough]];
  34. case 1:
  35. h ^= data[0];
  36. h *= m;
  37. break;
  38. }
  39. h ^= h >> 13;
  40. h *= m;
  41. h ^= h >> 15;
  42. return h;
  43. }
  44. //-----------------------------------------------------------------------------
  45. // MurmurHash2, 64-bit versions, by Austin Appleby
  46. // The same caveats as 32-bit MurmurHash2 apply here - beware of alignment
  47. // and endian-ness issues if used across multiple platforms.
  48. // 64-bit hash for 64-bit platforms
  49. Y_NO_INLINE ui64 MurmurHash64(const void* key, size_t len, ui64 seed) noexcept {
  50. const ui64 m = ULL(0xc6a4a7935bd1e995);
  51. const int r = 47;
  52. ui64 h = seed ^ (len * m);
  53. TUnalignedMemoryIterator<ui64> iter(key, len);
  54. while (!iter.AtEnd()) {
  55. ui64 k = iter.Next();
  56. k *= m;
  57. k ^= k >> r;
  58. k *= m;
  59. h ^= k;
  60. h *= m;
  61. }
  62. const unsigned char* data2 = iter.Last();
  63. switch (iter.Left()) {
  64. case 7:
  65. h ^= ui64(data2[6]) << 48;
  66. [[fallthrough]];
  67. case 6:
  68. h ^= ui64(data2[5]) << 40;
  69. [[fallthrough]];
  70. case 5:
  71. h ^= ui64(data2[4]) << 32;
  72. [[fallthrough]];
  73. case 4:
  74. h ^= ui64(data2[3]) << 24;
  75. [[fallthrough]];
  76. case 3:
  77. h ^= ui64(data2[2]) << 16;
  78. [[fallthrough]];
  79. case 2:
  80. h ^= ui64(data2[1]) << 8;
  81. [[fallthrough]];
  82. case 1:
  83. h ^= ui64(data2[0]);
  84. h *= m;
  85. break;
  86. }
  87. h ^= h >> r;
  88. h *= m;
  89. h ^= h >> r;
  90. return h;
  91. }
  92. }
  93. template size_t MurmurHash<size_t>(const void* buf, size_t len) noexcept;