wyhash.cc 3.9 KB

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