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 #include <algorithm>
23 #include <array>
24 #include <cmath>
25 #include <cstring>
26 #include <deque>
27 #include <forward_list>
28 #include <functional>
29 #include <iterator>
30 #include <limits>
31 #include <list>
32 #include <map>
33 #include <memory>
34 #include <set>
35 #include <string>
36 #include <tuple>
37 #include <type_traits>
38 #include <utility>
39 #include <vector>
40
41 #include "absl/base/config.h"
42 #include "absl/base/internal/unaligned_access.h"
43 #include "absl/base/port.h"
44 #include "absl/container/fixed_array.h"
45 #include "absl/hash/internal/wyhash.h"
46 #include "absl/meta/type_traits.h"
47 #include "absl/numeric/int128.h"
48 #include "absl/strings/string_view.h"
49 #include "absl/types/optional.h"
50 #include "absl/types/variant.h"
51 #include "absl/utility/utility.h"
52 #include "absl/hash/internal/city.h"
53
54 namespace absl {
55 ABSL_NAMESPACE_BEGIN
56 namespace hash_internal {
57
58 // Internal detail: Large buffers are hashed in smaller chunks. This function
59 // returns the size of these chunks.
PiecewiseChunkSize()60 constexpr size_t PiecewiseChunkSize() { return 1024; }
61
62 // PiecewiseCombiner
63 //
64 // PiecewiseCombiner is an internal-only helper class for hashing a piecewise
65 // buffer of `char` or `unsigned char` as though it were contiguous. This class
66 // provides two methods:
67 //
68 // H add_buffer(state, data, size)
69 // H finalize(state)
70 //
71 // `add_buffer` can be called zero or more times, followed by a single call to
72 // `finalize`. This will produce the same hash expansion as concatenating each
73 // buffer piece into a single contiguous buffer, and passing this to
74 // `H::combine_contiguous`.
75 //
76 // Example usage:
77 // PiecewiseCombiner combiner;
78 // for (const auto& piece : pieces) {
79 // state = combiner.add_buffer(std::move(state), piece.data, piece.size);
80 // }
81 // return combiner.finalize(std::move(state));
82 class PiecewiseCombiner {
83 public:
PiecewiseCombiner()84 PiecewiseCombiner() : position_(0) {}
85 PiecewiseCombiner(const PiecewiseCombiner&) = delete;
86 PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
87
88 // PiecewiseCombiner::add_buffer()
89 //
90 // Appends the given range of bytes to the sequence to be hashed, which may
91 // modify the provided hash state.
92 template <typename H>
93 H add_buffer(H state, const unsigned char* data, size_t size);
94 template <typename H>
add_buffer(H state,const char * data,size_t size)95 H add_buffer(H state, const char* data, size_t size) {
96 return add_buffer(std::move(state),
97 reinterpret_cast<const unsigned char*>(data), size);
98 }
99
100 // PiecewiseCombiner::finalize()
101 //
102 // Finishes combining the hash sequence, which may may modify the provided
103 // hash state.
104 //
105 // Once finalize() is called, add_buffer() may no longer be called. The
106 // resulting hash state will be the same as if the pieces passed to
107 // add_buffer() were concatenated into a single flat buffer, and then provided
108 // to H::combine_contiguous().
109 template <typename H>
110 H finalize(H state);
111
112 private:
113 unsigned char buf_[PiecewiseChunkSize()];
114 size_t position_;
115 };
116
117 // HashStateBase
118 //
119 // A hash state object represents an intermediate state in the computation
120 // of an unspecified hash algorithm. `HashStateBase` provides a CRTP style
121 // base class for hash state implementations. Developers adding type support
122 // for `absl::Hash` should not rely on any parts of the state object other than
123 // the following member functions:
124 //
125 // * HashStateBase::combine()
126 // * HashStateBase::combine_contiguous()
127 //
128 // A derived hash state class of type `H` must provide a static member function
129 // with a signature similar to the following:
130 //
131 // `static H combine_contiguous(H state, const unsigned char*, size_t)`.
132 //
133 // `HashStateBase` will provide a complete implementation for a hash state
134 // object in terms of this method.
135 //
136 // Example:
137 //
138 // // Use CRTP to define your derived class.
139 // struct MyHashState : HashStateBase<MyHashState> {
140 // static H combine_contiguous(H state, const unsigned char*, size_t);
141 // using MyHashState::HashStateBase::combine;
142 // using MyHashState::HashStateBase::combine_contiguous;
143 // };
144 template <typename H>
145 class HashStateBase {
146 public:
147 // HashStateBase::combine()
148 //
149 // Combines an arbitrary number of values into a hash state, returning the
150 // updated state.
151 //
152 // Each of the value types `T` must be separately hashable by the Abseil
153 // hashing framework.
154 //
155 // NOTE:
156 //
157 // state = H::combine(std::move(state), value1, value2, value3);
158 //
159 // is guaranteed to produce the same hash expansion as:
160 //
161 // state = H::combine(std::move(state), value1);
162 // state = H::combine(std::move(state), value2);
163 // state = H::combine(std::move(state), value3);
164 template <typename T, typename... Ts>
165 static H combine(H state, const T& value, const Ts&... values);
combine(H state)166 static H combine(H state) { return state; }
167
168 // HashStateBase::combine_contiguous()
169 //
170 // Combines a contiguous array of `size` elements into a hash state, returning
171 // the updated state.
172 //
173 // NOTE:
174 //
175 // state = H::combine_contiguous(std::move(state), data, size);
176 //
177 // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
178 // perform internal optimizations). If you need this guarantee, use the
179 // for-loop instead.
180 template <typename T>
181 static H combine_contiguous(H state, const T* data, size_t size);
182
183 using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
184 };
185
186 // is_uniquely_represented
187 //
188 // `is_uniquely_represented<T>` is a trait class that indicates whether `T`
189 // is uniquely represented.
190 //
191 // A type is "uniquely represented" if two equal values of that type are
192 // guaranteed to have the same bytes in their underlying storage. In other
193 // words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
194 // zero. This property cannot be detected automatically, so this trait is false
195 // by default, but can be specialized by types that wish to assert that they are
196 // uniquely represented. This makes them eligible for certain optimizations.
197 //
198 // If you have any doubt whatsoever, do not specialize this template.
199 // The default is completely safe, and merely disables some optimizations
200 // that will not matter for most types. Specializing this template,
201 // on the other hand, can be very hazardous.
202 //
203 // To be uniquely represented, a type must not have multiple ways of
204 // representing the same value; for example, float and double are not
205 // uniquely represented, because they have distinct representations for
206 // +0 and -0. Furthermore, the type's byte representation must consist
207 // solely of user-controlled data, with no padding bits and no compiler-
208 // controlled data such as vptrs or sanitizer metadata. This is usually
209 // very difficult to guarantee, because in most cases the compiler can
210 // insert data and padding bits at its own discretion.
211 //
212 // If you specialize this template for a type `T`, you must do so in the file
213 // that defines that type (or in this file). If you define that specialization
214 // anywhere else, `is_uniquely_represented<T>` could have different meanings
215 // in different places.
216 //
217 // The Enable parameter is meaningless; it is provided as a convenience,
218 // to support certain SFINAE techniques when defining specializations.
219 template <typename T, typename Enable = void>
220 struct is_uniquely_represented : std::false_type {};
221
222 // is_uniquely_represented<unsigned char>
223 //
224 // unsigned char is a synonym for "byte", so it is guaranteed to be
225 // uniquely represented.
226 template <>
227 struct is_uniquely_represented<unsigned char> : std::true_type {};
228
229 // is_uniquely_represented for non-standard integral types
230 //
231 // Integral types other than bool should be uniquely represented on any
232 // platform that this will plausibly be ported to.
233 template <typename Integral>
234 struct is_uniquely_represented<
235 Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
236 : std::true_type {};
237
238 // is_uniquely_represented<bool>
239 //
240 //
241 template <>
242 struct is_uniquely_represented<bool> : std::false_type {};
243
244 // hash_bytes()
245 //
246 // Convenience function that combines `hash_state` with the byte representation
247 // of `value`.
248 template <typename H, typename T>
249 H hash_bytes(H hash_state, const T& value) {
250 const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
251 return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
252 }
253
254 // -----------------------------------------------------------------------------
255 // AbslHashValue for Basic Types
256 // -----------------------------------------------------------------------------
257
258 // Note: Default `AbslHashValue` implementations live in `hash_internal`. This
259 // allows us to block lexical scope lookup when doing an unqualified call to
260 // `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
261 // only be found via ADL.
262
263 // AbslHashValue() for hashing bool values
264 //
265 // We use SFINAE to ensure that this overload only accepts bool, not types that
266 // are convertible to bool.
267 template <typename H, typename B>
268 typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
269 H hash_state, B value) {
270 return H::combine(std::move(hash_state),
271 static_cast<unsigned char>(value ? 1 : 0));
272 }
273
274 // AbslHashValue() for hashing enum values
275 template <typename H, typename Enum>
276 typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
277 H hash_state, Enum e) {
278 // In practice, we could almost certainly just invoke hash_bytes directly,
279 // but it's possible that a sanitizer might one day want to
280 // store data in the unused bits of an enum. To avoid that risk, we
281 // convert to the underlying type before hashing. Hopefully this will get
282 // optimized away; if not, we can reopen discussion with c-toolchain-team.
283 return H::combine(std::move(hash_state),
284 static_cast<typename std::underlying_type<Enum>::type>(e));
285 }
286 // AbslHashValue() for hashing floating-point values
287 template <typename H, typename Float>
288 typename std::enable_if<std::is_same<Float, float>::value ||
289 std::is_same<Float, double>::value,
290 H>::type
291 AbslHashValue(H hash_state, Float value) {
292 return hash_internal::hash_bytes(std::move(hash_state),
293 value == 0 ? 0 : value);
294 }
295
296 // Long double has the property that it might have extra unused bytes in it.
297 // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
298 // of it. This means we can't use hash_bytes on a long double and have to
299 // convert it to something else first.
300 template <typename H, typename LongDouble>
301 typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
302 AbslHashValue(H hash_state, LongDouble value) {
303 const int category = std::fpclassify(value);
304 switch (category) {
305 case FP_INFINITE:
306 // Add the sign bit to differentiate between +Inf and -Inf
307 hash_state = H::combine(std::move(hash_state), std::signbit(value));
308 break;
309
310 case FP_NAN:
311 case FP_ZERO:
312 default:
313 // Category is enough for these.
314 break;
315
316 case FP_NORMAL:
317 case FP_SUBNORMAL:
318 // We can't convert `value` directly to double because this would have
319 // undefined behavior if the value is out of range.
320 // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
321 // guaranteed to be in range for `double`. The truncation is
322 // implementation defined, but that works as long as it is deterministic.
323 int exp;
324 auto mantissa = static_cast<double>(std::frexp(value, &exp));
325 hash_state = H::combine(std::move(hash_state), mantissa, exp);
326 }
327
328 return H::combine(std::move(hash_state), category);
329 }
330
331 // AbslHashValue() for hashing pointers
332 template <typename H, typename T>
333 H AbslHashValue(H hash_state, T* ptr) {
334 auto v = reinterpret_cast<uintptr_t>(ptr);
335 // Due to alignment, pointers tend to have low bits as zero, and the next few
336 // bits follow a pattern since they are also multiples of some base value.
337 // Mixing the pointer twice helps prevent stuck low bits for certain alignment
338 // values.
339 return H::combine(std::move(hash_state), v, v);
340 }
341
342 // AbslHashValue() for hashing nullptr_t
343 template <typename H>
344 H AbslHashValue(H hash_state, std::nullptr_t) {
345 return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
346 }
347
348 // -----------------------------------------------------------------------------
349 // AbslHashValue for Composite Types
350 // -----------------------------------------------------------------------------
351
352 // is_hashable()
353 //
354 // Trait class which returns true if T is hashable by the absl::Hash framework.
355 // Used for the AbslHashValue implementations for composite types below.
356 template <typename T>
357 struct is_hashable;
358
359 // AbslHashValue() for hashing pairs
360 template <typename H, typename T1, typename T2>
361 typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
362 H>::type
363 AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
364 return H::combine(std::move(hash_state), p.first, p.second);
365 }
366
367 // hash_tuple()
368 //
369 // Helper function for hashing a tuple. The third argument should
370 // be an index_sequence running from 0 to tuple_size<Tuple> - 1.
371 template <typename H, typename Tuple, size_t... Is>
372 H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
373 return H::combine(std::move(hash_state), std::get<Is>(t)...);
374 }
375
376 // AbslHashValue for hashing tuples
377 template <typename H, typename... Ts>
378 #if defined(_MSC_VER)
379 // This SFINAE gets MSVC confused under some conditions. Let's just disable it
380 // for now.
381 H
382 #else // _MSC_VER
383 typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
384 #endif // _MSC_VER
385 AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
386 return hash_internal::hash_tuple(std::move(hash_state), t,
387 absl::make_index_sequence<sizeof...(Ts)>());
388 }
389
390 // -----------------------------------------------------------------------------
391 // AbslHashValue for Pointers
392 // -----------------------------------------------------------------------------
393
394 // AbslHashValue for hashing unique_ptr
395 template <typename H, typename T, typename D>
396 H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
397 return H::combine(std::move(hash_state), ptr.get());
398 }
399
400 // AbslHashValue for hashing shared_ptr
401 template <typename H, typename T>
402 H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
403 return H::combine(std::move(hash_state), ptr.get());
404 }
405
406 // -----------------------------------------------------------------------------
407 // AbslHashValue for String-Like Types
408 // -----------------------------------------------------------------------------
409
410 // AbslHashValue for hashing strings
411 //
412 // All the string-like types supported here provide the same hash expansion for
413 // the same character sequence. These types are:
414 //
415 // - `absl::Cord`
416 // - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for
417 // any allocator A)
418 // - `absl::string_view` and `std::string_view`
419 //
420 // For simplicity, we currently support only `char` strings. This support may
421 // be broadened, if necessary, but with some caution - this overload would
422 // misbehave in cases where the traits' `eq()` member isn't equivalent to `==`
423 // on the underlying character type.
424 template <typename H>
425 H AbslHashValue(H hash_state, absl::string_view str) {
426 return H::combine(
427 H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
428 str.size());
429 }
430
431 // Support std::wstring, std::u16string and std::u32string.
432 template <typename Char, typename Alloc, typename H,
433 typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
434 std::is_same<Char, char16_t>::value ||
435 std::is_same<Char, char32_t>::value>>
436 H AbslHashValue(
437 H hash_state,
438 const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
439 return H::combine(
440 H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
441 str.size());
442 }
443
444 // -----------------------------------------------------------------------------
445 // AbslHashValue for Sequence Containers
446 // -----------------------------------------------------------------------------
447
448 // AbslHashValue for hashing std::array
449 template <typename H, typename T, size_t N>
450 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
451 H hash_state, const std::array<T, N>& array) {
452 return H::combine_contiguous(std::move(hash_state), array.data(),
453 array.size());
454 }
455
456 // AbslHashValue for hashing std::deque
457 template <typename H, typename T, typename Allocator>
458 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
459 H hash_state, const std::deque<T, Allocator>& deque) {
460 // TODO(gromer): investigate a more efficient implementation taking
461 // advantage of the chunk structure.
462 for (const auto& t : deque) {
463 hash_state = H::combine(std::move(hash_state), t);
464 }
465 return H::combine(std::move(hash_state), deque.size());
466 }
467
468 // AbslHashValue for hashing std::forward_list
469 template <typename H, typename T, typename Allocator>
470 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
471 H hash_state, const std::forward_list<T, Allocator>& list) {
472 size_t size = 0;
473 for (const T& t : list) {
474 hash_state = H::combine(std::move(hash_state), t);
475 ++size;
476 }
477 return H::combine(std::move(hash_state), size);
478 }
479
480 // AbslHashValue for hashing std::list
481 template <typename H, typename T, typename Allocator>
482 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
483 H hash_state, const std::list<T, Allocator>& list) {
484 for (const auto& t : list) {
485 hash_state = H::combine(std::move(hash_state), t);
486 }
487 return H::combine(std::move(hash_state), list.size());
488 }
489
490 // AbslHashValue for hashing std::vector
491 //
492 // Do not use this for vector<bool>. It does not have a .data(), and a fallback
493 // for std::hash<> is most likely faster.
494 template <typename H, typename T, typename Allocator>
495 typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
496 H>::type
497 AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
498 return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
499 vector.size()),
500 vector.size());
501 }
502
503 // -----------------------------------------------------------------------------
504 // AbslHashValue for Ordered Associative Containers
505 // -----------------------------------------------------------------------------
506
507 // AbslHashValue for hashing std::map
508 template <typename H, typename Key, typename T, typename Compare,
509 typename Allocator>
510 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
511 H>::type
512 AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
513 for (const auto& t : map) {
514 hash_state = H::combine(std::move(hash_state), t);
515 }
516 return H::combine(std::move(hash_state), map.size());
517 }
518
519 // AbslHashValue for hashing std::multimap
520 template <typename H, typename Key, typename T, typename Compare,
521 typename Allocator>
522 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
523 H>::type
524 AbslHashValue(H hash_state,
525 const std::multimap<Key, T, Compare, Allocator>& map) {
526 for (const auto& t : map) {
527 hash_state = H::combine(std::move(hash_state), t);
528 }
529 return H::combine(std::move(hash_state), map.size());
530 }
531
532 // AbslHashValue for hashing std::set
533 template <typename H, typename Key, typename Compare, typename Allocator>
534 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
535 H hash_state, const std::set<Key, Compare, Allocator>& set) {
536 for (const auto& t : set) {
537 hash_state = H::combine(std::move(hash_state), t);
538 }
539 return H::combine(std::move(hash_state), set.size());
540 }
541
542 // AbslHashValue for hashing std::multiset
543 template <typename H, typename Key, typename Compare, typename Allocator>
544 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
545 H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
546 for (const auto& t : set) {
547 hash_state = H::combine(std::move(hash_state), t);
548 }
549 return H::combine(std::move(hash_state), set.size());
550 }
551
552 // -----------------------------------------------------------------------------
553 // AbslHashValue for Wrapper Types
554 // -----------------------------------------------------------------------------
555
556 // AbslHashValue for hashing std::reference_wrapper
557 template <typename H, typename T>
558 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
559 H hash_state, std::reference_wrapper<T> opt) {
560 return H::combine(std::move(hash_state), opt.get());
561 }
562
563 // AbslHashValue for hashing absl::optional
564 template <typename H, typename T>
565 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
566 H hash_state, const absl::optional<T>& opt) {
567 if (opt) hash_state = H::combine(std::move(hash_state), *opt);
568 return H::combine(std::move(hash_state), opt.has_value());
569 }
570
571 // VariantVisitor
572 template <typename H>
573 struct VariantVisitor {
574 H&& hash_state;
575 template <typename T>
576 H operator()(const T& t) const {
577 return H::combine(std::move(hash_state), t);
578 }
579 };
580
581 // AbslHashValue for hashing absl::variant
582 template <typename H, typename... T>
583 typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
584 AbslHashValue(H hash_state, const absl::variant<T...>& v) {
585 if (!v.valueless_by_exception()) {
586 hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
587 }
588 return H::combine(std::move(hash_state), v.index());
589 }
590
591 // -----------------------------------------------------------------------------
592 // AbslHashValue for Other Types
593 // -----------------------------------------------------------------------------
594
595 // AbslHashValue for hashing std::bitset is not defined, for the same reason as
596 // for vector<bool> (see std::vector above): It does not expose the raw bytes,
597 // and a fallback to std::hash<> is most likely faster.
598
599 // -----------------------------------------------------------------------------
600
601 // hash_range_or_bytes()
602 //
603 // Mixes all values in the range [data, data+size) into the hash state.
604 // This overload accepts only uniquely-represented types, and hashes them by
605 // hashing the entire range of bytes.
606 template <typename H, typename T>
607 typename std::enable_if<is_uniquely_represented<T>::value, H>::type
608 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
609 const auto* bytes = reinterpret_cast<const unsigned char*>(data);
610 return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
611 }
612
613 // hash_range_or_bytes()
614 template <typename H, typename T>
615 typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
616 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
617 for (const auto end = data + size; data < end; ++data) {
618 hash_state = H::combine(std::move(hash_state), *data);
619 }
620 return hash_state;
621 }
622
623 #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
624 ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
625 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
626 #else
627 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
628 #endif
629
630 // HashSelect
631 //
632 // Type trait to select the appropriate hash implementation to use.
633 // HashSelect::type<T> will give the proper hash implementation, to be invoked
634 // as:
635 // HashSelect::type<T>::Invoke(state, value)
636 // Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
637 // valid `Invoke` function. Types that are not hashable will have a ::value of
638 // `false`.
639 struct HashSelect {
640 private:
641 struct State : HashStateBase<State> {
642 static State combine_contiguous(State hash_state, const unsigned char*,
643 size_t);
644 using State::HashStateBase::combine_contiguous;
645 };
646
647 struct UniquelyRepresentedProbe {
648 template <typename H, typename T>
649 static auto Invoke(H state, const T& value)
650 -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
651 return hash_internal::hash_bytes(std::move(state), value);
652 }
653 };
654
655 struct HashValueProbe {
656 template <typename H, typename T>
657 static auto Invoke(H state, const T& value) -> absl::enable_if_t<
658 std::is_same<H,
659 decltype(AbslHashValue(std::move(state), value))>::value,
660 H> {
661 return AbslHashValue(std::move(state), value);
662 }
663 };
664
665 struct LegacyHashProbe {
666 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
667 template <typename H, typename T>
668 static auto Invoke(H state, const T& value) -> absl::enable_if_t<
669 std::is_convertible<
670 decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
671 size_t>::value,
672 H> {
673 return hash_internal::hash_bytes(
674 std::move(state),
675 ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
676 }
677 #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
678 };
679
680 struct StdHashProbe {
681 template <typename H, typename T>
682 static auto Invoke(H state, const T& value)
683 -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
684 return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
685 }
686 };
687
688 template <typename Hash, typename T>
689 struct Probe : Hash {
690 private:
691 template <typename H, typename = decltype(H::Invoke(
692 std::declval<State>(), std::declval<const T&>()))>
693 static std::true_type Test(int);
694 template <typename U>
695 static std::false_type Test(char);
696
697 public:
698 static constexpr bool value = decltype(Test<Hash>(0))::value;
699 };
700
701 public:
702 // Probe each implementation in order.
703 // disjunction provides short circuiting wrt instantiation.
704 template <typename T>
705 using Apply = absl::disjunction< //
706 Probe<UniquelyRepresentedProbe, T>, //
707 Probe<HashValueProbe, T>, //
708 Probe<LegacyHashProbe, T>, //
709 Probe<StdHashProbe, T>, //
710 std::false_type>;
711 };
712
713 template <typename T>
714 struct is_hashable
715 : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
716
717 // HashState
718 class ABSL_DLL HashState : public HashStateBase<HashState> {
719 // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
720 // We use the intrinsic when available to improve performance.
721 #ifdef ABSL_HAVE_INTRINSIC_INT128
722 using uint128 = __uint128_t;
723 #else // ABSL_HAVE_INTRINSIC_INT128
724 using uint128 = absl::uint128;
725 #endif // ABSL_HAVE_INTRINSIC_INT128
726
727 static constexpr uint64_t kMul =
728 sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51}
729 : uint64_t{0x9ddfea08eb382d69};
730
731 template <typename T>
732 using IntegralFastPath =
733 conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
734
735 public:
736 // Move only
737 HashState(HashState&&) = default;
738 HashState& operator=(HashState&&) = default;
739
740 // HashState::combine_contiguous()
741 //
742 // Fundamental base case for hash recursion: mixes the given range of bytes
743 // into the hash state.
744 static HashState combine_contiguous(HashState hash_state,
745 const unsigned char* first, size_t size) {
746 return HashState(
747 CombineContiguousImpl(hash_state.state_, first, size,
748 std::integral_constant<int, sizeof(size_t)>{}));
749 }
750 using HashState::HashStateBase::combine_contiguous;
751
752 // HashState::hash()
753 //
754 // For performance reasons in non-opt mode, we specialize this for
755 // integral types.
756 // Otherwise we would be instantiating and calling dozens of functions for
757 // something that is just one multiplication and a couple xor's.
758 // The result should be the same as running the whole algorithm, but faster.
759 template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
760 static size_t hash(T value) {
761 return static_cast<size_t>(Mix(Seed(), static_cast<uint64_t>(value)));
762 }
763
764 // Overload of HashState::hash()
765 template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
766 static size_t hash(const T& value) {
767 return static_cast<size_t>(combine(HashState{}, value).state_);
768 }
769
770 private:
771 // Invoked only once for a given argument; that plus the fact that this is
772 // move-only ensures that there is only one non-moved-from object.
773 HashState() : state_(Seed()) {}
774
775 // Workaround for MSVC bug.
776 // We make the type copyable to fix the calling convention, even though we
777 // never actually copy it. Keep it private to not affect the public API of the
778 // type.
779 HashState(const HashState&) = default;
780
781 explicit HashState(uint64_t state) : state_(state) {}
782
783 // Implementation of the base case for combine_contiguous where we actually
784 // mix the bytes into the state.
785 // Dispatch to different implementations of the combine_contiguous depending
786 // on the value of `sizeof(size_t)`.
787 static uint64_t CombineContiguousImpl(uint64_t state,
788 const unsigned char* first, size_t len,
789 std::integral_constant<int, 4>
790 /* sizeof_size_t */);
791 static uint64_t CombineContiguousImpl(uint64_t state,
792 const unsigned char* first, size_t len,
793 std::integral_constant<int, 8>
794 /* sizeof_size_t */);
795
796
797 // Slow dispatch path for calls to CombineContiguousImpl with a size argument
798 // larger than PiecewiseChunkSize(). Has the same effect as calling
799 // CombineContiguousImpl() repeatedly with the chunk stride size.
800 static uint64_t CombineLargeContiguousImpl32(uint64_t state,
801 const unsigned char* first,
802 size_t len);
803 static uint64_t CombineLargeContiguousImpl64(uint64_t state,
804 const unsigned char* first,
805 size_t len);
806
807 // Reads 9 to 16 bytes from p.
808 // The least significant 8 bytes are in .first, the rest (zero padded) bytes
809 // are in .second.
810 static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
811 size_t len) {
812 uint64_t low_mem = absl::base_internal::UnalignedLoad64(p);
813 uint64_t high_mem = absl::base_internal::UnalignedLoad64(p + len - 8);
814 #ifdef ABSL_IS_LITTLE_ENDIAN
815 uint64_t most_significant = high_mem;
816 uint64_t least_significant = low_mem;
817 #else
818 uint64_t most_significant = low_mem;
819 uint64_t least_significant = high_mem;
820 #endif
821 return {least_significant, most_significant >> (128 - len * 8)};
822 }
823
824 // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
825 static uint64_t Read4To8(const unsigned char* p, size_t len) {
826 uint32_t low_mem = absl::base_internal::UnalignedLoad32(p);
827 uint32_t high_mem = absl::base_internal::UnalignedLoad32(p + len - 4);
828 #ifdef ABSL_IS_LITTLE_ENDIAN
829 uint32_t most_significant = high_mem;
830 uint32_t least_significant = low_mem;
831 #else
832 uint32_t most_significant = low_mem;
833 uint32_t least_significant = high_mem;
834 #endif
835 return (static_cast<uint64_t>(most_significant) << (len - 4) * 8) |
836 least_significant;
837 }
838
839 // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
840 static uint32_t Read1To3(const unsigned char* p, size_t len) {
841 unsigned char mem0 = p[0];
842 unsigned char mem1 = p[len / 2];
843 unsigned char mem2 = p[len - 1];
844 #ifdef ABSL_IS_LITTLE_ENDIAN
845 unsigned char significant2 = mem2;
846 unsigned char significant1 = mem1;
847 unsigned char significant0 = mem0;
848 #else
849 unsigned char significant2 = mem0;
850 unsigned char significant1 = mem1;
851 unsigned char significant0 = mem2;
852 #endif
853 return static_cast<uint32_t>(significant0 | //
854 (significant1 << (len / 2 * 8)) | //
855 (significant2 << ((len - 1) * 8)));
856 }
857
858 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
859 using MultType =
860 absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
861 // We do the addition in 64-bit space to make sure the 128-bit
862 // multiplication is fast. If we were to do it as MultType the compiler has
863 // to assume that the high word is non-zero and needs to perform 2
864 // multiplications instead of one.
865 MultType m = state + v;
866 m *= kMul;
867 return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
868 }
869
870 // An extern to avoid bloat on a direct call to Wyhash() with fixed values for
871 // both the seed and salt parameters.
872 static uint64_t WyhashImpl(const unsigned char* data, size_t len);
873
874 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Hash64(const unsigned char* data,
875 size_t len) {
876 #ifdef ABSL_HAVE_INTRINSIC_INT128
877 return WyhashImpl(data, len);
878 #else
879 return absl::hash_internal::CityHash64(reinterpret_cast<const char*>(data), len);
880 #endif
881 }
882
883 // Seed()
884 //
885 // A non-deterministic seed.
886 //
887 // The current purpose of this seed is to generate non-deterministic results
888 // and prevent having users depend on the particular hash values.
889 // It is not meant as a security feature right now, but it leaves the door
890 // open to upgrade it to a true per-process random seed. A true random seed
891 // costs more and we don't need to pay for that right now.
892 //
893 // On platforms with ASLR, we take advantage of it to make a per-process
894 // random value.
895 // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
896 //
897 // On other platforms this is still going to be non-deterministic but most
898 // probably per-build and not per-process.
899 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
900 #if (!defined(__clang__) || __clang_major__ > 11) && \
901 !defined(__apple_build_version__)
902 return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&kSeed));
903 #else
904 // Workaround the absence of
905 // https://github.com/llvm/llvm-project/commit/bc15bf66dcca76cc06fe71fca35b74dc4d521021.
906 return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
907 #endif
908 }
909 static const void* const kSeed;
910
911 uint64_t state_;
912 };
913
914 // HashState::CombineContiguousImpl()
915 inline uint64_t HashState::CombineContiguousImpl(
916 uint64_t state, const unsigned char* first, size_t len,
917 std::integral_constant<int, 4> /* sizeof_size_t */) {
918 // For large values we use CityHash, for small ones we just use a
919 // multiplicative hash.
920 uint64_t v;
921 if (len > 8) {
922 if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
923 return CombineLargeContiguousImpl32(state, first, len);
924 }
925 v = absl::hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
926 } else if (len >= 4) {
927 v = Read4To8(first, len);
928 } else if (len > 0) {
929 v = Read1To3(first, len);
930 } else {
931 // Empty ranges have no effect.
932 return state;
933 }
934 return Mix(state, v);
935 }
936
937 // Overload of HashState::CombineContiguousImpl()
938 inline uint64_t HashState::CombineContiguousImpl(
939 uint64_t state, const unsigned char* first, size_t len,
940 std::integral_constant<int, 8> /* sizeof_size_t */) {
941 // For large values we use Wyhash or CityHash depending on the platform, for
942 // small ones we just use a multiplicative hash.
943 uint64_t v;
944 if (len > 16) {
945 if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
946 return CombineLargeContiguousImpl64(state, first, len);
947 }
948 v = Hash64(first, len);
949 } else if (len > 8) {
950 auto p = Read9To16(first, len);
951 state = Mix(state, p.first);
952 v = p.second;
953 } else if (len >= 4) {
954 v = Read4To8(first, len);
955 } else if (len > 0) {
956 v = Read1To3(first, len);
957 } else {
958 // Empty ranges have no effect.
959 return state;
960 }
961 return Mix(state, v);
962 }
963
964 struct AggregateBarrier {};
965
966 // HashImpl
967
968 // Add a private base class to make sure this type is not an aggregate.
969 // Aggregates can be aggregate initialized even if the default constructor is
970 // deleted.
971 struct PoisonedHash : private AggregateBarrier {
972 PoisonedHash() = delete;
973 PoisonedHash(const PoisonedHash&) = delete;
974 PoisonedHash& operator=(const PoisonedHash&) = delete;
975 };
976
977 template <typename T>
978 struct HashImpl {
979 size_t operator()(const T& value) const { return HashState::hash(value); }
980 };
981
982 template <typename T>
983 struct Hash
984 : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
985
986 template <typename H>
987 template <typename T, typename... Ts>
988 H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
989 return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
990 std::move(state), value),
991 values...);
992 }
993
994 // HashStateBase::combine_contiguous()
995 template <typename H>
996 template <typename T>
997 H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
998 return hash_internal::hash_range_or_bytes(std::move(state), data, size);
999 }
1000
1001 // HashStateBase::PiecewiseCombiner::add_buffer()
1002 template <typename H>
1003 H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
1004 size_t size) {
1005 if (position_ + size < PiecewiseChunkSize()) {
1006 // This partial chunk does not fill our existing buffer
1007 memcpy(buf_ + position_, data, size);
1008 position_ += size;
1009 return state;
1010 }
1011
1012 // If the buffer is partially filled we need to complete the buffer
1013 // and hash it.
1014 if (position_ != 0) {
1015 const size_t bytes_needed = PiecewiseChunkSize() - position_;
1016 memcpy(buf_ + position_, data, bytes_needed);
1017 state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
1018 data += bytes_needed;
1019 size -= bytes_needed;
1020 }
1021
1022 // Hash whatever chunks we can without copying
1023 while (size >= PiecewiseChunkSize()) {
1024 state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
1025 data += PiecewiseChunkSize();
1026 size -= PiecewiseChunkSize();
1027 }
1028 // Fill the buffer with the remainder
1029 memcpy(buf_, data, size);
1030 position_ = size;
1031 return state;
1032 }
1033
1034 // HashStateBase::PiecewiseCombiner::finalize()
1035 template <typename H>
1036 H PiecewiseCombiner::finalize(H state) {
1037 // Hash the remainder left in the buffer, which may be empty
1038 return H::combine_contiguous(std::move(state), buf_, position_);
1039 }
1040
1041 } // namespace hash_internal
1042 ABSL_NAMESPACE_END
1043 } // namespace absl
1044
1045 #endif // ABSL_HASH_INTERNAL_HASH_H_
1046