From b0f91804a98125ce98aca2226f6b86e640902927 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 21 May 2009 21:21:44 +0000 Subject: [PATCH] Move the two different hash float implementation into their own header. [SVN r53160] --- .../functional/hash/detail/hash_float.hpp | 133 +++++------------- .../hash/detail/hash_float_generic.hpp | 85 +++++++++++ .../functional/hash/detail/hash_float_x86.hpp | 56 ++++++++ 3 files changed, 180 insertions(+), 94 deletions(-) create mode 100644 include/boost/functional/hash/detail/hash_float_generic.hpp create mode 100644 include/boost/functional/hash/detail/hash_float_x86.hpp diff --git a/include/boost/functional/hash/detail/hash_float.hpp b/include/boost/functional/hash/detail/hash_float.hpp index 9844c6a..b5ed719 100644 --- a/include/boost/functional/hash/detail/hash_float.hpp +++ b/include/boost/functional/hash/detail/hash_float.hpp @@ -18,123 +18,57 @@ #endif #endif +#include #include #include #include #include #include -// Select implementation for the current platform. +// Include hash implementation for the current platform. // Cygwn #if defined(__CYGWIN__) # if defined(__i386__) || defined(_M_IX86) -# define BOOST_HASH_USE_x86_BINARY_HASH +# include +# else +# include # endif +#else +# include +#endif + +// Can we use fpclassify? // STLport -#elif defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) -// fpclassify aren't good enough on STLport. +#if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) +#defined BOOST_HASH_USE_FPCLASSIFY 0 // GNU libstdc++ 3 #elif defined(__GLIBCPP__) || defined(__GLIBCXX__) # if (defined(__USE_ISOC99) || defined(_GLIBCXX_USE_C99_MATH)) && \ !(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) -# define BOOST_HASH_USE_FPCLASSIFY +# define BOOST_HASH_USE_FPCLASSIFY 1 +# else +# define BOOST_HASH_USE_FPCLASSIFY 0 # endif -// Dinkumware Library, on Visual C++ -#elif (defined(_YVALS) && !defined(__IBMCPP__)) || defined(_CPPLIB_VER) - -// Not using _fpclass because it is only available for double. - +// Everything else +#else +# define BOOST_HASH_USE_FPCLASSIFY 0 #endif +#if BOOST_HASH_USE_FPCLASSIFY + +#include + namespace boost { namespace hash_detail { - inline void hash_float_combine(std::size_t& seed, std::size_t value) - { - seed ^= value + (seed<<6) + (seed>>2); - } - -// A simple, non-portable hash algorithm for x86. -#if defined(BOOST_HASH_USE_x86_BINARY_HASH) - inline std::size_t float_hash_impl(float v) - { - boost::uint32_t* ptr = (boost::uint32_t*)&v; - std::size_t seed = *ptr; - return seed; - } - - inline std::size_t float_hash_impl(double v) - { - boost::uint32_t* ptr = (boost::uint32_t*)&v; - std::size_t seed = *ptr++; - hash_float_combine(seed, *ptr); - return seed; - } - - inline std::size_t float_hash_impl(long double v) - { - boost::uint32_t* ptr = (boost::uint32_t*)&v; - std::size_t seed = *ptr++; - hash_float_combine(seed, *ptr++); - hash_float_combine(seed, *(boost::uint16_t*)ptr); - return seed; - } - -#else - - template - inline std::size_t float_hash_impl(T v) - { - int exp = 0; - - v = boost::hash_detail::call_frexp(v, &exp); - - // A postive value is easier to hash, so combine the - // sign with the exponent. - if(v < 0) { - v = -v; - exp += limits::max_exponent - - limits::min_exponent; - } - - // The result of frexp is always between 0.5 and 1, so its - // top bit will always be 1. Subtract by 0.5 to remove that. - v -= T(0.5); - v = boost::hash_detail::call_ldexp(v, - limits::digits + 1); - std::size_t seed = static_cast(v); - v -= seed; - - // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; - std::size_t const length - = (limits::digits * - boost::static_log2::radix>::value - 1) - / limits::digits; - - for(std::size_t i = 0; i != length; ++i) - { - v = boost::hash_detail::call_ldexp(v, - limits::digits); - std::size_t part = static_cast(v); - v -= part; - hash_float_combine(seed, part); - } - - hash_float_combine(seed, exp); - - return seed; - } -#endif - template inline std::size_t float_hash_value(T v) { -#if defined(BOOST_HASH_USE_FPCLASSIFY) using namespace std; switch (fpclassify(v)) { case FP_ZERO: @@ -150,15 +84,26 @@ namespace boost BOOST_ASSERT(0); return 0; } -#else - return v == 0 ? 0 : float_hash_impl(v); -#endif } } } -#if defined(BOOST_MSVC) -#pragma warning(pop) -#endif +#else // !BOOST_HASH_USE_FPCLASSIFY + +namespace boost +{ + namespace hash_detail + { + template + inline std::size_t float_hash_value(T v) + { + return v == 0 ? 0 : float_hash_impl(v); + } + } +} + +#endif // BOOST_HASH_USE_FPCLASSIFY + +#undef BOOST_HASH_USE_FPCLASSIFY #endif diff --git a/include/boost/functional/hash/detail/hash_float_generic.hpp b/include/boost/functional/hash/detail/hash_float_generic.hpp new file mode 100644 index 0000000..ee6b92a --- /dev/null +++ b/include/boost/functional/hash/detail/hash_float_generic.hpp @@ -0,0 +1,85 @@ + +// Copyright 2005-2009 Daniel James. +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// A general purpose hash function for non-zero floating point values. + +#if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER) +#define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER + +#include +#include +#include + +#if defined(_MSC_VER) && (_MSC_VER >= 1020) +# pragma once +#endif + +#if defined(BOOST_MSVC) +#pragma warning(push) +#if BOOST_MSVC >= 1400 +#pragma warning(disable:6294) // Ill-defined for-loop: initial condition does + // not satisfy test. Loop body not executed +#endif +#endif + +namespace boost +{ + namespace hash_detail + { + inline void hash_float_combine(std::size_t& seed, std::size_t value) + { + seed ^= value + (seed<<6) + (seed>>2); + } + + template + inline std::size_t float_hash_impl(T v) + { + int exp = 0; + + v = boost::hash_detail::call_frexp(v, &exp); + + // A postive value is easier to hash, so combine the + // sign with the exponent. + if(v < 0) { + v = -v; + exp += limits::max_exponent - + limits::min_exponent; + } + + // The result of frexp is always between 0.5 and 1, so its + // top bit will always be 1. Subtract by 0.5 to remove that. + v -= T(0.5); + v = boost::hash_detail::call_ldexp(v, + limits::digits + 1); + std::size_t seed = static_cast(v); + v -= seed; + + // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; + std::size_t const length + = (limits::digits * + boost::static_log2::radix>::value - 1) + / limits::digits; + + for(std::size_t i = 0; i != length; ++i) + { + v = boost::hash_detail::call_ldexp(v, + limits::digits); + std::size_t part = static_cast(v); + v -= part; + hash_float_combine(seed, part); + } + + hash_float_combine(seed, exp); + + return seed; + } + } +} + +#if defined(BOOST_MSVC) +#pragma warning(pop) +#endif + +#endif \ No newline at end of file diff --git a/include/boost/functional/hash/detail/hash_float_x86.hpp b/include/boost/functional/hash/detail/hash_float_x86.hpp new file mode 100644 index 0000000..b39bb0d --- /dev/null +++ b/include/boost/functional/hash/detail/hash_float_x86.hpp @@ -0,0 +1,56 @@ + +// Copyright 2005-2009 Daniel James. +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// A non-portable hash function form non-zero floats on x86. +// +// Even if you're on an x86 platform, this might not work if their floating +// point isn't set up as this expects. So this should only be used if it's +// absolutely certain that it will work. + +#if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_X86_HEADER) +#define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_X86_HEADER + +#include + +#if defined(_MSC_VER) && (_MSC_VER >= 1020) +# pragma once +#endif + +namespace boost +{ + namespace hash_detail + { + inline void hash_float_combine(std::size_t& seed, std::size_t value) + { + seed ^= value + (seed<<6) + (seed>>2); + } + + inline std::size_t float_hash_impl(float v) + { + boost::uint32_t* ptr = (boost::uint32_t*)&v; + std::size_t seed = *ptr; + return seed; + } + + inline std::size_t float_hash_impl(double v) + { + boost::uint32_t* ptr = (boost::uint32_t*)&v; + std::size_t seed = *ptr++; + hash_float_combine(seed, *ptr); + return seed; + } + + inline std::size_t float_hash_impl(long double v) + { + boost::uint32_t* ptr = (boost::uint32_t*)&v; + std::size_t seed = *ptr++; + hash_float_combine(seed, *ptr++); + hash_float_combine(seed, *(boost::uint16_t*)ptr); + return seed; + } + } +} + +#endif