Coverage Report

Created: 2026-09-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/hash/internal/hash.h
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
// -----------------------------------------------------------------------------
16
// File: hash.h
17
// -----------------------------------------------------------------------------
18
//
19
#ifndef ABSL_HASH_INTERNAL_HASH_H_
20
#define ABSL_HASH_INTERNAL_HASH_H_
21
22
// We include config.h here to make sure that ABSL_INTERNAL_CPLUSPLUS_LANG is
23
// defined.
24
#include "absl/base/config.h"
25
26
// GCC15 warns that <ciso646> is deprecated in C++17 and suggests using
27
// <version> instead, even though <version> is not available in C++17 mode prior
28
// to GCC9.
29
#if __has_include(<version>)
30
#define ABSL_INTERNAL_VERSION_HEADER_AVAILABLE 1
31
#endif
32
33
// For feature testing and determining which headers can be included.
34
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || \
35
    defined(ABSL_INTERNAL_VERSION_HEADER_AVAILABLE)
36
#include <version>
37
#else
38
#include <ciso646>
39
#endif
40
41
#undef ABSL_INTERNAL_VERSION_HEADER_AVAILABLE
42
43
#include <algorithm>
44
#include <array>
45
#include <bitset>
46
#include <cassert>
47
#include <cmath>
48
#include <cstddef>
49
#include <cstdint>
50
#include <cstring>
51
#include <deque>
52
#include <forward_list>
53
#include <functional>
54
#include <iterator>
55
#include <limits>
56
#include <list>
57
#include <map>
58
#include <memory>
59
#include <optional>
60
#include <set>
61
#include <string>
62
#include <string_view>
63
#include <tuple>
64
#include <type_traits>
65
#include <unordered_map>
66
#include <unordered_set>
67
#include <utility>
68
#include <variant>
69
#include <vector>
70
71
#include "absl/base/attributes.h"
72
#include "absl/base/internal/endian.h"
73
#include "absl/base/internal/unaligned_access.h"
74
#include "absl/base/optimization.h"
75
#include "absl/base/options.h"
76
#include "absl/base/port.h"
77
#include "absl/container/fixed_array.h"
78
#include "absl/hash/internal/city.h"
79
#include "absl/hash/internal/weakly_mixed_integer.h"
80
#include "absl/meta/type_traits.h"
81
#include "absl/numeric/bits.h"
82
#include "absl/numeric/int128.h"
83
#include "absl/strings/string_view.h"
84
#include "absl/types/optional.h"
85
#include "absl/types/variant.h"
86
#include "absl/utility/utility.h"
87
88
#ifdef __APPLE__
89
#include <Availability.h>
90
#include <TargetConditionals.h>
91
#endif
92
93
#if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
94
    !defined(__XTENSA__)
95
#include <filesystem>  // NOLINT
96
#endif
97
98
// We are allowed to use a non-portable hardware-accelerated implementation in
99
// headers if ABSL_OPTION_INLINE_HW_ACCEL_STRATEGY != 0
100
#if ABSL_OPTION_INLINE_HW_ACCEL_STRATEGY != 0
101
102
// 32-bit builds with SSE 4.2 do not have _mm_crc32_u64, so the
103
// __x86_64__ condition is necessary.
104
#if defined(__SSE4_2__) && defined(__x86_64__)
105
106
#include <x86intrin.h>
107
#define ABSL_HASH_INTERNAL_HAS_CRC32
108
#define ABSL_HASH_INTERNAL_CRC32_U64 _mm_crc32_u64
109
#define ABSL_HASH_INTERNAL_CRC32_U32 _mm_crc32_u32
110
#define ABSL_HASH_INTERNAL_CRC32_U8 _mm_crc32_u8
111
112
// 32-bit builds with AVX do not have _mm_crc32_u64, so the _M_X64 condition is
113
// necessary.
114
#elif defined(_MSC_VER) && !defined(__clang__) && defined(__AVX__) && \
115
    defined(_M_X64)
116
117
// MSVC AVX (/arch:AVX) implies SSE 4.2.
118
#include <intrin.h>
119
#define ABSL_HASH_INTERNAL_HAS_CRC32
120
#define ABSL_HASH_INTERNAL_CRC32_U64 _mm_crc32_u64
121
#define ABSL_HASH_INTERNAL_CRC32_U32 _mm_crc32_u32
122
#define ABSL_HASH_INTERNAL_CRC32_U8 _mm_crc32_u8
123
124
#elif defined(__ARM_FEATURE_CRC32)
125
126
#include <arm_acle.h>
127
#define ABSL_HASH_INTERNAL_HAS_CRC32
128
// Casting to uint32_t to be consistent with x86 intrinsic (_mm_crc32_u64
129
// accepts crc as 64 bit integer).
130
#define ABSL_HASH_INTERNAL_CRC32_U64(crc, data) \
131
  __crc32cd(static_cast<uint32_t>(crc), data)
132
#define ABSL_HASH_INTERNAL_CRC32_U32 __crc32cw
133
#define ABSL_HASH_INTERNAL_CRC32_U8 __crc32cb
134
135
#endif  // Platform tests
136
137
#endif  // ABSL_OPTION_INLINE_HW_ACCEL_STRATEGY != 0
138
139
140
#if ABSL_OPTION_INLINE_HW_ACCEL_STRATEGY == 1
141
#ifndef ABSL_HASH_INTERNAL_HAS_CRC32
142
#error "Hardware acceleration is required by ABSL_OPTION_INLINE_HW_ACCEL_STRATEGY but not supported on this platform; see absl/base/options.h"
143
#endif
144
#endif
145
146
namespace absl {
147
ABSL_NAMESPACE_BEGIN
148
149
class HashState;
150
151
namespace hash_internal {
152
153
// Internal detail: Large buffers are hashed in smaller chunks.  This function
154
// returns the size of these chunks.
155
3.75M
constexpr size_t PiecewiseChunkSize() { return 1024; }
156
157
// PiecewiseCombiner is an internal-only helper class for hashing a piecewise
158
// buffer of `char` or `unsigned char` as though it were contiguous.  This class
159
// provides two methods:
160
//
161
//   H add_buffer(state, data, size)
162
//   H finalize(state)
163
//
164
// `add_buffer` can be called zero or more times, followed by a single call to
165
// `finalize`.  This will produce the same hash expansion as concatenating each
166
// buffer piece into a single contiguous buffer, and passing this to
167
// `H::combine_contiguous`.
168
//
169
//  Example usage:
170
//    PiecewiseCombiner combiner;
171
//    for (const auto& piece : pieces) {
172
//      state = combiner.add_buffer(std::move(state), piece.data, piece.size);
173
//    }
174
//    return combiner.finalize(std::move(state));
175
class PiecewiseCombiner {
176
 public:
177
  PiecewiseCombiner() = default;
178
  PiecewiseCombiner(const PiecewiseCombiner&) = delete;
179
  PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
180
181
  // Appends the given range of bytes to the sequence to be hashed, which may
182
  // modify the provided hash state.
183
  template <typename H>
184
  H add_buffer(H state, const unsigned char* data, size_t size);
185
  template <typename H>
186
0
  H add_buffer(H state, const char* data, size_t size) {
187
0
    return add_buffer(std::move(state),
188
0
                      reinterpret_cast<const unsigned char*>(data), size);
189
0
  }
190
191
  // Finishes combining the hash sequence, which may may modify the provided
192
  // hash state.
193
  //
194
  // Once finalize() is called, add_buffer() may no longer be called. The
195
  // resulting hash state will be the same as if the pieces passed to
196
  // add_buffer() were concatenated into a single flat buffer, and then provided
197
  // to H::combine_contiguous().
198
  template <typename H>
199
  H finalize(H state);
200
201
 private:
202
  unsigned char buf_[PiecewiseChunkSize()];
203
  size_t position_ = 0;
204
  bool added_something_ = false;
205
};
206
207
// Trait class which returns true if T is hashable by the absl::Hash framework.
208
// Used for the AbslHashValue implementations for composite types below.
209
template <typename T>
210
struct is_hashable;
211
212
// HashStateBase is an internal implementation detail that contains common
213
// implementation details for all of the "hash state objects" objects generated
214
// by Abseil.  This is not a public API; users should not create classes that
215
// inherit from this.
216
//
217
// A hash state object is the template argument `H` passed to `AbslHashValue`.
218
// It represents an intermediate state in the computation of an unspecified hash
219
// algorithm. `HashStateBase` provides a CRTP style base class for hash state
220
// implementations. Developers adding type support for `absl::Hash` should not
221
// rely on any parts of the state object other than the following member
222
// functions:
223
//
224
//   * HashStateBase::combine()
225
//   * HashStateBase::combine_contiguous()
226
//   * HashStateBase::combine_unordered()
227
//
228
// A derived hash state class of type `H` must provide a public member function
229
// with a signature similar to the following:
230
//
231
//    `static H combine_contiguous(H state, const unsigned char*, size_t)`.
232
//
233
// It must also provide a private template method named RunCombineUnordered.
234
//
235
// A "consumer" is a 1-arg functor returning void.  Its argument is a reference
236
// to an inner hash state object, and it may be called multiple times.  When
237
// called, the functor consumes the entropy from the provided state object,
238
// and resets that object to its empty state.
239
//
240
// A "combiner" is a stateless 2-arg functor returning void.  Its arguments are
241
// an inner hash state object and an ElementStateConsumer functor.  A combiner
242
// uses the provided inner hash state object to hash each element of the
243
// container, passing the inner hash state object to the consumer after hashing
244
// each element.
245
//
246
// Given these definitions, a derived hash state class of type H
247
// must provide a private template method with a signature similar to the
248
// following:
249
//
250
//    `template <typename CombinerT>`
251
//    `static H RunCombineUnordered(H outer_state, CombinerT combiner)`
252
//
253
// This function is responsible for constructing the inner state object and
254
// providing a consumer to the combiner.  It uses side effects of the consumer
255
// and combiner to mix the state of each element in an order-independent manner,
256
// and uses this to return an updated value of `outer_state`.
257
//
258
// This inside-out approach generates efficient object code in the normal case,
259
// but allows us to use stack storage to implement the absl::HashState type
260
// erasure mechanism (avoiding heap allocations while hashing).
261
//
262
// `HashStateBase` will provide a complete implementation for a hash state
263
// object in terms of these two methods.
264
//
265
// Example:
266
//
267
//   // Use CRTP to define your derived class.
268
//   struct MyHashState : HashStateBase<MyHashState> {
269
//       static H combine_contiguous(H state, const unsigned char*, size_t);
270
//       using MyHashState::HashStateBase::combine;
271
//       using MyHashState::HashStateBase::combine_contiguous;
272
//       using MyHashState::HashStateBase::combine_unordered;
273
//     private:
274
//       template <typename CombinerT>
275
//       static H RunCombineUnordered(H state, CombinerT combiner);
276
//   };
277
template <typename H>
278
class HashStateBase {
279
 public:
280
  // Combines an arbitrary number of values into a hash state, returning the
281
  // updated state.
282
  //
283
  // Each of the value types `T` must be separately hashable by the Abseil
284
  // hashing framework.
285
  //
286
  // NOTE:
287
  //
288
  //   state = H::combine(std::move(state), value1, value2, value3);
289
  //
290
  // is guaranteed to produce the same hash expansion as:
291
  //
292
  //   state = H::combine(std::move(state), value1);
293
  //   state = H::combine(std::move(state), value2);
294
  //   state = H::combine(std::move(state), value3);
295
  template <typename T, typename... Ts>
296
  static H combine(H state, const T& value, const Ts&... values);
297
0
  static H combine(H state) { return state; }
298
299
  // Combines a contiguous array of `size` elements into a hash state, returning
300
  // the updated state.
301
  //
302
  // NOTE:
303
  //
304
  //   state = H::combine_contiguous(std::move(state), data, size);
305
  //
306
  // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
307
  // perform internal optimizations).  If you need this guarantee, use the
308
  // for-loop instead.
309
  template <typename T>
310
  static H combine_contiguous(H state, const T* data, size_t size);
311
312
  template <typename I>
313
  static H combine_unordered(H state, I begin, I end);
314
315
  using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
316
317
  template <typename T>
318
  using is_hashable = absl::hash_internal::is_hashable<T>;
319
320
 private:
321
  // Common implementation of the iteration step of a "combiner", as described
322
  // above.
323
  template <typename I>
324
  struct CombineUnorderedCallback {
325
    I begin;
326
    I end;
327
328
    template <typename InnerH, typename ElementStateConsumer>
329
    void operator()(InnerH inner_state, ElementStateConsumer cb) {
330
      for (; begin != end; ++begin) {
331
        inner_state = H::combine(std::move(inner_state), *begin);
332
        cb(inner_state);
333
      }
334
    }
335
  };
336
};
337
338
// `is_uniquely_represented<T>` is a trait class that indicates whether `T`
339
// is uniquely represented.
340
//
341
// A type is "uniquely represented" if two equal values of that type are
342
// guaranteed to have the same bytes in their underlying storage. In other
343
// words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
344
// zero. This property cannot be detected automatically, so this trait is false
345
// by default, but can be specialized by types that wish to assert that they are
346
// uniquely represented. This makes them eligible for certain optimizations.
347
//
348
// If you have any doubt whatsoever, do not specialize this template.
349
// The default is completely safe, and merely disables some optimizations
350
// that will not matter for most types. Specializing this template,
351
// on the other hand, can be very hazardous.
352
//
353
// To be uniquely represented, a type must not have multiple ways of
354
// representing the same value; for example, float and double are not
355
// uniquely represented, because they have distinct representations for
356
// +0 and -0. Furthermore, the type's byte representation must consist
357
// solely of user-controlled data, with no padding bits and no compiler-
358
// controlled data such as vptrs or sanitizer metadata. This is usually
359
// very difficult to guarantee, because in most cases the compiler can
360
// insert data and padding bits at its own discretion.
361
//
362
// If you specialize this template for a type `T`, you must do so in the file
363
// that defines that type (or in this file). If you define that specialization
364
// anywhere else, `is_uniquely_represented<T>` could have different meanings
365
// in different places.
366
//
367
// The Enable parameter is meaningless; it is provided as a convenience,
368
// to support certain SFINAE techniques when defining specializations.
369
template <typename T, typename Enable = void>
370
struct is_uniquely_represented : std::false_type {};
371
372
// unsigned char is a synonym for "byte", so it is guaranteed to be
373
// uniquely represented.
374
template <>
375
struct is_uniquely_represented<unsigned char> : std::true_type {};
376
377
// is_uniquely_represented for non-standard integral types
378
//
379
// Integral types other than bool should be uniquely represented on any
380
// platform that this will plausibly be ported to.
381
template <typename Integral>
382
struct is_uniquely_represented<Integral,
383
                               std::enable_if_t<std::is_integral_v<Integral>>>
384
    : std::true_type {};
385
386
template <>
387
struct is_uniquely_represented<bool> : std::false_type {};
388
389
#ifdef ABSL_HAVE_INTRINSIC_INT128
390
// Specialize the trait for GNU extension types.
391
template <>
392
struct is_uniquely_represented<__int128> : std::true_type {};
393
template <>
394
struct is_uniquely_represented<unsigned __int128> : std::true_type {};
395
#endif  // ABSL_HAVE_INTRINSIC_INT128
396
397
template <typename T>
398
struct FitsIn64Bits : std::bool_constant<sizeof(T) <= 8> {};
399
400
struct CombineRaw {
401
  template <typename H>
402
0
  H operator()(H state, uint64_t value) const {
403
0
    return H::combine_raw(std::move(state), value);
404
0
  }
405
};
406
407
// For use in `raw_hash_set` to pass a seed to the hash function.
408
struct HashWithSeed {
409
  template <typename Hasher, typename T>
410
0
  size_t hash(const Hasher& hasher, const T& value, size_t seed) const {
411
0
    // NOLINTNEXTLINE(clang-diagnostic-sign-conversion)
412
0
    return hasher.hash_with_seed(value, seed);
413
0
  }
Unexecuted instantiation: unsigned long absl::hash_internal::HashWithSeed::hash<absl::hash_internal::Hash<std::__1::basic_string_view<char, std::__1::char_traits<char> > >, std::__1::basic_string_view<char, std::__1::char_traits<char> > >(absl::hash_internal::Hash<std::__1::basic_string_view<char, std::__1::char_traits<char> > > const&, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, unsigned long) const
Unexecuted instantiation: unsigned long absl::hash_internal::HashWithSeed::hash<absl::hash_internal::Hash<absl::Cord>, absl::Cord>(absl::hash_internal::Hash<absl::Cord> const&, absl::Cord const&, unsigned long) const
414
};
415
416
// Convenience function that combines `hash_state` with the byte representation
417
// of `value`.
418
template <typename H, typename T,
419
          std::enable_if_t<FitsIn64Bits<T>::value, int> = 0>
420
0
H hash_bytes(H hash_state, const T& value) {
421
0
  const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
422
0
  uint64_t v;
423
  if constexpr (sizeof(T) == 1) {
424
    v = *start;
425
  } else if constexpr (sizeof(T) == 2) {
426
    v = absl::base_internal::UnalignedLoad16(start);
427
0
  } else if constexpr (sizeof(T) == 4) {
428
0
    v = absl::base_internal::UnalignedLoad32(start);
429
0
  } else {
430
0
    static_assert(sizeof(T) == 8);
431
0
    v = absl::base_internal::UnalignedLoad64(start);
432
0
  }
433
0
  return CombineRaw()(std::move(hash_state), v);
434
0
}
Unexecuted instantiation: _ZN4absl13hash_internal10hash_bytesINS0_15MixingHashStateEiTnNSt3__19enable_ifIXsr12FitsIn64BitsIT0_EE5valueEiE4typeELi0EEET_S8_RKS5_
Unexecuted instantiation: _ZN4absl13hash_internal10hash_bytesINS0_15MixingHashStateEmTnNSt3__19enable_ifIXsr12FitsIn64BitsIT0_EE5valueEiE4typeELi0EEET_S8_RKS5_
435
template <typename H, typename T,
436
          std::enable_if_t<!FitsIn64Bits<T>::value, int> = 0>
437
H hash_bytes(H hash_state, const T& value) {
438
  const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
439
  return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
440
}
441
442
template <typename H>
443
H hash_weakly_mixed_integer(H hash_state, WeaklyMixedInteger value) {
444
  return H::combine_weakly_mixed_integer(std::move(hash_state), value);
445
}
446
447
// -----------------------------------------------------------------------------
448
// AbslHashValue for Basic Types
449
// -----------------------------------------------------------------------------
450
451
// Note: Default `AbslHashValue` implementations live in `hash_internal`. This
452
// allows us to block lexical scope lookup when doing an unqualified call to
453
// `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
454
// only be found via ADL.
455
456
// AbslHashValue() for hashing bool values
457
//
458
// We use SFINAE to ensure that this overload only accepts bool, not types that
459
// are convertible to bool.
460
template <typename H, typename B>
461
std::enable_if_t<std::is_same_v<B, bool>, H> AbslHashValue(H hash_state,
462
                                                           B value) {
463
  // We use ~size_t{} instead of 1 so that all bits are different between
464
  // true/false instead of only 1.
465
  return H::combine(std::move(hash_state),
466
                    static_cast<size_t>(value ? ~size_t{} : 0));
467
}
468
469
// AbslHashValue() for hashing enum values
470
template <typename H, typename Enum>
471
std::enable_if_t<std::is_enum_v<Enum>, H> AbslHashValue(H hash_state, Enum e) {
472
  // In practice, we could almost certainly just invoke hash_bytes directly,
473
  // but it's possible that a sanitizer might one day want to
474
  // store data in the unused bits of an enum. To avoid that risk, we
475
  // convert to the underlying type before hashing. Hopefully this will get
476
  // optimized away; if not, we can reopen discussion with c-toolchain-team.
477
  return H::combine(std::move(hash_state),
478
                    static_cast<std::underlying_type_t<Enum>>(e));
479
}
480
// AbslHashValue() for hashing floating-point values
481
template <typename H, typename Float>
482
std::enable_if_t<std::is_same_v<Float, float> || std::is_same_v<Float, double>,
483
                 H>
484
AbslHashValue(H hash_state, Float value) {
485
  return hash_internal::hash_bytes(std::move(hash_state),
486
                                   value == 0 ? 0 : value);
487
}
488
489
// Long double has the property that it might have extra unused bytes in it.
490
// For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
491
// of it. This means we can't use hash_bytes on a long double and have to
492
// convert it to something else first.
493
template <typename H, typename LongDouble>
494
std::enable_if_t<std::is_same_v<LongDouble, long double>, H> AbslHashValue(
495
    H hash_state, LongDouble value) {
496
  const int category = std::fpclassify(value);
497
  switch (category) {
498
    case FP_INFINITE:
499
      // Add the sign bit to differentiate between +Inf and -Inf
500
      hash_state = H::combine(std::move(hash_state), std::signbit(value));
501
      break;
502
503
    case FP_NAN:
504
    case FP_ZERO:
505
    default:
506
      // Category is enough for these.
507
      break;
508
509
    case FP_NORMAL:
510
    case FP_SUBNORMAL:
511
      // We can't convert `value` directly to double because this would have
512
      // undefined behavior if the value is out of range.
513
      // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
514
      // guaranteed to be in range for `double`. The truncation is
515
      // implementation defined, but that works as long as it is deterministic.
516
      int exp;
517
      auto mantissa = static_cast<double>(std::frexp(value, &exp));
518
      hash_state = H::combine(std::move(hash_state), mantissa, exp);
519
  }
520
521
  return H::combine(std::move(hash_state), category);
522
}
523
524
// Without this overload, an array decays to a pointer and we hash that, which
525
// is not likely to be what the caller intended.
526
template <typename H, typename T, size_t N>
527
H AbslHashValue(H hash_state, T (&)[N]) {
528
  static_assert(
529
      sizeof(T) == -1,
530
      "Hashing C arrays is not allowed. For string literals, wrap the literal "
531
      "in absl::string_view(). To hash the array contents, use "
532
      "absl::MakeSpan() or make the array an std::array. To hash the array "
533
      "address, use &array[0].");
534
  return hash_state;
535
}
536
537
// AbslHashValue() for hashing pointers
538
template <typename H, typename T>
539
std::enable_if_t<std::is_pointer_v<T>, H> AbslHashValue(H hash_state, T ptr) {
540
  auto v = reinterpret_cast<uintptr_t>(ptr);
541
  // Due to alignment, pointers tend to have low bits as zero, and the next few
542
  // bits follow a pattern since they are also multiples of some base value.
543
  // The PointerAlignment test verifies that our mixing is good enough to handle
544
  // these cases.
545
  return H::combine(std::move(hash_state), v);
546
}
547
548
// AbslHashValue() for hashing nullptr_t
549
template <typename H>
550
H AbslHashValue(H hash_state, std::nullptr_t) {
551
  return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
552
}
553
554
// AbslHashValue() for hashing pointers-to-member
555
template <typename H, typename T, typename C>
556
H AbslHashValue(H hash_state, T C::*ptr) {
557
  auto salient_ptm_size = [](std::size_t n) -> std::size_t {
558
#if defined(_MSC_VER)
559
    // Pointers-to-member-function on MSVC consist of one pointer plus 0, 1, 2,
560
    // or 3 ints. In 64-bit mode, they are 8-byte aligned and thus can contain
561
    // padding (namely when they have 1 or 3 ints). The value below is a lower
562
    // bound on the number of salient, non-padding bytes that we use for
563
    // hashing.
564
    if constexpr (alignof(T C::*) == alignof(int)) {
565
      // No padding when all subobjects have the same size as the total
566
      // alignment. This happens in 32-bit mode.
567
      return n;
568
    } else {
569
      // Padding for 1 int (size 16) or 3 ints (size 24).
570
      // With 2 ints, the size is 16 with no padding, which we pessimize.
571
      return n == 24 ? 20 : n == 16 ? 12 : n;
572
    }
573
#else
574
  // On other platforms, we assume that pointers-to-members do not have
575
  // padding.
576
    static_assert(std::has_unique_object_representations_v<T C::*>);
577
    return n;
578
#endif
579
  };
580
  return H::combine_contiguous(std::move(hash_state),
581
                               reinterpret_cast<unsigned char*>(&ptr),
582
                               salient_ptm_size(sizeof ptr));
583
}
584
585
// -----------------------------------------------------------------------------
586
// AbslHashValue for Composite Types
587
// -----------------------------------------------------------------------------
588
589
// AbslHashValue() for hashing pairs
590
template <typename H, typename T1, typename T2>
591
std::enable_if_t<is_hashable<T1>::value && is_hashable<T2>::value, H>
592
AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
593
  return H::combine(std::move(hash_state), p.first, p.second);
594
}
595
596
// Helper function for hashing a tuple. The third argument should
597
// be an index_sequence running from 0 to tuple_size<Tuple> - 1.
598
template <typename H, typename Tuple, size_t... Is>
599
0
H hash_tuple(H hash_state, const Tuple& t, std::index_sequence<Is...>) {
600
0
  return H::combine(std::move(hash_state), std::get<Is>(t)...);
601
0
}
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::hash_tuple<absl::hash_internal::MixingHashState, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&>, 0ul, 1ul>(absl::hash_internal::MixingHashState, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&> const&, std::__1::integer_sequence<unsigned long, 0ul, 1ul>)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::hash_tuple<absl::hash_internal::MixingHashState, std::__1::tuple<unsigned long const&>, 0ul>(absl::hash_internal::MixingHashState, std::__1::tuple<unsigned long const&> const&, std::__1::integer_sequence<unsigned long, 0ul>)
602
603
// AbslHashValue for hashing tuples
604
template <typename H, typename... Ts>
605
#if defined(_MSC_VER)
606
// This SFINAE gets MSVC confused under some conditions. Let's just disable it
607
// for now.
608
H
609
#else   // _MSC_VER
610
std::enable_if_t<std::conjunction_v<is_hashable<Ts>...>, H>
611
#endif  // _MSC_VER
612
0
AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
613
0
  return hash_internal::hash_tuple(std::move(hash_state), t,
614
0
                                   std::make_index_sequence<sizeof...(Ts)>());
615
0
}
Unexecuted instantiation: _ZN4absl13hash_internal13AbslHashValueINS0_15MixingHashStateEJRKNSt3__117basic_string_viewIcNS3_11char_traitsIcEEEERKiEEENS3_9enable_ifIXsr3stdE13conjunction_vIDpNS0_11is_hashableIT0_EEEET_E4typeESH_RKNS3_5tupleIJDpSE_EEE
Unexecuted instantiation: _ZN4absl13hash_internal13AbslHashValueINS0_15MixingHashStateEJRKmEEENSt3__19enable_ifIXsr3stdE13conjunction_vIDpNS0_11is_hashableIT0_EEEET_E4typeESB_RKNS5_5tupleIJDpS8_EEE
616
617
// -----------------------------------------------------------------------------
618
// AbslHashValue for Pointers
619
// -----------------------------------------------------------------------------
620
621
// AbslHashValue for hashing unique_ptr
622
template <typename H, typename T, typename D>
623
H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
624
  return H::combine(std::move(hash_state), ptr.get());
625
}
626
627
// AbslHashValue for hashing shared_ptr
628
template <typename H, typename T>
629
H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
630
  return H::combine(std::move(hash_state), ptr.get());
631
}
632
633
// -----------------------------------------------------------------------------
634
// AbslHashValue for String-Like Types
635
// -----------------------------------------------------------------------------
636
637
// AbslHashValue for hashing strings
638
//
639
// All the string-like types supported here provide the same hash expansion for
640
// the same character sequence. These types are:
641
//
642
//  - `absl::Cord`
643
//  - `std::string` (and std::basic_string<T, std::char_traits<T>, A> for
644
//      any allocator A and any T in {char, wchar_t, char8_t, char16_t,
645
//      char32_t})
646
//  - `absl::string_view`, `std::string_view`, `std::wstring_view`,
647
//    `std::u8string_view`, `std::u16string_view`, and `std::u32_string_view`.
648
//
649
// For simplicity, we currently support only strings built on `char`, `wchar_t`,
650
// `char8_t`, `char16_t`, or `char32_t`. This support may be broadened, if
651
// necessary, but with some caution - this overload would misbehave in cases
652
// where the traits' `eq()` member isn't equivalent to `==` on the underlying
653
// character type.
654
template <typename H>
655
0
H AbslHashValue(H hash_state, absl::string_view str) {
656
0
  return H::combine_contiguous(std::move(hash_state), str.data(), str.size());
657
0
}
658
659
// Support std::wstring, std::u8string, std::u16string and std::u32string.
660
template <typename Char, typename Alloc, typename H,
661
          typename = std::enable_if_t<std::is_same_v<Char, wchar_t> ||
662
#ifdef __cpp_char8_t
663
                                      std::is_same_v<Char, char8_t> ||
664
#endif
665
                                      std::is_same_v<Char, char16_t> ||
666
                                      std::is_same_v<Char, char32_t>>>
667
H AbslHashValue(
668
    H hash_state,
669
    const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
670
  return H::combine_contiguous(std::move(hash_state), str.data(), str.size());
671
}
672
673
// Support std::wstring_view, std::u8string_view, std::u16string_view and
674
// std::u32string_view.
675
template <typename Char, typename H,
676
          typename = std::enable_if_t<std::is_same_v<Char, wchar_t> ||
677
#ifdef __cpp_char8_t
678
                                      std::is_same_v<Char, char8_t> ||
679
#endif
680
                                      std::is_same_v<Char, char16_t> ||
681
                                      std::is_same_v<Char, char32_t>>>
682
H AbslHashValue(H hash_state, std::basic_string_view<Char> str) {
683
  return H::combine_contiguous(std::move(hash_state), str.data(), str.size());
684
}
685
686
#if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
687
    (!defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) ||        \
688
     __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 130000) &&       \
689
    (!defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) ||         \
690
     __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500) &&        \
691
    (!defined(__XTENSA__))
692
693
#define ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE 1
694
695
// Support std::filesystem::path. The SFINAE is required because some string
696
// types are implicitly convertible to std::filesystem::path.
697
template <typename Path, typename H,
698
          typename = std::enable_if_t<
699
              std::is_same_v<Path, std::filesystem::path>>>
700
H AbslHashValue(H hash_state, const Path& path) {
701
  // Avoid deferring to std::filesystem::hash_value, as that makes it easy to
702
  // generate offline collisions, bypassing per-table and per-process hash
703
  // seeding. Instead, we hash it ourselves.
704
  size_t count = 0;
705
706
  for (const Path& component : path) {
707
    std::basic_string_view<typename Path::value_type> part = component.native();
708
709
    // If this is a directory separator, pretend it is the preferred directory
710
    // separator (rather than the alternate separator) to ensure that equal
711
    // paths produce equal hashes.
712
    // Analogous to LLVM commit aa427b1aae445ed46d9f60c5e2eaac61bdf76be3.
713
    if (!part.empty() &&
714
        (*part.begin() == '/' || *part.begin() == Path::preferred_separator)) {
715
      part = std::basic_string_view<typename Path::value_type>(
716
          &Path::preferred_separator, 1);
717
    }
718
719
    hash_state = H::combine(std::move(hash_state), part);
720
    ++count;
721
  }
722
  return H::combine(std::move(hash_state), count);
723
}
724
725
#endif  // ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE
726
727
// -----------------------------------------------------------------------------
728
// AbslHashValue for Sequence Containers
729
// -----------------------------------------------------------------------------
730
731
// AbslHashValue for hashing std::array
732
template <typename H, typename T, size_t N>
733
std::enable_if_t<is_hashable<T>::value, H> AbslHashValue(
734
    H hash_state, const std::array<T, N>& array) {
735
  return H::combine_contiguous(std::move(hash_state), array.data(),
736
                               array.size());
737
}
738
739
// AbslHashValue for hashing std::deque
740
template <typename H, typename T, typename Allocator>
741
std::enable_if_t<is_hashable<T>::value, H> AbslHashValue(
742
    H hash_state, const std::deque<T, Allocator>& deque) {
743
  // TODO(gromer): investigate a more efficient implementation taking
744
  // advantage of the chunk structure.
745
  for (const auto& t : deque) {
746
    hash_state = H::combine(std::move(hash_state), t);
747
  }
748
  return H::combine(std::move(hash_state), WeaklyMixedInteger{deque.size()});
749
}
750
751
// AbslHashValue for hashing std::forward_list
752
template <typename H, typename T, typename Allocator>
753
std::enable_if_t<is_hashable<T>::value, H> AbslHashValue(
754
    H hash_state, const std::forward_list<T, Allocator>& list) {
755
  size_t size = 0;
756
  for (const T& t : list) {
757
    hash_state = H::combine(std::move(hash_state), t);
758
    ++size;
759
  }
760
  return H::combine(std::move(hash_state), WeaklyMixedInteger{size});
761
}
762
763
// AbslHashValue for hashing std::list
764
template <typename H, typename T, typename Allocator>
765
std::enable_if_t<is_hashable<T>::value, H> AbslHashValue(
766
    H hash_state, const std::list<T, Allocator>& list) {
767
  for (const auto& t : list) {
768
    hash_state = H::combine(std::move(hash_state), t);
769
  }
770
  return H::combine(std::move(hash_state), WeaklyMixedInteger{list.size()});
771
}
772
773
// AbslHashValue for hashing std::vector
774
//
775
// Do not use this for vector<bool> on platforms that have a working
776
// implementation of std::hash. It does not have a .data(), and a fallback for
777
// std::hash<> is most likely faster.
778
template <typename H, typename T, typename Allocator>
779
std::enable_if_t<is_hashable<T>::value && !std::is_same_v<T, bool>, H>
780
AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
781
  return H::combine_contiguous(std::move(hash_state), vector.data(),
782
                               vector.size());
783
}
784
785
// AbslHashValue special cases for hashing std::vector<bool>
786
//
787
// To achieve high performance without depending on private standard library
788
// internals, we pack bits 64 at a time into uint64_t words using a fixed
789
// 64-step inner loop that allows compilers to unroll bit shifts cleanly.
790
//
791
// This is slower than std::hash<std::vector<bool>> which can access private
792
// storage directly, but more than fast enough for the very rare case of hashing
793
// std::vector<bool>. In the event that higher performance is needed, a custom
794
// key type is likely faster than building std::vector<bool> and hashing it,
795
// otherwise users can just use std::hash as the hasher.
796
template <typename H, typename T, typename Allocator>
797
std::enable_if_t<is_hashable<T>::value && std::is_same_v<T, bool>, H>
798
AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
799
  typename H::AbslInternalPiecewiseCombiner combiner;
800
  const size_t size = vector.size();
801
  size_t i = 0;
802
  // Pack full 64-bit words. Fixed inner loop count enables compiler unrolling.
803
  while (i + 64 <= size) {
804
    uint64_t word = 0;
805
    for (size_t j = 0; j < 64; ++j) {
806
      word |= static_cast<uint64_t>(vector[i + j]) << j;
807
    }
808
    if constexpr (absl::endian::native == absl::endian::big) {
809
      word = absl::byteswap(word);
810
    }
811
    hash_state = combiner.add_buffer(
812
        std::move(hash_state), reinterpret_cast<const unsigned char*>(&word),
813
        sizeof(word));
814
    i += 64;
815
  }
816
  // Pack remaining bits (< 64) into the final word.
817
  if (i < size) {
818
    uint64_t word = 0;
819
    const size_t rem = size - i;
820
    for (size_t j = 0; j < rem; ++j) {
821
      word |= static_cast<uint64_t>(vector[i + j]) << j;
822
    }
823
    if constexpr (absl::endian::native == absl::endian::big) {
824
      word = absl::byteswap(word);
825
    }
826
    hash_state = combiner.add_buffer(
827
        std::move(hash_state), reinterpret_cast<const unsigned char*>(&word),
828
        (rem + 7) / 8);
829
  }
830
  // Mix in vector.size() to distinguish vectors with trailing false/zero bits
831
  // (e.g. {true} vs {true, false}) that would otherwise produce identical bit
832
  // buffers.
833
  return H::combine(combiner.finalize(std::move(hash_state)),
834
                    WeaklyMixedInteger{size});
835
}
836
837
// -----------------------------------------------------------------------------
838
// AbslHashValue for Ordered Associative Containers
839
// -----------------------------------------------------------------------------
840
841
// AbslHashValue for hashing std::map
842
template <typename H, typename Key, typename T, typename Compare,
843
          typename Allocator>
844
std::enable_if_t<is_hashable<Key>::value && is_hashable<T>::value, H>
845
AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
846
  for (const auto& t : map) {
847
    hash_state = H::combine(std::move(hash_state), t);
848
  }
849
  return H::combine(std::move(hash_state), WeaklyMixedInteger{map.size()});
850
}
851
852
// AbslHashValue for hashing std::multimap
853
template <typename H, typename Key, typename T, typename Compare,
854
          typename Allocator>
855
std::enable_if_t<is_hashable<Key>::value && is_hashable<T>::value, H>
856
AbslHashValue(H hash_state,
857
              const std::multimap<Key, T, Compare, Allocator>& map) {
858
  for (const auto& t : map) {
859
    hash_state = H::combine(std::move(hash_state), t);
860
  }
861
  return H::combine(std::move(hash_state), WeaklyMixedInteger{map.size()});
862
}
863
864
// AbslHashValue for hashing std::set
865
template <typename H, typename Key, typename Compare, typename Allocator>
866
std::enable_if_t<is_hashable<Key>::value, H> AbslHashValue(
867
    H hash_state, const std::set<Key, Compare, Allocator>& set) {
868
  for (const auto& t : set) {
869
    hash_state = H::combine(std::move(hash_state), t);
870
  }
871
  return H::combine(std::move(hash_state), WeaklyMixedInteger{set.size()});
872
}
873
874
// AbslHashValue for hashing std::multiset
875
template <typename H, typename Key, typename Compare, typename Allocator>
876
std::enable_if_t<is_hashable<Key>::value, H> AbslHashValue(
877
    H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
878
  for (const auto& t : set) {
879
    hash_state = H::combine(std::move(hash_state), t);
880
  }
881
  return H::combine(std::move(hash_state), WeaklyMixedInteger{set.size()});
882
}
883
884
// -----------------------------------------------------------------------------
885
// AbslHashValue for Unordered Associative Containers
886
// -----------------------------------------------------------------------------
887
888
// AbslHashValue for hashing std::unordered_set
889
template <typename H, typename Key, typename Hash, typename KeyEqual,
890
          typename Alloc>
891
std::enable_if_t<is_hashable<Key>::value, H> AbslHashValue(
892
    H hash_state, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s) {
893
  return H::combine(
894
      H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
895
      WeaklyMixedInteger{s.size()});
896
}
897
898
// AbslHashValue for hashing std::unordered_multiset
899
template <typename H, typename Key, typename Hash, typename KeyEqual,
900
          typename Alloc>
901
std::enable_if_t<is_hashable<Key>::value, H> AbslHashValue(
902
    H hash_state,
903
    const std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& s) {
904
  return H::combine(
905
      H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
906
      WeaklyMixedInteger{s.size()});
907
}
908
909
// AbslHashValue for hashing std::unordered_set
910
template <typename H, typename Key, typename T, typename Hash,
911
          typename KeyEqual, typename Alloc>
912
std::enable_if_t<is_hashable<Key>::value && is_hashable<T>::value, H>
913
AbslHashValue(H hash_state,
914
              const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& s) {
915
  return H::combine(
916
      H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
917
      WeaklyMixedInteger{s.size()});
918
}
919
920
// AbslHashValue for hashing std::unordered_multiset
921
template <typename H, typename Key, typename T, typename Hash,
922
          typename KeyEqual, typename Alloc>
923
std::enable_if_t<is_hashable<Key>::value && is_hashable<T>::value, H>
924
AbslHashValue(H hash_state,
925
              const std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& s) {
926
  return H::combine(
927
      H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
928
      WeaklyMixedInteger{s.size()});
929
}
930
931
// -----------------------------------------------------------------------------
932
// AbslHashValue for Wrapper Types
933
// -----------------------------------------------------------------------------
934
935
// AbslHashValue for hashing std::reference_wrapper
936
template <typename H, typename T>
937
std::enable_if_t<is_hashable<T>::value, H> AbslHashValue(
938
    H hash_state, std::reference_wrapper<T> opt) {
939
  return H::combine(std::move(hash_state), opt.get());
940
}
941
942
// AbslHashValue for hashing std::optional
943
template <typename H, typename T>
944
std::enable_if_t<is_hashable<T>::value, H> AbslHashValue(
945
    H hash_state, const std::optional<T>& opt) {
946
  if (opt) hash_state = H::combine(std::move(hash_state), *opt);
947
  return H::combine(std::move(hash_state), opt.has_value());
948
}
949
950
template <typename H>
951
struct VariantVisitor {
952
  H&& hash_state;
953
  template <typename T>
954
  H operator()(const T& t) const {
955
    return H::combine(std::move(hash_state), t);
956
  }
957
};
958
959
// AbslHashValue for hashing std::variant
960
template <typename H, typename... T>
961
std::enable_if_t<std::conjunction_v<is_hashable<T>...>, H> AbslHashValue(
962
    H hash_state, const std::variant<T...>& v) {
963
  if (!v.valueless_by_exception()) {
964
    hash_state = std::visit(VariantVisitor<H>{std::move(hash_state)}, v);
965
  }
966
  return H::combine(std::move(hash_state), v.index());
967
}
968
969
// -----------------------------------------------------------------------------
970
// AbslHashValue for Other Types
971
// -----------------------------------------------------------------------------
972
973
// AbslHashValue for hashing std::bitset
974
//
975
// To achieve high performance without depending on private standard library
976
// internals, we pack bits 64 at a time into uint64_t words using a fixed
977
// 64-step inner loop that allows compilers to unroll bit shifts cleanly.
978
//
979
// This is slower than std::hash<std::bitset> which can access private storage
980
// directly, but more than fast enough for the very rare case of hashing
981
// std::bitset. In the event that higher-performance is needed, users can just
982
// use std::hash as the hasher.
983
template <typename H, size_t N>
984
H AbslHashValue(H hash_state, const std::bitset<N>& set) {
985
  typename H::AbslInternalPiecewiseCombiner combiner;
986
  size_t i = 0;
987
  // Pack full 64-bit words. Fixed inner loop count enables compiler unrolling.
988
  while (i + 64 <= N) {
989
    uint64_t word = 0;
990
    for (size_t j = 0; j < 64; ++j) {
991
      word |= static_cast<uint64_t>(set[i + j]) << j;
992
    }
993
    if constexpr (absl::endian::native == absl::endian::big) {
994
      word = absl::byteswap(word);
995
    }
996
    hash_state = combiner.add_buffer(
997
        std::move(hash_state), reinterpret_cast<const unsigned char*>(&word),
998
        sizeof(word));
999
    i += 64;
1000
  }
1001
  // Pack remaining bits (< 64) into the final word.
1002
  if (i < N) {
1003
    uint64_t word = 0;
1004
    const size_t rem = N - i;
1005
    for (size_t j = 0; j < rem; ++j) {
1006
      word |= static_cast<uint64_t>(set[i + j]) << j;
1007
    }
1008
    if constexpr (absl::endian::native == absl::endian::big) {
1009
      word = absl::byteswap(word);
1010
    }
1011
    hash_state = combiner.add_buffer(
1012
        std::move(hash_state), reinterpret_cast<const unsigned char*>(&word),
1013
        (rem + 7) / 8);
1014
  }
1015
  return H::combine(combiner.finalize(std::move(hash_state)), N);
1016
}
1017
1018
// -----------------------------------------------------------------------------
1019
1020
// Mixes all values in the range [data, data+size) into the hash state.
1021
// This overload accepts only uniquely-represented types, and hashes them by
1022
// hashing the entire range of bytes.
1023
template <typename H, typename T>
1024
std::enable_if_t<is_uniquely_represented<T>::value, H> hash_range_or_bytes(
1025
0
    H hash_state, const T* data, size_t size) {
1026
0
  const auto* bytes = reinterpret_cast<const unsigned char*>(data);
1027
0
  return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
1028
0
}
1029
1030
template <typename H, typename T>
1031
std::enable_if_t<!is_uniquely_represented<T>::value, H> hash_range_or_bytes(
1032
    H hash_state, const T* data, size_t size) {
1033
  for (const auto end = data + size; data < end; ++data) {
1034
    hash_state = H::combine(std::move(hash_state), *data);
1035
  }
1036
  return H::combine(std::move(hash_state),
1037
                    hash_internal::WeaklyMixedInteger{size});
1038
}
1039
1040
inline constexpr uint64_t kMul = uint64_t{0x79d5f9e0de1e8cf5};
1041
1042
// Random data taken from the hexadecimal digits of Pi's fractional component.
1043
// https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number
1044
ABSL_CACHELINE_ALIGNED inline constexpr uint64_t kStaticRandomData[] = {
1045
    0x243f'6a88'85a3'08d3, 0x1319'8a2e'0370'7344, 0xa409'3822'299f'31d0,
1046
    0x082e'fa98'ec4e'6c89, 0x4528'21e6'38d0'1377,
1047
};
1048
1049
// Extremely weak mixture of length that is mixed into the state before
1050
// combining the data. It is used only for small strings. This also ensures that
1051
// we have high entropy in all bits of the state.
1052
8.14k
inline uint64_t PrecombineLengthMix(uint64_t state, size_t len) {
1053
8.14k
  ABSL_ASSUME(len + sizeof(uint64_t) <= sizeof(kStaticRandomData));
1054
8.14k
  uint64_t data = absl::base_internal::UnalignedLoad64(
1055
8.14k
      reinterpret_cast<const unsigned char*>(&kStaticRandomData[0]) + len);
1056
8.14k
  return state ^ data;
1057
8.14k
}
1058
1059
57.9M
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t Mix(uint64_t lhs, uint64_t rhs) {
1060
  // Though the 128-bit product needs multiple instructions on non-x86-64
1061
  // platforms, it is still a good balance between speed and hash quality.
1062
57.9M
  absl::uint128 m = lhs;
1063
57.9M
  m *= rhs;
1064
57.9M
  return Uint128High64(m) ^ Uint128Low64(m);
1065
57.9M
}
1066
1067
// Suppress erroneous array bounds errors on GCC.
1068
#if defined(__GNUC__) && !defined(__clang__)
1069
#pragma GCC diagnostic push
1070
#pragma GCC diagnostic ignored "-Warray-bounds"
1071
#endif
1072
0
inline uint32_t Read4(const unsigned char* p) {
1073
0
  return absl::base_internal::UnalignedLoad32(p);
1074
0
}
1075
28.5k
inline uint64_t Read8(const unsigned char* p) {
1076
28.5k
  return absl::base_internal::UnalignedLoad64(p);
1077
28.5k
}
1078
#if defined(__GNUC__) && !defined(__clang__)
1079
#pragma GCC diagnostic pop
1080
#endif
1081
1082
// Reads 9 to 16 bytes from p.
1083
// The first 8 bytes are in .first, and the rest of the bytes are in .second
1084
// along with duplicated bytes from .first if len<16.
1085
inline std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
1086
617
                                               size_t len) {
1087
617
  return {Read8(p), Read8(p + len - 8)};
1088
617
}
1089
1090
// Reads 4 to 8 bytes from p.
1091
// Bytes are permuted and some input bytes may be duplicated in output.
1092
599
inline uint64_t Read4To8(const unsigned char* p, size_t len) {
1093
  // If `len < 8`, we duplicate bytes. We always put low memory at the end.
1094
  // E.g., on little endian platforms:
1095
  // `ABCD` will be read as `ABCDABCD`.
1096
  // `ABCDE` will be read as `BCDEABCD`.
1097
  // `ABCDEF` will be read as `CDEFABCD`.
1098
  // `ABCDEFG` will be read as `DEFGABCD`.
1099
  // `ABCDEFGH` will be read as `EFGHABCD`.
1100
  // We also do not care about endianness. On big-endian platforms, bytes will
1101
  // be permuted differently. We always shift low memory by 32, because that
1102
  // can be pipelined earlier. Reading high memory requires computing
1103
  // `p + len - 4`.
1104
599
  uint64_t most_significant =
1105
599
      static_cast<uint64_t>(absl::base_internal::UnalignedLoad32(p)) << 32;
1106
599
  uint64_t least_significant =
1107
599
      absl::base_internal::UnalignedLoad32(p + len - 4);
1108
599
  return most_significant | least_significant;
1109
599
}
1110
1111
// Reads 1 to 3 bytes from p. Some input bytes may be duplicated in output.
1112
90
inline uint32_t Read1To3(const unsigned char* p, size_t len) {
1113
  // The trick used by this implementation is to avoid branches.
1114
  // We always read three bytes by duplicating.
1115
  // E.g.,
1116
  // `A` is read as `AAA`.
1117
  // `AB` is read as `ABB`.
1118
  // `ABC` is read as `ABC`.
1119
  // We always shift `p[0]` so that it can be pipelined better.
1120
  // Other bytes require extra computation to find indices.
1121
90
  uint32_t mem0 = (static_cast<uint32_t>(p[0]) << 16) | p[len - 1];
1122
90
  uint32_t mem1 = static_cast<uint32_t>(p[len / 2]) << 8;
1123
90
  return mem0 | mem1;
1124
90
}
1125
1126
#ifdef ABSL_HASH_INTERNAL_HAS_CRC32
1127
1128
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t CombineRawImpl(uint64_t state,
1129
                                                            uint64_t value) {
1130
  // We use a union to access the high and low 32 bits of the state.
1131
  union {
1132
    uint64_t u64;
1133
    struct {
1134
#ifdef ABSL_IS_LITTLE_ENDIAN
1135
      uint32_t low, high;
1136
#else  // big endian
1137
      uint32_t high, low;
1138
#endif
1139
    } u32s;
1140
  } s;
1141
  s.u64 = state;
1142
  // The general idea here is to do two CRC32 operations in parallel using the
1143
  // low and high 32 bits of state as CRC states. Note that: (1) when absl::Hash
1144
  // is inlined into swisstable lookups, we know that the seed's high bits are
1145
  // zero so s.u32s.high is available immediately. (2) We chose to multiply
1146
  // value by 3 for the low CRC because (a) multiplication by 3 can be done in 1
1147
  // cycle on x86/ARM and (b) multiplication has carry bits so it's nonlinear in
1148
  // GF(2) and therefore ensures that the two CRCs are independent (unlike bit
1149
  // rotation, XOR, etc). (3) We also tried using addition instead of
1150
  // multiplication by 3, but (a) code size is larger and (b) if the input keys
1151
  // all have 0s in the bits where the addition constant has 1s, then the
1152
  // addition is equivalent to XOR and linear in GF(2). (4) The union makes it
1153
  // easy for the compiler to understand that the high and low CRC states are
1154
  // independent from each other so that when CombineRawImpl is repeated (e.g.
1155
  // for std::pair<size_t, size_t>), the CRC chains can run in parallel. We
1156
  // originally tried using bswaps rather than shifting by 32 bits (to get from
1157
  // high to low bits) because bswap is one byte smaller in code size, but the
1158
  // compiler couldn't understand that the CRC chains were independent.
1159
  s.u32s.high =
1160
      static_cast<uint32_t>(ABSL_HASH_INTERNAL_CRC32_U64(s.u32s.high, value));
1161
  s.u32s.low = static_cast<uint32_t>(
1162
      ABSL_HASH_INTERNAL_CRC32_U64(s.u32s.low, 3 * value));
1163
  return s.u64;
1164
}
1165
#else   // ABSL_HASH_INTERNAL_HAS_CRC32
1166
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t CombineRawImpl(uint64_t state,
1167
689
                                                            uint64_t value) {
1168
689
  return Mix(state ^ value, kMul);
1169
689
}
1170
#endif  // ABSL_HASH_INTERNAL_HAS_CRC32
1171
1172
// Slow dispatch path for calls to CombineContiguousImpl with a size argument
1173
// larger than inlined size. Has the same effect as calling
1174
// CombineContiguousImpl() repeatedly with the chunk stride size.
1175
uint64_t CombineLargeContiguousImplOn32BitLengthGt8(uint64_t state,
1176
                                                    const unsigned char* first,
1177
                                                    size_t len);
1178
uint64_t CombineLargeContiguousImplOn64BitLengthGt32(uint64_t state,
1179
                                                     const unsigned char* first,
1180
                                                     size_t len);
1181
1182
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t CombineSmallContiguousImpl(
1183
689
    uint64_t state, const unsigned char* first, size_t len) {
1184
689
  ABSL_ASSUME(len <= 8);
1185
689
  uint64_t v;
1186
689
  if (len >= 4) {
1187
599
    v = Read4To8(first, len);
1188
599
  } else if (len > 0) {
1189
90
    v = Read1To3(first, len);
1190
90
  } else {
1191
    // Empty string must modify the state.
1192
0
    v = 0x57;
1193
0
  }
1194
689
  return CombineRawImpl(state, v);
1195
689
}
1196
1197
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t CombineContiguousImpl9to16(
1198
617
    uint64_t state, const unsigned char* first, size_t len) {
1199
617
  ABSL_ASSUME(len >= 9);
1200
617
  ABSL_ASSUME(len <= 16);
1201
  // Note: any time one half of the mix function becomes zero it will fail to
1202
  // incorporate any bits from the other half. However, there is exactly 1 in
1203
  // 2^64 values for each side that achieve this, and only when the size is
1204
  // exactly 16 -- for smaller sizes there is an overlapping byte that makes
1205
  // this impossible unless the seed is *also* incredibly unlucky.
1206
617
  auto p = Read9To16(first, len);
1207
617
  return Mix(state ^ p.first, kMul ^ p.second);
1208
617
}
1209
1210
ABSL_ATTRIBUTE_ALWAYS_INLINE inline uint64_t CombineContiguousImpl17to32(
1211
6.83k
    uint64_t state, const unsigned char* first, size_t len) {
1212
6.83k
  ABSL_ASSUME(len >= 17);
1213
6.83k
  ABSL_ASSUME(len <= 32);
1214
  // Do two mixes of overlapping 16-byte ranges in parallel to minimize
1215
  // latency.
1216
6.83k
  const uint64_t m0 =
1217
6.83k
      Mix(Read8(first) ^ kStaticRandomData[1], Read8(first + 8) ^ state);
1218
1219
6.83k
  const unsigned char* tail_16b_ptr = first + (len - 16);
1220
6.83k
  const uint64_t m1 = Mix(Read8(tail_16b_ptr) ^ kStaticRandomData[3],
1221
6.83k
                          Read8(tail_16b_ptr + 8) ^ state);
1222
6.83k
  return m0 ^ m1;
1223
6.83k
}
1224
1225
// Implementation of the base case for combine_contiguous where we actually
1226
// mix the bytes into the state.
1227
// Dispatch to different implementations of combine_contiguous depending
1228
// on the value of `sizeof(size_t)`.
1229
inline uint64_t CombineContiguousImpl(
1230
    uint64_t state, const unsigned char* first, size_t len,
1231
0
    std::integral_constant<int, 4> /* sizeof_size_t */) {
1232
0
  // For large values we use CityHash, for small ones we use custom low latency
1233
0
  // hash.
1234
0
  if (len <= 8) {
1235
0
    return CombineSmallContiguousImpl(PrecombineLengthMix(state, len), first,
1236
0
                                      len);
1237
0
  }
1238
0
  return CombineLargeContiguousImplOn32BitLengthGt8(state, first, len);
1239
0
}
1240
1241
#ifdef ABSL_HASH_INTERNAL_HAS_CRC32
1242
inline uint64_t CombineContiguousImpl(
1243
    uint64_t state, const unsigned char* first, size_t len,
1244
    std::integral_constant<int, 8> /* sizeof_size_t */) {
1245
  if (ABSL_PREDICT_FALSE(len > 32)) {
1246
    return CombineLargeContiguousImplOn64BitLengthGt32(state, first, len);
1247
  }
1248
  // `mul` is the salt that is used for final mixing. It is important to fill
1249
  // high 32 bits because CRC wipes out high 32 bits.
1250
  // `rotr` is important to mix `len` into high 32 bits.
1251
  uint64_t mul = absl::rotr(kMul, static_cast<int>(len));
1252
  // Only low 32 bits of each uint64_t are used in CRC32 so we use gbswap_64 to
1253
  // move high 32 bits to low 32 bits. It has slightly smaller binary size than
1254
  // `>> 32`. `state + 8 * len` is a single instruction on both x86 and ARM, so
1255
  // we use it to better mix length. Although only the low 32 bits of the pair
1256
  // elements are used, we use pair<uint64_t, uint64_t> for better generated
1257
  // code.
1258
  std::pair<uint64_t, uint64_t> crcs = {state + 8 * len,
1259
                                        absl::gbswap_64(state)};
1260
1261
  // All CRC operations here directly read bytes from the memory.
1262
  // Single fused instructions are used, like `crc32 rcx, qword ptr [rsi]`.
1263
  // On x86, llvm-mca reports latency `R + 2` for such fused instructions, while
1264
  // `R + 3` for two separate `mov` + `crc` instructions. `R` is the latency of
1265
  // reading the memory. Fused instructions also reduce register pressure
1266
  // allowing surrounding code to be more efficient when this code is inlined.
1267
  if (len > 8) {
1268
    crcs = {ABSL_HASH_INTERNAL_CRC32_U64(crcs.first, Read8(first)),
1269
            ABSL_HASH_INTERNAL_CRC32_U64(crcs.second, Read8(first + len - 8))};
1270
    if (len > 16) {
1271
      // We compute the second round of dependent CRC32 operations.
1272
      crcs = {ABSL_HASH_INTERNAL_CRC32_U64(crcs.first, Read8(first + len - 16)),
1273
              ABSL_HASH_INTERNAL_CRC32_U64(crcs.second, Read8(first + 8))};
1274
    }
1275
  } else {
1276
    if (len >= 4) {
1277
      // We use CRC for 4 bytes to benefit from the fused instruction and better
1278
      // hash quality.
1279
      // Using `xor` or `add` may reduce latency for this case, but would
1280
      // require more registers, more instructions and will have worse hash
1281
      // quality.
1282
      crcs = {ABSL_HASH_INTERNAL_CRC32_U32(static_cast<uint32_t>(crcs.first),
1283
                                           Read4(first)),
1284
              ABSL_HASH_INTERNAL_CRC32_U32(static_cast<uint32_t>(crcs.second),
1285
                                           Read4(first + len - 4))};
1286
    } else if (len >= 1) {
1287
      // We mix three bytes all into different output registers.
1288
      // This way, we do not need shifting of these bytes (so they don't overlap
1289
      // with each other).
1290
      crcs = {ABSL_HASH_INTERNAL_CRC32_U8(static_cast<uint32_t>(crcs.first),
1291
                                          first[0]),
1292
              ABSL_HASH_INTERNAL_CRC32_U8(static_cast<uint32_t>(crcs.second),
1293
                                          first[len - 1])};
1294
      // Middle byte is mixed weaker. It is a new byte only for len == 3.
1295
      // Mixing is independent from CRC operations so it is scheduled ASAP.
1296
      mul += first[len / 2];
1297
    }
1298
  }
1299
  // `mul` is mixed into both sides of `Mix` to guarantee non-zero values for
1300
  // both multiplicands. Using Mix instead of just multiplication here improves
1301
  // hash quality, especially for short strings.
1302
  return Mix(mul - crcs.first, crcs.second - mul);
1303
}
1304
#else
1305
inline uint64_t CombineContiguousImpl(
1306
    uint64_t state, const unsigned char* first, size_t len,
1307
93.3k
    std::integral_constant<int, 8> /* sizeof_size_t */) {
1308
  // For large values we use LowLevelHash or CityHash depending on the platform,
1309
  // for small ones we use custom low latency hash.
1310
93.3k
  if (len <= 8) {
1311
689
    return CombineSmallContiguousImpl(PrecombineLengthMix(state, len), first,
1312
689
                                      len);
1313
689
  }
1314
92.6k
  if (len <= 16) {
1315
617
    return CombineContiguousImpl9to16(PrecombineLengthMix(state, len), first,
1316
617
                                      len);
1317
617
  }
1318
92.0k
  if (len <= 32) {
1319
6.83k
    return CombineContiguousImpl17to32(PrecombineLengthMix(state, len), first,
1320
6.83k
                                       len);
1321
6.83k
  }
1322
  // We must not mix length into the state here because calling
1323
  // CombineContiguousImpl twice with PiecewiseChunkSize() must be equivalent
1324
  // to calling CombineLargeContiguousImpl once with 2 * PiecewiseChunkSize().
1325
85.2k
  return CombineLargeContiguousImplOn64BitLengthGt32(state, first, len);
1326
92.0k
}
1327
#endif  // ABSL_HASH_INTERNAL_HAS_CRC32
1328
1329
#if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE)
1330
#define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
1331
#else
1332
#define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
1333
#endif
1334
1335
// Type trait to select the appropriate hash implementation to use.
1336
// HashSelect::type<T> will give the proper hash implementation, to be invoked
1337
// as:
1338
//   HashSelect::type<T>::Invoke(state, value)
1339
// Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
1340
// valid `Invoke` function. Types that are not hashable will have a ::value of
1341
// `false`.
1342
struct HashSelect {
1343
 private:
1344
  struct WeaklyMixedIntegerProbe {
1345
    template <typename H>
1346
    static H Invoke(H state, WeaklyMixedInteger value) {
1347
      return hash_internal::hash_weakly_mixed_integer(std::move(state), value);
1348
    }
1349
  };
1350
1351
  struct State : HashStateBase<State> {
1352
    static State combine_contiguous(State hash_state, const unsigned char*,
1353
                                    size_t);
1354
    using State::HashStateBase::combine_contiguous;
1355
    static State combine_raw(State state, uint64_t value);
1356
    static State combine_weakly_mixed_integer(State hash_state,
1357
                                              WeaklyMixedInteger value);
1358
  };
1359
1360
  struct UniquelyRepresentedProbe {
1361
    template <typename H, typename T>
1362
    static auto Invoke(H state, const T& value)
1363
0
        -> std::enable_if_t<is_uniquely_represented<T>::value, H> {
1364
0
      return hash_internal::hash_bytes(std::move(state), value);
1365
0
    }
Unexecuted instantiation: _ZN4absl13hash_internal10HashSelect24UniquelyRepresentedProbe6InvokeINS0_15MixingHashStateEiEENSt3__19enable_ifIXsr23is_uniquely_representedIT0_EE5valueET_E4typeES8_RKS7_
Unexecuted instantiation: _ZN4absl13hash_internal10HashSelect24UniquelyRepresentedProbe6InvokeINS0_15MixingHashStateEmEENSt3__19enable_ifIXsr23is_uniquely_representedIT0_EE5valueET_E4typeES8_RKS7_
1366
  };
1367
1368
  struct HashValueProbe {
1369
    template <typename H, typename T>
1370
    static auto Invoke(H state, const T& value) -> std::enable_if_t<
1371
        std::is_same_v<H, decltype(AbslHashValue(std::move(state), value))>,
1372
0
        H> {
1373
0
      return AbslHashValue(std::move(state), value);
1374
0
    }
Unexecuted instantiation: _ZN4absl13hash_internal10HashSelect14HashValueProbe6InvokeINS0_15MixingHashStateENSt3__15tupleIJRKNS5_17basic_string_viewIcNS5_11char_traitsIcEEEERKiEEEEENS5_9enable_ifIXsr3stdE9is_same_vIT_DTcl13AbslHashValueclsr3stdE4movefp_Efp0_EEEESH_E4typeESH_RKT0_
Unexecuted instantiation: _ZN4absl13hash_internal10HashSelect14HashValueProbe6InvokeINS0_15MixingHashStateENSt3__117basic_string_viewIcNS5_11char_traitsIcEEEEEENS5_9enable_ifIXsr3stdE9is_same_vIT_DTcl13AbslHashValueclsr3stdE4movefp_Efp0_EEEESB_E4typeESB_RKT0_
Unexecuted instantiation: _ZN4absl13hash_internal10HashSelect14HashValueProbe6InvokeINS0_15MixingHashStateENSt3__15tupleIJRKmEEEEENS5_9enable_ifIXsr3stdE9is_same_vIT_DTcl13AbslHashValueclsr3stdE4movefp_Efp0_EEEESB_E4typeESB_RKT0_
Unexecuted instantiation: _ZN4absl13hash_internal10HashSelect14HashValueProbe6InvokeINS0_15MixingHashStateENS_4CordEEENSt3__19enable_ifIXsr3stdE9is_same_vIT_DTcl13AbslHashValueclsr3stdE4movefp_Efp0_EEEES8_E4typeES8_RKT0_
1375
  };
1376
1377
  struct LegacyHashProbe {
1378
#if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
1379
    template <typename H, typename T>
1380
    static auto Invoke(H state, const T& value) -> std::enable_if_t<
1381
        std::is_convertible_v<
1382
            decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
1383
            size_t>,
1384
        H> {
1385
      return hash_internal::hash_bytes(
1386
          std::move(state),
1387
          ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
1388
    }
1389
#endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
1390
  };
1391
1392
  struct StdHashProbe {
1393
    template <typename H, typename T>
1394
    static auto Invoke(H state, const T& value)
1395
        -> std::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
1396
      return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
1397
    }
1398
  };
1399
1400
  template <typename Hash, typename T>
1401
  struct Probe : Hash {
1402
   private:
1403
    template <typename H, typename = decltype(H::Invoke(
1404
                              std::declval<State>(), std::declval<const T&>()))>
1405
    static std::true_type Test(int);
1406
    template <typename U>
1407
    static std::false_type Test(char);
1408
1409
   public:
1410
    static constexpr bool value = decltype(Test<Hash>(0))::value;
1411
  };
1412
1413
 public:
1414
  // Probe each implementation in order.
1415
  // disjunction provides short circuiting wrt instantiation.
1416
  template <typename T>
1417
  using Apply = std::disjunction<         //
1418
      Probe<WeaklyMixedIntegerProbe, T>,   //
1419
      Probe<UniquelyRepresentedProbe, T>,  //
1420
      Probe<HashValueProbe, T>,            //
1421
      Probe<LegacyHashProbe, T>,           //
1422
      Probe<StdHashProbe, T>,              //
1423
      std::false_type>;
1424
};
1425
1426
template <typename T>
1427
struct is_hashable : std::bool_constant<HashSelect::template Apply<T>::value> {
1428
};
1429
1430
class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
1431
  template <typename T>
1432
  using IntegralFastPath =
1433
      std::conjunction<std::is_integral<T>, is_uniquely_represented<T>,
1434
                       FitsIn64Bits<T>>;
1435
1436
 public:
1437
  // Move only
1438
  MixingHashState(MixingHashState&&) = default;
1439
  MixingHashState& operator=(MixingHashState&&) = default;
1440
1441
  // Fundamental base case for hash recursion: mixes the given range of bytes
1442
  // into the hash state.
1443
  static MixingHashState combine_contiguous(MixingHashState hash_state,
1444
                                            const unsigned char* first,
1445
0
                                            size_t size) {
1446
0
    return MixingHashState(
1447
0
        CombineContiguousImpl(hash_state.state_, first, size,
1448
0
                              std::integral_constant<int, sizeof(size_t)>{}));
1449
0
  }
1450
  using MixingHashState::HashStateBase::combine_contiguous;
1451
1452
  template <typename T>
1453
0
  static size_t hash(const T& value) {
1454
0
    return hash_with_seed(value, Seed());
1455
0
  }
Unexecuted instantiation: unsigned long absl::hash_internal::MixingHashState::hash<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&> >(std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&> const&)
Unexecuted instantiation: unsigned long absl::hash_internal::MixingHashState::hash<std::__1::tuple<unsigned long const&> >(std::__1::tuple<unsigned long const&> const&)
Unexecuted instantiation: unsigned long absl::hash_internal::MixingHashState::hash<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&)
Unexecuted instantiation: unsigned long absl::hash_internal::MixingHashState::hash<absl::Cord>(absl::Cord const&)
1456
1457
  // For performance reasons in non-opt mode, we specialize this for
1458
  // integral types.
1459
  // Otherwise we would be instantiating and calling dozens of functions for
1460
  // something that is just one multiplication and a couple xor's.
1461
  // The result should be the same as running the whole algorithm, but faster.
1462
  template <typename T, std::enable_if_t<IntegralFastPath<T>::value, int> = 0>
1463
  static size_t hash_with_seed(T value, size_t seed) {
1464
    return static_cast<size_t>(
1465
        CombineRawImpl(seed, static_cast<std::make_unsigned_t<T>>(value)));
1466
  }
1467
1468
  template <typename T, std::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
1469
0
  static size_t hash_with_seed(const T& value, size_t seed) {
1470
0
    return static_cast<size_t>(combine(MixingHashState{seed}, value).state_);
1471
0
  }
Unexecuted instantiation: _ZN4absl13hash_internal15MixingHashState14hash_with_seedINSt3__15tupleIJRKNS3_17basic_string_viewIcNS3_11char_traitsIcEEEERKiEEETnNS3_9enable_ifIXntsr16IntegralFastPathIT_EE5valueEiE4typeELi0EEEmRKSF_m
Unexecuted instantiation: _ZN4absl13hash_internal15MixingHashState14hash_with_seedINSt3__15tupleIJRKmEEETnNS3_9enable_ifIXntsr16IntegralFastPathIT_EE5valueEiE4typeELi0EEEmRKS9_m
Unexecuted instantiation: _ZN4absl13hash_internal15MixingHashState14hash_with_seedINSt3__117basic_string_viewIcNS3_11char_traitsIcEEEETnNS3_9enable_ifIXntsr16IntegralFastPathIT_EE5valueEiE4typeELi0EEEmRKS9_m
Unexecuted instantiation: _ZN4absl13hash_internal15MixingHashState14hash_with_seedINS_4CordETnNSt3__19enable_ifIXntsr16IntegralFastPathIT_EE5valueEiE4typeELi0EEEmRKS6_m
1472
1473
 private:
1474
  friend class MixingHashState::HashStateBase;
1475
  template <typename H>
1476
  friend H absl::hash_internal::hash_weakly_mixed_integer(H,
1477
                                                          WeaklyMixedInteger);
1478
  // Allow the HashState type-erasure implementation to invoke
1479
  // RunCombinedUnordered() directly.
1480
  friend class absl::HashState;
1481
  friend struct CombineRaw;
1482
1483
  // For use in Seed().
1484
  static const void* const kSeed;
1485
1486
  // Invoked only once for a given argument; that plus the fact that this is
1487
  // move-only ensures that there is only one non-moved-from object.
1488
0
  MixingHashState() : state_(Seed()) {}
1489
1490
  // Workaround for MSVC bug.
1491
  // We make the type copyable to fix the calling convention, even though we
1492
  // never actually copy it. Keep it private to not affect the public API of the
1493
  // type.
1494
  MixingHashState(const MixingHashState&) = default;
1495
1496
0
  explicit MixingHashState(uint64_t state) : state_(state) {}
1497
1498
  // Combines a raw value from e.g. integrals/floats/pointers/etc. This allows
1499
  // us to be consistent with IntegralFastPath when combining raw types, but
1500
  // optimize Read1To3 and Read4To8 differently for the string case.
1501
  static MixingHashState combine_raw(MixingHashState hash_state,
1502
0
                                     uint64_t value) {
1503
0
    return MixingHashState(CombineRawImpl(hash_state.state_, value));
1504
0
  }
1505
1506
  static MixingHashState combine_weakly_mixed_integer(
1507
0
      MixingHashState hash_state, WeaklyMixedInteger value) {
1508
0
    // Some transformation for the value is needed to make an empty
1509
0
    // string/container change the mixing hash state.
1510
0
    // We use constant smaller than 8 bits to make compiler use
1511
0
    // `add` with an immediate operand with 1 byte value.
1512
0
    return MixingHashState{hash_state.state_ + (0x57 + value.value)};
1513
0
  }
1514
1515
  template <typename CombinerT>
1516
  static MixingHashState RunCombineUnordered(MixingHashState state,
1517
                                             CombinerT combiner) {
1518
    uint64_t unordered_state = 0;
1519
    combiner(MixingHashState{}, [&](MixingHashState& inner_state) {
1520
      // Add the hash state of the element to the running total, but mix the
1521
      // carry bit back into the low bit.  This in intended to avoid losing
1522
      // entropy to overflow, especially when unordered_multisets contain
1523
      // multiple copies of the same value.
1524
      auto element_state = inner_state.state_;
1525
      unordered_state += element_state;
1526
      if (unordered_state < element_state) {
1527
        ++unordered_state;
1528
      }
1529
      inner_state = MixingHashState{};
1530
    });
1531
    return MixingHashState::combine(std::move(state), unordered_state);
1532
  }
1533
1534
  // A non-deterministic seed.
1535
  //
1536
  // The current purpose of this seed is to generate non-deterministic results
1537
  // and prevent having users depend on the particular hash values.
1538
  // It is not meant as a security feature right now, but it leaves the door
1539
  // open to upgrade it to a true per-process random seed. A true random seed
1540
  // costs more and we don't need to pay for that right now.
1541
  //
1542
  // On platforms with ASLR, we take advantage of it to make a per-process
1543
  // random value.
1544
  // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
1545
  //
1546
  // On other platforms this is still going to be non-deterministic but most
1547
  // probably per-build and not per-process.
1548
0
  ABSL_ATTRIBUTE_ALWAYS_INLINE static size_t Seed() {
1549
0
#if (!defined(__clang__) || __clang_major__ > 11) && \
1550
0
    (!defined(__apple_build_version__) ||            \
1551
0
     __apple_build_version__ >= 19558921)  // Xcode 12
1552
0
    return static_cast<size_t>(reinterpret_cast<uintptr_t>(&kSeed));
1553
#else
1554
    // Workaround the absence of
1555
    // https://github.com/llvm/llvm-project/commit/bc15bf66dcca76cc06fe71fca35b74dc4d521021.
1556
    return static_cast<size_t>(reinterpret_cast<uintptr_t>(kSeed));
1557
#endif
1558
0
  }
1559
1560
  uint64_t state_;
1561
};
1562
1563
struct AggregateBarrier {};
1564
1565
// Add a private base class to make sure this type is not an aggregate.
1566
// Aggregates can be aggregate initialized even if the default constructor is
1567
// deleted.
1568
struct PoisonedHash : private AggregateBarrier {
1569
  PoisonedHash() = delete;
1570
  PoisonedHash(const PoisonedHash&) = delete;
1571
  PoisonedHash& operator=(const PoisonedHash&) = delete;
1572
  void operator()() const = delete;
1573
  size_t hash_with_seed() const = delete;
1574
};
1575
1576
template <typename T>
1577
struct HashImpl {
1578
0
  size_t operator()(const T& value) const {
1579
0
    return MixingHashState::hash(value);
1580
0
  }
Unexecuted instantiation: absl::hash_internal::HashImpl<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&> >::operator()(std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&> const&) const
Unexecuted instantiation: absl::hash_internal::HashImpl<std::__1::tuple<unsigned long const&> >::operator()(std::__1::tuple<unsigned long const&> const&) const
Unexecuted instantiation: absl::hash_internal::HashImpl<std::__1::basic_string_view<char, std::__1::char_traits<char> > >::operator()(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const
Unexecuted instantiation: absl::hash_internal::HashImpl<absl::Cord>::operator()(absl::Cord const&) const
1581
1582
 protected:
1583
  friend HashWithSeed;
1584
1585
0
  size_t hash_with_seed(const T& value, size_t seed) const {
1586
0
    return MixingHashState::hash_with_seed(value, seed);
1587
0
  }
Unexecuted instantiation: absl::hash_internal::HashImpl<std::__1::basic_string_view<char, std::__1::char_traits<char> > >::hash_with_seed(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, unsigned long) const
Unexecuted instantiation: absl::hash_internal::HashImpl<absl::Cord>::hash_with_seed(absl::Cord const&, unsigned long) const
1588
};
1589
1590
template <typename T>
1591
struct Hash
1592
    : std::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
1593
1594
template <typename T, typename... Ts>
1595
inline constexpr bool pack_contains_v = (std::is_same_v<T, Ts> || ...);
1596
1597
template <size_t>
1598
struct EmptyDuplicatedHash {
1599
  void operator()() const = delete;
1600
  size_t hash_with_seed() const = delete;
1601
};
1602
1603
template <typename... Ts>
1604
class TransparentHashImpl;
1605
1606
template <typename T>
1607
class TransparentHashImpl<T> : private Hash<T> {
1608
 public:
1609
  using Hash<T>::operator();
1610
  using Hash<T>::hash_with_seed;
1611
};
1612
1613
template <typename T, typename... Ts>
1614
using TransparentHashImplSingle =
1615
    std::conditional_t<pack_contains_v<T, Ts...>,
1616
                       EmptyDuplicatedHash<sizeof...(Ts)>, Hash<T>>;
1617
1618
template <typename T, typename... Ts>
1619
class TransparentHashImpl<T, Ts...>
1620
    : private TransparentHashImpl<Ts...>,
1621
      private TransparentHashImplSingle<T, Ts...> {
1622
 public:
1623
  using TransparentHashImpl<Ts...>::operator();
1624
  using TransparentHashImplSingle<T, Ts...>::operator();
1625
  using TransparentHashImpl<Ts...>::hash_with_seed;
1626
  using TransparentHashImplSingle<T, Ts...>::hash_with_seed;
1627
};
1628
1629
template <typename... Ts>
1630
using TransparentHashBase =
1631
    std::conditional_t<(... && is_hashable<Ts>::value),
1632
                       TransparentHashImpl<Ts...>, PoisonedHash>;
1633
1634
template <typename... Ts>
1635
class TransparentHash : private TransparentHashBase<Ts...> {
1636
 public:
1637
  using is_transparent = void;
1638
  using TransparentHashBase<Ts...>::operator();
1639
1640
 private:
1641
  friend HashWithSeed;
1642
1643
  using TransparentHashBase<Ts...>::hash_with_seed;
1644
};
1645
1646
template <typename H>
1647
template <typename T, typename... Ts>
1648
0
H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
1649
0
  return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
1650
0
                        std::move(state), value),
1651
0
                    values...);
1652
0
}
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&>>(absl::hash_internal::MixingHashState, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&> const&)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<std::__1::basic_string_view<char, std::__1::char_traits<char> >, int>(absl::hash_internal::MixingHashState, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, int const&)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<int>(absl::hash_internal::MixingHashState, int const&)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<std::__1::tuple<unsigned long const&>>(absl::hash_internal::MixingHashState, std::__1::tuple<unsigned long const&> const&)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<unsigned long>(absl::hash_internal::MixingHashState, unsigned long const&)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<std::__1::basic_string_view<char, std::__1::char_traits<char> >>(absl::hash_internal::MixingHashState, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&)
Unexecuted instantiation: absl::hash_internal::MixingHashState absl::hash_internal::HashStateBase<absl::hash_internal::MixingHashState>::combine<absl::Cord>(absl::hash_internal::MixingHashState, absl::Cord const&)
1653
1654
template <typename H>
1655
template <typename T>
1656
0
H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
1657
0
  return hash_internal::hash_range_or_bytes(std::move(state), data, size);
1658
0
}
1659
1660
template <typename H>
1661
template <typename I>
1662
H HashStateBase<H>::combine_unordered(H state, I begin, I end) {
1663
  return H::RunCombineUnordered(std::move(state),
1664
                                CombineUnorderedCallback<I>{begin, end});
1665
}
1666
1667
template <typename H>
1668
H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
1669
0
                                size_t size) {
1670
0
  if (position_ + size < PiecewiseChunkSize()) {
1671
0
    // This partial chunk does not fill our existing buffer
1672
0
    memcpy(buf_ + position_, data, size);
1673
0
    position_ += size;
1674
0
    return state;
1675
0
  }
1676
0
  added_something_ = true;
1677
0
  // If the buffer is partially filled we need to complete the buffer
1678
0
  // and hash it.
1679
0
  if (position_ != 0) {
1680
0
    const size_t bytes_needed = PiecewiseChunkSize() - position_;
1681
0
    memcpy(buf_ + position_, data, bytes_needed);
1682
0
    state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
1683
0
    data += bytes_needed;
1684
0
    size -= bytes_needed;
1685
0
  }
1686
0
1687
0
  // Hash whatever chunks we can without copying
1688
0
  while (size >= PiecewiseChunkSize()) {
1689
0
    state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
1690
0
    data += PiecewiseChunkSize();
1691
0
    size -= PiecewiseChunkSize();
1692
0
  }
1693
0
  // Fill the buffer with the remainder
1694
0
  memcpy(buf_, data, size);
1695
0
  position_ = size;
1696
0
  return state;
1697
0
}
1698
1699
template <typename H>
1700
0
H PiecewiseCombiner::finalize(H state) {
1701
0
  // Do not call combine_contiguous with empty remainder since it is modifying
1702
0
  // state.
1703
0
  if (added_something_ && position_ == 0) {
1704
0
    return state;
1705
0
  }
1706
0
  // We still call combine_contiguous for the entirely empty buffer.
1707
0
  return H::combine_contiguous(std::move(state), buf_, position_);
1708
0
}
1709
1710
}  // namespace hash_internal
1711
ABSL_NAMESPACE_END
1712
}  // namespace absl
1713
1714
#undef ABSL_HASH_INTERNAL_HAS_CRC32
1715
#undef ABSL_HASH_INTERNAL_CRC32_U64
1716
#undef ABSL_HASH_INTERNAL_CRC32_U32
1717
#undef ABSL_HASH_INTERNAL_CRC32_U8
1718
1719
#endif  // ABSL_HASH_INTERNAL_HASH_H_