Coverage Report

Created: 2025-07-17 06:14

/src/abseil-cpp/absl/hash/internal/hash.cc
Line
Count
Source (jump to first uncovered line)
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
#include <cassert>
18
#include <cstddef>
19
#include <cstdint>
20
#include <type_traits>
21
22
#include "absl/base/attributes.h"
23
#include "absl/base/config.h"
24
#include "absl/base/internal/unaligned_access.h"
25
#include "absl/base/optimization.h"
26
#include "absl/base/prefetch.h"
27
#include "absl/hash/internal/city.h"
28
29
namespace absl {
30
ABSL_NAMESPACE_BEGIN
31
namespace hash_internal {
32
33
namespace {
34
35
3.45M
uint64_t Mix32Bytes(const uint8_t* ptr, uint64_t current_state) {
36
3.45M
  uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
37
3.45M
  uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
38
3.45M
  uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
39
3.45M
  uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
40
41
3.45M
  uint64_t cs0 = Mix(a ^ kStaticRandomData[1], b ^ current_state);
42
3.45M
  uint64_t cs1 = Mix(c ^ kStaticRandomData[2], d ^ current_state);
43
3.45M
  return cs0 ^ cs1;
44
3.45M
}
45
46
[[maybe_unused]] uint64_t LowLevelHashLenGt32(const void* data, size_t len,
47
1.81M
                                              uint64_t seed) {
48
1.81M
  assert(len > 32);
49
1.81M
  const uint8_t* ptr = static_cast<const uint8_t*>(data);
50
1.81M
  uint64_t current_state = seed ^ kStaticRandomData[0] ^ len;
51
1.81M
  const uint8_t* last_32_ptr = ptr + len - 32;
52
53
1.81M
  if (len > 64) {
54
    // If we have more than 64 bytes, we're going to handle chunks of 64
55
    // bytes at a time. We're going to build up four separate hash states
56
    // which we will then hash together. This avoids short dependency chains.
57
1.73M
    uint64_t duplicated_state0 = current_state;
58
1.73M
    uint64_t duplicated_state1 = current_state;
59
1.73M
    uint64_t duplicated_state2 = current_state;
60
61
22.2M
    do {
62
      // Always prefetch the next cacheline.
63
22.2M
      PrefetchToLocalCache(ptr + ABSL_CACHELINE_SIZE);
64
65
22.2M
      uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
66
22.2M
      uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
67
22.2M
      uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
68
22.2M
      uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
69
22.2M
      uint64_t e = absl::base_internal::UnalignedLoad64(ptr + 32);
70
22.2M
      uint64_t f = absl::base_internal::UnalignedLoad64(ptr + 40);
71
22.2M
      uint64_t g = absl::base_internal::UnalignedLoad64(ptr + 48);
72
22.2M
      uint64_t h = absl::base_internal::UnalignedLoad64(ptr + 56);
73
74
22.2M
      current_state = Mix(a ^ kStaticRandomData[1], b ^ current_state);
75
22.2M
      duplicated_state0 = Mix(c ^ kStaticRandomData[2], d ^ duplicated_state0);
76
77
22.2M
      duplicated_state1 = Mix(e ^ kStaticRandomData[3], f ^ duplicated_state1);
78
22.2M
      duplicated_state2 = Mix(g ^ kStaticRandomData[4], h ^ duplicated_state2);
79
80
22.2M
      ptr += 64;
81
22.2M
      len -= 64;
82
22.2M
    } while (len > 64);
83
84
1.73M
    current_state = (current_state ^ duplicated_state0) ^
85
1.73M
                    (duplicated_state1 + duplicated_state2);
86
1.73M
  }
87
88
  // We now have a data `ptr` with at most 64 bytes and the current state
89
  // of the hashing state machine stored in current_state.
90
1.81M
  if (len > 32) {
91
1.63M
    current_state = Mix32Bytes(ptr, current_state);
92
1.63M
  }
93
94
  // We now have a data `ptr` with at most 32 bytes and the current state
95
  // of the hashing state machine stored in current_state. But we can
96
  // safely read from `ptr + len - 32`.
97
1.81M
  return Mix32Bytes(last_32_ptr, current_state);
98
1.81M
}
99
100
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t HashBlockOn32Bit(
101
0
    const unsigned char* data, size_t len, uint64_t state) {
102
0
  // TODO(b/417141985): expose and use CityHash32WithSeed.
103
0
  // Note: we can't use PrecombineLengthMix here because len can be up to 1024.
104
0
  return CombineRawImpl(
105
0
      state + len,
106
0
      hash_internal::CityHash32(reinterpret_cast<const char*>(data), len));
107
0
}
108
109
ABSL_ATTRIBUTE_NOINLINE uint64_t
110
0
SplitAndCombineOn32Bit(const unsigned char* first, size_t len, uint64_t state) {
111
0
  while (len >= PiecewiseChunkSize()) {
112
0
    state = HashBlockOn32Bit(first, PiecewiseChunkSize(), state);
113
0
    len -= PiecewiseChunkSize();
114
0
    first += PiecewiseChunkSize();
115
0
  }
116
0
  // Do not call CombineContiguousImpl for empty range since it is modifying
117
0
  // state.
118
0
  if (len == 0) {
119
0
    return state;
120
0
  }
121
0
  // Handle the remainder.
122
0
  return CombineContiguousImpl(state, first, len,
123
0
                               std::integral_constant<int, 4>{});
124
0
}
125
126
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t HashBlockOn64Bit(
127
1.81M
    const unsigned char* data, size_t len, uint64_t state) {
128
1.81M
#ifdef ABSL_HAVE_INTRINSIC_INT128
129
1.81M
  return LowLevelHashLenGt32(data, len, state);
130
#else
131
  return hash_internal::CityHash64WithSeed(reinterpret_cast<const char*>(data),
132
                                           len, state);
133
#endif
134
1.81M
}
135
136
ABSL_ATTRIBUTE_NOINLINE uint64_t
137
137k
SplitAndCombineOn64Bit(const unsigned char* first, size_t len, uint64_t state) {
138
1.49M
  while (len >= PiecewiseChunkSize()) {
139
1.36M
    state = HashBlockOn64Bit(first, PiecewiseChunkSize(), state);
140
1.36M
    len -= PiecewiseChunkSize();
141
1.36M
    first += PiecewiseChunkSize();
142
1.36M
  }
143
  // Do not call CombineContiguousImpl for empty range since it is modifying
144
  // state.
145
137k
  if (len == 0) {
146
330
    return state;
147
330
  }
148
  // Handle the remainder.
149
136k
  return CombineContiguousImpl(state, first, len,
150
136k
                               std::integral_constant<int, 8>{});
151
137k
}
152
153
}  // namespace
154
155
uint64_t CombineLargeContiguousImplOn32BitLengthGt8(const unsigned char* first,
156
                                                    size_t len,
157
0
                                                    uint64_t state) {
158
0
  assert(len > 8);
159
0
  assert(sizeof(size_t) == 4);  // NOLINT(misc-static-assert)
160
0
  if (ABSL_PREDICT_TRUE(len <= PiecewiseChunkSize())) {
161
0
    return HashBlockOn32Bit(first, len, state);
162
0
  }
163
0
  return SplitAndCombineOn32Bit(first, len, state);
164
0
}
165
166
uint64_t CombineLargeContiguousImplOn64BitLengthGt32(const unsigned char* first,
167
                                                     size_t len,
168
595k
                                                     uint64_t state) {
169
595k
  assert(len > 32);
170
595k
  assert(sizeof(size_t) == 8);  // NOLINT(misc-static-assert)
171
595k
  if (ABSL_PREDICT_TRUE(len <= PiecewiseChunkSize())) {
172
458k
    return HashBlockOn64Bit(first, len, state);
173
458k
  }
174
137k
  return SplitAndCombineOn64Bit(first, len, state);
175
595k
}
176
177
ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
178
179
}  // namespace hash_internal
180
ABSL_NAMESPACE_END
181
}  // namespace absl