1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
//===-- llvm/ADT/Hashing.h - Utilities for hashing --------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines utilities for computing hash values for various data types.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ADT_HASHING_H
#define LLVM_ADT_HASHING_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include <cstring>
namespace llvm {
/// Class to compute a hash value from multiple data fields of arbitrary
/// types. Note that if you are hashing a single data type, such as a
/// string, it may be cheaper to use a hash algorithm that is tailored
/// for that specific data type.
/// Typical Usage:
/// GeneralHash Hash;
/// Hash.add(someValue);
/// Hash.add(someOtherValue);
/// return Hash.finish();
/// Adapted from MurmurHash2 by Austin Appleby
class GeneralHash {
private:
enum {
M = 0x5bd1e995
};
unsigned Hash;
unsigned Count;
public:
GeneralHash(unsigned Seed = 0) : Hash(Seed), Count(0) {}
/// Add a pointer value.
/// Note: this adds pointers to the hash using sizes and endianness that
/// depend on the host. It doesn't matter however, because hashing on
/// pointer values is inherently unstable.
template<typename T>
GeneralHash& add(const T *PtrVal) {
addBits(&PtrVal, &PtrVal + 1);
return *this;
}
/// Add an ArrayRef of arbitrary data.
template<typename T>
GeneralHash& add(ArrayRef<T> ArrayVal) {
addBits(ArrayVal.begin(), ArrayVal.end());
return *this;
}
/// Add a string
GeneralHash& add(StringRef StrVal) {
addBits(StrVal.begin(), StrVal.end());
return *this;
}
/// Add an signed 32-bit integer.
GeneralHash& add(int32_t Data) {
addInt(uint32_t(Data));
return *this;
}
/// Add an unsigned 32-bit integer.
GeneralHash& add(uint32_t Data) {
addInt(Data);
return *this;
}
/// Add an signed 64-bit integer.
GeneralHash& add(int64_t Data) {
addInt(uint64_t(Data));
return *this;
}
/// Add an unsigned 64-bit integer.
GeneralHash& add(uint64_t Data) {
addInt(Data);
return *this;
}
/// Add a float
GeneralHash& add(float Data) {
union {
float D; uint32_t I;
};
D = Data;
addInt(I);
return *this;
}
/// Add a double
GeneralHash& add(double Data) {
union {
double D; uint64_t I;
};
D = Data;
addInt(I);
return *this;
}
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
unsigned finish() {
mix(Count);
Hash ^= Hash >> 13;
Hash *= M;
Hash ^= Hash >> 15;
return Hash;
}
private:
void mix(uint32_t Data) {
++Count;
Data *= M;
Data ^= Data >> 24;
Data *= M;
Hash *= M;
Hash ^= Data;
}
// Add a single uint32 value
void addInt(uint32_t Val) {
mix(Val);
}
// Add a uint64 value
void addInt(uint64_t Val) {
mix(uint32_t(Val >> 32));
mix(uint32_t(Val));
}
// Add a range of bytes from I to E.
void addBytes(const char *I, const char *E) {
uint32_t Data;
// Note that aliasing rules forbid us from dereferencing
// reinterpret_cast<uint32_t *>(I) even if I happens to be suitably
// aligned, so we use memcpy instead.
for (; E - I >= ptrdiff_t(sizeof Data); I += sizeof Data) {
// A clever compiler should be able to turn this memcpy into a single
// aligned or unaligned load (depending on the alignment of the type T
// that was used in the call to addBits).
std::memcpy(&Data, I, sizeof Data);
mix(Data);
}
if (I != E) {
Data = 0;
std::memcpy(&Data, I, E - I);
mix(Data);
}
}
// Add a range of bits from I to E.
template<typename T>
void addBits(const T *I, const T *E) {
addBytes(reinterpret_cast<const char *>(I),
reinterpret_cast<const char *>(E));
}
};
} // end namespace llvm
#endif
|