v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
hashing.h
Go to the documentation of this file.
1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_HASHING_H_
6#define V8_BASE_HASHING_H_
7
8#include <stddef.h>
9#include <stdint.h>
10
11#include <cstddef>
12#include <cstring>
13#include <functional>
14#include <type_traits>
15#include <utility>
16
18#include "src/base/bits.h"
19#include "src/base/macros.h"
20
21namespace v8::base {
22
23// base::hash is an implementation of the hash function object specified by
24// C++11. It was designed to be compatible with std::hash (in C++11) and
25// boost:hash (which in turn is based on the hash function object specified by
26// the Draft Technical Report on C++ Library Extensions (TR1)).
27//
28// base::hash is implemented by calling either the hash_value function or the
29// hash_value member function. In the first case, the namespace is not specified
30// so that it can detect overloads via argument dependent lookup. So if there is
31// a free function hash_value in the same namespace as a custom type, it will
32// get called.
33//
34// If users are asked to implement a hash function for their own types with no
35// guidance, they generally write bad hash functions. Instead, we provide a
36// base::Hasher class to pass hash-relevant member variables into, in order to
37// define a decent hash function.
38//
39// Consider the following example:
40//
41// namespace v8 {
42// namespace bar {
43// struct Coordinate {
44// int val;
45// size_t hash_value() const { return hash_value(val); }
46// };
47// struct Point {
48// Coordinate x;
49// Coordinate y;
50// };
51// size_t hash_value(Point const& p) {
52// return base::Hasher::Combine(p.x, p.y);
53// }
54// }
55//
56// namespace foo {
57// void DoSomeWork(bar::Point const& p) {
58// base::hash<bar::Point> h;
59// ...
60// size_t hash = h(p); // calls bar::hash_value(Point const&), which
61// // calls p.x.hash_value() and p.y.hash_value().
62// ...
63// }
64// }
65// }
66//
67// This header also provides implementations of hash_value for basic types.
68//
69// Based on the "Hashing User-Defined Types in C++1y" proposal from Jeffrey
70// Yasskin and Chandler Carruth, see
71// http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2012/n3333.html.
72
73template <typename>
74struct hash;
75
76// Combine two hash values together. This code was taken from MurmurHash.
77V8_INLINE size_t hash_combine(size_t seed, size_t hash) {
78#if V8_HOST_ARCH_32_BIT
79 const uint32_t c1 = 0xCC9E2D51;
80 const uint32_t c2 = 0x1B873593;
81
82 hash *= c1;
84 hash *= c2;
85
86 seed ^= hash;
87 seed = bits::RotateRight32(seed, 13);
88 seed = seed * 5 + 0xE6546B64;
89#else
90 const uint64_t m = uint64_t{0xC6A4A7935BD1E995};
91 const uint32_t r = 47;
92
93 hash *= m;
94 hash ^= hash >> r;
95 hash *= m;
96
97 seed ^= hash;
98 seed *= m;
99#endif // V8_HOST_ARCH_32_BIT
100 return seed;
101}
102
103// base::Hasher makes it easier to combine multiple fields into one hash and
104// avoids the ambiguity of the different {hash_combine} methods.
105class Hasher {
106 public:
107 constexpr Hasher() = default;
108 constexpr explicit Hasher(size_t seed) : hash_(seed) {}
109
110 // Retrieve the current hash.
111 constexpr size_t hash() const { return hash_; }
112
113 // Combine an existing hash value into this hasher's hash.
114 Hasher& AddHash(size_t other_hash) {
115 hash_ = hash_combine(hash_, other_hash);
116 return *this;
117 }
118
119 // Hash a value {t} and combine its hash into this hasher's hash.
120 template <typename T>
121 Hasher& Add(const T& t) {
122 return AddHash(base::hash<T>{}(t));
123 }
124
125 // Hash a range of values and combine the hashes into this hasher's hash.
126 template <typename Iterator>
127 Hasher& AddRange(Iterator first, Iterator last) {
128 // TODO(clemensb): If the iterator returns an integral or POD value smaller
129 // than size_t we can combine multiple elements together to get better
130 // hashing performance.
131 for (; first != last; ++first) Add(*first);
132 return *this;
133 }
134
135 // Hash a collection of values and combine the hashes into this hasher's hash.
136 template <typename C>
137 auto AddRange(C collection)
138 -> decltype(AddRange(std::begin(collection), std::end(collection))) {
139 return AddRange(std::begin(collection), std::end(collection));
140 }
141
142 // Hash multiple values and combine their hashes.
143 template <typename... T>
144 constexpr static size_t Combine(const T&... ts) {
145 Hasher hasher;
146 (..., hasher.Add(ts));
147 return hasher.hash();
148 }
149
150 private:
151 size_t hash_ = 0;
152};
153
154// Thomas Wang, Integer Hash Functions.
155// https://gist.github.com/badboy/6267743
156template <typename T>
158 switch (sizeof(T)) {
159 case 4: {
160 // "32 bit Mix Functions"
161 v = ~v + (v << 15); // v = (v << 15) - v - 1;
162 v = v ^ (v >> 12);
163 v = v + (v << 2);
164 v = v ^ (v >> 4);
165 v = v * 2057; // v = (v + (v << 3)) + (v << 11);
166 v = v ^ (v >> 16);
167 return static_cast<size_t>(v);
168 }
169 case 8: {
170 switch (sizeof(size_t)) {
171 case 4: {
172 // "64 bit to 32 bit Hash Functions"
173 v = ~v + (v << 18); // v = (v << 18) - v - 1;
174 v = v ^ (v >> 31);
175 v = v * 21; // v = (v + (v << 2)) + (v << 4);
176 v = v ^ (v >> 11);
177 v = v + (v << 6);
178 v = v ^ (v >> 22);
179 return static_cast<size_t>(v);
180 }
181 case 8: {
182 // "64 bit Mix Functions"
183 v = ~v + (v << 21); // v = (v << 21) - v - 1;
184 v = v ^ (v >> 24);
185 v = (v + (v << 3)) + (v << 8); // v * 265
186 v = v ^ (v >> 14);
187 v = (v + (v << 2)) + (v << 4); // v * 21
188 v = v ^ (v >> 28);
189 v = v + (v << 31);
190 return static_cast<size_t>(v);
191 }
192 }
193 }
194 }
195 UNREACHABLE();
196}
197
198#define V8_BASE_HASH_VALUE_TRIVIAL(type) \
199 V8_INLINE size_t hash_value(type v) { return static_cast<size_t>(v); }
201V8_BASE_HASH_VALUE_TRIVIAL(unsigned char)
202V8_BASE_HASH_VALUE_TRIVIAL(unsigned short) // NOLINT(runtime/int)
203#undef V8_BASE_HASH_VALUE_TRIVIAL
204
205V8_INLINE size_t hash_value(unsigned int v) {
206 return hash_value_unsigned_impl(v);
207}
208
209V8_INLINE size_t hash_value(unsigned long v) { // NOLINT(runtime/int)
210 return hash_value_unsigned_impl(v);
211}
212
213V8_INLINE size_t hash_value(unsigned long long v) { // NOLINT(runtime/int)
214 return hash_value_unsigned_impl(v);
215}
216
217#define V8_BASE_HASH_VALUE_SIGNED(type) \
218 V8_INLINE size_t hash_value(signed type v) { \
219 return hash_value(base::bit_cast<unsigned type>(v)); \
220 }
222V8_BASE_HASH_VALUE_SIGNED(short) // NOLINT(runtime/int)
223V8_BASE_HASH_VALUE_SIGNED(int) // NOLINT(runtime/int)
224V8_BASE_HASH_VALUE_SIGNED(long) // NOLINT(runtime/int)
225V8_BASE_HASH_VALUE_SIGNED(long long) // NOLINT(runtime/int)
226#undef V8_BASE_HASH_VALUE_SIGNED
227
228V8_INLINE size_t hash_value(float v) {
229 // 0 and -0 both hash to zero.
230 return v != 0.0f ? hash_value(base::bit_cast<uint32_t>(v)) : 0;
231}
232
233V8_INLINE size_t hash_value(double v) {
234 // 0 and -0 both hash to zero.
235 return v != 0.0 ? hash_value(base::bit_cast<uint64_t>(v)) : 0;
236}
237
238template <typename T, size_t N>
239V8_INLINE size_t hash_value(const T (&v)[N]) {
240 return Hasher{}.AddRange(v, v + N).hash();
241}
242
243template <typename T, size_t N>
244V8_INLINE size_t hash_value(T (&v)[N]) {
245 return Hasher{}.AddRange(v, v + N).hash();
246}
247
248template <typename T>
249V8_INLINE size_t hash_value(T* const& v) {
250 return hash_value(reinterpret_cast<uintptr_t>(v));
251}
252
253template <typename T1, typename T2>
254V8_INLINE size_t hash_value(std::pair<T1, T2> const& v) {
255 return Hasher::Combine(v.first, v.second);
256}
257
258template <typename... T, size_t... I>
259V8_INLINE size_t hash_value_impl(std::tuple<T...> const& v,
260 std::index_sequence<I...>) {
261 return Hasher::Combine(std::get<I>(v)...);
262}
263
264template <typename... T>
265V8_INLINE size_t hash_value(std::tuple<T...> const& v) {
266 return hash_value_impl(v, std::make_index_sequence<sizeof...(T)>());
267}
268
269template <typename T>
271 requires std::is_enum<T>::value
272{
273 return hash_value(static_cast<std::underlying_type_t<T>>(v));
274}
275
276// Provide a hash_value function for each T with a hash_value member function.
277template <typename T>
278 requires requires(const T& t) {
279 { t.hash_value() } -> std::convertible_to<size_t>;
280 }
281V8_INLINE size_t hash_value(const T& v) {
282 return v.hash_value();
283}
284
285template <typename T>
286concept Hashable = requires(const T& t) {
287 { hash_value(t) } -> std::convertible_to<size_t>;
288};
289
290// Define base::hash to call the hash_value function.
291template <Hashable T>
292struct hash<T> {
293 V8_INLINE constexpr size_t operator()(const T& v) const {
294 return hash_value(v);
295 }
296};
297
298// TODO(clemensb): Depending on the types in this template the compiler might
299// pick {hash_combine(size_t, size_t)} instead. Thus remove this template and
300// switch callers to {Hasher::Combine}.
301template <typename... Ts>
302V8_INLINE size_t hash_combine(Ts const&... vs) {
303 return Hasher{}.Combine(vs...);
304}
305
306// TODO(clemensb): Switch users to {Hasher{}.AddRange(first, last).hash()}.
307template <typename Iterator>
308V8_INLINE size_t hash_range(Iterator first, Iterator last) {
309 return Hasher{}.AddRange(first, last).hash();
310}
311
312// base::bit_equal_to is a function object class for bitwise equality
313// comparison, similar to std::equal_to, except that the comparison is performed
314// on the bit representation of the operands.
315//
316// base::bit_hash is a function object class for bitwise hashing, similar to
317// base::hash. It can be used together with base::bit_equal_to to implement a
318// hash data structure based on the bitwise representation of types.
319
320template <typename T>
321struct bit_equal_to {};
322
323template <typename T>
324struct bit_hash {};
325
326#define V8_BASE_BIT_SPECIALIZE_TRIVIAL(type) \
327 template <> \
328 struct bit_equal_to<type> : public std::equal_to<type> {}; \
329 template <> \
330 struct bit_hash<type> : public hash<type> {};
333V8_BASE_BIT_SPECIALIZE_TRIVIAL(short) // NOLINT(runtime/int)
334V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned short) // NOLINT(runtime/int)
337V8_BASE_BIT_SPECIALIZE_TRIVIAL(long) // NOLINT(runtime/int)
338V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long) // NOLINT(runtime/int)
339V8_BASE_BIT_SPECIALIZE_TRIVIAL(long long) // NOLINT(runtime/int)
340V8_BASE_BIT_SPECIALIZE_TRIVIAL(unsigned long long) // NOLINT(runtime/int)
341#undef V8_BASE_BIT_SPECIALIZE_TRIVIAL
342
343#define V8_BASE_BIT_SPECIALIZE_BIT_CAST(type, btype) \
344 template <> \
345 struct bit_equal_to<type> { \
346 V8_INLINE bool operator()(type lhs, type rhs) const { \
347 return base::bit_cast<btype>(lhs) == base::bit_cast<btype>(rhs); \
348 } \
349 }; \
350 template <> \
351 struct bit_hash<type> { \
352 V8_INLINE size_t operator()(type v) const { \
353 hash<btype> h; \
354 return h(base::bit_cast<btype>(v)); \
355 } \
356 };
357V8_BASE_BIT_SPECIALIZE_BIT_CAST(float, uint32_t)
358V8_BASE_BIT_SPECIALIZE_BIT_CAST(double, uint64_t)
359#undef V8_BASE_BIT_SPECIALIZE_BIT_CAST
360
361} // namespace v8::base
362
363// Also define std::hash for all classes that can be hashed via v8::base::hash.
364namespace std {
365template <typename T>
366 requires requires { typename v8::base::hash<T>; }
367struct hash<T> : v8::base::hash<T> {};
368
369} // namespace std
370
371#endif // V8_BASE_HASHING_H_
#define T
auto AddRange(C collection) -> decltype(AddRange(std::begin(collection), std::end(collection)))
Definition hashing.h:137
Hasher & AddRange(Iterator first, Iterator last)
Definition hashing.h:127
constexpr Hasher()=default
constexpr Hasher(size_t seed)
Definition hashing.h:108
Hasher & Add(const T &t)
Definition hashing.h:121
constexpr size_t hash() const
Definition hashing.h:111
Hasher & AddHash(size_t other_hash)
Definition hashing.h:114
static constexpr size_t Combine(const T &... ts)
Definition hashing.h:144
#define V8_BASE_BIT_SPECIALIZE_BIT_CAST(type, btype)
Definition hashing.h:343
#define V8_BASE_HASH_VALUE_TRIVIAL(type)
Definition hashing.h:198
#define V8_BASE_BIT_SPECIALIZE_TRIVIAL(type)
Definition hashing.h:326
#define V8_BASE_HASH_VALUE_SIGNED(type)
Definition hashing.h:217
int m
Definition mul-fft.cc:294
int r
Definition mul-fft.cc:298
STL namespace.
constexpr uint32_t RotateRight32(uint32_t value, uint32_t shift)
Definition bits.h:274
V8_INLINE size_t hash_value(unsigned int v)
Definition hashing.h:205
V8_INLINE size_t hash_combine(size_t seed, size_t hash)
Definition hashing.h:77
V8_INLINE size_t hash_range(Iterator first, Iterator last)
Definition hashing.h:308
V8_INLINE size_t hash_value_unsigned_impl(T v)
Definition hashing.h:157
V8_INLINE size_t hash_value_impl(std::tuple< T... > const &v, std::index_sequence< I... >)
Definition hashing.h:259
V8_INLINE Dest bit_cast(Source const &source)
Definition macros.h:95
#define I(name, number_of_args, result_size)
Definition runtime.cc:36
#define UNREACHABLE()
Definition logging.h:67
V8_INLINE constexpr size_t operator()(const T &v) const
Definition hashing.h:293
#define V8_INLINE
Definition v8config.h:500