low_level_hash.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "absl/base/internal/unaligned_access.h"
  16. #include "absl/numeric/bits.h"
  17. #include "absl/numeric/int128.h"
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace hash_internal {
  21. static uint64_t Mix(uint64_t v0, uint64_t v1) {
  22. #if !defined(__aarch64__)
  23. // The default bit-mixer uses 64x64->128-bit multiplication.
  24. absl::uint128 p = v0;
  25. p *= v1;
  26. return absl::Uint128Low64(p) ^ absl::Uint128High64(p);
  27. #else
  28. // The default bit-mixer above would perform poorly on some ARM microarchs,
  29. // where calculating a 128-bit product requires a sequence of two
  30. // instructions with a high combined latency and poor throughput.
  31. // Instead, we mix bits using only 64-bit arithmetic, which is faster.
  32. uint64_t p = v0 ^ absl::rotl(v1, 40);
  33. p *= v1 ^ absl::rotl(v0, 39);
  34. return p ^ (p >> 11);
  35. #endif
  36. }
  37. uint64_t LowLevelHash(const void* data, size_t len, uint64_t seed,
  38. const uint64_t salt[]) {
  39. const uint8_t* ptr = static_cast<const uint8_t*>(data);
  40. uint64_t starting_length = static_cast<uint64_t>(len);
  41. uint64_t current_state = seed ^ salt[0];
  42. if (len > 64) {
  43. // If we have more than 64 bytes, we're going to handle chunks of 64
  44. // bytes at a time. We're going to build up two separate hash states
  45. // which we will then hash together.
  46. uint64_t duplicated_state = current_state;
  47. do {
  48. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  49. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  50. uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
  51. uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
  52. uint64_t e = absl::base_internal::UnalignedLoad64(ptr + 32);
  53. uint64_t f = absl::base_internal::UnalignedLoad64(ptr + 40);
  54. uint64_t g = absl::base_internal::UnalignedLoad64(ptr + 48);
  55. uint64_t h = absl::base_internal::UnalignedLoad64(ptr + 56);
  56. uint64_t cs0 = Mix(a ^ salt[1], b ^ current_state);
  57. uint64_t cs1 = Mix(c ^ salt[2], d ^ current_state);
  58. current_state = (cs0 ^ cs1);
  59. uint64_t ds0 = Mix(e ^ salt[3], f ^ duplicated_state);
  60. uint64_t ds1 = Mix(g ^ salt[4], h ^ duplicated_state);
  61. duplicated_state = (ds0 ^ ds1);
  62. ptr += 64;
  63. len -= 64;
  64. } while (len > 64);
  65. current_state = current_state ^ duplicated_state;
  66. }
  67. // We now have a data `ptr` with at most 64 bytes and the current state
  68. // of the hashing state machine stored in current_state.
  69. while (len > 16) {
  70. uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
  71. uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
  72. current_state = Mix(a ^ salt[1], b ^ current_state);
  73. ptr += 16;
  74. len -= 16;
  75. }
  76. // We now have a data `ptr` with at most 16 bytes.
  77. uint64_t a = 0;
  78. uint64_t b = 0;
  79. if (len > 8) {
  80. // When we have at least 9 and at most 16 bytes, set A to the first 64
  81. // bits of the input and B to the last 64 bits of the input. Yes, they will
  82. // overlap in the middle if we are working with less than the full 16
  83. // bytes.
  84. a = absl::base_internal::UnalignedLoad64(ptr);
  85. b = absl::base_internal::UnalignedLoad64(ptr + len - 8);
  86. } else if (len > 3) {
  87. // If we have at least 4 and at most 8 bytes, set A to the first 32
  88. // bits and B to the last 32 bits.
  89. a = absl::base_internal::UnalignedLoad32(ptr);
  90. b = absl::base_internal::UnalignedLoad32(ptr + len - 4);
  91. } else if (len > 0) {
  92. // If we have at least 1 and at most 3 bytes, read all of the provided
  93. // bits into A, with some adjustments.
  94. a = ((ptr[0] << 16) | (ptr[len >> 1] << 8) | ptr[len - 1]);
  95. b = 0;
  96. } else {
  97. a = 0;
  98. b = 0;
  99. }
  100. uint64_t w = Mix(a ^ salt[1], b ^ current_state);
  101. uint64_t z = salt[1] ^ starting_length;
  102. return Mix(w, z);
  103. }
  104. } // namespace hash_internal
  105. ABSL_NAMESPACE_END
  106. } // namespace absl