farmhashcc.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "common.h"
  2. namespace {
  3. #include "farmhashsa.cc"
  4. }
  5. namespace farmhashcc {
  6. // This file provides a 32-bit hash equivalent to CityHash32 (v1.1.1)
  7. // and a 128-bit hash equivalent to CityHash128 (v1.1.1). It also provides
  8. // a seeded 32-bit hash function similar to CityHash32.
  9. #undef Fetch
  10. #define Fetch Fetch32
  11. #undef Rotate
  12. #define Rotate Rotate32
  13. #undef Bswap
  14. #define Bswap Bswap32
  15. STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len) {
  16. uint32_t a = Fetch(s - 4 + (len >> 1));
  17. uint32_t b = Fetch(s + 4);
  18. uint32_t c = Fetch(s + len - 8);
  19. uint32_t d = Fetch(s + (len >> 1));
  20. uint32_t e = Fetch(s);
  21. uint32_t f = Fetch(s + len - 4);
  22. uint32_t h = len;
  23. return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
  24. }
  25. STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len) {
  26. uint32_t b = 0;
  27. uint32_t c = 9;
  28. for (size_t i = 0; i < len; i++) {
  29. signed char v = s[i];
  30. b = b * c1 + v;
  31. c ^= b;
  32. }
  33. return fmix(Mur(b, Mur(len, c)));
  34. }
  35. STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len) {
  36. uint32_t a = len, b = len * 5, c = 9, d = b;
  37. a += Fetch(s);
  38. b += Fetch(s + len - 4);
  39. c += Fetch(s + ((len >> 1) & 4));
  40. return fmix(Mur(c, Mur(b, Mur(a, d))));
  41. }
  42. uint32_t Hash32(const char *s, size_t len) {
  43. if (len <= 24) {
  44. return len <= 12 ?
  45. (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
  46. Hash32Len13to24(s, len);
  47. }
  48. // len > 24
  49. uint32_t h = len, g = c1 * len, f = g;
  50. uint32_t a0 = Rotate(Fetch(s + len - 4) * c1, 17) * c2;
  51. uint32_t a1 = Rotate(Fetch(s + len - 8) * c1, 17) * c2;
  52. uint32_t a2 = Rotate(Fetch(s + len - 16) * c1, 17) * c2;
  53. uint32_t a3 = Rotate(Fetch(s + len - 12) * c1, 17) * c2;
  54. uint32_t a4 = Rotate(Fetch(s + len - 20) * c1, 17) * c2;
  55. h ^= a0;
  56. h = Rotate(h, 19);
  57. h = h * 5 + 0xe6546b64;
  58. h ^= a2;
  59. h = Rotate(h, 19);
  60. h = h * 5 + 0xe6546b64;
  61. g ^= a1;
  62. g = Rotate(g, 19);
  63. g = g * 5 + 0xe6546b64;
  64. g ^= a3;
  65. g = Rotate(g, 19);
  66. g = g * 5 + 0xe6546b64;
  67. f += a4;
  68. f = Rotate(f, 19);
  69. f = f * 5 + 0xe6546b64;
  70. size_t iters = (len - 1) / 20;
  71. do {
  72. uint32_t a0 = Rotate(Fetch(s) * c1, 17) * c2;
  73. uint32_t a1 = Fetch(s + 4);
  74. uint32_t a2 = Rotate(Fetch(s + 8) * c1, 17) * c2;
  75. uint32_t a3 = Rotate(Fetch(s + 12) * c1, 17) * c2;
  76. uint32_t a4 = Fetch(s + 16);
  77. h ^= a0;
  78. h = Rotate(h, 18);
  79. h = h * 5 + 0xe6546b64;
  80. f += a1;
  81. f = Rotate(f, 19);
  82. f = f * c1;
  83. g += a2;
  84. g = Rotate(g, 18);
  85. g = g * 5 + 0xe6546b64;
  86. h ^= a3 + a1;
  87. h = Rotate(h, 19);
  88. h = h * 5 + 0xe6546b64;
  89. g ^= a4;
  90. g = Bswap(g) * 5;
  91. h += a4 * 5;
  92. h = Bswap(h);
  93. f += a0;
  94. PERMUTE3(f, h, g);
  95. s += 20;
  96. } while (--iters != 0);
  97. g = Rotate(g, 11) * c1;
  98. g = Rotate(g, 17) * c1;
  99. f = Rotate(f, 11) * c1;
  100. f = Rotate(f, 17) * c1;
  101. h = Rotate(h + g, 19);
  102. h = h * 5 + 0xe6546b64;
  103. h = Rotate(h, 17) * c1;
  104. h = Rotate(h + f, 19);
  105. h = h * 5 + 0xe6546b64;
  106. h = Rotate(h, 17) * c1;
  107. return h;
  108. }
  109. uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
  110. if (len <= 24) {
  111. if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * c1);
  112. else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed);
  113. else return farmhashmk::Hash32Len0to4(s, len, seed);
  114. }
  115. uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len);
  116. return Mur(Hash32(s + 24, len - 24) + seed, h);
  117. }
  118. #undef Fetch
  119. #define Fetch Fetch64
  120. #undef Rotate
  121. #define Rotate Rotate64
  122. #undef Bswap
  123. #define Bswap Bswap64
  124. STATIC_INLINE uint64_t ShiftMix(uint64_t val) {
  125. return val ^ (val >> 47);
  126. }
  127. STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v) {
  128. return Hash128to64(Uint128(u, v));
  129. }
  130. STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
  131. // Murmur-inspired hashing.
  132. uint64_t a = (u ^ v) * mul;
  133. a ^= (a >> 47);
  134. uint64_t b = (v ^ a) * mul;
  135. b ^= (b >> 47);
  136. b *= mul;
  137. return b;
  138. }
  139. STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len) {
  140. if (len >= 8) {
  141. uint64_t mul = k2 + len * 2;
  142. uint64_t a = Fetch(s) + k2;
  143. uint64_t b = Fetch(s + len - 8);
  144. uint64_t c = Rotate(b, 37) * mul + a;
  145. uint64_t d = (Rotate(a, 25) + b) * mul;
  146. return HashLen16(c, d, mul);
  147. }
  148. if (len >= 4) {
  149. uint64_t mul = k2 + len * 2;
  150. uint64_t a = Fetch32(s);
  151. return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
  152. }
  153. if (len > 0) {
  154. uint8_t a = s[0];
  155. uint8_t b = s[len >> 1];
  156. uint8_t c = s[len - 1];
  157. uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
  158. uint32_t z = len + (static_cast<uint32_t>(c) << 2);
  159. return ShiftMix(y * k2 ^ z * k0) * k2;
  160. }
  161. return k2;
  162. }
  163. // Return a 16-byte hash for 48 bytes. Quick and dirty.
  164. // Callers do best to use "random-looking" values for a and b.
  165. STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
  166. uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
  167. a += w;
  168. b = Rotate(b + a + z, 21);
  169. uint64_t c = a;
  170. a += x;
  171. a += y;
  172. b += Rotate(a, 44);
  173. return make_pair(a + z, b + c);
  174. }
  175. // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
  176. STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
  177. const char* s, uint64_t a, uint64_t b) {
  178. return WeakHashLen32WithSeeds(Fetch(s),
  179. Fetch(s + 8),
  180. Fetch(s + 16),
  181. Fetch(s + 24),
  182. a,
  183. b);
  184. }
  185. // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
  186. // of any length representable in signed long. Based on City and Murmur.
  187. STATIC_INLINE uint128_t CityMurmur(const char *s, size_t len, uint128_t seed) {
  188. uint64_t a = Uint128Low64(seed);
  189. uint64_t b = Uint128High64(seed);
  190. uint64_t c = 0;
  191. uint64_t d = 0;
  192. signed long l = len - 16;
  193. if (l <= 0) { // len <= 16
  194. a = ShiftMix(a * k1) * k1;
  195. c = b * k1 + HashLen0to16(s, len);
  196. d = ShiftMix(a + (len >= 8 ? Fetch(s) : c));
  197. } else { // len > 16
  198. c = HashLen16(Fetch(s + len - 8) + k1, a);
  199. d = HashLen16(b + len, c + Fetch(s + len - 16));
  200. a += d;
  201. do {
  202. a ^= ShiftMix(Fetch(s) * k1) * k1;
  203. a *= k1;
  204. b ^= a;
  205. c ^= ShiftMix(Fetch(s + 8) * k1) * k1;
  206. c *= k1;
  207. d ^= c;
  208. s += 16;
  209. l -= 16;
  210. } while (l > 0);
  211. }
  212. a = HashLen16(a, c);
  213. b = HashLen16(d, b);
  214. return Uint128(a ^ b, HashLen16(b, a));
  215. }
  216. uint128_t CityHash128WithSeed(const char *s, size_t len, uint128_t seed) {
  217. if (len < 128) {
  218. return CityMurmur(s, len, seed);
  219. }
  220. // We expect len >= 128 to be the common case. Keep 56 bytes of state:
  221. // v, w, x, y, and z.
  222. pair<uint64_t, uint64_t> v, w;
  223. uint64_t x = Uint128Low64(seed);
  224. uint64_t y = Uint128High64(seed);
  225. uint64_t z = len * k1;
  226. v.first = Rotate(y ^ k1, 49) * k1 + Fetch(s);
  227. v.second = Rotate(v.first, 42) * k1 + Fetch(s + 8);
  228. w.first = Rotate(y + z, 35) * k1 + x;
  229. w.second = Rotate(x + Fetch(s + 88), 53) * k1;
  230. // This is the same inner loop as CityHash64(), manually unrolled.
  231. do {
  232. x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1;
  233. y = Rotate(y + v.second + Fetch(s + 48), 42) * k1;
  234. x ^= w.second;
  235. y += v.first + Fetch(s + 40);
  236. z = Rotate(z + w.first, 33) * k1;
  237. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  238. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16));
  239. std::swap(z, x);
  240. s += 64;
  241. x = Rotate(x + y + v.first + Fetch(s + 8), 37) * k1;
  242. y = Rotate(y + v.second + Fetch(s + 48), 42) * k1;
  243. x ^= w.second;
  244. y += v.first + Fetch(s + 40);
  245. z = Rotate(z + w.first, 33) * k1;
  246. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  247. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16));
  248. std::swap(z, x);
  249. s += 64;
  250. len -= 128;
  251. } while (LIKELY(len >= 128));
  252. x += Rotate(v.first + z, 49) * k0;
  253. y = y * k0 + Rotate(w.second, 37);
  254. z = z * k0 + Rotate(w.first, 27);
  255. w.first *= 9;
  256. v.first *= k0;
  257. // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
  258. for (size_t tail_done = 0; tail_done < len; ) {
  259. tail_done += 32;
  260. y = Rotate(x + y, 42) * k0 + v.second;
  261. w.first += Fetch(s + len - tail_done + 16);
  262. x = x * k0 + w.first;
  263. z += w.second + Fetch(s + len - tail_done);
  264. w.second += v.first;
  265. v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
  266. v.first *= k0;
  267. }
  268. // At this point our 56 bytes of state should contain more than
  269. // enough information for a strong 128-bit hash. We use two
  270. // different 56-byte-to-8-byte hashes to get a 16-byte final result.
  271. x = HashLen16(x, v.first);
  272. y = HashLen16(y + z, w.first);
  273. return Uint128(HashLen16(x + v.second, w.second) + y,
  274. HashLen16(x + w.second, y + v.second));
  275. }
  276. STATIC_INLINE uint128_t CityHash128(const char *s, size_t len) {
  277. return len >= 16 ?
  278. CityHash128WithSeed(s + 16, len - 16,
  279. Uint128(Fetch(s), Fetch(s + 8) + k0)) :
  280. CityHash128WithSeed(s, len, Uint128(k0, k1));
  281. }
  282. uint128_t Fingerprint128(const char* s, size_t len) {
  283. return CityHash128(s, len);
  284. }
  285. } // namespace farmhashcc