hash.cc 2.3 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 "absl/hash/internal/hash.h"
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <type_traits>
  18. #include "absl/base/attributes.h"
  19. #include "absl/base/config.h"
  20. #include "absl/hash/internal/low_level_hash.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace hash_internal {
  24. uint64_t MixingHashState::CombineLargeContiguousImpl32(
  25. uint64_t state, const unsigned char* first, size_t len) {
  26. while (len >= PiecewiseChunkSize()) {
  27. state = Mix(
  28. state ^ hash_internal::CityHash32(reinterpret_cast<const char*>(first),
  29. PiecewiseChunkSize()),
  30. kMul);
  31. len -= PiecewiseChunkSize();
  32. first += PiecewiseChunkSize();
  33. }
  34. // Handle the remainder.
  35. return CombineContiguousImpl(state, first, len,
  36. std::integral_constant<int, 4>{});
  37. }
  38. uint64_t MixingHashState::CombineLargeContiguousImpl64(
  39. uint64_t state, const unsigned char* first, size_t len) {
  40. while (len >= PiecewiseChunkSize()) {
  41. state = Mix(state ^ Hash64(first, PiecewiseChunkSize()), kMul);
  42. len -= PiecewiseChunkSize();
  43. first += PiecewiseChunkSize();
  44. }
  45. // Handle the remainder.
  46. return CombineContiguousImpl(state, first, len,
  47. std::integral_constant<int, 8>{});
  48. }
  49. ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
  50. #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  51. constexpr uint64_t MixingHashState::kStaticRandomData[];
  52. #endif
  53. uint64_t MixingHashState::LowLevelHashImpl(const unsigned char* data,
  54. size_t len) {
  55. return LowLevelHashLenGt16(data, len, Seed(), &kStaticRandomData[0]);
  56. }
  57. } // namespace hash_internal
  58. ABSL_NAMESPACE_END
  59. } // namespace absl