BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
city.cpp
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 "config.h"
31 //#include <city.h>
32 
33 #include <algorithm>
34 #include <array>
35 #include <string.h> // for memcpy and memset
36 #include <fc/crypto/city.hpp>
37 #include <boost/endian/buffers.hpp>
38 
39 #if defined(__SSE4_2__) && defined(__x86_64__)
40  #include <nmmintrin.h>
41  #define _mm_crc32_u64_impl _mm_crc32_u64
42 #else
43  uint64_t _mm_crc32_u64_impl(uint64_t a, uint64_t b );
44 #endif
45 
46 namespace fc {
47 
48 using namespace std;
49 
50 // Hash 128 input bits down to 64 bits of output.
51 // This is intended to be a reasonably good hash function.
52 inline uint64_t Hash128to64(const uint128_t& x) {
53  // Murmur-inspired hashing.
54  const uint64_t kMul = 0x9ddfea08eb382d69ULL;
55  uint64_t a = (uint128_lo64(x) ^ uint128_hi64(x)) * kMul;
56  a ^= (a >> 47);
57  uint64_t b = (uint128_hi64(x) ^ a) * kMul;
58  b ^= (b >> 47);
59  b *= kMul;
60  return b;
61 }
62 
63 #ifdef _WIN32
64 
65 #include <stdlib.h>
66 #define bswap_32(x) _byteswap_ulong(x)
67 #define bswap_64(x) _byteswap_uint64(x)
68 
69 #elif defined(__APPLE__)
70 
71 // Mac OS X / Darwin features
72 #include <libkern/OSByteOrder.h>
73 #define bswap_32(x) OSSwapInt32(x)
74 #define bswap_64(x) OSSwapInt64(x)
75 
76 #elif defined(__sun) || defined(sun)
77 
78 #include <sys/byteorder.h>
79 #define bswap_32(x) BSWAP_32(x)
80 #define bswap_64(x) BSWAP_64(x)
81 
82 #elif defined(__FreeBSD__)
83 
84 #include <sys/endian.h>
85 #define bswap_32(x) bswap32(x)
86 #define bswap_64(x) bswap64(x)
87 
88 #elif defined(__OpenBSD__)
89 
90 #include <sys/types.h>
91 #define bswap_32(x) swap32(x)
92 #define bswap_64(x) swap64(x)
93 
94 #elif defined(__NetBSD__)
95 
96 #include <sys/types.h>
97 #include <machine/bswap.h>
98 #if defined(__BSWAP_RENAME) && !defined(__bswap_32)
99 #define bswap_32(x) bswap32(x)
100 #define bswap_64(x) bswap64(x)
101 #endif
102 
103 #else
104 
106 #include <byteswap.h>
108 
109 #endif
110 
111 #if !defined(LIKELY)
112 #if HAVE_BUILTIN_EXPECT
113 #define LIKELY(x) (__builtin_expect(!!(x), 1))
114 #else
115 #define LIKELY(x) (x)
116 #endif
117 #endif
118 
119 static uint64_t Fetch64(const char *p) {
120  boost::endian::little_uint64_buf_t result;
121  memcpy(&result, p, sizeof(result));
122  return result.value();
123 }
124 
125 static uint32_t Fetch32(const char *p) {
126  boost::endian::little_uint32_buf_t result;
127  memcpy(&result, p, sizeof(result));
128  return result.value();
129 }
130 
131 // Some primes between 2^63 and 2^64 for various uses.
132 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
133 static const uint64_t k1 = 0xb492b66fbe98f273ULL;
134 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
135 
136 // Magic numbers for 32-bit hashing. Copied from Murmur3.
137 static const uint32_t c1 = 0xcc9e2d51;
138 static const uint32_t c2 = 0x1b873593;
139 
140 // A 32-bit to 32-bit integer hash copied from Murmur3.
141 static uint32_t fmix(uint32_t h)
142 {
143  h ^= h >> 16;
144  h *= 0x85ebca6b;
145  h ^= h >> 13;
146  h *= 0xc2b2ae35;
147  h ^= h >> 16;
148  return h;
149 }
150 
151 static uint32_t Rotate32(uint32_t val, int shift) {
152  // Avoid shifting by 32: doing so yields an undefined result.
153  return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
154 }
155 
156 #undef PERMUTE3
157 #define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0)
158 
159 static uint32_t Mur(uint32_t a, uint32_t h) {
160  // Helper from Murmur3 for combining two 32-bit values.
161  a *= c1;
162  a = Rotate32(a, 17);
163  a *= c2;
164  h ^= a;
165  h = Rotate32(h, 19);
166  return h * 5 + 0xe6546b64;
167 }
168 
169 static uint32_t Hash32Len13to24(const char *s, size_t len) {
170  uint32_t a = Fetch32(s - 4 + (len >> 1));
171  uint32_t b = Fetch32(s + 4);
172  uint32_t c = Fetch32(s + len - 8);
173  uint32_t d = Fetch32(s + (len >> 1));
174  uint32_t e = Fetch32(s);
175  uint32_t f = Fetch32(s + len - 4);
176  uint32_t h = len;
177 
178  return fmix(Mur(f, Mur(e, Mur(d, Mur(c, Mur(b, Mur(a, h)))))));
179 }
180 
181 static uint32_t Hash32Len0to4(const char *s, size_t len) {
182  uint32_t b = 0;
183  uint32_t c = 9;
184  for (size_t i = 0; i < len; i++) {
185  signed char v = s[i];
186  b = b * c1 + v;
187  c ^= b;
188  }
189  return fmix(Mur(b, Mur(len, c)));
190 }
191 
192 static uint32_t Hash32Len5to12(const char *s, size_t len) {
193  uint32_t a = len, b = len * 5, c = 9, d = b;
194  a += Fetch32(s);
195  b += Fetch32(s + len - 4);
196  c += Fetch32(s + ((len >> 1) & 4));
197  return fmix(Mur(c, Mur(b, Mur(a, d))));
198 }
199 
200 uint32_t city_hash32(const char *s, size_t len) {
201  if (len <= 24) {
202  return len <= 12 ?
203  (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
204  Hash32Len13to24(s, len);
205  }
206 
207  // len > 24
208  uint32_t h = len, g = c1 * len, f = g;
209  uint32_t a0 = Rotate32(Fetch32(s + len - 4) * c1, 17) * c2;
210  uint32_t a1 = Rotate32(Fetch32(s + len - 8) * c1, 17) * c2;
211  uint32_t a2 = Rotate32(Fetch32(s + len - 16) * c1, 17) * c2;
212  uint32_t a3 = Rotate32(Fetch32(s + len - 12) * c1, 17) * c2;
213  uint32_t a4 = Rotate32(Fetch32(s + len - 20) * c1, 17) * c2;
214  h ^= a0;
215  h = Rotate32(h, 19);
216  h = h * 5 + 0xe6546b64;
217  h ^= a2;
218  h = Rotate32(h, 19);
219  h = h * 5 + 0xe6546b64;
220  g ^= a1;
221  g = Rotate32(g, 19);
222  g = g * 5 + 0xe6546b64;
223  g ^= a3;
224  g = Rotate32(g, 19);
225  g = g * 5 + 0xe6546b64;
226  f += a4;
227  f = Rotate32(f, 19);
228  f = f * 5 + 0xe6546b64;
229  size_t iters = (len - 1) / 20;
230  do {
231  uint32_t a0 = Rotate32(Fetch32(s) * c1, 17) * c2;
232  uint32_t a1 = Fetch32(s + 4);
233  uint32_t a2 = Rotate32(Fetch32(s + 8) * c1, 17) * c2;
234  uint32_t a3 = Rotate32(Fetch32(s + 12) * c1, 17) * c2;
235  uint32_t a4 = Fetch32(s + 16);
236  h ^= a0;
237  h = Rotate32(h, 18);
238  h = h * 5 + 0xe6546b64;
239  f += a1;
240  f = Rotate32(f, 19);
241  f = f * c1;
242  g += a2;
243  g = Rotate32(g, 18);
244  g = g * 5 + 0xe6546b64;
245  h ^= a3 + a1;
246  h = Rotate32(h, 19);
247  h = h * 5 + 0xe6546b64;
248  g ^= a4;
249  g = bswap_32(g) * 5;
250  h += a4 * 5;
251  h = bswap_32(h);
252  f += a0;
253  PERMUTE3(f, h, g);
254  s += 20;
255  } while (--iters != 0);
256  g = Rotate32(g, 11) * c1;
257  g = Rotate32(g, 17) * c1;
258  f = Rotate32(f, 11) * c1;
259  f = Rotate32(f, 17) * c1;
260  h = Rotate32(h + g, 19);
261  h = h * 5 + 0xe6546b64;
262  h = Rotate32(h, 17) * c1;
263  h = Rotate32(h + f, 19);
264  h = h * 5 + 0xe6546b64;
265  h = Rotate32(h, 17) * c1;
266  return h;
267 }
268 
269 // Bitwise right rotate. Normally this will compile to a single
270 // instruction, especially if the shift is a manifest constant.
271 static uint64_t Rotate(uint64_t val, int shift) {
272  // Avoid shifting by 64: doing so yields an undefined result.
273  return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
274 }
275 
276 static uint64_t ShiftMix(uint64_t val) {
277  return val ^ (val >> 47);
278 }
279 
280 static uint64_t HashLen16(uint64_t u, uint64_t v) {
281  return Hash128to64( uint128( u, v) );
282 }
283 
284 static uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
285  // Murmur-inspired hashing.
286  uint64_t a = (u ^ v) * mul;
287  a ^= (a >> 47);
288  uint64_t b = (v ^ a) * mul;
289  b ^= (b >> 47);
290  b *= mul;
291  return b;
292 }
293 
294 static uint64_t HashLen0to16(const char *s, size_t len) {
295  if (len >= 8) {
296  uint64_t mul = k2 + len * 2;
297  uint64_t a = Fetch64(s) + k2;
298  uint64_t b = Fetch64(s + len - 8);
299  uint64_t c = Rotate(b, 37) * mul + a;
300  uint64_t d = (Rotate(a, 25) + b) * mul;
301  return HashLen16(c, d, mul);
302  }
303  if (len >= 4) {
304  uint64_t mul = k2 + len * 2;
305  uint64_t a = Fetch32(s);
306  return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul);
307  }
308  if (len > 0) {
309  uint8_t a = s[0];
310  uint8_t b = s[len >> 1];
311  uint8_t c = s[len - 1];
312  uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
313  uint32_t z = len + (static_cast<uint32_t>(c) << 2);
314  return ShiftMix(y * k2 ^ z * k0) * k2;
315  }
316  return k2;
317 }
318 
319 // This probably works well for 16-byte strings as well, but it may be overkill
320 // in that case.
321 static uint64_t HashLen17to32(const char *s, size_t len) {
322  uint64_t mul = k2 + len * 2;
323  uint64_t a = Fetch64(s) * k1;
324  uint64_t b = Fetch64(s + 8);
325  uint64_t c = Fetch64(s + len - 8) * mul;
326  uint64_t d = Fetch64(s + len - 16) * k2;
327  return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
328  a + Rotate(b + k2, 18) + c, mul);
329 }
330 
331 // Return a 16-byte hash for 48 bytes. Quick and dirty.
332 // Callers do best to use "random-looking" values for a and b.
333 static pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
334  uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
335  a += w;
336  b = Rotate(b + a + z, 21);
337  uint64_t c = a;
338  a += x;
339  a += y;
340  b += Rotate(a, 44);
341  return make_pair(a + z, b + c);
342 }
343 
344 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
345 static pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
346  const char* s, uint64_t a, uint64_t b) {
347  return WeakHashLen32WithSeeds(Fetch64(s),
348  Fetch64(s + 8),
349  Fetch64(s + 16),
350  Fetch64(s + 24),
351  a,
352  b);
353 }
354 
355 // Return an 8-byte hash for 33 to 64 bytes.
356 static uint64_t HashLen33to64(const char *s, size_t len) {
357  uint64_t mul = k2 + len * 2;
358  uint64_t a = Fetch64(s) * k2;
359  uint64_t b = Fetch64(s + 8);
360  uint64_t c = Fetch64(s + len - 24);
361  uint64_t d = Fetch64(s + len - 32);
362  uint64_t e = Fetch64(s + 16) * k2;
363  uint64_t f = Fetch64(s + 24) * 9;
364  uint64_t g = Fetch64(s + len - 8);
365  uint64_t h = Fetch64(s + len - 16) * mul;
366  uint64_t u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9;
367  uint64_t v = ((a + g) ^ d) + f + 1;
368  uint64_t w = bswap_64((u + v) * mul) + h;
369  uint64_t x = Rotate(e + f, 42) + c;
370  uint64_t y = (bswap_64((v + w) * mul) + g) * mul;
371  uint64_t z = e + f + c;
372  a = bswap_64((x + z) * mul + y) + b;
373  b = ShiftMix((z + a) * mul + d + h) * mul;
374  return b + x;
375 }
376 
377 uint64_t city_hash64(const char *s, size_t len) {
378  if (len <= 32) {
379  if (len <= 16) {
380  return HashLen0to16(s, len);
381  } else {
382  return HashLen17to32(s, len);
383  }
384  } else if (len <= 64) {
385  return HashLen33to64(s, len);
386  }
387 
388  // For strings over 64 bytes we hash the end first, and then as we
389  // loop we keep 56 bytes of state: v, w, x, y, and z.
390  uint64_t x = Fetch64(s + len - 40);
391  uint64_t y = Fetch64(s + len - 16) + Fetch64(s + len - 56);
392  uint64_t z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24));
393  pair<uint64_t, uint64_t> v = WeakHashLen32WithSeeds(s + len - 64, len, z);
394  pair<uint64_t, uint64_t> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x);
395  x = x * k1 + Fetch64(s);
396 
397  // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
398  len = (len - 1) & ~static_cast<size_t>(63);
399  do {
400  x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
401  y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
402  x ^= w.second;
403  y += v.first + Fetch64(s + 40);
404  z = Rotate(z + w.first, 33) * k1;
405  v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
406  w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
407  std::swap(z, x);
408  s += 64;
409  len -= 64;
410  } while (len != 0);
411  return HashLen16(HashLen16(v.first, w.first) + ShiftMix(y) * k1 + z,
412  HashLen16(v.second, w.second) + x);
413 }
414 
415 uint64_t CityHash64WithSeeds(const char *s, size_t len,
416  uint64_t seed0, uint64_t seed1) {
417  return HashLen16(city_hash64(s, len) - seed0, seed1);
418 }
419 
420 uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed) {
421  return CityHash64WithSeeds(s, len, k2, seed);
422 }
423 
424 // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
425 // of any length representable in signed long. Based on City and Murmur.
426 uint128_t CityMurmur(const char *s, size_t len, uint128_t seed) {
427  uint64_t a = uint128_lo64(seed);
428  uint64_t b = uint128_hi64(seed);
429  uint64_t c = 0;
430  uint64_t d = 0;
431  signed long l = len - 16;
432  if (l <= 0) { // len <= 16
433  a = ShiftMix(a * k1) * k1;
434  c = b * k1 + HashLen0to16(s, len);
435  d = ShiftMix(a + (len >= 8 ? Fetch64(s) : c));
436  } else { // len > 16
437  c = HashLen16(Fetch64(s + len - 8) + k1, a);
438  d = HashLen16(b + len, c + Fetch64(s + len - 16));
439  a += d;
440  do {
441  a ^= ShiftMix(Fetch64(s) * k1) * k1;
442  a *= k1;
443  b ^= a;
444  c ^= ShiftMix(Fetch64(s + 8) * k1) * k1;
445  c *= k1;
446  d ^= c;
447  s += 16;
448  l -= 16;
449  } while (l > 0);
450  }
451  a = HashLen16(a, c);
452  b = HashLen16(d, b);
453  return uint128( a ^ b, HashLen16(b, a) );
454 }
455 
456 uint128_t CityHash128WithSeed(const char *s, size_t len, uint128_t seed) {
457  if (len < 128) {
458  return CityMurmur(s, len, seed);
459  }
460 
461  // We expect len >= 128 to be the common case. Keep 56 bytes of state:
462  // v, w, x, y, and z.
463  pair<uint64_t, uint64_t> v, w;
464  uint64_t x = uint128_lo64(seed);
465  uint64_t y = uint128_hi64(seed);
466  uint64_t z = len * k1;
467  v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s);
468  v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8);
469  w.first = Rotate(y + z, 35) * k1 + x;
470  w.second = Rotate(x + Fetch64(s + 88), 53) * k1;
471 
472  // This is the same inner loop as CityHash64(), manually unrolled.
473  do {
474  x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
475  y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
476  x ^= w.second;
477  y += v.first + Fetch64(s + 40);
478  z = Rotate(z + w.first, 33) * k1;
479  v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
480  w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
481  std::swap(z, x);
482  s += 64;
483  x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1;
484  y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1;
485  x ^= w.second;
486  y += v.first + Fetch64(s + 40);
487  z = Rotate(z + w.first, 33) * k1;
488  v = WeakHashLen32WithSeeds(s, v.second * k1, x + w.first);
489  w = WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch64(s + 16));
490  std::swap(z, x);
491  s += 64;
492  len -= 128;
493  } while (LIKELY(len >= 128));
494  x += Rotate(v.first + z, 49) * k0;
495  y = y * k0 + Rotate(w.second, 37);
496  z = z * k0 + Rotate(w.first, 27);
497  w.first *= 9;
498  v.first *= k0;
499  // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
500  for (size_t tail_done = 0; tail_done < len; ) {
501  tail_done += 32;
502  y = Rotate(x + y, 42) * k0 + v.second;
503  w.first += Fetch64(s + len - tail_done + 16);
504  x = x * k0 + w.first;
505  z += w.second + Fetch64(s + len - tail_done);
506  w.second += v.first;
507  v = WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second);
508  v.first *= k0;
509  }
510  // At this point our 56 bytes of state should contain more than
511  // enough information for a strong 128-bit hash. We use two
512  // different 56-byte-to-8-byte hashes to get a 16-byte final result.
513  x = HashLen16(x, v.first);
514  y = HashLen16(y + z, w.first);
515  return uint128( HashLen16(x + v.second, w.second) + y, HashLen16(x + w.second, y + v.second) );
516 }
517 
518 uint128_t city_hash128(const char *s, size_t len) {
519  return len >= 16 ?
520  CityHash128WithSeed( s + 16, len - 16, uint128( Fetch64(s), Fetch64(s + 8) + k0 ) ) :
521  CityHash128WithSeed( s, len, uint128( k0, k1 ) );
522 }
523 
524 //#ifdef __SSE4_2__
525 //#include <citycrc.h>
526 //#include <nmmintrin.h>
527 
528 // Requires len >= 240.
529 static void CityHashCrc256Long(const char *s, size_t len,
530  uint32_t seed, uint64_t *result) {
531  uint64_t a = Fetch64(s + 56) + k0;
532  uint64_t b = Fetch64(s + 96) + k0;
533  uint64_t c = result[0] = HashLen16(b, len);
534  uint64_t d = result[1] = Fetch64(s + 120) * k0 + len;
535  uint64_t e = Fetch64(s + 184) + seed;
536  uint64_t f = 0;
537  uint64_t g = 0;
538  uint64_t h = c + d;
539  uint64_t x = seed;
540  uint64_t y = 0;
541  uint64_t z = 0;
542 
543  // 240 bytes of input per iter.
544  size_t iters = len / 240;
545  len -= iters * 240;
546  do {
547 #undef CHUNK
548 #define CHUNK(r) \
549  PERMUTE3(x, z, y); \
550  b += Fetch64(s); \
551  c += Fetch64(s + 8); \
552  d += Fetch64(s + 16); \
553  e += Fetch64(s + 24); \
554  f += Fetch64(s + 32); \
555  a += b; \
556  h += f; \
557  b += c; \
558  f += d; \
559  g += e; \
560  e += z; \
561  g += x; \
562  z = _mm_crc32_u64_impl(z, b + g); \
563  y = _mm_crc32_u64_impl(y, e + h); \
564  x = _mm_crc32_u64_impl(x, f + a); \
565  e = Rotate(e, r); \
566  c += e; \
567  s += 40
568 
569  CHUNK(0); PERMUTE3(a, h, c);
570  CHUNK(33); PERMUTE3(a, h, f);
571  CHUNK(0); PERMUTE3(b, h, f);
572  CHUNK(42); PERMUTE3(b, h, d);
573  CHUNK(0); PERMUTE3(b, h, e);
574  CHUNK(33); PERMUTE3(a, h, e);
575  } while (--iters > 0);
576 
577  while (len >= 40) {
578  CHUNK(29);
579  e ^= Rotate(a, 20);
580  h += Rotate(b, 30);
581  g ^= Rotate(c, 40);
582  f += Rotate(d, 34);
583  PERMUTE3(c, h, g);
584  len -= 40;
585  }
586  if (len > 0) {
587  s = s + len - 40;
588  CHUNK(33);
589  e ^= Rotate(a, 43);
590  h += Rotate(b, 42);
591  g ^= Rotate(c, 41);
592  f += Rotate(d, 40);
593  }
594  result[0] ^= h;
595  result[1] ^= g;
596  g += h;
597  a = HashLen16(a, g + z);
598  x += y << 32;
599  b += x;
600  c = HashLen16(c, z) + h;
601  d = HashLen16(d, e + result[0]);
602  g += e;
603  h += HashLen16(x, f);
604  e = HashLen16(a, d) + g;
605  z = HashLen16(b, c) + a;
606  y = HashLen16(g, h) + c;
607  result[0] = e + z + y + x;
608  a = ShiftMix((a + y) * k0) * k0 + b;
609  result[1] += a + result[0];
610  a = ShiftMix(a * k0) * k0 + c;
611  result[2] = a + result[1];
612  a = ShiftMix((a + e) * k0) * k0;
613  result[3] = a + result[2];
614 }
615 
616 // Requires len < 240.
617 static void CityHashCrc256Short(const char *s, size_t len, uint64_t *result) {
618  char buf[240];
619  memcpy(buf, s, len);
620  memset(buf + len, 0, 240 - len);
621  CityHashCrc256Long(buf, 240, ~static_cast<uint32_t>(len), result);
622 }
623 
624 void CityHashCrc256(const char *s, size_t len, uint64_t *result) {
625  if (LIKELY(len >= 240)) {
626  CityHashCrc256Long(s, len, 0, result);
627  } else {
628  CityHashCrc256Short(s, len, result);
629  }
630 }
631 
632 array<uint64_t,4> city_hash_crc_256(const char *s, size_t len)
633 {
634  array<uint64_t,4> buf;
635  CityHashCrc256( s, len, (uint64_t*)&buf );
636  return buf;
637 }
638 
639 uint128_t CityHashCrc128WithSeed(const char *s, size_t len, uint128_t seed) {
640  if (len <= 900) {
641  return CityHash128WithSeed(s, len, seed);
642  } else {
643  uint64_t result[4];
644  CityHashCrc256(s, len, result);
645  uint64_t u = uint128_hi64(seed) + result[0];
646  uint64_t v = uint128_lo64(seed) + result[1];
647  return uint128( HashLen16(u, v + result[2]), HashLen16(Rotate(v, 32), u * k0 + result[3]) );
648  }
649 }
650 
651 uint128_t city_hash_crc_128(const char *s, size_t len) {
652  if (len <= 900) {
653  return city_hash128(s, len);
654  } else {
655  uint64_t result[4];
656  CityHashCrc256(s, len, result);
657  return uint128( result[2], result[3] );
658  }
659 }
660 
661 } // end namespace fc
662 
663 //#endif
fc::city_hash_crc_256
array< uint64_t, 4 > city_hash_crc_256(const char *s, size_t len)
Definition: city.cpp:632
fc::CityHash128WithSeed
uint128_t CityHash128WithSeed(const char *s, size_t len, uint128_t seed)
Definition: city.cpp:456
fc::CityHash64WithSeed
uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed)
Definition: city.cpp:420
fc
Definition: api.hpp:15
LIKELY
#define LIKELY(x)
Definition: city.cpp:115
fc::CityHashCrc256
void CityHashCrc256(const char *s, size_t len, uint64_t *result)
Definition: city.cpp:624
fc::uint128_hi64
uint64_t uint128_hi64(const uint128_t &x)
Definition: uint128.hpp:57
fc::city_hash32
uint32_t city_hash32(const char *buf, size_t len)
Definition: city.cpp:200
fc::city_hash64
uint64_t city_hash64(const char *buf, size_t len)
Definition: city.cpp:377
PERMUTE3
#define PERMUTE3(a, b, c)
Definition: city.cpp:157
fc::uint128_lo64
uint64_t uint128_lo64(const uint128_t &x)
Definition: uint128.hpp:54
fc::CityMurmur
uint128_t CityMurmur(const char *s, size_t len, uint128_t seed)
Definition: city.cpp:426
fc::Hash128to64
uint64_t Hash128to64(const uint128_t &x)
Definition: city.cpp:52
CHUNK
#define CHUNK(r)
fc::city_hash128
uint128_t city_hash128(const char *s, size_t len)
Definition: city.cpp:518
city.hpp
_mm_crc32_u64_impl
uint64_t _mm_crc32_u64_impl(uint64_t a, uint64_t b)
Definition: crc.cpp:607
std
Definition: zeroed_array.hpp:76
fc::uint128
uint128_t uint128(const uint64_t hi, const uint64_t lo)
Definition: uint128.hpp:67
fc::CityHashCrc128WithSeed
uint128_t CityHashCrc128WithSeed(const char *s, size_t len, uint128_t seed)
Definition: city.cpp:639
fc::CityHash64WithSeeds
uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1)
Definition: city.cpp:415
fc::city_hash_crc_128
uint128_t city_hash_crc_128(const char *s, size_t len)
Definition: city.cpp:651