farmhashuo.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "common.h"
  2. namespace {
  3. #include "farmhashna.cc"
  4. }
  5. namespace farmhashuo {
  6. #undef Fetch
  7. #define Fetch Fetch64
  8. #undef Rotate
  9. #define Rotate Rotate64
  10. STATIC_INLINE uint64_t H(uint64_t x, uint64_t y, uint64_t mul, int r) {
  11. uint64_t a = (x ^ y) * mul;
  12. a ^= (a >> 47);
  13. uint64_t b = (y ^ a) * mul;
  14. return Rotate(b, r) * mul;
  15. }
  16. uint64_t Hash64WithSeeds(const char *s, size_t len,
  17. uint64_t seed0, uint64_t seed1) {
  18. if (len <= 64) {
  19. return farmhashna::Hash64WithSeeds(s, len, seed0, seed1);
  20. }
  21. // For strings over 64 bytes we loop. Internal state consists of
  22. // 64 bytes: u, v, w, x, y, and z.
  23. uint64_t x = seed0;
  24. uint64_t y = seed1 * k2 + 113;
  25. uint64_t z = farmhashna::ShiftMix(y * k2) * k2;
  26. pair<uint64_t, uint64_t> v = make_pair(seed0, seed1);
  27. pair<uint64_t, uint64_t> w = make_pair(0, 0);
  28. uint64_t u = x - z;
  29. x *= k2;
  30. uint64_t mul = k2 + (u & 0x82);
  31. // Set end so that after the loop we have 1 to 64 bytes left to process.
  32. const char* end = s + ((len - 1) / 64) * 64;
  33. const char* last64 = end + ((len - 1) & 63) - 63;
  34. assert(s + len - 64 == last64);
  35. do {
  36. uint64_t a0 = Fetch(s);
  37. uint64_t a1 = Fetch(s + 8);
  38. uint64_t a2 = Fetch(s + 16);
  39. uint64_t a3 = Fetch(s + 24);
  40. uint64_t a4 = Fetch(s + 32);
  41. uint64_t a5 = Fetch(s + 40);
  42. uint64_t a6 = Fetch(s + 48);
  43. uint64_t a7 = Fetch(s + 56);
  44. x += a0 + a1;
  45. y += a2;
  46. z += a3;
  47. v.first += a4;
  48. v.second += a5 + a1;
  49. w.first += a6;
  50. w.second += a7;
  51. x = Rotate(x, 26);
  52. x *= 9;
  53. y = Rotate(y, 29);
  54. z *= mul;
  55. v.first = Rotate(v.first, 33);
  56. v.second = Rotate(v.second, 30);
  57. w.first ^= x;
  58. w.first *= 9;
  59. z = Rotate(z, 32);
  60. z += w.second;
  61. w.second += z;
  62. z *= 9;
  63. std::swap(u, y);
  64. z += a0 + a6;
  65. v.first += a2;
  66. v.second += a3;
  67. w.first += a4;
  68. w.second += a5 + a6;
  69. x += a1;
  70. y += a7;
  71. y += v.first;
  72. v.first += x - y;
  73. v.second += w.first;
  74. w.first += v.second;
  75. w.second += x - y;
  76. x += w.second;
  77. w.second = Rotate(w.second, 34);
  78. std::swap(u, z);
  79. s += 64;
  80. } while (s != end);
  81. // Make s point to the last 64 bytes of input.
  82. s = last64;
  83. u *= 9;
  84. v.second = Rotate(v.second, 28);
  85. v.first = Rotate(v.first, 20);
  86. w.first += ((len - 1) & 63);
  87. u += y;
  88. y += u;
  89. x = Rotate(y - x + v.first + Fetch(s + 8), 37) * mul;
  90. y = Rotate(y ^ v.second ^ Fetch(s + 48), 42) * mul;
  91. x ^= w.second * 9;
  92. y += v.first + Fetch(s + 40);
  93. z = Rotate(z + w.first, 33) * mul;
  94. v = farmhashna::WeakHashLen32WithSeeds(s, v.second * mul, x + w.first);
  95. w = farmhashna::WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16));
  96. return H(farmhashna::HashLen16(v.first + x, w.first ^ y, mul) + z - u,
  97. H(v.second + y, w.second + z, k2, 30) ^ x,
  98. k2,
  99. 31);
  100. }
  101. uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
  102. return len <= 64 ? farmhashna::Hash64WithSeed(s, len, seed) :
  103. Hash64WithSeeds(s, len, 0, seed);
  104. }
  105. uint64_t Hash64(const char *s, size_t len) {
  106. return len <= 64 ? farmhashna::Hash64(s, len) :
  107. Hash64WithSeeds(s, len, 81, 0);
  108. }
  109. } // namespace farmhashuo