city.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // This file provides CityHash64() and related functions.
  16. //
  17. // It's probably possible to create even faster hash functions by
  18. // writing a program that systematically explores some of the space of
  19. // possible hash functions, by using SIMD instructions, or by
  20. // compromising on hash quality.
  21. #include "absl/hash/internal/city.h"
  22. #include <string.h> // for memcpy and memset
  23. #include <algorithm>
  24. #include "absl/base/config.h"
  25. #include "absl/base/internal/endian.h"
  26. #include "absl/base/internal/unaligned_access.h"
  27. #include "absl/base/optimization.h"
  28. namespace absl {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace hash_internal {
  31. #ifdef ABSL_IS_BIG_ENDIAN
  32. #define uint32_in_expected_order(x) (absl::gbswap_32(x))
  33. #define uint64_in_expected_order(x) (absl::gbswap_64(x))
  34. #else
  35. #define uint32_in_expected_order(x) (x)
  36. #define uint64_in_expected_order(x) (x)
  37. #endif
  38. static uint64_t Fetch64(const char *p) {
  39. return uint64_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
  40. }
  41. static uint32_t Fetch32(const char *p) {
  42. return uint32_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
  43. }
  44. // Some primes between 2^63 and 2^64 for various uses.
  45. static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
  46. static const uint64_t k1 = 0xb492b66fbe98f273ULL;
  47. static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
  48. // Magic numbers for 32-bit hashing. Copied from Murmur3.
  49. static const uint32_t c1 = 0xcc9e2d51;
  50. static const uint32_t c2 = 0x1b873593;
  51. // A 32-bit to 32-bit integer hash copied from Murmur3.
  52. static uint32_t fmix(uint32_t h) {
  53. h ^= h >> 16;
  54. h *= 0x85ebca6b;
  55. h ^= h >> 13;
  56. h *= 0xc2b2ae35;
  57. h ^= h >> 16;
  58. return h;
  59. }
  60. static uint32_t Rotate32(uint32_t val, int shift) {
  61. // Avoid shifting by 32: doing so yields an undefined result.
  62. return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
  63. }
  64. #undef PERMUTE3
  65. #define PERMUTE3(a, b, c) \
  66. do { \
  67. std::swap(a, b); \
  68. std::swap(a, c); \
  69. } while (0)
  70. static uint32_t Mur(uint32_t a, uint32_t h) {
  71. // Helper from Murmur3 for combining two 32-bit values.
  72. a *= c1;
  73. a = Rotate32(a, 17);
  74. a *= c2;
  75. h ^= a;
  76. h = Rotate32(h, 19);
  77. return h * 5 + 0xe6546b64;
  78. }
  79. static uint32_t Hash32Len13to24(const char *s, size_t len) {
  80. uint32_t a = Fetch32(s - 4 + (len >> 1));
  81. uint32_t b = Fetch32(s + 4);
  82. uint32_t c = Fetch32(s + len - 8);
  83. uint32_t d = Fetch32(s + (len >> 1));
  84. uint32_t e = Fetch32(s);
  85. uint32_t f = Fetch32(s + len - 4);
  86. uint32_t h = static_cast<uint32_t>(len);
  87. return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
  88. }
  89. static uint32_t Hash32Len0to4(const char *s, size_t len) {
  90. uint32_t b = 0;
  91. uint32_t c = 9;
  92. for (size_t i = 0; i < len; i++) {
  93. signed char v = static_cast<signed char>(s[i]);
  94. b = b * c1 + static_cast<uint32_t>(v);
  95. c ^= b;
  96. }
  97. return fmix(Mur(b, Mur(static_cast<uint32_t>(len), c)));
  98. }
  99. static uint32_t Hash32Len5to12(const char *s, size_t len) {
  100. uint32_t a = static_cast<uint32_t>(len), b = a * 5, c = 9, d = b;
  101. a += Fetch32(s);
  102. b += Fetch32(s + len - 4);
  103. c += Fetch32(s + ((len >> 1) & 4));
  104. return fmix(Mur(c, Mur(b, Mur(a, d))));
  105. }
  106. uint32_t CityHash32(const char *s, size_t len) {
  107. if (len <= 24) {
  108. return len <= 12
  109. ? (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len))
  110. : Hash32Len13to24(s, len);
  111. }
  112. // len > 24
  113. uint32_t h = static_cast<uint32_t>(len), g = c1 * h, f = g;
  114. uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
  115. uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
  116. uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
  117. uint32_t a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
  118. uint32_t a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
  119. h ^= a0;
  120. h = Rotate32(h, 19);
  121. h = h * 5 + 0xe6546b64;
  122. h ^= a2;
  123. h = Rotate32(h, 19);
  124. h = h * 5 + 0xe6546b64;
  125. g ^= a1;
  126. g = Rotate32(g, 19);
  127. g = g * 5 + 0xe6546b64;
  128. g ^= a3;
  129. g = Rotate32(g, 19);
  130. g = g * 5 + 0xe6546b64;
  131. f += a4;
  132. f = Rotate32(f, 19);
  133. f = f * 5 + 0xe6546b64;
  134. size_t iters = (len - 1) / 20;
  135. do {
  136. uint32_t b0 = Rotate32(Fetch32(s) * c1, 17) * c2;
  137. uint32_t b1 = Fetch32(s + 4);
  138. uint32_t b2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
  139. uint32_t b3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
  140. uint32_t b4 = Fetch32(s + 16);
  141. h ^= b0;
  142. h = Rotate32(h, 18);
  143. h = h * 5 + 0xe6546b64;
  144. f += b1;
  145. f = Rotate32(f, 19);
  146. f = f * c1;
  147. g += b2;
  148. g = Rotate32(g, 18);
  149. g = g * 5 + 0xe6546b64;
  150. h ^= b3 + b1;
  151. h = Rotate32(h, 19);
  152. h = h * 5 + 0xe6546b64;
  153. g ^= b4;
  154. g = absl::gbswap_32(g) * 5;
  155. h += b4 * 5;
  156. h = absl::gbswap_32(h);
  157. f += b0;
  158. PERMUTE3(f, h, g);
  159. s += 20;
  160. } while (--iters != 0);
  161. g = Rotate32(g, 11) * c1;
  162. g = Rotate32(g, 17) * c1;
  163. f = Rotate32(f, 11) * c1;
  164. f = Rotate32(f, 17) * c1;
  165. h = Rotate32(h + g, 19);
  166. h = h * 5 + 0xe6546b64;
  167. h = Rotate32(h, 17) * c1;
  168. h = Rotate32(h + f, 19);
  169. h = h * 5 + 0xe6546b64;
  170. h = Rotate32(h, 17) * c1;
  171. return h;
  172. }
  173. // Bitwise right rotate. Normally this will compile to a single
  174. // instruction, especially if the shift is a manifest constant.
  175. static uint64_t Rotate(uint64_t val, int shift) {
  176. // Avoid shifting by 64: doing so yields an undefined result.
  177. return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
  178. }
  179. static uint64_t ShiftMix(uint64_t val) { return val ^ (val >> 47); }
  180. static uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
  181. // Murmur-inspired hashing.
  182. uint64_t a = (u ^ v) * mul;
  183. a ^= (a >> 47);
  184. uint64_t b = (v ^ a) * mul;
  185. b ^= (b >> 47);
  186. b *= mul;
  187. return b;
  188. }
  189. static uint64_t HashLen16(uint64_t u, uint64_t v) {
  190. const uint64_t kMul = 0x9ddfea08eb382d69ULL;
  191. return HashLen16(u, v, kMul);
  192. }
  193. static uint64_t HashLen0to16(const char *s, size_t len) {
  194. if (len >= 8) {
  195. uint64_t mul = k2 + len * 2;
  196. uint64_t a = Fetch64(s) + k2;
  197. uint64_t b = Fetch64(s + len - 8);
  198. uint64_t c = Rotate(b, 37) * mul + a;
  199. uint64_t d = (Rotate(a, 25) + b) * mul;
  200. return HashLen16(c, d, mul);
  201. }
  202. if (len >= 4) {
  203. uint64_t mul = k2 + len * 2;
  204. uint64_t a = Fetch32(s);
  205. return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
  206. }
  207. if (len > 0) {
  208. uint8_t a = static_cast<uint8_t>(s[0]);
  209. uint8_t b = static_cast<uint8_t>(s[len >> 1]);
  210. uint8_t c = static_cast<uint8_t>(s[len - 1]);
  211. uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
  212. uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
  213. return ShiftMix(y * k2 ^ z * k0) * k2;
  214. }
  215. return k2;
  216. }
  217. // This probably works well for 16-byte strings as well, but it may be overkill
  218. // in that case.
  219. static uint64_t HashLen17to32(const char *s, size_t len) {
  220. uint64_t mul = k2 + len * 2;
  221. uint64_t a = Fetch64(s) * k1;
  222. uint64_t b = Fetch64(s + 8);
  223. uint64_t c = Fetch64(s + len - 8) * mul;
  224. uint64_t d = Fetch64(s + len - 16) * k2;
  225. return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
  226. a + Rotate(b + k2, 18) + c, mul);
  227. }
  228. // Return a 16-byte hash for 48 bytes. Quick and dirty.
  229. // Callers do best to use "random-looking" values for a and b.
  230. static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
  231. uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
  232. a += w;
  233. b = Rotate(b + a + z, 21);
  234. uint64_t c = a;
  235. a += x;
  236. a += y;
  237. b += Rotate(a, 44);
  238. return std::make_pair(a + z, b + c);
  239. }
  240. // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
  241. static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char *s,
  242. uint64_t a,
  243. uint64_t b) {
  244. return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16),
  245. Fetch64(s + 24), a, b);
  246. }
  247. // Return an 8-byte hash for 33 to 64 bytes.
  248. static uint64_t HashLen33to64(const char *s, size_t len) {
  249. uint64_t mul = k2 + len * 2;
  250. uint64_t a = Fetch64(s) * k2;
  251. uint64_t b = Fetch64(s + 8);
  252. uint64_t c = Fetch64(s + len - 24);
  253. uint64_t d = Fetch64(s + len - 32);
  254. uint64_t e = Fetch64(s + 16) * k2;
  255. uint64_t f = Fetch64(s + 24) * 9;
  256. uint64_t g = Fetch64(s + len - 8);
  257. uint64_t h = Fetch64(s + len - 16) * mul;
  258. uint64_t u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
  259. uint64_t v = ((a + g) ^ d) + f + 1;
  260. uint64_t w = absl::gbswap_64((u + v) * mul) + h;
  261. uint64_t x = Rotate(e + f, 42) + c;
  262. uint64_t y = (absl::gbswap_64((v + w) * mul) + g) * mul;
  263. uint64_t z = e + f + c;
  264. a = absl::gbswap_64((x + z) * mul + y) + b;
  265. b = ShiftMix((z + a) * mul + d + h) * mul;
  266. return b + x;
  267. }
  268. uint64_t CityHash64(const char *s, size_t len) {
  269. if (len <= 32) {
  270. if (len <= 16) {
  271. return HashLen0to16(s, len);
  272. } else {
  273. return HashLen17to32(s, len);
  274. }
  275. } else if (len <= 64) {
  276. return HashLen33to64(s, len);
  277. }
  278. // For strings over 64 bytes we hash the end first, and then as we
  279. // loop we keep 56 bytes of state: v, w, x, y, and z.
  280. uint64_t x = Fetch64(s + len - 40);
  281. uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
  282. uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
  283. std::pair<uint64_t, uint64_t> v =
  284. WeakHashLen32WithSeeds(s + len - 64, len, z);
  285. std::pair<uint64_t, uint64_t> w =
  286. WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
  287. x = x * k1 + Fetch64(s);
  288. // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
  289. len = (len - 1) & ~static_cast<size_t>(63);
  290. do {
  291. x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
  292. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  293. x ^= w.second;
  294. y += v.first + Fetch64(s + 40);
  295. z = Rotate(z + w.first, 33) * k1;
  296. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  297. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
  298. std::swap(z, x);
  299. s += 64;
  300. len -= 64;
  301. } while (len != 0);
  302. return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
  303. HashLen16(v.second, w.second) + x);
  304. }
  305. uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) {
  306. return CityHash64WithSeeds(s, len, k2, seed);
  307. }
  308. uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
  309. uint64_t seed1) {
  310. return HashLen16(CityHash64(s, len) - seed0, seed1);
  311. }
  312. } // namespace hash_internal
  313. ABSL_NAMESPACE_END
  314. } // namespace absl