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