low_level_hash.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2020 The Abseil Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/hash/internal/low_level_hash.h"
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include "absl/base/internal/unaligned_access.h"
  18. #include "absl/base/prefetch.h"
  19. #include "absl/numeric/int128.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. namespace hash_internal {
  23. static uint64_t Mix(uint64_t v0, uint64_t v1) {
  24. absl::uint128 p = v0;
  25. p *= v1;
  26. return absl::Uint128Low64(p) ^ absl::Uint128High64(p);
  27. }
  28. uint64_t LowLevelHashLenGt16(const void* data, size_t len, uint64_t seed,
  29. const uint64_t salt[5]) {
  30. const uint8_t* ptr = static_cast<const uint8_t*>(data);
  31. uint64_t starting_length = static_cast<uint64_t>(len);
  32. const uint8_t* last_16_ptr = ptr + starting_length - 16;
  33. uint64_t current_state = seed ^ salt[0];
  34. if (len > 64) {
  35. // If we have more than 64 bytes, we're going to handle chunks of 64
  36. // bytes at a time. We're going to build up four separate hash states
  37. // which we will then hash together. This avoids short dependency chains.
  38. uint64_t duplicated_state0 = current_state;
  39. uint64_t duplicated_state1 = current_state;
  40. uint64_t duplicated_state2 = current_state;
  41. do {
  42. // Always prefetch the next cacheline.
  43. PrefetchToLocalCache(ptr + ABSL_CACHELINE_SIZE);
  44. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  45. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  46. uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
  47. uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
  48. uint64_t e = absl::base_internal::UnalignedLoad64(ptr + 32);
  49. uint64_t f = absl::base_internal::UnalignedLoad64(ptr + 40);
  50. uint64_t g = absl::base_internal::UnalignedLoad64(ptr + 48);
  51. uint64_t h = absl::base_internal::UnalignedLoad64(ptr + 56);
  52. current_state = Mix(a ^ salt[1], b ^ current_state);
  53. duplicated_state0 = Mix(c ^ salt[2], d ^ duplicated_state0);
  54. duplicated_state1 = Mix(e ^ salt[3], f ^ duplicated_state1);
  55. duplicated_state2 = Mix(g ^ salt[4], h ^ duplicated_state2);
  56. ptr += 64;
  57. len -= 64;
  58. } while (len > 64);
  59. current_state = (current_state ^ duplicated_state0) ^
  60. (duplicated_state1 + duplicated_state2);
  61. }
  62. // We now have a data `ptr` with at most 64 bytes and the current state
  63. // of the hashing state machine stored in current_state.
  64. if (len > 32) {
  65. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  66. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  67. uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
  68. uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
  69. uint64_t cs0 = Mix(a ^ salt[1], b ^ current_state);
  70. uint64_t cs1 = Mix(c ^ salt[2], d ^ current_state);
  71. current_state = cs0 ^ cs1;
  72. ptr += 32;
  73. len -= 32;
  74. }
  75. // We now have a data `ptr` with at most 32 bytes and the current state
  76. // of the hashing state machine stored in current_state.
  77. if (len > 16) {
  78. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  79. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  80. current_state = Mix(a ^ salt[1], b ^ current_state);
  81. }
  82. // We now have a data `ptr` with at least 1 and at most 16 bytes. But we can
  83. // safely read from `ptr + len - 16`.
  84. uint64_t a = absl::base_internal::UnalignedLoad64(last_16_ptr);
  85. uint64_t b = absl::base_internal::UnalignedLoad64(last_16_ptr + 8);
  86. return Mix(a ^ salt[1] ^ starting_length, b ^ current_state);
  87. }
  88. uint64_t LowLevelHash(const void* data, size_t len, uint64_t seed,
  89. const uint64_t salt[5]) {
  90. if (len > 16) return LowLevelHashLenGt16(data, len, seed, salt);
  91. // Prefetch the cacheline that data resides in.
  92. PrefetchToLocalCache(data);
  93. const uint8_t* ptr = static_cast<const uint8_t*>(data);
  94. uint64_t starting_length = static_cast<uint64_t>(len);
  95. uint64_t current_state = seed ^ salt[0];
  96. if (len == 0) return current_state;
  97. uint64_t a = 0;
  98. uint64_t b = 0;
  99. // We now have a data `ptr` with at least 1 and at most 16 bytes.
  100. if (len > 8) {
  101. // When we have at least 9 and at most 16 bytes, set A to the first 64
  102. // bits of the input and B to the last 64 bits of the input. Yes, they
  103. // will overlap in the middle if we are working with less than the full 16
  104. // bytes.
  105. a = absl::base_internal::UnalignedLoad64(ptr);
  106. b = absl::base_internal::UnalignedLoad64(ptr + len - 8);
  107. } else if (len > 3) {
  108. // If we have at least 4 and at most 8 bytes, set A to the first 32
  109. // bits and B to the last 32 bits.
  110. a = absl::base_internal::UnalignedLoad32(ptr);
  111. b = absl::base_internal::UnalignedLoad32(ptr + len - 4);
  112. } else {
  113. // If we have at least 1 and at most 3 bytes, read 2 bytes into A and the
  114. // other byte into B, with some adjustments.
  115. a = static_cast<uint64_t>((ptr[0] << 8) | ptr[len - 1]);
  116. b = static_cast<uint64_t>(ptr[len >> 1]);
  117. }
  118. return Mix(a ^ salt[1] ^ starting_length, b ^ current_state);
  119. }
  120. } // namespace hash_internal
  121. ABSL_NAMESPACE_END
  122. } // namespace absl