low_level_hash.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "y_absl/hash/internal/low_level_hash.h"
  15. #include "y_absl/base/internal/unaligned_access.h"
  16. #include "y_absl/base/prefetch.h"
  17. #include "y_absl/numeric/int128.h"
  18. namespace y_absl {
  19. Y_ABSL_NAMESPACE_BEGIN
  20. namespace hash_internal {
  21. static uint64_t Mix(uint64_t v0, uint64_t v1) {
  22. y_absl::uint128 p = v0;
  23. p *= v1;
  24. return y_absl::Uint128Low64(p) ^ y_absl::Uint128High64(p);
  25. }
  26. uint64_t LowLevelHash(const void* data, size_t len, uint64_t seed,
  27. const uint64_t salt[5]) {
  28. // Prefetch the cacheline that data resides in.
  29. PrefetchToLocalCache(data);
  30. const uint8_t* ptr = static_cast<const uint8_t*>(data);
  31. uint64_t starting_length = static_cast<uint64_t>(len);
  32. uint64_t current_state = seed ^ salt[0];
  33. if (len > 64) {
  34. // If we have more than 64 bytes, we're going to handle chunks of 64
  35. // bytes at a time. We're going to build up two separate hash states
  36. // which we will then hash together.
  37. uint64_t duplicated_state = current_state;
  38. do {
  39. // Always prefetch the next cacheline.
  40. PrefetchToLocalCache(ptr + Y_ABSL_CACHELINE_SIZE);
  41. uint64_t a = y_absl::base_internal::UnalignedLoad64(ptr);
  42. uint64_t b = y_absl::base_internal::UnalignedLoad64(ptr + 8);
  43. uint64_t c = y_absl::base_internal::UnalignedLoad64(ptr + 16);
  44. uint64_t d = y_absl::base_internal::UnalignedLoad64(ptr + 24);
  45. uint64_t e = y_absl::base_internal::UnalignedLoad64(ptr + 32);
  46. uint64_t f = y_absl::base_internal::UnalignedLoad64(ptr + 40);
  47. uint64_t g = y_absl::base_internal::UnalignedLoad64(ptr + 48);
  48. uint64_t h = y_absl::base_internal::UnalignedLoad64(ptr + 56);
  49. uint64_t cs0 = Mix(a ^ salt[1], b ^ current_state);
  50. uint64_t cs1 = Mix(c ^ salt[2], d ^ current_state);
  51. current_state = (cs0 ^ cs1);
  52. uint64_t ds0 = Mix(e ^ salt[3], f ^ duplicated_state);
  53. uint64_t ds1 = Mix(g ^ salt[4], h ^ duplicated_state);
  54. duplicated_state = (ds0 ^ ds1);
  55. ptr += 64;
  56. len -= 64;
  57. } while (len > 64);
  58. current_state = current_state ^ duplicated_state;
  59. }
  60. // We now have a data `ptr` with at most 64 bytes and the current state
  61. // of the hashing state machine stored in current_state.
  62. while (len > 16) {
  63. uint64_t a = y_absl::base_internal::UnalignedLoad64(ptr);
  64. uint64_t b = y_absl::base_internal::UnalignedLoad64(ptr + 8);
  65. current_state = Mix(a ^ salt[1], b ^ current_state);
  66. ptr += 16;
  67. len -= 16;
  68. }
  69. // We now have a data `ptr` with at most 16 bytes.
  70. uint64_t a = 0;
  71. uint64_t b = 0;
  72. if (len > 8) {
  73. // When we have at least 9 and at most 16 bytes, set A to the first 64
  74. // bits of the input and B to the last 64 bits of the input. Yes, they will
  75. // overlap in the middle if we are working with less than the full 16
  76. // bytes.
  77. a = y_absl::base_internal::UnalignedLoad64(ptr);
  78. b = y_absl::base_internal::UnalignedLoad64(ptr + len - 8);
  79. } else if (len > 3) {
  80. // If we have at least 4 and at most 8 bytes, set A to the first 32
  81. // bits and B to the last 32 bits.
  82. a = y_absl::base_internal::UnalignedLoad32(ptr);
  83. b = y_absl::base_internal::UnalignedLoad32(ptr + len - 4);
  84. } else if (len > 0) {
  85. // If we have at least 1 and at most 3 bytes, read all of the provided
  86. // bits into A, with some adjustments.
  87. a = static_cast<uint64_t>((ptr[0] << 16) | (ptr[len >> 1] << 8) |
  88. ptr[len - 1]);
  89. b = 0;
  90. } else {
  91. a = 0;
  92. b = 0;
  93. }
  94. uint64_t w = Mix(a ^ salt[1], b ^ current_state);
  95. uint64_t z = salt[1] ^ starting_length;
  96. return Mix(w, z);
  97. }
  98. } // namespace hash_internal
  99. Y_ABSL_NAMESPACE_END
  100. } // namespace y_absl