xref: /freebsd/contrib/llvm-project/libcxx/include/__functional/hash.h (revision a2464ee12761660f50d0b6f59f233949ebcacc87)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___FUNCTIONAL_HASH_H
10 #define _LIBCPP___FUNCTIONAL_HASH_H
11 
12 #include <__config>
13 #include <__functional/unary_function.h>
14 #include <__tuple>
15 #include <__utility/forward.h>
16 #include <__utility/move.h>
17 #include <__utility/pair.h>
18 #include <__utility/swap.h>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstring>
22 #include <limits>
23 #include <type_traits>
24 
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 #pragma GCC system_header
27 #endif
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _Size>
32 inline _LIBCPP_INLINE_VISIBILITY
33 _Size
34 __loadword(const void* __p)
35 {
36     _Size __r;
37     _VSTD::memcpy(&__r, __p, sizeof(__r));
38     return __r;
39 }
40 
41 // We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
42 // is 64 bits.  This is because cityhash64 uses 64bit x 64bit
43 // multiplication, which can be very slow on 32-bit systems.
44 template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
45 struct __murmur2_or_cityhash;
46 
47 template <class _Size>
48 struct __murmur2_or_cityhash<_Size, 32>
49 {
50     inline _Size operator()(const void* __key, _Size __len)
51          _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
52 };
53 
54 // murmur2
55 template <class _Size>
56 _Size
57 __murmur2_or_cityhash<_Size, 32>::operator()(const void* __key, _Size __len)
58 {
59     const _Size __m = 0x5bd1e995;
60     const _Size __r = 24;
61     _Size __h = __len;
62     const unsigned char* __data = static_cast<const unsigned char*>(__key);
63     for (; __len >= 4; __data += 4, __len -= 4)
64     {
65         _Size __k = __loadword<_Size>(__data);
66         __k *= __m;
67         __k ^= __k >> __r;
68         __k *= __m;
69         __h *= __m;
70         __h ^= __k;
71     }
72     switch (__len)
73     {
74     case 3:
75         __h ^= static_cast<_Size>(__data[2] << 16);
76         _LIBCPP_FALLTHROUGH();
77     case 2:
78         __h ^= static_cast<_Size>(__data[1] << 8);
79         _LIBCPP_FALLTHROUGH();
80     case 1:
81         __h ^= __data[0];
82         __h *= __m;
83     }
84     __h ^= __h >> 13;
85     __h *= __m;
86     __h ^= __h >> 15;
87     return __h;
88 }
89 
90 template <class _Size>
91 struct __murmur2_or_cityhash<_Size, 64>
92 {
93     inline _Size operator()(const void* __key, _Size __len)  _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
94 
95  private:
96   // Some primes between 2^63 and 2^64.
97   static const _Size __k0 = 0xc3a5c85c97cb3127ULL;
98   static const _Size __k1 = 0xb492b66fbe98f273ULL;
99   static const _Size __k2 = 0x9ae16a3b2f90404fULL;
100   static const _Size __k3 = 0xc949d7c7509e6557ULL;
101 
102   static _Size __rotate(_Size __val, int __shift) {
103     return __shift == 0 ? __val : ((__val >> __shift) | (__val << (64 - __shift)));
104   }
105 
106   static _Size __rotate_by_at_least_1(_Size __val, int __shift) {
107     return (__val >> __shift) | (__val << (64 - __shift));
108   }
109 
110   static _Size __shift_mix(_Size __val) {
111     return __val ^ (__val >> 47);
112   }
113 
114   static _Size __hash_len_16(_Size __u, _Size __v)
115      _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
116   {
117     const _Size __mul = 0x9ddfea08eb382d69ULL;
118     _Size __a = (__u ^ __v) * __mul;
119     __a ^= (__a >> 47);
120     _Size __b = (__v ^ __a) * __mul;
121     __b ^= (__b >> 47);
122     __b *= __mul;
123     return __b;
124   }
125 
126   static _Size __hash_len_0_to_16(const char* __s, _Size __len)
127      _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
128   {
129     if (__len > 8) {
130       const _Size __a = __loadword<_Size>(__s);
131       const _Size __b = __loadword<_Size>(__s + __len - 8);
132       return __hash_len_16(__a, __rotate_by_at_least_1(__b + __len, __len)) ^ __b;
133     }
134     if (__len >= 4) {
135       const uint32_t __a = __loadword<uint32_t>(__s);
136       const uint32_t __b = __loadword<uint32_t>(__s + __len - 4);
137       return __hash_len_16(__len + (__a << 3), __b);
138     }
139     if (__len > 0) {
140       const unsigned char __a = static_cast<unsigned char>(__s[0]);
141       const unsigned char __b = static_cast<unsigned char>(__s[__len >> 1]);
142       const unsigned char __c = static_cast<unsigned char>(__s[__len - 1]);
143       const uint32_t __y = static_cast<uint32_t>(__a) +
144                            (static_cast<uint32_t>(__b) << 8);
145       const uint32_t __z = __len + (static_cast<uint32_t>(__c) << 2);
146       return __shift_mix(__y * __k2 ^ __z * __k3) * __k2;
147     }
148     return __k2;
149   }
150 
151   static _Size __hash_len_17_to_32(const char *__s, _Size __len)
152      _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
153   {
154     const _Size __a = __loadword<_Size>(__s) * __k1;
155     const _Size __b = __loadword<_Size>(__s + 8);
156     const _Size __c = __loadword<_Size>(__s + __len - 8) * __k2;
157     const _Size __d = __loadword<_Size>(__s + __len - 16) * __k0;
158     return __hash_len_16(__rotate(__a - __b, 43) + __rotate(__c, 30) + __d,
159                          __a + __rotate(__b ^ __k3, 20) - __c + __len);
160   }
161 
162   // Return a 16-byte hash for 48 bytes.  Quick and dirty.
163   // Callers do best to use "random-looking" values for a and b.
164   static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
165       _Size __w, _Size __x, _Size __y, _Size __z, _Size __a, _Size __b)
166         _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
167   {
168     __a += __w;
169     __b = __rotate(__b + __a + __z, 21);
170     const _Size __c = __a;
171     __a += __x;
172     __a += __y;
173     __b += __rotate(__a, 44);
174     return pair<_Size, _Size>(__a + __z, __b + __c);
175   }
176 
177   // Return a 16-byte hash for s[0] ... s[31], a, and b.  Quick and dirty.
178   static pair<_Size, _Size> __weak_hash_len_32_with_seeds(
179       const char* __s, _Size __a, _Size __b)
180     _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
181   {
182     return __weak_hash_len_32_with_seeds(__loadword<_Size>(__s),
183                                          __loadword<_Size>(__s + 8),
184                                          __loadword<_Size>(__s + 16),
185                                          __loadword<_Size>(__s + 24),
186                                          __a,
187                                          __b);
188   }
189 
190   // Return an 8-byte hash for 33 to 64 bytes.
191   static _Size __hash_len_33_to_64(const char *__s, size_t __len)
192     _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
193   {
194     _Size __z = __loadword<_Size>(__s + 24);
195     _Size __a = __loadword<_Size>(__s) +
196                 (__len + __loadword<_Size>(__s + __len - 16)) * __k0;
197     _Size __b = __rotate(__a + __z, 52);
198     _Size __c = __rotate(__a, 37);
199     __a += __loadword<_Size>(__s + 8);
200     __c += __rotate(__a, 7);
201     __a += __loadword<_Size>(__s + 16);
202     _Size __vf = __a + __z;
203     _Size __vs = __b + __rotate(__a, 31) + __c;
204     __a = __loadword<_Size>(__s + 16) + __loadword<_Size>(__s + __len - 32);
205     __z += __loadword<_Size>(__s + __len - 8);
206     __b = __rotate(__a + __z, 52);
207     __c = __rotate(__a, 37);
208     __a += __loadword<_Size>(__s + __len - 24);
209     __c += __rotate(__a, 7);
210     __a += __loadword<_Size>(__s + __len - 16);
211     _Size __wf = __a + __z;
212     _Size __ws = __b + __rotate(__a, 31) + __c;
213     _Size __r = __shift_mix((__vf + __ws) * __k2 + (__wf + __vs) * __k0);
214     return __shift_mix(__r * __k0 + __vs) * __k2;
215   }
216 };
217 
218 // cityhash64
219 template <class _Size>
220 _Size
221 __murmur2_or_cityhash<_Size, 64>::operator()(const void* __key, _Size __len)
222 {
223   const char* __s = static_cast<const char*>(__key);
224   if (__len <= 32) {
225     if (__len <= 16) {
226       return __hash_len_0_to_16(__s, __len);
227     } else {
228       return __hash_len_17_to_32(__s, __len);
229     }
230   } else if (__len <= 64) {
231     return __hash_len_33_to_64(__s, __len);
232   }
233 
234   // For strings over 64 bytes we hash the end first, and then as we
235   // loop we keep 56 bytes of state: v, w, x, y, and z.
236   _Size __x = __loadword<_Size>(__s + __len - 40);
237   _Size __y = __loadword<_Size>(__s + __len - 16) +
238               __loadword<_Size>(__s + __len - 56);
239   _Size __z = __hash_len_16(__loadword<_Size>(__s + __len - 48) + __len,
240                           __loadword<_Size>(__s + __len - 24));
241   pair<_Size, _Size> __v = __weak_hash_len_32_with_seeds(__s + __len - 64, __len, __z);
242   pair<_Size, _Size> __w = __weak_hash_len_32_with_seeds(__s + __len - 32, __y + __k1, __x);
243   __x = __x * __k1 + __loadword<_Size>(__s);
244 
245   // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks.
246   __len = (__len - 1) & ~static_cast<_Size>(63);
247   do {
248     __x = __rotate(__x + __y + __v.first + __loadword<_Size>(__s + 8), 37) * __k1;
249     __y = __rotate(__y + __v.second + __loadword<_Size>(__s + 48), 42) * __k1;
250     __x ^= __w.second;
251     __y += __v.first + __loadword<_Size>(__s + 40);
252     __z = __rotate(__z + __w.first, 33) * __k1;
253     __v = __weak_hash_len_32_with_seeds(__s, __v.second * __k1, __x + __w.first);
254     __w = __weak_hash_len_32_with_seeds(__s + 32, __z + __w.second,
255                                         __y + __loadword<_Size>(__s + 16));
256     _VSTD::swap(__z, __x);
257     __s += 64;
258     __len -= 64;
259   } while (__len != 0);
260   return __hash_len_16(
261       __hash_len_16(__v.first, __w.first) + __shift_mix(__y) * __k1 + __z,
262       __hash_len_16(__v.second, __w.second) + __x);
263 }
264 
265 template <class _Tp, size_t = sizeof(_Tp) / sizeof(size_t)>
266 struct __scalar_hash;
267 
268 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
269 template <class _Tp>
270 struct __scalar_hash<_Tp, 0>
271 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
272     : public unary_function<_Tp, size_t>
273 #endif
274 {
275 _LIBCPP_SUPPRESS_DEPRECATED_POP
276 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
277     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
278     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp argument_type;
279 #endif
280     _LIBCPP_INLINE_VISIBILITY
281     size_t operator()(_Tp __v) const _NOEXCEPT
282     {
283         union
284         {
285             _Tp    __t;
286             size_t __a;
287         } __u;
288         __u.__a = 0;
289         __u.__t = __v;
290         return __u.__a;
291     }
292 };
293 
294 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
295 template <class _Tp>
296 struct __scalar_hash<_Tp, 1>
297 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
298     : public unary_function<_Tp, size_t>
299 #endif
300 {
301 _LIBCPP_SUPPRESS_DEPRECATED_POP
302 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
303     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
304     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp argument_type;
305 #endif
306     _LIBCPP_INLINE_VISIBILITY
307     size_t operator()(_Tp __v) const _NOEXCEPT
308     {
309         union
310         {
311             _Tp    __t;
312             size_t __a;
313         } __u;
314         __u.__t = __v;
315         return __u.__a;
316     }
317 };
318 
319 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
320 template <class _Tp>
321 struct __scalar_hash<_Tp, 2>
322 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
323     : public unary_function<_Tp, size_t>
324 #endif
325 {
326 _LIBCPP_SUPPRESS_DEPRECATED_POP
327 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
328     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
329     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp argument_type;
330 #endif
331     _LIBCPP_INLINE_VISIBILITY
332     size_t operator()(_Tp __v) const _NOEXCEPT
333     {
334         union
335         {
336             _Tp __t;
337             struct
338             {
339                 size_t __a;
340                 size_t __b;
341             } __s;
342         } __u;
343         __u.__t = __v;
344         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
345     }
346 };
347 
348 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
349 template <class _Tp>
350 struct __scalar_hash<_Tp, 3>
351 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
352     : public unary_function<_Tp, size_t>
353 #endif
354 {
355 _LIBCPP_SUPPRESS_DEPRECATED_POP
356 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
357     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
358     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp argument_type;
359 #endif
360     _LIBCPP_INLINE_VISIBILITY
361     size_t operator()(_Tp __v) const _NOEXCEPT
362     {
363         union
364         {
365             _Tp __t;
366             struct
367             {
368                 size_t __a;
369                 size_t __b;
370                 size_t __c;
371             } __s;
372         } __u;
373         __u.__t = __v;
374         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
375     }
376 };
377 
378 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
379 template <class _Tp>
380 struct __scalar_hash<_Tp, 4>
381 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
382     : public unary_function<_Tp, size_t>
383 #endif
384 {
385 _LIBCPP_SUPPRESS_DEPRECATED_POP
386 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
387     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
388     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp argument_type;
389 #endif
390     _LIBCPP_INLINE_VISIBILITY
391     size_t operator()(_Tp __v) const _NOEXCEPT
392     {
393         union
394         {
395             _Tp __t;
396             struct
397             {
398                 size_t __a;
399                 size_t __b;
400                 size_t __c;
401                 size_t __d;
402             } __s;
403         } __u;
404         __u.__t = __v;
405         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
406     }
407 };
408 
409 struct _PairT {
410   size_t first;
411   size_t second;
412 };
413 
414 _LIBCPP_INLINE_VISIBILITY
415 inline size_t __hash_combine(size_t __lhs, size_t __rhs) _NOEXCEPT {
416     typedef __scalar_hash<_PairT> _HashT;
417     const _PairT __p = {__lhs, __rhs};
418     return _HashT()(__p);
419 }
420 
421 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
422 template<class _Tp>
423 struct _LIBCPP_TEMPLATE_VIS hash<_Tp*>
424 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
425     : public unary_function<_Tp*, size_t>
426 #endif
427 {
428 _LIBCPP_SUPPRESS_DEPRECATED_POP
429 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
430     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
431     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* argument_type;
432 #endif
433     _LIBCPP_INLINE_VISIBILITY
434     size_t operator()(_Tp* __v) const _NOEXCEPT
435     {
436         union
437         {
438             _Tp* __t;
439             size_t __a;
440         } __u;
441         __u.__t = __v;
442         return __murmur2_or_cityhash<size_t>()(&__u, sizeof(__u));
443     }
444 };
445 
446 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
447 template <>
448 struct _LIBCPP_TEMPLATE_VIS hash<bool>
449 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
450     : public unary_function<bool, size_t>
451 #endif
452 {
453 _LIBCPP_SUPPRESS_DEPRECATED_POP
454 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
455     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
456     _LIBCPP_DEPRECATED_IN_CXX17 typedef bool argument_type;
457 #endif
458     _LIBCPP_INLINE_VISIBILITY
459     size_t operator()(bool __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
460 };
461 
462 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
463 template <>
464 struct _LIBCPP_TEMPLATE_VIS hash<char>
465 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
466     : public unary_function<char, size_t>
467 #endif
468 {
469 _LIBCPP_SUPPRESS_DEPRECATED_POP
470 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
471     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
472     _LIBCPP_DEPRECATED_IN_CXX17 typedef char argument_type;
473 #endif
474     _LIBCPP_INLINE_VISIBILITY
475     size_t operator()(char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
476 };
477 
478 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
479 template <>
480 struct _LIBCPP_TEMPLATE_VIS hash<signed char>
481 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
482     : public unary_function<signed char, size_t>
483 #endif
484 {
485 _LIBCPP_SUPPRESS_DEPRECATED_POP
486 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
487     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
488     _LIBCPP_DEPRECATED_IN_CXX17 typedef signed char argument_type;
489 #endif
490     _LIBCPP_INLINE_VISIBILITY
491     size_t operator()(signed char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
492 };
493 
494 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
495 template <>
496 struct _LIBCPP_TEMPLATE_VIS hash<unsigned char>
497 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
498     : public unary_function<unsigned char, size_t>
499 #endif
500 {
501 _LIBCPP_SUPPRESS_DEPRECATED_POP
502 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
503     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
504     _LIBCPP_DEPRECATED_IN_CXX17 typedef unsigned char argument_type;
505 #endif
506     _LIBCPP_INLINE_VISIBILITY
507     size_t operator()(unsigned char __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
508 };
509 
510 #ifndef _LIBCPP_HAS_NO_CHAR8_T
511 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
512 template <>
513 struct _LIBCPP_TEMPLATE_VIS hash<char8_t>
514 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
515     : public unary_function<char8_t, size_t>
516 #endif
517 {
518 _LIBCPP_SUPPRESS_DEPRECATED_POP
519 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
520     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
521     _LIBCPP_DEPRECATED_IN_CXX17 typedef char8_t argument_type;
522 #endif
523     _LIBCPP_INLINE_VISIBILITY
524     size_t operator()(char8_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
525 };
526 #endif // !_LIBCPP_HAS_NO_CHAR8_T
527 
528 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
529 
530 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
531 template <>
532 struct _LIBCPP_TEMPLATE_VIS hash<char16_t>
533 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
534     : public unary_function<char16_t, size_t>
535 #endif
536 {
537 _LIBCPP_SUPPRESS_DEPRECATED_POP
538 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
539     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
540     _LIBCPP_DEPRECATED_IN_CXX17 typedef char16_t argument_type;
541 #endif
542     _LIBCPP_INLINE_VISIBILITY
543     size_t operator()(char16_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
544 };
545 
546 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
547 template <>
548 struct _LIBCPP_TEMPLATE_VIS hash<char32_t>
549 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
550     : public unary_function<char32_t, size_t>
551 #endif
552 {
553 _LIBCPP_SUPPRESS_DEPRECATED_POP
554 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
555     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
556     _LIBCPP_DEPRECATED_IN_CXX17 typedef char32_t argument_type;
557 #endif
558     _LIBCPP_INLINE_VISIBILITY
559     size_t operator()(char32_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
560 };
561 
562 #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
563 
564 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
565 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
566 template <>
567 struct _LIBCPP_TEMPLATE_VIS hash<wchar_t>
568 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
569     : public unary_function<wchar_t, size_t>
570 #endif
571 {
572 _LIBCPP_SUPPRESS_DEPRECATED_POP
573 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
574     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
575     _LIBCPP_DEPRECATED_IN_CXX17 typedef wchar_t argument_type;
576 #endif
577     _LIBCPP_INLINE_VISIBILITY
578     size_t operator()(wchar_t __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
579 };
580 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
581 
582 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
583 template <>
584 struct _LIBCPP_TEMPLATE_VIS hash<short>
585 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
586     : public unary_function<short, size_t>
587 #endif
588 {
589 _LIBCPP_SUPPRESS_DEPRECATED_POP
590 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
591     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
592     _LIBCPP_DEPRECATED_IN_CXX17 typedef short argument_type;
593 #endif
594     _LIBCPP_INLINE_VISIBILITY
595     size_t operator()(short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
596 };
597 
598 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
599 template <>
600 struct _LIBCPP_TEMPLATE_VIS hash<unsigned short>
601 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
602     : public unary_function<unsigned short, size_t>
603 #endif
604 {
605 _LIBCPP_SUPPRESS_DEPRECATED_POP
606 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
607     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
608     _LIBCPP_DEPRECATED_IN_CXX17 typedef unsigned short argument_type;
609 #endif
610     _LIBCPP_INLINE_VISIBILITY
611     size_t operator()(unsigned short __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
612 };
613 
614 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
615 template <>
616 struct _LIBCPP_TEMPLATE_VIS hash<int>
617 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
618     : public unary_function<int, size_t>
619 #endif
620 {
621 _LIBCPP_SUPPRESS_DEPRECATED_POP
622 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
623     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
624     _LIBCPP_DEPRECATED_IN_CXX17 typedef int argument_type;
625 #endif
626     _LIBCPP_INLINE_VISIBILITY
627     size_t operator()(int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
628 };
629 
630 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
631 template <>
632 struct _LIBCPP_TEMPLATE_VIS hash<unsigned int>
633 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
634     : public unary_function<unsigned int, size_t>
635 #endif
636 {
637 _LIBCPP_SUPPRESS_DEPRECATED_POP
638 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
639     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
640     _LIBCPP_DEPRECATED_IN_CXX17 typedef unsigned int argument_type;
641 #endif
642     _LIBCPP_INLINE_VISIBILITY
643     size_t operator()(unsigned int __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
644 };
645 
646 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
647 template <>
648 struct _LIBCPP_TEMPLATE_VIS hash<long>
649 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
650     : public unary_function<long, size_t>
651 #endif
652 {
653 _LIBCPP_SUPPRESS_DEPRECATED_POP
654 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
655     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
656     _LIBCPP_DEPRECATED_IN_CXX17 typedef long argument_type;
657 #endif
658     _LIBCPP_INLINE_VISIBILITY
659     size_t operator()(long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
660 };
661 
662 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
663 template <>
664 struct _LIBCPP_TEMPLATE_VIS hash<unsigned long>
665 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
666     : public unary_function<unsigned long, size_t>
667 #endif
668 {
669 _LIBCPP_SUPPRESS_DEPRECATED_POP
670 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
671     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
672     _LIBCPP_DEPRECATED_IN_CXX17 typedef unsigned long argument_type;
673 #endif
674     _LIBCPP_INLINE_VISIBILITY
675     size_t operator()(unsigned long __v) const _NOEXCEPT {return static_cast<size_t>(__v);}
676 };
677 
678 template <>
679 struct _LIBCPP_TEMPLATE_VIS hash<long long>
680     : public __scalar_hash<long long>
681 {
682 };
683 
684 template <>
685 struct _LIBCPP_TEMPLATE_VIS hash<unsigned long long>
686     : public __scalar_hash<unsigned long long>
687 {
688 };
689 
690 #ifndef _LIBCPP_HAS_NO_INT128
691 
692 template <>
693 struct _LIBCPP_TEMPLATE_VIS hash<__int128_t>
694     : public __scalar_hash<__int128_t>
695 {
696 };
697 
698 template <>
699 struct _LIBCPP_TEMPLATE_VIS hash<__uint128_t>
700     : public __scalar_hash<__uint128_t>
701 {
702 };
703 
704 #endif
705 
706 template <>
707 struct _LIBCPP_TEMPLATE_VIS hash<float>
708     : public __scalar_hash<float>
709 {
710     _LIBCPP_INLINE_VISIBILITY
711     size_t operator()(float __v) const _NOEXCEPT
712     {
713         // -0.0 and 0.0 should return same hash
714        if (__v == 0.0f)
715            return 0;
716         return __scalar_hash<float>::operator()(__v);
717     }
718 };
719 
720 template <>
721 struct _LIBCPP_TEMPLATE_VIS hash<double>
722     : public __scalar_hash<double>
723 {
724     _LIBCPP_INLINE_VISIBILITY
725     size_t operator()(double __v) const _NOEXCEPT
726     {
727         // -0.0 and 0.0 should return same hash
728        if (__v == 0.0)
729            return 0;
730         return __scalar_hash<double>::operator()(__v);
731     }
732 };
733 
734 template <>
735 struct _LIBCPP_TEMPLATE_VIS hash<long double>
736     : public __scalar_hash<long double>
737 {
738     _LIBCPP_INLINE_VISIBILITY
739     size_t operator()(long double __v) const _NOEXCEPT
740     {
741         // -0.0 and 0.0 should return same hash
742         if (__v == 0.0L)
743             return 0;
744 #if defined(__i386__) || (defined(__x86_64__) && defined(__ILP32__))
745         // Zero out padding bits
746         union
747         {
748             long double __t;
749             struct
750             {
751                 size_t __a;
752                 size_t __b;
753                 size_t __c;
754                 size_t __d;
755             } __s;
756         } __u;
757         __u.__s.__a = 0;
758         __u.__s.__b = 0;
759         __u.__s.__c = 0;
760         __u.__s.__d = 0;
761         __u.__t = __v;
762         return __u.__s.__a ^ __u.__s.__b ^ __u.__s.__c ^ __u.__s.__d;
763 #elif defined(__x86_64__)
764         // Zero out padding bits
765         union
766         {
767             long double __t;
768             struct
769             {
770                 size_t __a;
771                 size_t __b;
772             } __s;
773         } __u;
774         __u.__s.__a = 0;
775         __u.__s.__b = 0;
776         __u.__t = __v;
777         return __u.__s.__a ^ __u.__s.__b;
778 #else
779         return __scalar_hash<long double>::operator()(__v);
780 #endif
781     }
782 };
783 
784 #if _LIBCPP_STD_VER > 11
785 
786 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
787 template <class _Tp, bool = is_enum<_Tp>::value>
788 struct _LIBCPP_TEMPLATE_VIS __enum_hash
789 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
790     : public unary_function<_Tp, size_t>
791 #endif
792 {
793 _LIBCPP_SUPPRESS_DEPRECATED_POP
794 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
795     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
796     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp argument_type;
797 #endif
798     _LIBCPP_INLINE_VISIBILITY
799     size_t operator()(_Tp __v) const _NOEXCEPT
800     {
801         typedef typename underlying_type<_Tp>::type type;
802         return hash<type>{}(static_cast<type>(__v));
803     }
804 };
805 template <class _Tp>
806 struct _LIBCPP_TEMPLATE_VIS __enum_hash<_Tp, false> {
807     __enum_hash() = delete;
808     __enum_hash(__enum_hash const&) = delete;
809     __enum_hash& operator=(__enum_hash const&) = delete;
810 };
811 
812 template <class _Tp>
813 struct _LIBCPP_TEMPLATE_VIS hash : public __enum_hash<_Tp>
814 {
815 };
816 #endif
817 
818 #if _LIBCPP_STD_VER > 14
819 
820 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
821 template <>
822 struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
823 #if !defined(_LIBCPP_ABI_NO_BINDER_BASES)
824   : public unary_function<nullptr_t, size_t>
825 #endif
826 {
827 _LIBCPP_SUPPRESS_DEPRECATED_POP
828 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
829     _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
830     _LIBCPP_DEPRECATED_IN_CXX17 typedef nullptr_t argument_type;
831 #endif
832     _LIBCPP_INLINE_VISIBILITY
833     size_t operator()(nullptr_t) const _NOEXCEPT {
834         return 662607004ull;
835     }
836 };
837 #endif
838 
839 #ifndef _LIBCPP_CXX03_LANG
840 template <class _Key, class _Hash>
841 using __check_hash_requirements _LIBCPP_NODEBUG = integral_constant<bool,
842     is_copy_constructible<_Hash>::value &&
843     is_move_constructible<_Hash>::value &&
844     __invokable_r<size_t, _Hash, _Key const&>::value
845 >;
846 
847 template <class _Key, class _Hash = hash<_Key> >
848 using __has_enabled_hash _LIBCPP_NODEBUG = integral_constant<bool,
849     __check_hash_requirements<_Key, _Hash>::value &&
850     is_default_constructible<_Hash>::value
851 >;
852 
853 #if _LIBCPP_STD_VER > 14
854 template <class _Type, class>
855 using __enable_hash_helper_imp _LIBCPP_NODEBUG = _Type;
856 
857 template <class _Type, class ..._Keys>
858 using __enable_hash_helper _LIBCPP_NODEBUG = __enable_hash_helper_imp<_Type,
859   typename enable_if<__all<__has_enabled_hash<_Keys>::value...>::value>::type
860 >;
861 #else
862 template <class _Type, class ...>
863 using __enable_hash_helper _LIBCPP_NODEBUG = _Type;
864 #endif
865 
866 #endif // !_LIBCPP_CXX03_LANG
867 
868 _LIBCPP_END_NAMESPACE_STD
869 
870 #endif // _LIBCPP___FUNCTIONAL_HASH_H
871