hash.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2018 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/hash.h"
  15. namespace y_absl {
  16. Y_ABSL_NAMESPACE_BEGIN
  17. namespace hash_internal {
  18. uint64_t MixingHashState::CombineLargeContiguousImpl32(
  19. uint64_t state, const unsigned char* first, size_t len) {
  20. while (len >= PiecewiseChunkSize()) {
  21. state = Mix(state,
  22. hash_internal::CityHash32(reinterpret_cast<const char*>(first),
  23. PiecewiseChunkSize()));
  24. len -= PiecewiseChunkSize();
  25. first += PiecewiseChunkSize();
  26. }
  27. // Handle the remainder.
  28. return CombineContiguousImpl(state, first, len,
  29. std::integral_constant<int, 4>{});
  30. }
  31. uint64_t MixingHashState::CombineLargeContiguousImpl64(
  32. uint64_t state, const unsigned char* first, size_t len) {
  33. while (len >= PiecewiseChunkSize()) {
  34. state = Mix(state, Hash64(first, PiecewiseChunkSize()));
  35. len -= PiecewiseChunkSize();
  36. first += PiecewiseChunkSize();
  37. }
  38. // Handle the remainder.
  39. return CombineContiguousImpl(state, first, len,
  40. std::integral_constant<int, 8>{});
  41. }
  42. Y_ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
  43. // The salt array used by LowLevelHash. This array is NOT the mechanism used to
  44. // make y_absl::Hash non-deterministic between program invocations. See `Seed()`
  45. // for that mechanism.
  46. //
  47. // Any random values are fine. These values are just digits from the decimal
  48. // part of pi.
  49. // https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number
  50. constexpr uint64_t kHashSalt[5] = {
  51. uint64_t{0x243F6A8885A308D3}, uint64_t{0x13198A2E03707344},
  52. uint64_t{0xA4093822299F31D0}, uint64_t{0x082EFA98EC4E6C89},
  53. uint64_t{0x452821E638D01377},
  54. };
  55. uint64_t MixingHashState::LowLevelHashImpl(const unsigned char* data,
  56. size_t len) {
  57. return LowLevelHash(data, len, Seed(), kHashSalt);
  58. }
  59. } // namespace hash_internal
  60. Y_ABSL_NAMESPACE_END
  61. } // namespace y_absl