2 // Copyright 2005-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // A general purpose hash function for non-zero floating point values.
8 #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER)
9 #define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER
11 #include <boost/functional/hash/detail/float_functions.hpp>
12 #include <boost/integer/static_log2.hpp>
13 #include <boost/functional/hash/detail/limits.hpp>
15 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
19 #if defined(BOOST_MSVC)
21 #if BOOST_MSVC >= 1400
22 #pragma warning(disable:6294) // Ill-defined for-loop: initial condition does
23 // not satisfy test. Loop body not executed
31 inline void hash_float_combine(std::size_t& seed, std::size_t value)
33 seed ^= value + (seed<<6) + (seed>>2);
37 inline std::size_t float_hash_impl2(T v)
39 boost::hash_detail::call_frexp<T> frexp;
40 boost::hash_detail::call_ldexp<T> ldexp;
46 // A postive value is easier to hash, so combine the
47 // sign with the exponent and use the absolute value.
50 exp += limits<T>::max_exponent -
51 limits<T>::min_exponent;
54 // The result of frexp is always between 0.5 and 1, so its
55 // top bit will always be 1. Subtract by 0.5 to remove that.
57 v = ldexp(v, limits<std::size_t>::digits + 1);
58 std::size_t seed = static_cast<std::size_t>(v);
61 // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
62 std::size_t const length
63 = (limits<T>::digits *
64 boost::static_log2<limits<T>::radix>::value - 1)
65 / limits<std::size_t>::digits;
67 for(std::size_t i = 0; i != length; ++i)
69 v = ldexp(v, limits<std::size_t>::digits);
70 std::size_t part = static_cast<std::size_t>(v);
72 hash_float_combine(seed, part);
75 hash_float_combine(seed, exp);
81 inline std::size_t float_hash_impl(T v)
83 typedef BOOST_DEDUCED_TYPENAME select_hash_type<T>::type type;
84 return float_hash_impl2(static_cast<type>(v));
89 #if defined(BOOST_MSVC)