low_level_hash.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Prefetch the cacheline that data resides in.
  31. PrefetchToLocalCache(data);
  32. const uint8_t* ptr = static_cast<const uint8_t*>(data);
  33. uint64_t starting_length = static_cast<uint64_t>(len);
  34. const uint8_t* last_16_ptr = ptr + starting_length - 16;
  35. uint64_t current_state = seed ^ salt[0];
  36. if (len > 64) {
  37. // If we have more than 64 bytes, we're going to handle chunks of 64
  38. // bytes at a time. We're going to build up two separate hash states
  39. // which we will then hash together.
  40. uint64_t duplicated_state0 = current_state;
  41. uint64_t duplicated_state1 = current_state;
  42. uint64_t duplicated_state2 = current_state;
  43. do {
  44. // Always prefetch the next cacheline.
  45. PrefetchToLocalCache(ptr + ABSL_CACHELINE_SIZE);
  46. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  47. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  48. uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
  49. uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
  50. uint64_t e = absl::base_internal::UnalignedLoad64(ptr + 32);
  51. uint64_t f = absl::base_internal::UnalignedLoad64(ptr + 40);
  52. uint64_t g = absl::base_internal::UnalignedLoad64(ptr + 48);
  53. uint64_t h = absl::base_internal::UnalignedLoad64(ptr + 56);
  54. current_state = Mix(a ^ salt[1], b ^ current_state);
  55. duplicated_state0 = Mix(c ^ salt[2], d ^ duplicated_state0);
  56. duplicated_state1 = Mix(e ^ salt[3], f ^ duplicated_state1);
  57. duplicated_state2 = Mix(g ^ salt[4], h ^ duplicated_state2);
  58. ptr += 64;
  59. len -= 64;
  60. } while (len > 64);
  61. current_state = (current_state ^ duplicated_state0) ^
  62. (duplicated_state1 + duplicated_state2);
  63. }
  64. // We now have a data `ptr` with at most 64 bytes and the current state
  65. // of the hashing state machine stored in current_state.
  66. if (len > 32) {
  67. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  68. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  69. uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
  70. uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
  71. uint64_t cs0 = Mix(a ^ salt[1], b ^ current_state);
  72. uint64_t cs1 = Mix(c ^ salt[2], d ^ current_state);
  73. current_state = cs0 ^ cs1;
  74. ptr += 32;
  75. len -= 32;
  76. }
  77. // We now have a data `ptr` with at most 32 bytes and the current state
  78. // of the hashing state machine stored in current_state.
  79. if (len > 16) {
  80. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  81. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  82. current_state = Mix(a ^ salt[1], b ^ current_state);
  83. }
  84. // We now have a data `ptr` with at least 1 and at most 16 bytes. But we can
  85. // safely read from `ptr + len - 16`.
  86. uint64_t a = absl::base_internal::UnalignedLoad64(last_16_ptr);
  87. uint64_t b = absl::base_internal::UnalignedLoad64(last_16_ptr + 8);
  88. return Mix(a ^ salt[1] ^ starting_length, b ^ current_state);
  89. }
  90. uint64_t LowLevelHash(const void* data, size_t len, uint64_t seed,
  91. const uint64_t salt[5]) {
  92. if (len > 16) return LowLevelHashLenGt16(data, len, seed, salt);
  93. // Prefetch the cacheline that data resides in.
  94. PrefetchToLocalCache(data);
  95. const uint8_t* ptr = static_cast<const uint8_t*>(data);
  96. uint64_t starting_length = static_cast<uint64_t>(len);
  97. uint64_t current_state = seed ^ salt[0];
  98. if (len == 0) return current_state;
  99. uint64_t a = 0;
  100. uint64_t b = 0;
  101. // We now have a data `ptr` with at least 1 and at most 16 bytes.
  102. if (len > 8) {
  103. // When we have at least 9 and at most 16 bytes, set A to the first 64
  104. // bits of the input and B to the last 64 bits of the input. Yes, they
  105. // will overlap in the middle if we are working with less than the full 16
  106. // bytes.
  107. a = absl::base_internal::UnalignedLoad64(ptr);
  108. b = absl::base_internal::UnalignedLoad64(ptr + len - 8);
  109. } else if (len > 3) {
  110. // If we have at least 4 and at most 8 bytes, set A to the first 32
  111. // bits and B to the last 32 bits.
  112. a = absl::base_internal::UnalignedLoad32(ptr);
  113. b = absl::base_internal::UnalignedLoad32(ptr + len - 4);
  114. } else {
  115. // If we have at least 1 and at most 3 bytes, read 2 bytes into A and the
  116. // other byte into B, with some adjustments.
  117. a = static_cast<uint64_t>((ptr[0] << 8) | ptr[len - 1]);
  118. b = static_cast<uint64_t>(ptr[len >> 1]);
  119. }
  120. return Mix(a ^ salt[1] ^ starting_length, b ^ current_state);
  121. }
  122. } // namespace hash_internal
  123. ABSL_NAMESPACE_END
  124. } // namespace absl