Coverage Report

Created: 2026-10-28 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/hash/internal/hash.cc
Line
Count
Source
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
#ifdef ABSL_AES_INTERNAL_HAVE_X86_SIMD
30
#error ABSL_AES_INTERNAL_HAVE_X86_SIMD cannot be directly set
31
#elif defined(__SSE4_2__) && defined(__AES__)
32
#define ABSL_AES_INTERNAL_HAVE_X86_SIMD
33
#endif
34
35
36
#ifdef ABSL_AES_INTERNAL_HAVE_X86_SIMD
37
#include <smmintrin.h>
38
#include <wmmintrin.h>
39
#include <xmmintrin.h>
40
#endif  // ABSL_AES_INTERNAL_HAVE_X86_SIMD
41
42
#ifdef ABSL_AES_INTERNAL_HAVE_ARM_SIMD
43
#error ABSL_AES_INTERNAL_HAVE_ARM_SIMD cannot be directly set
44
#elif defined(ABSL_INTERNAL_HAVE_ARM_NEON) && defined(__ARM_FEATURE_CRYPTO)
45
#include <arm_neon.h>
46
#define ABSL_AES_INTERNAL_HAVE_ARM_SIMD
47
#endif  // ABSL_INTERNAL_HAVE_ARM_NEON
48
49
namespace absl {
50
ABSL_NAMESPACE_BEGIN
51
namespace hash_internal {
52
53
namespace {
54
55
13.6M
void PrefetchFutureDataToLocalCache(const uint8_t* ptr) {
56
13.6M
  PrefetchToLocalCache(ptr + 5 * ABSL_CACHELINE_SIZE);
57
13.6M
}
58
59
#if defined(ABSL_AES_INTERNAL_HAVE_X86_SIMD) || \
60
    defined(ABSL_AES_INTERNAL_HAVE_ARM_SIMD)
61
62
#if defined(ABSL_AES_INTERNAL_HAVE_X86_SIMD)
63
using Vector128 = __m128i;
64
65
inline Vector128 Load128(const uint8_t* ptr) {
66
  return _mm_loadu_si128(reinterpret_cast<const Vector128*>(ptr));
67
}
68
69
inline Vector128 Set128(uint64_t a, uint64_t b) {
70
  return _mm_set_epi64x(static_cast<int64_t>(a), static_cast<int64_t>(b));
71
}
72
73
inline Vector128 Add128(Vector128 a, Vector128 b) {
74
  return _mm_add_epi64(a, b);
75
}
76
77
inline Vector128 Sub128(Vector128 a, Vector128 b) {
78
  return _mm_sub_epi64(a, b);
79
}
80
81
// Bits of the second argument to Encrypt128/Decrypt128 are XORed with the
82
// first argument after encryption/decryption.
83
84
inline Vector128 Encrypt128(Vector128 data, Vector128 key) {
85
  return _mm_aesenc_si128(data, key);
86
}
87
88
inline Vector128 Decrypt128(Vector128 data, Vector128 key) {
89
  return _mm_aesdec_si128(data, key);
90
}
91
92
// We use each value as the first argument to shuffle all the bits around. We do
93
// not add any salt to the state or loaded data, instead we vary instructions
94
// used to mix bits Encrypt128/Decrypt128 and Add128/Sub128. On x86,
95
// Add128/Sub128 are combined to one instruction with data loading like
96
// `vpaddq xmm1, xmm0, xmmword ptr [rdi]`.
97
98
inline Vector128 MixA(Vector128 a, Vector128 state) {
99
  return Decrypt128(Add128(state, a), state);
100
}
101
102
inline Vector128 MixB(Vector128 b, Vector128 state) {
103
  return Decrypt128(Sub128(state, b), state);
104
}
105
106
inline Vector128 MixC(Vector128 c, Vector128 state) {
107
  return Encrypt128(Add128(state, c), state);
108
}
109
110
inline Vector128 MixD(Vector128 d, Vector128 state) {
111
  return Encrypt128(Sub128(state, d), state);
112
}
113
114
inline uint64_t ExtractLow64(Vector128 v) {
115
  return static_cast<uint64_t>(_mm_cvtsi128_si64(v));
116
}
117
118
inline uint64_t ExtractHigh64(Vector128 v) {
119
  return static_cast<uint64_t>(_mm_extract_epi64(v, 1));
120
}
121
122
inline uint64_t Mix4x16Vectors(Vector128 a, Vector128 b, Vector128 c,
123
                               Vector128 d) {
124
  Vector128 res128 =
125
      Add128(Encrypt128(Add128(a, c), d), Decrypt128(Sub128(b, d), a));
126
  uint64_t x64 = ExtractLow64(res128);
127
  uint64_t y64 = ExtractHigh64(res128);
128
  return x64 ^ y64;
129
}
130
131
#else  // ABSL_AES_INTERNAL_HAVE_ARM_SIMD
132
133
using Vector128 = uint8x16_t;
134
135
inline Vector128 Load128(const uint8_t* ptr) { return vld1q_u8(ptr); }
136
137
inline Vector128 Set128(uint64_t a, uint64_t b) {
138
  return vreinterpretq_u8_u64(vsetq_lane_u64(a, vdupq_n_u64(b), 1));
139
}
140
141
inline Vector128 Add128(Vector128 a, Vector128 b) {
142
  return vreinterpretq_u8_u64(
143
      vaddq_u64(vreinterpretq_u64_u8(a), vreinterpretq_u64_u8(b)));
144
}
145
146
// Bits of the second argument to Decrypt128/Encrypt128 are XORed with the
147
// state argument BEFORE encryption (in x86 version they are XORed after).
148
149
inline Vector128 Encrypt128(Vector128 data, Vector128 key) {
150
  return vaesmcq_u8(vaeseq_u8(data, key));
151
}
152
153
inline Vector128 Decrypt128(Vector128 data, Vector128 key) {
154
  return vaesimcq_u8(vaesdq_u8(data, key));
155
}
156
157
// We use decryption for a, b and encryption for c, d. That helps us to avoid
158
// collisions for trivial byte rotations. Mix4x16Vectors later uses
159
// encrypted/decrypted pairs differently to ensure that the order of blocks is
160
// important for the hash value.
161
// We also avoid using Add128/Sub128 instructions because state is being mixed
162
// before encryption/decryption. On ARM, there is no fusion of load and add/sub
163
// instructions so it is more expensive to use them.
164
165
inline Vector128 MixA(Vector128 a, Vector128 state) {
166
  return Decrypt128(a, state);
167
}
168
169
inline Vector128 MixB(Vector128 b, Vector128 state) {
170
  return Decrypt128(b, state);
171
}
172
173
inline Vector128 MixC(Vector128 c, Vector128 state) {
174
  return Encrypt128(c, state);
175
}
176
177
inline Vector128 MixD(Vector128 d, Vector128 state) {
178
  return Encrypt128(d, state);
179
}
180
181
inline uint64_t ExtractLow64(Vector128 v) {
182
  return vgetq_lane_u64(vreinterpretq_u64_u8(v), 0);
183
}
184
185
inline uint64_t ExtractHigh64(Vector128 v) {
186
  return vgetq_lane_u64(vreinterpretq_u64_u8(v), 1);
187
}
188
189
uint64_t Mix4x16Vectors(Vector128 a, Vector128 b, Vector128 c, Vector128 d) {
190
  Vector128 res128 = Add128(Encrypt128(a, c), Decrypt128(b, d));
191
  uint64_t x64 = ExtractLow64(res128);
192
  uint64_t y64 = ExtractHigh64(res128);
193
  return x64 ^ y64;
194
}
195
196
#endif  // ABSL_AES_INTERNAL_HAVE_X86_SIMD
197
198
uint64_t LowLevelHash33To64(uint64_t seed, const uint8_t* ptr, size_t len) {
199
  assert(len > 32);
200
  assert(len <= 64);
201
  Vector128 state = Set128(seed, len);
202
  Vector128 a = Load128(ptr);
203
  Vector128 b = Load128(ptr + 16);
204
  auto* last32_ptr = ptr + len - 32;
205
  Vector128 c = Load128(last32_ptr);
206
  Vector128 d = Load128(last32_ptr + 16);
207
208
  Vector128 na = MixA(a, state);
209
  Vector128 nb = MixB(b, state);
210
  Vector128 nc = MixC(c, state);
211
  Vector128 nd = MixD(d, state);
212
213
  // We perform another round of encryption to mix bits between two halves of
214
  // the input.
215
  return Mix4x16Vectors(na, nb, nc, nd);
216
}
217
218
[[maybe_unused]] ABSL_ATTRIBUTE_NOINLINE uint64_t
219
LowLevelHashLenGt64(uint64_t seed, const void* data, size_t len) {
220
  assert(len > 64);
221
  const uint8_t* ptr = static_cast<const uint8_t*>(data);
222
  const uint8_t* last_32_ptr = ptr + len - 32;
223
224
  // If we have more than 64 bytes, we're going to handle chunks of 64
225
  // bytes at a time. We're going to build up four separate hash states
226
  // which we will then hash together. This avoids short dependency chains.
227
  Vector128 state0 = Set128(seed, len);
228
  Vector128 state1 = state0;
229
  Vector128 state2 = state1;
230
  Vector128 state3 = state2;
231
232
  // Mixing two 128-bit vectors at a time with corresponding states.
233
  // All variables are mixed slightly differently to avoid hash collision
234
  // due to trivial byte rotation.
235
  // We combine state and data with _mm_add_epi64/_mm_sub_epi64 before applying
236
  // AES encryption to make hash function dependent on the order of the blocks.
237
  // See comments in LowLevelHash33To64 for more considerations.
238
  struct MixerAB {
239
    Vector128& state0;
240
    Vector128& state1;
241
    ABSL_ATTRIBUTE_ALWAYS_INLINE auto operator()(const uint8_t* p) const {
242
      Vector128 a = Load128(p);
243
      Vector128 b = Load128(p + 16);
244
      state0 = MixA(a, state0);
245
      state1 = MixB(b, state1);
246
    }
247
  };
248
  MixerAB mix_ab = {state0, state1};
249
  struct MixerCD {
250
    Vector128& state2;
251
    Vector128& state3;
252
    ABSL_ATTRIBUTE_ALWAYS_INLINE auto operator()(const uint8_t* p) const {
253
      Vector128 c = Load128(p);
254
      Vector128 d = Load128(p + 16);
255
      state2 = MixC(c, state2);
256
      state3 = MixD(d, state3);
257
    }
258
  };
259
  MixerCD mix_cd = {state2, state3};
260
  do {
261
    PrefetchFutureDataToLocalCache(ptr);
262
    mix_ab(ptr);
263
    mix_cd(ptr + 32);
264
265
    ptr += 64;
266
    len -= 64;
267
  } while (len > 64);
268
269
  // We now have a data `ptr` with at most 64 bytes.
270
  if (len > 32) {
271
    mix_ab(ptr);
272
  }
273
  mix_cd(last_32_ptr);
274
275
  return Mix4x16Vectors(state0, state1, state2, state3);
276
}
277
#else
278
2.35M
uint64_t Mix32Bytes(const uint8_t* ptr, uint64_t current_state) {
279
2.35M
  uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
280
2.35M
  uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
281
2.35M
  uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
282
2.35M
  uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
283
284
2.35M
  uint64_t cs0 = Mix(a ^ kStaticRandomData[1], b ^ current_state);
285
2.35M
  uint64_t cs1 = Mix(c ^ kStaticRandomData[2], d ^ current_state);
286
2.35M
  return cs0 ^ cs1;
287
2.35M
}
288
289
81.0k
uint64_t LowLevelHash33To64(uint64_t seed, const uint8_t* ptr, size_t len) {
290
81.0k
  assert(len > 32);
291
81.0k
  assert(len <= 64);
292
81.0k
  uint64_t current_state = seed ^ kStaticRandomData[0] ^ len;
293
81.0k
  const uint8_t* last_32_ptr = ptr + len - 32;
294
81.0k
  return Mix32Bytes(last_32_ptr, Mix32Bytes(ptr, current_state));
295
81.0k
}
296
297
[[maybe_unused]] ABSL_ATTRIBUTE_NOINLINE uint64_t
298
1.19M
LowLevelHashLenGt64(uint64_t seed, const void* data, size_t len) {
299
1.19M
  assert(len > 64);
300
1.19M
  const uint8_t* ptr = static_cast<const uint8_t*>(data);
301
1.19M
  uint64_t current_state = seed ^ kStaticRandomData[0] ^ len;
302
1.19M
  const uint8_t* last_32_ptr = ptr + len - 32;
303
  // If we have more than 64 bytes, we're going to handle chunks of 64
304
  // bytes at a time. We're going to build up four separate hash states
305
  // which we will then hash together. This avoids short dependency chains.
306
1.19M
  uint64_t duplicated_state0 = current_state;
307
1.19M
  uint64_t duplicated_state1 = current_state;
308
1.19M
  uint64_t duplicated_state2 = current_state;
309
310
13.6M
  do {
311
13.6M
    PrefetchFutureDataToLocalCache(ptr);
312
313
13.6M
    uint64_t a = absl::base_internal::UnalignedLoad64(ptr);
314
13.6M
    uint64_t b = absl::base_internal::UnalignedLoad64(ptr + 8);
315
13.6M
    uint64_t c = absl::base_internal::UnalignedLoad64(ptr + 16);
316
13.6M
    uint64_t d = absl::base_internal::UnalignedLoad64(ptr + 24);
317
13.6M
    uint64_t e = absl::base_internal::UnalignedLoad64(ptr + 32);
318
13.6M
    uint64_t f = absl::base_internal::UnalignedLoad64(ptr + 40);
319
13.6M
    uint64_t g = absl::base_internal::UnalignedLoad64(ptr + 48);
320
13.6M
    uint64_t h = absl::base_internal::UnalignedLoad64(ptr + 56);
321
322
13.6M
    current_state = Mix(a ^ kStaticRandomData[1], b ^ current_state);
323
13.6M
    duplicated_state0 = Mix(c ^ kStaticRandomData[2], d ^ duplicated_state0);
324
325
13.6M
    duplicated_state1 = Mix(e ^ kStaticRandomData[3], f ^ duplicated_state1);
326
13.6M
    duplicated_state2 = Mix(g ^ kStaticRandomData[4], h ^ duplicated_state2);
327
328
13.6M
    ptr += 64;
329
13.6M
    len -= 64;
330
13.6M
  } while (len > 64);
331
332
1.19M
  current_state = (current_state ^ duplicated_state0) ^
333
1.19M
                  (duplicated_state1 + duplicated_state2);
334
  // We now have a data `ptr` with at most 64 bytes and the current state
335
  // of the hashing state machine stored in current_state.
336
1.19M
  if (len > 32) {
337
993k
    current_state = Mix32Bytes(ptr, current_state);
338
993k
  }
339
340
  // We now have a data `ptr` with at most 32 bytes and the current state
341
  // of the hashing state machine stored in current_state. But we can
342
  // safely read from `ptr + len - 32`.
343
1.19M
  return Mix32Bytes(last_32_ptr, current_state);
344
1.19M
}
345
#endif  // ABSL_AES_INTERNAL_HAVE_X86_SIMD
346
347
[[maybe_unused]] uint64_t LowLevelHashLenGt32(uint64_t seed, const void* data,
348
1.27M
                                              size_t len) {
349
1.27M
  assert(len > 32);
350
1.27M
  if (ABSL_PREDICT_FALSE(len > 64)) {
351
1.19M
    return LowLevelHashLenGt64(seed, data, len);
352
1.19M
  }
353
81.0k
  return LowLevelHash33To64(seed, static_cast<const uint8_t*>(data), len);
354
1.27M
}
355
356
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t HashBlockOn32Bit(
357
0
    uint64_t state, const unsigned char* data, size_t len) {
358
0
  // TODO(b/417141985): expose and use CityHash32WithSeed.
359
0
  // Note: we can't use PrecombineLengthMix here because len can be up to 1024.
360
0
  return CombineRawImpl(
361
0
      state + len,
362
0
      hash_internal::CityHash32(reinterpret_cast<const char*>(data), len));
363
0
}
364
365
ABSL_ATTRIBUTE_NOINLINE uint64_t
366
0
SplitAndCombineOn32Bit(uint64_t state, const unsigned char* first, size_t len) {
367
0
  while (len >= PiecewiseChunkSize()) {
368
0
    state = HashBlockOn32Bit(state, first, PiecewiseChunkSize());
369
0
    len -= PiecewiseChunkSize();
370
0
    first += PiecewiseChunkSize();
371
0
  }
372
0
  // Do not call CombineContiguousImpl for empty range since it is modifying
373
0
  // state.
374
0
  if (len == 0) {
375
0
    return state;
376
0
  }
377
0
  // Handle the remainder.
378
0
  return CombineContiguousImpl(state, first, len,
379
0
                               std::integral_constant<int, 4>{});
380
0
}
381
382
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t HashBlockOn64Bit(
383
1.27M
    uint64_t state, const unsigned char* data, size_t len) {
384
1.27M
#ifdef ABSL_HAVE_INTRINSIC_INT128
385
1.27M
  return LowLevelHashLenGt32(state, data, len);
386
#else
387
  return hash_internal::CityHash64WithSeed(reinterpret_cast<const char*>(data),
388
                                           len, state);
389
#endif
390
1.27M
}
391
392
ABSL_ATTRIBUTE_NOINLINE uint64_t
393
86.7k
SplitAndCombineOn64Bit(uint64_t state, const unsigned char* first, size_t len) {
394
876k
  while (len >= PiecewiseChunkSize()) {
395
789k
    state = HashBlockOn64Bit(state, first, PiecewiseChunkSize());
396
789k
    len -= PiecewiseChunkSize();
397
789k
    first += PiecewiseChunkSize();
398
789k
  }
399
  // Do not call CombineContiguousImpl for empty range since it is modifying
400
  // state.
401
86.7k
  if (len == 0) {
402
220
    return state;
403
220
  }
404
  // Handle the remainder.
405
86.5k
  return CombineContiguousImpl(state, first, len,
406
86.5k
                               std::integral_constant<int, 8>{});
407
86.7k
}
408
409
}  // namespace
410
411
uint64_t CombineLargeContiguousImplOn32BitLengthGt8(uint64_t state,
412
                                                    const unsigned char* first,
413
0
                                                    size_t len) {
414
0
  assert(len > 8);
415
0
  assert(sizeof(size_t) == 4);  // NOLINT(misc-static-assert)
416
0
  if (ABSL_PREDICT_TRUE(len <= PiecewiseChunkSize())) {
417
0
    return HashBlockOn32Bit(state, first, len);
418
0
  }
419
0
  return SplitAndCombineOn32Bit(state, first, len);
420
0
}
421
422
uint64_t CombineLargeContiguousImplOn64BitLengthGt32(uint64_t state,
423
                                                     const unsigned char* first,
424
574k
                                                     size_t len) {
425
574k
  assert(len > 32);
426
574k
  assert(sizeof(size_t) == 8);  // NOLINT(misc-static-assert)
427
574k
  if (ABSL_PREDICT_TRUE(len <= PiecewiseChunkSize())) {
428
487k
    return HashBlockOn64Bit(state, first, len);
429
487k
  }
430
86.7k
  return SplitAndCombineOn64Bit(state, first, len);
431
574k
}
432
433
ABSL_CONST_INIT const void* const MixingHashState::kSeed = &kSeed;
434
435
}  // namespace hash_internal
436
ABSL_NAMESPACE_END
437
}  // namespace absl