farmhashxo.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "common.h"
  2. namespace {
  3. #include "farmhashuo.cc"
  4. }
  5. namespace farmhashxo {
  6. #undef Fetch
  7. #define Fetch Fetch64
  8. #undef Rotate
  9. #define Rotate Rotate64
  10. STATIC_INLINE uint64_t H32(const char *s, size_t len, uint64_t mul,
  11. uint64_t seed0 = 0, uint64_t seed1 = 0) {
  12. uint64_t a = Fetch(s) * k1;
  13. uint64_t b = Fetch(s + 8);
  14. uint64_t c = Fetch(s + len - 8) * mul;
  15. uint64_t d = Fetch(s + len - 16) * k2;
  16. uint64_t u = Rotate(a + b, 43) + Rotate(c, 30) + d + seed0;
  17. uint64_t v = a + Rotate(b + k2, 18) + c + seed1;
  18. a = farmhashna::ShiftMix((u ^ v) * mul);
  19. b = farmhashna::ShiftMix((v ^ a) * mul);
  20. return b;
  21. }
  22. // Return an 8-byte hash for 33 to 64 bytes.
  23. STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len) {
  24. uint64_t mul0 = k2 - 30;
  25. uint64_t mul1 = k2 - 30 + 2 * len;
  26. uint64_t h0 = H32(s, 32, mul0);
  27. uint64_t h1 = H32(s + len - 32, 32, mul1);
  28. return ((h1 * mul1) + h0) * mul1;
  29. }
  30. // Return an 8-byte hash for 65 to 96 bytes.
  31. STATIC_INLINE uint64_t HashLen65to96(const char *s, size_t len) {
  32. uint64_t mul0 = k2 - 114;
  33. uint64_t mul1 = k2 - 114 + 2 * len;
  34. uint64_t h0 = H32(s, 32, mul0);
  35. uint64_t h1 = H32(s + 32, 32, mul1);
  36. uint64_t h2 = H32(s + len - 32, 32, mul1, h0, h1);
  37. return (h2 * 9 + (h0 >> 17) + (h1 >> 21)) * mul1;
  38. }
  39. uint64_t Hash64(const char *s, size_t len) {
  40. if (len <= 32) {
  41. if (len <= 16) {
  42. return farmhashna::HashLen0to16(s, len);
  43. } else {
  44. return farmhashna::HashLen17to32(s, len);
  45. }
  46. } else if (len <= 64) {
  47. return HashLen33to64(s, len);
  48. } else if (len <= 96) {
  49. return HashLen65to96(s, len);
  50. } else if (len <= 256) {
  51. return farmhashna::Hash64(s, len);
  52. } else {
  53. return farmhashuo::Hash64(s, len);
  54. }
  55. }
  56. uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
  57. return farmhashuo::Hash64WithSeeds(s, len, seed0, seed1);
  58. }
  59. uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
  60. return farmhashuo::Hash64WithSeed(s, len, seed);
  61. }
  62. } // namespace farmhashxo