Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

765 lines
30 KiB

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