// Copyright 2021-2023 Peter Dimov // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #ifndef BOOST_HASH_DETAIL_HASH_INTEGRAL_HPP #define BOOST_HASH_DETAIL_HASH_INTEGRAL_HPP #include #include #include #include namespace boost { namespace hash_detail { // libstdc++ doesn't provide support for __int128 in the standard traits template struct is_integral: public std::is_integral { }; template struct is_unsigned: public std::is_unsigned { }; template struct make_unsigned: public std::make_unsigned { }; #if defined(__SIZEOF_INT128__) template<> struct is_integral<__int128_t>: public std::true_type { }; template<> struct is_integral<__uint128_t>: public std::true_type { }; template<> struct is_unsigned<__int128_t>: public std::false_type { }; template<> struct is_unsigned<__uint128_t>: public std::true_type { }; template<> struct make_unsigned<__int128_t> { typedef __uint128_t type; }; template<> struct make_unsigned<__uint128_t> { typedef __uint128_t type; }; #endif template sizeof(std::size_t)), bool is_unsigned = is_unsigned::value, std::size_t size_t_bits = sizeof(std::size_t) * CHAR_BIT, std::size_t type_bits = sizeof(T) * CHAR_BIT> struct hash_integral_impl; template struct hash_integral_impl { static std::size_t fn( T v ) { return static_cast( v ); } }; template struct hash_integral_impl { static std::size_t fn( T v ) { typedef typename make_unsigned::type U; if( v >= 0 ) { return hash_integral_impl::fn( static_cast( v ) ); } else { return ~hash_integral_impl::fn( static_cast( ~static_cast( v ) ) ); } } }; template struct hash_integral_impl { static std::size_t fn( T v ) { std::size_t seed = 0; seed = static_cast( v >> 32 ) + hash_detail::hash_mix( seed ); seed = static_cast( v & 0xFFFFFFFF ) + hash_detail::hash_mix( seed ); return seed; } }; template struct hash_integral_impl { static std::size_t fn( T v ) { std::size_t seed = 0; seed = static_cast( v >> 96 ) + hash_detail::hash_mix( seed ); seed = static_cast( v >> 64 ) + hash_detail::hash_mix( seed ); seed = static_cast( v >> 32 ) + hash_detail::hash_mix( seed ); seed = static_cast( v ) + hash_detail::hash_mix( seed ); return seed; } }; template struct hash_integral_impl { static std::size_t fn( T v ) { std::size_t seed = 0; seed = static_cast( v >> 64 ) + hash_detail::hash_mix( seed ); seed = static_cast( v ) + hash_detail::hash_mix( seed ); return seed; } }; } // namespace hash_detail template typename std::enable_if::value, std::size_t>::type hash_value( T v ) { return hash_detail::hash_integral_impl::fn( v ); } } // namespace boost #endif // #ifndef BOOST_HASH_DETAIL_HASH_INTEGRAL_HPP