Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
city.cc
Go to the documentation of this file.
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 
30 #include "../interface/city.h"
31 
32 #include <algorithm>
33 #include <string.h> // for memcpy and memset
34 
35 using namespace std;
36 
37 static uint64 UNALIGNED_LOAD64(const char *p) {
38  uint64 result;
39  memcpy(&result, p, sizeof(result));
40  return result;
41 }
42 
43 static uint32 UNALIGNED_LOAD32(const char *p) {
44  uint32 result;
45  memcpy(&result, p, sizeof(result));
46  return result;
47 }
48 
49 #if !defined(WORDS_BIGENDIAN)
50 
51 #define uint32_in_expected_order(x) (x)
52 #define uint64_in_expected_order(x) (x)
53 
54 #else
55 
56 #ifdef _MSC_VER
57 #include <stdlib.h>
58 #define bswap_32(x) _byteswap_ulong(x)
59 #define bswap_64(x) _byteswap_uint64(x)
60 
61 #elif defined(__APPLE__)
62 // Mac OS X / Darwin features
63 #include <libkern/OSByteOrder.h>
64 #define bswap_32(x) OSSwapInt32(x)
65 #define bswap_64(x) OSSwapInt64(x)
66 
67 #else
68 #include <byteswap.h>
69 #endif
70 
71 #define uint32_in_expected_order(x) (bswap_32(x))
72 #define uint64_in_expected_order(x) (bswap_64(x))
73 
74 #endif // WORDS_BIGENDIAN
75 
76 #if !defined(LIKELY)
77 #if HAVE_BUILTIN_EXPECT
78 #define LIKELY(x) (__builtin_expect(!!(x), 1))
79 #else
80 #define LIKELY(x) (x)
81 #endif
82 #endif
83 
84 static uint64 Fetch64(const char *p) {
85  return uint64_in_expected_order(UNALIGNED_LOAD64(p));
86 }
87 
88 static uint32 Fetch32(const char *p) {
89  return uint32_in_expected_order(UNALIGNED_LOAD32(p));
90 }
91 
92 // Some primes between 2^63 and 2^64 for various uses.
93 static const uint64 k0 = 0xc3a5c85c97cb3127ULL;
94 static const uint64 k1 = 0xb492b66fbe98f273ULL;
95 static const uint64 k2 = 0x9ae16a3b2f90404fULL;
96 static const uint64 k3 = 0xc949d7c7509e6557ULL;
97 
98 // Bitwise right rotate. Normally this will compile to a single
99 // instruction, especially if the shift is a manifest constant.
100 static uint64 Rotate(uint64 val, int shift) {
101  // Avoid shifting by 64: doing so yields an undefined result.
102  return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
103 }
104 
105 // Equivalent to Rotate(), but requires the second arg to be non-zero.
106 // On x86-64, and probably others, it's possible for this to compile
107 // to a single instruction if both args are already in registers.
108 static uint64 RotateByAtLeast1(uint64 val, int shift) {
109  return (val >> shift) | (val << (64 - shift));
110 }
111 
112 static uint64 ShiftMix(uint64 val) {
113  return val ^ (val >> 47);
114 }
115 
116 static uint64 HashLen16(uint64 u, uint64 v) {
117  return Hash128to64(uint128(u, v));
118 }
119 
120 static uint64 HashLen0to16(const char *s, size_t len) {
121  if (len > 8) {
122  uint64 a = Fetch64(s);
123  uint64 b = Fetch64(s + len - 8);
124  return HashLen16(a, RotateByAtLeast1(b + len, len)) ^ b;
125  }
126  if (len >= 4) {
127  uint64 a = Fetch32(s);
128  return HashLen16(len + (a << 3), Fetch32(s + len - 4));
129  }
130  if (len > 0) {
131  uint8 a = s[0];
132  uint8 b = s[len >> 1];
133  uint8 c = s[len - 1];
134  uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8);
135  uint32 z = len + (static_cast<uint32>(c) << 2);
136  return ShiftMix(y * k2 ^ z * k3) * k2;
137  }
138  return k2;
139 }
140 
141 // This probably works well for 16-byte strings as well, but it may be overkill
142 // in that case.
143 static uint64 HashLen17to32(const char *s, size_t len) {
144  uint64 a = Fetch64(s) * k1;
145  uint64 b = Fetch64(s + 8);
146  uint64 c = Fetch64(s + len - 8) * k2;
147  uint64 d = Fetch64(s + len - 16) * k0;
148  return HashLen16(Rotate(a - b, 43) + Rotate(c, 30) + d,
149  a + Rotate(b ^ k3, 20) - c + len);
150 }
151 
152 // Return a 16-byte hash for 48 bytes. Quick and dirty.
153 // Callers do best to use "random-looking" values for a and b.
154 static pair<uint64, uint64> WeakHashLen32WithSeeds(
155  uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, uint64 b) {
156  a += w;
157  b = Rotate(b + a + z, 21);
158  uint64 c = a;
159  a += x;
160  a += y;
161  b += Rotate(a, 44);
162  return make_pair(a + z, b + c);
163 }
164 
165 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
166 static pair<uint64, uint64> WeakHashLen32WithSeeds(
167  const char* s, uint64 a, uint64 b) {
168  return WeakHashLen32WithSeeds(Fetch64(s),
169  Fetch64(s + 8),
170  Fetch64(s + 16),
171  Fetch64(s + 24),
172  a,
173  b);
174 }
175 
176 // Return an 8-byte hash for 33 to 64 bytes.
177 static uint64 HashLen33to64(const char *s, size_t len) {
178  uint64 z = Fetch64(s + 24);
179  uint64 a = Fetch64(s) + (len + Fetch64(s + len - 16)) * k0;
180  uint64 b = Rotate(a + z, 52);
181  uint64 c = Rotate(a, 37);
182  a += Fetch64(s + 8);
183  c += Rotate(a, 7);
184  a += Fetch64(s + 16);
185  uint64 vf = a + z;
186  uint64 vs = b + Rotate(a, 31) + c;
187  a = Fetch64(s + 16) + Fetch64(s + len - 32);
188  z = Fetch64(s + len - 8);
189  b = Rotate(a + z, 52);
190  c = Rotate(a, 37);
191  a += Fetch64(s + len - 24);
192  c += Rotate(a, 7);
193  a += Fetch64(s + len - 16);
194  uint64 wf = a + z;
195  uint64 ws = b + Rotate(a, 31) + c;
196  uint64 r = ShiftMix((vf + ws) * k2 + (wf + vs) * k0);
197  return ShiftMix(r * k0 + vs) * k2;
198 }
199 
200 uint64 CityHash64(const char *s, size_t len) {
201  if (len <= 32) {
202  if (len <= 16) {
203  return HashLen0to16(s, len);
204  } else {
205  return HashLen17to32(s, len);
206  }
207  } else if (len <= 64) {
208  return HashLen33to64(s, len);
209  }
210 
211  // For strings over 64 bytes we hash the end first, and then as we
212  // loop we keep 56 bytes of state: v, w, x, y, and z.
213  uint64 x = Fetch64(s + len - 40);
214  uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
215  uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
216  pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
217  pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
218  x = x * k1 + Fetch64(s);
219 
220  // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
221  len = (len - 1) & ~static_cast<size_t>(63);
222  do {
223  x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
224  y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
225  x ^= w.second;
226  y += v.first + Fetch64(s + 40);
227  z = Rotate(z + w.first, 33) * k1;
228  v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
229  w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
230  std::swap(z, x);
231  s += 64;
232  len -= 64;
233  } while (len != 0);
234  return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
235  HashLen16(v.second, w.second) + x);
236 }
237 
238 uint64 CityHash64(std::string str) {
239  return CityHash64(&(str[0]),str.length());
240 }
241 
242 uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed) {
243  return CityHash64WithSeeds(s, len, k2, seed);
244 }
245 
246 uint64 CityHash64WithSeeds(const char *s, size_t len,
247  uint64 seed0, uint64 seed1) {
248  return HashLen16(CityHash64(s, len) - seed0, seed1);
249 }
250 
251 // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
252 // of any length representable in signed long. Based on City and Murmur.
253 static uint128 CityMurmur(const char *s, size_t len, uint128 seed) {
254  uint64 a = Uint128Low64(seed);
255  uint64 b = Uint128High64(seed);
256  uint64 c = 0;
257  uint64 d = 0;
258  signed long l = len - 16;
259  if (l <= 0) { // len <= 16
260  a = ShiftMix(a * k1) * k1;
261  c = b * k1 + HashLen0to16(s, len);
262  d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
263  } else { // len > 16
264  c = HashLen16(Fetch64(s + len - 8) + k1, a);
265  d = HashLen16(b + len, c + Fetch64(s + len - 16));
266  a += d;
267  do {
268  a ^= ShiftMix(Fetch64(s) * k1) * k1;
269  a *= k1;
270  b ^= a;
271  c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
272  c *= k1;
273  d ^= c;
274  s += 16;
275  l -= 16;
276  } while (l > 0);
277  }
278  a = HashLen16(a, c);
279  b = HashLen16(d, b);
280  return uint128(a ^ b, HashLen16(b, a));
281 }
282 
283 uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed) {
284  if (len < 128) {
285  return CityMurmur(s, len, seed);
286  }
287 
288  // We expect len >= 128 to be the common case. Keep 56 bytes of state:
289  // v, w, x, y, and z.
290  pair<uint64, uint64> v, w;
291  uint64 x = Uint128Low64(seed);
292  uint64 y = Uint128High64(seed);
293  uint64 z = len * k1;
294  v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
295  v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
296  w.first = Rotate(y + z, 35) * k1 + x;
297  w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
298 
299  // This is the same inner loop as CityHash64(), manually unrolled.
300  do {
301  x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
302  y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
303  x ^= w.second;
304  y += v.first + Fetch64(s + 40);
305  z = Rotate(z + w.first, 33) * k1;
306  v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
307  w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
308  std::swap(z, x);
309  s += 64;
310  x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
311  y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
312  x ^= w.second;
313  y += v.first + Fetch64(s + 40);
314  z = Rotate(z + w.first, 33) * k1;
315  v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
316  w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
317  std::swap(z, x);
318  s += 64;
319  len -= 128;
320  } while (LIKELY(len >= 128));
321  x += Rotate(v.first + z, 49) * k0;
322  z += Rotate(w.first, 37) * k0;
323  // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
324  for (size_t tail_done = 0; tail_done < len; ) {
325  tail_done += 32;
326  y = Rotate(x + y, 42) * k0 + v.second;
327  w.first += Fetch64(s + len - tail_done + 16);
328  x = x * k0 + w.first;
329  z += w.second + Fetch64(s + len - tail_done);
330  w.second += v.first;
331  v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
332  }
333  // At this point our 56 bytes of state should contain more than
334  // enough information for a strong 128-bit hash. We use two
335  // different 56-byte-to-8-byte hashes to get a 16-byte final result.
336  x = HashLen16(x, v.first);
337  y = HashLen16(y + z, w.first);
338  return uint128(HashLen16(x + v.second, w.second) + y,
339  HashLen16(x + w.second, y + v.second));
340 }
341 
342 uint128 CityHash128(const char *s, size_t len) {
343  if (len >= 16) {
344  return CityHash128WithSeed(s + 16,
345  len - 16,
346  uint128(Fetch64(s) ^ k3,
347  Fetch64(s + 8)));
348  } else if (len >= 8) {
349  return CityHash128WithSeed(NULL,
350  0,
351  uint128(Fetch64(s) ^ (len * k0),
352  Fetch64(s + len - 8) ^ k1));
353  } else {
354  return CityHash128WithSeed(s, len, uint128(k0, k1));
355  }
356 }
357 
358 #ifdef __SSE4_2__
359 #include <citycrc.h>
360 #include <nmmintrin.h>
361 
362 // Requires len >= 240.
363 static void CityHashCrc256Long(const char *s, size_t len,
364  uint32 seed, uint64 *result) {
365  uint64 a = Fetch64(s + 56) + k0;
366  uint64 b = Fetch64(s + 96) + k0;
367  uint64 c = result[0] = HashLen16(b, len);
368  uint64 d = result[1] = Fetch64(s + 120) * k0 + len;
369  uint64 e = Fetch64(s + 184) + seed;
370  uint64 f = seed;
371  uint64 g = 0;
372  uint64 h = 0;
373  uint64 i = 0;
374  uint64 j = 0;
375  uint64 t = c + d;
376 
377  // 240 bytes of input per iter.
378  size_t iters = len / 240;
379  len -= iters * 240;
380  do {
381 #define CHUNK(multiplier, z) \
382  { \
383  uint64 old_a = a; \
384  a = Rotate(b, 41 ^ z) * multiplier + Fetch64(s); \
385  b = Rotate(c, 27 ^ z) * multiplier + Fetch64(s + 8); \
386  c = Rotate(d, 41 ^ z) * multiplier + Fetch64(s + 16); \
387  d = Rotate(e, 33 ^ z) * multiplier + Fetch64(s + 24); \
388  e = Rotate(t, 25 ^ z) * multiplier + Fetch64(s + 32); \
389  t = old_a; \
390  } \
391  f = _mm_crc32_u64(f, a); \
392  g = _mm_crc32_u64(g, b); \
393  h = _mm_crc32_u64(h, c); \
394  i = _mm_crc32_u64(i, d); \
395  j = _mm_crc32_u64(j, e); \
396  s += 40
397 
398  CHUNK(1, 1); CHUNK(k0, 0);
399  CHUNK(1, 1); CHUNK(k0, 0);
400  CHUNK(1, 1); CHUNK(k0, 0);
401  } while (--iters > 0);
402 
403  while (len >= 40) {
404  CHUNK(k0, 0);
405  len -= 40;
406  }
407  if (len > 0) {
408  s = s + len - 40;
409  CHUNK(k0, 0);
410  }
411  j += i << 32;
412  a = HashLen16(a, j);
413  h += g << 32;
414  b += h;
415  c = HashLen16(c, f) + i;
416  d = HashLen16(d, e + result[0]);
417  j += e;
418  i += HashLen16(h, t);
419  e = HashLen16(a, d) + j;
420  f = HashLen16(b, c) + a;
421  g = HashLen16(j, i) + c;
422  result[0] = e + f + g + h;
423  a = ShiftMix((a + g) * k0) * k0 + b;
424  result[1] += a + result[0];
425  a = ShiftMix(a * k0) * k0 + c;
426  result[2] = a + result[1];
427  a = ShiftMix((a + e) * k0) * k0;
428  result[3] = a + result[2];
429 }
430 
431 // Requires len < 240.
432 static void CityHashCrc256Short(const char *s, size_t len, uint64 *result) {
433  char buf[240];
434  memcpy(buf, s, len);
435  memset(buf + len, 0, 240 - len);
436  CityHashCrc256Long(buf, 240, ~static_cast<uint32>(len), result);
437 }
438 
439 void CityHashCrc256(const char *s, size_t len, uint64 *result) {
440  if (LIKELY(len >= 240)) {
441  CityHashCrc256Long(s, len, 0, result);
442  } else {
443  CityHashCrc256Short(s, len, result);
444  }
445 }
446 
447 uint128 CityHashCrc128WithSeed(const char *s, size_t len, uint128 seed) {
448  if (len <= 900) {
449  return CityHash128WithSeed(s, len, seed);
450  } else {
451  uint64 result[4];
452  CityHashCrc256(s, len, result);
453  uint64 u = Uint128High64(seed) + result[0];
454  uint64 v = Uint128Low64(seed) + result[1];
455  return uint128(HashLen16(u, v + result[2]),
456  HashLen16(Rotate(v, 32), u * k0 + result[3]));
457  }
458 }
459 
460 uint128 CityHashCrc128(const char *s, size_t len) {
461  if (len <= 900) {
462  return CityHash128(s, len);
463  } else {
464  uint64 result[4];
465  CityHashCrc256(s, len, result);
466  return uint128(result[2], result[3]);
467  }
468 }
469 
470 #endif
uint64 Uint128High64(const uint128 &x)
Definition: city.h:57
#define LIKELY(x)
Definition: city.cc:80
uint64 Hash128to64(const uint128 &x)
Definition: city.h:82
uint64 Uint128Low64(const uint128 &x)
Definition: city.h:56
uint8_t uint8
Definition: city.h:51
#define uint64_in_expected_order(x)
Definition: city.cc:52
uint64 CityHash64WithSeeds(const char *s, size_t len, uint64 seed0, uint64 seed1)
Definition: city.cc:246
uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed)
Definition: city.cc:283
uint64 CityHash64(const char *s, size_t len)
Definition: city.cc:200
uint128 CityHash128(const char *s, size_t len)
Definition: city.cc:342
uint64 CityHash64WithSeed(const char *s, size_t len, uint64 seed)
Definition: city.cc:242
std::pair< uint64, uint64 > uint128
Definition: city.h:54
uint64_t uint64
Definition: city.h:53
#define uint32_in_expected_order(x)
Definition: city.cc:51
uint32_t uint32
Definition: city.h:52