diff options
Diffstat (limited to 'kernel/lib/libcxx/include/__functional/hash.h')
| -rw-r--r-- | kernel/lib/libcxx/include/__functional/hash.h | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/kernel/lib/libcxx/include/__functional/hash.h b/kernel/lib/libcxx/include/__functional/hash.h new file mode 100644 index 0000000..24b6487 --- /dev/null +++ b/kernel/lib/libcxx/include/__functional/hash.h @@ -0,0 +1,85 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the BastionOS freestanding C++ standard library. +// +// Basic hash specializations for integral types, pointers, and bool. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBBASTION_FUNCTIONAL_HASH_H +#define _LIBBASTION_FUNCTIONAL_HASH_H + +#include <__config> +#include <cstddef> +#include <cstdint> + +_LIBBASTION_BEGIN_NAMESPACE_STD + +// Primary template — disabled. +template<class _Tp> +struct hash; + +namespace __detail { + +// FNV-1a for types larger than size_t would be overkill in a kernel. +// Identity hash for integers, cast-to-size_t for pointers. +inline constexpr size_t __hash_integral(size_t __val) noexcept { + // Mix bits for better distribution — based on splitmix64. + __val ^= __val >> 30; + __val *= 0xbf58476d1ce4e5b9ULL; + __val ^= __val >> 27; + __val *= 0x94d049bb133111ebULL; + __val ^= __val >> 31; + return __val; +} + +} // namespace __detail + +// Macro for integral type specializations. +#define _LIBBASTION_HASH_INTEGRAL(_Type) \ +template<> \ +struct hash<_Type> { \ + _LIBBASTION_NODISCARD size_t operator()(_Type __val) const noexcept { \ + return __detail::__hash_integral(static_cast<size_t>(__val)); \ + } \ +}; + +_LIBBASTION_HASH_INTEGRAL(bool) +_LIBBASTION_HASH_INTEGRAL(char) +_LIBBASTION_HASH_INTEGRAL(signed char) +_LIBBASTION_HASH_INTEGRAL(unsigned char) +_LIBBASTION_HASH_INTEGRAL(char8_t) +_LIBBASTION_HASH_INTEGRAL(char16_t) +_LIBBASTION_HASH_INTEGRAL(char32_t) +_LIBBASTION_HASH_INTEGRAL(wchar_t) +_LIBBASTION_HASH_INTEGRAL(short) +_LIBBASTION_HASH_INTEGRAL(unsigned short) +_LIBBASTION_HASH_INTEGRAL(int) +_LIBBASTION_HASH_INTEGRAL(unsigned int) +_LIBBASTION_HASH_INTEGRAL(long) +_LIBBASTION_HASH_INTEGRAL(unsigned long) +_LIBBASTION_HASH_INTEGRAL(long long) +_LIBBASTION_HASH_INTEGRAL(unsigned long long) + +#undef _LIBBASTION_HASH_INTEGRAL + +// Pointer specialization +template<class _Tp> +struct hash<_Tp*> { + _LIBBASTION_NODISCARD size_t operator()(_Tp* __ptr) const noexcept { + return __detail::__hash_integral(reinterpret_cast<size_t>(__ptr)); + } +}; + +// nullptr_t +template<> +struct hash<nullptr_t> { + _LIBBASTION_NODISCARD size_t operator()(nullptr_t) const noexcept { + return 0; + } +}; + +_LIBBASTION_END_NAMESPACE_STD + +#endif // _LIBBASTION_FUNCTIONAL_HASH_H |
