city.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Copyright (c) 2011 Google, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //
  21. // CityHash, by Geoff Pike and Jyrki Alakuijala
  22. //
  23. // This file provides CityHash64() and related functions.
  24. //
  25. // It's probably possible to create even faster hash functions by
  26. // writing a program that systematically explores some of the space of
  27. // possible hash functions, by using SIMD instructions, or by
  28. // compromising on hash quality.
  29. #include "config.h"
  30. #include <city.h>
  31. #include <algorithm>
  32. #include <string.h> // for memcpy and memset
  33. #ifdef __SSE4_2__
  34. #include <citycrc.h>
  35. #include <nmmintrin.h>
  36. #endif
  37. using namespace std;
  38. #if !defined(WORDS_BIGENDIAN)
  39. #define uint32_in_expected_order(x) (x)
  40. #define uint64_in_expected_order(x) (x)
  41. #else
  42. #ifdef _MSC_VER
  43. #include <stdlib.h>
  44. #define bswap_32(x) _byteswap_ulong(x)
  45. #define bswap_64(x) _byteswap_uint64(x)
  46. #elif defined(__APPLE__)
  47. // Mac OS X / Darwin features
  48. #include <libkern/OSByteOrder.h>
  49. #define bswap_32(x) OSSwapInt32(x)
  50. #define bswap_64(x) OSSwapInt64(x)
  51. #else
  52. #include <byteswap.h>
  53. #endif
  54. #define uint32_in_expected_order(x) (bswap_32(x))
  55. #define uint64_in_expected_order(x) (bswap_64(x))
  56. #endif // WORDS_BIGENDIAN
  57. #if !defined(LIKELY)
  58. #if HAVE_BUILTIN_EXPECT
  59. #define LIKELY(x) (__builtin_expect(!!(x), 1))
  60. #else
  61. #define LIKELY(x) (x)
  62. #endif
  63. #endif
  64. namespace CityHash_v1_0_2
  65. {
  66. static uint64 UNALIGNED_LOAD64(const char *p) {
  67. uint64 result;
  68. memcpy(&result, p, sizeof(result));
  69. return result;
  70. }
  71. static uint32 UNALIGNED_LOAD32(const char *p) {
  72. uint32 result;
  73. memcpy(&result, p, sizeof(result));
  74. return result;
  75. }
  76. static uint64 Fetch64(const char *p) {
  77. return uint64_in_expected_order(UNALIGNED_LOAD64(p));
  78. }
  79. static uint32 Fetch32(const char *p) {
  80. return uint32_in_expected_order(UNALIGNED_LOAD32(p));
  81. }
  82. // Some primes between 2^63 and 2^64 for various uses.
  83. static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
  84. static const uint64 k1 = 0xb492b66fbe98f273ULL;
  85. static const uint64 k2 = 0x9ae16a3b2f90404fULL;
  86. static const uint64 k3 = 0xc949d7c7509e6557ULL;
  87. // Bitwise right rotate. Normally this will compile to a single
  88. // instruction, especially if the shift is a manifest constant.
  89. static uint64 Rotate(uint64 val, int shift) {
  90. // Avoid shifting by 64: doing so yields an undefined result.
  91. return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
  92. }
  93. // Equivalent to Rotate(), but requires the second arg to be non-zero.
  94. // On x86-64, and probably others, it's possible for this to compile
  95. // to a single instruction if both args are already in registers.
  96. static uint64 RotateByAtLeast1(uint64 val, int shift) {
  97. return (val >> shift) | (val << (64 - shift));
  98. }
  99. static uint64 ShiftMix(uint64 val) {
  100. return val ^ (val >> 47);
  101. }
  102. static uint64 HashLen16(uint64 u, uint64 v) {
  103. return Hash128to64(uint128(u, v));
  104. }
  105. static uint64 HashLen0to16(const char *s, size_t len) {
  106. if (len > 8) {
  107. uint64 a = Fetch64(s);
  108. uint64 b = Fetch64(s + len - 8);
  109. return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b;
  110. }
  111. if (len >= 4) {
  112. uint64 a = Fetch32(s);
  113. return HashLen16(len + (a << 3), Fetch32(s + len - 4));
  114. }
  115. if (len > 0) {
  116. uint8 a = s[0];
  117. uint8 b = s[len >> 1];
  118. uint8 c = s[len - 1];
  119. uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
  120. uint32 z = len + (static_cast<uint32>(c) << 2);
  121. return ShiftMix(y * k2 ^ z * k3) * k2;
  122. }
  123. return k2;
  124. }
  125. // This probably works well for 16-byte strings as well, but it may be overkill
  126. // in that case.
  127. static uint64 HashLen17to32(const char *s, size_t len) {
  128. uint64 a = Fetch64(s) * k1;
  129. uint64 b = Fetch64(s + 8);
  130. uint64 c = Fetch64(s + len - 8) * k2;
  131. uint64 d = Fetch64(s + len - 16) * k0;
  132. return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d,
  133. a + Rotate(b ^ k3, 20) - c + len);
  134. }
  135. // Return a 16-byte hash for 48 bytes. Quick and dirty.
  136. // Callers do best to use "random-looking" values for a and b.
  137. static pair<uint64, uint64> WeakHashLen32WithSeeds(
  138. uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) {
  139. a += w;
  140. b = Rotate(b + a + z, 21);
  141. uint64 c = a;
  142. a += x;
  143. a += y;
  144. b += Rotate(a, 44);
  145. return make_pair(a + z, b + c);
  146. }
  147. // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
  148. static pair<uint64, uint64> WeakHashLen32WithSeeds(
  149. const char* s, uint64 a, uint64 b) {
  150. return WeakHashLen32WithSeeds(Fetch64(s),
  151. Fetch64(s + 8),
  152. Fetch64(s + 16),
  153. Fetch64(s + 24),
  154. a,
  155. b);
  156. }
  157. // Return an 8-byte hash for 33 to 64 bytes.
  158. static uint64 HashLen33to64(const char *s, size_t len) {
  159. uint64 z = Fetch64(s + 24);
  160. uint64 a = Fetch64(s) + (len + Fetch64(s + len - 16)) * k0;
  161. uint64 b = Rotate(a + z, 52);
  162. uint64 c = Rotate(a, 37);
  163. a += Fetch64(s + 8);
  164. c += Rotate(a, 7);
  165. a += Fetch64(s + 16);
  166. uint64 vf = a + z;
  167. uint64 vs = b + Rotate(a, 31) + c;
  168. a = Fetch64(s + 16) + Fetch64(s + len - 32);
  169. z = Fetch64(s + len - 8);
  170. b = Rotate(a + z, 52);
  171. c = Rotate(a, 37);
  172. a += Fetch64(s + len - 24);
  173. c += Rotate(a, 7);
  174. a += Fetch64(s + len - 16);
  175. uint64 wf = a + z;
  176. uint64 ws = b + Rotate(a, 31) + c;
  177. uint64 r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0);
  178. return ShiftMix(r * k0 + vs) * k2;
  179. }
  180. uint64 CityHash64(const char *s, size_t len) {
  181. if (len <= 32) {
  182. if (len <= 16) {
  183. return HashLen0to16(s, len);
  184. } else {
  185. return HashLen17to32(s, len);
  186. }
  187. } else if (len <= 64) {
  188. return HashLen33to64(s, len);
  189. }
  190. // For strings over 64 bytes we hash the end first, and then as we
  191. // loop we keep 56 bytes of state: v, w, x, y, and z.
  192. uint64 x = Fetch64(s);
  193. uint64 y = Fetch64(s + len - 16) ^ k1;
  194. uint64 z = Fetch64(s + len - 56) ^ k0;
  195. pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, y);
  196. pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, len * k1, k0);
  197. z += ShiftMix(v.second) * k1;
  198. x = Rotate(z + x, 39) * k1;
  199. y = Rotate(y, 33) * k1;
  200. // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
  201. len = (len - 1) & ~static_cast<size_t>(63);
  202. do {
  203. x = Rotate(x + y + v.first + Fetch64(s + 16), 37) * k1;
  204. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  205. x ^= w.second;
  206. y ^= v.first;
  207. z = Rotate(z ^ w.first, 33);
  208. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  209. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y);
  210. std::swap(z, x);
  211. s += 64;
  212. len -= 64;
  213. } while (len != 0);
  214. return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
  215. HashLen16(v.second, w.second) + x);
  216. }
  217. uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) {
  218. return CityHash64WithSeeds(s, len, k2, seed);
  219. }
  220. uint64 CityHash64WithSeeds(const char *s, size_t len,
  221. uint64 seed0, uint64 seed1) {
  222. return HashLen16(CityHash64(s, len) - seed0, seed1);
  223. }
  224. // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
  225. // of any length representable in ssize_t. Based on City and Murmur.
  226. static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
  227. uint64 a = Uint128Low64(seed);
  228. uint64 b = Uint128High64(seed);
  229. uint64 c = 0;
  230. uint64 d = 0;
  231. ssize_t l = len - 16;
  232. if (l <= 0) { // len <= 16
  233. a = ShiftMix(a * k1) * k1;
  234. c = b * k1 + HashLen0to16(s, len);
  235. d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
  236. } else { // len > 16
  237. c = HashLen16(Fetch64(s + len - 8) + k1, a);
  238. d = HashLen16(b + len, c + Fetch64(s + len - 16));
  239. a += d;
  240. do {
  241. a ^= ShiftMix(Fetch64(s) * k1) * k1;
  242. a *= k1;
  243. b ^= a;
  244. c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
  245. c *= k1;
  246. d ^= c;
  247. s += 16;
  248. l -= 16;
  249. } while (l > 0);
  250. }
  251. a = HashLen16(a, c);
  252. b = HashLen16(d, b);
  253. return uint128(a ^ b, HashLen16(b, a));
  254. }
  255. uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
  256. if (len < 128) {
  257. return CityMurmur(s, len, seed);
  258. }
  259. // We expect len >= 128 to be the common case. Keep 56 bytes of state:
  260. // v, w, x, y, and z.
  261. pair<uint64, uint64> v, w;
  262. uint64 x = Uint128Low64(seed);
  263. uint64 y = Uint128High64(seed);
  264. uint64 z = len * k1;
  265. v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
  266. v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
  267. w.first = Rotate(y + z, 35) * k1 + x;
  268. w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
  269. // This is the same inner loop as CityHash64(), manually unrolled.
  270. do {
  271. x = Rotate(x + y + v.first + Fetch64(s + 16), 37) * k1;
  272. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  273. x ^= w.second;
  274. y ^= v.first;
  275. z = Rotate(z ^ w.first, 33);
  276. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  277. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y);
  278. std::swap(z, x);
  279. s += 64;
  280. x = Rotate(x + y + v.first + Fetch64(s + 16), 37) * k1;
  281. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  282. x ^= w.second;
  283. y ^= v.first;
  284. z = Rotate(z ^ w.first, 33);
  285. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  286. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y);
  287. std::swap(z, x);
  288. s += 64;
  289. len -= 128;
  290. } while (LIKELY(len >= 128));
  291. y += Rotate(w.first, 37) * k0 + z;
  292. x += Rotate(v.first + z, 49) * k0;
  293. // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
  294. for (size_t tail_done = 0; tail_done < len; ) {
  295. tail_done += 32;
  296. y = Rotate(y - x, 42) * k0 + v.second;
  297. w.first += Fetch64(s + len - tail_done + 16);
  298. x = Rotate(x, 49) * k0 + w.first;
  299. w.first += v.first;
  300. v = WeakHashLen32WithSeeds(s + len - tail_done, v.first, v.second);
  301. }
  302. // At this point our 48 bytes of state should contain more than
  303. // enough information for a strong 128-bit hash. We use two
  304. // different 48-byte-to-8-byte hashes to get a 16-byte final result.
  305. x = HashLen16(x, v.first);
  306. y = HashLen16(y, w.first);
  307. return uint128(HashLen16(x + v.second, w.second) + y,
  308. HashLen16(x + w.second, y + v.second));
  309. }
  310. uint128 CityHash128(const char *s, size_t len) {
  311. if (len >= 16) {
  312. return CityHash128WithSeed(s + 16,
  313. len - 16,
  314. uint128(Fetch64(s) ^ k3,
  315. Fetch64(s + 8)));
  316. } else if (len >= 8) {
  317. return CityHash128WithSeed(NULL,
  318. 0,
  319. uint128(Fetch64(s) ^ (len * k0),
  320. Fetch64(s + len - 8) ^ k1));
  321. } else {
  322. return CityHash128WithSeed(s, len, uint128(k0, k1));
  323. }
  324. }
  325. #ifdef __SSE4_2__
  326. // Requires len >= 240.
  327. static void CityHashCrc256Long(const char *s, size_t len,
  328. uint32 seed, uint64 *result) {
  329. uint64 a = Fetch64(s + 56) + k0;
  330. uint64 b = Fetch64(s + 96) + k0;
  331. uint64 c = result[1] = HashLen16(b, len);
  332. uint64 d = result[2] = Fetch64(s + 120) * k0 + len;
  333. uint64 e = Fetch64(s + 184) + seed;
  334. uint64 f = seed;
  335. uint64 g = 0;
  336. uint64 h = 0;
  337. uint64 i = 0;
  338. uint64 j = 0;
  339. uint64 t = c + d;
  340. // 240 bytes of input per iter.
  341. size_t iters = len / 240;
  342. len -= iters * 240;
  343. do {
  344. #define CHUNK(multiplier, z) \
  345. { \
  346. uint64 old_a = a; \
  347. a = Rotate(b, 41 ^ z) * multiplier + Fetch64(s); \
  348. b = Rotate(c, 27 ^ z) * multiplier + Fetch64(s + 8); \
  349. c = Rotate(d, 41 ^ z) * multiplier + Fetch64(s + 16); \
  350. d = Rotate(e, 33 ^ z) * multiplier + Fetch64(s + 24); \
  351. e = Rotate(t, 25 ^ z) * multiplier + Fetch64(s + 32); \
  352. t = old_a; \
  353. } \
  354. f = _mm_crc32_u64(f, a); \
  355. g = _mm_crc32_u64(g, b); \
  356. h = _mm_crc32_u64(h, c); \
  357. i = _mm_crc32_u64(i, d); \
  358. j = _mm_crc32_u64(j, e); \
  359. s += 40
  360. CHUNK(1, 1); CHUNK(k0, 0);
  361. CHUNK(1, 1); CHUNK(k0, 0);
  362. CHUNK(1, 1); CHUNK(k0, 0);
  363. } while (--iters > 0);
  364. j += i << 32;
  365. a = HashLen16(a, j);
  366. h += g << 32;
  367. b = b * k0 + h;
  368. c = HashLen16(c, f) + i;
  369. d = HashLen16(d, e);
  370. pair<uint64, uint64> v(j + e, HashLen16(h, t));
  371. h = v.second + f;
  372. // If 0 < len < 240, hash chunks of 32 bytes each from the end of s.
  373. for (size_t tail_done = 0; tail_done < len; ) {
  374. tail_done += 32;
  375. c = Rotate(c - a, 42) * k0 + v.second;
  376. d += Fetch64(s + len - tail_done + 16);
  377. a = Rotate(a, 49) * k0 + d;
  378. d += v.first;
  379. v = WeakHashLen32WithSeeds(s + len - tail_done, v.first, v.second);
  380. }
  381. // Final mix.
  382. e = HashLen16(a, d) + v.first;
  383. f = HashLen16(b, c) + a;
  384. g = HashLen16(v.first, v.second) + c;
  385. result[0] = e + f + g + h;
  386. a = ShiftMix((a + g) * k0) * k0 + b;
  387. result[1] += a + result[0];
  388. a = ShiftMix(a * k0) * k0 + c;
  389. result[2] += a + result[1];
  390. a = ShiftMix((a + e) * k0) * k0;
  391. result[3] = a + result[2];
  392. }
  393. // Requires len < 240.
  394. static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) {
  395. char buf[240];
  396. memcpy(buf, s, len);
  397. memset(buf + len, 0, 240 - len);
  398. CityHashCrc256Long(buf, 240, ~static_cast<uint32>(len), result);
  399. }
  400. void CityHashCrc256(const char *s, size_t len, uint64 *result) {
  401. if (LIKELY(len >= 240)) {
  402. CityHashCrc256Long(s, len, 0, result);
  403. } else {
  404. CityHashCrc256Short(s, len, result);
  405. }
  406. }
  407. uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
  408. if (len <= 900) {
  409. return CityHash128WithSeed(s, len, seed);
  410. } else {
  411. uint64 result[4];
  412. CityHashCrc256(s, len, result);
  413. uint64 u = Uint128High64(seed) + result[0];
  414. uint64 v = Uint128Low64(seed) + result[1];
  415. return uint128(HashLen16(u, v + result[2]),
  416. HashLen16(Rotate(v, 32), u * k0 + result[3]));
  417. }
  418. }
  419. uint128 CityHashCrc128(const char *s, size_t len) {
  420. if (len <= 900) {
  421. return CityHash128(s, len);
  422. } else {
  423. uint64 result[4];
  424. CityHashCrc256(s, len, result);
  425. return uint128(result[2], result[3]);
  426. }
  427. }
  428. #endif // __SSE4_2__
  429. } // namespace CityHash_v1_0_2