hash.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "base/hash/hash.h"
  5. #include "base/check_op.h"
  6. #include "base/notreached.h"
  7. #include "base/rand_util.h"
  8. #include "base/third_party/cityhash/city.h"
  9. #include "build/build_config.h"
  10. // Definition in base/third_party/superfasthash/superfasthash.c. (Third-party
  11. // code did not come with its own header file, so declaring the function here.)
  12. // Note: This algorithm is also in Blink under Source/wtf/StringHasher.h.
  13. extern "C" uint32_t SuperFastHash(const char* data, int len);
  14. namespace base {
  15. namespace {
  16. size_t FastHashImpl(base::span<const uint8_t> data) {
  17. // We use the updated CityHash within our namespace (not the deprecated
  18. // version from third_party/smhasher).
  19. if constexpr (sizeof(size_t) > 4) {
  20. return base::internal::cityhash_v111::CityHash64(
  21. reinterpret_cast<const char*>(data.data()), data.size());
  22. } else {
  23. return base::internal::cityhash_v111::CityHash32(
  24. reinterpret_cast<const char*>(data.data()), data.size());
  25. }
  26. }
  27. // Implement hashing for pairs of at-most 32 bit integer values.
  28. // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
  29. // multiply-add hashing. This algorithm, as described in
  30. // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
  31. // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
  32. //
  33. // h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
  34. //
  35. // Contact danakj@chromium.org for any questions.
  36. size_t HashInts32Impl(uint32_t value1, uint32_t value2) {
  37. uint64_t value1_64 = value1;
  38. uint64_t hash64 = (value1_64 << 32) | value2;
  39. if (sizeof(size_t) >= sizeof(uint64_t))
  40. return static_cast<size_t>(hash64);
  41. uint64_t odd_random = 481046412LL << 32 | 1025306955LL;
  42. uint32_t shift_random = 10121U << 16;
  43. hash64 = hash64 * odd_random + shift_random;
  44. size_t high_bits =
  45. static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
  46. return high_bits;
  47. }
  48. // Implement hashing for pairs of up-to 64-bit integer values.
  49. // We use the compound integer hash method to produce a 64-bit hash code, by
  50. // breaking the two 64-bit inputs into 4 32-bit values:
  51. // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
  52. // Then we reduce our result to 32 bits if required, similar to above.
  53. size_t HashInts64Impl(uint64_t value1, uint64_t value2) {
  54. uint32_t short_random1 = 842304669U;
  55. uint32_t short_random2 = 619063811U;
  56. uint32_t short_random3 = 937041849U;
  57. uint32_t short_random4 = 3309708029U;
  58. uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff);
  59. uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff);
  60. uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff);
  61. uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff);
  62. uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1;
  63. uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2;
  64. uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3;
  65. uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4;
  66. uint64_t hash64 = product1 + product2 + product3 + product4;
  67. if (sizeof(size_t) >= sizeof(uint64_t))
  68. return static_cast<size_t>(hash64);
  69. uint64_t odd_random = 1578233944LL << 32 | 194370989LL;
  70. uint32_t shift_random = 20591U << 16;
  71. hash64 = hash64 * odd_random + shift_random;
  72. size_t high_bits =
  73. static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
  74. return high_bits;
  75. }
  76. // The random seed is used to perturb the output of base::FastHash() and
  77. // base::HashInts() so that it is only deterministic within the lifetime of a
  78. // process. This prevents inadvertent dependencies on the underlying
  79. // implementation, e.g. anything that persists the hash value and expects it to
  80. // be unchanging will break.
  81. //
  82. // Note: this is the same trick absl uses to generate a random seed. This is
  83. // more robust than using base::RandBytes(), which can fail inside a sandboxed
  84. // environment. Note that without ASLR, the seed won't be quite as random...
  85. #if DCHECK_IS_ON()
  86. constexpr const void* kSeed = &kSeed;
  87. #endif
  88. template <typename T>
  89. T Scramble(T input) {
  90. #if DCHECK_IS_ON()
  91. return HashInts64Impl(input, reinterpret_cast<uintptr_t>(kSeed));
  92. #else
  93. return input;
  94. #endif
  95. }
  96. } // namespace
  97. size_t FastHash(base::span<const uint8_t> data) {
  98. return Scramble(FastHashImpl(data));
  99. }
  100. uint32_t Hash(const void* data, size_t length) {
  101. // Currently our in-memory hash is the same as the persistent hash. The
  102. // split between in-memory and persistent hash functions is maintained to
  103. // allow the in-memory hash function to be updated in the future.
  104. return PersistentHash(data, length);
  105. }
  106. uint32_t Hash(const std::string& str) {
  107. return PersistentHash(as_bytes(make_span(str)));
  108. }
  109. uint32_t Hash(const std::u16string& str) {
  110. return PersistentHash(as_bytes(make_span(str)));
  111. }
  112. uint32_t PersistentHash(span<const uint8_t> data) {
  113. // This hash function must not change, since it is designed to be persistable
  114. // to disk.
  115. if (data.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
  116. NOTREACHED();
  117. return 0;
  118. }
  119. return ::SuperFastHash(reinterpret_cast<const char*>(data.data()),
  120. static_cast<int>(data.size()));
  121. }
  122. uint32_t PersistentHash(const void* data, size_t length) {
  123. return PersistentHash(make_span(static_cast<const uint8_t*>(data), length));
  124. }
  125. uint32_t PersistentHash(const std::string& str) {
  126. return PersistentHash(str.data(), str.size());
  127. }
  128. size_t HashInts32(uint32_t value1, uint32_t value2) {
  129. return Scramble(HashInts32Impl(value1, value2));
  130. }
  131. // Implement hashing for pairs of up-to 64-bit integer values.
  132. // We use the compound integer hash method to produce a 64-bit hash code, by
  133. // breaking the two 64-bit inputs into 4 32-bit values:
  134. // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
  135. // Then we reduce our result to 32 bits if required, similar to above.
  136. size_t HashInts64(uint64_t value1, uint64_t value2) {
  137. return Scramble(HashInts64Impl(value1, value2));
  138. }
  139. } // namespace base