1 //===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines utilities for computing hash values for various data types.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_HASHING_H
15 #define LLVM_ADT_HASHING_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/AlignOf.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/DataTypes.h"
26 /// Class to compute a hash value from multiple data fields of arbitrary
27 /// types. Note that if you are hashing a single data type, such as a
28 /// string, it may be cheaper to use a hash algorithm that is tailored
29 /// for that specific data type.
32 /// Hash.add(someValue);
33 /// Hash.add(someOtherValue);
34 /// return Hash.finish();
35 /// Adapted from MurmurHash2 by Austin Appleby
44 GeneralHash(unsigned Seed = 0) : Hash(Seed), Count(0) {}
46 /// Add a pointer value.
47 /// Note: this adds pointers to the hash using sizes and endianness that
48 /// depend on the host. It doesn't matter however, because hashing on
49 /// pointer values is inherently unstable.
51 GeneralHash& add(const T *PtrVal) {
52 addBits(&PtrVal, &PtrVal + 1);
56 /// Add an ArrayRef of arbitrary data.
58 GeneralHash& add(ArrayRef<T> ArrayVal) {
59 addBits(ArrayVal.begin(), ArrayVal.end());
64 GeneralHash& add(StringRef StrVal) {
65 addBits(StrVal.begin(), StrVal.end());
69 /// Add an signed 32-bit integer.
70 GeneralHash& add(int32_t Data) {
71 addInt(uint32_t(Data));
75 /// Add an unsigned 32-bit integer.
76 GeneralHash& add(uint32_t Data) {
81 /// Add an signed 64-bit integer.
82 GeneralHash& add(int64_t Data) {
83 addInt(uint64_t(Data));
87 /// Add an unsigned 64-bit integer.
88 GeneralHash& add(uint64_t Data) {
94 GeneralHash& add(float Data) {
104 GeneralHash& add(double Data) {
106 double D; uint64_t I;
113 // Do a few final mixes of the hash to ensure the last few
114 // bytes are well-incorporated.
124 void mix(uint32_t Data) {
133 // Add a single uint32 value
134 void addInt(uint32_t Val) {
138 // Add a uint64 value
139 void addInt(uint64_t Val) {
140 mix(uint32_t(Val >> 32));
144 // Add a range of bytes from I to E.
145 template<bool ElementsHaveEvenLength>
146 void addBytes(const char *I, const char *E) {
148 // Note that aliasing rules forbid us from dereferencing
149 // reinterpret_cast<uint32_t *>(I) even if I happens to be suitably
150 // aligned, so we use memcpy instead.
151 for (; E - I >= ptrdiff_t(sizeof Data); I += sizeof Data) {
152 // A clever compiler should be able to turn this memcpy into a single
153 // aligned or unaligned load (depending on the alignment of the type T
154 // that was used in the call to addBits).
155 std::memcpy(&Data, I, sizeof Data);
158 if (!ElementsHaveEvenLength && I != E) {
160 std::memcpy(&Data, I, E - I);
165 // Add a range of bits from I to E.
167 void addBits(const T *I, const T *E) {
168 addBytes<sizeof (T) % sizeof (uint32_t) == 0>(
169 reinterpret_cast<const char *>(I),
170 reinterpret_cast<const char *>(E));
174 } // end namespace llvm