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