1*9356374aSAndroid Build Coastguard Worker // Copyright 2018 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker
15*9356374aSAndroid Build Coastguard Worker #include "absl/hash/internal/hash.h"
16*9356374aSAndroid Build Coastguard Worker
17*9356374aSAndroid Build Coastguard Worker namespace absl {
18*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
19*9356374aSAndroid Build Coastguard Worker namespace hash_internal {
20*9356374aSAndroid Build Coastguard Worker
CombineLargeContiguousImpl32(uint64_t state,const unsigned char * first,size_t len)21*9356374aSAndroid Build Coastguard Worker uint64_t MixingHashState::CombineLargeContiguousImpl32(
22*9356374aSAndroid Build Coastguard Worker uint64_t state, const unsigned char* first, size_t len) {
23*9356374aSAndroid Build Coastguard Worker while (len >= PiecewiseChunkSize()) {
24*9356374aSAndroid Build Coastguard Worker state = Mix(state,
25*9356374aSAndroid Build Coastguard Worker hash_internal::CityHash32(reinterpret_cast<const char*>(first),
26*9356374aSAndroid Build Coastguard Worker PiecewiseChunkSize()));
27*9356374aSAndroid Build Coastguard Worker len -= PiecewiseChunkSize();
28*9356374aSAndroid Build Coastguard Worker first += PiecewiseChunkSize();
29*9356374aSAndroid Build Coastguard Worker }
30*9356374aSAndroid Build Coastguard Worker // Handle the remainder.
31*9356374aSAndroid Build Coastguard Worker return CombineContiguousImpl(state, first, len,
32*9356374aSAndroid Build Coastguard Worker std::integral_constant<int, 4>{});
33*9356374aSAndroid Build Coastguard Worker }
34*9356374aSAndroid Build Coastguard Worker
CombineLargeContiguousImpl64(uint64_t state,const unsigned char * first,size_t len)35*9356374aSAndroid Build Coastguard Worker uint64_t MixingHashState::CombineLargeContiguousImpl64(
36*9356374aSAndroid Build Coastguard Worker uint64_t state, const unsigned char* first, size_t len) {
37*9356374aSAndroid Build Coastguard Worker while (len >= PiecewiseChunkSize()) {
38*9356374aSAndroid Build Coastguard Worker state = Mix(state, Hash64(first, PiecewiseChunkSize()));
39*9356374aSAndroid Build Coastguard Worker len -= PiecewiseChunkSize();
40*9356374aSAndroid Build Coastguard Worker first += PiecewiseChunkSize();
41*9356374aSAndroid Build Coastguard Worker }
42*9356374aSAndroid Build Coastguard Worker // Handle the remainder.
43*9356374aSAndroid Build Coastguard Worker return CombineContiguousImpl(state, first, len,
44*9356374aSAndroid Build Coastguard Worker std::integral_constant<int, 8>{});
45*9356374aSAndroid Build Coastguard Worker }
46*9356374aSAndroid Build Coastguard Worker
47*9356374aSAndroid Build Coastguard Worker ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
48*9356374aSAndroid Build Coastguard Worker
49*9356374aSAndroid Build Coastguard Worker // The salt array used by LowLevelHash. This array is NOT the mechanism used to
50*9356374aSAndroid Build Coastguard Worker // make absl::Hash non-deterministic between program invocations. See `Seed()`
51*9356374aSAndroid Build Coastguard Worker // for that mechanism.
52*9356374aSAndroid Build Coastguard Worker //
53*9356374aSAndroid Build Coastguard Worker // Any random values are fine. These values are just digits from the decimal
54*9356374aSAndroid Build Coastguard Worker // part of pi.
55*9356374aSAndroid Build Coastguard Worker // https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number
56*9356374aSAndroid Build Coastguard Worker constexpr uint64_t kHashSalt[5] = {
57*9356374aSAndroid Build Coastguard Worker uint64_t{0x243F6A8885A308D3}, uint64_t{0x13198A2E03707344},
58*9356374aSAndroid Build Coastguard Worker uint64_t{0xA4093822299F31D0}, uint64_t{0x082EFA98EC4E6C89},
59*9356374aSAndroid Build Coastguard Worker uint64_t{0x452821E638D01377},
60*9356374aSAndroid Build Coastguard Worker };
61*9356374aSAndroid Build Coastguard Worker
LowLevelHashImpl(const unsigned char * data,size_t len)62*9356374aSAndroid Build Coastguard Worker uint64_t MixingHashState::LowLevelHashImpl(const unsigned char* data,
63*9356374aSAndroid Build Coastguard Worker size_t len) {
64*9356374aSAndroid Build Coastguard Worker return LowLevelHashLenGt16(data, len, Seed(), kHashSalt);
65*9356374aSAndroid Build Coastguard Worker }
66*9356374aSAndroid Build Coastguard Worker
67*9356374aSAndroid Build Coastguard Worker } // namespace hash_internal
68*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
69*9356374aSAndroid Build Coastguard Worker } // namespace absl
70