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