Coverage Report

Created: 2026-04-12 06:05

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