1 | |
---|
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) |
---|
5 | |
---|
6 | // A general purpose hash function for non-zero floating point values. |
---|
7 | |
---|
8 | #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER) |
---|
9 | #define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER |
---|
10 | |
---|
11 | #include <boost/functional/hash/detail/float_functions.hpp> |
---|
12 | #include <boost/integer/static_log2.hpp> |
---|
13 | #include <boost/functional/hash/detail/limits.hpp> |
---|
14 | |
---|
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
---|
16 | # pragma once |
---|
17 | #endif |
---|
18 | |
---|
19 | #if defined(BOOST_MSVC) |
---|
20 | #pragma warning(push) |
---|
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 |
---|
24 | #endif |
---|
25 | #endif |
---|
26 | |
---|
27 | namespace boost |
---|
28 | { |
---|
29 | namespace hash_detail |
---|
30 | { |
---|
31 | inline void hash_float_combine(std::size_t& seed, std::size_t value) |
---|
32 | { |
---|
33 | seed ^= value + (seed<<6) + (seed>>2); |
---|
34 | } |
---|
35 | |
---|
36 | template <class T> |
---|
37 | inline std::size_t float_hash_impl2(T v) |
---|
38 | { |
---|
39 | boost::hash_detail::call_frexp<T> frexp; |
---|
40 | boost::hash_detail::call_ldexp<T> ldexp; |
---|
41 | |
---|
42 | int exp = 0; |
---|
43 | |
---|
44 | v = frexp(v, &exp); |
---|
45 | |
---|
46 | // A postive value is easier to hash, so combine the |
---|
47 | // sign with the exponent and use the absolute value. |
---|
48 | if(v < 0) { |
---|
49 | v = -v; |
---|
50 | exp += limits<T>::max_exponent - |
---|
51 | limits<T>::min_exponent; |
---|
52 | } |
---|
53 | |
---|
54 | v = ldexp(v, limits<std::size_t>::digits); |
---|
55 | std::size_t seed = static_cast<std::size_t>(v); |
---|
56 | v -= seed; |
---|
57 | |
---|
58 | // ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1; |
---|
59 | std::size_t const length |
---|
60 | = (limits<T>::digits * |
---|
61 | boost::static_log2<limits<T>::radix>::value |
---|
62 | + limits<std::size_t>::digits - 1) |
---|
63 | / limits<std::size_t>::digits; |
---|
64 | |
---|
65 | for(std::size_t i = 0; i != length; ++i) |
---|
66 | { |
---|
67 | v = ldexp(v, limits<std::size_t>::digits); |
---|
68 | std::size_t part = static_cast<std::size_t>(v); |
---|
69 | v -= part; |
---|
70 | hash_float_combine(seed, part); |
---|
71 | } |
---|
72 | |
---|
73 | hash_float_combine(seed, exp); |
---|
74 | |
---|
75 | return seed; |
---|
76 | } |
---|
77 | |
---|
78 | template <class T> |
---|
79 | inline std::size_t float_hash_impl(T v) |
---|
80 | { |
---|
81 | typedef BOOST_DEDUCED_TYPENAME select_hash_type<T>::type type; |
---|
82 | return float_hash_impl2(static_cast<type>(v)); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | #if defined(BOOST_MSVC) |
---|
88 | #pragma warning(pop) |
---|
89 | #endif |
---|
90 | |
---|
91 | #endif |
---|