2
// Copyright 2005-2008 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
// Based on Peter Dimov's proposal
7
// http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf
10
#if !defined(BOOST_FUNCTIONAL_DETAIL_HASH_FLOAT_HEADER)
11
#define BOOST_FUNCTIONAL_DETAIL_HASH_FLOAT_HEADER
13
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
17
#if defined(BOOST_MSVC)
19
#if BOOST_MSVC >= 1400
20
#pragma warning(disable:6294) // Ill-defined for-loop: initial condition does
21
// not satisfy test. Loop body not executed
25
#include <boost/functional/detail/float_functions.hpp>
26
#include <boost/integer/static_log2.hpp>
27
#include <boost/cstdint.hpp>
28
#include <boost/limits.hpp>
29
#include <boost/assert.hpp>
31
// Select implementation for the current platform.
34
#if defined(__CYGWIN__)
35
# if defined(__i386__) || defined(_M_IX86)
36
# define BOOST_HASH_USE_x86_BINARY_HASH
40
#elif defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
41
// fpclassify aren't good enough on STLport.
44
#elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
45
# if (defined(__USE_ISOC99) || defined(_GLIBCXX_USE_C99_MATH)) && \
46
!(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
47
# define BOOST_HASH_USE_FPCLASSIFY
50
// Dinkumware Library, on Visual C++
51
#elif (defined(_YVALS) && !defined(__IBMCPP__)) || defined(_CPPLIB_VER)
53
// Not using _fpclass because it is only available for double.
57
// On OpenBSD, numeric_limits is not reliable for long doubles, but
58
// the macros defined in <float.h> are.
60
#if defined(__OpenBSD__)
69
struct limits : std::numeric_limits<T> {};
71
#if defined(__OpenBSD__)
73
struct limits<long double>
74
: std::numeric_limits<long double>
76
static long double epsilon() {
80
static long double (max)() {
84
static long double (min)() {
88
BOOST_STATIC_CONSTANT(int, digits = LDBL_MANT_DIG);
89
BOOST_STATIC_CONSTANT(int, max_exponent = LDBL_MAX_EXP);
90
BOOST_STATIC_CONSTANT(int, min_exponent = LDBL_MIN_EXP);
94
inline void hash_float_combine(std::size_t& seed, std::size_t value)
96
seed ^= value + (seed<<6) + (seed>>2);
99
// A simple, non-portable hash algorithm for x86.
100
#if defined(BOOST_HASH_USE_x86_BINARY_HASH)
101
inline std::size_t float_hash_impl(float v)
103
boost::uint32_t* ptr = (boost::uint32_t*)&v;
104
std::size_t seed = *ptr;
108
inline std::size_t float_hash_impl(double v)
110
boost::uint32_t* ptr = (boost::uint32_t*)&v;
111
std::size_t seed = *ptr++;
112
hash_float_combine(seed, *ptr);
116
inline std::size_t float_hash_impl(long double v)
118
boost::uint32_t* ptr = (boost::uint32_t*)&v;
119
std::size_t seed = *ptr++;
120
hash_float_combine(seed, *ptr++);
121
hash_float_combine(seed, *(boost::uint16_t*)ptr);
128
inline std::size_t float_hash_impl(T v)
132
v = boost::hash_detail::call_frexp(v, &exp);
134
// A postive value is easier to hash, so combine the
135
// sign with the exponent.
138
exp += limits<T>::max_exponent -
139
limits<T>::min_exponent;
142
// The result of frexp is always between 0.5 and 1, so its
143
// top bit will always be 1. Subtract by 0.5 to remove that.
145
v = boost::hash_detail::call_ldexp(v,
146
limits<std::size_t>::digits + 1);
147
std::size_t seed = static_cast<std::size_t>(v);
150
// ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
151
std::size_t const length
152
= (limits<T>::digits *
153
boost::static_log2<limits<T>::radix>::value - 1)
154
/ limits<std::size_t>::digits;
156
for(std::size_t i = 0; i != length; ++i)
158
v = boost::hash_detail::call_ldexp(v,
159
limits<std::size_t>::digits);
160
std::size_t part = static_cast<std::size_t>(v);
162
hash_float_combine(seed, part);
165
hash_float_combine(seed, exp);
172
inline std::size_t float_hash_value(T v)
174
#if defined(BOOST_HASH_USE_FPCLASSIFY)
176
switch (fpclassify(v)) {
180
return (std::size_t)(v > 0 ? -1 : -2);
182
return (std::size_t)(-3);
185
return float_hash_impl(v);
191
return v == 0 ? 0 : float_hash_impl(v);
197
#if defined(BOOST_MSVC)