1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Utilities for hashing. */
10 * This file exports functions for hashing data down to a 32-bit value,
13 * - HashString Hash a char* or uint16_t/wchar_t* of known or unknown
16 * - HashBytes Hash a byte array of known length.
18 * - HashGeneric Hash one or more values. Currently, we support uint32_t,
19 * types which can be implicitly cast to uint32_t, data
20 * pointers, and function pointers.
22 * - AddToHash Add one or more values to the given hash. This supports the
23 * same list of types as HashGeneric.
26 * You can chain these functions together to hash complex objects. For example:
31 * uint32_t uint1, uint2;
32 * void (*callbackFn)();
36 * uint32_t hash = HashString(str);
37 * hash = AddToHash(hash, uint1, uint2);
38 * return AddToHash(hash, callbackFn);
42 * If you want to hash an nsAString or nsACString, use the HashString functions
46 #ifndef mozilla_HashFunctions_h
47 #define mozilla_HashFunctions_h
49 #include "mozilla/Assertions.h"
50 #include "mozilla/Attributes.h"
51 #include "mozilla/Char16.h"
52 #include "mozilla/Types.h"
60 * The golden ratio as a 32-bit fixed-point value.
62 static const uint32_t GoldenRatioU32
= 0x9E3779B9U
;
65 RotateBitsLeft32(uint32_t value
, uint8_t bits
)
67 MOZ_ASSERT(bits
< 32);
68 return (value
<< bits
) | (value
>> (32 - bits
));
74 AddU32ToHash(uint32_t hash
, uint32_t value
)
77 * This is the meat of all our hash routines. This hash function is not
78 * particularly sophisticated, but it seems to work well for our mostly
79 * plain-text inputs. Implementation notes follow.
81 * Our use of the golden ratio here is arbitrary; we could pick almost any
84 * * is odd (because otherwise, all our hash values will be even)
86 * * has a reasonably-even mix of 1's and 0's (consider the extreme case
87 * where we multiply by 0x3 or 0xeffffff -- this will not produce good
88 * mixing across all bits of the hash).
90 * The rotation length of 5 is also arbitrary, although an odd number is again
91 * preferable so our hash explores the whole universe of possible rotations.
93 * Finally, we multiply by the golden ratio *after* xor'ing, not before.
94 * Otherwise, if |hash| is 0 (as it often is for the beginning of a message),
97 * (GoldenRatioU32 * RotateBitsLeft(hash, 5)) |xor| value
99 * evaluates to |value|.
101 * (Number-theoretic aside: Because any odd number |m| is relatively prime to
102 * our modulus (2^32), the list
104 * [x * m (mod 2^32) for 0 <= x < 2^32]
106 * has no duplicate elements. This means that multiplying by |m| does not
107 * cause us to skip any possible hash values.
109 * It's also nice if |m| has large-ish order mod 2^32 -- that is, if the
110 * smallest k such that m^k == 1 (mod 2^32) is large -- so we can safely
111 * multiply our hash value by |m| a few times without negating the
112 * multiplicative effect. Our golden ratio constant has order 2^29, which is
113 * more than enough for our purposes.)
115 return GoldenRatioU32
* (RotateBitsLeft32(hash
, 5) ^ value
);
119 * AddUintptrToHash takes sizeof(uintptr_t) as a template parameter.
121 template<size_t PtrSize
>
123 AddUintptrToHash(uint32_t hash
, uintptr_t value
);
127 AddUintptrToHash
<4>(uint32_t hash
, uintptr_t value
)
129 return AddU32ToHash(hash
, static_cast<uint32_t>(value
));
134 AddUintptrToHash
<8>(uint32_t hash
, uintptr_t value
)
137 * The static cast to uint64_t below is necessary because this function
138 * sometimes gets compiled on 32-bit platforms (yes, even though it's a
139 * template and we never call this particular override in a 32-bit build). If
140 * we do value >> 32 on a 32-bit machine, we're shifting a 32-bit uintptr_t
141 * right 32 bits, and the compiler throws an error.
143 uint32_t v1
= static_cast<uint32_t>(value
);
144 uint32_t v2
= static_cast<uint32_t>(static_cast<uint64_t>(value
) >> 32);
145 return AddU32ToHash(AddU32ToHash(hash
, v1
), v2
);
148 } /* namespace detail */
151 * AddToHash takes a hash and some values and returns a new hash based on the
154 * Currently, we support hashing uint32_t's, values which we can implicitly
155 * convert to uint32_t, data pointers, and function pointers.
158 MOZ_WARN_UNUSED_RESULT
160 AddToHash(uint32_t hash
, A a
)
163 * Try to convert |A| to uint32_t implicitly. If this works, great. If not,
166 return detail::AddU32ToHash(hash
, a
);
170 MOZ_WARN_UNUSED_RESULT
172 AddToHash(uint32_t hash
, A
* a
)
175 * You might think this function should just take a void*. But then we'd only
176 * catch data pointers and couldn't handle function pointers.
179 static_assert(sizeof(a
) == sizeof(uintptr_t),
182 return detail::AddUintptrToHash
<sizeof(uintptr_t)>(hash
, uintptr_t(a
));
186 MOZ_WARN_UNUSED_RESULT
188 AddToHash(uint32_t hash
, uintptr_t a
)
190 return detail::AddUintptrToHash
<sizeof(uintptr_t)>(hash
, a
);
193 template<typename A
, typename B
>
194 MOZ_WARN_UNUSED_RESULT
196 AddToHash(uint32_t hash
, A a
, B b
)
198 return AddToHash(AddToHash(hash
, a
), b
);
201 template<typename A
, typename B
, typename C
>
202 MOZ_WARN_UNUSED_RESULT
204 AddToHash(uint32_t hash
, A a
, B b
, C c
)
206 return AddToHash(AddToHash(hash
, a
, b
), c
);
209 template<typename A
, typename B
, typename C
, typename D
>
210 MOZ_WARN_UNUSED_RESULT
212 AddToHash(uint32_t hash
, A a
, B b
, C c
, D d
)
214 return AddToHash(AddToHash(hash
, a
, b
, c
), d
);
217 template<typename A
, typename B
, typename C
, typename D
, typename E
>
218 MOZ_WARN_UNUSED_RESULT
220 AddToHash(uint32_t hash
, A a
, B b
, C c
, D d
, E e
)
222 return AddToHash(AddToHash(hash
, a
, b
, c
, d
), e
);
226 * The HashGeneric class of functions let you hash one or more values.
228 * If you want to hash together two values x and y, calling HashGeneric(x, y) is
229 * much better than calling AddToHash(x, y), because AddToHash(x, y) assumes
230 * that x has already been hashed.
233 MOZ_WARN_UNUSED_RESULT
237 return AddToHash(0, a
);
240 template<typename A
, typename B
>
241 MOZ_WARN_UNUSED_RESULT
243 HashGeneric(A a
, B b
)
245 return AddToHash(0, a
, b
);
248 template<typename A
, typename B
, typename C
>
249 MOZ_WARN_UNUSED_RESULT
251 HashGeneric(A a
, B b
, C c
)
253 return AddToHash(0, a
, b
, c
);
256 template<typename A
, typename B
, typename C
, typename D
>
257 MOZ_WARN_UNUSED_RESULT
259 HashGeneric(A a
, B b
, C c
, D d
)
261 return AddToHash(0, a
, b
, c
, d
);
264 template<typename A
, typename B
, typename C
, typename D
, typename E
>
265 MOZ_WARN_UNUSED_RESULT
267 HashGeneric(A a
, B b
, C c
, D d
, E e
)
269 return AddToHash(0, a
, b
, c
, d
, e
);
276 HashUntilZero(const T
* str
)
279 for (T c
; (c
= *str
); str
++)
280 hash
= AddToHash(hash
, c
);
286 HashKnownLength(const T
* str
, size_t length
)
289 for (size_t i
= 0; i
< length
; i
++)
290 hash
= AddToHash(hash
, str
[i
]);
294 } /* namespace detail */
297 * The HashString overloads below do just what you'd expect.
299 * If you have the string's length, you might as well call the overload which
300 * includes the length. It may be marginally faster.
302 MOZ_WARN_UNUSED_RESULT
304 HashString(const char* str
)
306 return detail::HashUntilZero(str
);
309 MOZ_WARN_UNUSED_RESULT
311 HashString(const char* str
, size_t length
)
313 return detail::HashKnownLength(str
, length
);
316 MOZ_WARN_UNUSED_RESULT
318 HashString(const uint16_t* str
)
320 return detail::HashUntilZero(str
);
323 MOZ_WARN_UNUSED_RESULT
325 HashString(const uint16_t* str
, size_t length
)
327 return detail::HashKnownLength(str
, length
);
330 #ifdef MOZ_CHAR16_IS_NOT_WCHAR
331 MOZ_WARN_UNUSED_RESULT
333 HashString(const char16_t
* str
)
335 return detail::HashUntilZero(str
);
338 MOZ_WARN_UNUSED_RESULT
340 HashString(const char16_t
* str
, size_t length
)
342 return detail::HashKnownLength(str
, length
);
347 * On Windows, wchar_t (PRUnichar) is not the same as uint16_t, even though it's
351 MOZ_WARN_UNUSED_RESULT
353 HashString(const wchar_t* str
)
355 return detail::HashUntilZero(str
);
358 MOZ_WARN_UNUSED_RESULT
360 HashString(const wchar_t* str
, size_t length
)
362 return detail::HashKnownLength(str
, length
);
367 * Hash some number of bytes.
369 * This hash walks word-by-word, rather than byte-by-byte, so you won't get the
370 * same result out of HashBytes as you would out of HashString.
372 MOZ_WARN_UNUSED_RESULT
373 extern MFBT_API
uint32_t
374 HashBytes(const void* bytes
, size_t length
);
376 } /* namespace mozilla */
377 #endif /* __cplusplus */
379 #endif /* mozilla_HashFunctions_h */