1 // Copyright (c) 2011 The Chromium 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 BASE_HASH_H_
6 #define BASE_HASH_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <limits>
12 #include <string>
13 #include <utility>
14
15 #include "base/base_export.h"
16 #include "base/logging.h"
17
18 namespace base {
19
20 // WARNING: This hash function should not be used for any cryptographic purpose.
21 BASE_EXPORT uint32_t SuperFastHash(const char* data, size_t length);
22
23 // Computes a hash of a memory buffer |data| of a given |length|.
24 // WARNING: This hash function should not be used for any cryptographic purpose.
Hash(const char * data,size_t length)25 inline uint32_t Hash(const char* data, size_t length) {
26 return SuperFastHash(data, length);
27 }
28
29 // Computes a hash of a string |str|.
30 // WARNING: This hash function should not be used for any cryptographic purpose.
Hash(const std::string & str)31 inline uint32_t Hash(const std::string& str) {
32 return Hash(str.data(), str.size());
33 }
34
35 // Implement hashing for pairs of at-most 32 bit integer values.
36 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
37 // multiply-add hashing. This algorithm, as described in
38 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
39 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
40 //
41 // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
42 //
43 // Contact danakj@chromium.org for any questions.
HashInts32(uint32_t value1,uint32_t value2)44 inline size_t HashInts32(uint32_t value1, uint32_t value2) {
45 uint64_t value1_64 = value1;
46 uint64_t hash64 = (value1_64 << 32) | value2;
47
48 if (sizeof(size_t) >= sizeof(uint64_t))
49 return static_cast<size_t>(hash64);
50
51 uint64_t odd_random = 481046412LL << 32 | 1025306955LL;
52 uint32_t shift_random = 10121U << 16;
53
54 hash64 = hash64 * odd_random + shift_random;
55 size_t high_bits =
56 static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
57 return high_bits;
58 }
59
60 // Implement hashing for pairs of up-to 64-bit integer values.
61 // We use the compound integer hash method to produce a 64-bit hash code, by
62 // breaking the two 64-bit inputs into 4 32-bit values:
63 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
64 // Then we reduce our result to 32 bits if required, similar to above.
HashInts64(uint64_t value1,uint64_t value2)65 inline size_t HashInts64(uint64_t value1, uint64_t value2) {
66 uint32_t short_random1 = 842304669U;
67 uint32_t short_random2 = 619063811U;
68 uint32_t short_random3 = 937041849U;
69 uint32_t short_random4 = 3309708029U;
70
71 uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff);
72 uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff);
73 uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff);
74 uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff);
75
76 uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1;
77 uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2;
78 uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3;
79 uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4;
80
81 uint64_t hash64 = product1 + product2 + product3 + product4;
82
83 if (sizeof(size_t) >= sizeof(uint64_t))
84 return static_cast<size_t>(hash64);
85
86 uint64_t odd_random = 1578233944LL << 32 | 194370989LL;
87 uint32_t shift_random = 20591U << 16;
88
89 hash64 = hash64 * odd_random + shift_random;
90 size_t high_bits =
91 static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
92 return high_bits;
93 }
94
95 template <typename T1, typename T2>
HashInts(T1 value1,T2 value2)96 inline size_t HashInts(T1 value1, T2 value2) {
97 // This condition is expected to be compile-time evaluated and optimised away
98 // in release builds.
99 if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t)))
100 return HashInts64(value1, value2);
101
102 return HashInts32(value1, value2);
103 }
104
105 // A templated hasher for pairs of integer types.
106 template <typename T>
107 struct IntPairHash;
108
109 template <typename Type1, typename Type2>
110 struct IntPairHash<std::pair<Type1, Type2>> {
111 size_t operator()(std::pair<Type1, Type2> value) const {
112 return HashInts(value.first, value.second);
113 }
114 };
115
116 } // namespace base
117
118 #endif // BASE_HASH_H_
119