cityhash.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <algorithm>
  30. #include <string.h> // for memcpy and memset
  31. #include "cityhash.h"
  32. #include "common/swap.h"
  33. // #include "config.h"
  34. #ifdef __GNUC__
  35. #define HAVE_BUILTIN_EXPECT 1
  36. #endif
  37. #ifdef COMMON_BIG_ENDIAN
  38. #define WORDS_BIGENDIAN 1
  39. #endif
  40. using namespace std;
  41. typedef uint8_t uint8;
  42. typedef uint32_t uint32;
  43. typedef uint64_t uint64;
  44. namespace Common {
  45. static uint64 UNALIGNED_LOAD64(const char* p) {
  46. uint64 result;
  47. memcpy(&result, p, sizeof(result));
  48. return result;
  49. }
  50. static uint32 UNALIGNED_LOAD32(const char* p) {
  51. uint32 result;
  52. memcpy(&result, p, sizeof(result));
  53. return result;
  54. }
  55. #ifdef WORDS_BIGENDIAN
  56. #define uint32_in_expected_order(x) (swap32(x))
  57. #define uint64_in_expected_order(x) (swap64(x))
  58. #else
  59. #define uint32_in_expected_order(x) (x)
  60. #define uint64_in_expected_order(x) (x)
  61. #endif
  62. #if !defined(LIKELY)
  63. #if HAVE_BUILTIN_EXPECT
  64. #define LIKELY(x) (__builtin_expect(!!(x), 1))
  65. #else
  66. #define LIKELY(x) (x)
  67. #endif
  68. #endif
  69. static uint64 Fetch64(const char* p) {
  70. return uint64_in_expected_order(UNALIGNED_LOAD64(p));
  71. }
  72. static uint32 Fetch32(const char* p) {
  73. return uint32_in_expected_order(UNALIGNED_LOAD32(p));
  74. }
  75. // Some primes between 2^63 and 2^64 for various uses.
  76. static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
  77. static const uint64 k1 = 0xb492b66fbe98f273ULL;
  78. static const uint64 k2 = 0x9ae16a3b2f90404fULL;
  79. // Bitwise right rotate. Normally this will compile to a single
  80. // instruction, especially if the shift is a manifest constant.
  81. static uint64 Rotate(uint64 val, int shift) {
  82. // Avoid shifting by 64: doing so yields an undefined result.
  83. return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
  84. }
  85. static uint64 ShiftMix(uint64 val) {
  86. return val ^ (val >> 47);
  87. }
  88. static uint64 HashLen16(uint64 u, uint64 v) {
  89. return Hash128to64(uint128(u, v));
  90. }
  91. static uint64 HashLen16(uint64 u, uint64 v, uint64 mul) {
  92. // Murmur-inspired hashing.
  93. uint64 a = (u ^ v) * mul;
  94. a ^= (a >> 47);
  95. uint64 b = (v ^ a) * mul;
  96. b ^= (b >> 47);
  97. b *= mul;
  98. return b;
  99. }
  100. static uint64 HashLen0to16(const char* s, size_t len) {
  101. if (len >= 8) {
  102. uint64 mul = k2 + len * 2;
  103. uint64 a = Fetch64(s) + k2;
  104. uint64 b = Fetch64(s + len - 8);
  105. uint64 c = Rotate(b, 37) * mul + a;
  106. uint64 d = (Rotate(a, 25) + b) * mul;
  107. return HashLen16(c, d, mul);
  108. }
  109. if (len >= 4) {
  110. uint64 mul = k2 + len * 2;
  111. uint64 a = Fetch32(s);
  112. return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
  113. }
  114. if (len > 0) {
  115. uint8 a = s[0];
  116. uint8 b = s[len >> 1];
  117. uint8 c = s[len - 1];
  118. uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
  119. uint32 z = static_cast<uint32>(len) + (static_cast<uint32>(c) << 2);
  120. return ShiftMix(y * k2 ^ z * k0) * k2;
  121. }
  122. return k2;
  123. }
  124. // This probably works well for 16-byte strings as well, but it may be overkill
  125. // in that case.
  126. static uint64 HashLen17to32(const char* s, size_t len) {
  127. uint64 mul = k2 + len * 2;
  128. uint64 a = Fetch64(s) * k1;
  129. uint64 b = Fetch64(s + 8);
  130. uint64 c = Fetch64(s + len - 8) * mul;
  131. uint64 d = Fetch64(s + len - 16) * k2;
  132. return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d, a + Rotate(b + k2, 18) + c, mul);
  133. }
  134. // Return a 16-byte hash for 48 bytes. Quick and dirty.
  135. // Callers do best to use "random-looking" values for a and b.
  136. static pair<uint64, uint64> WeakHashLen32WithSeeds(uint64 w, uint64 x, uint64 y, uint64 z, uint64 a,
  137. uint64 b) {
  138. a += w;
  139. b = Rotate(b + a + z, 21);
  140. uint64 c = a;
  141. a += x;
  142. a += y;
  143. b += Rotate(a, 44);
  144. return make_pair(a + z, b + c);
  145. }
  146. // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
  147. static pair<uint64, uint64> WeakHashLen32WithSeeds(const char* s, uint64 a, uint64 b) {
  148. return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16), Fetch64(s + 24), a,
  149. b);
  150. }
  151. // Return an 8-byte hash for 33 to 64 bytes.
  152. static uint64 HashLen33to64(const char* s, size_t len) {
  153. uint64 mul = k2 + len * 2;
  154. uint64 a = Fetch64(s) * k2;
  155. uint64 b = Fetch64(s + 8);
  156. uint64 c = Fetch64(s + len - 24);
  157. uint64 d = Fetch64(s + len - 32);
  158. uint64 e = Fetch64(s + 16) * k2;
  159. uint64 f = Fetch64(s + 24) * 9;
  160. uint64 g = Fetch64(s + len - 8);
  161. uint64 h = Fetch64(s + len - 16) * mul;
  162. uint64 u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
  163. uint64 v = ((a + g) ^ d) + f + 1;
  164. uint64 w = swap64((u + v) * mul) + h;
  165. uint64 x = Rotate(e + f, 42) + c;
  166. uint64 y = (swap64((v + w) * mul) + g) * mul;
  167. uint64 z = e + f + c;
  168. a = swap64((x + z) * mul + y) + b;
  169. b = ShiftMix((z + a) * mul + d + h) * mul;
  170. return b + x;
  171. }
  172. uint64 CityHash64(const char* s, size_t len) {
  173. if (len <= 32) {
  174. if (len <= 16) {
  175. return HashLen0to16(s, len);
  176. } else {
  177. return HashLen17to32(s, len);
  178. }
  179. } else if (len <= 64) {
  180. return HashLen33to64(s, len);
  181. }
  182. // For strings over 64 bytes we hash the end first, and then as we
  183. // loop we keep 56 bytes of state: v, w, x, y, and z.
  184. uint64 x = Fetch64(s + len - 40);
  185. uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
  186. uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
  187. pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
  188. pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
  189. x = x * k1 + Fetch64(s);
  190. // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
  191. len = (len - 1) & ~static_cast<size_t>(63);
  192. do {
  193. x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
  194. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  195. x ^= w.second;
  196. y += v.first + Fetch64(s + 40);
  197. z = Rotate(z + w.first, 33) * k1;
  198. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  199. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
  200. std::swap(z, x);
  201. s += 64;
  202. len -= 64;
  203. } while (len != 0);
  204. return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
  205. HashLen16(v.second, w.second) + x);
  206. }
  207. uint64 CityHash64WithSeed(const char* s, size_t len, uint64 seed) {
  208. return CityHash64WithSeeds(s, len, k2, seed);
  209. }
  210. uint64 CityHash64WithSeeds(const char* s, size_t len, uint64 seed0, uint64 seed1) {
  211. return HashLen16(CityHash64(s, len) - seed0, seed1);
  212. }
  213. // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
  214. // of any length representable in signed long. Based on City and Murmur.
  215. static uint128 CityMurmur(const char* s, size_t len, uint128 seed) {
  216. uint64 a = Uint128Low64(seed);
  217. uint64 b = Uint128High64(seed);
  218. uint64 c = 0;
  219. uint64 d = 0;
  220. signed long l = static_cast<long>(len) - 16;
  221. if (l <= 0) { // len <= 16
  222. a = ShiftMix(a * k1) * k1;
  223. c = b * k1 + HashLen0to16(s, len);
  224. d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
  225. } else { // len > 16
  226. c = HashLen16(Fetch64(s + len - 8) + k1, a);
  227. d = HashLen16(b + len, c + Fetch64(s + len - 16));
  228. a += d;
  229. do {
  230. a ^= ShiftMix(Fetch64(s) * k1) * k1;
  231. a *= k1;
  232. b ^= a;
  233. c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
  234. c *= k1;
  235. d ^= c;
  236. s += 16;
  237. l -= 16;
  238. } while (l > 0);
  239. }
  240. a = HashLen16(a, c);
  241. b = HashLen16(d, b);
  242. return uint128(a ^ b, HashLen16(b, a));
  243. }
  244. uint128 CityHash128WithSeed(const char* s, size_t len, uint128 seed) {
  245. if (len < 128) {
  246. return CityMurmur(s, len, seed);
  247. }
  248. // We expect len >= 128 to be the common case. Keep 56 bytes of state:
  249. // v, w, x, y, and z.
  250. pair<uint64, uint64> v, w;
  251. uint64 x = Uint128Low64(seed);
  252. uint64 y = Uint128High64(seed);
  253. uint64 z = len * k1;
  254. v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
  255. v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
  256. w.first = Rotate(y + z, 35) * k1 + x;
  257. w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
  258. // This is the same inner loop as CityHash64(), manually unrolled.
  259. do {
  260. x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
  261. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  262. x ^= w.second;
  263. y += v.first + Fetch64(s + 40);
  264. z = Rotate(z + w.first, 33) * k1;
  265. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  266. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
  267. std::swap(z, x);
  268. s += 64;
  269. x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
  270. y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
  271. x ^= w.second;
  272. y += v.first + Fetch64(s + 40);
  273. z = Rotate(z + w.first, 33) * k1;
  274. v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
  275. w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
  276. std::swap(z, x);
  277. s += 64;
  278. len -= 128;
  279. } while (LIKELY(len >= 128));
  280. x += Rotate(v.first + z, 49) * k0;
  281. y = y * k0 + Rotate(w.second, 37);
  282. z = z * k0 + Rotate(w.first, 27);
  283. w.first *= 9;
  284. v.first *= k0;
  285. // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
  286. for (size_t tail_done = 0; tail_done < len;) {
  287. tail_done += 32;
  288. y = Rotate(x + y, 42) * k0 + v.second;
  289. w.first += Fetch64(s + len - tail_done + 16);
  290. x = x * k0 + w.first;
  291. z += w.second + Fetch64(s + len - tail_done);
  292. w.second += v.first;
  293. v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
  294. v.first *= k0;
  295. }
  296. // At this point our 56 bytes of state should contain more than
  297. // enough information for a strong 128-bit hash. We use two
  298. // different 56-byte-to-8-byte hashes to get a 16-byte final result.
  299. x = HashLen16(x, v.first);
  300. y = HashLen16(y + z, w.first);
  301. return uint128(HashLen16(x + v.second, w.second) + y, HashLen16(x + w.second, y + v.second));
  302. }
  303. uint128 CityHash128(const char* s, size_t len) {
  304. return len >= 16
  305. ? CityHash128WithSeed(s + 16, len - 16, uint128(Fetch64(s), Fetch64(s + 8) + k0))
  306. : CityHash128WithSeed(s, len, uint128(k0, k1));
  307. }
  308. } // namespace Common