HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
farmhash.h
Go to the documentation of this file.
1 // Copyright (c) 2014 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 // FarmHash, by Geoff Pike
22 
23 // Additional modifications for OpenImageIO:
24 // Copyright Contributors to the OpenImageIO project.
25 // SPDX-License-Identifier: Apache-2.0
26 // https://github.com/AcademySoftwareFoundation/OpenImageIO
27 
28 // clang-format off
29 
30 // https://github.com/google/farmhash
31 
32 #pragma once
33 
34 #include <OpenImageIO/hash.h>
35 
36 #define NAMESPACE_FOR_HASH_FUNCTIONS OIIO::farmhash
37 
38 #ifdef _MSC_VER
39 // Disable pointless windows warning
40 #pragma warning( disable : 4319 )
41 #endif
42 
43 
44 // FARMHASH ASSUMPTIONS: Modify as needed, or use -DFARMHASH_ASSUME_SSE42 etc.
45 // Note that if you use -DFARMHASH_ASSUME_SSE42 you likely need -msse42
46 // (or its equivalent for your compiler); if you use -DFARMHASH_ASSUME_AESNI
47 // you likely need -maes (or its equivalent for your compiler).
48 
49 #ifdef FARMHASH_ASSUME_SSSE3
50 #undef FARMHASH_ASSUME_SSSE3
51 #define FARMHASH_ASSUME_SSSE3 1
52 #endif
53 
54 #ifdef FARMHASH_ASSUME_SSE41
55 #undef FARMHASH_ASSUME_SSE41
56 #define FARMHASH_ASSUME_SSE41 1
57 #endif
58 
59 #ifdef FARMHASH_ASSUME_SSE42
60 #undef FARMHASH_ASSUME_SSE42
61 #define FARMHASH_ASSUME_SSE42 1
62 #endif
63 
64 #ifdef FARMHASH_ASSUME_AESNI
65 #undef FARMHASH_ASSUME_AESNI
66 #define FARMHASH_ASSUME_AESNI 1
67 #endif
68 
69 #ifdef FARMHASH_ASSUME_AVX
70 #undef FARMHASH_ASSUME_AVX
71 #define FARMHASH_ASSUME_AVX 1
72 #endif
73 
74 #if !defined(FARMHASH_CAN_USE_CXX11) && defined(LANG_CXX11)
75 #define FARMHASH_CAN_USE_CXX11 1
76 #else
77 #undef FARMHASH_CAN_USE_CXX11
78 #define FARMHASH_CAN_USE_CXX11 0
79 #endif
80 
81 // FARMHASH PORTABILITY LAYER: "static inline" or similar
82 
83 // Make static inline 'const expr, if possible. Also, try to make CUDA friendly
84 // for device code.
85 #undef STATIC_INLINE
86 #if OIIO_CPLUSPLUS_VERSION >= 14
87 # define HASH_CAN_USE_CONSTEXPR 1
88 #endif
89 #define STATIC_INLINE OIIO_HOSTDEVICE inline OIIO_CONSTEXPR14
90 
91 // FARMHASH PORTABILITY LAYER: Runtime error if misconfigured
92 
93 #ifndef FARMHASH_DIE_IF_MISCONFIGURED
94 #ifdef HASH_CAN_USE_CONSTEXPR
95 #define FARMHASH_DIE_IF_MISCONFIGURED
96 #else
97 #define FARMHASH_DIE_IF_MISCONFIGURED do { *(char*)(len % 17) = 0; } while (0)
98 #endif
99 #endif
100 
101 // FARMHASH PORTABILITY LAYER: endianness and byteswapping functions
102 
103 #ifdef WORDS_BIGENDIAN
104 #undef FARMHASH_BIG_ENDIAN
105 #define FARMHASH_BIG_ENDIAN 1
106 #endif
107 
108 #if defined(FARMHASH_LITTLE_ENDIAN) && defined(FARMHASH_BIG_ENDIAN)
109 #error
110 #endif
111 
112 #if !defined(FARMHASH_LITTLE_ENDIAN) && !defined(FARMHASH_BIG_ENDIAN)
113 #define FARMHASH_UNKNOWN_ENDIAN 1
114 #endif
115 
116 #if !defined(bswap_32) || !defined(bswap_64)
117 #undef bswap_32
118 #undef bswap_64
119 
120 #if defined(HAVE_BUILTIN_BSWAP) || defined(__clang__) || \
121  (defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || \
122  __GNUC__ >= 5))
123 // Easy case for bswap: no header file needed.
124 #define bswap_32(x) __builtin_bswap32(x)
125 #define bswap_64(x) __builtin_bswap64(x)
126 #endif
127 
128 #endif
129 
130 #if defined(FARMHASH_UNKNOWN_ENDIAN) || !defined(bswap_64)
131 
132 #ifdef _WIN32
133 
134 #undef bswap_32
135 #undef bswap_64
136 #define bswap_32(x) _byteswap_ulong(x)
137 #define bswap_64(x) _byteswap_uint64(x)
138 
139 #elif defined(__APPLE__)
140 
141 // Mac OS X / Darwin features
142 #include <libkern/OSByteOrder.h>
143 #undef bswap_32
144 #undef bswap_64
145 #define bswap_32(x) OSSwapInt32(x)
146 #define bswap_64(x) OSSwapInt64(x)
147 
148 #elif defined(__sun) || defined(sun)
149 
150 #include <sys/byteorder.h>
151 #undef bswap_32
152 #undef bswap_64
153 #define bswap_32(x) BSWAP_32(x)
154 #define bswap_64(x) BSWAP_64(x)
155 
156 #elif defined(__FreeBSD__)
157 
158 #include <sys/endian.h>
159 #undef bswap_32
160 #undef bswap_64
161 #define bswap_32(x) bswap32(x)
162 #define bswap_64(x) bswap64(x)
163 
164 #elif defined(__OpenBSD__)
165 
166 #include <sys/types.h>
167 #undef bswap_32
168 #undef bswap_64
169 #define bswap_32(x) swap32(x)
170 #define bswap_64(x) swap64(x)
171 
172 #elif defined(__NetBSD__)
173 
174 #include <sys/types.h>
175 #include <machine/bswap.h>
176 #if defined(__BSWAP_RENAME) && !defined(__bswap_32)
177 #undef bswap_32
178 #undef bswap_64
179 #define bswap_32(x) bswap32(x)
180 #define bswap_64(x) bswap64(x)
181 #endif
182 
183 #else
184 
185 #undef bswap_32
186 #undef bswap_64
187 #include <byteswap.h>
188 
189 #endif
190 
191 #ifdef WORDS_BIGENDIAN
192 #define FARMHASH_BIG_ENDIAN 1
193 #endif
194 
195 #endif
196 
197 
198 #ifdef FARMHASH_BIG_ENDIAN
199 #define uint32_in_expected_order(x) (bswap_32(x))
200 #define uint64_in_expected_order(x) (bswap_64(x))
201 #else
202 #define uint32_in_expected_order(x) (x)
203 #define uint64_in_expected_order(x) (x)
204 #endif
205 
206 #if !defined(FARMHASH_UINT128_T_DEFINED)
207 #define uint128_t OIIO::farmhash::uint128_t
208 #endif
209 
210 // Simple GPU-friendly swap function
211 template <typename T>
213  T c = a;
214  a = b;
215  b = c;
216 }
217 
218 #define Uint128 OIIO::farmhash::Uint128
219 #define CopyUint128 OIIO::farmhash::CopyUint128
220 #define Uint128Low64 OIIO::farmhash::Uint128Low64
221 #define Uint128High64 OIIO::farmhash::Uint128High64
222 #define Hash128to64 OIIO::farmhash::Hash128to64
223 
224 // namespace NAMESPACE_FOR_HASH_FUNCTIONS {
226 
227 namespace farmhash {
228 namespace inlined {
229 
230 
231 #if !defined(HASH_CAN_USE_CONSTEXPR) || HASH_CAN_USE_CONSTEXPR == 0
232 
233 STATIC_INLINE uint64_t Fetch64(const char *p) {
234  uint64_t result = 0;
235  memcpy(&result, p, sizeof(result));
236  return uint64_in_expected_order(result);
237 }
238 
239 STATIC_INLINE uint32_t Fetch32(const char *p) {
240  uint32_t result;
241  memcpy(&result, p, sizeof(result));
242  return uint32_in_expected_order(result);
243 }
244 
245 #else
246 
247 #if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806L
248 // C++20 compiler with constexpr support std::bitcast
249 STATIC_INLINE uint64_t Fetch64(const char *p) {
250  struct BlockOfBytes {
251  char bytes[8];
252  };
253  BlockOfBytes bob{p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]};
254  return std::bit_cast<uint64_t>(bob);
255 }
256 STATIC_INLINE uint32_t Fetch32(const char *p) {
257  struct BlockOfBytes {
258  char bytes[4];
259  };
260  BlockOfBytes bob{p[0],p[1],p[2],p[3]};
261  return std::bit_cast<uint32_t>(bob);
262 }
263 
264 #else
265 
266 // constexpr supported for C++14,17 versions that manually load bytes and
267 // bitshift into proper order for the unsigned integer.
268 // NOTE: bigendian untested
269 STATIC_INLINE uint64_t Fetch64(const char *p) {
270  // Favor letting uint8_t construction to handle
271  // signed to unsigned conversion vs. uint64_t(p[0]&0xff)
272  uint8_t b0 = p[0];
273  uint8_t b1 = p[1];
274  uint8_t b2 = p[2];
275  uint8_t b3 = p[3];
276  uint8_t b4 = p[4];
277  uint8_t b5 = p[5];
278  uint8_t b6 = p[6];
279  uint8_t b7 = p[7];
280  return littleendian() ?
281  (uint64_t(b0)) | // LSB
282  (uint64_t(b1) << 8) |
283  (uint64_t(b2) << 16) |
284  (uint64_t(b3) << 24) |
285  (uint64_t(b4) << 32) |
286  (uint64_t(b5) << 40) |
287  (uint64_t(b6) << 48) |
288  (uint64_t(b7) << 56) // MSB
289  : // Big Endian byte order
290  (uint64_t(b0) << 56) | // MSB
291  (uint64_t(b1) << 48) |
292  (uint64_t(b2) << 40) |
293  (uint64_t(b3) << 32) |
294  (uint64_t(b4) << 24) |
295  (uint64_t(b5) << 16) |
296  (uint64_t(b6) << 8) |
297  (uint64_t(b7)) ; // LSB
298 }
299 
300 STATIC_INLINE uint32_t Fetch32(const char *p) {
301  uint8_t b0 = p[0];
302  uint8_t b1 = p[1];
303  uint8_t b2 = p[2];
304  uint8_t b3 = p[3];
305  return littleendian() ?
306  (uint32_t(b0)) | // LSB
307  (uint32_t(b1) << 8) |
308  (uint32_t(b2) << 16) |
309  (uint32_t(b3) << 24) // MSB
310  : // Big Endian byte order
311  (uint32_t(b0) << 24) | // MSB
312  (uint32_t(b1) << 16) |
313  (uint32_t(b2) << 8) |
314  (uint32_t(b3)); // LSB
315 }
316 #endif
317 
318 
319 // devices don't seem to have bswap_64() or bswap_32()
320 template<typename T>
321 STATIC_INLINE T bswap(T v) {
322  const int S = sizeof(T);
323  T result = 0;
324 
325  const T mask = 0xff;
326  for (int i = 0; i < S/2; i++) {
327  int hi = 8 * (7 - i);
328  int lo = 8 * i;
329  int shift = 8 * (S - (2*i + 1));
330  T lo_mask = mask << lo;
331  T hi_mask = mask << hi;
332  T new_lo = (v & hi_mask) >> shift;
333  T new_hi = (v & lo_mask) << shift;
334  result |= (new_lo | new_hi);
335  }
336  return result;
337 }
338 #undef bswap_32
339 #undef bswap_64
340 
341 #define bswap_32(x) bswap<uint32_t>(x)
342 #define bswap_64(x) bswap<uint64_t>(x)
343 
344 #endif
345 
346 STATIC_INLINE uint32_t Bswap32(uint32_t val) { return bswap_32(val); }
347 STATIC_INLINE uint64_t Bswap64(uint64_t val) { return bswap_64(val); }
348 
349 // FARMHASH PORTABILITY LAYER: bitwise rot
350 
351 STATIC_INLINE uint32_t BasicRotate32(uint32_t val, int shift) {
352  // Avoid shifting by 32: doing so yields an undefined result.
353  return shift == 0 ? val : ((val >> shift) | (val << (32 - shift)));
354 }
355 
356 STATIC_INLINE uint64_t BasicRotate64(uint64_t val, int shift) {
357  // Avoid shifting by 64: doing so yields an undefined result.
358  return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
359 }
360 
361 #if defined(_MSC_VER) && defined(FARMHASH_ROTR)
362 
363 STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift) {
364  return sizeof(unsigned long) == sizeof(val) ?
365  _lrotr(val, shift) :
366  BasicRotate32(val, shift);
367 }
368 
369 STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift) {
370  return sizeof(unsigned long) == sizeof(val) ?
371  _lrotr(val, shift) :
372  BasicRotate64(val, shift);
373 }
374 
375 #else
376 
377 STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift) {
378  return BasicRotate32(val, shift);
379 }
380 STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift) {
381  return BasicRotate64(val, shift);
382 }
383 
384 #endif
385 
386 // } // namespace NAMESPACE_FOR_HASH_FUNCTIONS
387 } /*end namespace inlined */
388 } /*end namespace farmhash*/ OIIO_NAMESPACE_END
389 
390 // FARMHASH PORTABILITY LAYER: debug mode or max speed?
391 // One may use -DFARMHASH_DEBUG=1 or -DFARMHASH_DEBUG=0 to force the issue.
392 
393 #define FARMHASH_DEBUG 0 /*OIIO addition to ensure no debug vs opt differences*/
394 
395 #if !defined(FARMHASH_DEBUG) && (!defined(NDEBUG) || defined(_DEBUG))
396 #define FARMHASH_DEBUG 1
397 #endif
398 
399 #undef debug_mode
400 #if FARMHASH_DEBUG
401 #define debug_mode 1
402 #else
403 #define debug_mode 0
404 #endif
405 
406 // PLATFORM-SPECIFIC FUNCTIONS AND MACROS
407 
408 #undef x86_64
409 #if defined (__x86_64) || defined (__x86_64__)
410 #define x86_64 1
411 #else
412 #define x86_64 0
413 #endif
414 
415 #undef x86
416 #if defined(__i386__) || defined(__i386) || defined(__X86__)
417 #define x86 1
418 #else
419 #define x86 x86_64
420 #endif
421 
422 #if !defined(is_64bit)
423 #define is_64bit (x86_64 || (sizeof(void*) == 8))
424 #endif
425 
426 #undef can_use_ssse3
427 #if defined(__SSSE3__) || defined(FARMHASH_ASSUME_SSSE3)
428 
429 #include <immintrin.h>
430 #define can_use_ssse3 1
431 // Now we can use _mm_hsub_epi16 and so on.
432 
433 #else
434 #define can_use_ssse3 0
435 #endif
436 
437 #undef can_use_sse41
438 #if defined(__SSE4_1__) || defined(FARMHASH_ASSUME_SSE41)
439 
440 #include <immintrin.h>
441 #define can_use_sse41 1
442 // Now we can use _mm_insert_epi64 and so on.
443 
444 #else
445 #define can_use_sse41 0
446 #endif
447 
448 #undef can_use_sse42
449 #if defined(__SSE4_2__) || defined(FARMHASH_ASSUME_SSE42)
450 
451 #include <nmmintrin.h>
452 #define can_use_sse42 1
453 // Now we can use _mm_crc32_u{32,16,8}. And on 64-bit platforms, _mm_crc32_u64.
454 
455 #else
456 #define can_use_sse42 0
457 #endif
458 
459 #undef can_use_aesni
460 #if defined(__AES__) || defined(FARMHASH_ASSUME_AESNI)
461 
462 #include <wmmintrin.h>
463 #define can_use_aesni 1
464 // Now we can use _mm_aesimc_si128 and so on.
465 
466 #else
467 #define can_use_aesni 0
468 #endif
469 
470 #undef can_use_avx
471 #if defined(__AVX__) || defined(FARMHASH_ASSUME_AVX)
472 
473 #include <immintrin.h>
474 #define can_use_avx 1
475 
476 #else
477 #define can_use_avx 0
478 #endif
479 
480 // clang seems to define __x86_64 flags etc. even if you're compiling for gpu-device
481 #ifdef __CUDA_ARCH__
482 # undef _x86_64
483 # define _x86_64 0
484 # undef x86
485 # define x86 x86_64
486 #endif
487 
488 // skip non-constexpr code-path
489 #ifdef HASH_CAN_USE_CONSTEXPR
490 # undef can_use_ssse3
491 # define can_use_ssse3 0
492 # undef can_use_sse41
493 # define can_use_sse41 0
494 # undef can_use_sse42
495 # define can_use_sse42 0
496 # undef can_use_aesni
497 # define can_use_aesni 0
498 # undef can_use_avx
499 # define can_use_avx 0
500 #endif
501 
502 
503 // Building blocks for hash functions
504 
505 // std::swap() was in <algorithm> but is in <utility> from C++11 on.
506 #if !FARMHASH_CAN_USE_CXX11
507 #include <algorithm>
508 #endif
509 
510 #undef PERMUTE3
511 #define PERMUTE3(a, b, c) do { simpleSwap(a, b); simpleSwap(a, c); } while (0)
512 
513 
514 // namespace NAMESPACE_FOR_HASH_FUNCTIONS {
516 //using namespace OIIO::farmhash;
517  namespace farmhash {
518  namespace inlined {
519 
520 #ifndef __CUDA_ARCH__
521 #if can_use_ssse3 || can_use_sse41 || can_use_sse42 || can_use_aesni || can_use_avx
522 STATIC_INLINE __m128i Fetch128(const char* s) {
523  return _mm_loadu_si128(reinterpret_cast<const __m128i*>(s));
524 }
525 #endif
526 #endif
527 
528 // Some primes between 2^63 and 2^64 for various uses.
529 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
530 static const uint64_t k1 = 0xb492b66fbe98f273ULL;
531 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
532 
533 // Magic numbers for 32-bit hashing. Copied from Murmur3.
534 static const uint32_t c1 = 0xcc9e2d51;
535 static const uint32_t c2 = 0x1b873593;
536 
537 // A 32-bit to 32-bit integer hash copied from Murmur3.
538 STATIC_INLINE uint32_t fmix(uint32_t h)
539 {
540  h ^= h >> 16;
541  h *= 0x85ebca6b;
542  h ^= h >> 13;
543  h *= 0xc2b2ae35;
544  h ^= h >> 16;
545  return h;
546 }
547 
548 STATIC_INLINE uint32_t Mur(uint32_t a, uint32_t h) {
549  // Helper from Murmur3 for combining two 32-bit values.
550  a *= inlined::c1;
551  a = Rotate32(a, 17);
552  a *= inlined::c2;
553  h ^= a;
554  h = Rotate32(h, 19);
555  return h * 5 + 0xe6546b64;
556 }
557 
558 template <typename T> STATIC_INLINE T DebugTweak(T x) {
559  if (debug_mode) {
560  if (sizeof(x) == 4) {
561  x = ~Bswap32(x * inlined::c1);
562  } else {
563  x = ~Bswap64(x * inlined::k1);
564  }
565  }
566  return x;
567 }
568 
570  if (debug_mode) {
571  uint64_t y = DebugTweak(Uint128Low64(x));
572  uint64_t z = DebugTweak(Uint128High64(x));
573  y += z;
574  z += y;
575  CopyUint128(x, Uint128(y, z * inlined::k1));
576  }
577  return x;
578 }
579 
580 // } // namespace NAMESPACE_FOR_HASH_FUNCTIONS
581 } /*end namespace inlined*/
582 
583 using namespace std;
584 // using namespace NAMESPACE_FOR_HASH_FUNCTIONS;
585 namespace farmhashna {
586 #undef Fetch
587 #define Fetch farmhash::inlined::Fetch64
588 
589 #undef Rotate
590 #define Rotate farmhash::inlined::Rotate64
591 
592 #undef Bswap
593 #define Bswap farmhash::inlined::Bswap64
594 
595 STATIC_INLINE uint64_t ShiftMix(uint64_t val) {
596  return val ^ (val >> 47);
597 }
598 
599 STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v) {
600  return Hash128to64(Uint128(u, v));
601 }
602 
603 STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
604  // Murmur-inspired hashing.
605  uint64_t a = (u ^ v) * mul;
606  a ^= (a >> 47);
607  uint64_t b = (v ^ a) * mul;
608  b ^= (b >> 47);
609  b *= mul;
610  return b;
611 }
612 
613 STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len) {
614  if (len >= 8) {
615  uint64_t mul = inlined::k2 + len * 2;
616  uint64_t a = Fetch(s) + inlined::k2;
617  uint64_t b = Fetch(s + len - 8);
618  uint64_t c = Rotate(b, 37) * mul + a;
619  uint64_t d = (Rotate(a, 25) + b) * mul;
620  return HashLen16(c, d, mul);
621  }
622  if (len >= 4) {
623  uint64_t mul = inlined::k2 + len * 2;
624  uint64_t a = inlined::Fetch32(s);
625  return HashLen16(len + (a << 3), inlined::Fetch32(s + len - 4), mul);
626  }
627  if (len > 0) {
628  uint8_t a = s[0];
629  uint8_t b = s[len >> 1];
630  uint8_t c = s[len - 1];
631  uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
632  uint32_t z = len + (static_cast<uint32_t>(c) << 2);
633  return ShiftMix(y * inlined::k2 ^ z * inlined::k0) * inlined::k2;
634  }
635  return inlined::k2;
636 }
637 
638 // This probably works well for 16-byte strings as well, but it may be overkill
639 // in that case.
640 STATIC_INLINE uint64_t HashLen17to32(const char *s, size_t len) {
641  uint64_t mul = inlined::k2 + len * 2;
642  uint64_t a = Fetch(s) * inlined::k1;
643  uint64_t b = Fetch(s + 8);
644  uint64_t c = Fetch(s + len - 8) * mul;
645  uint64_t d = Fetch(s + len - 16) * inlined::k2;
646  return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d,
647  a + Rotate(b + inlined::k2, 18) + c, mul);
648 }
649 
650 // Return a 16-byte hash for 48 bytes. Quick and dirty.
651 // Callers do best to use "random-looking" values for a and b.
652 STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
653  uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
654  a += w;
655  b = Rotate(b + a + z, 21);
656  uint64_t c = a;
657  a += x;
658  a += y;
659  b += Rotate(a, 44);
660  return make_pair(a + z, b + c);
661 }
662 
663 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
664 STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
665  const char* s, uint64_t a, uint64_t b) {
666  return WeakHashLen32WithSeeds(Fetch(s),
667  Fetch(s + 8),
668  Fetch(s + 16),
669  Fetch(s + 24),
670  a,
671  b);
672 }
673 
674 // Return an 8-byte hash for 33 to 64 bytes.
675 STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len) {
676  uint64_t mul = inlined::k2 + len * 2;
677  uint64_t a = Fetch(s) * inlined::k2;
678  uint64_t b = Fetch(s + 8);
679  uint64_t c = Fetch(s + len - 8) * mul;
680  uint64_t d = Fetch(s + len - 16) * inlined::k2;
681  uint64_t y = Rotate(a + b, 43) + Rotate(c, 30) + d;
682  uint64_t z = HashLen16(y, a + Rotate(b + inlined::k2, 18) + c, mul);
683  uint64_t e = Fetch(s + 16) * mul;
684  uint64_t f = Fetch(s + 24);
685  uint64_t g = (y + Fetch(s + len - 32)) * mul;
686  uint64_t h = (z + Fetch(s + len - 24)) * mul;
687  return HashLen16(Rotate(e + f, 43) + Rotate(g, 30) + h,
688  e + Rotate(f + a, 18) + g, mul);
689 }
690 
691 STATIC_INLINE uint64_t Hash64(const char *s, size_t len) {
692  const uint64_t seed = 81;
693  if (len <= 32) {
694  if (len <= 16) {
695  return HashLen0to16(s, len);
696  } else {
697  return HashLen17to32(s, len);
698  }
699  } else if (len <= 64) {
700  return HashLen33to64(s, len);
701  }
702 
703  // For strings over 64 bytes we loop. Internal state consists of
704  // 56 bytes: v, w, x, y, and z.
705  uint64_t x = seed;
706  uint64_t y = seed * inlined::k1 + 113;
707  uint64_t z = ShiftMix(y * inlined::k2 + 113) * inlined::k2;
708  pair<uint64_t, uint64_t> v = make_pair(0, 0);
709  pair<uint64_t, uint64_t> w = make_pair(0, 0);
710  x = x * inlined::k2 + Fetch(s);
711 
712  // Set end so that after the loop we have 1 to 64 bytes left to process.
713  const char* end = s + ((len - 1) / 64) * 64;
714  const char* last64 = end + ((len - 1) & 63) - 63;
715  assert(s + len - 64 == last64);
716  do {
717  x = Rotate(x + y + v.first + Fetch(s + 8), 37) * inlined::k1;
718  y = Rotate(y + v.second + Fetch(s + 48), 42) * inlined::k1;
719  x ^= w.second;
720  y += v.first + Fetch(s + 40);
721  z = Rotate(z + w.first, 33) * inlined::k1;
722  CopyUint128(v, WeakHashLen32WithSeeds(s, v.second * inlined::k1, x + w.first));
723  CopyUint128(w, WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)));
724  simpleSwap(z, x);
725  s += 64;
726  } while (s != end);
727  uint64_t mul = inlined::k1 + ((z & 0xff) << 1);
728  // Make s point to the last 64 bytes of input.
729  s = last64;
730  w.first += ((len - 1) & 63);
731  v.first += w.first;
732  w.first += v.first;
733  x = Rotate(x + y + v.first + Fetch(s + 8), 37) * mul;
734  y = Rotate(y + v.second + Fetch(s + 48), 42) * mul;
735  x ^= w.second * 9;
736  y += v.first * 9 + Fetch(s + 40);
737  z = Rotate(z + w.first, 33) * mul;
738  CopyUint128(v, WeakHashLen32WithSeeds(s, v.second * mul, x + w.first));
739  CopyUint128(w, WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)));
740  simpleSwap(z, x);
741  return HashLen16(HashLen16(v.first, w.first, mul) + ShiftMix(y) * inlined::k0 + z,
742  HashLen16(v.second, w.second, mul) + x,
743  mul);
744 }
745 
746 STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
747  return HashLen16(Hash64(s, len) - seed0, seed1);
748 }
749 
750 STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
751  return Hash64WithSeeds(s, len, inlined::k2, seed);
752 }
753 
754 } // namespace farmhashna
755 namespace farmhashuo {
756 #undef Fetch
757 #define Fetch inlined::Fetch64
758 
759 #undef Rotate
760 #define Rotate inlined::Rotate64
761 
762 STATIC_INLINE uint64_t H(uint64_t x, uint64_t y, uint64_t mul, int r) {
763  uint64_t a = (x ^ y) * mul;
764  a ^= (a >> 47);
765  uint64_t b = (y ^ a) * mul;
766  return Rotate(b, r) * mul;
767 }
768 
769 STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len,
770  uint64_t seed0, uint64_t seed1) {
771  if (len <= 64) {
772  return farmhashna::Hash64WithSeeds(s, len, seed0, seed1);
773  }
774 
775  // For strings over 64 bytes we loop. Internal state consists of
776  // 64 bytes: u, v, w, x, y, and z.
777  uint64_t x = seed0;
778  uint64_t y = seed1 * inlined::k2 + 113;
779  uint64_t z = farmhashna::ShiftMix(y * inlined::k2) * inlined::k2;
780  pair<uint64_t, uint64_t> v = make_pair(seed0, seed1);
781  pair<uint64_t, uint64_t> w = make_pair(0, 0);
782  uint64_t u = x - z;
783  x *= inlined::k2;
784  uint64_t mul = inlined::k2 + (u & 0x82);
785 
786  // Set end so that after the loop we have 1 to 64 bytes left to process.
787  const char* end = s + ((len - 1) / 64) * 64;
788  const char* last64 = end + ((len - 1) & 63) - 63;
789  assert(s + len - 64 == last64);
790  do {
791  uint64_t a0 = Fetch(s);
792  uint64_t a1 = Fetch(s + 8);
793  uint64_t a2 = Fetch(s + 16);
794  uint64_t a3 = Fetch(s + 24);
795  uint64_t a4 = Fetch(s + 32);
796  uint64_t a5 = Fetch(s + 40);
797  uint64_t a6 = Fetch(s + 48);
798  uint64_t a7 = Fetch(s + 56);
799  x += a0 + a1;
800  y += a2;
801  z += a3;
802  v.first += a4;
803  v.second += a5 + a1;
804  w.first += a6;
805  w.second += a7;
806 
807  x = Rotate(x, 26);
808  x *= 9;
809  y = Rotate(y, 29);
810  z *= mul;
811  v.first = Rotate(v.first, 33);
812  v.second = Rotate(v.second, 30);
813  w.first ^= x;
814  w.first *= 9;
815  z = Rotate(z, 32);
816  z += w.second;
817  w.second += z;
818  z *= 9;
819  simpleSwap(u, y);
820 
821  z += a0 + a6;
822  v.first += a2;
823  v.second += a3;
824  w.first += a4;
825  w.second += a5 + a6;
826  x += a1;
827  y += a7;
828 
829  y += v.first;
830  v.first += x - y;
831  v.second += w.first;
832  w.first += v.second;
833  w.second += x - y;
834  x += w.second;
835  w.second = Rotate(w.second, 34);
836  simpleSwap(u, z);
837  s += 64;
838  } while (s != end);
839  // Make s point to the last 64 bytes of input.
840  s = last64;
841  u *= 9;
842  v.second = Rotate(v.second, 28);
843  v.first = Rotate(v.first, 20);
844  w.first += ((len - 1) & 63);
845  u += y;
846  y += u;
847  x = Rotate(y - x + v.first + Fetch(s + 8), 37) * mul;
848  y = Rotate(y ^ v.second ^ Fetch(s + 48), 42) * mul;
849  x ^= w.second * 9;
850  y += v.first + Fetch(s + 40);
851  z = Rotate(z + w.first, 33) * mul;
852  CopyUint128(v, farmhashna::WeakHashLen32WithSeeds(s, v.second * mul, x + w.first));
853  CopyUint128(w, farmhashna::WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)));
854  return H(farmhashna::HashLen16(v.first + x, w.first ^ y, mul) + z - u,
855  H(v.second + y, w.second + z, inlined::k2, 30) ^ x,
856  inlined::k2,
857  31);
858 }
859 
860 STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
861  return len <= 64 ? farmhashna::Hash64WithSeed(s, len, seed) :
862  Hash64WithSeeds(s, len, 0, seed);
863 }
864 
865 STATIC_INLINE uint64_t Hash64(const char *s, size_t len) {
866  return len <= 64 ? farmhashna::Hash64(s, len) :
867  Hash64WithSeeds(s, len, 81, 0);
868 }
869 } // namespace farmhashuo
870 namespace farmhashxo {
871 #undef Fetch
872 #define Fetch inlined::Fetch64
873 
874 #undef Rotate
875 #define Rotate inlined::Rotate64
876 
877 STATIC_INLINE uint64_t H32(const char *s, size_t len, uint64_t mul,
878  uint64_t seed0 = 0, uint64_t seed1 = 0) {
879  uint64_t a = Fetch(s) * inlined::k1;
880  uint64_t b = Fetch(s + 8);
881  uint64_t c = Fetch(s + len - 8) * mul;
882  uint64_t d = Fetch(s + len - 16) * inlined::k2;
883  uint64_t u = Rotate(a + b, 43) + Rotate(c, 30) + d + seed0;
884  uint64_t v = a + Rotate(b + inlined::k2, 18) + c + seed1;
885  a = farmhashna::ShiftMix((u ^ v) * mul);
886  b = farmhashna::ShiftMix((v ^ a) * mul);
887  return b;
888 }
889 
890 // Return an 8-byte hash for 33 to 64 bytes.
891 STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len) {
892  uint64_t mul0 = inlined::k2 - 30;
893  uint64_t mul1 = inlined::k2 - 30 + 2 * len;
894  uint64_t h0 = H32(s, 32, mul0);
895  uint64_t h1 = H32(s + len - 32, 32, mul1);
896  return ((h1 * mul1) + h0) * mul1;
897 }
898 
899 // Return an 8-byte hash for 65 to 96 bytes.
900 STATIC_INLINE uint64_t HashLen65to96(const char *s, size_t len) {
901  uint64_t mul0 = inlined::k2 - 114;
902  uint64_t mul1 = inlined::k2 - 114 + 2 * len;
903  uint64_t h0 = H32(s, 32, mul0);
904  uint64_t h1 = H32(s + 32, 32, mul1);
905  uint64_t h2 = H32(s + len - 32, 32, mul1, h0, h1);
906  return (h2 * 9 + (h0 >> 17) + (h1 >> 21)) * mul1;
907 }
908 
909 STATIC_INLINE uint64_t Hash64(const char *s, size_t len) {
910  if (len <= 32) {
911  if (len <= 16) {
912  return farmhashna::HashLen0to16(s, len);
913  } else {
914  return farmhashna::HashLen17to32(s, len);
915  }
916  } else if (len <= 64) {
917  return HashLen33to64(s, len);
918  } else if (len <= 96) {
919  return HashLen65to96(s, len);
920  } else if (len <= 256) {
921  return farmhashna::Hash64(s, len);
922  } else {
923  return farmhashuo::Hash64(s, len);
924  }
925 }
926 
927 STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
928  return farmhashuo::Hash64WithSeeds(s, len, seed0, seed1);
929 }
930 
931 STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
932  return farmhashuo::Hash64WithSeed(s, len, seed);
933 }
934 } // namespace farmhashxo
935 namespace farmhashte {
936 #if !can_use_sse41 || !x86_64
937 
938 STATIC_INLINE uint64_t Hash64(const char *s, size_t len) {
940  return s == NULL ? 0 : len;
941 }
942 
943 STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
945  return seed + Hash64(s, len);
946 }
947 
948 STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len,
949  uint64_t seed0, uint64_t seed1) {
951  return seed0 + seed1 + Hash64(s, len);
952 }
953 
954 #else
955 
956 #undef Fetch
957 #define Fetch inlined::Fetch64
958 
959 #undef Rotate
960 #define Rotate inlined::Rotate64
961 
962 #undef Bswap
963 #define Bswap inlined::Bswap64
964 
965 // Helpers for data-parallel operations (1x 128 bits or 2x 64 or 4x 32).
966 STATIC_INLINE __m128i Add(__m128i x, __m128i y) { return _mm_add_epi64(x, y); }
967 STATIC_INLINE __m128i Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); }
968 STATIC_INLINE __m128i Mul(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); }
969 STATIC_INLINE __m128i Shuf(__m128i x, __m128i y) { return _mm_shuffle_epi8(y, x); }
970 
971 // Requires n >= 256. Requires SSE4.1. Should be slightly faster if the
972 // compiler uses AVX instructions (e.g., use the -mavx flag with GCC).
973 STATIC_INLINE uint64_t Hash64Long(const char* s, size_t n,
974  uint64_t seed0, uint64_t seed1) {
975  const __m128i kShuf =
976  _mm_set_epi8(4, 11, 10, 5, 8, 15, 6, 9, 12, 2, 14, 13, 0, 7, 3, 1);
977  const __m128i kMult =
978  _mm_set_epi8(0xbd, 0xd6, 0x33, 0x39, 0x45, 0x54, 0xfa, 0x03,
979  0x34, 0x3e, 0x33, 0xed, 0xcc, 0x9e, 0x2d, 0x51);
980  uint64_t seed2 = (seed0 + 113) * (seed1 + 9);
981  uint64_t seed3 = (Rotate(seed0, 23) + 27) * (Rotate(seed1, 30) + 111);
982  __m128i d0 = _mm_cvtsi64_si128(seed0);
983  __m128i d1 = _mm_cvtsi64_si128(seed1);
984  __m128i d2 = Shuf(kShuf, d0);
985  __m128i d3 = Shuf(kShuf, d1);
986  __m128i d4 = Xor(d0, d1);
987  __m128i d5 = Xor(d1, d2);
988  __m128i d6 = Xor(d2, d4);
989  __m128i d7 = _mm_set1_epi32(seed2 >> 32);
990  __m128i d8 = Mul(kMult, d2);
991  __m128i d9 = _mm_set1_epi32(seed3 >> 32);
992  __m128i d10 = _mm_set1_epi32(seed3);
993  __m128i d11 = Add(d2, _mm_set1_epi32(seed2));
994  const char* end = s + (n & ~static_cast<size_t>(255));
995  do {
996  __m128i z;
997  z = inlined::Fetch128(s);
998  d0 = Add(d0, z);
999  d1 = Shuf(kShuf, d1);
1000  d2 = Xor(d2, d0);
1001  d4 = Xor(d4, z);
1002  d4 = Xor(d4, d1);
1003  simpleSwap(d0, d6);
1004  z = inlined::Fetch128(s + 16);
1005  d5 = Add(d5, z);
1006  d6 = Shuf(kShuf, d6);
1007  d8 = Shuf(kShuf, d8);
1008  d7 = Xor(d7, d5);
1009  d0 = Xor(d0, z);
1010  d0 = Xor(d0, d6);
1011  simpleSwap(d5, d11);
1012  z = inlined::Fetch128(s + 32);
1013  d1 = Add(d1, z);
1014  d2 = Shuf(kShuf, d2);
1015  d4 = Shuf(kShuf, d4);
1016  d5 = Xor(d5, z);
1017  d5 = Xor(d5, d2);
1018  simpleSwap(d10, d4);
1019  z = inlined::Fetch128(s + 48);
1020  d6 = Add(d6, z);
1021  d7 = Shuf(kShuf, d7);
1022  d0 = Shuf(kShuf, d0);
1023  d8 = Xor(d8, d6);
1024  d1 = Xor(d1, z);
1025  d1 = Add(d1, d7);
1026  z = inlined::Fetch128(s + 64);
1027  d2 = Add(d2, z);
1028  d5 = Shuf(kShuf, d5);
1029  d4 = Add(d4, d2);
1030  d6 = Xor(d6, z);
1031  d6 = Xor(d6, d11);
1032  simpleSwap(d8, d2);
1033  z = inlined::Fetch128(s + 80);
1034  d7 = Xor(d7, z);
1035  d8 = Shuf(kShuf, d8);
1036  d1 = Shuf(kShuf, d1);
1037  d0 = Add(d0, d7);
1038  d2 = Add(d2, z);
1039  d2 = Add(d2, d8);
1040  simpleSwap(d1, d7);
1041  z = inlined::Fetch128(s + 96);
1042  d4 = Shuf(kShuf, d4);
1043  d6 = Shuf(kShuf, d6);
1044  d8 = Mul(kMult, d8);
1045  d5 = Xor(d5, d11);
1046  d7 = Xor(d7, z);
1047  d7 = Add(d7, d4);
1048  simpleSwap(d6, d0);
1049  z = inlined::Fetch128(s + 112);
1050  d8 = Add(d8, z);
1051  d0 = Shuf(kShuf, d0);
1052  d2 = Shuf(kShuf, d2);
1053  d1 = Xor(d1, d8);
1054  d10 = Xor(d10, z);
1055  d10 = Xor(d10, d0);
1056  simpleSwap(d11, d5);
1057  z = inlined::Fetch128(s + 128);
1058  d4 = Add(d4, z);
1059  d5 = Shuf(kShuf, d5);
1060  d7 = Shuf(kShuf, d7);
1061  d6 = Add(d6, d4);
1062  d8 = Xor(d8, z);
1063  d8 = Xor(d8, d5);
1064  simpleSwap(d4, d10);
1065  z = inlined::Fetch128(s + 144);
1066  d0 = Add(d0, z);
1067  d1 = Shuf(kShuf, d1);
1068  d2 = Add(d2, d0);
1069  d4 = Xor(d4, z);
1070  d4 = Xor(d4, d1);
1071  z = inlined::Fetch128(s + 160);
1072  d5 = Add(d5, z);
1073  d6 = Shuf(kShuf, d6);
1074  d8 = Shuf(kShuf, d8);
1075  d7 = Xor(d7, d5);
1076  d0 = Xor(d0, z);
1077  d0 = Xor(d0, d6);
1078  simpleSwap(d2, d8);
1079  z = inlined::Fetch128(s + 176);
1080  d1 = Add(d1, z);
1081  d2 = Shuf(kShuf, d2);
1082  d4 = Shuf(kShuf, d4);
1083  d5 = Mul(kMult, d5);
1084  d5 = Xor(d5, z);
1085  d5 = Xor(d5, d2);
1086  simpleSwap(d7, d1);
1087  z = inlined::Fetch128(s + 192);
1088  d6 = Add(d6, z);
1089  d7 = Shuf(kShuf, d7);
1090  d0 = Shuf(kShuf, d0);
1091  d8 = Add(d8, d6);
1092  d1 = Xor(d1, z);
1093  d1 = Xor(d1, d7);
1094  simpleSwap(d0, d6);
1095  z = inlined::Fetch128(s + 208);
1096  d2 = Add(d2, z);
1097  d5 = Shuf(kShuf, d5);
1098  d4 = Xor(d4, d2);
1099  d6 = Xor(d6, z);
1100  d6 = Xor(d6, d9);
1101  simpleSwap(d5, d11);
1102  z = inlined::Fetch128(s + 224);
1103  d7 = Add(d7, z);
1104  d8 = Shuf(kShuf, d8);
1105  d1 = Shuf(kShuf, d1);
1106  d0 = Xor(d0, d7);
1107  d2 = Xor(d2, z);
1108  d2 = Xor(d2, d8);
1109  simpleSwap(d10, d4);
1110  z = inlined::Fetch128(s + 240);
1111  d3 = Add(d3, z);
1112  d4 = Shuf(kShuf, d4);
1113  d6 = Shuf(kShuf, d6);
1114  d7 = Mul(kMult, d7);
1115  d5 = Add(d5, d3);
1116  d7 = Xor(d7, z);
1117  d7 = Xor(d7, d4);
1118  simpleSwap(d3, d9);
1119  s += 256;
1120  } while (s != end);
1121  d6 = Add(Mul(kMult, d6), _mm_cvtsi64_si128(n));
1122  if (n % 256 != 0) {
1123  d7 = Add(_mm_shuffle_epi32(d8, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0)), d7);
1124  d8 = Add(Mul(kMult, d8), _mm_cvtsi64_si128(farmhashxo::Hash64(s, n % 256)));
1125  }
1126  __m128i t[8];
1127  d0 = Mul(kMult, Shuf(kShuf, Mul(kMult, d0)));
1128  d3 = Mul(kMult, Shuf(kShuf, Mul(kMult, d3)));
1129  d9 = Mul(kMult, Shuf(kShuf, Mul(kMult, d9)));
1130  d1 = Mul(kMult, Shuf(kShuf, Mul(kMult, d1)));
1131  d0 = Add(d11, d0);
1132  d3 = Xor(d7, d3);
1133  d9 = Add(d8, d9);
1134  d1 = Add(d10, d1);
1135  d4 = Add(d3, d4);
1136  d5 = Add(d9, d5);
1137  d6 = Xor(d1, d6);
1138  d2 = Add(d0, d2);
1139  t[0] = d0;
1140  t[1] = d3;
1141  t[2] = d9;
1142  t[3] = d1;
1143  t[4] = d4;
1144  t[5] = d5;
1145  t[6] = d6;
1146  t[7] = d2;
1147  return farmhashxo::Hash64(reinterpret_cast<const char*>(t), sizeof(t));
1148 }
1149 
1150 STATIC_INLINE uint64_t Hash64(const char *s, size_t len) {
1151  // Empirically, farmhashxo seems faster until length 512.
1152  return len >= 512 ? Hash64Long(s, len, inlined::k2, inlined::k1) : farmhashxo::Hash64(s, len);
1153 }
1154 
1155 STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed) {
1156  return len >= 512 ? Hash64Long(s, len, inlined::k1, seed) :
1157  farmhashxo::Hash64WithSeed(s, len, seed);
1158 }
1159 
1160 STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1) {
1161  return len >= 512 ? Hash64Long(s, len, seed0, seed1) :
1162  farmhashxo::Hash64WithSeeds(s, len, seed0, seed1);
1163 }
1164 
1165 #endif
1166 } // namespace farmhashte
1167 namespace farmhashnt {
1168 #if !can_use_sse41 || !x86_64
1169 
1170 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1172  return s == NULL ? 0 : len;
1173 }
1174 
1175 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1177  return seed + Hash32(s, len);
1178 }
1179 
1180 #else
1181 
1182 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1183  return static_cast<uint32_t>(farmhashte::Hash64(s, len));
1184 }
1185 
1186 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1187  return static_cast<uint32_t>(farmhashte::Hash64WithSeed(s, len, seed));
1188 }
1189 
1190 #endif
1191 } // namespace farmhashnt
1192 namespace farmhashmk {
1193 #undef Fetch
1194 #define Fetch inlined::Fetch32
1195 
1196 #undef Rotate
1197 #define Rotate inlined::Rotate32
1198 
1199 #undef Bswap
1200 #define Bswap inlined::Bswap32
1201 
1202 #undef fmix
1203 #define fmix farmhash::inlined::fmix
1204 
1205 
1206 STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len, uint32_t seed = 0) {
1207  uint32_t a = Fetch(s - 4 + (len >> 1));
1208  uint32_t b = Fetch(s + 4);
1209  uint32_t c = Fetch(s + len - 8);
1210  uint32_t d = Fetch(s + (len >> 1));
1211  uint32_t e = Fetch(s);
1212  uint32_t f = Fetch(s + len - 4);
1213  uint32_t h = d * inlined::c1 + len + seed;
1214  a = Rotate(a, 12) + f;
1215  h = inlined::Mur(c, h) + a;
1216  a = Rotate(a, 3) + c;
1217  h = inlined::Mur(e, h) + a;
1218  a = Rotate(a + f, 12) + d;
1219  h = inlined::Mur(b ^ seed, h) + a;
1220  return fmix(h);
1221 }
1222 
1223 STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len, uint32_t seed = 0) {
1224  uint32_t b = seed;
1225  uint32_t c = 9;
1226  for (size_t i = 0; i < len; i++) {
1227  signed char v = s[i];
1228  b = b * inlined::c1 + v;
1229  c ^= b;
1230  }
1231  return fmix(inlined::Mur(b, inlined::Mur(len, c)));
1232 }
1233 
1234 STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len, uint32_t seed = 0) {
1235  uint32_t a = len, b = len * 5, c = 9, d = b + seed;
1236  a += Fetch(s);
1237  b += Fetch(s + len - 4);
1238  c += Fetch(s + ((len >> 1) & 4));
1239  return fmix(seed ^ inlined::Mur(c, inlined::Mur(b, inlined::Mur(a, d))));
1240 }
1241 
1242 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1243  if (len <= 24) {
1244  return len <= 12 ?
1245  (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
1246  Hash32Len13to24(s, len);
1247  }
1248 
1249  // len > 24
1250  uint32_t h = len, g = inlined::c1 * len, f = g;
1251  uint32_t a0 = Rotate(Fetch(s + len - 4) * inlined::c1, 17) * inlined::c2;
1252  uint32_t a1 = Rotate(Fetch(s + len - 8) * inlined::c1, 17) * inlined::c2;
1253  uint32_t a2 = Rotate(Fetch(s + len - 16) * inlined::c1, 17) * inlined::c2;
1254  uint32_t a3 = Rotate(Fetch(s + len - 12) * inlined::c1, 17) * inlined::c2;
1255  uint32_t a4 = Rotate(Fetch(s + len - 20) * inlined::c1, 17) * inlined::c2;
1256  h ^= a0;
1257  h = Rotate(h, 19);
1258  h = h * 5 + 0xe6546b64;
1259  h ^= a2;
1260  h = Rotate(h, 19);
1261  h = h * 5 + 0xe6546b64;
1262  g ^= a1;
1263  g = Rotate(g, 19);
1264  g = g * 5 + 0xe6546b64;
1265  g ^= a3;
1266  g = Rotate(g, 19);
1267  g = g * 5 + 0xe6546b64;
1268  f += a4;
1269  f = Rotate(f, 19) + 113;
1270  size_t iters = (len - 1) / 20;
1271  do {
1272  uint32_t a = Fetch(s);
1273  uint32_t b = Fetch(s + 4);
1274  uint32_t c = Fetch(s + 8);
1275  uint32_t d = Fetch(s + 12);
1276  uint32_t e = Fetch(s + 16);
1277  h += a;
1278  g += b;
1279  f += c;
1280  h = inlined::Mur(d, h) + e;
1281  g = inlined::Mur(c, g) + a;
1282  f = inlined::Mur(b + e * inlined::c1, f) + d;
1283  f += g;
1284  g += f;
1285  s += 20;
1286  } while (--iters != 0);
1287  g = Rotate(g, 11) * inlined::c1;
1288  g = Rotate(g, 17) * inlined::c1;
1289  f = Rotate(f, 11) * inlined::c1;
1290  f = Rotate(f, 17) * inlined::c1;
1291  h = Rotate(h + g, 19);
1292  h = h * 5 + 0xe6546b64;
1293  h = Rotate(h, 17) * inlined::c1;
1294  h = Rotate(h + f, 19);
1295  h = h * 5 + 0xe6546b64;
1296  h = Rotate(h, 17) * inlined::c1;
1297  return h;
1298 }
1299 
1300 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1301  if (len <= 24) {
1302  if (len >= 13) return Hash32Len13to24(s, len, seed * inlined::c1);
1303  else if (len >= 5) return Hash32Len5to12(s, len, seed);
1304  else return Hash32Len0to4(s, len, seed);
1305  }
1306  uint32_t h = Hash32Len13to24(s, 24, seed ^ len);
1307  return inlined::Mur(Hash32(s + 24, len - 24) + seed, h);
1308 }
1309 } // namespace farmhashmk
1310 namespace farmhashsu {
1311 #if !can_use_sse42 || !can_use_aesni
1312 
1313 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1315  return s == NULL ? 0 : len;
1316 }
1317 
1318 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1320  return seed + Hash32(s, len);
1321 }
1322 
1323 #else
1324 
1325 #undef Fetch
1326 #define Fetch inlined::Fetch32
1327 
1328 #undef Rotate
1329 #define Rotate inlined::Rotate32
1330 
1331 #undef Bswap
1332 #define Bswap inlined::Bswap32
1333 
1334 // Helpers for data-parallel operations (4x 32-bits).
1335 STATIC_INLINE __m128i Add(__m128i x, __m128i y) { return _mm_add_epi32(x, y); }
1336 STATIC_INLINE __m128i Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); }
1337 STATIC_INLINE __m128i Or(__m128i x, __m128i y) { return _mm_or_si128(x, y); }
1338 STATIC_INLINE __m128i Mul(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); }
1339 STATIC_INLINE __m128i Mul5(__m128i x) { return Add(x, _mm_slli_epi32(x, 2)); }
1340 STATIC_INLINE __m128i RotateLeft(__m128i x, int c) {
1341  return Or(_mm_slli_epi32(x, c),
1342  _mm_srli_epi32(x, 32 - c));
1343 }
1344 STATIC_INLINE __m128i Rol17(__m128i x) { return RotateLeft(x, 17); }
1345 STATIC_INLINE __m128i Rol19(__m128i x) { return RotateLeft(x, 19); }
1346 STATIC_INLINE __m128i Shuffle0321(__m128i x) {
1347  return _mm_shuffle_epi32(x, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0));
1348 }
1349 
1350 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1351  const uint32_t seed = 81;
1352  if (len <= 24) {
1353  return len <= 12 ?
1354  (len <= 4 ?
1355  farmhashmk::Hash32Len0to4(s, len) :
1356  farmhashmk::Hash32Len5to12(s, len)) :
1357  farmhashmk::Hash32Len13to24(s, len);
1358  }
1359 
1360  if (len < 40) {
1361  uint32_t a = len, b = seed * inlined::c2, c = a + b;
1362  a += Fetch(s + len - 4);
1363  b += Fetch(s + len - 20);
1364  c += Fetch(s + len - 16);
1365  uint32_t d = a;
1366  a = inlined::Rotate32(a, 21);
1367  a = inlined::Mur(a, inlined::Mur(b, _mm_crc32_u32(c, d)));
1368  a += Fetch(s + len - 12);
1369  b += Fetch(s + len - 8);
1370  d += a;
1371  a += d;
1372  b = inlined::Mur(b, d) * inlined::c2;
1373  a = _mm_crc32_u32(a, b + c);
1374  return farmhashmk::Hash32Len13to24(s, (len + 1) / 2, a) + b;
1375  }
1376 
1377 #undef Mulc1
1378 #define Mulc1(x) Mul((x), cc1)
1379 
1380 #undef Mulc2
1381 #define Mulc2(x) Mul((x), cc2)
1382 
1383 #undef Murk
1384 #define Murk(a, h) \
1385  Add(k, \
1386  Mul5( \
1387  Rol19( \
1388  Xor( \
1389  Mulc2( \
1390  Rol17( \
1391  Mulc1(a))), \
1392  (h)))))
1393 
1394  const __m128i cc1 = _mm_set1_epi32(inlined::c1);
1395  const __m128i cc2 = _mm_set1_epi32(inlined::c2);
1396  __m128i h = _mm_set1_epi32(seed);
1397  __m128i g = _mm_set1_epi32(inlined::c1 * seed);
1398  __m128i f = g;
1399  __m128i k = _mm_set1_epi32(0xe6546b64);
1400  __m128i q;
1401  if (len < 80) {
1402  __m128i a = inlined::Fetch128(s);
1403  __m128i b = inlined::Fetch128(s + 16);
1404  __m128i c = inlined::Fetch128(s + (len - 15) / 2);
1405  __m128i d = inlined::Fetch128(s + len - 32);
1406  __m128i e = inlined::Fetch128(s + len - 16);
1407  h = Add(h, a);
1408  g = Add(g, b);
1409  q = g;
1410  g = Shuffle0321(g);
1411  f = Add(f, c);
1412  __m128i be = Add(b, Mulc1(e));
1413  h = Add(h, f);
1414  f = Add(f, h);
1415  h = Add(Murk(d, h), e);
1416  k = Xor(k, _mm_shuffle_epi8(g, f));
1417  g = Add(Xor(c, g), a);
1418  f = Add(Xor(be, f), d);
1419  k = Add(k, be);
1420  k = Add(k, _mm_shuffle_epi8(f, h));
1421  f = Add(f, g);
1422  g = Add(g, f);
1423  g = Add(_mm_set1_epi32(len), Mulc1(g));
1424  } else {
1425  // len >= 80
1426  // The following is loosely modelled after farmhashmk::Hash32.
1427  size_t iters = (len - 1) / 80;
1428  len -= iters * 80;
1429 
1430 #undef Chunk
1431 #define Chunk() do { \
1432  __m128i a = inlined::Fetch128(s); \
1433  __m128i b = inlined::Fetch128(s + 16); \
1434  __m128i c = inlined::Fetch128(s + 32); \
1435  __m128i d = inlined::Fetch128(s + 48); \
1436  __m128i e = inlined::Fetch128(s + 64); \
1437  h = Add(h, a); \
1438  g = Add(g, b); \
1439  g = Shuffle0321(g); \
1440  f = Add(f, c); \
1441  __m128i be = Add(b, Mulc1(e)); \
1442  h = Add(h, f); \
1443  f = Add(f, h); \
1444  h = Add(h, d); \
1445  q = Add(q, e); \
1446  h = Rol17(h); \
1447  h = Mulc1(h); \
1448  k = Xor(k, _mm_shuffle_epi8(g, f)); \
1449  g = Add(Xor(c, g), a); \
1450  f = Add(Xor(be, f), d); \
1451  simpleSwap(f, q); \
1452  q = _mm_aesimc_si128(q); \
1453  k = Add(k, be); \
1454  k = Add(k, _mm_shuffle_epi8(f, h)); \
1455  f = Add(f, g); \
1456  g = Add(g, f); \
1457  f = Mulc1(f); \
1458 } while (0)
1459 
1460  q = g;
1461  while (iters-- != 0) {
1462  Chunk();
1463  s += 80;
1464  }
1465 
1466  if (len != 0) {
1467  h = Add(h, _mm_set1_epi32(len));
1468  s = s + len - 80;
1469  Chunk();
1470  }
1471  }
1472 
1473  g = Shuffle0321(g);
1474  k = Xor(k, g);
1475  k = Xor(k, q);
1476  h = Xor(h, q);
1477  f = Mulc1(f);
1478  k = Mulc2(k);
1479  g = Mulc1(g);
1480  h = Mulc2(h);
1481  k = Add(k, _mm_shuffle_epi8(g, f));
1482  h = Add(h, f);
1483  f = Add(f, h);
1484  g = Add(g, k);
1485  k = Add(k, g);
1486  k = Xor(k, _mm_shuffle_epi8(f, h));
1487  __m128i buf[4];
1488  buf[0] = f;
1489  buf[1] = g;
1490  buf[2] = k;
1491  buf[3] = h;
1492  s = reinterpret_cast<char*>(buf);
1493  uint32_t x = Fetch(s);
1494  uint32_t y = Fetch(s+4);
1495  uint32_t z = Fetch(s+8);
1496  x = _mm_crc32_u32(x, Fetch(s+12));
1497  y = _mm_crc32_u32(y, Fetch(s+16));
1498  z = _mm_crc32_u32(z * inlined::c1, Fetch(s+20));
1499  x = _mm_crc32_u32(x, Fetch(s+24));
1500  y = _mm_crc32_u32(y * inlined::c1, Fetch(s+28));
1501  uint32_t o = y;
1502  z = _mm_crc32_u32(z, Fetch(s+32));
1503  x = _mm_crc32_u32(x * inlined::c1, Fetch(s+36));
1504  y = _mm_crc32_u32(y, Fetch(s+40));
1505  z = _mm_crc32_u32(z * inlined::c1, Fetch(s+44));
1506  x = _mm_crc32_u32(x, Fetch(s+48));
1507  y = _mm_crc32_u32(y * inlined::c1, Fetch(s+52));
1508  z = _mm_crc32_u32(z, Fetch(s+56));
1509  x = _mm_crc32_u32(x, Fetch(s+60));
1510  return (o - x + y - z) * inlined::c1;
1511 }
1512 
1513 #undef Chunk
1514 #undef Murk
1515 #undef Mulc2
1516 #undef Mulc1
1517 
1518 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1519  if (len <= 24) {
1520  if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * inlined::c1);
1521  else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed);
1522  else return farmhashmk::Hash32Len0to4(s, len, seed);
1523  }
1524  uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len);
1525  return _mm_crc32_u32(Hash32(s + 24, len - 24) + seed, h);
1526 }
1527 
1528 #endif
1529 } // namespace farmhashsu
1530 namespace farmhashsa {
1531 #if !can_use_sse42
1532 
1533 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1535  return s == NULL ? 0 : len;
1536 }
1537 
1538 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1540  return seed + Hash32(s, len);
1541 }
1542 
1543 #else
1544 
1545 #undef Fetch
1546 #define Fetch inlined::Fetch32
1547 
1548 #undef Rotate
1549 #define Rotate inlined::Rotate32
1550 
1551 #undef Bswap
1552 #define Bswap inlined::Bswap32
1553 
1554 // Helpers for data-parallel operations (4x 32-bits).
1555 STATIC_INLINE __m128i Add(__m128i x, __m128i y) { return _mm_add_epi32(x, y); }
1556 STATIC_INLINE __m128i Xor(__m128i x, __m128i y) { return _mm_xor_si128(x, y); }
1557 STATIC_INLINE __m128i Or(__m128i x, __m128i y) { return _mm_or_si128(x, y); }
1558 STATIC_INLINE __m128i Mul(__m128i x, __m128i y) { return _mm_mullo_epi32(x, y); }
1559 STATIC_INLINE __m128i Mul5(__m128i x) { return Add(x, _mm_slli_epi32(x, 2)); }
1560 STATIC_INLINE __m128i Rotatesse(__m128i x, int c) {
1561  return Or(_mm_slli_epi32(x, c),
1562  _mm_srli_epi32(x, 32 - c));
1563 }
1564 STATIC_INLINE __m128i Rot17(__m128i x) { return Rotatesse(x, 17); }
1565 STATIC_INLINE __m128i Rot19(__m128i x) { return Rotatesse(x, 19); }
1566 STATIC_INLINE __m128i Shuffle0321(__m128i x) {
1567  return _mm_shuffle_epi32(x, (0 << 6) + (3 << 4) + (2 << 2) + (1 << 0));
1568 }
1569 
1570 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1571  const uint32_t seed = 81;
1572  if (len <= 24) {
1573  return len <= 12 ?
1574  (len <= 4 ?
1575  farmhashmk::Hash32Len0to4(s, len) :
1576  farmhashmk::Hash32Len5to12(s, len)) :
1577  farmhashmk::Hash32Len13to24(s, len);
1578  }
1579 
1580  if (len < 40) {
1581  uint32_t a = len, b = seed * inlined::c2, c = a + b;
1582  a += Fetch(s + len - 4);
1583  b += Fetch(s + len - 20);
1584  c += Fetch(s + len - 16);
1585  uint32_t d = a;
1586  a = inlined::Rotate32(a, 21);
1587  a = inlined::Mur(a, inlined::Mur(b, inlined::Mur(c, d)));
1588  a += Fetch(s + len - 12);
1589  b += Fetch(s + len - 8);
1590  d += a;
1591  a += d;
1592  b = inlined::Mur(b, d) * inlined::c2;
1593  a = _mm_crc32_u32(a, b + c);
1594  return farmhashmk::Hash32Len13to24(s, (len + 1) / 2, a) + b;
1595  }
1596 
1597 #undef Mulc1
1598 #define Mulc1(x) Mul((x), cc1)
1599 
1600 #undef Mulc2
1601 #define Mulc2(x) Mul((x), cc2)
1602 
1603 #undef Murk
1604 #define Murk(a, h) \
1605  Add(k, \
1606  Mul5( \
1607  Rot19( \
1608  Xor( \
1609  Mulc2( \
1610  Rot17( \
1611  Mulc1(a))), \
1612  (h)))))
1613 
1614  const __m128i cc1 = _mm_set1_epi32(inlined::c1);
1615  const __m128i cc2 = _mm_set1_epi32(inlined::c2);
1616  __m128i h = _mm_set1_epi32(seed);
1617  __m128i g = _mm_set1_epi32(inlined::c1 * seed);
1618  __m128i f = g;
1619  __m128i k = _mm_set1_epi32(0xe6546b64);
1620  if (len < 80) {
1621  __m128i a = inlined::Fetch128(s);
1622  __m128i b = inlined::Fetch128(s + 16);
1623  __m128i c = inlined::Fetch128(s + (len - 15) / 2);
1624  __m128i d = inlined::Fetch128(s + len - 32);
1625  __m128i e = inlined::Fetch128(s + len - 16);
1626  h = Add(h, a);
1627  g = Add(g, b);
1628  g = Shuffle0321(g);
1629  f = Add(f, c);
1630  __m128i be = Add(b, Mulc1(e));
1631  h = Add(h, f);
1632  f = Add(f, h);
1633  h = Add(Murk(d, h), e);
1634  k = Xor(k, _mm_shuffle_epi8(g, f));
1635  g = Add(Xor(c, g), a);
1636  f = Add(Xor(be, f), d);
1637  k = Add(k, be);
1638  k = Add(k, _mm_shuffle_epi8(f, h));
1639  f = Add(f, g);
1640  g = Add(g, f);
1641  g = Add(_mm_set1_epi32(len), Mulc1(g));
1642  } else {
1643  // len >= 80
1644  // The following is loosely modelled after farmhashmk::Hash32.
1645  size_t iters = (len - 1) / 80;
1646  len -= iters * 80;
1647 
1648 #undef Chunk
1649 #define Chunk() do { \
1650  __m128i a = inlined::Fetch128(s); \
1651  __m128i b = inlined::Fetch128(s + 16); \
1652  __m128i c = inlined::Fetch128(s + 32); \
1653  __m128i d = inlined::Fetch128(s + 48); \
1654  __m128i e = inlined::Fetch128(s + 64); \
1655  h = Add(h, a); \
1656  g = Add(g, b); \
1657  g = Shuffle0321(g); \
1658  f = Add(f, c); \
1659  __m128i be = Add(b, Mulc1(e)); \
1660  h = Add(h, f); \
1661  f = Add(f, h); \
1662  h = Add(Murk(d, h), e); \
1663  k = Xor(k, _mm_shuffle_epi8(g, f)); \
1664  g = Add(Xor(c, g), a); \
1665  f = Add(Xor(be, f), d); \
1666  k = Add(k, be); \
1667  k = Add(k, _mm_shuffle_epi8(f, h)); \
1668  f = Add(f, g); \
1669  g = Add(g, f); \
1670  f = Mulc1(f); \
1671 } while (0)
1672 
1673  while (iters-- != 0) {
1674  Chunk();
1675  s += 80;
1676  }
1677 
1678  if (len != 0) {
1679  h = Add(h, _mm_set1_epi32(len));
1680  s = s + len - 80;
1681  Chunk();
1682  }
1683  }
1684 
1685  g = Shuffle0321(g);
1686  k = Xor(k, g);
1687  f = Mulc1(f);
1688  k = Mulc2(k);
1689  g = Mulc1(g);
1690  h = Mulc2(h);
1691  k = Add(k, _mm_shuffle_epi8(g, f));
1692  h = Add(h, f);
1693  f = Add(f, h);
1694  g = Add(g, k);
1695  k = Add(k, g);
1696  k = Xor(k, _mm_shuffle_epi8(f, h));
1697  __m128i buf[4];
1698  buf[0] = f;
1699  buf[1] = g;
1700  buf[2] = k;
1701  buf[3] = h;
1702  s = reinterpret_cast<char*>(buf);
1703  uint32_t x = Fetch(s);
1704  uint32_t y = Fetch(s+4);
1705  uint32_t z = Fetch(s+8);
1706  x = _mm_crc32_u32(x, Fetch(s+12));
1707  y = _mm_crc32_u32(y, Fetch(s+16));
1708  z = _mm_crc32_u32(z * inlined::c1, Fetch(s+20));
1709  x = _mm_crc32_u32(x, Fetch(s+24));
1710  y = _mm_crc32_u32(y * inlined::c1, Fetch(s+28));
1711  uint32_t o = y;
1712  z = _mm_crc32_u32(z, Fetch(s+32));
1713  x = _mm_crc32_u32(x * inlined::c1, Fetch(s+36));
1714  y = _mm_crc32_u32(y, Fetch(s+40));
1715  z = _mm_crc32_u32(z * inlined::c1, Fetch(s+44));
1716  x = _mm_crc32_u32(x, Fetch(s+48));
1717  y = _mm_crc32_u32(y * inlined::c1, Fetch(s+52));
1718  z = _mm_crc32_u32(z, Fetch(s+56));
1719  x = _mm_crc32_u32(x, Fetch(s+60));
1720  return (o - x + y - z) * inlined::c1;
1721 }
1722 
1723 #undef Chunk
1724 #undef Murk
1725 #undef Mulc2
1726 #undef Mulc1
1727 
1728 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1729  if (len <= 24) {
1730  if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * inlined::c1);
1731  else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed);
1732  else return farmhashmk::Hash32Len0to4(s, len, seed);
1733  }
1734  uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len);
1735  return _mm_crc32_u32(Hash32(s + 24, len - 24) + seed, h);
1736 }
1737 
1738 #endif
1739 } // namespace farmhashsa
1740 namespace farmhashcc {
1741 // This file provides a 32-bit hash equivalent to CityHash32 (v1.1.1)
1742 // and a 128-bit hash equivalent to CityHash128 (v1.1.1). It also provides
1743 // a seeded 32-bit hash function similar to CityHash32.
1744 
1745 #undef Fetch
1746 #define Fetch inlined::Fetch32
1747 
1748 #undef Rotate
1749 #define Rotate inlined::Rotate32
1750 
1751 #undef Bswap
1752 #define Bswap inlined::Bswap32
1753 
1754 #undef fmix
1755 #define fmix farmhash::inlined::fmix
1756 
1757 STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len) {
1758  uint32_t a = Fetch(s - 4 + (len >> 1));
1759  uint32_t b = Fetch(s + 4);
1760  uint32_t c = Fetch(s + len - 8);
1761  uint32_t d = Fetch(s + (len >> 1));
1762  uint32_t e = Fetch(s);
1763  uint32_t f = Fetch(s + len - 4);
1764  uint32_t h = len;
1765 
1767 }
1768 
1769 STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len) {
1770  uint32_t b = 0;
1771  uint32_t c = 9;
1772  for (size_t i = 0; i < len; i++) {
1773  signed char v = s[i];
1774  b = b * inlined::c1 + v;
1775  c ^= b;
1776  }
1777  return fmix(inlined::Mur(b, inlined::Mur(len, c)));
1778 }
1779 
1780 STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len) {
1781  uint32_t a = len, b = len * 5, c = 9, d = b;
1782  a += Fetch(s);
1783  b += Fetch(s + len - 4);
1784  c += Fetch(s + ((len >> 1) & 4));
1785  return fmix(inlined::Mur(c, inlined::Mur(b, inlined::Mur(a, d))));
1786 }
1787 
1788 STATIC_INLINE uint32_t Hash32(const char *s, size_t len) {
1789  if (len <= 24) {
1790  return len <= 12 ?
1791  (len <= 4 ? Hash32Len0to4(s, len) : Hash32Len5to12(s, len)) :
1792  Hash32Len13to24(s, len);
1793  }
1794 
1795  // len > 24
1796  uint32_t h = len, g = inlined::c1 * len, f = g;
1797  uint32_t a0 = Rotate(Fetch(s + len - 4) * inlined::c1, 17) * inlined::c2;
1798  uint32_t a1 = Rotate(Fetch(s + len - 8) * inlined::c1, 17) * inlined::c2;
1799  uint32_t a2 = Rotate(Fetch(s + len - 16) * inlined::c1, 17) * inlined::c2;
1800  uint32_t a3 = Rotate(Fetch(s + len - 12) * inlined::c1, 17) * inlined::c2;
1801  uint32_t a4 = Rotate(Fetch(s + len - 20) * inlined::c1, 17) * inlined::c2;
1802  h ^= a0;
1803  h = Rotate(h, 19);
1804  h = h * 5 + 0xe6546b64;
1805  h ^= a2;
1806  h = Rotate(h, 19);
1807  h = h * 5 + 0xe6546b64;
1808  g ^= a1;
1809  g = Rotate(g, 19);
1810  g = g * 5 + 0xe6546b64;
1811  g ^= a3;
1812  g = Rotate(g, 19);
1813  g = g * 5 + 0xe6546b64;
1814  f += a4;
1815  f = Rotate(f, 19);
1816  f = f * 5 + 0xe6546b64;
1817  size_t iters = (len - 1) / 20;
1818  do {
1819  uint32_t a0 = Rotate(Fetch(s) * inlined::c1, 17) * inlined::c2;
1820  uint32_t a1 = Fetch(s + 4);
1821  uint32_t a2 = Rotate(Fetch(s + 8) * inlined::c1, 17) * inlined::c2;
1822  uint32_t a3 = Rotate(Fetch(s + 12) * inlined::c1, 17) * inlined::c2;
1823  uint32_t a4 = Fetch(s + 16);
1824  h ^= a0;
1825  h = Rotate(h, 18);
1826  h = h * 5 + 0xe6546b64;
1827  f += a1;
1828  f = Rotate(f, 19);
1829  f = f * inlined::c1;
1830  g += a2;
1831  g = Rotate(g, 18);
1832  g = g * 5 + 0xe6546b64;
1833  h ^= a3 + a1;
1834  h = Rotate(h, 19);
1835  h = h * 5 + 0xe6546b64;
1836  g ^= a4;
1837  g = Bswap(g) * 5;
1838  h += a4 * 5;
1839  h = Bswap(h);
1840  f += a0;
1841  PERMUTE3(f, h, g);
1842  s += 20;
1843  } while (--iters != 0);
1844  g = Rotate(g, 11) * inlined::c1;
1845  g = Rotate(g, 17) * inlined::c1;
1846  f = Rotate(f, 11) * inlined::c1;
1847  f = Rotate(f, 17) * inlined::c1;
1848  h = Rotate(h + g, 19);
1849  h = h * 5 + 0xe6546b64;
1850  h = Rotate(h, 17) * inlined::c1;
1851  h = Rotate(h + f, 19);
1852  h = h * 5 + 0xe6546b64;
1853  h = Rotate(h, 17) * inlined::c1;
1854  return h;
1855 }
1856 
1857 STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed) {
1858  if (len <= 24) {
1859  if (len >= 13) return farmhashmk::Hash32Len13to24(s, len, seed * inlined::c1);
1860  else if (len >= 5) return farmhashmk::Hash32Len5to12(s, len, seed);
1861  else return farmhashmk::Hash32Len0to4(s, len, seed);
1862  }
1863  uint32_t h = farmhashmk::Hash32Len13to24(s, 24, seed ^ len);
1864  return inlined::Mur(Hash32(s + 24, len - 24) + seed, h);
1865 }
1866 
1867 #undef Fetch
1868 #define Fetch farmhash::inlined::Fetch64
1869 
1870 #undef Rotate
1871 #define Rotate inlined::Rotate64
1872 
1873 #undef Bswap
1874 #define Bswap inlined::Bswap64
1875 
1876 #undef DebugTweak
1877 #define DebugTweak farmhash::inlined::DebugTweak
1878 
1879 STATIC_INLINE uint64_t ShiftMix(uint64_t val) {
1880  return val ^ (val >> 47);
1881 }
1882 
1883 STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v) {
1884  return Hash128to64(Uint128(u, v));
1885 }
1886 
1887 STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul) {
1888  // Murmur-inspired hashing.
1889  uint64_t a = (u ^ v) * mul;
1890  a ^= (a >> 47);
1891  uint64_t b = (v ^ a) * mul;
1892  b ^= (b >> 47);
1893  b *= mul;
1894  return b;
1895 }
1896 
1897 STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len) {
1898  if (len >= 8) {
1899  uint64_t mul = inlined::k2 + len * 2;
1900  uint64_t a = Fetch(s) + inlined::k2;
1901  uint64_t b = Fetch(s + len - 8);
1902  uint64_t c = Rotate(b, 37) * mul + a;
1903  uint64_t d = (Rotate(a, 25) + b) * mul;
1904  return HashLen16(c, d, mul);
1905  }
1906  if (len >= 4) {
1907  uint64_t mul = inlined::k2 + len * 2;
1908  uint64_t a = inlined::Fetch32(s);
1909  return HashLen16(len + (a << 3), inlined::Fetch32(s + len - 4), mul);
1910  }
1911  if (len > 0) {
1912  uint8_t a = s[0];
1913  uint8_t b = s[len >> 1];
1914  uint8_t c = s[len - 1];
1915  uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
1916  uint32_t z = len + (static_cast<uint32_t>(c) << 2);
1917  return ShiftMix(y * inlined::k2 ^ z * inlined::k0) * inlined::k2;
1918  }
1919  return inlined::k2;
1920 }
1921 
1922 // Return a 16-byte hash for 48 bytes. Quick and dirty.
1923 // Callers do best to use "random-looking" values for a and b.
1924 STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
1925  uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b) {
1926  a += w;
1927  b = Rotate(b + a + z, 21);
1928  uint64_t c = a;
1929  a += x;
1930  a += y;
1931  b += Rotate(a, 44);
1932  return make_pair(a + z, b + c);
1933 }
1934 
1935 // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty.
1936 STATIC_INLINE pair<uint64_t, uint64_t> WeakHashLen32WithSeeds(
1937  const char* s, uint64_t a, uint64_t b) {
1938  return WeakHashLen32WithSeeds(Fetch(s),
1939  Fetch(s + 8),
1940  Fetch(s + 16),
1941  Fetch(s + 24),
1942  a,
1943  b);
1944 }
1945 
1946 
1947 
1948 // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings
1949 // of any length representable in signed long. Based on City and Murmur.
1950 STATIC_INLINE uint128_t CityMurmur(const char *s, size_t len, uint128_t seed) {
1951  uint64_t a = Uint128Low64(seed);
1952  uint64_t b = Uint128High64(seed);
1953  uint64_t c = 0;
1954  uint64_t d = 0;
1955  signed long l = len - 16;
1956  if (l <= 0) { // len <= 16
1957  a = ShiftMix(a * inlined::k1) * inlined::k1;
1958  c = b * inlined::k1 + HashLen0to16(s, len);
1959  d = ShiftMix(a + (len >= 8 ? Fetch(s) : c));
1960  } else { // len > 16
1961  c = HashLen16(Fetch(s + len - 8) + inlined::k1, a);
1962  d = HashLen16(b + len, c + Fetch(s + len - 16));
1963  a += d;
1964  do {
1965  a ^= ShiftMix(Fetch(s) * inlined::k1) * inlined::k1;
1966  a *= inlined::k1;
1967  b ^= a;
1968  c ^= ShiftMix(Fetch(s + 8) * inlined::k1) * inlined::k1;
1969  c *= inlined::k1;
1970  d ^= c;
1971  s += 16;
1972  l -= 16;
1973  } while (l > 0);
1974  }
1975  a = HashLen16(a, c);
1976  b = HashLen16(d, b);
1977  return uint128_t(a ^ b, HashLen16(b, a));
1978 }
1979 
1980 STATIC_INLINE uint128_t CityHash128WithSeed(const char *s, size_t len, uint128_t seed) {
1981  if (len < 128) {
1982  return CityMurmur(s, len, seed);
1983  }
1984 
1985  // We expect len >= 128 to be the common case. Keep 56 bytes of state:
1986  // v, w, x, y, and z.
1987  pair<uint64_t, uint64_t> v, w;
1988  uint64_t x = Uint128Low64(seed);
1989  uint64_t y = Uint128High64(seed);
1990  uint64_t z = len * inlined::k1;
1991  v.first = Rotate(y ^ inlined::k1, 49) * inlined::k1 + Fetch(s);
1992  v.second = Rotate(v.first, 42) * inlined::k1 + Fetch(s + 8);
1993  w.first = Rotate(y + z, 35) * inlined::k1 + x;
1994  w.second = Rotate(x + Fetch(s + 88), 53) * inlined::k1;
1995 
1996  // This is the same inner loop as CityHash64(), manually unrolled.
1997  do {
1998  x = Rotate(x + y + v.first + Fetch(s + 8), 37) * inlined::k1;
1999  y = Rotate(y + v.second + Fetch(s + 48), 42) * inlined::k1;
2000  x ^= w.second;
2001  y += v.first + Fetch(s + 40);
2002  z = Rotate(z + w.first, 33) * inlined::k1;
2003  CopyUint128(v, WeakHashLen32WithSeeds(s, v.second * inlined::k1, x + w.first));
2004  CopyUint128(w, WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)));
2005  simpleSwap(z, x);
2006  s += 64;
2007  x = Rotate(x + y + v.first + Fetch(s + 8), 37) * inlined::k1;
2008  y = Rotate(y + v.second + Fetch(s + 48), 42) * inlined::k1;
2009  x ^= w.second;
2010  y += v.first + Fetch(s + 40);
2011  z = Rotate(z + w.first, 33) * inlined::k1;
2012  CopyUint128(v, WeakHashLen32WithSeeds(s, v.second * inlined::k1, x + w.first));
2013  CopyUint128(w, WeakHashLen32WithSeeds(s + 32, z + w.second, y + Fetch(s + 16)));
2014  simpleSwap(z, x);
2015  s += 64;
2016  len -= 128;
2017  } while (OIIO_LIKELY(len >= 128));
2018  x += Rotate(v.first + z, 49) * inlined::k0;
2019  y = y * inlined::k0 + Rotate(w.second, 37);
2020  z = z * inlined::k0 + Rotate(w.first, 27);
2021  w.first *= 9;
2022  v.first *= inlined::k0;
2023  // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s.
2024  for (size_t tail_done = 0; tail_done < len; ) {
2025  tail_done += 32;
2026  y = Rotate(x + y, 42) * inlined::k0 + v.second;
2027  w.first += Fetch(s + len - tail_done + 16);
2028  x = x * inlined::k0 + w.first;
2029  z += w.second + Fetch(s + len - tail_done);
2030  w.second += v.first;
2031  CopyUint128(v, WeakHashLen32WithSeeds(s + len - tail_done, v.first + z, v.second));
2032  v.first *= inlined::k0;
2033  }
2034  // At this point our 56 bytes of state should contain more than
2035  // enough information for a strong 128-bit hash. We use two
2036  // different 56-byte-to-8-byte hashes to get a 16-byte final result.
2037  x = HashLen16(x, v.first);
2038  y = HashLen16(y + z, w.first);
2039  return uint128_t(HashLen16(x + v.second, w.second) + y,
2040  HashLen16(x + w.second, y + v.second));
2041 }
2042 
2043 STATIC_INLINE uint128_t CityHash128(const char *s, size_t len) {
2044  return len >= 16 ?
2045  CityHash128WithSeed(s + 16, len - 16,
2046  uint128_t(Fetch(s), Fetch(s + 8) + inlined::k0)) :
2047  CityHash128WithSeed(s, len, uint128_t(inlined::k0, inlined::k1));
2048 }
2049 
2050 uint128_t STATIC_INLINE Fingerprint128(const char* s, size_t len) {
2051  return CityHash128(s, len);
2052 }
2053 } // namespace farmhashcc
2054 
2055 
2056 // namespace NAMESPACE_FOR_HASH_FUNCTIONS {
2057 namespace inlined {
2058 
2059 
2060 // BASIC STRING HASHING
2061 
2062 // Hash function for a byte array. See also Hash(), below.
2063 // May change from time to time, may differ on different platforms, may differ
2064 // depending on NDEBUG.
2065 STATIC_INLINE uint32_t Hash32(const char* s, size_t len) {
2066  return DebugTweak(
2067  (can_use_sse41 & x86_64) ? farmhashnt::Hash32(s, len) :
2069  can_use_sse42 ? farmhashsa::Hash32(s, len) :
2070  farmhashmk::Hash32(s, len));
2071 }
2072 
2073 // Hash function for a byte array. For convenience, a 32-bit seed is also
2074 // hashed into the result.
2075 // May change from time to time, may differ on different platforms, may differ
2076 // depending on NDEBUG.
2077 STATIC_INLINE uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed) {
2078  return DebugTweak(
2079  (can_use_sse41 & x86_64) ? farmhashnt::Hash32WithSeed(s, len, seed) :
2081  can_use_sse42 ? farmhashsa::Hash32WithSeed(s, len, seed) :
2082  farmhashmk::Hash32WithSeed(s, len, seed));
2083 }
2084 
2085 // Hash function for a byte array. For convenience, a 64-bit seed is also
2086 // hashed into the result. See also Hash(), below.
2087 // May change from time to time, may differ on different platforms, may differ
2088 // depending on NDEBUG.
2089 STATIC_INLINE uint64_t Hash64(const char* s, size_t len) {
2090  return DebugTweak(
2091  (can_use_sse42 & x86_64) ?
2092  farmhashte::Hash64(s, len) :
2093  farmhashxo::Hash64(s, len));
2094 }
2095 
2096 // Hash function for a byte array.
2097 // May change from time to time, may differ on different platforms, may differ
2098 // depending on NDEBUG.
2099 STATIC_INLINE size_t Hash(const char* s, size_t len) {
2100  return sizeof(size_t) == 8 ? size_t(Hash64(s, len)) : size_t(Hash32(s, len));
2101 }
2102 
2103 // Hash function for a byte array. For convenience, a 64-bit seed is also
2104 // hashed into the result.
2105 // May change from time to time, may differ on different platforms, may differ
2106 // depending on NDEBUG.
2107 STATIC_INLINE uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed) {
2108  return DebugTweak(farmhashna::Hash64WithSeed(s, len, seed));
2109 }
2110 
2111 // Hash function for a byte array. For convenience, two seeds are also
2112 // hashed into the result.
2113 // May change from time to time, may differ on different platforms, may differ
2114 // depending on NDEBUG.
2115 STATIC_INLINE uint64_t Hash64WithSeeds(const char* s, size_t len, uint64_t seed0, uint64_t seed1) {
2116  return DebugTweak(farmhashna::Hash64WithSeeds(s, len, seed0, seed1));
2117 }
2118 
2119 // Hash function for a byte array.
2120 // May change from time to time, may differ on different platforms, may differ
2121 // depending on NDEBUG.
2122 STATIC_INLINE uint128_t Hash128(const char* s, size_t len) {
2123  return DebugTweak(farmhashcc::Fingerprint128(s, len));
2124 }
2125 
2126 // Hash function for a byte array. For convenience, a 128-bit seed is also
2127 // hashed into the result.
2128 // May change from time to time, may differ on different platforms, may differ
2129 // depending on NDEBUG.
2130 STATIC_INLINE uint128_t Hash128WithSeed(const char* s, size_t len, uint128_t seed) {
2131  return DebugTweak(farmhashcc::CityHash128WithSeed(s, len, seed));
2132 }
2133 
2134 // BASIC NON-STRING HASHING
2135 
2136 // FINGERPRINTING (i.e., good, portable, forever-fixed hash functions)
2137 
2138 // Fingerprint function for a byte array. Most useful in 32-bit binaries.
2139 STATIC_INLINE uint32_t Fingerprint32(const char* s, size_t len) {
2140  return farmhashmk::Hash32(s, len);
2141 }
2142 
2143 // Fingerprint function for a byte array.
2144 STATIC_INLINE uint64_t Fingerprint64(const char* s, size_t len) {
2145  return farmhashna::Hash64(s, len);
2146 }
2147 
2148 // Fingerprint function for a byte array.
2149 STATIC_INLINE uint128_t Fingerprint128(const char* s, size_t len) {
2150  return farmhashcc::Fingerprint128(s, len);
2151 }
2152 
2153 // Older and still available but perhaps not as fast as the above:
2154 // farmhashns::Hash32{,WithSeed}()
2155 
2156 } /*end namespace inlined*/
2157 // } // namespace NAMESPACE_FOR_HASH_FUNCTIONS
2158 } /*end namespace farmhash*/
2159 
2160 // Undefine any of the poorly namespaced things
2161 #undef Fetch
2162 #undef Rotate
2163 #undef Bswap
2164 #undef fmix
2165 #undef DebugTweak
2166 #undef uint128_t
2167 #undef Uint128
2168 #undef CopyUint128
2169 #undef Uint128Low64
2170 #undef Uint128High64
2171 #undef Hash128to64
2172 #undef Hash64WithSeeds
2173 #undef x86
2174 #undef x86_64
2175 #undef is_64bit
2176 #undef can_use_ssse3
2177 #undef can_use_sse41
2178 #undef can_use_sse42
2179 #undef can_use_aesni
2180 #undef can_use_avx
2181 #undef bswap_32
2182 #undef bswap_64
2183 #undef STATIC_INLINE
2184 #undef uint32_in_expected_order
2185 #undef uint64_in_expected_order
2186 #undef debug_mode
2187 #undef PERMUTE3
2188 #undef Mulc1
2189 #undef Mulc2
2190 #undef Murk
2191 #undef Chunk
2192 
2194 
2195 #undef STATIC_INLINE
2196 
2197 
STATIC_INLINE uint32_t Hash32(const char *s, size_t len)
Definition: farmhash.h:1242
STATIC_INLINE uint128_t CityHash128WithSeed(const char *s, size_t len, uint128_t seed)
Definition: farmhash.h:1980
#define OIIO_LIKELY(x)
Definition: platform.h:387
STATIC_INLINE uint32_t Hash32(const char *s, size_t len)
Definition: farmhash.h:1170
STATIC_INLINE pair< uint64_t, uint64_t > WeakHashLen32WithSeeds(const char *s, uint64_t a, uint64_t b)
Definition: farmhash.h:1936
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
#define STATIC_INLINE
Definition: farmhash.h:89
STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed)
Definition: farmhash.h:1318
STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed)
Definition: farmhash.h:1538
STATIC_INLINE uint32_t Hash32(const char *s, size_t len)
Definition: farmhash.h:1533
#define Rotate
Definition: farmhash.h:1871
STATIC_INLINE uint32_t fmix(uint32_t h)
Definition: farmhash.h:538
uint128_t STATIC_INLINE Fingerprint128(const char *s, size_t len)
Definition: farmhash.h:2050
STATIC_INLINE uint64_t Hash64(const char *s, size_t len)
Definition: farmhash.h:865
OIIO_HOSTDEVICE constexpr uint64_t Uint128Low64(const uint128_t x)
Definition: hash.h:331
const GLdouble * v
Definition: glcorearb.h:837
#define x86_64
Definition: farmhash.h:412
uint128_t OIIO_API Hash128WithSeed(const char *s, size_t len, uint128_t seed)
Definition: farmhash.h:2130
STATIC_INLINE uint64_t HashLen33to64(const char *s, size_t len)
Definition: farmhash.h:891
STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed)
Definition: farmhash.h:943
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLboolean GLboolean g
Definition: glcorearb.h:1222
#define can_use_sse42
Definition: farmhash.h:456
STATIC_INLINE uint32_t Bswap32(uint32_t val)
Definition: farmhash.h:346
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len)
Definition: farmhash.h:1757
STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v)
Definition: farmhash.h:599
GLint y
Definition: glcorearb.h:103
**But if you need a result
Definition: thread.h:622
GLdouble GLdouble GLdouble q
Definition: glad.h:2445
OIIO_HOSTDEVICE OIIO_CONSTEXPR14 uint64_t Hash128to64(uint128_t x)
Definition: hash.h:405
STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed)
Definition: farmhash.h:1300
STATIC_INLINE uint64_t HashLen65to96(const char *s, size_t len)
Definition: farmhash.h:900
uint64_t OIIO_API Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1)
Definition: farmhash.h:746
size_t OIIO_API Hash(const char *s, size_t len)
Definition: farmhash.h:2099
STATIC_INLINE uint32_t Rotate32(uint32_t val, int shift)
Definition: farmhash.h:377
OIIO_HOSTDEVICE OIIO_CONSTEXPR14 uint128_t Uint128(uint64_t lo, uint64_t hi)
Definition: hash.h:339
STATIC_INLINE uint64_t H32(const char *s, size_t len, uint64_t mul, uint64_t seed0=0, uint64_t seed1=0)
Definition: farmhash.h:877
STATIC_INLINE uint32_t Fetch32(const char *p)
Definition: farmhash.h:239
STATIC_INLINE T DebugTweak(T x)
Definition: farmhash.h:558
#define can_use_sse41
Definition: farmhash.h:445
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
STATIC_INLINE uint32_t BasicRotate32(uint32_t val, int shift)
Definition: farmhash.h:351
#define Bswap
Definition: farmhash.h:1874
#define DebugTweak
Definition: farmhash.h:1877
STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len)
Definition: farmhash.h:1780
STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1)
Definition: farmhash.h:769
STATIC_INLINE uint32_t Mur(uint32_t a, uint32_t h)
Definition: farmhash.h:548
STATIC_INLINE uint64_t ShiftMix(uint64_t val)
Definition: farmhash.h:1879
GLuint GLuint end
Definition: glcorearb.h:475
#define uint64_in_expected_order(x)
Definition: farmhash.h:203
#define can_use_aesni
Definition: farmhash.h:467
STATIC_INLINE uint128_t CityHash128(const char *s, size_t len)
Definition: farmhash.h:2043
GLint GLuint mask
Definition: glcorearb.h:124
STATIC_INLINE uint32_t Hash32Len5to12(const char *s, size_t len, uint32_t seed=0)
Definition: farmhash.h:1234
OIIO_HOSTDEVICE constexpr uint64_t Uint128High64(const uint128_t x)
Definition: hash.h:335
STATIC_INLINE uint64_t H(uint64_t x, uint64_t y, uint64_t mul, int r)
Definition: farmhash.h:762
#define uint32_in_expected_order(x)
Definition: farmhash.h:202
STATIC_INLINE uint64_t Hash64(const char *s, size_t len)
Definition: farmhash.h:909
STATIC_INLINE uint64_t HashLen17to32(const char *s, size_t len)
Definition: farmhash.h:640
STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed)
Definition: farmhash.h:750
uint64_t OIIO_API Hash64(const char *s, size_t len)
Definition: farmhash.h:691
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
uint32_t OIIO_API Fingerprint32(const char *s, size_t len)
Definition: farmhash.h:2139
STATIC_INLINE uint32_t Hash32Len13to24(const char *s, size_t len, uint32_t seed=0)
Definition: farmhash.h:1206
STATIC_INLINE uint64_t Rotate64(uint64_t val, int shift)
Definition: farmhash.h:380
STATIC_INLINE uint64_t Bswap64(uint64_t val)
Definition: farmhash.h:347
GLdouble t
Definition: glad.h:2397
uint128_t OIIO_API Hash128(const char *s, size_t len)
Definition: farmhash.h:2122
#define PERMUTE3(a, b, c)
Definition: farmhash.h:511
uint64_t OIIO_API Fingerprint64(const char *s, size_t len)
Definition: farmhash.h:2144
STATIC_INLINE uint32_t Hash32WithSeed(const char *s, size_t len, uint32_t seed)
Definition: farmhash.h:1175
STATIC_INLINE uint32_t Hash32(const char *s, size_t len)
Definition: farmhash.h:1313
STATIC_INLINE pair< uint64_t, uint64_t > WeakHashLen32WithSeeds(uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b)
Definition: farmhash.h:652
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len)
Definition: farmhash.h:1897
STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len)
Definition: farmhash.h:1769
STATIC_INLINE void simpleSwap(T &a, T &b)
Definition: farmhash.h:212
FMT_CONSTEXPR20 auto bit_cast(const From &from) -> To
Definition: format.h:336
STATIC_INLINE uint64_t HashLen0to16(const char *s, size_t len)
Definition: farmhash.h:613
STATIC_INLINE uint128_t CityMurmur(const char *s, size_t len, uint128_t seed)
Definition: farmhash.h:1950
uint64_t OIIO_API Hash64WithSeed(const char *s, size_t len, uint64_t seed)
Definition: farmhash.h:750
STATIC_INLINE uint64_t Hash64(const char *s, size_t len)
Definition: farmhash.h:691
#define fmix
Definition: farmhash.h:1755
GLuint GLfloat * val
Definition: glcorearb.h:1608
STATIC_INLINE uint64_t Fetch64(const char *p)
Definition: farmhash.h:233
OIIO_FORCEINLINE constexpr bool littleendian(void) noexcept
Return true if the architecture we are running on is little endian.
Definition: platform.h:577
#define Fetch
Definition: farmhash.h:1868
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
GLboolean r
Definition: glcorearb.h:1222
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:127
STATIC_INLINE uint64_t HashLen16(uint64_t u, uint64_t v, uint64_t mul)
Definition: farmhash.h:1887
#define FARMHASH_DIE_IF_MISCONFIGURED
Definition: farmhash.h:97
OIIO_HOSTDEVICE OIIO_CONSTEXPR14 void CopyUint128(uint128_t &dst, const uint128_t src)
Definition: hash.h:344
STATIC_INLINE uint64_t BasicRotate64(uint64_t val, int shift)
Definition: farmhash.h:356
STATIC_INLINE uint64_t Hash64WithSeeds(const char *s, size_t len, uint64_t seed0, uint64_t seed1)
Definition: farmhash.h:746
uint32_t OIIO_API Hash32WithSeed(const char *s, size_t len, uint32_t seed)
Definition: farmhash.h:1175
STATIC_INLINE uint64_t Hash64(const char *s, size_t len)
Definition: farmhash.h:938
uint32_t OIIO_API Hash32(const char *s, size_t len)
Definition: farmhash.h:1170
std::pair< uint64_t, uint64_t > uint128_t
Definition: hash.h:330
#define debug_mode
Definition: farmhash.h:403
Definition: format.h:4365
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:126
ImageBuf OIIO_API mul(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
STATIC_INLINE uint64_t ShiftMix(uint64_t val)
Definition: farmhash.h:595
STATIC_INLINE uint64_t Hash64WithSeed(const char *s, size_t len, uint64_t seed)
Definition: farmhash.h:860
uint128_t OIIO_API Fingerprint128(const char *s, size_t len)
Definition: farmhash.h:2050
STATIC_INLINE uint32_t Hash32Len0to4(const char *s, size_t len, uint32_t seed=0)
Definition: farmhash.h:1223