45 #ifndef WPIUTIL_WPI_HASHING_H
46 #define WPIUTIL_WPI_HASHING_H
48 #include "wpi/type_traits.h"
82 operator size_t()
const {
return value; }
85 return lhs.value == rhs.value;
88 return lhs.value != rhs.value;
102 template <
typename T>
103 typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
109 template <
typename T> hash_code hash_value(
const T *ptr);
112 template <
typename T,
typename U>
113 hash_code hash_value(
const std::pair<T, U> &arg);
116 template <
typename T>
117 hash_code hash_value(
const std::basic_string<T> &arg);
143 inline uint64_t fetch64(
const char *p) {
145 memcpy(&result, p,
sizeof(result));
151 inline uint32_t fetch32(
const char *p) {
153 memcpy(&result, p,
sizeof(result));
160 static const uint64_t k0 = 0xc3a5c85c97cb3127ULL;
161 static const uint64_t k1 = 0xb492b66fbe98f273ULL;
162 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
163 static const uint64_t k3 = 0xc949d7c7509e6557ULL;
168 inline uint64_t rotate(uint64_t val,
size_t shift) {
170 return shift == 0 ? val : ((val >> shift) | (val << (64 - shift)));
173 inline uint64_t shift_mix(uint64_t val) {
174 return val ^ (val >> 47);
177 inline uint64_t hash_16_bytes(uint64_t low, uint64_t high) {
179 const uint64_t kMul = 0x9ddfea08eb382d69ULL;
180 uint64_t a = (low ^ high) * kMul;
182 uint64_t b = (high ^ a) * kMul;
188 inline uint64_t hash_1to3_bytes(
const char *s,
size_t len, uint64_t seed) {
190 uint8_t b = s[len >> 1];
191 uint8_t c = s[len - 1];
192 uint32_t y =
static_cast<uint32_t
>(a) + (static_cast<uint32_t>(b) << 8);
193 uint32_t z = len + (
static_cast<uint32_t
>(c) << 2);
194 return shift_mix(y * k2 ^ z * k3 ^ seed) * k2;
197 inline uint64_t hash_4to8_bytes(
const char *s,
size_t len, uint64_t seed) {
198 uint64_t a = fetch32(s);
199 return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4));
202 inline uint64_t hash_9to16_bytes(
const char *s,
size_t len, uint64_t seed) {
203 uint64_t a = fetch64(s);
204 uint64_t b = fetch64(s + len - 8);
205 return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b;
208 inline uint64_t hash_17to32_bytes(
const char *s,
size_t len, uint64_t seed) {
209 uint64_t a = fetch64(s) * k1;
210 uint64_t b = fetch64(s + 8);
211 uint64_t c = fetch64(s + len - 8) * k2;
212 uint64_t d = fetch64(s + len - 16) * k0;
213 return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d,
214 a + rotate(b ^ k3, 20) - c + len + seed);
217 inline uint64_t hash_33to64_bytes(
const char *s,
size_t len, uint64_t seed) {
218 uint64_t z = fetch64(s + 24);
219 uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0;
220 uint64_t b = rotate(a + z, 52);
221 uint64_t c = rotate(a, 37);
224 a += fetch64(s + 16);
226 uint64_t vs = b + rotate(a, 31) + c;
227 a = fetch64(s + 16) + fetch64(s + len - 32);
228 z = fetch64(s + len - 8);
229 b = rotate(a + z, 52);
231 a += fetch64(s + len - 24);
233 a += fetch64(s + len - 16);
235 uint64_t ws = b + rotate(a, 31) + c;
236 uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0);
237 return shift_mix((seed ^ (r * k0)) + vs) * k2;
240 inline uint64_t hash_short(
const char *s,
size_t length, uint64_t seed) {
241 if (length >= 4 && length <= 8)
242 return hash_4to8_bytes(s, length, seed);
243 if (length > 8 && length <= 16)
244 return hash_9to16_bytes(s, length, seed);
245 if (length > 16 && length <= 32)
246 return hash_17to32_bytes(s, length, seed);
248 return hash_33to64_bytes(s, length, seed);
250 return hash_1to3_bytes(s, length, seed);
259 uint64_t h0, h1, h2, h3, h4, h5, h6;
266 0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49),
267 seed * k1, shift_mix(seed), 0 };
268 state.h6 = hash_16_bytes(state.h4, state.h5);
277 uint64_t c = fetch64(s + 24);
278 b = rotate(b + a + c, 21);
280 a += fetch64(s + 8) + fetch64(s + 16);
281 b += rotate(a, 44) + d;
289 h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1;
290 h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1;
292 h1 += h3 + fetch64(s + 40);
293 h2 = rotate(h2 + h5, 33) * k1;
298 h6 = h1 + fetch64(s + 16);
306 return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
307 hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0);
317 extern size_t fixed_seed_override;
319 inline size_t get_execution_seed() {
326 const uint64_t seed_prime = 0xff51afd7ed558ccdULL;
327 static size_t seed = fixed_seed_override ? fixed_seed_override
328 : (size_t)seed_prime;
346 : std::integral_constant<bool, ((is_integral_or_enum<T>::value ||
347 std::is_pointer<T>::value) &&
348 64 % sizeof(T) == 0)> {};
355 : std::integral_constant<bool, (is_hashable_data<T>::value &&
356 is_hashable_data<U>::value &&
357 (sizeof(T) + sizeof(U)) ==
358 sizeof(std::pair<T, U>))> {};
362 template <
typename T>
363 typename std::enable_if<is_hashable_data<T>::value, T>::type
364 get_hashable_data(
const T &value) {
370 template <
typename T>
371 typename std::enable_if<!is_hashable_data<T>::value,
size_t>::type
372 get_hashable_data(
const T &value) {
373 using ::wpi::hash_value;
374 return hash_value(value);
384 template <
typename T>
385 bool store_and_advance(
char *&buffer_ptr,
char *buffer_end,
const T& value,
387 size_t store_size =
sizeof(value) - offset;
388 if (buffer_ptr + store_size > buffer_end)
390 const char *value_data =
reinterpret_cast<const char *
>(&value);
391 memcpy(buffer_ptr, value_data + offset, store_size);
392 buffer_ptr += store_size;
401 template <
typename InputIteratorT>
402 hash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) {
403 const size_t seed = get_execution_seed();
404 char buffer[64], *buffer_ptr = buffer;
405 char *
const buffer_end = std::end(buffer);
406 while (first != last && store_and_advance(buffer_ptr, buffer_end,
407 get_hashable_data(*first)))
410 return hash_short(buffer, buffer_ptr - buffer, seed);
411 assert(buffer_ptr == buffer_end);
413 hash_state state = state.create(buffer, seed);
415 while (first != last) {
419 while (first != last && store_and_advance(buffer_ptr, buffer_end,
420 get_hashable_data(*first)))
426 std::rotate(buffer, buffer_ptr, buffer_end);
430 length += buffer_ptr - buffer;
433 return state.finalize(length);
444 template <
typename ValueT>
445 typename std::enable_if<is_hashable_data<ValueT>::value, hash_code>::type
446 hash_combine_range_impl(ValueT *first, ValueT *last) {
447 const size_t seed = get_execution_seed();
448 const char *s_begin =
reinterpret_cast<const char *
>(first);
449 const char *s_end =
reinterpret_cast<const char *
>(last);
450 const size_t length = std::distance(s_begin, s_end);
452 return hash_short(s_begin, length, seed);
454 const char *s_aligned_end = s_begin + (length & ~63);
455 hash_state state = state.create(s_begin, seed);
457 while (s_begin != s_aligned_end) {
462 state.mix(s_end - 64);
464 return state.finalize(length);
477 template <
typename InputIteratorT>
479 return ::wpi::hashing::detail::hash_combine_range_impl(first, last);
505 : seed(get_execution_seed()) {}
513 template <
typename T>
514 char *
combine_data(
size_t &length,
char *buffer_ptr,
char *buffer_end, T data) {
515 if (!store_and_advance(buffer_ptr, buffer_end, data)) {
520 size_t partial_store_size = buffer_end - buffer_ptr;
521 memcpy(buffer_ptr, &data, partial_store_size);
528 state = state.
create(buffer, seed);
541 if (!store_and_advance(buffer_ptr, buffer_end, data,
552 template <
typename T,
typename ...Ts>
554 const T &arg,
const Ts &...args) {
555 buffer_ptr =
combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg));
558 return combine(length, buffer_ptr, buffer_end, args...);
570 return hash_short(buffer, buffer_ptr - buffer, seed);
576 std::rotate(buffer, buffer_ptr, buffer_end);
580 length += buffer_ptr - buffer;
603 return helper.
combine(0, helper.buffer, helper.buffer + 64, args...);
616 inline hash_code hash_integer_value(uint64_t value) {
618 const uint64_t seed = get_execution_seed();
619 const char *s =
reinterpret_cast<const char *
>(&value);
620 const uint64_t a = fetch32(s);
621 return hash_16_bytes(seed + (a << 3), fetch32(s + 4));
629 template <
typename T>
630 typename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type
631 hash_value(T value) {
632 return ::wpi::hashing::detail::hash_integer_value(
633 static_cast<uint64_t>(value));
638 template <
typename T>
hash_code hash_value(
const T *ptr) {
639 return ::wpi::hashing::detail::hash_integer_value(
640 reinterpret_cast<uintptr_t>(ptr));
645 template <
typename T,
typename U>
652 template <
typename T>
hash_combine_recursive_helper()
Construct a recursive hash combining helper.
Definition: Hashing.h:504
The intermediate state used during hashing.
Definition: Hashing.h:258
Trait to indicate whether a type's bits can be hashed directly.
Definition: Hashing.h:345
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end)
Base case for recursive, variadic combining.
Definition: Hashing.h:566
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:600
hash_code()=default
Default construct a hash_code.
hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, const T &arg, const Ts &...args)
Recursive, variadic combining method.
Definition: Hashing.h:553
Definition: SmallVector.h:946
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 ...
Definition: Hashing.h:275
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...
Definition: Hashing.h:264
void set_fixed_execution_hash_seed(size_t fixed_value)
Override the execution seed with a fixed value.
namespace to hold default to_json function
Definition: SmallString.h:21
friend size_t hash_value(const hash_code &code)
Allow a hash_code to be directly run through hash_value.
Definition: Hashing.h:92
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...
Definition: Hashing.h:305
void mix(const char *s)
Mix in a 64-byte buffer of data.
Definition: Hashing.h:288
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:478
Helper class to manage the recursive combining of hash_combine arguments.
Definition: Hashing.h:494
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.
Definition: Hashing.h:514
hash_code(size_t value)
Form a hash code directly from a numerical value.
Definition: Hashing.h:79
An opaque object representing a hash code.
Definition: Hashing.h:70