Line data Source code
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 :
60 : /// An opaque object representing a hash code.
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:
76 : /// Default construct a hash_code.
77 : /// Note that this leaves the value uninitialized.
78 : hash_code() = default;
79 :
80 : /// Form a hash code directly from a numerical value.
81 4546 : hash_code(size_t value) : value(value) {}
82 :
83 : /// Convert the hash code to its numerical value for use.
84 0 : /*explicit*/ operator size_t() const { return value; }
85 :
86 0 : friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
87 70 : return lhs.value == rhs.value;
88 : }
89 0 : friend bool operator!=(const hash_code &lhs, const hash_code &rhs) {
90 0 : return lhs.value != rhs.value;
91 : }
92 :
93 : /// Allow a hash_code to be directly run through hash_value.
94 0 : friend size_t hash_value(const hash_code &code) { return code.value; }
95 : };
96 :
97 : /// Compute a hash_code for any integer value.
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 :
108 : /// Compute a hash_code for a pointer's address.
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 :
113 : /// Compute a hash_code for a pair of objects.
114 : template <typename T, typename U>
115 : hash_code hash_value(const std::pair<T, U> &arg);
116 :
117 : /// Compute a hash_code for a standard string.
118 : template <typename T>
119 : hash_code hash_value(const std::basic_string<T> &arg);
120 :
121 :
122 : /// Override the execution seed with a fixed value.
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.
136 : void set_fixed_execution_hash_seed(uint64_t fixed_value);
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 5675950800 : 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 626313028 : 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 :
167 : /// Bitwise right rotate.
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 4144992665 : return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
173 : }
174 :
175 : inline uint64_t shift_mix(uint64_t val) {
176 511026774 : 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 1576398364 : uint64_t a = (low ^ high) * kMul;
183 1576398364 : a ^= (a >> 47);
184 1576398364 : uint64_t b = (high ^ a) * kMul;
185 1576398364 : b ^= (b >> 47);
186 1576398364 : b *= kMul;
187 : return b;
188 : }
189 :
190 : inline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) {
191 1533845 : uint8_t a = s[0];
192 1533845 : uint8_t b = s[len >> 1];
193 1533845 : uint8_t c = s[len - 1];
194 1533845 : uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
195 1533845 : uint32_t z = len + (static_cast<uint32_t>(c) << 2);
196 3067690 : 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 313155705 : uint64_t a = fetch32(s);
201 626311410 : 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 474249665 : uint64_t b = fetch64(s + len - 8);
207 1422748995 : return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
208 : }
209 :
210 557195098 : inline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) {
211 557195098 : uint64_t a = fetch64(s) * k1;
212 : uint64_t b = fetch64(s + 8);
213 1114390196 : uint64_t c = fetch64(s + len - 8) * k2;
214 1114390196 : uint64_t d = fetch64(s + len - 16) * k0;
215 1671585294 : return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d,
216 1671585294 : a + rotate(b ^ k3, 20) - c + len + seed);
217 : }
218 :
219 185208404 : inline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) {
220 : uint64_t z = fetch64(s + 24);
221 370416808 : uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
222 185208404 : uint64_t b = rotate(a + z, 52);
223 : uint64_t c = rotate(a, 37);
224 185208404 : a += fetch64(s + 8);
225 185208404 : c += rotate(a, 7);
226 185208404 : a += fetch64(s + 16);
227 185208404 : uint64_t vf = a + z;
228 185208404 : uint64_t vs = b + rotate(a, 31) + c;
229 370416808 : a = fetch64(s + 16) + fetch64(s + len - 32);
230 185208404 : z = fetch64(s + len - 8);
231 185208404 : b = rotate(a + z, 52);
232 : c = rotate(a, 37);
233 370416808 : a += fetch64(s + len - 24);
234 185208404 : c += rotate(a, 7);
235 185208404 : a += fetch64(s + len - 16);
236 185208404 : uint64_t wf = a + z;
237 185208404 : uint64_t ws = b + rotate(a, 31) + c;
238 185208404 : uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
239 370416808 : return shift_mix((seed ^ (r * k0)) + vs) * k2;
240 : }
241 :
242 1553601475 : inline uint64_t hash_short(const char *s, size_t length, uint64_t seed) {
243 1553601475 : if (length >= 4 && length <= 8)
244 313155705 : return hash_4to8_bytes(s, length, seed);
245 1240445770 : if (length > 8 && length <= 16)
246 474249665 : return hash_9to16_bytes(s, length, seed);
247 766196105 : if (length > 16 && length <= 32)
248 557195098 : return hash_17to32_bytes(s, length, seed);
249 209001007 : if (length > 32)
250 185208404 : return hash_33to64_bytes(s, length, seed);
251 23792603 : if (length != 0)
252 1533845 : return hash_1to3_bytes(s, length, seed);
253 :
254 22258758 : return k2 ^ seed;
255 : }
256 :
257 : /// The intermediate state used during hashing.
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 :
263 : /// Create a new hash_state structure and initialize it based on the
264 : /// seed and the first 64-byte chunk.
265 : /// This effectively performs the initial mix.
266 46358707 : static hash_state create(const char *s, uint64_t seed) {
267 46358707 : hash_state state = {
268 : 0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49),
269 185434828 : seed * k1, shift_mix(seed), 0 };
270 46358707 : state.h6 = hash_16_bytes(state.h4, state.h5);
271 46358707 : state.mix(s);
272 46358706 : return state;
273 : }
274 :
275 : /// Mix 32-bytes from the input sequence into the 16-bytes of 'a'
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 240442450 : a += fetch64(s);
279 : uint64_t c = fetch64(s + 24);
280 480884900 : b = rotate(b + a + c, 21);
281 : uint64_t d = a;
282 240442450 : a += fetch64(s + 8) + fetch64(s + 16);
283 240442450 : b += rotate(a, 44) + d;
284 240442450 : a += c;
285 : }
286 :
287 : /// Mix in a 64-byte buffer of data.
288 : /// We mix all 64 bytes even when the chunk length is smaller, but we
289 : /// record the actual length.
290 120221225 : void mix(const char *s) {
291 360663675 : h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
292 360663675 : h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1;
293 120221225 : h0 ^= h6;
294 120221225 : h1 += h3 + fetch64(s + 40);
295 240442450 : h2 = rotate(h2 + h5, 33) * k1;
296 120221225 : h3 = h4 * k1;
297 120221225 : h4 = h0 + h5;
298 : mix_32_bytes(s, h3, h4);
299 120221225 : h5 = h2 + h6;
300 120221225 : h6 = h1 + fetch64(s + 16);
301 : mix_32_bytes(s + 32, h5, h6);
302 : std::swap(h2, h0);
303 120221225 : }
304 :
305 : /// Compute the final 64-bit hash code value based on the current
306 : /// state and the length of bytes hashed.
307 46358707 : uint64_t finalize(size_t length) {
308 139076121 : return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
309 139076121 : hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
310 : }
311 : };
312 :
313 :
314 : /// A global, fixed seed-override variable.
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.
319 : extern uint64_t fixed_seed_override;
320 :
321 1599961800 : inline uint64_t get_execution_seed() {
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;
329 1599961800 : static uint64_t seed = fixed_seed_override ? fixed_seed_override : seed_prime;
330 1599961800 : return seed;
331 : }
332 :
333 :
334 : /// Trait to indicate whether a type's bits can be hashed directly.
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 :
361 : /// Helper to get the hashable data representation for a type.
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 0 : get_hashable_data(const T &value) {
366 13 : return value;
367 : }
368 0 : /// Helper to get the hashable data representation for a type.
369 0 : /// This variant is enabled when we must first call hash_value and use the
370 : /// result as our data.
371 0 : template <typename T>
372 0 : typename std::enable_if<!is_hashable_data<T>::value, size_t>::type
373 0 : get_hashable_data(const T &value) {
374 0 : using ::llvm::hash_value;
375 1014057 : return hash_value(value);
376 : }
377 0 :
378 0 : /// Helper to store data from a value into a buffer and advance the
379 : /// pointer into that buffer.
380 0 : ///
381 13141822 : /// This routine first checks whether there is enough space in the provided
382 0 : /// buffer, and if not immediately returns false. If there is space, it
383 0 : /// copies the underlying bytes of value into the buffer, advances the
384 5864 : /// buffer_ptr past the copied bytes, and returns true.
385 0 : template <typename T>
386 0 : bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value,
387 1824421 : size_t offset = 0) {
388 0 : size_t store_size = sizeof(value) - offset;
389 138433778 : if (buffer_ptr + store_size > buffer_end)
390 0 : return false;
391 : const char *value_data = reinterpret_cast<const char *>(&value);
392 134308981 : memcpy(buffer_ptr, value_data + offset, store_size);
393 1347540 : buffer_ptr += store_size;
394 0 : return true;
395 41062978 : }
396 42839590 :
397 0 : /// Implement the combining of integral values into a hash_code.
398 42794548 : ///
399 0 : /// This overload is selected when the value type of the iterator is
400 0 : /// integral. Rather than computing a hash_code for each object and then
401 7276220 : /// combining them, this (as an optimization) directly combines the integers.
402 0 : template <typename InputIteratorT>
403 1882091 : hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
404 7581033 : const uint64_t seed = get_execution_seed();
405 0 : char buffer[64], *buffer_ptr = buffer;
406 16 : char *const buffer_end = std::end(buffer);
407 7378968 : while (first != last && store_and_advance(buffer_ptr, buffer_end,
408 230 : get_hashable_data(*first)))
409 136246 : ++first;
410 151974777 : if (first == last)
411 1859325 : return hash_short(buffer, buffer_ptr - buffer, seed);
412 494 : assert(buffer_ptr == buffer_end);
413 200635599 :
414 22766 : hash_state state = state.create(buffer, seed);
415 44839 : size_t length = 64;
416 43710262 : while (first != last) {
417 20284 : // Fill up the buffer. We don't clear it, which re-mixes the last round
418 558 : // when only a partial 64-byte chunk is left.
419 1720152 : buffer_ptr = buffer;
420 38729871 : while (first != last && store_and_advance(buffer_ptr, buffer_end,
421 16 : get_hashable_data(*first)))
422 32901510 : ++first;
423 27751 :
424 0 : // Rotate the buffer if we did a partial fill in order to simulate doing
425 150 : // a mix of the last 64-bytes. That is how the algorithm works when we
426 17220 : // have a contiguous byte sequence, and we want to emulate that here.
427 3 : std::rotate(buffer, buffer_ptr, buffer_end);
428 40082 :
429 15 : // Mix this chunk into the current state.
430 4124797 : state.mix(buffer);
431 4124797 : length += buffer_ptr - buffer;
432 764145 : };
433 747056 :
434 91155102 : return state.finalize(length);
435 0 : }
436 0 :
437 91132334 : /// Implement the combining of integral values into a hash_code.
438 2 : ///
439 1 : /// This overload is selected when the value type of the iterator is integral
440 0 : /// and when the input iterator is actually a pointer. Rather than computing
441 0 : /// a hash_code for each object and then combining them, this (as an
442 22978 : /// optimization) directly combines the integers. Also, because the integers
443 22978 : /// are stored in contiguous memory, this routine avoids copying each value
444 : /// and directly reads from the underlying memory.
445 : template <typename ValueT>
446 17088 : typename std::enable_if<is_hashable_data<ValueT>::value, hash_code>::type
447 1374734963 : hash_combine_range_impl(ValueT *first, ValueT *last) {
448 1374734964 : const uint64_t seed = get_execution_seed();
449 1 : const char *s_begin = reinterpret_cast<const char *>(first);
450 : const char *s_end = reinterpret_cast<const char *>(last);
451 1374734964 : const size_t length = std::distance(s_begin, s_end);
452 1374734965 : if (length <= 64)
453 1338487031 : return hash_short(s_begin, length, seed);
454 10026031 :
455 46273964 : const char *s_aligned_end = s_begin + (length & ~63);
456 46276586 : hash_state state = state.create(s_begin, seed);
457 56302612 : s_begin += 64;
458 81356364 : while (s_begin != s_aligned_end) {
459 35232693 : state.mix(s_begin);
460 25228009 : s_begin += 64;
461 21395 : }
462 46295353 : if (length & 63)
463 44738963 : state.mix(s_end - 64);
464 190885 :
465 46445946 : return state.finalize(length);
466 3061 : }
467 136 :
468 21743888 : } // namespace detail
469 21743358 : } // namespace hashing
470 134 :
471 18769 :
472 21724988 : /// Compute a hash_code for a sequence of values.
473 21730992 : ///
474 21706530 : /// This hashes a sequence of values. It produces the same hash_code as
475 134 : /// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
476 24596 : /// and is significantly faster given pointers and types which can be hashed as
477 30735 : /// a sequence of bytes.
478 30601 : template <typename InputIteratorT>
479 93303 : hash_code hash_combine_range(InputIteratorT first, InputIteratorT last) {
480 1351002959 : return ::llvm::hashing::detail::hash_combine_range_impl(first, last);
481 62711 : }
482 5 :
483 24600 :
484 7269 : // Implementation details for hash_combine.
485 1 : namespace hashing {
486 13186144 : namespace detail {
487 9 :
488 13 : /// Helper class to manage the recursive combining of hash_combine
489 711 : /// arguments.
490 : ///
491 88 : /// This class exists to manage the state and various calls involved in the
492 118273 : /// recursive combining of arguments used in hash_combine. It is particularly
493 0 : /// useful at minimizing the code in the recursive calls to ease the pain
494 9 : /// caused by a lack of variadic functions.
495 9 : struct hash_combine_recursive_helper {
496 0 : char buffer[64];
497 0 : hash_state state;
498 0 : const uint64_t seed;
499 0 :
500 0 : public:
501 1334816 : /// Construct a recursive hash combining helper.
502 0 : ///
503 0 : /// This sets up the state for a recursive hash combine, including getting
504 68125 : /// the seed and buffer setup.
505 0 : hash_combine_recursive_helper()
506 190741469 : : seed(get_execution_seed()) {}
507 :
508 21724987 : /// Combine one chunk of data into the current in-flight hash.
509 21724987 : ///
510 : /// This merges one chunk of data into the hash. First it tries to buffer
511 0 : /// the data. If the buffer is full, it hashes the buffer into its
512 50039799 : /// hash_state, empties it, and then merges the new chunk in. This also
513 21724987 : /// handles cases where the data straddles the end of the buffer.
514 21700391 : template <typename T>
515 96876461 : char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) {
516 96475633 : if (!store_and_advance(buffer_ptr, buffer_end, data)) {
517 24596 : // Check for skew which prevents the buffer from being packed, and do
518 2717360 : // a partial store into the buffer to fill it. This is only a concern
519 87302 : // with the variadic combine because that formation can have varying
520 62707 : // argument types.
521 41072128 : size_t partial_store_size = buffer_end - buffer_ptr;
522 40965243 : memcpy(buffer_ptr, &data, partial_store_size);
523 24596 :
524 2267851 : // If the store fails, our buffer is full and ready to hash. We have to
525 1731012 : // either initialize the hash state (on the first full buffer) or mix
526 30604 : // this buffer into the existing hash state. Length tracks the *hashed*
527 10895886 : // length, not the buffered length.
528 3106246 : if (length == 0) {
529 0 : state = state.create(buffer, seed);
530 15923688 : length = 64;
531 176712 : } else {
532 : // Mix this chunk into the current state and bump length up by 64.
533 374891 : state.mix(buffer);
534 374891 : length += 64;
535 0 : }
536 11373523 : // Reset the buffer_ptr to the head of the buffer for the next chunk of
537 11373523 : // data.
538 0 : buffer_ptr = buffer;
539 39497082 :
540 39497082 : // Try again to store into the buffer -- this cannot fail as we only
541 21727402 : // store types smaller than the buffer.
542 0 : if (!store_and_advance(buffer_ptr, buffer_end, data,
543 0 : partial_store_size))
544 0 : abort();
545 0 : }
546 96451037 : return buffer_ptr;
547 0 : }
548 1014230 :
549 1014230 : /// Recursive, variadic combining method.
550 0 : ///
551 50394260 : /// This function recurses through each argument, combining that argument
552 40993511 : /// into a single hash.
553 1 : template <typename T, typename ...Ts>
554 13150839 : hash_code combine(size_t length, char *buffer_ptr, char *buffer_end,
555 14878802 : const T &arg, const Ts &...args) {
556 3048 : buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
557 419498 :
558 3525742 : // Recurse to the next argument.
559 3048 : return combine(length, buffer_ptr, buffer_end, args...);
560 91238587 : }
561 91467177 :
562 50354 : /// Base case for recursive, variadic combining.
563 23613 : ///
564 396980 : /// The base case when combining arguments recursively is reached when all
565 0 : /// arguments have been handled. It flushes the remaining buffer and
566 1532 : /// constructs a hash_code.
567 191620058 : hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) {
568 1524 : // Check whether the entire set of values fit in the buffer. If so, we'll
569 341822 : // use the optimized short hashing routine and skip state entirely.
570 134131243 : if (length == 0)
571 94290815 : return hash_short(buffer, buffer_ptr - buffer, seed);
572 32706 :
573 34230 : // Mix the final buffer, rotating it if we did a partial fill in order to
574 0 : // simulate doing a mix of the last 64-bytes. That is how the algorithm
575 0 : // works when we have a contiguous byte sequence, and we want to emulate
576 149697393 : // that here.
577 149697393 : std::rotate(buffer, buffer_ptr, buffer_end);
578 0 :
579 1014230 : // Mix this chunk into the current state.
580 0 : state.mix(buffer);
581 1080302 : length += buffer_ptr - buffer;
582 1014612 :
583 0 : return state.finalize(length);
584 65690 : }
585 13213480 : };
586 1 :
587 13761323 : } // namespace detail
588 14180816 : } // namespace hashing
589 0 :
590 629827 : /// Combine values into a single hash_code.
591 91868412 : ///
592 50354 : /// This routine accepts a varying number of arguments of any type. It will
593 499507 : /// attempt to combine them into a single hash_code. For user-defined types it
594 529548 : /// attempts to call a \see hash_value overload (via ADL) for the type. For
595 7952 : /// integer and pointer types it directly combines their data into the
596 22089 : /// resulting hash_code.
597 22097 : ///
598 0 : /// The result is suitable for returning from a user's hash_value
599 3 : /// *implementation* for their user-defined type. Consumers of a type should
600 341825 : /// *not* call this routine, they should instead call 'hash_value'.
601 94290814 : template <typename ...Ts> hash_code hash_combine(const Ts &...args) {
602 739585 : // Recursively hash each argument using a helper class.
603 772291 : ::llvm::hashing::detail::hash_combine_recursive_helper helper;
604 94290815 : return helper.combine(0, helper.buffer, helper.buffer + 64, args...);
605 6745907 : }
606 6745907 :
607 149697393 : // Implementation details for implementations of hash_value overloads provided
608 0 : // here.
609 21248247 : namespace hashing {
610 21248247 : namespace detail {
611 0 :
612 1014612 : /// Helper to hash the value of a single integer.
613 0 : ///
614 0 : /// Overloads for smaller integer types are not provided to ensure consistent
615 65690 : /// behavior in the presence of integral promotions. Essentially,
616 0 : /// "hash_value('4')" and "hash_value('0' + 4)" should be the same.
617 586 : inline hash_code hash_integer_value(uint64_t value) {
618 13827012 : // Similar to hash_4to8_bytes but using a seed instead of length.
619 587 : const uint64_t seed = get_execution_seed();
620 15070189 : const char *s = reinterpret_cast<const char *>(&value);
621 14685954 : const uint64_t a = fetch32(s);
622 1015229 : return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
623 681217 : }
624 1180722 :
625 1022009 : } // namespace detail
626 15289749 : } // namespace hashing
627 16367933 :
628 42456 : // Declared and documented above, but defined here so that any of the hashing
629 1035836 : // infrastructure is available.
630 22092 : template <typename T>
631 54 : typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
632 1013754 : hash_value(T value) {
633 739646 : return ::llvm::hashing::detail::hash_integer_value(
634 310 : static_cast<uint64_t>(value));
635 13341 : }
636 6759504 :
637 0 : // Declared and documented above, but defined here so that any of the hashing
638 755847 : // infrastructure is available.
639 756103 : template <typename T> hash_code hash_value(const T *ptr) {
640 21248833 : return ::llvm::hashing::detail::hash_integer_value(
641 0 : reinterpret_cast<uintptr_t>(ptr));
642 134297 : }
643 134297 :
644 0 : // Declared and documented above, but defined here so that any of the hashing
645 0 : // infrastructure is available.
646 0 : template <typename T, typename U>
647 310 : hash_code hash_value(const std::pair<T, U> &arg) {
648 0 : return hash_combine(arg.first, arg.second);
649 0 : }
650 310 :
651 14056441 : // Declared and documented above, but defined here so that any of the hashing
652 0 : // infrastructure is available.
653 626 : template <typename T>
654 1695133 : hash_code hash_value(const std::basic_string<T> &arg) {
655 24572 : return hash_combine_range(arg.begin(), arg.end());
656 1108 : }
657 16304722 :
658 1056322 : } // namespace llvm
659 14966189 :
660 1947159 : #endif
|