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 #include <algorithm>
28 #include <array>
29 #include <bitset>
30 #include <cmath>
31 #include <cstddef>
32 #include <cstring>
33 #include <deque>
34 #include <forward_list>
35 #include <functional>
36 #include <iterator>
37 #include <limits>
38 #include <list>
39 #include <map>
40 #include <memory>
41 #include <set>
42 #include <string>
43 #include <tuple>
44 #include <type_traits>
45 #include <unordered_map>
46 #include <unordered_set>
47 #include <utility>
48 #include <vector>
49
50 #include "absl/base/config.h"
51 #include "absl/base/internal/unaligned_access.h"
52 #include "absl/base/port.h"
53 #include "absl/container/fixed_array.h"
54 #include "absl/hash/internal/city.h"
55 #include "absl/hash/internal/low_level_hash.h"
56 #include "absl/meta/type_traits.h"
57 #include "absl/numeric/bits.h"
58 #include "absl/numeric/int128.h"
59 #include "absl/strings/string_view.h"
60 #include "absl/types/optional.h"
61 #include "absl/types/variant.h"
62 #include "absl/utility/utility.h"
63
64 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L && \
65 !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
66 #include <filesystem> // NOLINT
67 #endif
68
69 #ifdef ABSL_HAVE_STD_STRING_VIEW
70 #include <string_view>
71 #endif
72
73 namespace absl {
74 ABSL_NAMESPACE_BEGIN
75
76 class HashState;
77
78 namespace hash_internal {
79
80 // Internal detail: Large buffers are hashed in smaller chunks. This function
81 // returns the size of these chunks.
PiecewiseChunkSize()82 constexpr size_t PiecewiseChunkSize() { return 1024; }
83
84 // PiecewiseCombiner
85 //
86 // PiecewiseCombiner is an internal-only helper class for hashing a piecewise
87 // buffer of `char` or `unsigned char` as though it were contiguous. This class
88 // provides two methods:
89 //
90 // H add_buffer(state, data, size)
91 // H finalize(state)
92 //
93 // `add_buffer` can be called zero or more times, followed by a single call to
94 // `finalize`. This will produce the same hash expansion as concatenating each
95 // buffer piece into a single contiguous buffer, and passing this to
96 // `H::combine_contiguous`.
97 //
98 // Example usage:
99 // PiecewiseCombiner combiner;
100 // for (const auto& piece : pieces) {
101 // state = combiner.add_buffer(std::move(state), piece.data, piece.size);
102 // }
103 // return combiner.finalize(std::move(state));
104 class PiecewiseCombiner {
105 public:
PiecewiseCombiner()106 PiecewiseCombiner() : position_(0) {}
107 PiecewiseCombiner(const PiecewiseCombiner&) = delete;
108 PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
109
110 // PiecewiseCombiner::add_buffer()
111 //
112 // Appends the given range of bytes to the sequence to be hashed, which may
113 // modify the provided hash state.
114 template <typename H>
115 H add_buffer(H state, const unsigned char* data, size_t size);
116 template <typename H>
add_buffer(H state,const char * data,size_t size)117 H add_buffer(H state, const char* data, size_t size) {
118 return add_buffer(std::move(state),
119 reinterpret_cast<const unsigned char*>(data), size);
120 }
121
122 // PiecewiseCombiner::finalize()
123 //
124 // Finishes combining the hash sequence, which may may modify the provided
125 // hash state.
126 //
127 // Once finalize() is called, add_buffer() may no longer be called. The
128 // resulting hash state will be the same as if the pieces passed to
129 // add_buffer() were concatenated into a single flat buffer, and then provided
130 // to H::combine_contiguous().
131 template <typename H>
132 H finalize(H state);
133
134 private:
135 unsigned char buf_[PiecewiseChunkSize()];
136 size_t position_;
137 };
138
139 // is_hashable()
140 //
141 // Trait class which returns true if T is hashable by the absl::Hash framework.
142 // Used for the AbslHashValue implementations for composite types below.
143 template <typename T>
144 struct is_hashable;
145
146 // HashStateBase
147 //
148 // An internal implementation detail that contains common implementation details
149 // for all of the "hash state objects" objects generated by Abseil. This is not
150 // a public API; users should not create classes that inherit from this.
151 //
152 // A hash state object is the template argument `H` passed to `AbslHashValue`.
153 // It represents an intermediate state in the computation of an unspecified hash
154 // algorithm. `HashStateBase` provides a CRTP style base class for hash state
155 // implementations. Developers adding type support for `absl::Hash` should not
156 // rely on any parts of the state object other than the following member
157 // functions:
158 //
159 // * HashStateBase::combine()
160 // * HashStateBase::combine_contiguous()
161 // * HashStateBase::combine_unordered()
162 //
163 // A derived hash state class of type `H` must provide a public member function
164 // with a signature similar to the following:
165 //
166 // `static H combine_contiguous(H state, const unsigned char*, size_t)`.
167 //
168 // It must also provide a private template method named RunCombineUnordered.
169 //
170 // A "consumer" is a 1-arg functor returning void. Its argument is a reference
171 // to an inner hash state object, and it may be called multiple times. When
172 // called, the functor consumes the entropy from the provided state object,
173 // and resets that object to its empty state.
174 //
175 // A "combiner" is a stateless 2-arg functor returning void. Its arguments are
176 // an inner hash state object and an ElementStateConsumer functor. A combiner
177 // uses the provided inner hash state object to hash each element of the
178 // container, passing the inner hash state object to the consumer after hashing
179 // each element.
180 //
181 // Given these definitions, a derived hash state class of type H
182 // must provide a private template method with a signature similar to the
183 // following:
184 //
185 // `template <typename CombinerT>`
186 // `static H RunCombineUnordered(H outer_state, CombinerT combiner)`
187 //
188 // This function is responsible for constructing the inner state object and
189 // providing a consumer to the combiner. It uses side effects of the consumer
190 // and combiner to mix the state of each element in an order-independent manner,
191 // and uses this to return an updated value of `outer_state`.
192 //
193 // This inside-out approach generates efficient object code in the normal case,
194 // but allows us to use stack storage to implement the absl::HashState type
195 // erasure mechanism (avoiding heap allocations while hashing).
196 //
197 // `HashStateBase` will provide a complete implementation for a hash state
198 // object in terms of these two methods.
199 //
200 // Example:
201 //
202 // // Use CRTP to define your derived class.
203 // struct MyHashState : HashStateBase<MyHashState> {
204 // static H combine_contiguous(H state, const unsigned char*, size_t);
205 // using MyHashState::HashStateBase::combine;
206 // using MyHashState::HashStateBase::combine_contiguous;
207 // using MyHashState::HashStateBase::combine_unordered;
208 // private:
209 // template <typename CombinerT>
210 // static H RunCombineUnordered(H state, CombinerT combiner);
211 // };
212 template <typename H>
213 class HashStateBase {
214 public:
215 // HashStateBase::combine()
216 //
217 // Combines an arbitrary number of values into a hash state, returning the
218 // updated state.
219 //
220 // Each of the value types `T` must be separately hashable by the Abseil
221 // hashing framework.
222 //
223 // NOTE:
224 //
225 // state = H::combine(std::move(state), value1, value2, value3);
226 //
227 // is guaranteed to produce the same hash expansion as:
228 //
229 // state = H::combine(std::move(state), value1);
230 // state = H::combine(std::move(state), value2);
231 // state = H::combine(std::move(state), value3);
232 template <typename T, typename... Ts>
233 static H combine(H state, const T& value, const Ts&... values);
combine(H state)234 static H combine(H state) { return state; }
235
236 // HashStateBase::combine_contiguous()
237 //
238 // Combines a contiguous array of `size` elements into a hash state, returning
239 // the updated state.
240 //
241 // NOTE:
242 //
243 // state = H::combine_contiguous(std::move(state), data, size);
244 //
245 // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
246 // perform internal optimizations). If you need this guarantee, use the
247 // for-loop instead.
248 template <typename T>
249 static H combine_contiguous(H state, const T* data, size_t size);
250
251 template <typename I>
252 static H combine_unordered(H state, I begin, I end);
253
254 using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
255
256 template <typename T>
257 using is_hashable = absl::hash_internal::is_hashable<T>;
258
259 private:
260 // Common implementation of the iteration step of a "combiner", as described
261 // above.
262 template <typename I>
263 struct CombineUnorderedCallback {
264 I begin;
265 I end;
266
267 template <typename InnerH, typename ElementStateConsumer>
operatorCombineUnorderedCallback268 void operator()(InnerH inner_state, ElementStateConsumer cb) {
269 for (; begin != end; ++begin) {
270 inner_state = H::combine(std::move(inner_state), *begin);
271 cb(inner_state);
272 }
273 }
274 };
275 };
276
277 // is_uniquely_represented
278 //
279 // `is_uniquely_represented<T>` is a trait class that indicates whether `T`
280 // is uniquely represented.
281 //
282 // A type is "uniquely represented" if two equal values of that type are
283 // guaranteed to have the same bytes in their underlying storage. In other
284 // words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
285 // zero. This property cannot be detected automatically, so this trait is false
286 // by default, but can be specialized by types that wish to assert that they are
287 // uniquely represented. This makes them eligible for certain optimizations.
288 //
289 // If you have any doubt whatsoever, do not specialize this template.
290 // The default is completely safe, and merely disables some optimizations
291 // that will not matter for most types. Specializing this template,
292 // on the other hand, can be very hazardous.
293 //
294 // To be uniquely represented, a type must not have multiple ways of
295 // representing the same value; for example, float and double are not
296 // uniquely represented, because they have distinct representations for
297 // +0 and -0. Furthermore, the type's byte representation must consist
298 // solely of user-controlled data, with no padding bits and no compiler-
299 // controlled data such as vptrs or sanitizer metadata. This is usually
300 // very difficult to guarantee, because in most cases the compiler can
301 // insert data and padding bits at its own discretion.
302 //
303 // If you specialize this template for a type `T`, you must do so in the file
304 // that defines that type (or in this file). If you define that specialization
305 // anywhere else, `is_uniquely_represented<T>` could have different meanings
306 // in different places.
307 //
308 // The Enable parameter is meaningless; it is provided as a convenience,
309 // to support certain SFINAE techniques when defining specializations.
310 template <typename T, typename Enable = void>
311 struct is_uniquely_represented : std::false_type {};
312
313 // is_uniquely_represented<unsigned char>
314 //
315 // unsigned char is a synonym for "byte", so it is guaranteed to be
316 // uniquely represented.
317 template <>
318 struct is_uniquely_represented<unsigned char> : std::true_type {};
319
320 // is_uniquely_represented for non-standard integral types
321 //
322 // Integral types other than bool should be uniquely represented on any
323 // platform that this will plausibly be ported to.
324 template <typename Integral>
325 struct is_uniquely_represented<
326 Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
327 : std::true_type {};
328
329 // is_uniquely_represented<bool>
330 //
331 //
332 template <>
333 struct is_uniquely_represented<bool> : std::false_type {};
334
335 // hash_bytes()
336 //
337 // Convenience function that combines `hash_state` with the byte representation
338 // of `value`.
339 template <typename H, typename T>
340 H hash_bytes(H hash_state, const T& value) {
341 const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
342 return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
343 }
344
345 // -----------------------------------------------------------------------------
346 // AbslHashValue for Basic Types
347 // -----------------------------------------------------------------------------
348
349 // Note: Default `AbslHashValue` implementations live in `hash_internal`. This
350 // allows us to block lexical scope lookup when doing an unqualified call to
351 // `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
352 // only be found via ADL.
353
354 // AbslHashValue() for hashing bool values
355 //
356 // We use SFINAE to ensure that this overload only accepts bool, not types that
357 // are convertible to bool.
358 template <typename H, typename B>
359 typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
360 H hash_state, B value) {
361 return H::combine(std::move(hash_state),
362 static_cast<unsigned char>(value ? 1 : 0));
363 }
364
365 // AbslHashValue() for hashing enum values
366 template <typename H, typename Enum>
367 typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
368 H hash_state, Enum e) {
369 // In practice, we could almost certainly just invoke hash_bytes directly,
370 // but it's possible that a sanitizer might one day want to
371 // store data in the unused bits of an enum. To avoid that risk, we
372 // convert to the underlying type before hashing. Hopefully this will get
373 // optimized away; if not, we can reopen discussion with c-toolchain-team.
374 return H::combine(std::move(hash_state),
375 static_cast<typename std::underlying_type<Enum>::type>(e));
376 }
377 // AbslHashValue() for hashing floating-point values
378 template <typename H, typename Float>
379 typename std::enable_if<std::is_same<Float, float>::value ||
380 std::is_same<Float, double>::value,
381 H>::type
382 AbslHashValue(H hash_state, Float value) {
383 return hash_internal::hash_bytes(std::move(hash_state),
384 value == 0 ? 0 : value);
385 }
386
387 // Long double has the property that it might have extra unused bytes in it.
388 // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
389 // of it. This means we can't use hash_bytes on a long double and have to
390 // convert it to something else first.
391 template <typename H, typename LongDouble>
392 typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
393 AbslHashValue(H hash_state, LongDouble value) {
394 const int category = std::fpclassify(value);
395 switch (category) {
396 case FP_INFINITE:
397 // Add the sign bit to differentiate between +Inf and -Inf
398 hash_state = H::combine(std::move(hash_state), std::signbit(value));
399 break;
400
401 case FP_NAN:
402 case FP_ZERO:
403 default:
404 // Category is enough for these.
405 break;
406
407 case FP_NORMAL:
408 case FP_SUBNORMAL:
409 // We can't convert `value` directly to double because this would have
410 // undefined behavior if the value is out of range.
411 // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
412 // guaranteed to be in range for `double`. The truncation is
413 // implementation defined, but that works as long as it is deterministic.
414 int exp;
415 auto mantissa = static_cast<double>(std::frexp(value, &exp));
416 hash_state = H::combine(std::move(hash_state), mantissa, exp);
417 }
418
419 return H::combine(std::move(hash_state), category);
420 }
421
422 // Without this overload, an array decays to a pointer and we hash that, which
423 // is not likely to be what the caller intended.
424 template <typename H, typename T, size_t N>
425 H AbslHashValue(H hash_state, T (&)[N]) {
426 static_assert(
427 sizeof(T) == -1,
428 "Hashing C arrays is not allowed. For string literals, wrap the literal "
429 "in absl::string_view(). To hash the array contents, use "
430 "absl::MakeSpan() or make the array an std::array. To hash the array "
431 "address, use &array[0].");
432 return hash_state;
433 }
434
435 // AbslHashValue() for hashing pointers
436 template <typename H, typename T>
437 std::enable_if_t<std::is_pointer<T>::value, H> AbslHashValue(H hash_state,
438 T ptr) {
439 auto v = reinterpret_cast<uintptr_t>(ptr);
440 // Due to alignment, pointers tend to have low bits as zero, and the next few
441 // bits follow a pattern since they are also multiples of some base value.
442 // Mixing the pointer twice helps prevent stuck low bits for certain alignment
443 // values.
444 return H::combine(std::move(hash_state), v, v);
445 }
446
447 // AbslHashValue() for hashing nullptr_t
448 template <typename H>
449 H AbslHashValue(H hash_state, std::nullptr_t) {
450 return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
451 }
452
453 // AbslHashValue() for hashing pointers-to-member
454 template <typename H, typename T, typename C>
455 H AbslHashValue(H hash_state, T C::*ptr) {
456 auto salient_ptm_size = [](std::size_t n) -> std::size_t {
457 #if defined(_MSC_VER)
458 // Pointers-to-member-function on MSVC consist of one pointer plus 0, 1, 2,
459 // or 3 ints. In 64-bit mode, they are 8-byte aligned and thus can contain
460 // padding (namely when they have 1 or 3 ints). The value below is a lower
461 // bound on the number of salient, non-padding bytes that we use for
462 // hashing.
463 if (alignof(T C::*) == alignof(int)) {
464 // No padding when all subobjects have the same size as the total
465 // alignment. This happens in 32-bit mode.
466 return n;
467 } else {
468 // Padding for 1 int (size 16) or 3 ints (size 24).
469 // With 2 ints, the size is 16 with no padding, which we pessimize.
470 return n == 24 ? 20 : n == 16 ? 12 : n;
471 }
472 #else
473 // On other platforms, we assume that pointers-to-members do not have
474 // padding.
475 #ifdef __cpp_lib_has_unique_object_representations
476 static_assert(std::has_unique_object_representations<T C::*>::value);
477 #endif // __cpp_lib_has_unique_object_representations
478 return n;
479 #endif
480 };
481 return H::combine_contiguous(std::move(hash_state),
482 reinterpret_cast<unsigned char*>(&ptr),
483 salient_ptm_size(sizeof ptr));
484 }
485
486 // -----------------------------------------------------------------------------
487 // AbslHashValue for Composite Types
488 // -----------------------------------------------------------------------------
489
490 // AbslHashValue() for hashing pairs
491 template <typename H, typename T1, typename T2>
492 typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
493 H>::type
494 AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
495 return H::combine(std::move(hash_state), p.first, p.second);
496 }
497
498 // hash_tuple()
499 //
500 // Helper function for hashing a tuple. The third argument should
501 // be an index_sequence running from 0 to tuple_size<Tuple> - 1.
502 template <typename H, typename Tuple, size_t... Is>
503 H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
504 return H::combine(std::move(hash_state), std::get<Is>(t)...);
505 }
506
507 // AbslHashValue for hashing tuples
508 template <typename H, typename... Ts>
509 #if defined(_MSC_VER)
510 // This SFINAE gets MSVC confused under some conditions. Let's just disable it
511 // for now.
512 H
513 #else // _MSC_VER
514 typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
515 #endif // _MSC_VER
516 AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
517 return hash_internal::hash_tuple(std::move(hash_state), t,
518 absl::make_index_sequence<sizeof...(Ts)>());
519 }
520
521 // -----------------------------------------------------------------------------
522 // AbslHashValue for Pointers
523 // -----------------------------------------------------------------------------
524
525 // AbslHashValue for hashing unique_ptr
526 template <typename H, typename T, typename D>
527 H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
528 return H::combine(std::move(hash_state), ptr.get());
529 }
530
531 // AbslHashValue for hashing shared_ptr
532 template <typename H, typename T>
533 H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
534 return H::combine(std::move(hash_state), ptr.get());
535 }
536
537 // -----------------------------------------------------------------------------
538 // AbslHashValue for String-Like Types
539 // -----------------------------------------------------------------------------
540
541 // AbslHashValue for hashing strings
542 //
543 // All the string-like types supported here provide the same hash expansion for
544 // the same character sequence. These types are:
545 //
546 // - `absl::Cord`
547 // - `std::string` (and std::basic_string<T, std::char_traits<T>, A> for
548 // any allocator A and any T in {char, wchar_t, char16_t, char32_t})
549 // - `absl::string_view`, `std::string_view`, `std::wstring_view`,
550 // `std::u16string_view`, and `std::u32_string_view`.
551 //
552 // For simplicity, we currently support only strings built on `char`, `wchar_t`,
553 // `char16_t`, or `char32_t`. This support may be broadened, if necessary, but
554 // with some caution - this overload would misbehave in cases where the traits'
555 // `eq()` member isn't equivalent to `==` on the underlying character type.
556 template <typename H>
557 H AbslHashValue(H hash_state, absl::string_view str) {
558 return H::combine(
559 H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
560 str.size());
561 }
562
563 // Support std::wstring, std::u16string and std::u32string.
564 template <typename Char, typename Alloc, typename H,
565 typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
566 std::is_same<Char, char16_t>::value ||
567 std::is_same<Char, char32_t>::value>>
568 H AbslHashValue(
569 H hash_state,
570 const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
571 return H::combine(
572 H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
573 str.size());
574 }
575
576 #ifdef ABSL_HAVE_STD_STRING_VIEW
577
578 // Support std::wstring_view, std::u16string_view and std::u32string_view.
579 template <typename Char, typename H,
580 typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
581 std::is_same<Char, char16_t>::value ||
582 std::is_same<Char, char32_t>::value>>
583 H AbslHashValue(H hash_state, std::basic_string_view<Char> str) {
584 return H::combine(
585 H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
586 str.size());
587 }
588
589 #endif // ABSL_HAVE_STD_STRING_VIEW
590
591 #if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
592 !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) && \
593 (!defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) || \
594 __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 130000)
595
596 #define ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE 1
597
598 // Support std::filesystem::path. The SFINAE is required because some string
599 // types are implicitly convertible to std::filesystem::path.
600 template <typename Path, typename H,
601 typename = absl::enable_if_t<
602 std::is_same_v<Path, std::filesystem::path>>>
603 H AbslHashValue(H hash_state, const Path& path) {
604 // This is implemented by deferring to the standard library to compute the
605 // hash. The standard library requires that for two paths, `p1 == p2`, then
606 // `hash_value(p1) == hash_value(p2)`. `AbslHashValue` has the same
607 // requirement. Since `operator==` does platform specific matching, deferring
608 // to the standard library is the simplest approach.
609 return H::combine(std::move(hash_state), std::filesystem::hash_value(path));
610 }
611
612 #endif // ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE
613
614 // -----------------------------------------------------------------------------
615 // AbslHashValue for Sequence Containers
616 // -----------------------------------------------------------------------------
617
618 // AbslHashValue for hashing std::array
619 template <typename H, typename T, size_t N>
620 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
621 H hash_state, const std::array<T, N>& array) {
622 return H::combine_contiguous(std::move(hash_state), array.data(),
623 array.size());
624 }
625
626 // AbslHashValue for hashing std::deque
627 template <typename H, typename T, typename Allocator>
628 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
629 H hash_state, const std::deque<T, Allocator>& deque) {
630 // TODO(gromer): investigate a more efficient implementation taking
631 // advantage of the chunk structure.
632 for (const auto& t : deque) {
633 hash_state = H::combine(std::move(hash_state), t);
634 }
635 return H::combine(std::move(hash_state), deque.size());
636 }
637
638 // AbslHashValue for hashing std::forward_list
639 template <typename H, typename T, typename Allocator>
640 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
641 H hash_state, const std::forward_list<T, Allocator>& list) {
642 size_t size = 0;
643 for (const T& t : list) {
644 hash_state = H::combine(std::move(hash_state), t);
645 ++size;
646 }
647 return H::combine(std::move(hash_state), size);
648 }
649
650 // AbslHashValue for hashing std::list
651 template <typename H, typename T, typename Allocator>
652 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
653 H hash_state, const std::list<T, Allocator>& list) {
654 for (const auto& t : list) {
655 hash_state = H::combine(std::move(hash_state), t);
656 }
657 return H::combine(std::move(hash_state), list.size());
658 }
659
660 // AbslHashValue for hashing std::vector
661 //
662 // Do not use this for vector<bool> on platforms that have a working
663 // implementation of std::hash. It does not have a .data(), and a fallback for
664 // std::hash<> is most likely faster.
665 template <typename H, typename T, typename Allocator>
666 typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
667 H>::type
668 AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
669 return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
670 vector.size()),
671 vector.size());
672 }
673
674 // AbslHashValue special cases for hashing std::vector<bool>
675
676 #if defined(ABSL_IS_BIG_ENDIAN) && \
677 (defined(__GLIBCXX__) || defined(__GLIBCPP__))
678
679 // std::hash in libstdc++ does not work correctly with vector<bool> on Big
680 // Endian platforms therefore we need to implement a custom AbslHashValue for
681 // it. More details on the bug:
682 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
683 template <typename H, typename T, typename Allocator>
684 typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
685 H>::type
686 AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
687 typename H::AbslInternalPiecewiseCombiner combiner;
688 for (const auto& i : vector) {
689 unsigned char c = static_cast<unsigned char>(i);
690 hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
691 }
692 return H::combine(combiner.finalize(std::move(hash_state)), vector.size());
693 }
694 #else
695 // When not working around the libstdc++ bug above, we still have to contend
696 // with the fact that std::hash<vector<bool>> is often poor quality, hashing
697 // directly on the internal words and on no other state. On these platforms,
698 // vector<bool>{1, 1} and vector<bool>{1, 1, 0} hash to the same value.
699 //
700 // Mixing in the size (as we do in our other vector<> implementations) on top
701 // of the library-provided hash implementation avoids this QOI issue.
702 template <typename H, typename T, typename Allocator>
703 typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
704 H>::type
705 AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
706 return H::combine(std::move(hash_state),
707 std::hash<std::vector<T, Allocator>>{}(vector),
708 vector.size());
709 }
710 #endif
711
712 // -----------------------------------------------------------------------------
713 // AbslHashValue for Ordered Associative Containers
714 // -----------------------------------------------------------------------------
715
716 // AbslHashValue for hashing std::map
717 template <typename H, typename Key, typename T, typename Compare,
718 typename Allocator>
719 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
720 H>::type
721 AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
722 for (const auto& t : map) {
723 hash_state = H::combine(std::move(hash_state), t);
724 }
725 return H::combine(std::move(hash_state), map.size());
726 }
727
728 // AbslHashValue for hashing std::multimap
729 template <typename H, typename Key, typename T, typename Compare,
730 typename Allocator>
731 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
732 H>::type
733 AbslHashValue(H hash_state,
734 const std::multimap<Key, T, Compare, Allocator>& map) {
735 for (const auto& t : map) {
736 hash_state = H::combine(std::move(hash_state), t);
737 }
738 return H::combine(std::move(hash_state), map.size());
739 }
740
741 // AbslHashValue for hashing std::set
742 template <typename H, typename Key, typename Compare, typename Allocator>
743 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
744 H hash_state, const std::set<Key, Compare, Allocator>& set) {
745 for (const auto& t : set) {
746 hash_state = H::combine(std::move(hash_state), t);
747 }
748 return H::combine(std::move(hash_state), set.size());
749 }
750
751 // AbslHashValue for hashing std::multiset
752 template <typename H, typename Key, typename Compare, typename Allocator>
753 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
754 H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
755 for (const auto& t : set) {
756 hash_state = H::combine(std::move(hash_state), t);
757 }
758 return H::combine(std::move(hash_state), set.size());
759 }
760
761 // -----------------------------------------------------------------------------
762 // AbslHashValue for Unordered Associative Containers
763 // -----------------------------------------------------------------------------
764
765 // AbslHashValue for hashing std::unordered_set
766 template <typename H, typename Key, typename Hash, typename KeyEqual,
767 typename Alloc>
768 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
769 H hash_state, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s) {
770 return H::combine(
771 H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
772 s.size());
773 }
774
775 // AbslHashValue for hashing std::unordered_multiset
776 template <typename H, typename Key, typename Hash, typename KeyEqual,
777 typename Alloc>
778 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
779 H hash_state,
780 const std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& s) {
781 return H::combine(
782 H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
783 s.size());
784 }
785
786 // AbslHashValue for hashing std::unordered_set
787 template <typename H, typename Key, typename T, typename Hash,
788 typename KeyEqual, typename Alloc>
789 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
790 H>::type
791 AbslHashValue(H hash_state,
792 const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& s) {
793 return H::combine(
794 H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
795 s.size());
796 }
797
798 // AbslHashValue for hashing std::unordered_multiset
799 template <typename H, typename Key, typename T, typename Hash,
800 typename KeyEqual, typename Alloc>
801 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
802 H>::type
803 AbslHashValue(H hash_state,
804 const std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& s) {
805 return H::combine(
806 H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
807 s.size());
808 }
809
810 // -----------------------------------------------------------------------------
811 // AbslHashValue for Wrapper Types
812 // -----------------------------------------------------------------------------
813
814 // AbslHashValue for hashing std::reference_wrapper
815 template <typename H, typename T>
816 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
817 H hash_state, std::reference_wrapper<T> opt) {
818 return H::combine(std::move(hash_state), opt.get());
819 }
820
821 // AbslHashValue for hashing absl::optional
822 template <typename H, typename T>
823 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
824 H hash_state, const absl::optional<T>& opt) {
825 if (opt) hash_state = H::combine(std::move(hash_state), *opt);
826 return H::combine(std::move(hash_state), opt.has_value());
827 }
828
829 // VariantVisitor
830 template <typename H>
831 struct VariantVisitor {
832 H&& hash_state;
833 template <typename T>
834 H operator()(const T& t) const {
835 return H::combine(std::move(hash_state), t);
836 }
837 };
838
839 // AbslHashValue for hashing absl::variant
840 template <typename H, typename... T>
841 typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
842 AbslHashValue(H hash_state, const absl::variant<T...>& v) {
843 if (!v.valueless_by_exception()) {
844 hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
845 }
846 return H::combine(std::move(hash_state), v.index());
847 }
848
849 // -----------------------------------------------------------------------------
850 // AbslHashValue for Other Types
851 // -----------------------------------------------------------------------------
852
853 // AbslHashValue for hashing std::bitset is not defined on Little Endian
854 // platforms, for the same reason as for vector<bool> (see std::vector above):
855 // It does not expose the raw bytes, and a fallback to std::hash<> is most
856 // likely faster.
857
858 #if defined(ABSL_IS_BIG_ENDIAN) && \
859 (defined(__GLIBCXX__) || defined(__GLIBCPP__))
860 // AbslHashValue for hashing std::bitset
861 //
862 // std::hash in libstdc++ does not work correctly with std::bitset on Big Endian
863 // platforms therefore we need to implement a custom AbslHashValue for it. More
864 // details on the bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
865 template <typename H, size_t N>
866 H AbslHashValue(H hash_state, const std::bitset<N>& set) {
867 typename H::AbslInternalPiecewiseCombiner combiner;
868 for (size_t i = 0; i < N; i++) {
869 unsigned char c = static_cast<unsigned char>(set[i]);
870 hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
871 }
872 return H::combine(combiner.finalize(std::move(hash_state)), N);
873 }
874 #endif
875
876 // -----------------------------------------------------------------------------
877
878 // hash_range_or_bytes()
879 //
880 // Mixes all values in the range [data, data+size) into the hash state.
881 // This overload accepts only uniquely-represented types, and hashes them by
882 // hashing the entire range of bytes.
883 template <typename H, typename T>
884 typename std::enable_if<is_uniquely_represented<T>::value, H>::type
885 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
886 const auto* bytes = reinterpret_cast<const unsigned char*>(data);
887 return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
888 }
889
890 // hash_range_or_bytes()
891 template <typename H, typename T>
892 typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
893 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
894 for (const auto end = data + size; data < end; ++data) {
895 hash_state = H::combine(std::move(hash_state), *data);
896 }
897 return hash_state;
898 }
899
900 #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
901 ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
902 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
903 #else
904 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
905 #endif
906
907 // HashSelect
908 //
909 // Type trait to select the appropriate hash implementation to use.
910 // HashSelect::type<T> will give the proper hash implementation, to be invoked
911 // as:
912 // HashSelect::type<T>::Invoke(state, value)
913 // Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
914 // valid `Invoke` function. Types that are not hashable will have a ::value of
915 // `false`.
916 struct HashSelect {
917 private:
918 struct State : HashStateBase<State> {
919 static State combine_contiguous(State hash_state, const unsigned char*,
920 size_t);
921 using State::HashStateBase::combine_contiguous;
922 };
923
924 struct UniquelyRepresentedProbe {
925 template <typename H, typename T>
926 static auto Invoke(H state, const T& value)
927 -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
928 return hash_internal::hash_bytes(std::move(state), value);
929 }
930 };
931
932 struct HashValueProbe {
933 template <typename H, typename T>
934 static auto Invoke(H state, const T& value) -> absl::enable_if_t<
935 std::is_same<H,
936 decltype(AbslHashValue(std::move(state), value))>::value,
937 H> {
938 return AbslHashValue(std::move(state), value);
939 }
940 };
941
942 struct LegacyHashProbe {
943 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
944 template <typename H, typename T>
945 static auto Invoke(H state, const T& value) -> absl::enable_if_t<
946 std::is_convertible<
947 decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
948 size_t>::value,
949 H> {
950 return hash_internal::hash_bytes(
951 std::move(state),
952 ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
953 }
954 #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
955 };
956
957 struct StdHashProbe {
958 template <typename H, typename T>
959 static auto Invoke(H state, const T& value)
960 -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
961 return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
962 }
963 };
964
965 template <typename Hash, typename T>
966 struct Probe : Hash {
967 private:
968 template <typename H, typename = decltype(H::Invoke(
969 std::declval<State>(), std::declval<const T&>()))>
970 static std::true_type Test(int);
971 template <typename U>
972 static std::false_type Test(char);
973
974 public:
975 static constexpr bool value = decltype(Test<Hash>(0))::value;
976 };
977
978 public:
979 // Probe each implementation in order.
980 // disjunction provides short circuiting wrt instantiation.
981 template <typename T>
982 using Apply = absl::disjunction< //
983 Probe<UniquelyRepresentedProbe, T>, //
984 Probe<HashValueProbe, T>, //
985 Probe<LegacyHashProbe, T>, //
986 Probe<StdHashProbe, T>, //
987 std::false_type>;
988 };
989
990 template <typename T>
991 struct is_hashable
992 : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
993
994 // MixingHashState
995 class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
996 // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
997 // We use the intrinsic when available to improve performance.
998 #ifdef ABSL_HAVE_INTRINSIC_INT128
999 using uint128 = __uint128_t;
1000 #else // ABSL_HAVE_INTRINSIC_INT128
1001 using uint128 = absl::uint128;
1002 #endif // ABSL_HAVE_INTRINSIC_INT128
1003
1004 static constexpr uint64_t kMul =
1005 sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51}
1006 : uint64_t{0x9ddfea08eb382d69};
1007
1008 template <typename T>
1009 using IntegralFastPath =
1010 conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
1011
1012 public:
1013 // Move only
1014 MixingHashState(MixingHashState&&) = default;
1015 MixingHashState& operator=(MixingHashState&&) = default;
1016
1017 // MixingHashState::combine_contiguous()
1018 //
1019 // Fundamental base case for hash recursion: mixes the given range of bytes
1020 // into the hash state.
1021 static MixingHashState combine_contiguous(MixingHashState hash_state,
1022 const unsigned char* first,
1023 size_t size) {
1024 return MixingHashState(
1025 CombineContiguousImpl(hash_state.state_, first, size,
1026 std::integral_constant<int, sizeof(size_t)>{}));
1027 }
1028 using MixingHashState::HashStateBase::combine_contiguous;
1029
1030 // MixingHashState::hash()
1031 //
1032 // For performance reasons in non-opt mode, we specialize this for
1033 // integral types.
1034 // Otherwise we would be instantiating and calling dozens of functions for
1035 // something that is just one multiplication and a couple xor's.
1036 // The result should be the same as running the whole algorithm, but faster.
1037 template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
1038 static size_t hash(T value) {
1039 return static_cast<size_t>(
1040 Mix(Seed(), static_cast<std::make_unsigned_t<T>>(value)));
1041 }
1042
1043 // Overload of MixingHashState::hash()
1044 template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
1045 static size_t hash(const T& value) {
1046 return static_cast<size_t>(combine(MixingHashState{}, value).state_);
1047 }
1048
1049 private:
1050 // Invoked only once for a given argument; that plus the fact that this is
1051 // move-only ensures that there is only one non-moved-from object.
1052 MixingHashState() : state_(Seed()) {}
1053
1054 friend class MixingHashState::HashStateBase;
1055
1056 template <typename CombinerT>
1057 static MixingHashState RunCombineUnordered(MixingHashState state,
1058 CombinerT combiner) {
1059 uint64_t unordered_state = 0;
1060 combiner(MixingHashState{}, [&](MixingHashState& inner_state) {
1061 // Add the hash state of the element to the running total, but mix the
1062 // carry bit back into the low bit. This in intended to avoid losing
1063 // entropy to overflow, especially when unordered_multisets contain
1064 // multiple copies of the same value.
1065 auto element_state = inner_state.state_;
1066 unordered_state += element_state;
1067 if (unordered_state < element_state) {
1068 ++unordered_state;
1069 }
1070 inner_state = MixingHashState{};
1071 });
1072 return MixingHashState::combine(std::move(state), unordered_state);
1073 }
1074
1075 // Allow the HashState type-erasure implementation to invoke
1076 // RunCombinedUnordered() directly.
1077 friend class absl::HashState;
1078
1079 // Workaround for MSVC bug.
1080 // We make the type copyable to fix the calling convention, even though we
1081 // never actually copy it. Keep it private to not affect the public API of the
1082 // type.
1083 MixingHashState(const MixingHashState&) = default;
1084
1085 explicit MixingHashState(uint64_t state) : state_(state) {}
1086
1087 // Implementation of the base case for combine_contiguous where we actually
1088 // mix the bytes into the state.
1089 // Dispatch to different implementations of the combine_contiguous depending
1090 // on the value of `sizeof(size_t)`.
1091 static uint64_t CombineContiguousImpl(uint64_t state,
1092 const unsigned char* first, size_t len,
1093 std::integral_constant<int, 4>
1094 /* sizeof_size_t */);
1095 static uint64_t CombineContiguousImpl(uint64_t state,
1096 const unsigned char* first, size_t len,
1097 std::integral_constant<int, 8>
1098 /* sizeof_size_t */);
1099
1100 // Slow dispatch path for calls to CombineContiguousImpl with a size argument
1101 // larger than PiecewiseChunkSize(). Has the same effect as calling
1102 // CombineContiguousImpl() repeatedly with the chunk stride size.
1103 static uint64_t CombineLargeContiguousImpl32(uint64_t state,
1104 const unsigned char* first,
1105 size_t len);
1106 static uint64_t CombineLargeContiguousImpl64(uint64_t state,
1107 const unsigned char* first,
1108 size_t len);
1109
1110 // Reads 9 to 16 bytes from p.
1111 // The least significant 8 bytes are in .first, the rest (zero padded) bytes
1112 // are in .second.
1113 static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
1114 size_t len) {
1115 uint64_t low_mem = absl::base_internal::UnalignedLoad64(p);
1116 uint64_t high_mem = absl::base_internal::UnalignedLoad64(p + len - 8);
1117 #ifdef ABSL_IS_LITTLE_ENDIAN
1118 uint64_t most_significant = high_mem;
1119 uint64_t least_significant = low_mem;
1120 #else
1121 uint64_t most_significant = low_mem;
1122 uint64_t least_significant = high_mem;
1123 #endif
1124 return {least_significant, most_significant};
1125 }
1126
1127 // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
1128 static uint64_t Read4To8(const unsigned char* p, size_t len) {
1129 uint32_t low_mem = absl::base_internal::UnalignedLoad32(p);
1130 uint32_t high_mem = absl::base_internal::UnalignedLoad32(p + len - 4);
1131 #ifdef ABSL_IS_LITTLE_ENDIAN
1132 uint32_t most_significant = high_mem;
1133 uint32_t least_significant = low_mem;
1134 #else
1135 uint32_t most_significant = low_mem;
1136 uint32_t least_significant = high_mem;
1137 #endif
1138 return (static_cast<uint64_t>(most_significant) << (len - 4) * 8) |
1139 least_significant;
1140 }
1141
1142 // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
1143 static uint32_t Read1To3(const unsigned char* p, size_t len) {
1144 // The trick used by this implementation is to avoid branches if possible.
1145 unsigned char mem0 = p[0];
1146 unsigned char mem1 = p[len / 2];
1147 unsigned char mem2 = p[len - 1];
1148 #ifdef ABSL_IS_LITTLE_ENDIAN
1149 unsigned char significant2 = mem2;
1150 unsigned char significant1 = mem1;
1151 unsigned char significant0 = mem0;
1152 #else
1153 unsigned char significant2 = mem0;
1154 unsigned char significant1 = len == 2 ? mem0 : mem1;
1155 unsigned char significant0 = mem2;
1156 #endif
1157 return static_cast<uint32_t>(significant0 | //
1158 (significant1 << (len / 2 * 8)) | //
1159 (significant2 << ((len - 1) * 8)));
1160 }
1161
1162 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
1163 // Though the 128-bit product on AArch64 needs two instructions, it is
1164 // still a good balance between speed and hash quality.
1165 using MultType =
1166 absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
1167 // We do the addition in 64-bit space to make sure the 128-bit
1168 // multiplication is fast. If we were to do it as MultType the compiler has
1169 // to assume that the high word is non-zero and needs to perform 2
1170 // multiplications instead of one.
1171 MultType m = state + v;
1172 m *= kMul;
1173 return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
1174 }
1175
1176 // An extern to avoid bloat on a direct call to LowLevelHash() with fixed
1177 // values for both the seed and salt parameters.
1178 static uint64_t LowLevelHashImpl(const unsigned char* data, size_t len);
1179
1180 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Hash64(const unsigned char* data,
1181 size_t len) {
1182 #ifdef ABSL_HAVE_INTRINSIC_INT128
1183 return LowLevelHashImpl(data, len);
1184 #else
1185 return hash_internal::CityHash64(reinterpret_cast<const char*>(data), len);
1186 #endif
1187 }
1188
1189 // Seed()
1190 //
1191 // A non-deterministic seed.
1192 //
1193 // The current purpose of this seed is to generate non-deterministic results
1194 // and prevent having users depend on the particular hash values.
1195 // It is not meant as a security feature right now, but it leaves the door
1196 // open to upgrade it to a true per-process random seed. A true random seed
1197 // costs more and we don't need to pay for that right now.
1198 //
1199 // On platforms with ASLR, we take advantage of it to make a per-process
1200 // random value.
1201 // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
1202 //
1203 // On other platforms this is still going to be non-deterministic but most
1204 // probably per-build and not per-process.
1205 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
1206 #if (!defined(__clang__) || __clang_major__ > 11) && \
1207 (!defined(__apple_build_version__) || \
1208 __apple_build_version__ >= 19558921) // Xcode 12
1209 return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&kSeed));
1210 #else
1211 // Workaround the absence of
1212 // https://github.com/llvm/llvm-project/commit/bc15bf66dcca76cc06fe71fca35b74dc4d521021.
1213 return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
1214 #endif
1215 }
1216 static const void* const kSeed;
1217
1218 uint64_t state_;
1219 };
1220
1221 // MixingHashState::CombineContiguousImpl()
1222 inline uint64_t MixingHashState::CombineContiguousImpl(
1223 uint64_t state, const unsigned char* first, size_t len,
1224 std::integral_constant<int, 4> /* sizeof_size_t */) {
1225 // For large values we use CityHash, for small ones we just use a
1226 // multiplicative hash.
1227 uint64_t v;
1228 if (len > 8) {
1229 if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
1230 return CombineLargeContiguousImpl32(state, first, len);
1231 }
1232 v = hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
1233 } else if (len >= 4) {
1234 v = Read4To8(first, len);
1235 } else if (len > 0) {
1236 v = Read1To3(first, len);
1237 } else {
1238 // Empty ranges have no effect.
1239 return state;
1240 }
1241 return Mix(state, v);
1242 }
1243
1244 // Overload of MixingHashState::CombineContiguousImpl()
1245 inline uint64_t MixingHashState::CombineContiguousImpl(
1246 uint64_t state, const unsigned char* first, size_t len,
1247 std::integral_constant<int, 8> /* sizeof_size_t */) {
1248 // For large values we use LowLevelHash or CityHash depending on the platform,
1249 // for small ones we just use a multiplicative hash.
1250 uint64_t v;
1251 if (len > 16) {
1252 if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
1253 return CombineLargeContiguousImpl64(state, first, len);
1254 }
1255 v = Hash64(first, len);
1256 } else if (len > 8) {
1257 // This hash function was constructed by the ML-driven algorithm discovery
1258 // using reinforcement learning. We fed the agent lots of inputs from
1259 // microbenchmarks, SMHasher, low hamming distance from generated inputs and
1260 // picked up the one that was good on micro and macrobenchmarks.
1261 auto p = Read9To16(first, len);
1262 uint64_t lo = p.first;
1263 uint64_t hi = p.second;
1264 // Rotation by 53 was found to be most often useful when discovering these
1265 // hashing algorithms with ML techniques.
1266 lo = absl::rotr(lo, 53);
1267 state += kMul;
1268 lo += state;
1269 state ^= hi;
1270 uint128 m = state;
1271 m *= lo;
1272 return static_cast<uint64_t>(m ^ (m >> 64));
1273 } else if (len >= 4) {
1274 v = Read4To8(first, len);
1275 } else if (len > 0) {
1276 v = Read1To3(first, len);
1277 } else {
1278 // Empty ranges have no effect.
1279 return state;
1280 }
1281 return Mix(state, v);
1282 }
1283
1284 struct AggregateBarrier {};
1285
1286 // HashImpl
1287
1288 // Add a private base class to make sure this type is not an aggregate.
1289 // Aggregates can be aggregate initialized even if the default constructor is
1290 // deleted.
1291 struct PoisonedHash : private AggregateBarrier {
1292 PoisonedHash() = delete;
1293 PoisonedHash(const PoisonedHash&) = delete;
1294 PoisonedHash& operator=(const PoisonedHash&) = delete;
1295 };
1296
1297 template <typename T>
1298 struct HashImpl {
1299 size_t operator()(const T& value) const {
1300 return MixingHashState::hash(value);
1301 }
1302 };
1303
1304 template <typename T>
1305 struct Hash
1306 : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
1307
1308 template <typename H>
1309 template <typename T, typename... Ts>
1310 H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
1311 return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
1312 std::move(state), value),
1313 values...);
1314 }
1315
1316 // HashStateBase::combine_contiguous()
1317 template <typename H>
1318 template <typename T>
1319 H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
1320 return hash_internal::hash_range_or_bytes(std::move(state), data, size);
1321 }
1322
1323 // HashStateBase::combine_unordered()
1324 template <typename H>
1325 template <typename I>
1326 H HashStateBase<H>::combine_unordered(H state, I begin, I end) {
1327 return H::RunCombineUnordered(std::move(state),
1328 CombineUnorderedCallback<I>{begin, end});
1329 }
1330
1331 // HashStateBase::PiecewiseCombiner::add_buffer()
1332 template <typename H>
1333 H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
1334 size_t size) {
1335 if (position_ + size < PiecewiseChunkSize()) {
1336 // This partial chunk does not fill our existing buffer
1337 memcpy(buf_ + position_, data, size);
1338 position_ += size;
1339 return state;
1340 }
1341
1342 // If the buffer is partially filled we need to complete the buffer
1343 // and hash it.
1344 if (position_ != 0) {
1345 const size_t bytes_needed = PiecewiseChunkSize() - position_;
1346 memcpy(buf_ + position_, data, bytes_needed);
1347 state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
1348 data += bytes_needed;
1349 size -= bytes_needed;
1350 }
1351
1352 // Hash whatever chunks we can without copying
1353 while (size >= PiecewiseChunkSize()) {
1354 state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
1355 data += PiecewiseChunkSize();
1356 size -= PiecewiseChunkSize();
1357 }
1358 // Fill the buffer with the remainder
1359 memcpy(buf_, data, size);
1360 position_ = size;
1361 return state;
1362 }
1363
1364 // HashStateBase::PiecewiseCombiner::finalize()
1365 template <typename H>
1366 H PiecewiseCombiner::finalize(H state) {
1367 // Hash the remainder left in the buffer, which may be empty
1368 return H::combine_contiguous(std::move(state), buf_, position_);
1369 }
1370
1371 } // namespace hash_internal
1372 ABSL_NAMESPACE_END
1373 } // namespace absl
1374
1375 #endif // ABSL_HASH_INTERNAL_HASH_H_
1376