microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
cmake/externals/farmhash/src/farmhash.h
311lines · modecode
| 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 | // |
| 24 | // http://code.google.com/p/farmhash/ |
| 25 | // |
| 26 | // This file provides a few functions for hashing strings and other |
| 27 | // data. All of them are high-quality functions in the sense that |
| 28 | // they do well on standard tests such as Austin Appleby's SMHasher. |
| 29 | // They're also fast. FarmHash is the successor to CityHash. |
| 30 | // |
| 31 | // Functions in the FarmHash family are not suitable for cryptography. |
| 32 | // |
| 33 | // WARNING: This code has been only lightly tested on big-endian platforms! |
| 34 | // It is known to work well on little-endian platforms that have a small penalty |
| 35 | // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs. |
| 36 | // It should work on all 32-bit and 64-bit platforms that allow unaligned reads; |
| 37 | // bug reports are welcome. |
| 38 | // |
| 39 | // By the way, for some hash functions, given strings a and b, the hash |
| 40 | // of a+b is easily derived from the hashes of a and b. This property |
| 41 | // doesn't hold for any hash functions in this file. |
| 42 | |
| 43 | #ifndef FARM_HASH_H_ |
| 44 | #define FARM_HASH_H_ |
| 45 | |
| 46 | #include <assert.h> |
| 47 | #include <stdint.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> // for memcpy and memset |
| 50 | #include <utility> |
| 51 | |
| 52 | #ifndef NAMESPACE_FOR_HASH_FUNCTIONS |
| 53 | #define NAMESPACE_FOR_HASH_FUNCTIONS util |
| 54 | #endif |
| 55 | |
| 56 | namespace NAMESPACE_FOR_HASH_FUNCTIONS { |
| 57 | |
| 58 | typedef std::pair<uint64_t, uint64_t> uint128_t; |
| 59 | inline uint64_t Uint128Low64(const NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) { return x.first; } |
| 60 | inline uint64_t Uint128High64(const NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) { return x.second; } |
| 61 | inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Uint128(uint64_t lo, uint64_t hi) { return NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t(lo, hi); } |
| 62 | |
| 63 | |
| 64 | // BASIC STRING HASHING |
| 65 | |
| 66 | // Hash function for a byte array. |
| 67 | // May change from time to time, may differ on different platforms, may differ |
| 68 | // depending on NDEBUG. |
| 69 | size_t Hash(const char* s, size_t len); |
| 70 | |
| 71 | // Hash function for a byte array. Most useful in 32-bit binaries. |
| 72 | // May change from time to time, may differ on different platforms, may differ |
| 73 | // depending on NDEBUG. |
| 74 | uint32_t Hash32(const char* s, size_t len); |
| 75 | |
| 76 | // Hash function for a byte array. For convenience, a 32-bit seed is also |
| 77 | // hashed into the result. |
| 78 | // May change from time to time, may differ on different platforms, may differ |
| 79 | // depending on NDEBUG. |
| 80 | uint32_t Hash32WithSeed(const char* s, size_t len, uint32_t seed); |
| 81 | |
| 82 | // Hash function for a byte array. |
| 83 | // May change from time to time, may differ on different platforms, may differ |
| 84 | // depending on NDEBUG. |
| 85 | uint64_t Hash64(const char* s, size_t len); |
| 86 | |
| 87 | // Hash function for a byte array. For convenience, a 64-bit seed is also |
| 88 | // hashed into the result. |
| 89 | // May change from time to time, may differ on different platforms, may differ |
| 90 | // depending on NDEBUG. |
| 91 | uint64_t Hash64WithSeed(const char* s, size_t len, uint64_t seed); |
| 92 | |
| 93 | // Hash function for a byte array. For convenience, two seeds are also |
| 94 | // hashed into the result. |
| 95 | // May change from time to time, may differ on different platforms, may differ |
| 96 | // depending on NDEBUG. |
| 97 | uint64_t Hash64WithSeeds(const char* s, size_t len, |
| 98 | uint64_t seed0, uint64_t seed1); |
| 99 | |
| 100 | // Hash function for a byte array. |
| 101 | // May change from time to time, may differ on different platforms, may differ |
| 102 | // depending on NDEBUG. |
| 103 | NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128(const char* s, size_t len); |
| 104 | |
| 105 | // Hash function for a byte array. For convenience, a 128-bit seed is also |
| 106 | // hashed into the result. |
| 107 | // May change from time to time, may differ on different platforms, may differ |
| 108 | // depending on NDEBUG. |
| 109 | NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128WithSeed(const char* s, size_t len, NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t seed); |
| 110 | |
| 111 | // BASIC NON-STRING HASHING |
| 112 | |
| 113 | // Hash 128 input bits down to 64 bits of output. |
| 114 | // This is intended to be a reasonably good hash function. |
| 115 | // May change from time to time, may differ on different platforms, may differ |
| 116 | // depending on NDEBUG. |
| 117 | inline uint64_t Hash128to64(NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) { |
| 118 | // Murmur-inspired hashing. |
| 119 | const uint64_t kMul = 0x9ddfea08eb382d69ULL; |
| 120 | uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; |
| 121 | a ^= (a >> 47); |
| 122 | uint64_t b = (Uint128High64(x) ^ a) * kMul; |
| 123 | b ^= (b >> 47); |
| 124 | b *= kMul; |
| 125 | return b; |
| 126 | } |
| 127 | |
| 128 | // FINGERPRINTING (i.e., good, portable, forever-fixed hash functions) |
| 129 | |
| 130 | // Fingerprint function for a byte array. Most useful in 32-bit binaries. |
| 131 | uint32_t Fingerprint32(const char* s, size_t len); |
| 132 | |
| 133 | // Fingerprint function for a byte array. |
| 134 | uint64_t Fingerprint64(const char* s, size_t len); |
| 135 | |
| 136 | // Fingerprint function for a byte array. |
| 137 | NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Fingerprint128(const char* s, size_t len); |
| 138 | |
| 139 | // This is intended to be a good fingerprinting primitive. |
| 140 | // See below for more overloads. |
| 141 | inline uint64_t Fingerprint(NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t x) { |
| 142 | // Murmur-inspired hashing. |
| 143 | const uint64_t kMul = 0x9ddfea08eb382d69ULL; |
| 144 | uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; |
| 145 | a ^= (a >> 47); |
| 146 | uint64_t b = (Uint128High64(x) ^ a) * kMul; |
| 147 | b ^= (b >> 44); |
| 148 | b *= kMul; |
| 149 | b ^= (b >> 41); |
| 150 | b *= kMul; |
| 151 | return b; |
| 152 | } |
| 153 | |
| 154 | // This is intended to be a good fingerprinting primitive. |
| 155 | inline uint64_t Fingerprint(uint64_t x) { |
| 156 | // Murmur-inspired hashing. |
| 157 | const uint64_t kMul = 0x9ddfea08eb382d69ULL; |
| 158 | uint64_t b = x * kMul; |
| 159 | b ^= (b >> 44); |
| 160 | b *= kMul; |
| 161 | b ^= (b >> 41); |
| 162 | b *= kMul; |
| 163 | return b; |
| 164 | } |
| 165 | |
| 166 | #ifndef FARMHASH_NO_CXX_STRING |
| 167 | |
| 168 | // Convenience functions to hash or fingerprint C++ strings. |
| 169 | // These require that Str::data() return a pointer to the first char |
| 170 | // (as a const char*) and that Str::length() return the string's length; |
| 171 | // they work with std::string, for example. |
| 172 | |
| 173 | // Hash function for a byte array. |
| 174 | // May change from time to time, may differ on different platforms, may differ |
| 175 | // depending on NDEBUG. |
| 176 | template <typename Str> |
| 177 | inline size_t Hash(const Str& s) { |
| 178 | assert(sizeof(s[0]) == 1); |
| 179 | return Hash(s.data(), s.length()); |
| 180 | } |
| 181 | |
| 182 | // Hash function for a byte array. Most useful in 32-bit binaries. |
| 183 | // May change from time to time, may differ on different platforms, may differ |
| 184 | // depending on NDEBUG. |
| 185 | template <typename Str> |
| 186 | inline uint32_t Hash32(const Str& s) { |
| 187 | assert(sizeof(s[0]) == 1); |
| 188 | return Hash32(s.data(), s.length()); |
| 189 | } |
| 190 | |
| 191 | // Hash function for a byte array. For convenience, a 32-bit seed is also |
| 192 | // hashed into the result. |
| 193 | // May change from time to time, may differ on different platforms, may differ |
| 194 | // depending on NDEBUG. |
| 195 | template <typename Str> |
| 196 | inline uint32_t Hash32WithSeed(const Str& s, uint32_t seed) { |
| 197 | assert(sizeof(s[0]) == 1); |
| 198 | return Hash32WithSeed(s.data(), s.length(), seed); |
| 199 | } |
| 200 | |
| 201 | // Hash 128 input bits down to 64 bits of output. |
| 202 | // Hash function for a byte array. |
| 203 | // May change from time to time, may differ on different platforms, may differ |
| 204 | // depending on NDEBUG. |
| 205 | template <typename Str> |
| 206 | inline uint64_t Hash64(const Str& s) { |
| 207 | assert(sizeof(s[0]) == 1); |
| 208 | return Hash64(s.data(), s.length()); |
| 209 | } |
| 210 | |
| 211 | // Hash function for a byte array. For convenience, a 64-bit seed is also |
| 212 | // hashed into the result. |
| 213 | // May change from time to time, may differ on different platforms, may differ |
| 214 | // depending on NDEBUG. |
| 215 | template <typename Str> |
| 216 | inline uint64_t Hash64WithSeed(const Str& s, uint64_t seed) { |
| 217 | assert(sizeof(s[0]) == 1); |
| 218 | return Hash64WithSeed(s.data(), s.length(), seed); |
| 219 | } |
| 220 | |
| 221 | // Hash function for a byte array. For convenience, two seeds are also |
| 222 | // hashed into the result. |
| 223 | // May change from time to time, may differ on different platforms, may differ |
| 224 | // depending on NDEBUG. |
| 225 | template <typename Str> |
| 226 | inline uint64_t Hash64WithSeeds(const Str& s, uint64_t seed0, uint64_t seed1) { |
| 227 | assert(sizeof(s[0]) == 1); |
| 228 | return Hash64WithSeeds(s.data(), s.length(), seed0, seed1); |
| 229 | } |
| 230 | |
| 231 | // Hash function for a byte array. |
| 232 | // May change from time to time, may differ on different platforms, may differ |
| 233 | // depending on NDEBUG. |
| 234 | template <typename Str> |
| 235 | inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128(const Str& s) { |
| 236 | assert(sizeof(s[0]) == 1); |
| 237 | return Hash128(s.data(), s.length()); |
| 238 | } |
| 239 | |
| 240 | // Hash function for a byte array. For convenience, a 128-bit seed is also |
| 241 | // hashed into the result. |
| 242 | // May change from time to time, may differ on different platforms, may differ |
| 243 | // depending on NDEBUG. |
| 244 | template <typename Str> |
| 245 | inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Hash128WithSeed(const Str& s, NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t seed) { |
| 246 | assert(sizeof(s[0]) == 1); |
| 247 | return Hash128(s.data(), s.length(), seed); |
| 248 | } |
| 249 | |
| 250 | // FINGERPRINTING (i.e., good, portable, forever-fixed hash functions) |
| 251 | |
| 252 | // Fingerprint function for a byte array. Most useful in 32-bit binaries. |
| 253 | template <typename Str> |
| 254 | inline uint32_t Fingerprint32(const Str& s) { |
| 255 | assert(sizeof(s[0]) == 1); |
| 256 | return Fingerprint32(s.data(), s.length()); |
| 257 | } |
| 258 | |
| 259 | // Fingerprint 128 input bits down to 64 bits of output. |
| 260 | // Fingerprint function for a byte array. |
| 261 | template <typename Str> |
| 262 | inline uint64_t Fingerprint64(const Str& s) { |
| 263 | assert(sizeof(s[0]) == 1); |
| 264 | return Fingerprint64(s.data(), s.length()); |
| 265 | } |
| 266 | |
| 267 | // Fingerprint function for a byte array. |
| 268 | template <typename Str> |
| 269 | inline NAMESPACE_FOR_HASH_FUNCTIONS::uint128_t Fingerprint128(const Str& s) { |
| 270 | assert(sizeof(s[0]) == 1); |
| 271 | return Fingerprint128(s.data(), s.length()); |
| 272 | } |
| 273 | |
| 274 | #endif |
| 275 | |
| 276 | } // namespace NAMESPACE_FOR_HASH_FUNCTIONS |
| 277 | |
| 278 | /* gently define FARMHASH_BIG_ENDIAN when detected big-endian machine */ |
| 279 | #if defined(__BIG_ENDIAN__) |
| 280 | #if !defined(FARMHASH_BIG_ENDIAN) |
| 281 | #define FARMHASH_BIG_ENDIAN |
| 282 | #endif |
| 283 | #elif defined(__LITTLE_ENDIAN__) |
| 284 | // nothing for little-endian |
| 285 | #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER == __ORDER_LITTLE_ENDIAN__) |
| 286 | // nothing for little-endian |
| 287 | #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER == __ORDER_BIG_ENDIAN__) |
| 288 | #if !defined(FARMHASH_BIG_ENDIAN) |
| 289 | #define FARMHASH_BIG_ENDIAN |
| 290 | #endif |
| 291 | #elif defined(__linux__) || defined(__CYGWIN__) || defined( __GNUC__ ) && !defined(_WIN32) || defined( __GNU_LIBRARY__ ) |
| 292 | #include <endian.h> // libc6-dev, GLIBC |
| 293 | #if BYTE_ORDER == BIG_ENDIAN |
| 294 | #if !defined(FARMHASH_BIG_ENDIAN) |
| 295 | #define FARMHASH_BIG_ENDIAN |
| 296 | #endif |
| 297 | #endif |
| 298 | #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__s390x__) |
| 299 | #include <sys/endian.h> |
| 300 | #if BYTE_ORDER == BIG_ENDIAN |
| 301 | #if !defined(FARMHASH_BIG_ENDIAN) |
| 302 | #define FARMHASH_BIG_ENDIAN |
| 303 | #endif |
| 304 | #endif |
| 305 | #elif defined(_WIN32) |
| 306 | // Windows is (currently) little-endian |
| 307 | #else |
| 308 | #error "Unable to determine endianness!" |
| 309 | #endif /* __BIG_ENDIAN__ */ |
| 310 | |
| 311 | #endif // FARM_HASH_H_ |
| 312 | |