microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
cmake/externals/farmhash/dev/basics.cc
68lines · modecode
| 1 | // Building blocks for hash functions |
| 2 | |
| 3 | // std::swap() was in <algorithm> but is in <utility> from C++11 on. |
| 4 | #if !FARMHASH_CAN_USE_CXX11 |
| 5 | #include <algorithm> |
| 6 | #endif |
| 7 | |
| 8 | #undef PERMUTE3 |
| 9 | #define PERMUTE3(a, b, c) do { std::swap(a, b); std::swap(a, c); } while (0) |
| 10 | |
| 11 | namespace NAMESPACE_FOR_HASH_FUNCTIONS { |
| 12 | |
| 13 | // Some primes between 2^63 and 2^64 for various uses. |
| 14 | static const uint64_t k0 = 0xc3a5c85c97cb3127ULL; |
| 15 | static const uint64_t k1 = 0xb492b66fbe98f273ULL; |
| 16 | static const uint64_t k2 = 0x9ae16a3b2f90404fULL; |
| 17 | |
| 18 | // Magic numbers for 32-bit hashing. Copied from Murmur3. |
| 19 | static const uint32_t c1 = 0xcc9e2d51; |
| 20 | static const uint32_t c2 = 0x1b873593; |
| 21 | |
| 22 | // A 32-bit to 32-bit integer hash copied from Murmur3. |
| 23 | STATIC_INLINE uint32_t fmix(uint32_t h) |
| 24 | { |
| 25 | h ^= h >> 16; |
| 26 | h *= 0x85ebca6b; |
| 27 | h ^= h >> 13; |
| 28 | h *= 0xc2b2ae35; |
| 29 | h ^= h >> 16; |
| 30 | return h; |
| 31 | } |
| 32 | |
| 33 | STATIC_INLINE uint32_t Mur(uint32_t a, uint32_t h) { |
| 34 | // Helper from Murmur3 for combining two 32-bit values. |
| 35 | a *= c1; |
| 36 | a = Rotate32(a, 17); |
| 37 | a *= c2; |
| 38 | h ^= a; |
| 39 | h = Rotate32(h, 19); |
| 40 | return h * 5 + 0xe6546b64; |
| 41 | } |
| 42 | |
| 43 | template <typename T> STATIC_INLINE T DebugTweak(T x) { |
| 44 | if (debug_mode) { |
| 45 | if (sizeof(x) == 4) { |
| 46 | x = ~Bswap32(x * c1); |
| 47 | } else { |
| 48 | x = ~Bswap64(x * k1); |
| 49 | } |
| 50 | } |
| 51 | return x; |
| 52 | } |
| 53 | |
| 54 | template <> NAMESPACE_FOR_HASH_FUNCTIONS::NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t DebugTweak(uint128_t x) { |
| 55 | if (debug_mode) { |
| 56 | uint64_t y = DebugTweak(Uint128Low64(x)); |
| 57 | uint64_t z = DebugTweak(Uint128High64(x)); |
| 58 | y += z; |
| 59 | z += y; |
| 60 | x = Uint128(y, z * k1); |
| 61 | } |
| 62 | return x; |
| 63 | } |
| 64 | |
| 65 | } // namespace NAMESPACE_FOR_HASH_FUNCTIONS |
| 66 | |
| 67 | using namespace std; |
| 68 | using namespace NAMESPACE_FOR_HASH_FUNCTIONS; |
| 69 | |