Sprout/sprout/functional/hash/detail/hash_float.hpp

86 lines
3.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2013-02-07 14:12:57 +00:00
#ifndef SPROUT_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HPP
#define SPROUT_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HPP
#include <cstddef>
#include <sprout/config.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/functional/hash/hash_fwd.hpp>
#include <sprout/math/fpclassify.hpp>
2013-02-10 02:54:54 +00:00
#include <sprout/math/ldexp.hpp>
#include <sprout/math/float2_sig_exp.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/detail/integer/static_log2.hpp>
namespace sprout {
namespace detail {
inline SPROUT_CONSTEXPR std::size_t
hash_float_combine(std::size_t seed, std::size_t value) {
return seed ^ (value + (seed << 6) + (seed >> 2));
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value_impl_4(T v, int exp, std::size_t seed, std::size_t length, std::size_t i = 0) {
return i != length ? sprout::detail::float_hash_value_impl_4(
2013-08-06 15:15:09 +00:00
sprout::ldexp(v - static_cast<T>(static_cast<std::size_t>(v)), sprout::numeric_limits<std::size_t>::digits),
2013-02-07 14:12:57 +00:00
exp, sprout::detail::hash_float_combine(seed, static_cast<std::size_t>(v)),
length, i + 1
)
: sprout::detail::hash_float_combine(seed, exp)
;
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value_impl_3(T v, int exp) {
return sprout::detail::float_hash_value_impl_4(
2013-08-06 15:15:09 +00:00
sprout::ldexp(v - static_cast<T>(static_cast<std::size_t>(v)), sprout::numeric_limits<std::size_t>::digits),
2013-02-07 14:12:57 +00:00
exp, static_cast<std::size_t>(v),
2013-08-06 15:15:09 +00:00
(sprout::numeric_limits<T>::digits * sprout::detail::static_log2<sprout::numeric_limits<T>::radix>::value + sprout::numeric_limits<std::size_t>::digits - 1)
/ sprout::numeric_limits<std::size_t>::digits
2013-02-07 14:12:57 +00:00
);
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value_impl_2(T v, int exp) {
2013-08-06 15:15:09 +00:00
return sprout::detail::float_hash_value_impl_3(sprout::ldexp(v, sprout::numeric_limits<std::size_t>::digits), exp);
2013-02-07 14:12:57 +00:00
}
template<typename T, typename P>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value_impl_1(P const& p) {
return ((p.first) < 0) ? sprout::detail::float_hash_value_impl_2(
2013-08-06 15:15:09 +00:00
-p.first, p.second + (sprout::numeric_limits<T>::max_exponent - sprout::numeric_limits<T>::min_exponent)
2013-02-07 14:12:57 +00:00
)
: sprout::detail::float_hash_value_impl_2(p.first, p.second)
;
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value_impl(T v) {
2013-02-10 02:54:54 +00:00
return sprout::detail::float_hash_value_impl_1<T>(sprout::math::float2_sig_exp(v));
2013-02-07 14:12:57 +00:00
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value_1(T v, int fp) {
return fp == FP_ZERO ? 0
: fp == FP_INFINITE ? std::size_t(v > 0 ? -1 : -2)
: fp == FP_NAN ? std::size_t(-3)
: sprout::detail::float_hash_value_impl(v)
;
}
template<typename T>
inline SPROUT_CONSTEXPR std::size_t
float_hash_value(T v) {
2013-04-23 10:03:03 +00:00
return sprout::detail::float_hash_value_1(v, sprout::fpclassify(v));
2013-02-19 16:12:56 +00:00
}
2013-02-07 14:12:57 +00:00
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HPP