11a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===// 21a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// 31a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// The LLVM Compiler Infrastructure 41a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// 51a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// This file is distributed under the University of Illinois Open Source 61a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// License. See LICENSE.TXT for details. 71a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// 81a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//===----------------------------------------------------------------------===// 91a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// 100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// This file implements the newly proposed standard C++ interfaces for hashing 110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// arbitrary data and building hash functions for user-defined types. This 120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// interface was originally proposed in N3333[1] and is currently under review 130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// for inclusion in a future TR and/or standard. 140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// 150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// The primary interfaces provide are comprised of one type and three functions: 160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// 170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// -- 'hash_code' class is an opaque type representing the hash code for some 180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// data. It is the intended product of hashing, and can be used to implement 190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// hash tables, checksumming, and other common uses of hashes. It is not an 200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// integer type (although it can be converted to one) because it is risky 210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// to assume much about the internals of a hash_code. In particular, each 220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// execution of the program has a high probability of producing a different 230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// hash_code for a given input. Thus their values are not stable to save or 240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// persist, and should only be used during the execution for the 250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// construction of hashing datastructures. 260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// 270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// -- 'hash_value' is a function designed to be overloaded for each 280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// user-defined type which wishes to be used within a hashing context. It 290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// should be overloaded within the user-defined type's namespace and found 300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// via ADL. Overloads for primitive types are provided by this library. 310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// 320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// -- 'hash_combine' and 'hash_combine_range' are functions designed to aid 330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// programmers in easily and intuitively combining a set of data into 340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// a single hash_code for their object. They should only logically be used 350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// within the implementation of a 'hash_value' routine or similar context. 360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// 370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Note that 'hash_combine_range' contains very special logic for hashing 380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// a contiguous array of integers or pointers. This logic is *extremely* fast, 390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// on a modern Intel "Gainestown" Xeon (Nehalem uarch) @2.2 GHz, these were 400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// benchmarked at over 6.5 GiB/s for large keys, and <20 cycles/hash for keys 410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// under 32-bytes. 421a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin// 431a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin//===----------------------------------------------------------------------===// 441a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 451a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#ifndef LLVM_ADT_HASHING_H 461a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#define LLVM_ADT_HASHING_H 471a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 481a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#include "llvm/Support/DataTypes.h" 49dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth#include "llvm/Support/Host.h" 50dc62a9069648e86846f9f5a8eed7ad29de6f4163Chandler Carruth#include "llvm/Support/SwapByteOrder.h" 510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include "llvm/Support/type_traits.h" 520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <algorithm> 530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <cassert> 546592eacf9006d046e8bc4999600e2973a3b56eacJay Foad#include <cstring> 550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <iterator> 566948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#include <string> 570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth#include <utility> 580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 591a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinnamespace llvm { 601a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief An opaque object representing a hash code. 620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This object represents the result of hashing some entity. It is intended to 640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// be used to implement hashtables or other hashing-based data structures. 650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// While it wraps and exposes a numeric value, this value should not be 660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// trusted to be stable or predictable across processes or executions. 670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// In order to obtain the hash_code for an object 'x': 690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \code 700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// using llvm::hash_value; 710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// llvm::hash_code code = hash_value(x); 720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \endcode 730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthclass hash_code { 740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth size_t value; 750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 761a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talinpublic: 774166989f10eaecfb357551788a3d91275e75f119Chandler Carruth /// \brief Default construct a hash_code. 784166989f10eaecfb357551788a3d91275e75f119Chandler Carruth /// Note that this leaves the value uninitialized. 790c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar hash_code() = default; 801a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Form a hash code directly from a numerical value. 824166989f10eaecfb357551788a3d91275e75f119Chandler Carruth hash_code(size_t value) : value(value) {} 831a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Convert the hash code to its numerical value for use. 850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /*explicit*/ operator size_t() const { return value; } 860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth friend bool operator==(const hash_code &lhs, const hash_code &rhs) { 880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return lhs.value == rhs.value; 890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth } 900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth friend bool operator!=(const hash_code &lhs, const hash_code &rhs) { 910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return lhs.value != rhs.value; 921a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 931a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Allow a hash_code to be directly run through hash_value. 950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth friend size_t hash_value(const hash_code &code) { return code.value; } 960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}; 970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 98c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for any integer value. 99c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// 100c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Note that this function is intended to compute the same hash_code for 101c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// a particular value without regard to the pre-promotion type. This is in 102c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// contrast to hash_combine which may produce different hash_codes for 103c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// differing argument types even if they would implicit promote to a common 104c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// type without changing the value. 105c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> 10636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type 10736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hineshash_value(T value); 108c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 109c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pointer's address. 110c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// 111c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// N.B.: This hashes the *address*. Not the value and not the type. 112c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr); 113c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 114c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Compute a hash_code for a pair of objects. 115c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U> 116c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg); 117c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 1189406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth/// \brief Compute a hash_code for a standard string. 1199406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthtemplate <typename T> 1209406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthhash_code hash_value(const std::basic_string<T> &arg); 1219406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth 122c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 123c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Override the execution seed with a fixed value. 124c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// 125c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// This hashing library uses a per-execution seed designed to change on each 126c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// run with high probability in order to ensure that the hash codes are not 127c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// attackable and to ensure that output which is intended to be stable does 128c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// not rely on the particulars of the hash codes produced. 129c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// 130c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// That said, there are use cases where it is important to be able to 131c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// reproduce *exactly* a specific behavior. To that end, we provide a function 132c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// which will forcibly set the seed to a fixed value. This must be done at the 133c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// start of the program, before any hashes are computed. Also, it cannot be 134c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// undone. This makes it thread-hostile and very hard to use outside of 135c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// immediately on start of a simple program designed for reproducible 136c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior. 137c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthvoid set_fixed_execution_hash_seed(size_t fixed_value); 138c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 1390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// All of the implementation details of actually computing the various hash 1410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// code values are held within this namespace. These routines are included in 1420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// the header file mainly to allow inlining and constant propagation. 1430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing { 1440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail { 1450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t fetch64(const char *p) { 1470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t result; 1480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth memcpy(&result, p, sizeof(result)); 14921a01d1ea89dba97c4f9e1f9f41485729a4046bcRafael Espindola if (sys::IsBigEndianHost) 150c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines sys::swapByteOrder(result); 1510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return result; 1520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 1530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint32_t fetch32(const char *p) { 1550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint32_t result; 1560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth memcpy(&result, p, sizeof(result)); 15721a01d1ea89dba97c4f9e1f9f41485729a4046bcRafael Espindola if (sys::IsBigEndianHost) 158c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines sys::swapByteOrder(result); 1590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return result; 1600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 1610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Some primes between 2^63 and 2^64 for various uses. 1630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k0 = 0xc3a5c85c97cb3127ULL; 1640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k1 = 0xb492b66fbe98f273ULL; 1650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k2 = 0x9ae16a3b2f90404fULL; 1660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstatic const uint64_t k3 = 0xc949d7c7509e6557ULL; 1670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Bitwise right rotate. 1690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Normally this will compile to a single instruction, especially if the 1700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// shift is a manifest constant. 1710c7374d87e6bf002028cd19f8ae9547927c9c645Benjamin Kramerinline uint64_t rotate(uint64_t val, size_t shift) { 1720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Avoid shifting by 64: doing so yields an undefined result. 1730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); 1740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 1750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t shift_mix(uint64_t val) { 1770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return val ^ (val >> 47); 1780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 1790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_16_bytes(uint64_t low, uint64_t high) { 1810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Murmur-inspired hashing. 1820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const uint64_t kMul = 0x9ddfea08eb382d69ULL; 1830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t a = (low ^ high) * kMul; 1840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a ^= (a >> 47); 1850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t b = (high ^ a) * kMul; 1860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth b ^= (b >> 47); 1870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth b *= kMul; 1880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return b; 1890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 1900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 1910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_1to3_bytes(const char *s, size_t len, uint64_t seed) { 1920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint8_t a = s[0]; 1930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint8_t b = s[len >> 1]; 1940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint8_t c = s[len - 1]; 1950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8); 1960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint32_t z = len + (static_cast<uint32_t>(c) << 2); 1970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return shift_mix(y * k2 ^ z * k3 ^ seed) * k2; 1980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 1990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_4to8_bytes(const char *s, size_t len, uint64_t seed) { 2010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t a = fetch32(s); 2020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return hash_16_bytes(len + (a << 3), seed ^ fetch32(s + len - 4)); 2030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 2040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_9to16_bytes(const char *s, size_t len, uint64_t seed) { 206171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer uint64_t a = fetch64(s); 207171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer uint64_t b = fetch64(s + len - 8); 208171cda96c8fa7ea48f792b700847e750a2036f2fBenjamin Kramer return hash_16_bytes(seed ^ a, rotate(b + len, len)) ^ b; 2090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 2100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_17to32_bytes(const char *s, size_t len, uint64_t seed) { 2120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t a = fetch64(s) * k1; 2130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t b = fetch64(s + 8); 2140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t c = fetch64(s + len - 8) * k2; 2150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t d = fetch64(s + len - 16) * k0; 2160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return hash_16_bytes(rotate(a - b, 43) + rotate(c ^ seed, 30) + d, 2170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a + rotate(b ^ k3, 20) - c + len + seed); 2180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 2190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_33to64_bytes(const char *s, size_t len, uint64_t seed) { 2210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t z = fetch64(s + 24); 2220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t a = fetch64(s) + (len + fetch64(s + len - 16)) * k0; 2230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t b = rotate(a + z, 52); 2240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t c = rotate(a, 37); 2250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += fetch64(s + 8); 2260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth c += rotate(a, 7); 2270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += fetch64(s + 16); 2280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t vf = a + z; 2290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t vs = b + rotate(a, 31) + c; 2300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a = fetch64(s + 16) + fetch64(s + len - 32); 2310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth z = fetch64(s + len - 8); 2320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth b = rotate(a + z, 52); 2330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth c = rotate(a, 37); 2340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += fetch64(s + len - 24); 2350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth c += rotate(a, 7); 2360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += fetch64(s + len - 16); 2370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t wf = a + z; 2380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t ws = b + rotate(a, 31) + c; 2390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t r = shift_mix((vf + ws) * k2 + (wf + vs) * k0); 240b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth return shift_mix((seed ^ (r * k0)) + vs) * k2; 2410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 2420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline uint64_t hash_short(const char *s, size_t length, uint64_t seed) { 2440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (length >= 4 && length <= 8) 2454166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return hash_4to8_bytes(s, length, seed); 2464166989f10eaecfb357551788a3d91275e75f119Chandler Carruth if (length > 8 && length <= 16) 2474166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return hash_9to16_bytes(s, length, seed); 2484166989f10eaecfb357551788a3d91275e75f119Chandler Carruth if (length > 16 && length <= 32) 2494166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return hash_17to32_bytes(s, length, seed); 2504166989f10eaecfb357551788a3d91275e75f119Chandler Carruth if (length > 32) 2514166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return hash_33to64_bytes(s, length, seed); 2524166989f10eaecfb357551788a3d91275e75f119Chandler Carruth if (length != 0) 2534166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return hash_1to3_bytes(s, length, seed); 2544166989f10eaecfb357551788a3d91275e75f119Chandler Carruth 2554166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return k2 ^ seed; 2560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 2570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief The intermediate state used during hashing. 2590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// Currently, the algorithm for computing hash codes is based on CityHash and 2600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// keeps 56 bytes of arbitrary state. 2610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthstruct hash_state { 2620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t h0, h1, h2, h3, h4, h5, h6; 2630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 2640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Create a new hash_state structure and initialize it based on the 2650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// seed and the first 64-byte chunk. 2660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// This effectively performs the initial mix. 2670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth static hash_state create(const char *s, uint64_t seed) { 2680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth hash_state state = { 2690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 0, seed, hash_16_bytes(seed, k1), rotate(seed ^ k1, 49), 270dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines seed * k1, shift_mix(seed), 0 }; 271a2a9b9e7752f1eeb8c158112fc940ff67b04d7a1Daniel Dunbar state.h6 = hash_16_bytes(state.h4, state.h5); 2720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state.mix(s); 2730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return state; 2741a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 2751a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 2760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a' 2770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// and 'b', including whatever is already in 'a' and 'b'. 2780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) { 2790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += fetch64(s); 2800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t c = fetch64(s + 24); 2810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth b = rotate(b + a + c, 21); 2820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t d = a; 2830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += fetch64(s + 8) + fetch64(s + 16); 2840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth b += rotate(a, 44) + d; 2850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth a += c; 2861a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 2871a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 2880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Mix in a 64-byte buffer of data. 2890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// We mix all 64 bytes even when the chunk length is smaller, but we 2900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// record the actual length. 2910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth void mix(const char *s) { 2920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h0 = rotate(h0 + h1 + h3 + fetch64(s + 8), 37) * k1; 2930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h1 = rotate(h1 + h4 + fetch64(s + 48), 42) * k1; 2940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h0 ^= h6; 2950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h1 += h3 + fetch64(s + 40); 2960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h2 = rotate(h2 + h5, 33) * k1; 2970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h3 = h4 * k1; 2980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h4 = h0 + h5; 2990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth mix_32_bytes(s, h3, h4); 3000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h5 = h2 + h6; 3010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth h6 = h1 + fetch64(s + 16); 3020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth mix_32_bytes(s + 32, h5, h6); 3030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth std::swap(h2, h0); 3041a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 3051a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 3060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Compute the final 64-bit hash code value based on the current 3070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// state and the length of bytes hashed. 3080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth uint64_t finalize(size_t length) { 3094166989f10eaecfb357551788a3d91275e75f119Chandler Carruth return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2, 3104166989f10eaecfb357551788a3d91275e75f119Chandler Carruth hash_16_bytes(h4, h6) + shift_mix(length) * k1 + h0); 3111a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 3120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth}; 3131a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 3140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief A global, fixed seed-override variable. 3160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 3170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variable can be set using the \see llvm::set_fixed_execution_seed 3180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// function. See that function for details. Do not, under any circumstances, 3190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// set or read this variable. 3200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthextern size_t fixed_seed_override; 3210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthinline size_t get_execution_seed() { 3230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // FIXME: This needs to be a per-execution seed. This is just a placeholder 3240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // implementation. Switching to a per-execution seed is likely to flush out 3250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // instability bugs and so will happen as its own commit. 3260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // 3270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // However, if there is a fixed seed override set the first time this is 3280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // called, return that instead of the per-execution seed. 329b4d023503b40b45fd11835f1697a17e23f958af3Chandler Carruth const uint64_t seed_prime = 0xff51afd7ed558ccdULL; 3300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth static size_t seed = fixed_seed_override ? fixed_seed_override 331179bc7cb597c98045fae055fae754da5690ba187Chandler Carruth : (size_t)seed_prime; 3320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return seed; 3330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 3340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Trait to indicate whether a type's bits can be hashed directly. 3370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 3380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// A type trait which is true if we want to combine values for hashing by 3390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// reading the underlying data. It is false if values of this type must 3400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// first be passed to hash_value, and the resulting hash_codes combined. 3410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// 342d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// FIXME: We want to replace is_integral_or_enum and is_pointer here with 343d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// a predicate which asserts that comparing the underlying storage of two 344d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// values of the type for equality is equivalent to comparing the two values 345d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// for equality. For all the platforms we care about, this holds for integers 346d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// and pointers, but there are platforms where it doesn't and we would like to 347d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruth// support user-defined types which happen to satisfy this property. 3480b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> struct is_hashable_data 34936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines : std::integral_constant<bool, ((is_integral_or_enum<T>::value || 35036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines std::is_pointer<T>::value) && 35136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 64 % sizeof(T) == 0)> {}; 3520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3534d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// Special case std::pair to detect when both types are viable and when there 3544d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// is no alignment-derived padding in the pair. This is a bit of a lie because 3554d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// std::pair isn't truly POD, but it's close enough in all reasonable 3564d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth// implementations for our use case of hashing the underlying data. 3574d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruthtemplate <typename T, typename U> struct is_hashable_data<std::pair<T, U> > 35836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines : std::integral_constant<bool, (is_hashable_data<T>::value && 35936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines is_hashable_data<U>::value && 36036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines (sizeof(T) + sizeof(U)) == 36136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines sizeof(std::pair<T, U>))> {}; 3624d628e200f7133e353c38806b57a229ef6ad2ab4Chandler Carruth 3630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to get the hashable data representation for a type. 3640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variant is enabled when the type itself can be used. 3650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> 36636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_hashable_data<T>::value, T>::type 3670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthget_hashable_data(const T &value) { 3680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return value; 3690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 3700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to get the hashable data representation for a type. 3710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This variant is enabled when we must first call hash_value and use the 3720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// result as our data. 3730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> 37436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<!is_hashable_data<T>::value, size_t>::type 3750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthget_hashable_data(const T &value) { 3760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth using ::llvm::hash_value; 3770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return hash_value(value); 3780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 3790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper to store data from a value into a buffer and advance the 3810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// pointer into that buffer. 3820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 3830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This routine first checks whether there is enough space in the provided 3840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// buffer, and if not immediately returns false. If there is space, it 3850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// copies the underlying bytes of value into the buffer, advances the 3860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// buffer_ptr past the copied bytes, and returns true. 3870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename T> 3880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthbool store_and_advance(char *&buffer_ptr, char *buffer_end, const T& value, 3890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth size_t offset = 0) { 3900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth size_t store_size = sizeof(value) - offset; 3910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (buffer_ptr + store_size > buffer_end) 3920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return false; 3930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const char *value_data = reinterpret_cast<const char *>(&value); 3940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth memcpy(buffer_ptr, value_data + offset, store_size); 3950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth buffer_ptr += store_size; 3960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return true; 3970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 3980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 3990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code. 4000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 4010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is 4020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// integral. Rather than computing a hash_code for each object and then 4030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// combining them, this (as an optimization) directly combines the integers. 4040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT> 4050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range_impl(InputIteratorT first, InputIteratorT last) { 4060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const size_t seed = get_execution_seed(); 4070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth char buffer[64], *buffer_ptr = buffer; 408dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines char *const buffer_end = std::end(buffer); 4090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth while (first != last && store_and_advance(buffer_ptr, buffer_end, 4100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth get_hashable_data(*first))) 4110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth ++first; 4120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (first == last) 4130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return hash_short(buffer, buffer_ptr - buffer, seed); 4140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth assert(buffer_ptr == buffer_end); 4150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4160b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth hash_state state = state.create(buffer, seed); 4170b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth size_t length = 64; 4180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth while (first != last) { 4190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Fill up the buffer. We don't clear it, which re-mixes the last round 4200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // when only a partial 64-byte chunk is left. 4210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth buffer_ptr = buffer; 4220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth while (first != last && store_and_advance(buffer_ptr, buffer_end, 4230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth get_hashable_data(*first))) 4240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth ++first; 4250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Rotate the buffer if we did a partial fill in order to simulate doing 4270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // a mix of the last 64-bytes. That is how the algorithm works when we 4280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // have a contiguous byte sequence, and we want to emulate that here. 4290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth std::rotate(buffer, buffer_ptr, buffer_end); 4300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Mix this chunk into the current state. 4320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state.mix(buffer); 4330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth length += buffer_ptr - buffer; 4340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth }; 4350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return state.finalize(length); 4370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 4380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Implement the combining of integral values into a hash_code. 4400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 4410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This overload is selected when the value type of the iterator is integral 4420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and when the input iterator is actually a pointer. Rather than computing 4430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a hash_code for each object and then combining them, this (as an 4440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// optimization) directly combines the integers. Also, because the integers 4450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// are stored in contiguous memory, this routine avoids copying each value 4460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and directly reads from the underlying memory. 4470b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ValueT> 44836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_hashable_data<ValueT>::value, hash_code>::type 449005874056e536324cba9fa253d7e14a6f9dcf4faChandler Carruthhash_combine_range_impl(ValueT *first, ValueT *last) { 4500b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const size_t seed = get_execution_seed(); 4510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const char *s_begin = reinterpret_cast<const char *>(first); 4520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const char *s_end = reinterpret_cast<const char *>(last); 4530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const size_t length = std::distance(s_begin, s_end); 4540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (length <= 64) 4550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return hash_short(s_begin, length, seed); 4560b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4570b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth const char *s_aligned_end = s_begin + (length & ~63); 4580b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth hash_state state = state.create(s_begin, seed); 4590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth s_begin += 64; 4600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth while (s_begin != s_aligned_end) { 4610b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state.mix(s_begin); 4620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth s_begin += 64; 4631a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 4640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (length & 63) 4650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state.mix(s_end - 64); 4661a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 4670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return state.finalize(length); 4680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 4690b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail 4710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing 4720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Compute a hash_code for a sequence of values. 4750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 4760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This hashes a sequence of values. It produces the same hash_code as 4770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences 4780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// and is significantly faster given pointers and types which can be hashed as 4790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// a sequence of bytes. 4800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename InputIteratorT> 4810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthhash_code hash_combine_range(InputIteratorT first, InputIteratorT last) { 4820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return ::llvm::hashing::detail::hash_combine_range_impl(first, last); 4830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 4840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4860b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth// Implementation details for hash_combine. 4870b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace hashing { 4880b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthnamespace detail { 4890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 4900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Helper class to manage the recursive combining of hash_combine 4910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// arguments. 4920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 4930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This class exists to manage the state and various calls involved in the 4940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// recursive combining of arguments used in hash_combine. It is particularly 4950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// useful at minimizing the code in the recursive calls to ease the pain 4960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// caused by a lack of variadic functions. 4975cd79bc14ca51019af4db735d13eac95dab088edChandler Carruthstruct hash_combine_recursive_helper { 4980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth char buffer[64]; 4990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth hash_state state; 5005cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth const size_t seed; 5010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthpublic: 5030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Construct a recursive hash combining helper. 5040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// 5050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// This sets up the state for a recursive hash combine, including getting 5060b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// the seed and buffer setup. 5070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth hash_combine_recursive_helper() 5085cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth : seed(get_execution_seed()) {} 5090b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5100b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Combine one chunk of data into the current in-flight hash. 5110b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// 5120b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// This merges one chunk of data into the hash. First it tries to buffer 5130b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// the data. If the buffer is full, it hashes the buffer into its 5140b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// hash_state, empties it, and then merges the new chunk in. This also 5150b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// handles cases where the data straddles the end of the buffer. 5165cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth template <typename T> 5175cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth char *combine_data(size_t &length, char *buffer_ptr, char *buffer_end, T data) { 5180b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (!store_and_advance(buffer_ptr, buffer_end, data)) { 5190b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Check for skew which prevents the buffer from being packed, and do 5200b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // a partial store into the buffer to fill it. This is only a concern 5210b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // with the variadic combine because that formation can have varying 5220b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // argument types. 5230b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth size_t partial_store_size = buffer_end - buffer_ptr; 5240b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth memcpy(buffer_ptr, &data, partial_store_size); 5250b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5260b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // If the store fails, our buffer is full and ready to hash. We have to 5270b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // either initialize the hash state (on the first full buffer) or mix 5280b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // this buffer into the existing hash state. Length tracks the *hashed* 5290b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // length, not the buffered length. 5300b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (length == 0) { 5310b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state = state.create(buffer, seed); 5320b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth length = 64; 5330b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth } else { 5340b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Mix this chunk into the current state and bump length up by 64. 5350b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state.mix(buffer); 5360b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth length += 64; 5370b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth } 5380b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Reset the buffer_ptr to the head of the buffer for the next chunk of 5390b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // data. 5400b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth buffer_ptr = buffer; 5410b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5420b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Try again to store into the buffer -- this cannot fail as we only 5430b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // store types smaller than the buffer. 5440b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (!store_and_advance(buffer_ptr, buffer_end, data, 5450b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth partial_store_size)) 5460b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth abort(); 5471a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 5485cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth return buffer_ptr; 5496592eacf9006d046e8bc4999600e2973a3b56eacJay Foad } 5501a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 5510b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Recursive, variadic combining method. 5520b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// 5530b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// This function recurses through each argument, combining that argument 5540b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// into a single hash. 5550b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth template <typename T, typename ...Ts> 5565cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth hash_code combine(size_t length, char *buffer_ptr, char *buffer_end, 5575cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth const T &arg, const Ts &...args) { 5585cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth buffer_ptr = combine_data(length, buffer_ptr, buffer_end, get_hashable_data(arg)); 5590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5600b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Recurse to the next argument. 5615cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth return combine(length, buffer_ptr, buffer_end, args...); 5620b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth } 5630b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5640b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// \brief Base case for recursive, variadic combining. 5650b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// 5660b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// The base case when combining arguments recursively is reached when all 5670b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// arguments have been handled. It flushes the remaining buffer and 5680b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth /// constructs a hash_code. 5695cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth hash_code combine(size_t length, char *buffer_ptr, char *buffer_end) { 5700b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Check whether the entire set of values fit in the buffer. If so, we'll 5710b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // use the optimized short hashing routine and skip state entirely. 5720b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth if (length == 0) 5730b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return hash_short(buffer, buffer_ptr - buffer, seed); 5740b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5750b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Mix the final buffer, rotating it if we did a partial fill in order to 5760b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // simulate doing a mix of the last 64-bytes. That is how the algorithm 5770b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // works when we have a contiguous byte sequence, and we want to emulate 5780b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // that here. 5790b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth std::rotate(buffer, buffer_ptr, buffer_end); 5800b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5810b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Mix this chunk into the current state. 5820b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth state.mix(buffer); 5830b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth length += buffer_ptr - buffer; 5840b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5850b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth return state.finalize(length); 5861a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin } 5871a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin}; 5881a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 5890b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace detail 5900b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace hashing 5910b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 5920b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// \brief Combine values into a single hash_code. 5930b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 5940b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// This routine accepts a varying number of arguments of any type. It will 5950b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// attempt to combine them into a single hash_code. For user-defined types it 5960b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// attempts to call a \see hash_value overload (via ADL) for the type. For 5970b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// integer and pointer types it directly combines their data into the 5980b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// resulting hash_code. 5990b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// 6000b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// The result is suitable for returning from a user's hash_value 6010b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// *implementation* for their user-defined type. Consumers of a type should 6020b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth/// *not* call this routine, they should instead call 'hash_value'. 6030b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruthtemplate <typename ...Ts> hash_code hash_combine(const Ts &...args) { 6040b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth // Recursively hash each argument using a helper class. 6050b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth ::llvm::hashing::detail::hash_combine_recursive_helper helper; 6065cd79bc14ca51019af4db735d13eac95dab088edChandler Carruth return helper.combine(0, helper.buffer, helper.buffer + 64, args...); 6070b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} 6080b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth 609b2eb7406719a0cd70489a1229af643b04882b046Nick Lewycky// Implementation details for implementations of hash_value overloads provided 610c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// here. 611c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace hashing { 612c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthnamespace detail { 613c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 614c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// \brief Helper to hash the value of a single integer. 615c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// 616c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// Overloads for smaller integer types are not provided to ensure consistent 617c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// behavior in the presence of integral promotions. Essentially, 618c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth/// "hash_value('4')" and "hash_value('0' + 4)" should be the same. 619c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthinline hash_code hash_integer_value(uint64_t value) { 620c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth // Similar to hash_4to8_bytes but using a seed instead of length. 621c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth const uint64_t seed = get_execution_seed(); 622c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth const char *s = reinterpret_cast<const char *>(&value); 623c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth const uint64_t a = fetch32(s); 624c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth return hash_16_bytes(seed + (a << 3), fetch32(s + 4)); 625c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} 626c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 627c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace detail 628c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} // namespace hashing 629c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 630c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing 631c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available. 632c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> 63336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestypename std::enable_if<is_integral_or_enum<T>::value, hash_code>::type 634d4d8b2a7f6d29b44cbc084b49042f44ef109da0cChandler Carruthhash_value(T value) { 635c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth return ::llvm::hashing::detail::hash_integer_value(value); 636c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} 637c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 638c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing 639c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available. 640c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T> hash_code hash_value(const T *ptr) { 641c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth return ::llvm::hashing::detail::hash_integer_value( 642c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth reinterpret_cast<uintptr_t>(ptr)); 643c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} 644c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 645c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// Declared and documented above, but defined here so that any of the hashing 646c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth// infrastructure is available. 647c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthtemplate <typename T, typename U> 648c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruthhash_code hash_value(const std::pair<T, U> &arg) { 649c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth return hash_combine(arg.first, arg.second); 650c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth} 651c7384cfc7addb5d2818ac0bb4492778f28183c49Chandler Carruth 6529406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth// Declared and documented above, but defined here so that any of the hashing 6539406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth// infrastructure is available. 6549406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthtemplate <typename T> 6559406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruthhash_code hash_value(const std::basic_string<T> &arg) { 6569406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth return hash_combine_range(arg.begin(), arg.end()); 6579406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth} 6589406da6e664a24c8e408cbba63daf162ca166ed9Chandler Carruth 6590b66c6fca22e85f732cf58f459a06c06833d1882Chandler Carruth} // namespace llvm 6601a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin 6611a4b19ef9b870d8c914bcd5ceb520a64a9a2cc52Talin#endif 662