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