From 8bb7d43646f5cd60bfc6bbc0662d386882ee3ad4 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 20 Sep 2022 15:01:45 +0300 Subject: [PATCH] Simplify hash_value for floating point --- include/boost/container_hash/hash.hpp | 75 ++++----------------------- 1 file changed, 11 insertions(+), 64 deletions(-) diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index 533de16..092d256 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -176,12 +176,11 @@ namespace boost { template::digits, - std::size_t size_t_bits = sizeof(std::size_t) * CHAR_BIT> + int Digits = std::numeric_limits::digits> struct hash_float_impl; // float - template struct hash_float_impl + template struct hash_float_impl { static std::size_t fn( T v ) { @@ -193,35 +192,19 @@ namespace boost }; // double - template struct hash_float_impl + template struct hash_float_impl { static std::size_t fn( T v ) { boost::uint64_t w; std::memcpy( &w, &v, sizeof( v ) ); - return w; - } - }; - - template struct hash_float_impl - { - static std::size_t fn( T v ) - { - boost::uint32_t w[ 2 ]; - std::memcpy( &w, &v, sizeof( v ) ); - - std::size_t seed = 0; - - seed = static_cast( w[0] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[1] ) + hash_detail::hash_mix( seed ); - - return seed; + return hash_value( w ); } }; // 80 bit long double in 12 bytes - template struct hash_float_impl + template struct hash_float_impl { static std::size_t fn( T v ) { @@ -230,32 +213,15 @@ namespace boost std::size_t seed = 0; - seed = static_cast( w[0] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[1] ) + hash_detail::hash_mix( seed ); - - return seed; - } - }; - - template struct hash_float_impl - { - static std::size_t fn( T v ) - { - boost::uint32_t w[ 3 ] = {}; - std::memcpy( &w, &v, 80 / CHAR_BIT ); - - std::size_t seed = 0; - - seed = static_cast( w[0] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[1] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[2] ) + hash_detail::hash_mix( seed ); + seed = hash_value( w[0] ) + hash_detail::hash_mix( seed ); + seed = hash_value( w[1] ) + hash_detail::hash_mix( seed ); return seed; } }; // 80 bit long double in 16 bytes - template struct hash_float_impl + template struct hash_float_impl { static std::size_t fn( T v ) { @@ -264,32 +230,15 @@ namespace boost std::size_t seed = 0; - seed = static_cast( w[0] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[1] ) + hash_detail::hash_mix( seed ); - - return seed; - } - }; - - template struct hash_float_impl - { - static std::size_t fn( T v ) - { - boost::uint32_t w[ 3 ] = {}; - std::memcpy( &w, &v, 80 / CHAR_BIT ); - - std::size_t seed = 0; - - seed = static_cast( w[0] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[1] ) + hash_detail::hash_mix( seed ); - seed = static_cast( w[2] ) + hash_detail::hash_mix( seed ); + seed = hash_value( w[0] ) + hash_detail::hash_mix( seed ); + seed = hash_value( w[1] ) + hash_detail::hash_mix( seed ); return seed; } }; // 128 bit long double - template struct hash_float_impl + template struct hash_float_impl { static std::size_t fn( T v ) { @@ -630,6 +579,4 @@ namespace boost #endif } -#undef BOOST_FUNCTIONAL_HASH_ROTL32 - #endif // #ifndef BOOST_FUNCTIONAL_HASH_HASH_HPP