1 // Copyright 2014 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 #include "base/hash.h"
6 
7 #include <functional>
8 
SuperFastHash(const char * data,size_t len)9 uint32_t SuperFastHash(const char* data, size_t len) {
10   std::hash<std::string> hash_fn;
11   return hash_fn(std::string(data, len));
12 }
13 
14 namespace base {
15 
Hash(const void * data,size_t length)16 uint32_t Hash(const void* data, size_t length) {
17   // Currently our in-memory hash is the same as the persistent hash. The
18   // split between in-memory and persistent hash functions is maintained to
19   // allow the in-memory hash function to be updated in the future.
20   return PersistentHash(data, length);
21 }
22 
Hash(const std::string & str)23 uint32_t Hash(const std::string& str) {
24   return PersistentHash(str.data(), str.size());
25 }
26 
Hash(const string16 & str)27 uint32_t Hash(const string16& str) {
28   return PersistentHash(str.data(), str.size() * sizeof(char16));
29 }
30 
PersistentHash(const void * data,size_t length)31 uint32_t PersistentHash(const void* data, size_t length) {
32   // This hash function must not change, since it is designed to be persistable
33   // to disk.
34   if (length > static_cast<size_t>(std::numeric_limits<int>::max())) {
35     NOTREACHED();
36     return 0;
37   }
38   return ::SuperFastHash(reinterpret_cast<const char*>(data),
39                          static_cast<int>(length));
40 }
41 
PersistentHash(const std::string & str)42 uint32_t PersistentHash(const std::string& str) {
43   return PersistentHash(str.data(), str.size());
44 }
45 
46 // Implement hashing for pairs of at-most 32 bit integer values.
47 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
48 // multiply-add hashing. This algorithm, as described in
49 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
50 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
51 //
52 //   h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
53 //
54 // Contact danakj@chromium.org for any questions.
HashInts32(uint32_t value1,uint32_t value2)55 size_t HashInts32(uint32_t value1, uint32_t value2) {
56   uint64_t value1_64 = value1;
57   uint64_t hash64 = (value1_64 << 32) | value2;
58 
59   if (sizeof(size_t) >= sizeof(uint64_t))
60     return static_cast<size_t>(hash64);
61 
62   uint64_t odd_random = 481046412LL << 32 | 1025306955LL;
63   uint32_t shift_random = 10121U << 16;
64 
65   hash64 = hash64 * odd_random + shift_random;
66   size_t high_bits =
67       static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
68   return high_bits;
69 }
70 
71 // Implement hashing for pairs of up-to 64-bit integer values.
72 // We use the compound integer hash method to produce a 64-bit hash code, by
73 // breaking the two 64-bit inputs into 4 32-bit values:
74 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
75 // Then we reduce our result to 32 bits if required, similar to above.
HashInts64(uint64_t value1,uint64_t value2)76 size_t HashInts64(uint64_t value1, uint64_t value2) {
77   uint32_t short_random1 = 842304669U;
78   uint32_t short_random2 = 619063811U;
79   uint32_t short_random3 = 937041849U;
80   uint32_t short_random4 = 3309708029U;
81 
82   uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff);
83   uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff);
84   uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff);
85   uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff);
86 
87   uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1;
88   uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2;
89   uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3;
90   uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4;
91 
92   uint64_t hash64 = product1 + product2 + product3 + product4;
93 
94   if (sizeof(size_t) >= sizeof(uint64_t))
95     return static_cast<size_t>(hash64);
96 
97   uint64_t odd_random = 1578233944LL << 32 | 194370989LL;
98   uint32_t shift_random = 20591U << 16;
99 
100   hash64 = hash64 * odd_random + shift_random;
101   size_t high_bits =
102       static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
103   return high_bits;
104 }
105 
106 }  // namespace base
107