45 #ifndef LLVM_ADT_HASHING_H
46 #define LLVM_ADT_HASHING_H
48 #include "llvm/Support/DataTypes.h"
85 operator size_t()
const {
return value; }
88 return lhs.value == rhs.value;
91 return lhs.value != rhs.value;
105 template <
typename T>
106 typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
112 template <
typename T> hash_code
hash_value(
const T *ptr);
115 template <
typename T,
typename U>
116 hash_code
hash_value(
const std::pair<T, U> &arg);
119 template <
typename T>
120 hash_code
hash_value(
const std::basic_string<T> &arg);
148 memcpy(&result, p,
sizeof(result));
156 memcpy(&result, p,
sizeof(result));
163 static const uint64_t
k0 = 0xc3a5c85c97cb3127ULL;
164 static const uint64_t
k1 = 0xb492b66fbe98f273ULL;
165 static const uint64_t
k2 = 0x9ae16a3b2f90404fULL;
166 static const uint64_t
k3 = 0xc949d7c7509e6557ULL;
171 inline uint64_t
rotate(uint64_t val,
size_t shift) {
173 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
177 return val ^ (val >> 47);
182 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
183 uint64_t a = (low ^ high) * kMul;
185 uint64_t b = (high ^ a) * kMul;
193 uint8_t b = s[len >> 1];
194 uint8_t c = s[len - 1];
195 uint32_t y =
static_cast<uint32_t
>(a) + (static_cast<uint32_t>(b) << 8);
196 uint32_t z = len + (
static_cast<uint32_t
>(c) << 2);
207 uint64_t b =
fetch64(s + len - 8);
217 a +
rotate(b ^
k3, 20) - c + len + seed);
223 uint64_t b =
rotate(a + z, 52);
224 uint64_t c =
rotate(a, 37);
229 uint64_t vs = b +
rotate(a, 31) + c;
238 uint64_t ws = b +
rotate(a, 31) + c;
243 inline uint64_t
hash_short(
const char *s,
size_t length, uint64_t seed) {
244 if (length >= 4 && length <= 8)
246 if (length > 8 && length <= 16)
248 if (length > 16 && length <= 32)
281 b =
rotate(b + a + c, 21);
329 const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
331 : (size_t)seed_prime;
349 : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
350 std::is_pointer<T>::value) &&
351 64 % sizeof(T) == 0)> {};
358 : std::integral_constant<bool, (is_hashable_data<T>::value &&
359 is_hashable_data<U>::value &&
360 (sizeof(T) + sizeof(U)) ==
361 sizeof(std::pair<T, U>))> {};
365 template <
typename T>
366 typename std::enable_if<is_hashable_data<T>::value,
T>::type
373 template <
typename T>
374 typename std::enable_if<!is_hashable_data<T>::value,
size_t>::type
387 template <
typename T>
390 size_t store_size =
sizeof(value) - offset;
391 if (buffer_ptr + store_size > buffer_end)
393 const char *value_data =
reinterpret_cast<const char *
>(&value);
394 memcpy(buffer_ptr, value_data + offset, store_size);
395 buffer_ptr += store_size;
404 template <
typename InputIteratorT>
407 char buffer[64], *buffer_ptr = buffer;
408 char *
const buffer_end =
std::end(buffer);
413 return hash_short(buffer, buffer_ptr - buffer, seed);
414 assert(buffer_ptr == buffer_end);
418 while (first != last) {
433 length += buffer_ptr - buffer;
447 template <
typename ValueT>
448 typename std::enable_if<is_hashable_data<ValueT>::value,
hash_code>::type
451 const char *s_begin =
reinterpret_cast<const char *
>(first);
452 const char *s_end =
reinterpret_cast<const char *
>(last);
453 const size_t length = std::distance(s_begin, s_end);
457 const char *s_aligned_end = s_begin + (length & ~63);
460 while (s_begin != s_aligned_end) {
465 state.mix(s_end - 64);
467 return state.finalize(length);
480 template <
typename InputIteratorT>
516 template <
typename T>
517 char *
combine_data(
size_t &length,
char *buffer_ptr,
char *buffer_end,
T data) {
523 size_t partial_store_size = buffer_end - buffer_ptr;
524 memcpy(buffer_ptr, &data, partial_store_size);
555 template <
typename T,
typename ...Ts>
557 const T &arg,
const Ts &...args) {
561 return combine(length, buffer_ptr, buffer_end, args...);
583 length += buffer_ptr -
buffer;
622 const char *s =
reinterpret_cast<const char *
>(&value);
632 template <
typename T>
633 typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
642 reinterpret_cast<uintptr_t>(ptr));
647 template <
typename T,
typename U>
654 template <
typename T>
const_iterator end(StringRef path)
Get end iterator over path.
hash_code hash_value(const std::basic_string< T > &arg)
Compute a hash_code for a standard string.
void swapByteOrder(T &Value)
Helper class to manage the recursive combining of hash_combine arguments.
static const uint64_t k0
Some primes between 2^63 and 2^64 for various uses.
uint64_t shift_mix(uint64_t val)
void set_fixed_execution_hash_seed(size_t fixed_value)
Override the execution seed with a fixed value.
std::enable_if< is_hashable_data< T >::value, T >::type get_hashable_data(const T &value)
Helper to get the hashable data representation for a type.
Trait to indicate whether a type's bits can be hashed directly.
uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed)
hash_code(size_t value)
Form a hash code directly from a numerical value.
uint64_t finalize(size_t length)
Compute the final 64-bit hash code value based on the current state and the length of bytes hashed...
uint64_t fetch64(const char *p)
static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b)
Mix 32-bytes from the input sequence into the 16-bytes of 'a' and 'b', including whatever is already ...
uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed)
uint64_t rotate(uint64_t val, size_t shift)
Bitwise right rotate.
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
uint32_t fetch32(const char *p)
uint64_t hash_16_bytes(uint64_t low, uint64_t high)
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end)
Base case for recursive, variadic combining.
friend bool operator==(const hash_code &lhs, const hash_code &rhs)
void mix(const char *s)
Mix in a 64-byte buffer of data.
static hash_state create(const char *s, uint64_t seed)
Create a new hash_state structure and initialize it based on the seed and the first 64-byte chunk...
uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed)
hash_code hash_integer_value(uint64_t value)
Helper to hash the value of a single integer.
hash_code()=default
Default construct a hash_code.
std::enable_if< is_hashable_data< ValueT >::value, hash_code >::type hash_combine_range_impl(ValueT *first, ValueT *last)
Implement the combining of integral values into a hash_code.
hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last)
Implement the combining of integral values into a hash_code.
uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed)
hash_combine_recursive_helper()
Construct a recursive hash combining helper.
uint64_t hash_short(const char *s, size_t length, uint64_t seed)
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, const T &arg, const Ts &...args)
Recursive, variadic combining method.
char * combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data)
Combine one chunk of data into the current in-flight hash.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
An opaque object representing a hash code.
bool store_and_advance(char *&buffer_ptr, char *buffer_end, const T &value, size_t offset=0)
Helper to store data from a value into a buffer and advance the pointer into that buffer...
static const bool IsBigEndianHost
The intermediate state used during hashing.
size_t get_execution_seed()
friend size_t hash_value(const hash_code &code)
Allow a hash_code to be directly run through hash_value.
uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed)
size_t fixed_seed_override
A global, fixed seed-override variable.
friend bool operator!=(const hash_code &lhs, const hash_code &rhs)