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