Chris@16: Chris@16: // Copyright 2005-2012 Daniel James. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HEADER) Chris@16: #define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HEADER Chris@16: Chris@101: #include Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: #pragma warning(push) Chris@16: #if BOOST_MSVC >= 1400 Chris@16: #pragma warning(disable:6294) // Ill-defined for-loop: initial condition does Chris@16: // not satisfy test. Loop body not executed Chris@16: #endif Chris@16: #endif Chris@16: Chris@16: // Can we use fpclassify? Chris@16: Chris@16: // STLport Chris@16: #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) Chris@16: #define BOOST_HASH_USE_FPCLASSIFY 0 Chris@16: Chris@16: // GNU libstdc++ 3 Chris@16: #elif defined(__GLIBCPP__) || defined(__GLIBCXX__) Chris@16: # if (defined(__USE_ISOC99) || defined(_GLIBCXX_USE_C99_MATH)) && \ Chris@16: !(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)) Chris@16: # define BOOST_HASH_USE_FPCLASSIFY 1 Chris@16: # else Chris@16: # define BOOST_HASH_USE_FPCLASSIFY 0 Chris@16: # endif Chris@16: Chris@16: // Everything else Chris@16: #else Chris@16: # define BOOST_HASH_USE_FPCLASSIFY 0 Chris@16: #endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace hash_detail Chris@16: { Chris@16: inline void hash_float_combine(std::size_t& seed, std::size_t value) Chris@16: { Chris@16: seed ^= value + (seed<<6) + (seed>>2); Chris@16: } Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////// Chris@16: // Binary hash function Chris@16: // Chris@16: // Only used for floats with known iec559 floats, and certain values in Chris@16: // numeric_limits Chris@16: Chris@16: inline std::size_t hash_binary(char* ptr, std::size_t length) Chris@16: { Chris@16: std::size_t seed = 0; Chris@16: Chris@16: if (length >= sizeof(std::size_t)) { Chris@101: std::memcpy(&seed, ptr, sizeof(std::size_t)); Chris@16: length -= sizeof(std::size_t); Chris@16: ptr += sizeof(std::size_t); Chris@16: Chris@16: while(length >= sizeof(std::size_t)) { Chris@16: std::size_t buffer = 0; Chris@16: std::memcpy(&buffer, ptr, sizeof(std::size_t)); Chris@16: hash_float_combine(seed, buffer); Chris@16: length -= sizeof(std::size_t); Chris@16: ptr += sizeof(std::size_t); Chris@16: } Chris@16: } Chris@16: Chris@16: if (length > 0) { Chris@16: std::size_t buffer = 0; Chris@16: std::memcpy(&buffer, ptr, length); Chris@16: hash_float_combine(seed, buffer); Chris@16: } Chris@16: Chris@16: return seed; Chris@16: } Chris@16: Chris@16: template Chris@16: struct enable_binary_hash Chris@16: { Chris@16: BOOST_STATIC_CONSTANT(bool, value = Chris@16: std::numeric_limits::is_iec559 && Chris@16: std::numeric_limits::digits == digits && Chris@16: std::numeric_limits::radix == 2 && Chris@16: std::numeric_limits::max_exponent == max_exponent); Chris@16: }; Chris@16: Chris@16: template Chris@16: inline std::size_t float_hash_impl(Float v, Chris@16: BOOST_DEDUCED_TYPENAME boost::enable_if_c< Chris@16: enable_binary_hash::value, Chris@16: std::size_t>::type) Chris@16: { Chris@16: return hash_binary((char*) &v, 4); Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: inline std::size_t float_hash_impl(Float v, Chris@16: BOOST_DEDUCED_TYPENAME boost::enable_if_c< Chris@16: enable_binary_hash::value, Chris@16: std::size_t>::type) Chris@16: { Chris@16: return hash_binary((char*) &v, 8); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::size_t float_hash_impl(Float v, Chris@16: BOOST_DEDUCED_TYPENAME boost::enable_if_c< Chris@16: enable_binary_hash::value, Chris@16: std::size_t>::type) Chris@16: { Chris@16: return hash_binary((char*) &v, 10); Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::size_t float_hash_impl(Float v, Chris@16: BOOST_DEDUCED_TYPENAME boost::enable_if_c< Chris@16: enable_binary_hash::value, Chris@16: std::size_t>::type) Chris@16: { Chris@16: return hash_binary((char*) &v, 16); Chris@16: } Chris@16: Chris@16: //////////////////////////////////////////////////////////////////////// Chris@16: // Portable hash function Chris@16: // Chris@16: // Used as a fallback when the binary hash function isn't supported. Chris@16: Chris@16: template Chris@16: inline std::size_t float_hash_impl2(T v) Chris@16: { Chris@16: boost::hash_detail::call_frexp frexp; Chris@16: boost::hash_detail::call_ldexp ldexp; Chris@16: Chris@16: int exp = 0; Chris@16: Chris@16: v = frexp(v, &exp); Chris@16: Chris@16: // A postive value is easier to hash, so combine the Chris@16: // sign with the exponent and use the absolute value. Chris@16: if(v < 0) { Chris@16: v = -v; Chris@16: exp += limits::max_exponent - Chris@16: limits::min_exponent; Chris@16: } Chris@16: Chris@16: v = ldexp(v, limits::digits); Chris@16: std::size_t seed = static_cast(v); Chris@16: v -= static_cast(seed); Chris@16: Chris@16: // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; Chris@16: std::size_t const length Chris@16: = (limits::digits * Chris@16: boost::static_log2::radix>::value Chris@16: + limits::digits - 1) Chris@16: / limits::digits; Chris@16: Chris@16: for(std::size_t i = 0; i != length; ++i) Chris@16: { Chris@16: v = ldexp(v, limits::digits); Chris@16: std::size_t part = static_cast(v); Chris@16: v -= static_cast(part); Chris@16: hash_float_combine(seed, part); Chris@16: } Chris@16: Chris@16: hash_float_combine(seed, exp); Chris@16: Chris@16: return seed; Chris@16: } Chris@16: Chris@16: #if !defined(BOOST_HASH_DETAIL_TEST_WITHOUT_GENERIC) Chris@16: template Chris@16: inline std::size_t float_hash_impl(T v, ...) Chris@16: { Chris@16: typedef BOOST_DEDUCED_TYPENAME select_hash_type::type type; Chris@16: return float_hash_impl2(static_cast(v)); Chris@16: } Chris@16: #endif Chris@16: } Chris@16: } Chris@16: Chris@16: #if BOOST_HASH_USE_FPCLASSIFY Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace hash_detail Chris@16: { Chris@16: template Chris@16: inline std::size_t float_hash_value(T v) Chris@16: { Chris@16: #if defined(fpclassify) Chris@16: switch (fpclassify(v)) Chris@16: #elif BOOST_HASH_CONFORMANT_FLOATS Chris@16: switch (std::fpclassify(v)) Chris@16: #else Chris@16: using namespace std; Chris@16: switch (fpclassify(v)) Chris@16: #endif Chris@16: { Chris@16: case FP_ZERO: Chris@16: return 0; Chris@16: case FP_INFINITE: Chris@16: return (std::size_t)(v > 0 ? -1 : -2); Chris@16: case FP_NAN: Chris@16: return (std::size_t)(-3); Chris@16: case FP_NORMAL: Chris@16: case FP_SUBNORMAL: Chris@16: return float_hash_impl(v, 0); Chris@16: default: Chris@16: BOOST_ASSERT(0); Chris@16: return 0; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: #else // !BOOST_HASH_USE_FPCLASSIFY Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace hash_detail Chris@16: { Chris@16: template Chris@16: inline bool is_zero(T v) Chris@16: { Chris@16: #if !defined(__GNUC__) Chris@16: return v == 0; Chris@16: #else Chris@16: // GCC's '-Wfloat-equal' will complain about comparing Chris@16: // v to 0, but because it disables warnings for system Chris@16: // headers it won't complain if you use std::equal_to to Chris@16: // compare with 0. Resulting in this silliness: Chris@16: return std::equal_to()(v, 0); Chris@16: #endif Chris@16: } Chris@16: Chris@16: template Chris@16: inline std::size_t float_hash_value(T v) Chris@16: { Chris@16: return boost::hash_detail::is_zero(v) ? 0 : float_hash_impl(v, 0); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: #endif // BOOST_HASH_USE_FPCLASSIFY Chris@16: Chris@16: #undef BOOST_HASH_USE_FPCLASSIFY Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif