Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the newly proposed standard C++ interfaces for hashing |
| 11 | // arbitrary data and building hash functions for user-defined types. This |
| 12 | // interface was originally proposed in N3333[1] and is currently under review |
| 13 | // for inclusion in a future TR and/or standard. |
| 14 | // |
| 15 | // The primary interfaces provide are comprised of one type and three functions: |
| 16 | // |
| 17 | // -- 'hash_code' class is an opaque type representing the hash code for some |
| 18 | // data. It is the intended product of hashing, and can be used to implement |
| 19 | // hash tables, checksumming, and other common uses of hashes. It is not an |
| 20 | // integer type (although it can be converted to one) because it is risky |
| 21 | // to assume much about the internals of a hash_code. In particular, each |
| 22 | // execution of the program has a high probability of producing a different |
| 23 | // hash_code for a given input. Thus their values are not stable to save or |
| 24 | // persist, and should only be used during the execution for the |
| 25 | // construction of hashing datastructures. |
| 26 | // |
| 27 | // -- 'hash_value' is a function designed to be overloaded for each |
| 28 | // user-defined type which wishes to be used within a hashing context. It |
| 29 | // should be overloaded within the user-defined type's namespace and found |
| 30 | // via ADL. Overloads for primitive types are provided by this library. |
| 31 | // |
| 32 | // -- 'hash_combine' and 'hash_combine_range' are functions designed to aid |
| 33 | // programmers in easily and intuitively combining a set of data into |
| 34 | // a single hash_code for their object. They should only logically be used |
| 35 | // within the implementation of a 'hash_value' routine or similar context. |
| 36 | // |
| 37 | // Note that 'hash_combine_range' contains very special logic for hashing |
| 38 | // a contiguous array of integers or pointers. This logic is *extremely* fast, |
| 39 | // on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were |
| 40 | // benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys |
| 41 | // under 32-bytes. |
| 42 | // |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | #ifndef LLVM_ADT_HASHING_H |
| 46 | #define LLVM_ADT_HASHING_H |
| 47 | |
| 48 | #include "llvm/Support/DataTypes.h" |
| 49 | #include "llvm/Support/Host.h" |
| 50 | #include "llvm/Support/SwapByteOrder.h" |
| 51 | #include "llvm/Support/type_traits.h" |
| 52 | #include <algorithm> |
| 53 | #include <cassert> |
| 54 | #include <cstring> |
| 55 | #include <string> |
| 56 | #include <utility> |
| 57 | |
| 58 | namespace llvm { |
| 59 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | /// An opaque object representing a hash code. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | /// |
| 62 | /// This object represents the result of hashing some entity. It is intended to |
| 63 | /// be used to implement hashtables or other hashing-based data structures. |
| 64 | /// While it wraps and exposes a numeric value, this value should not be |
| 65 | /// trusted to be stable or predictable across processes or executions. |
| 66 | /// |
| 67 | /// In order to obtain the hash_code for an object 'x': |
| 68 | /// \code |
| 69 | /// using llvm::hash_value; |
| 70 | /// llvm::hash_code code = hash_value(x); |
| 71 | /// \endcode |
| 72 | class hash_code { |
| 73 | size_t value; |
| 74 | |
| 75 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 76 | /// Default construct a hash_code. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 77 | /// Note that this leaves the value uninitialized. |
| 78 | hash_code() = default; |
| 79 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 80 | /// Form a hash code directly from a numerical value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | hash_code(size_t value) : value(value) {} |
| 82 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 83 | /// Convert the hash code to its numerical value for use. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 84 | /*explicit*/ operator size_t() const { return value; } |
| 85 | |
| 86 | friend bool operator==(const hash_code &lhs, const hash_code &rhs) { |
| 87 | return lhs.value == rhs.value; |
| 88 | } |
| 89 | friend bool operator!=(const hash_code &lhs, const hash_code &rhs) { |
| 90 | return lhs.value != rhs.value; |
| 91 | } |
| 92 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 93 | /// Allow a hash_code to be directly run through hash_value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 94 | friend size_t hash_value(const hash_code &code) { return code.value; } |
| 95 | }; |
| 96 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 97 | /// Compute a hash_code for any integer value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 98 | /// |
| 99 | /// Note that this function is intended to compute the same hash_code for |
| 100 | /// a particular value without regard to the pre-promotion type. This is in |
| 101 | /// contrast to hash_combine which may produce different hash_codes for |
| 102 | /// differing argument types even if they would implicit promote to a common |
| 103 | /// type without changing the value. |
| 104 | template <typename T> |
| 105 | typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type |
| 106 | hash_value(T value); |
| 107 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 108 | /// Compute a hash_code for a pointer's address. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | /// |
| 110 | /// N.B.: This hashes the *address*. Not the value and not the type. |
| 111 | template <typename T> hash_code hash_value(const T *ptr); |
| 112 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 113 | /// Compute a hash_code for a pair of objects. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 114 | template <typename T, typename U> |
| 115 | hash_code hash_value(const std::pair<T, U> &arg); |
| 116 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 117 | /// Compute a hash_code for a standard string. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 118 | template <typename T> |
| 119 | hash_code hash_value(const std::basic_string<T> &arg); |
| 120 | |
| 121 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 122 | /// Override the execution seed with a fixed value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 123 | /// |
| 124 | /// This hashing library uses a per-execution seed designed to change on each |
| 125 | /// run with high probability in order to ensure that the hash codes are not |
| 126 | /// attackable and to ensure that output which is intended to be stable does |
| 127 | /// not rely on the particulars of the hash codes produced. |
| 128 | /// |
| 129 | /// That said, there are use cases where it is important to be able to |
| 130 | /// reproduce *exactly* a specific behavior. To that end, we provide a function |
| 131 | /// which will forcibly set the seed to a fixed value. This must be done at the |
| 132 | /// start of the program, before any hashes are computed. Also, it cannot be |
| 133 | /// undone. This makes it thread-hostile and very hard to use outside of |
| 134 | /// immediately on start of a simple program designed for reproducible |
| 135 | /// behavior. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 136 | void set_fixed_execution_hash_seed(uint64_t fixed_value); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 137 | |
| 138 | |
| 139 | // All of the implementation details of actually computing the various hash |
| 140 | // code values are held within this namespace. These routines are included in |
| 141 | // the header file mainly to allow inlining and constant propagation. |
| 142 | namespace hashing { |
| 143 | namespace detail { |
| 144 | |
| 145 | inline uint64_t fetch64(const char *p) { |
| 146 | uint64_t result; |
| 147 | memcpy(&result, p, sizeof(result)); |
| 148 | if (sys::IsBigEndianHost) |
| 149 | sys::swapByteOrder(result); |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | inline uint32_t fetch32(const char *p) { |
| 154 | uint32_t result; |
| 155 | memcpy(&result, p, sizeof(result)); |
| 156 | if (sys::IsBigEndianHost) |
| 157 | sys::swapByteOrder(result); |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | /// Some primes between 2^63 and 2^64 for various uses. |
| 162 | static const uint64_t k0 = 0xc3a5c85c97cb3127ULL; |
| 163 | static const uint64_t k1 = 0xb492b66fbe98f273ULL; |
| 164 | static const uint64_t k2 = 0x9ae16a3b2f90404fULL; |
| 165 | static const uint64_t k3 = 0xc949d7c7509e6557ULL; |
| 166 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 167 | /// Bitwise right rotate. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 168 | /// Normally this will compile to a single instruction, especially if the |
| 169 | /// shift is a manifest constant. |
| 170 | inline uint64_t rotate(uint64_t val, size_t shift) { |
| 171 | // Avoid shifting by 64: doing so yields an undefined result. |
| 172 | return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); |
| 173 | } |
| 174 | |
| 175 | inline uint64_t shift_mix(uint64_t val) { |
| 176 | return val ^ (val >> 47); |
| 177 | } |
| 178 | |
| 179 | inline uint64_t hash_16_bytes(uint64_t low, uint64_t high) { |
| 180 | // Murmur-inspired hashing. |
| 181 | const uint64_t kMul = 0x9ddfea08eb382d69ULL; |
| 182 | uint64_t a = (low ^ high) * kMul; |
| 183 | a ^= (a >> 47); |
| 184 | uint64_t b = (high ^ a) * kMul; |
| 185 | b ^= (b >> 47); |
| 186 | b *= kMul; |
| 187 | return b; |
| 188 | } |
| 189 | |
| 190 | inline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) { |
| 191 | uint8_t a = s[0]; |
| 192 | uint8_t b = s[len >> 1]; |
| 193 | uint8_t c = s[len - 1]; |
| 194 | uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8); |
| 195 | uint32_t z = len + (static_cast<uint32_t>(c) << 2); |
| 196 | return shift_mix(y * k2 ^ z * k3 ^ seed) * k2; |
| 197 | } |
| 198 | |
| 199 | inline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) { |
| 200 | uint64_t a = fetch32(s); |
| 201 | return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4)); |
| 202 | } |
| 203 | |
| 204 | inline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) { |
| 205 | uint64_t a = fetch64(s); |
| 206 | uint64_t b = fetch64(s + len - 8); |
| 207 | return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b; |
| 208 | } |
| 209 | |
| 210 | inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) { |
| 211 | uint64_t a = fetch64(s) * k1; |
| 212 | uint64_t b = fetch64(s + 8); |
| 213 | uint64_t c = fetch64(s + len - 8) * k2; |
| 214 | uint64_t d = fetch64(s + len - 16) * k0; |
| 215 | return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d, |
| 216 | a + rotate(b ^ k3, 20) - c + len + seed); |
| 217 | } |
| 218 | |
| 219 | inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) { |
| 220 | uint64_t z = fetch64(s + 24); |
| 221 | uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0; |
| 222 | uint64_t b = rotate(a + z, 52); |
| 223 | uint64_t c = rotate(a, 37); |
| 224 | a += fetch64(s + 8); |
| 225 | c += rotate(a, 7); |
| 226 | a += fetch64(s + 16); |
| 227 | uint64_t vf = a + z; |
| 228 | uint64_t vs = b + rotate(a, 31) + c; |
| 229 | a = fetch64(s + 16) + fetch64(s + len - 32); |
| 230 | z = fetch64(s + len - 8); |
| 231 | b = rotate(a + z, 52); |
| 232 | c = rotate(a, 37); |
| 233 | a += fetch64(s + len - 24); |
| 234 | c += rotate(a, 7); |
| 235 | a += fetch64(s + len - 16); |
| 236 | uint64_t wf = a + z; |
| 237 | uint64_t ws = b + rotate(a, 31) + c; |
| 238 | uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0); |
| 239 | return shift_mix((seed ^ (r * k0)) + vs) * k2; |
| 240 | } |
| 241 | |
| 242 | inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) { |
| 243 | if (length >= 4 && length <= 8) |
| 244 | return hash_4to8_bytes(s, length, seed); |
| 245 | if (length > 8 && length <= 16) |
| 246 | return hash_9to16_bytes(s, length, seed); |
| 247 | if (length > 16 && length <= 32) |
| 248 | return hash_17to32_bytes(s, length, seed); |
| 249 | if (length > 32) |
| 250 | return hash_33to64_bytes(s, length, seed); |
| 251 | if (length != 0) |
| 252 | return hash_1to3_bytes(s, length, seed); |
| 253 | |
| 254 | return k2 ^ seed; |
| 255 | } |
| 256 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 257 | /// The intermediate state used during hashing. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 258 | /// Currently, the algorithm for computing hash codes is based on CityHash and |
| 259 | /// keeps 56 bytes of arbitrary state. |
| 260 | struct hash_state { |
| 261 | uint64_t h0, h1, h2, h3, h4, h5, h6; |
| 262 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 263 | /// Create a new hash_state structure and initialize it based on the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 264 | /// seed and the first 64-byte chunk. |
| 265 | /// This effectively performs the initial mix. |
| 266 | static hash_state create(const char *s, uint64_t seed) { |
| 267 | hash_state state = { |
| 268 | 0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49), |
| 269 | seed * k1, shift_mix(seed), 0 }; |
| 270 | state.h6 = hash_16_bytes(state.h4, state.h5); |
| 271 | state.mix(s); |
| 272 | return state; |
| 273 | } |
| 274 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 275 | /// Mix 32-bytes from the input sequence into the 16-bytes of 'a' |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 276 | /// and 'b', including whatever is already in 'a' and 'b'. |
| 277 | static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) { |
| 278 | a += fetch64(s); |
| 279 | uint64_t c = fetch64(s + 24); |
| 280 | b = rotate(b + a + c, 21); |
| 281 | uint64_t d = a; |
| 282 | a += fetch64(s + 8) + fetch64(s + 16); |
| 283 | b += rotate(a, 44) + d; |
| 284 | a += c; |
| 285 | } |
| 286 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 287 | /// Mix in a 64-byte buffer of data. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 288 | /// We mix all 64 bytes even when the chunk length is smaller, but we |
| 289 | /// record the actual length. |
| 290 | void mix(const char *s) { |
| 291 | h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1; |
| 292 | h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1; |
| 293 | h0 ^= h6; |
| 294 | h1 += h3 + fetch64(s + 40); |
| 295 | h2 = rotate(h2 + h5, 33) * k1; |
| 296 | h3 = h4 * k1; |
| 297 | h4 = h0 + h5; |
| 298 | mix_32_bytes(s, h3, h4); |
| 299 | h5 = h2 + h6; |
| 300 | h6 = h1 + fetch64(s + 16); |
| 301 | mix_32_bytes(s + 32, h5, h6); |
| 302 | std::swap(h2, h0); |
| 303 | } |
| 304 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 305 | /// Compute the final 64-bit hash code value based on the current |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 306 | /// state and the length of bytes hashed. |
| 307 | uint64_t finalize(size_t length) { |
| 308 | return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2, |
| 309 | hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0); |
| 310 | } |
| 311 | }; |
| 312 | |
| 313 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 314 | /// A global, fixed seed-override variable. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 315 | /// |
| 316 | /// This variable can be set using the \see llvm::set_fixed_execution_seed |
| 317 | /// function. See that function for details. Do not, under any circumstances, |
| 318 | /// set or read this variable. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 319 | extern uint64_t fixed_seed_override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 320 | |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 321 | inline uint64_t get_execution_seed() { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 322 | // FIXME: This needs to be a per-execution seed. This is just a placeholder |
| 323 | // implementation. Switching to a per-execution seed is likely to flush out |
| 324 | // instability bugs and so will happen as its own commit. |
| 325 | // |
| 326 | // However, if there is a fixed seed override set the first time this is |
| 327 | // called, return that instead of the per-execution seed. |
| 328 | const uint64_t seed_prime = 0xff51afd7ed558ccdULL; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 329 | static uint64_t seed = fixed_seed_override ? fixed_seed_override : seed_prime; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 330 | return seed; |
| 331 | } |
| 332 | |
| 333 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 334 | /// Trait to indicate whether a type's bits can be hashed directly. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 335 | /// |
| 336 | /// A type trait which is true if we want to combine values for hashing by |
| 337 | /// reading the underlying data. It is false if values of this type must |
| 338 | /// first be passed to hash_value, and the resulting hash_codes combined. |
| 339 | // |
| 340 | // FIXME: We want to replace is_integral_or_enum and is_pointer here with |
| 341 | // a predicate which asserts that comparing the underlying storage of two |
| 342 | // values of the type for equality is equivalent to comparing the two values |
| 343 | // for equality. For all the platforms we care about, this holds for integers |
| 344 | // and pointers, but there are platforms where it doesn't and we would like to |
| 345 | // support user-defined types which happen to satisfy this property. |
| 346 | template <typename T> struct is_hashable_data |
| 347 | : std::integral_constant<bool, ((is_integral_or_enum<T>::value || |
| 348 | std::is_pointer<T>::value) && |
| 349 | 64 % sizeof(T) == 0)> {}; |
| 350 | |
| 351 | // Special case std::pair to detect when both types are viable and when there |
| 352 | // is no alignment-derived padding in the pair. This is a bit of a lie because |
| 353 | // std::pair isn't truly POD, but it's close enough in all reasonable |
| 354 | // implementations for our use case of hashing the underlying data. |
| 355 | template <typename T, typename U> struct is_hashable_data<std::pair<T, U> > |
| 356 | : std::integral_constant<bool, (is_hashable_data<T>::value && |
| 357 | is_hashable_data<U>::value && |
| 358 | (sizeof(T) + sizeof(U)) == |
| 359 | sizeof(std::pair<T, U>))> {}; |
| 360 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 361 | /// Helper to get the hashable data representation for a type. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 362 | /// This variant is enabled when the type itself can be used. |
| 363 | template <typename T> |
| 364 | typename std::enable_if<is_hashable_data<T>::value, T>::type |
| 365 | get_hashable_data(const T &value) { |
| 366 | return value; |
| 367 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 368 | /// Helper to get the hashable data representation for a type. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 369 | /// This variant is enabled when we must first call hash_value and use the |
| 370 | /// result as our data. |
| 371 | template <typename T> |
| 372 | typename std::enable_if<!is_hashable_data<T>::value, size_t>::type |
| 373 | get_hashable_data(const T &value) { |
| 374 | using ::llvm::hash_value; |
| 375 | return hash_value(value); |
| 376 | } |
| 377 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 378 | /// Helper to store data from a value into a buffer and advance the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 379 | /// pointer into that buffer. |
| 380 | /// |
| 381 | /// This routine first checks whether there is enough space in the provided |
| 382 | /// buffer, and if not immediately returns false. If there is space, it |
| 383 | /// copies the underlying bytes of value into the buffer, advances the |
| 384 | /// buffer_ptr past the copied bytes, and returns true. |
| 385 | template <typename T> |
| 386 | bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value, |
| 387 | size_t offset = 0) { |
| 388 | size_t store_size = sizeof(value) - offset; |
| 389 | if (buffer_ptr + store_size > buffer_end) |
| 390 | return false; |
| 391 | const char *value_data = reinterpret_cast<const char *>(&value); |
| 392 | memcpy(buffer_ptr, value_data + offset, store_size); |
| 393 | buffer_ptr += store_size; |
| 394 | return true; |
| 395 | } |
| 396 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 397 | /// Implement the combining of integral values into a hash_code. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 398 | /// |
| 399 | /// This overload is selected when the value type of the iterator is |
| 400 | /// integral. Rather than computing a hash_code for each object and then |
| 401 | /// combining them, this (as an optimization) directly combines the integers. |
| 402 | template <typename InputIteratorT> |
| 403 | hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 404 | const uint64_t seed = get_execution_seed(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 405 | char buffer[64], *buffer_ptr = buffer; |
| 406 | char *const buffer_end = std::end(buffer); |
| 407 | while (first != last && store_and_advance(buffer_ptr, buffer_end, |
| 408 | get_hashable_data(*first))) |
| 409 | ++first; |
| 410 | if (first == last) |
| 411 | return hash_short(buffer, buffer_ptr - buffer, seed); |
| 412 | assert(buffer_ptr == buffer_end); |
| 413 | |
| 414 | hash_state state = state.create(buffer, seed); |
| 415 | size_t length = 64; |
| 416 | while (first != last) { |
| 417 | // Fill up the buffer. We don't clear it, which re-mixes the last round |
| 418 | // when only a partial 64-byte chunk is left. |
| 419 | buffer_ptr = buffer; |
| 420 | while (first != last && store_and_advance(buffer_ptr, buffer_end, |
| 421 | get_hashable_data(*first))) |
| 422 | ++first; |
| 423 | |
| 424 | // Rotate the buffer if we did a partial fill in order to simulate doing |
| 425 | // a mix of the last 64-bytes. That is how the algorithm works when we |
| 426 | // have a contiguous byte sequence, and we want to emulate that here. |
| 427 | std::rotate(buffer, buffer_ptr, buffer_end); |
| 428 | |
| 429 | // Mix this chunk into the current state. |
| 430 | state.mix(buffer); |
| 431 | length += buffer_ptr - buffer; |
| 432 | }; |
| 433 | |
| 434 | return state.finalize(length); |
| 435 | } |
| 436 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 437 | /// Implement the combining of integral values into a hash_code. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 438 | /// |
| 439 | /// This overload is selected when the value type of the iterator is integral |
| 440 | /// and when the input iterator is actually a pointer. Rather than computing |
| 441 | /// a hash_code for each object and then combining them, this (as an |
| 442 | /// optimization) directly combines the integers. Also, because the integers |
| 443 | /// are stored in contiguous memory, this routine avoids copying each value |
| 444 | /// and directly reads from the underlying memory. |
| 445 | template <typename ValueT> |
| 446 | typename std::enable_if<is_hashable_data<ValueT>::value, hash_code>::type |
| 447 | hash_combine_range_impl(ValueT *first, ValueT *last) { |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 448 | const uint64_t seed = get_execution_seed(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 449 | const char *s_begin = reinterpret_cast<const char *>(first); |
| 450 | const char *s_end = reinterpret_cast<const char *>(last); |
| 451 | const size_t length = std::distance(s_begin, s_end); |
| 452 | if (length <= 64) |
| 453 | return hash_short(s_begin, length, seed); |
| 454 | |
| 455 | const char *s_aligned_end = s_begin + (length & ~63); |
| 456 | hash_state state = state.create(s_begin, seed); |
| 457 | s_begin += 64; |
| 458 | while (s_begin != s_aligned_end) { |
| 459 | state.mix(s_begin); |
| 460 | s_begin += 64; |
| 461 | } |
| 462 | if (length & 63) |
| 463 | state.mix(s_end - 64); |
| 464 | |
| 465 | return state.finalize(length); |
| 466 | } |
| 467 | |
| 468 | } // namespace detail |
| 469 | } // namespace hashing |
| 470 | |
| 471 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 472 | /// Compute a hash_code for a sequence of values. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 473 | /// |
| 474 | /// This hashes a sequence of values. It produces the same hash_code as |
| 475 | /// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences |
| 476 | /// and is significantly faster given pointers and types which can be hashed as |
| 477 | /// a sequence of bytes. |
| 478 | template <typename InputIteratorT> |
| 479 | hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) { |
| 480 | return ::llvm::hashing::detail::hash_combine_range_impl(first, last); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | // Implementation details for hash_combine. |
| 485 | namespace hashing { |
| 486 | namespace detail { |
| 487 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 488 | /// Helper class to manage the recursive combining of hash_combine |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 489 | /// arguments. |
| 490 | /// |
| 491 | /// This class exists to manage the state and various calls involved in the |
| 492 | /// recursive combining of arguments used in hash_combine. It is particularly |
| 493 | /// useful at minimizing the code in the recursive calls to ease the pain |
| 494 | /// caused by a lack of variadic functions. |
| 495 | struct hash_combine_recursive_helper { |
| 496 | char buffer[64]; |
| 497 | hash_state state; |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame^] | 498 | const uint64_t seed; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 499 | |
| 500 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 501 | /// Construct a recursive hash combining helper. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 502 | /// |
| 503 | /// This sets up the state for a recursive hash combine, including getting |
| 504 | /// the seed and buffer setup. |
| 505 | hash_combine_recursive_helper() |
| 506 | : seed(get_execution_seed()) {} |
| 507 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 508 | /// Combine one chunk of data into the current in-flight hash. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 509 | /// |
| 510 | /// This merges one chunk of data into the hash. First it tries to buffer |
| 511 | /// the data. If the buffer is full, it hashes the buffer into its |
| 512 | /// hash_state, empties it, and then merges the new chunk in. This also |
| 513 | /// handles cases where the data straddles the end of the buffer. |
| 514 | template <typename T> |
| 515 | char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) { |
| 516 | if (!store_and_advance(buffer_ptr, buffer_end, data)) { |
| 517 | // Check for skew which prevents the buffer from being packed, and do |
| 518 | // a partial store into the buffer to fill it. This is only a concern |
| 519 | // with the variadic combine because that formation can have varying |
| 520 | // argument types. |
| 521 | size_t partial_store_size = buffer_end - buffer_ptr; |
| 522 | memcpy(buffer_ptr, &data, partial_store_size); |
| 523 | |
| 524 | // If the store fails, our buffer is full and ready to hash. We have to |
| 525 | // either initialize the hash state (on the first full buffer) or mix |
| 526 | // this buffer into the existing hash state. Length tracks the *hashed* |
| 527 | // length, not the buffered length. |
| 528 | if (length == 0) { |
| 529 | state = state.create(buffer, seed); |
| 530 | length = 64; |
| 531 | } else { |
| 532 | // Mix this chunk into the current state and bump length up by 64. |
| 533 | state.mix(buffer); |
| 534 | length += 64; |
| 535 | } |
| 536 | // Reset the buffer_ptr to the head of the buffer for the next chunk of |
| 537 | // data. |
| 538 | buffer_ptr = buffer; |
| 539 | |
| 540 | // Try again to store into the buffer -- this cannot fail as we only |
| 541 | // store types smaller than the buffer. |
| 542 | if (!store_and_advance(buffer_ptr, buffer_end, data, |
| 543 | partial_store_size)) |
| 544 | abort(); |
| 545 | } |
| 546 | return buffer_ptr; |
| 547 | } |
| 548 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 549 | /// Recursive, variadic combining method. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 550 | /// |
| 551 | /// This function recurses through each argument, combining that argument |
| 552 | /// into a single hash. |
| 553 | template <typename T, typename ...Ts> |
| 554 | hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, |
| 555 | const T &arg, const Ts &...args) { |
| 556 | buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg)); |
| 557 | |
| 558 | // Recurse to the next argument. |
| 559 | return combine(length, buffer_ptr, buffer_end, args...); |
| 560 | } |
| 561 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 562 | /// Base case for recursive, variadic combining. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 563 | /// |
| 564 | /// The base case when combining arguments recursively is reached when all |
| 565 | /// arguments have been handled. It flushes the remaining buffer and |
| 566 | /// constructs a hash_code. |
| 567 | hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) { |
| 568 | // Check whether the entire set of values fit in the buffer. If so, we'll |
| 569 | // use the optimized short hashing routine and skip state entirely. |
| 570 | if (length == 0) |
| 571 | return hash_short(buffer, buffer_ptr - buffer, seed); |
| 572 | |
| 573 | // Mix the final buffer, rotating it if we did a partial fill in order to |
| 574 | // simulate doing a mix of the last 64-bytes. That is how the algorithm |
| 575 | // works when we have a contiguous byte sequence, and we want to emulate |
| 576 | // that here. |
| 577 | std::rotate(buffer, buffer_ptr, buffer_end); |
| 578 | |
| 579 | // Mix this chunk into the current state. |
| 580 | state.mix(buffer); |
| 581 | length += buffer_ptr - buffer; |
| 582 | |
| 583 | return state.finalize(length); |
| 584 | } |
| 585 | }; |
| 586 | |
| 587 | } // namespace detail |
| 588 | } // namespace hashing |
| 589 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 590 | /// Combine values into a single hash_code. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 591 | /// |
| 592 | /// This routine accepts a varying number of arguments of any type. It will |
| 593 | /// attempt to combine them into a single hash_code. For user-defined types it |
| 594 | /// attempts to call a \see hash_value overload (via ADL) for the type. For |
| 595 | /// integer and pointer types it directly combines their data into the |
| 596 | /// resulting hash_code. |
| 597 | /// |
| 598 | /// The result is suitable for returning from a user's hash_value |
| 599 | /// *implementation* for their user-defined type. Consumers of a type should |
| 600 | /// *not* call this routine, they should instead call 'hash_value'. |
| 601 | template <typename ...Ts> hash_code hash_combine(const Ts &...args) { |
| 602 | // Recursively hash each argument using a helper class. |
| 603 | ::llvm::hashing::detail::hash_combine_recursive_helper helper; |
| 604 | return helper.combine(0, helper.buffer, helper.buffer + 64, args...); |
| 605 | } |
| 606 | |
| 607 | // Implementation details for implementations of hash_value overloads provided |
| 608 | // here. |
| 609 | namespace hashing { |
| 610 | namespace detail { |
| 611 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 612 | /// Helper to hash the value of a single integer. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 613 | /// |
| 614 | /// Overloads for smaller integer types are not provided to ensure consistent |
| 615 | /// behavior in the presence of integral promotions. Essentially, |
| 616 | /// "hash_value('4')" and "hash_value('0' + 4)" should be the same. |
| 617 | inline hash_code hash_integer_value(uint64_t value) { |
| 618 | // Similar to hash_4to8_bytes but using a seed instead of length. |
| 619 | const uint64_t seed = get_execution_seed(); |
| 620 | const char *s = reinterpret_cast<const char *>(&value); |
| 621 | const uint64_t a = fetch32(s); |
| 622 | return hash_16_bytes(seed + (a << 3), fetch32(s + 4)); |
| 623 | } |
| 624 | |
| 625 | } // namespace detail |
| 626 | } // namespace hashing |
| 627 | |
| 628 | // Declared and documented above, but defined here so that any of the hashing |
| 629 | // infrastructure is available. |
| 630 | template <typename T> |
| 631 | typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type |
| 632 | hash_value(T value) { |
| 633 | return ::llvm::hashing::detail::hash_integer_value( |
| 634 | static_cast<uint64_t>(value)); |
| 635 | } |
| 636 | |
| 637 | // Declared and documented above, but defined here so that any of the hashing |
| 638 | // infrastructure is available. |
| 639 | template <typename T> hash_code hash_value(const T *ptr) { |
| 640 | return ::llvm::hashing::detail::hash_integer_value( |
| 641 | reinterpret_cast<uintptr_t>(ptr)); |
| 642 | } |
| 643 | |
| 644 | // Declared and documented above, but defined here so that any of the hashing |
| 645 | // infrastructure is available. |
| 646 | template <typename T, typename U> |
| 647 | hash_code hash_value(const std::pair<T, U> &arg) { |
| 648 | return hash_combine(arg.first, arg.second); |
| 649 | } |
| 650 | |
| 651 | // Declared and documented above, but defined here so that any of the hashing |
| 652 | // infrastructure is available. |
| 653 | template <typename T> |
| 654 | hash_code hash_value(const std::basic_string<T> &arg) { |
| 655 | return hash_combine_range(arg.begin(), arg.end()); |
| 656 | } |
| 657 | |
| 658 | } // namespace llvm |
| 659 | |
| 660 | #endif |