LLVM API Documentation
00001 //===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the newly proposed standard C++ interfaces for hashing 00011 // arbitrary data and building hash functions for user-defined types. This 00012 // interface was originally proposed in N3333[1] and is currently under review 00013 // for inclusion in a future TR and/or standard. 00014 // 00015 // The primary interfaces provide are comprised of one type and three functions: 00016 // 00017 // -- 'hash_code' class is an opaque type representing the hash code for some 00018 // data. It is the intended product of hashing, and can be used to implement 00019 // hash tables, checksumming, and other common uses of hashes. It is not an 00020 // integer type (although it can be converted to one) because it is risky 00021 // to assume much about the internals of a hash_code. In particular, each 00022 // execution of the program has a high probability of producing a different 00023 // hash_code for a given input. Thus their values are not stable to save or 00024 // persist, and should only be used during the execution for the 00025 // construction of hashing datastructures. 00026 // 00027 // -- 'hash_value' is a function designed to be overloaded for each 00028 // user-defined type which wishes to be used within a hashing context. It 00029 // should be overloaded within the user-defined type's namespace and found 00030 // via ADL. Overloads for primitive types are provided by this library. 00031 // 00032 // -- 'hash_combine' and 'hash_combine_range' are functions designed to aid 00033 // programmers in easily and intuitively combining a set of data into 00034 // a single hash_code for their object. They should only logically be used 00035 // within the implementation of a 'hash_value' routine or similar context. 00036 // 00037 // Note that 'hash_combine_range' contains very special logic for hashing 00038 // a contiguous array of integers or pointers. This logic is *extremely* fast, 00039 // on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were 00040 // benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys 00041 // under 32-bytes. 00042 // 00043 //===----------------------------------------------------------------------===// 00044 00045 #ifndef LLVM_ADT_HASHING_H 00046 #define LLVM_ADT_HASHING_H 00047 00048 #include "llvm/ADT/STLExtras.h" 00049 #include "llvm/Support/DataTypes.h" 00050 #include "llvm/Support/Host.h" 00051 #include "llvm/Support/SwapByteOrder.h" 00052 #include "llvm/Support/type_traits.h" 00053 #include <algorithm> 00054 #include <cassert> 00055 #include <cstring> 00056 #include <iterator> 00057 #include <utility> 00058 00059 // Allow detecting C++11 feature availability when building with Clang without 00060 // breaking other compilers. 00061 #ifndef __has_feature 00062 # define __has_feature(x) 0 00063 #endif 00064 00065 namespace llvm { 00066 00067 /// \brief An opaque object representing a hash code. 00068 /// 00069 /// This object represents the result of hashing some entity. It is intended to 00070 /// be used to implement hashtables or other hashing-based data structures. 00071 /// While it wraps and exposes a numeric value, this value should not be 00072 /// trusted to be stable or predictable across processes or executions. 00073 /// 00074 /// In order to obtain the hash_code for an object 'x': 00075 /// \code 00076 /// using llvm::hash_value; 00077 /// llvm::hash_code code = hash_value(x); 00078 /// \endcode 00079 class hash_code { 00080 size_t value; 00081 00082 public: 00083 /// \brief Default construct a hash_code. 00084 /// Note that this leaves the value uninitialized. 00085 hash_code() {} 00086 00087 /// \brief Form a hash code directly from a numerical value. 00088 hash_code(size_t value) : value(value) {} 00089 00090 /// \brief Convert the hash code to its numerical value for use. 00091 /*explicit*/ operator size_t() const { return value; } 00092 00093 friend bool operator==(const hash_code &lhs, const hash_code &rhs) { 00094 return lhs.value == rhs.value; 00095 } 00096 friend bool operator!=(const hash_code &lhs, const hash_code &rhs) { 00097 return lhs.value != rhs.value; 00098 } 00099 00100 /// \brief Allow a hash_code to be directly run through hash_value. 00101 friend size_t hash_value(const hash_code &code) { return code.value; } 00102 }; 00103 00104 /// \brief Compute a hash_code for any integer value. 00105 /// 00106 /// Note that this function is intended to compute the same hash_code for 00107 /// a particular value without regard to the pre-promotion type. This is in 00108 /// contrast to hash_combine which may produce different hash_codes for 00109 /// differing argument types even if they would implicit promote to a common 00110 /// type without changing the value. 00111 template <typename T> 00112 typename enable_if<is_integral_or_enum<T>, hash_code>::type hash_value(T value); 00113 00114 /// \brief Compute a hash_code for a pointer's address. 00115 /// 00116 /// N.B.: This hashes the *address*. Not the value and not the type. 00117 template <typename T> hash_code hash_value(const T *ptr); 00118 00119 /// \brief Compute a hash_code for a pair of objects. 00120 template <typename T, typename U> 00121 hash_code hash_value(const std::pair<T, U> &arg); 00122 00123 /// \brief Compute a hash_code for a standard string. 00124 template <typename T> 00125 hash_code hash_value(const std::basic_string<T> &arg); 00126 00127 00128 /// \brief Override the execution seed with a fixed value. 00129 /// 00130 /// This hashing library uses a per-execution seed designed to change on each 00131 /// run with high probability in order to ensure that the hash codes are not 00132 /// attackable and to ensure that output which is intended to be stable does 00133 /// not rely on the particulars of the hash codes produced. 00134 /// 00135 /// That said, there are use cases where it is important to be able to 00136 /// reproduce *exactly* a specific behavior. To that end, we provide a function 00137 /// which will forcibly set the seed to a fixed value. This must be done at the 00138 /// start of the program, before any hashes are computed. Also, it cannot be 00139 /// undone. This makes it thread-hostile and very hard to use outside of 00140 /// immediately on start of a simple program designed for reproducible 00141 /// behavior. 00142 void set_fixed_execution_hash_seed(size_t fixed_value); 00143 00144 00145 // All of the implementation details of actually computing the various hash 00146 // code values are held within this namespace. These routines are included in 00147 // the header file mainly to allow inlining and constant propagation. 00148 namespace hashing { 00149 namespace detail { 00150 00151 inline uint64_t fetch64(const char *p) { 00152 uint64_t result; 00153 memcpy(&result, p, sizeof(result)); 00154 if (sys::IsBigEndianHost) 00155 return sys::SwapByteOrder(result); 00156 return result; 00157 } 00158 00159 inline uint32_t fetch32(const char *p) { 00160 uint32_t result; 00161 memcpy(&result, p, sizeof(result)); 00162 if (sys::IsBigEndianHost) 00163 return sys::SwapByteOrder(result); 00164 return result; 00165 } 00166 00167 /// Some primes between 2^63 and 2^64 for various uses. 00168 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL; 00169 static const uint64_t k1 = 0xb492b66fbe98f273ULL; 00170 static const uint64_t k2 = 0x9ae16a3b2f90404fULL; 00171 static const uint64_t k3 = 0xc949d7c7509e6557ULL; 00172 00173 /// \brief Bitwise right rotate. 00174 /// Normally this will compile to a single instruction, especially if the 00175 /// shift is a manifest constant. 00176 inline uint64_t rotate(uint64_t val, size_t shift) { 00177 // Avoid shifting by 64: doing so yields an undefined result. 00178 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); 00179 } 00180 00181 inline uint64_t shift_mix(uint64_t val) { 00182 return val ^ (val >> 47); 00183 } 00184 00185 inline uint64_t hash_16_bytes(uint64_t low, uint64_t high) { 00186 // Murmur-inspired hashing. 00187 const uint64_t kMul = 0x9ddfea08eb382d69ULL; 00188 uint64_t a = (low ^ high) * kMul; 00189 a ^= (a >> 47); 00190 uint64_t b = (high ^ a) * kMul; 00191 b ^= (b >> 47); 00192 b *= kMul; 00193 return b; 00194 } 00195 00196 inline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) { 00197 uint8_t a = s[0]; 00198 uint8_t b = s[len >> 1]; 00199 uint8_t c = s[len - 1]; 00200 uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8); 00201 uint32_t z = len + (static_cast<uint32_t>(c) << 2); 00202 return shift_mix(y * k2 ^ z * k3 ^ seed) * k2; 00203 } 00204 00205 inline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) { 00206 uint64_t a = fetch32(s); 00207 return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4)); 00208 } 00209 00210 inline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) { 00211 uint64_t a = fetch64(s); 00212 uint64_t b = fetch64(s + len - 8); 00213 return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b; 00214 } 00215 00216 inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) { 00217 uint64_t a = fetch64(s) * k1; 00218 uint64_t b = fetch64(s + 8); 00219 uint64_t c = fetch64(s + len - 8) * k2; 00220 uint64_t d = fetch64(s + len - 16) * k0; 00221 return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d, 00222 a + rotate(b ^ k3, 20) - c + len + seed); 00223 } 00224 00225 inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) { 00226 uint64_t z = fetch64(s + 24); 00227 uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0; 00228 uint64_t b = rotate(a + z, 52); 00229 uint64_t c = rotate(a, 37); 00230 a += fetch64(s + 8); 00231 c += rotate(a, 7); 00232 a += fetch64(s + 16); 00233 uint64_t vf = a + z; 00234 uint64_t vs = b + rotate(a, 31) + c; 00235 a = fetch64(s + 16) + fetch64(s + len - 32); 00236 z = fetch64(s + len - 8); 00237 b = rotate(a + z, 52); 00238 c = rotate(a, 37); 00239 a += fetch64(s + len - 24); 00240 c += rotate(a, 7); 00241 a += fetch64(s + len - 16); 00242 uint64_t wf = a + z; 00243 uint64_t ws = b + rotate(a, 31) + c; 00244 uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0); 00245 return shift_mix((seed ^ (r * k0)) + vs) * k2; 00246 } 00247 00248 inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) { 00249 if (length >= 4 && length <= 8) 00250 return hash_4to8_bytes(s, length, seed); 00251 if (length > 8 && length <= 16) 00252 return hash_9to16_bytes(s, length, seed); 00253 if (length > 16 && length <= 32) 00254 return hash_17to32_bytes(s, length, seed); 00255 if (length > 32) 00256 return hash_33to64_bytes(s, length, seed); 00257 if (length != 0) 00258 return hash_1to3_bytes(s, length, seed); 00259 00260 return k2 ^ seed; 00261 } 00262 00263 /// \brief The intermediate state used during hashing. 00264 /// Currently, the algorithm for computing hash codes is based on CityHash and 00265 /// keeps 56 bytes of arbitrary state. 00266 struct hash_state { 00267 uint64_t h0, h1, h2, h3, h4, h5, h6; 00268 uint64_t seed; 00269 00270 /// \brief Create a new hash_state structure and initialize it based on the 00271 /// seed and the first 64-byte chunk. 00272 /// This effectively performs the initial mix. 00273 static hash_state create(const char *s, uint64_t seed) { 00274 hash_state state = { 00275 0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49), 00276 seed * k1, shift_mix(seed), 0, seed }; 00277 state.h6 = hash_16_bytes(state.h4, state.h5); 00278 state.mix(s); 00279 return state; 00280 } 00281 00282 /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a' 00283 /// and 'b', including whatever is already in 'a' and 'b'. 00284 static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) { 00285 a += fetch64(s); 00286 uint64_t c = fetch64(s + 24); 00287 b = rotate(b + a + c, 21); 00288 uint64_t d = a; 00289 a += fetch64(s + 8) + fetch64(s + 16); 00290 b += rotate(a, 44) + d; 00291 a += c; 00292 } 00293 00294 /// \brief Mix in a 64-byte buffer of data. 00295 /// We mix all 64 bytes even when the chunk length is smaller, but we 00296 /// record the actual length. 00297 void mix(const char *s) { 00298 h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1; 00299 h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1; 00300 h0 ^= h6; 00301 h1 += h3 + fetch64(s + 40); 00302 h2 = rotate(h2 + h5, 33) * k1; 00303 h3 = h4 * k1; 00304 h4 = h0 + h5; 00305 mix_32_bytes(s, h3, h4); 00306 h5 = h2 + h6; 00307 h6 = h1 + fetch64(s + 16); 00308 mix_32_bytes(s + 32, h5, h6); 00309 std::swap(h2, h0); 00310 } 00311 00312 /// \brief Compute the final 64-bit hash code value based on the current 00313 /// state and the length of bytes hashed. 00314 uint64_t finalize(size_t length) { 00315 return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2, 00316 hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0); 00317 } 00318 }; 00319 00320 00321 /// \brief A global, fixed seed-override variable. 00322 /// 00323 /// This variable can be set using the \see llvm::set_fixed_execution_seed 00324 /// function. See that function for details. Do not, under any circumstances, 00325 /// set or read this variable. 00326 extern size_t fixed_seed_override; 00327 00328 inline size_t get_execution_seed() { 00329 // FIXME: This needs to be a per-execution seed. This is just a placeholder 00330 // implementation. Switching to a per-execution seed is likely to flush out 00331 // instability bugs and so will happen as its own commit. 00332 // 00333 // However, if there is a fixed seed override set the first time this is 00334 // called, return that instead of the per-execution seed. 00335 const uint64_t seed_prime = 0xff51afd7ed558ccdULL; 00336 static size_t seed = fixed_seed_override ? fixed_seed_override 00337 : (size_t)seed_prime; 00338 return seed; 00339 } 00340 00341 00342 /// \brief Trait to indicate whether a type's bits can be hashed directly. 00343 /// 00344 /// A type trait which is true if we want to combine values for hashing by 00345 /// reading the underlying data. It is false if values of this type must 00346 /// first be passed to hash_value, and the resulting hash_codes combined. 00347 // 00348 // FIXME: We want to replace is_integral_or_enum and is_pointer here with 00349 // a predicate which asserts that comparing the underlying storage of two 00350 // values of the type for equality is equivalent to comparing the two values 00351 // for equality. For all the platforms we care about, this holds for integers 00352 // and pointers, but there are platforms where it doesn't and we would like to 00353 // support user-defined types which happen to satisfy this property. 00354 template <typename T> struct is_hashable_data 00355 : integral_constant<bool, ((is_integral_or_enum<T>::value || 00356 is_pointer<T>::value) && 00357 64 % sizeof(T) == 0)> {}; 00358 00359 // Special case std::pair to detect when both types are viable and when there 00360 // is no alignment-derived padding in the pair. This is a bit of a lie because 00361 // std::pair isn't truly POD, but it's close enough in all reasonable 00362 // implementations for our use case of hashing the underlying data. 00363 template <typename T, typename U> struct is_hashable_data<std::pair<T, U> > 00364 : integral_constant<bool, (is_hashable_data<T>::value && 00365 is_hashable_data<U>::value && 00366 (sizeof(T) + sizeof(U)) == 00367 sizeof(std::pair<T, U>))> {}; 00368 00369 /// \brief Helper to get the hashable data representation for a type. 00370 /// This variant is enabled when the type itself can be used. 00371 template <typename T> 00372 typename enable_if<is_hashable_data<T>, T>::type 00373 get_hashable_data(const T &value) { 00374 return value; 00375 } 00376 /// \brief Helper to get the hashable data representation for a type. 00377 /// This variant is enabled when we must first call hash_value and use the 00378 /// result as our data. 00379 template <typename T> 00380 typename enable_if_c<!is_hashable_data<T>::value, size_t>::type 00381 get_hashable_data(const T &value) { 00382 using ::llvm::hash_value; 00383 return hash_value(value); 00384 } 00385 00386 /// \brief Helper to store data from a value into a buffer and advance the 00387 /// pointer into that buffer. 00388 /// 00389 /// This routine first checks whether there is enough space in the provided 00390 /// buffer, and if not immediately returns false. If there is space, it 00391 /// copies the underlying bytes of value into the buffer, advances the 00392 /// buffer_ptr past the copied bytes, and returns true. 00393 template <typename T> 00394 bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value, 00395 size_t offset = 0) { 00396 size_t store_size = sizeof(value) - offset; 00397 if (buffer_ptr + store_size > buffer_end) 00398 return false; 00399 const char *value_data = reinterpret_cast<const char *>(&value); 00400 memcpy(buffer_ptr, value_data + offset, store_size); 00401 buffer_ptr += store_size; 00402 return true; 00403 } 00404 00405 /// \brief Implement the combining of integral values into a hash_code. 00406 /// 00407 /// This overload is selected when the value type of the iterator is 00408 /// integral. Rather than computing a hash_code for each object and then 00409 /// combining them, this (as an optimization) directly combines the integers. 00410 template <typename InputIteratorT> 00411 hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) { 00412 const size_t seed = get_execution_seed(); 00413 char buffer[64], *buffer_ptr = buffer; 00414 char *const buffer_end = buffer_ptr + array_lengthof(buffer); 00415 while (first != last && store_and_advance(buffer_ptr, buffer_end, 00416 get_hashable_data(*first))) 00417 ++first; 00418 if (first == last) 00419 return hash_short(buffer, buffer_ptr - buffer, seed); 00420 assert(buffer_ptr == buffer_end); 00421 00422 hash_state state = state.create(buffer, seed); 00423 size_t length = 64; 00424 while (first != last) { 00425 // Fill up the buffer. We don't clear it, which re-mixes the last round 00426 // when only a partial 64-byte chunk is left. 00427 buffer_ptr = buffer; 00428 while (first != last && store_and_advance(buffer_ptr, buffer_end, 00429 get_hashable_data(*first))) 00430 ++first; 00431 00432 // Rotate the buffer if we did a partial fill in order to simulate doing 00433 // a mix of the last 64-bytes. That is how the algorithm works when we 00434 // have a contiguous byte sequence, and we want to emulate that here. 00435 std::rotate(buffer, buffer_ptr, buffer_end); 00436 00437 // Mix this chunk into the current state. 00438 state.mix(buffer); 00439 length += buffer_ptr - buffer; 00440 }; 00441 00442 return state.finalize(length); 00443 } 00444 00445 /// \brief Implement the combining of integral values into a hash_code. 00446 /// 00447 /// This overload is selected when the value type of the iterator is integral 00448 /// and when the input iterator is actually a pointer. Rather than computing 00449 /// a hash_code for each object and then combining them, this (as an 00450 /// optimization) directly combines the integers. Also, because the integers 00451 /// are stored in contiguous memory, this routine avoids copying each value 00452 /// and directly reads from the underlying memory. 00453 template <typename ValueT> 00454 typename enable_if<is_hashable_data<ValueT>, hash_code>::type 00455 hash_combine_range_impl(ValueT *first, ValueT *last) { 00456 const size_t seed = get_execution_seed(); 00457 const char *s_begin = reinterpret_cast<const char *>(first); 00458 const char *s_end = reinterpret_cast<const char *>(last); 00459 const size_t length = std::distance(s_begin, s_end); 00460 if (length <= 64) 00461 return hash_short(s_begin, length, seed); 00462 00463 const char *s_aligned_end = s_begin + (length & ~63); 00464 hash_state state = state.create(s_begin, seed); 00465 s_begin += 64; 00466 while (s_begin != s_aligned_end) { 00467 state.mix(s_begin); 00468 s_begin += 64; 00469 } 00470 if (length & 63) 00471 state.mix(s_end - 64); 00472 00473 return state.finalize(length); 00474 } 00475 00476 } // namespace detail 00477 } // namespace hashing 00478 00479 00480 /// \brief Compute a hash_code for a sequence of values. 00481 /// 00482 /// This hashes a sequence of values. It produces the same hash_code as 00483 /// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences 00484 /// and is significantly faster given pointers and types which can be hashed as 00485 /// a sequence of bytes. 00486 template <typename InputIteratorT> 00487 hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) { 00488 return ::llvm::hashing::detail::hash_combine_range_impl(first, last); 00489 } 00490 00491 00492 // Implementation details for hash_combine. 00493 namespace hashing { 00494 namespace detail { 00495 00496 /// \brief Helper class to manage the recursive combining of hash_combine 00497 /// arguments. 00498 /// 00499 /// This class exists to manage the state and various calls involved in the 00500 /// recursive combining of arguments used in hash_combine. It is particularly 00501 /// useful at minimizing the code in the recursive calls to ease the pain 00502 /// caused by a lack of variadic functions. 00503 struct hash_combine_recursive_helper { 00504 char buffer[64]; 00505 hash_state state; 00506 const size_t seed; 00507 00508 public: 00509 /// \brief Construct a recursive hash combining helper. 00510 /// 00511 /// This sets up the state for a recursive hash combine, including getting 00512 /// the seed and buffer setup. 00513 hash_combine_recursive_helper() 00514 : seed(get_execution_seed()) {} 00515 00516 /// \brief Combine one chunk of data into the current in-flight hash. 00517 /// 00518 /// This merges one chunk of data into the hash. First it tries to buffer 00519 /// the data. If the buffer is full, it hashes the buffer into its 00520 /// hash_state, empties it, and then merges the new chunk in. This also 00521 /// handles cases where the data straddles the end of the buffer. 00522 template <typename T> 00523 char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) { 00524 if (!store_and_advance(buffer_ptr, buffer_end, data)) { 00525 // Check for skew which prevents the buffer from being packed, and do 00526 // a partial store into the buffer to fill it. This is only a concern 00527 // with the variadic combine because that formation can have varying 00528 // argument types. 00529 size_t partial_store_size = buffer_end - buffer_ptr; 00530 memcpy(buffer_ptr, &data, partial_store_size); 00531 00532 // If the store fails, our buffer is full and ready to hash. We have to 00533 // either initialize the hash state (on the first full buffer) or mix 00534 // this buffer into the existing hash state. Length tracks the *hashed* 00535 // length, not the buffered length. 00536 if (length == 0) { 00537 state = state.create(buffer, seed); 00538 length = 64; 00539 } else { 00540 // Mix this chunk into the current state and bump length up by 64. 00541 state.mix(buffer); 00542 length += 64; 00543 } 00544 // Reset the buffer_ptr to the head of the buffer for the next chunk of 00545 // data. 00546 buffer_ptr = buffer; 00547 00548 // Try again to store into the buffer -- this cannot fail as we only 00549 // store types smaller than the buffer. 00550 if (!store_and_advance(buffer_ptr, buffer_end, data, 00551 partial_store_size)) 00552 abort(); 00553 } 00554 return buffer_ptr; 00555 } 00556 00557 #if defined(__has_feature) && __has_feature(__cxx_variadic_templates__) 00558 00559 /// \brief Recursive, variadic combining method. 00560 /// 00561 /// This function recurses through each argument, combining that argument 00562 /// into a single hash. 00563 template <typename T, typename ...Ts> 00564 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00565 const T &arg, const Ts &...args) { 00566 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg)); 00567 00568 // Recurse to the next argument. 00569 return combine(length, buffer_ptr, buffer_end, args...); 00570 } 00571 00572 #else 00573 // Manually expanded recursive combining methods. See variadic above for 00574 // documentation. 00575 00576 template <typename T1, typename T2, typename T3, typename T4, typename T5, 00577 typename T6> 00578 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00579 const T1 &arg1, const T2 &arg2, const T3 &arg3, 00580 const T4 &arg4, const T5 &arg5, const T6 &arg6) { 00581 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1)); 00582 return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5, arg6); 00583 } 00584 template <typename T1, typename T2, typename T3, typename T4, typename T5> 00585 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00586 const T1 &arg1, const T2 &arg2, const T3 &arg3, 00587 const T4 &arg4, const T5 &arg5) { 00588 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1)); 00589 return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4, arg5); 00590 } 00591 template <typename T1, typename T2, typename T3, typename T4> 00592 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00593 const T1 &arg1, const T2 &arg2, const T3 &arg3, 00594 const T4 &arg4) { 00595 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1)); 00596 return combine(length, buffer_ptr, buffer_end, arg2, arg3, arg4); 00597 } 00598 template <typename T1, typename T2, typename T3> 00599 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00600 const T1 &arg1, const T2 &arg2, const T3 &arg3) { 00601 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1)); 00602 return combine(length, buffer_ptr, buffer_end, arg2, arg3); 00603 } 00604 template <typename T1, typename T2> 00605 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00606 const T1 &arg1, const T2 &arg2) { 00607 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1)); 00608 return combine(length, buffer_ptr, buffer_end, arg2); 00609 } 00610 template <typename T1> 00611 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 00612 const T1 &arg1) { 00613 buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg1)); 00614 return combine(length, buffer_ptr, buffer_end); 00615 } 00616 00617 #endif 00618 00619 /// \brief Base case for recursive, variadic combining. 00620 /// 00621 /// The base case when combining arguments recursively is reached when all 00622 /// arguments have been handled. It flushes the remaining buffer and 00623 /// constructs a hash_code. 00624 hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) { 00625 // Check whether the entire set of values fit in the buffer. If so, we'll 00626 // use the optimized short hashing routine and skip state entirely. 00627 if (length == 0) 00628 return hash_short(buffer, buffer_ptr - buffer, seed); 00629 00630 // Mix the final buffer, rotating it if we did a partial fill in order to 00631 // simulate doing a mix of the last 64-bytes. That is how the algorithm 00632 // works when we have a contiguous byte sequence, and we want to emulate 00633 // that here. 00634 std::rotate(buffer, buffer_ptr, buffer_end); 00635 00636 // Mix this chunk into the current state. 00637 state.mix(buffer); 00638 length += buffer_ptr - buffer; 00639 00640 return state.finalize(length); 00641 } 00642 }; 00643 00644 } // namespace detail 00645 } // namespace hashing 00646 00647 00648 #if __has_feature(__cxx_variadic_templates__) 00649 00650 /// \brief Combine values into a single hash_code. 00651 /// 00652 /// This routine accepts a varying number of arguments of any type. It will 00653 /// attempt to combine them into a single hash_code. For user-defined types it 00654 /// attempts to call a \see hash_value overload (via ADL) for the type. For 00655 /// integer and pointer types it directly combines their data into the 00656 /// resulting hash_code. 00657 /// 00658 /// The result is suitable for returning from a user's hash_value 00659 /// *implementation* for their user-defined type. Consumers of a type should 00660 /// *not* call this routine, they should instead call 'hash_value'. 00661 template <typename ...Ts> hash_code hash_combine(const Ts &...args) { 00662 // Recursively hash each argument using a helper class. 00663 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00664 return helper.combine(0, helper.buffer, helper.buffer + 64, args...); 00665 } 00666 00667 #else 00668 00669 // What follows are manually exploded overloads for each argument width. See 00670 // the above variadic definition for documentation and specification. 00671 00672 template <typename T1, typename T2, typename T3, typename T4, typename T5, 00673 typename T6> 00674 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, 00675 const T4 &arg4, const T5 &arg5, const T6 &arg6) { 00676 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00677 return helper.combine(0, helper.buffer, helper.buffer + 64, 00678 arg1, arg2, arg3, arg4, arg5, arg6); 00679 } 00680 template <typename T1, typename T2, typename T3, typename T4, typename T5> 00681 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, 00682 const T4 &arg4, const T5 &arg5) { 00683 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00684 return helper.combine(0, helper.buffer, helper.buffer + 64, 00685 arg1, arg2, arg3, arg4, arg5); 00686 } 00687 template <typename T1, typename T2, typename T3, typename T4> 00688 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3, 00689 const T4 &arg4) { 00690 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00691 return helper.combine(0, helper.buffer, helper.buffer + 64, 00692 arg1, arg2, arg3, arg4); 00693 } 00694 template <typename T1, typename T2, typename T3> 00695 hash_code hash_combine(const T1 &arg1, const T2 &arg2, const T3 &arg3) { 00696 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00697 return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2, arg3); 00698 } 00699 template <typename T1, typename T2> 00700 hash_code hash_combine(const T1 &arg1, const T2 &arg2) { 00701 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00702 return helper.combine(0, helper.buffer, helper.buffer + 64, arg1, arg2); 00703 } 00704 template <typename T1> 00705 hash_code hash_combine(const T1 &arg1) { 00706 ::llvm::hashing::detail::hash_combine_recursive_helper helper; 00707 return helper.combine(0, helper.buffer, helper.buffer + 64, arg1); 00708 } 00709 00710 #endif 00711 00712 00713 // Implementation details for implementations of hash_value overloads provided 00714 // here. 00715 namespace hashing { 00716 namespace detail { 00717 00718 /// \brief Helper to hash the value of a single integer. 00719 /// 00720 /// Overloads for smaller integer types are not provided to ensure consistent 00721 /// behavior in the presence of integral promotions. Essentially, 00722 /// "hash_value('4')" and "hash_value('0' + 4)" should be the same. 00723 inline hash_code hash_integer_value(uint64_t value) { 00724 // Similar to hash_4to8_bytes but using a seed instead of length. 00725 const uint64_t seed = get_execution_seed(); 00726 const char *s = reinterpret_cast<const char *>(&value); 00727 const uint64_t a = fetch32(s); 00728 return hash_16_bytes(seed + (a << 3), fetch32(s + 4)); 00729 } 00730 00731 } // namespace detail 00732 } // namespace hashing 00733 00734 // Declared and documented above, but defined here so that any of the hashing 00735 // infrastructure is available. 00736 template <typename T> 00737 typename enable_if<is_integral_or_enum<T>, hash_code>::type 00738 hash_value(T value) { 00739 return ::llvm::hashing::detail::hash_integer_value(value); 00740 } 00741 00742 // Declared and documented above, but defined here so that any of the hashing 00743 // infrastructure is available. 00744 template <typename T> hash_code hash_value(const T *ptr) { 00745 return ::llvm::hashing::detail::hash_integer_value( 00746 reinterpret_cast<uintptr_t>(ptr)); 00747 } 00748 00749 // Declared and documented above, but defined here so that any of the hashing 00750 // infrastructure is available. 00751 template <typename T, typename U> 00752 hash_code hash_value(const std::pair<T, U> &arg) { 00753 return hash_combine(arg.first, arg.second); 00754 } 00755 00756 // Declared and documented above, but defined here so that any of the hashing 00757 // infrastructure is available. 00758 template <typename T> 00759 hash_code hash_value(const std::basic_string<T> &arg) { 00760 return hash_combine_range(arg.begin(), arg.end()); 00761 } 00762 00763 } // namespace llvm 00764 00765 #endif