2 // Copyright 2005-2012 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 #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HEADER)
7 #define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HEADER
9 #include <boost/config.hpp>
10 #if defined(BOOST_HAS_PRAGMA_ONCE)
14 #include <boost/functional/hash/detail/float_functions.hpp>
15 #include <boost/functional/hash/detail/limits.hpp>
16 #include <boost/utility/enable_if.hpp>
17 #include <boost/integer/static_log2.hpp>
18 #include <boost/cstdint.hpp>
19 #include <boost/assert.hpp>
20 #include <boost/limits.hpp>
23 #if defined(BOOST_MSVC)
25 #if BOOST_MSVC >= 1400
26 #pragma warning(disable:6294) // Ill-defined for-loop: initial condition does
27 // not satisfy test. Loop body not executed
31 // Can we use fpclassify?
34 #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
35 #define BOOST_HASH_USE_FPCLASSIFY 0
38 #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
39 # if (defined(__USE_ISOC99) || defined(_GLIBCXX_USE_C99_MATH)) && \
40 !(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
41 # define BOOST_HASH_USE_FPCLASSIFY 1
43 # define BOOST_HASH_USE_FPCLASSIFY 0
48 # define BOOST_HASH_USE_FPCLASSIFY 0
55 inline void hash_float_combine(std::size_t& seed, std::size_t value)
57 seed ^= value + (seed<<6) + (seed>>2);
60 ////////////////////////////////////////////////////////////////////////
61 // Binary hash function
63 // Only used for floats with known iec559 floats, and certain values in
66 inline std::size_t hash_binary(char* ptr, std::size_t length)
70 if (length >= sizeof(std::size_t)) {
71 std::memcpy(&seed, ptr, sizeof(std::size_t));
72 length -= sizeof(std::size_t);
73 ptr += sizeof(std::size_t);
75 while(length >= sizeof(std::size_t)) {
76 std::size_t buffer = 0;
77 std::memcpy(&buffer, ptr, sizeof(std::size_t));
78 hash_float_combine(seed, buffer);
79 length -= sizeof(std::size_t);
80 ptr += sizeof(std::size_t);
85 std::size_t buffer = 0;
86 std::memcpy(&buffer, ptr, length);
87 hash_float_combine(seed, buffer);
93 template <typename Float, unsigned digits, unsigned max_exponent>
94 struct enable_binary_hash
96 BOOST_STATIC_CONSTANT(bool, value =
97 std::numeric_limits<Float>::is_iec559 &&
98 std::numeric_limits<Float>::digits == digits &&
99 std::numeric_limits<Float>::radix == 2 &&
100 std::numeric_limits<Float>::max_exponent == max_exponent);
103 template <typename Float>
104 inline std::size_t float_hash_impl(Float v,
105 BOOST_DEDUCED_TYPENAME boost::enable_if_c<
106 enable_binary_hash<Float, 24, 128>::value,
109 return hash_binary((char*) &v, 4);
113 template <typename Float>
114 inline std::size_t float_hash_impl(Float v,
115 BOOST_DEDUCED_TYPENAME boost::enable_if_c<
116 enable_binary_hash<Float, 53, 1024>::value,
119 return hash_binary((char*) &v, 8);
122 template <typename Float>
123 inline std::size_t float_hash_impl(Float v,
124 BOOST_DEDUCED_TYPENAME boost::enable_if_c<
125 enable_binary_hash<Float, 64, 16384>::value,
128 return hash_binary((char*) &v, 10);
131 template <typename Float>
132 inline std::size_t float_hash_impl(Float v,
133 BOOST_DEDUCED_TYPENAME boost::enable_if_c<
134 enable_binary_hash<Float, 113, 16384>::value,
137 return hash_binary((char*) &v, 16);
140 ////////////////////////////////////////////////////////////////////////
141 // Portable hash function
143 // Used as a fallback when the binary hash function isn't supported.
146 inline std::size_t float_hash_impl2(T v)
148 boost::hash_detail::call_frexp<T> frexp;
149 boost::hash_detail::call_ldexp<T> ldexp;
155 // A postive value is easier to hash, so combine the
156 // sign with the exponent and use the absolute value.
159 exp += limits<T>::max_exponent -
160 limits<T>::min_exponent;
163 v = ldexp(v, limits<std::size_t>::digits);
164 std::size_t seed = static_cast<std::size_t>(v);
165 v -= static_cast<T>(seed);
167 // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
168 std::size_t const length
169 = (limits<T>::digits *
170 boost::static_log2<limits<T>::radix>::value
171 + limits<std::size_t>::digits - 1)
172 / limits<std::size_t>::digits;
174 for(std::size_t i = 0; i != length; ++i)
176 v = ldexp(v, limits<std::size_t>::digits);
177 std::size_t part = static_cast<std::size_t>(v);
178 v -= static_cast<T>(part);
179 hash_float_combine(seed, part);
182 hash_float_combine(seed, static_cast<std::size_t>(exp));
187 #if !defined(BOOST_HASH_DETAIL_TEST_WITHOUT_GENERIC)
189 inline std::size_t float_hash_impl(T v, ...)
191 typedef BOOST_DEDUCED_TYPENAME select_hash_type<T>::type type;
192 return float_hash_impl2(static_cast<type>(v));
198 #if BOOST_HASH_USE_FPCLASSIFY
200 #include <boost/config/no_tr1/cmath.hpp>
204 namespace hash_detail
207 inline std::size_t float_hash_value(T v)
209 #if defined(fpclassify)
210 switch (fpclassify(v))
211 #elif BOOST_HASH_CONFORMANT_FLOATS
212 switch (std::fpclassify(v))
215 switch (fpclassify(v))
221 return (std::size_t)(v > 0 ? -1 : -2);
223 return (std::size_t)(-3);
226 return float_hash_impl(v, 0);
235 #else // !BOOST_HASH_USE_FPCLASSIFY
239 namespace hash_detail
242 inline bool is_zero(T v)
244 #if !defined(__GNUC__)
247 // GCC's '-Wfloat-equal' will complain about comparing
248 // v to 0, but because it disables warnings for system
249 // headers it won't complain if you use std::equal_to to
250 // compare with 0. Resulting in this silliness:
251 return std::equal_to<T>()(v, 0);
256 inline std::size_t float_hash_value(T v)
258 return boost::hash_detail::is_zero(v) ? 0 : float_hash_impl(v, 0);
263 #endif // BOOST_HASH_USE_FPCLASSIFY
265 #undef BOOST_HASH_USE_FPCLASSIFY
267 #if defined(BOOST_MSVC)