00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "absl/hash/internal/city.h"
00023
00024 #include <string.h>
00025 #include <algorithm>
00026
00027 #include "absl/base/config.h"
00028 #include "absl/base/internal/endian.h"
00029 #include "absl/base/internal/unaligned_access.h"
00030 #include "absl/base/optimization.h"
00031
00032 namespace absl {
00033 namespace hash_internal {
00034
00035 #ifdef ABSL_IS_BIG_ENDIAN
00036 #define uint32_in_expected_order(x) (absl::gbswap_32(x))
00037 #define uint64_in_expected_order(x) (absl::gbswap_64(x))
00038 #else
00039 #define uint32_in_expected_order(x) (x)
00040 #define uint64_in_expected_order(x) (x)
00041 #endif
00042
00043 static uint64_t Fetch64(const char *p) {
00044 return uint64_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD64(p));
00045 }
00046
00047 static uint32_t Fetch32(const char *p) {
00048 return uint32_in_expected_order(ABSL_INTERNAL_UNALIGNED_LOAD32(p));
00049 }
00050
00051
00052 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
00053 static const uint64_t k1 = 0xb492b66fbe98f273ULL;
00054 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
00055
00056
00057 static const uint32_t c1 = 0xcc9e2d51;
00058 static const uint32_t c2 = 0x1b873593;
00059
00060
00061 static uint32_t fmix(uint32_t h) {
00062 h ^= h >> 16;
00063 h *= 0x85ebca6b;
00064 h ^= h >> 13;
00065 h *= 0xc2b2ae35;
00066 h ^= h >> 16;
00067 return h;
00068 }
00069
00070 static uint32_t Rotate32(uint32_t val, int shift) {
00071
00072 return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
00073 }
00074
00075 #undef PERMUTE3
00076 #define PERMUTE3(a, b, c) \
00077 do { \
00078 std::swap(a, b); \
00079 std::swap(a, c); \
00080 } while (0)
00081
00082 static uint32_t Mur(uint32_t a, uint32_t h) {
00083
00084 a *= c1;
00085 a = Rotate32(a, 17);
00086 a *= c2;
00087 h ^= a;
00088 h = Rotate32(h, 19);
00089 return h * 5 + 0xe6546b64;
00090 }
00091
00092 static uint32_t Hash32Len13to24(const char *s, size_t len) {
00093 uint32_t a = Fetch32(s - 4 + (len >> 1));
00094 uint32_t b = Fetch32(s + 4);
00095 uint32_t c = Fetch32(s + len - 8);
00096 uint32_t d = Fetch32(s + (len >> 1));
00097 uint32_t e = Fetch32(s);
00098 uint32_t f = Fetch32(s + len - 4);
00099 uint32_t h = len;
00100
00101 return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
00102 }
00103
00104 static uint32_t Hash32Len0to4(const char *s, size_t len) {
00105 uint32_t b = 0;
00106 uint32_t c = 9;
00107 for (size_t i = 0; i < len; i++) {
00108 signed char v = s[i];
00109 b = b * c1 + v;
00110 c ^= b;
00111 }
00112 return fmix(Mur(b, Mur(len, c)));
00113 }
00114
00115 static uint32_t Hash32Len5to12(const char *s, size_t len) {
00116 uint32_t a = len, b = len * 5, c = 9, d = b;
00117 a += Fetch32(s);
00118 b += Fetch32(s + len - 4);
00119 c += Fetch32(s + ((len >> 1) & 4));
00120 return fmix(Mur(c, Mur(b, Mur(a, d))));
00121 }
00122
00123 uint32_t CityHash32(const char *s, size_t len) {
00124 if (len <= 24) {
00125 return len <= 12
00126 ? (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len))
00127 : Hash32Len13to24(s, len);
00128 }
00129
00130
00131 uint32_t h = len, g = c1 * len, f = g;
00132
00133 uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
00134 uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
00135 uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
00136 uint32_t a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
00137 uint32_t a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
00138 h ^= a0;
00139 h = Rotate32(h, 19);
00140 h = h * 5 + 0xe6546b64;
00141 h ^= a2;
00142 h = Rotate32(h, 19);
00143 h = h * 5 + 0xe6546b64;
00144 g ^= a1;
00145 g = Rotate32(g, 19);
00146 g = g * 5 + 0xe6546b64;
00147 g ^= a3;
00148 g = Rotate32(g, 19);
00149 g = g * 5 + 0xe6546b64;
00150 f += a4;
00151 f = Rotate32(f, 19);
00152 f = f * 5 + 0xe6546b64;
00153 size_t iters = (len - 1) / 20;
00154 do {
00155 uint32_t b0 = Rotate32(Fetch32(s) * c1, 17) * c2;
00156 uint32_t b1 = Fetch32(s + 4);
00157 uint32_t b2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
00158 uint32_t b3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
00159 uint32_t b4 = Fetch32(s + 16);
00160 h ^= b0;
00161 h = Rotate32(h, 18);
00162 h = h * 5 + 0xe6546b64;
00163 f += b1;
00164 f = Rotate32(f, 19);
00165 f = f * c1;
00166 g += b2;
00167 g = Rotate32(g, 18);
00168 g = g * 5 + 0xe6546b64;
00169 h ^= b3 + b1;
00170 h = Rotate32(h, 19);
00171 h = h * 5 + 0xe6546b64;
00172 g ^= b4;
00173 g = absl::gbswap_32(g) * 5;
00174 h += b4 * 5;
00175 h = absl::gbswap_32(h);
00176 f += b0;
00177 PERMUTE3(f, h, g);
00178 s += 20;
00179 } while (--iters != 0);
00180 g = Rotate32(g, 11) * c1;
00181 g = Rotate32(g, 17) * c1;
00182 f = Rotate32(f, 11) * c1;
00183 f = Rotate32(f, 17) * c1;
00184 h = Rotate32(h + g, 19);
00185 h = h * 5 + 0xe6546b64;
00186 h = Rotate32(h, 17) * c1;
00187 h = Rotate32(h + f, 19);
00188 h = h * 5 + 0xe6546b64;
00189 h = Rotate32(h, 17) * c1;
00190 return h;
00191 }
00192
00193
00194
00195 static uint64_t Rotate(uint64_t val, int shift) {
00196
00197 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
00198 }
00199
00200 static uint64_t ShiftMix(uint64_t val) { return val ^ (val >> 47); }
00201
00202 static uint64_t HashLen16(uint64_t u, uint64_t v) {
00203 return Hash128to64(uint128(u, v));
00204 }
00205
00206 static uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
00207
00208 uint64_t a = (u ^ v) * mul;
00209 a ^= (a >> 47);
00210 uint64_t b = (v ^ a) * mul;
00211 b ^= (b >> 47);
00212 b *= mul;
00213 return b;
00214 }
00215
00216 static uint64_t HashLen0to16(const char *s, size_t len) {
00217 if (len >= 8) {
00218 uint64_t mul = k2 + len * 2;
00219 uint64_t a = Fetch64(s) + k2;
00220 uint64_t b = Fetch64(s + len - 8);
00221 uint64_t c = Rotate(b, 37) * mul + a;
00222 uint64_t d = (Rotate(a, 25) + b) * mul;
00223 return HashLen16(c, d, mul);
00224 }
00225 if (len >= 4) {
00226 uint64_t mul = k2 + len * 2;
00227 uint64_t a = Fetch32(s);
00228 return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
00229 }
00230 if (len > 0) {
00231 uint8_t a = s[0];
00232 uint8_t b = s[len >> 1];
00233 uint8_t c = s[len - 1];
00234 uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
00235 uint32_t z = len + (static_cast<uint32_t>(c) << 2);
00236 return ShiftMix(y * k2 ^ z * k0) * k2;
00237 }
00238 return k2;
00239 }
00240
00241
00242
00243 static uint64_t HashLen17to32(const char *s, size_t len) {
00244 uint64_t mul = k2 + len * 2;
00245 uint64_t a = Fetch64(s) * k1;
00246 uint64_t b = Fetch64(s + 8);
00247 uint64_t c = Fetch64(s + len - 8) * mul;
00248 uint64_t d = Fetch64(s + len - 16) * k2;
00249 return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
00250 a + Rotate(b + k2, 18) + c, mul);
00251 }
00252
00253
00254
00255 static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(uint64_t w, uint64_t x,
00256 uint64_t y, uint64_t z,
00257 uint64_t a, uint64_t b) {
00258 a += w;
00259 b = Rotate(b + a + z, 21);
00260 uint64_t c = a;
00261 a += x;
00262 a += y;
00263 b += Rotate(a, 44);
00264 return std::make_pair(a + z, b + c);
00265 }
00266
00267
00268 static std::pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(const char *s, uint64_t a,
00269 uint64_t b) {
00270 return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16),
00271 Fetch64(s + 24), a, b);
00272 }
00273
00274
00275 static uint64_t HashLen33to64(const char *s, size_t len) {
00276 uint64_t mul = k2 + len * 2;
00277 uint64_t a = Fetch64(s) * k2;
00278 uint64_t b = Fetch64(s + 8);
00279 uint64_t c = Fetch64(s + len - 24);
00280 uint64_t d = Fetch64(s + len - 32);
00281 uint64_t e = Fetch64(s + 16) * k2;
00282 uint64_t f = Fetch64(s + 24) * 9;
00283 uint64_t g = Fetch64(s + len - 8);
00284 uint64_t h = Fetch64(s + len - 16) * mul;
00285 uint64_t u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
00286 uint64_t v = ((a + g) ^ d) + f + 1;
00287 uint64_t w = absl::gbswap_64((u + v) * mul) + h;
00288 uint64_t x = Rotate(e + f, 42) + c;
00289 uint64_t y = (absl::gbswap_64((v + w) * mul) + g) * mul;
00290 uint64_t z = e + f + c;
00291 a = absl::gbswap_64((x + z) * mul + y) + b;
00292 b = ShiftMix((z + a) * mul + d + h) * mul;
00293 return b + x;
00294 }
00295
00296 uint64_t CityHash64(const char *s, size_t len) {
00297 if (len <= 32) {
00298 if (len <= 16) {
00299 return HashLen0to16(s, len);
00300 } else {
00301 return HashLen17to32(s, len);
00302 }
00303 } else if (len <= 64) {
00304 return HashLen33to64(s, len);
00305 }
00306
00307
00308
00309 uint64_t x = Fetch64(s + len - 40);
00310 uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
00311 uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
00312 std::pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
00313 std::pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
00314 x = x * k1 + Fetch64(s);
00315
00316
00317 len = (len - 1) & ~static_cast<size_t>(63);
00318 do {
00319 x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
00320 y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
00321 x ^= w.second;
00322 y += v.first + Fetch64(s + 40);
00323 z = Rotate(z + w.first, 33) * k1;
00324 v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
00325 w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
00326 std::swap(z, x);
00327 s += 64;
00328 len -= 64;
00329 } while (len != 0);
00330 return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
00331 HashLen16(v.second, w.second) + x);
00332 }
00333
00334 uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) {
00335 return CityHash64WithSeeds(s, len, k2, seed);
00336 }
00337
00338 uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
00339 uint64_t seed1) {
00340 return HashLen16(CityHash64(s, len) - seed0, seed1);
00341 }
00342
00343 }
00344 }