1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
* vim: set ts=8 sw=4 et tw=99 ft=cpp:
4
* This Source Code Form is subject to the terms of the Mozilla Public
5
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
6
* You can obtain one at http://mozilla.org/MPL/2.0/. */
8
/* Utilities for hashing */
11
* This file exports functions for hashing data down to a 32-bit value,
14
* - HashString Hash a char* or uint16_t/wchar_t* of known or unknown
17
* - HashBytes Hash a byte array of known length.
19
* - HashGeneric Hash one or more values. Currently, we support uint32_t,
20
* types which can be implicitly cast to uint32_t, data
21
* pointers, and function pointers.
23
* - AddToHash Add one or more values to the given hash. This supports the
24
* same list of types as HashGeneric.
27
* You can chain these functions together to hash complex objects. For example:
29
* class ComplexObject {
31
* uint32_t uint1, uint2;
32
* void (*callbackFn)();
35
* uint32_t hash = HashString(str);
36
* hash = AddToHash(hash, uint1, uint2);
37
* return AddToHash(hash, callbackFn);
41
* If you want to hash an nsAString or nsACString, use the HashString functions
45
#ifndef mozilla_HashFunctions_h_
46
#define mozilla_HashFunctions_h_
48
#include "mozilla/Assertions.h"
49
#include "mozilla/Attributes.h"
50
#include "mozilla/StandardInteger.h"
56
* The golden ratio as a 32-bit fixed-point value.
58
static const uint32_t GoldenRatioU32 = 0x9E3779B9U;
61
RotateLeft32(uint32_t value, uint8_t bits)
63
MOZ_ASSERT(bits < 32);
64
return (value << bits) | (value >> (32 - bits));
70
AddU32ToHash(uint32_t hash, uint32_t value)
73
* This is the meat of all our hash routines. This hash function is not
74
* particularly sophisticated, but it seems to work well for our mostly
75
* plain-text inputs. Implementation notes follow.
77
* Our use of the golden ratio here is arbitrary; we could pick almost any
80
* * is odd (because otherwise, all our hash values will be even)
82
* * has a reasonably-even mix of 1's and 0's (consider the extreme case
83
* where we multiply by 0x3 or 0xeffffff -- this will not produce good
84
* mixing across all bits of the hash).
86
* The rotation length of 5 is also arbitrary, although an odd number is again
87
* preferable so our hash explores the whole universe of possible rotations.
89
* Finally, we multiply by the golden ratio *after* xor'ing, not before.
90
* Otherwise, if |hash| is 0 (as it often is for the beginning of a message),
93
* (GoldenRatioU32 * RotateLeft(hash, 5)) |xor| value
95
* evaluates to |value|.
97
* (Number-theoretic aside: Because any odd number |m| is relatively prime to
98
* our modulus (2^32), the list
100
* [x * m (mod 2^32) for 0 <= x < 2^32]
102
* has no duplicate elements. This means that multiplying by |m| does not
103
* cause us to skip any possible hash values.
105
* It's also nice if |m| has large-ish order mod 2^32 -- that is, if the
106
* smallest k such that m^k == 1 (mod 2^32) is large -- so we can safely
107
* multiply our hash value by |m| a few times without negating the
108
* multiplicative effect. Our golden ratio constant has order 2^29, which is
109
* more than enough for our purposes.)
111
return GoldenRatioU32 * (RotateLeft32(hash, 5) ^ value);
115
* AddUintptrToHash takes sizeof(uintptr_t) as a template parameter.
117
template<size_t PtrSize>
119
AddUintptrToHash(uint32_t hash, uintptr_t value);
123
AddUintptrToHash<4>(uint32_t hash, uintptr_t value)
125
return AddU32ToHash(hash, static_cast<uint32_t>(value));
130
AddUintptrToHash<8>(uint32_t hash, uintptr_t value)
133
* The static cast to uint64_t below is necessary because this function
134
* sometimes gets compiled on 32-bit platforms (yes, even though it's a
135
* template and we never call this particular override in a 32-bit build). If
136
* we do value >> 32 on a 32-bit machine, we're shifting a 32-bit uintptr_t
137
* right 32 bits, and the compiler throws an error.
139
uint32_t v1 = static_cast<uint32_t>(value);
140
uint32_t v2 = static_cast<uint32_t>(static_cast<uint64_t>(value) >> 32);
141
return AddU32ToHash(AddU32ToHash(hash, v1), v2);
144
} /* namespace detail */
147
* AddToHash takes a hash and some values and returns a new hash based on the
150
* Currently, we support hashing uint32_t's, values which we can implicitly
151
* convert to uint32_t, data pointers, and function pointers.
154
MOZ_WARN_UNUSED_RESULT
156
AddToHash(uint32_t hash, A a)
159
* Try to convert |A| to uint32_t implicitly. If this works, great. If not,
162
return detail::AddU32ToHash(hash, a);
166
MOZ_WARN_UNUSED_RESULT
168
AddToHash(uint32_t hash, A* a)
171
* You might think this function should just take a void*. But then we'd only
172
* catch data pointers and couldn't handle function pointers.
175
MOZ_STATIC_ASSERT(sizeof(a) == sizeof(uintptr_t),
178
return detail::AddUintptrToHash<sizeof(uintptr_t)>(hash, uintptr_t(a));
181
template<typename A, typename B>
182
MOZ_WARN_UNUSED_RESULT
184
AddToHash(uint32_t hash, A a, B b)
186
return AddToHash(AddToHash(hash, a), b);
189
template<typename A, typename B, typename C>
190
MOZ_WARN_UNUSED_RESULT
192
AddToHash(uint32_t hash, A a, B b, C c)
194
return AddToHash(AddToHash(hash, a, b), c);
197
template<typename A, typename B, typename C, typename D>
198
MOZ_WARN_UNUSED_RESULT
200
AddToHash(uint32_t hash, A a, B b, C c, D d)
202
return AddToHash(AddToHash(hash, a, b, c), d);
205
template<typename A, typename B, typename C, typename D, typename E>
206
MOZ_WARN_UNUSED_RESULT
208
AddToHash(uint32_t hash, A a, B b, C c, D d, E e)
210
return AddToHash(AddToHash(hash, a, b, c, d), e);
214
* The HashGeneric class of functions let you hash one or more values.
216
* If you want to hash together two values x and y, calling HashGeneric(x, y) is
217
* much better than calling AddToHash(x, y), because AddToHash(x, y) assumes
218
* that x has already been hashed.
221
MOZ_WARN_UNUSED_RESULT
225
return AddToHash(0, a);
228
template<typename A, typename B>
229
MOZ_WARN_UNUSED_RESULT
231
HashGeneric(A a, B b)
233
return AddToHash(0, a, b);
236
template<typename A, typename B, typename C>
237
MOZ_WARN_UNUSED_RESULT
239
HashGeneric(A a, B b, C c)
241
return AddToHash(0, a, b, c);
244
template<typename A, typename B, typename C, typename D>
245
MOZ_WARN_UNUSED_RESULT
247
HashGeneric(A a, B b, C c, D d)
249
return AddToHash(0, a, b, c, d);
252
template<typename A, typename B, typename C, typename D, typename E>
253
MOZ_WARN_UNUSED_RESULT
255
HashGeneric(A a, B b, C c, D d, E e)
257
return AddToHash(0, a, b, c, d, e);
264
HashUntilZero(const T* str)
267
for (T c; (c = *str); str++)
268
hash = AddToHash(hash, c);
274
HashKnownLength(const T* str, size_t length)
277
for (size_t i = 0; i < length; i++)
278
hash = AddToHash(hash, str[i]);
282
} /* namespace detail */
285
* The HashString overloads below do just what you'd expect.
287
* If you have the string's length, you might as well call the overload which
288
* includes the length. It may be marginally faster.
290
MOZ_WARN_UNUSED_RESULT
292
HashString(const char* str)
294
return detail::HashUntilZero(str);
297
MOZ_WARN_UNUSED_RESULT
299
HashString(const char* str, size_t length)
301
return detail::HashKnownLength(str, length);
304
MOZ_WARN_UNUSED_RESULT
306
HashString(const uint16_t* str)
308
return detail::HashUntilZero(str);
311
MOZ_WARN_UNUSED_RESULT
313
HashString(const uint16_t* str, size_t length)
315
return detail::HashKnownLength(str, length);
319
* On Windows, wchar_t (PRUnichar) is not the same as uint16_t, even though it's
323
MOZ_WARN_UNUSED_RESULT
325
HashString(const wchar_t* str)
327
return detail::HashUntilZero(str);
330
MOZ_WARN_UNUSED_RESULT
332
HashString(const wchar_t* str, size_t length)
334
return detail::HashKnownLength(str, length);
339
* Hash some number of bytes.
341
* This hash walks word-by-word, rather than byte-by-byte, so you won't get the
342
* same result out of HashBytes as you would out of HashString.
344
MOZ_WARN_UNUSED_RESULT
345
extern MFBT_API(uint32_t)
346
HashBytes(const void* bytes, size_t length);
348
} /* namespace mozilla */
349
#endif /* __cplusplus */
350
#endif /* mozilla_HashFunctions_h_ */