diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index ed1d5bf..4f5dace 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #include #include @@ -74,91 +76,20 @@ namespace boost typedef T argument_type; typedef std::size_t result_type; }; - - struct enable_hash_value { typedef std::size_t type; }; - - template struct basic_numbers {}; - template struct long_numbers; - template struct ulong_numbers; - template struct float_numbers {}; - - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; - -#if !defined(BOOST_NO_INTRINSIC_WCHAR_T) - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; -#endif - -#if !defined(BOOST_NO_CXX11_CHAR16_T) - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; -#endif - -#if !defined(BOOST_NO_CXX11_CHAR32_T) - template <> struct basic_numbers : - boost::hash_detail::enable_hash_value {}; -#endif - - // long_numbers is defined like this to allow for separate - // specialization for long_long and int128_type, in case - // they conflict. - template struct long_numbers2 {}; - template struct ulong_numbers2 {}; - template struct long_numbers : long_numbers2 {}; - template struct ulong_numbers : ulong_numbers2 {}; - -#if !defined(BOOST_NO_LONG_LONG) - template <> struct long_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct ulong_numbers : - boost::hash_detail::enable_hash_value {}; -#endif - -#if defined(BOOST_HAS_INT128) - template <> struct long_numbers2 : - boost::hash_detail::enable_hash_value {}; - template <> struct ulong_numbers2 : - boost::hash_detail::enable_hash_value {}; -#endif - - template <> struct float_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct float_numbers : - boost::hash_detail::enable_hash_value {}; - template <> struct float_numbers : - boost::hash_detail::enable_hash_value {}; } template - typename boost::hash_detail::basic_numbers::type hash_value(T); - template - typename boost::hash_detail::long_numbers::type hash_value(T); - template - typename boost::hash_detail::ulong_numbers::type hash_value(T); + typename boost::enable_if, std::size_t>::type + hash_value(T); template typename boost::enable_if, std::size_t>::type hash_value(T); + template + typename boost::enable_if, std::size_t>::type + hash_value(T); + template std::size_t hash_value(T* const&); template< class T, unsigned N > @@ -177,9 +108,6 @@ namespace boost std::basic_string_view > const&); #endif - template - typename boost::hash_detail::float_numbers::type hash_value(T); - #if !defined(BOOST_NO_CXX17_HDR_OPTIONAL) template std::size_t hash_value(std::optional const&); @@ -204,46 +132,33 @@ namespace boost namespace hash_detail { - template - inline std::size_t hash_value_signed(T val) + template struct hash_integral_impl; + + template struct hash_integral_impl { - const unsigned int size_t_bits = std::numeric_limits::digits; - // ceiling(std::numeric_limits::digits / size_t_bits) - 1 - const int length = (std::numeric_limits::digits - 1) - / static_cast(size_t_bits); + static std::size_t fn( T v ) + { + return static_cast( v ); + } + }; - std::size_t seed = 0; - T positive = val < 0 ? -1 - val : val; - - // Hopefully, this loop can be unrolled. - for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) - { - seed ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2); - } - seed ^= (std::size_t) val + (seed<<6) + (seed>>2); - - return seed; - } - - template - inline std::size_t hash_value_unsigned(T val) + template struct hash_integral_impl { - const unsigned int size_t_bits = std::numeric_limits::digits; - // ceiling(std::numeric_limits::digits / size_t_bits) - 1 - const int length = (std::numeric_limits::digits - 1) - / static_cast(size_t_bits); + static std::size_t fn( T v ) + { + // 4294967291 = 2^32-5, biggest prime under 2^32 + return static_cast( static_cast::type>( v ) % 4294967291 ); + } + }; - std::size_t seed = 0; - - // Hopefully, this loop can be unrolled. - for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) - { - seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2); - } - seed ^= (std::size_t) val + (seed<<6) + (seed>>2); - - return seed; - } + template struct hash_integral_impl + { + static std::size_t fn( T v ) + { + // 18446744073709551557ULL = 2^64-59, biggest prime under 2^64 + return static_cast( static_cast::type>( v ) % 18446744073709551557ULL ); + } + }; template struct hash_combine_impl { @@ -298,21 +213,10 @@ namespace boost } template - typename boost::hash_detail::basic_numbers::type hash_value(T v) + typename boost::enable_if, std::size_t>::type + hash_value(T v) { - return static_cast(v); - } - - template - typename boost::hash_detail::long_numbers::type hash_value(T v) - { - return hash_detail::hash_value_signed(v); - } - - template - typename boost::hash_detail::ulong_numbers::type hash_value(T v) - { - return hash_detail::hash_value_unsigned(v); + return hash_detail::hash_integral_impl sizeof(std::size_t)), (sizeof(std::size_t) * CHAR_BIT >= 64)>::fn( v ); } template @@ -322,6 +226,13 @@ namespace boost return static_cast(v); } + template + typename boost::enable_if, std::size_t>::type + hash_value(T v) + { + return boost::hash_detail::float_hash_value(v); + } + // Implementation by Alberto Barbati and Dave Harris. template std::size_t hash_value(T* const& v) { @@ -427,12 +338,6 @@ namespace boost } #endif - template - typename boost::hash_detail::float_numbers::type hash_value(T v) - { - return boost::hash_detail::float_hash_value(v); - } - #if !defined(BOOST_NO_CXX17_HDR_OPTIONAL) template inline std::size_t hash_value(std::optional const& v) { diff --git a/test/hash_complex_test.cpp b/test/hash_complex_test.cpp index 5ecacbf..5afd7ac 100644 --- a/test/hash_complex_test.cpp +++ b/test/hash_complex_test.cpp @@ -35,7 +35,7 @@ int main() {} #endif #endif -#if defined(__GNUC__) && !defined(BOOST_INTEL_CXX_VERSION) +#if ( defined(__GNUC__) || defined(__clang__) ) && !defined(BOOST_INTEL_CXX_VERSION) #pragma GCC diagnostic ignored "-Wfloat-equal" #endif diff --git a/test/hash_float_test.hpp b/test/hash_float_test.hpp index fd31687..6b28a2e 100644 --- a/test/hash_float_test.hpp +++ b/test/hash_float_test.hpp @@ -30,7 +30,7 @@ #endif #endif -#if defined(__GNUC__) && !defined(BOOST_INTEL_CXX_VERSION) +#if ( defined(__GNUC__) || defined(__clang__) ) && !defined(BOOST_INTEL_CXX_VERSION) #pragma GCC diagnostic ignored "-Wfloat-equal" #endif diff --git a/test/hash_number_test.cpp b/test/hash_number_test.cpp index 72ab626..b56d2c6 100644 --- a/test/hash_number_test.cpp +++ b/test/hash_number_test.cpp @@ -26,7 +26,7 @@ #pragma warning(disable:4310) // cast truncates constant value #endif -#if defined(__GNUC__) && !defined(BOOST_INTEL_CXX_VERSION) +#if ( defined(__GNUC__) || defined(__clang__) ) && !defined(BOOST_INTEL_CXX_VERSION) #pragma GCC diagnostic ignored "-Wfloat-equal" #endif @@ -37,8 +37,7 @@ void numeric_extra_tests(typename { typedef boost::hash_detail::limits limits; - if(limits::is_signed || - limits::digits <= boost::hash_detail::limits::digits) + if(limits::digits <= boost::hash_detail::limits::digits) { BOOST_TEST(BOOST_HASH_TEST_NAMESPACE::hash_value(T(-5)) == (std::size_t)T(-5)); }