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