mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-10-05 13:00:00 +00:00
support hash: floating point
This commit is contained in:
parent
63ef07d124
commit
7aa10d1e08
276 changed files with 542 additions and 325 deletions
|
@ -5,7 +5,7 @@
|
|||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/array/array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/array/array.hpp>
|
||||
#include <sprout/type_traits/is_int.hpp>
|
||||
#include <sprout/type_traits/is_uint.hpp>
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef SPROUT_MATH_FLOAT_MANTISSA_HPP
|
||||
#define SPROUT_MATH_FLOAT_MANTISSA_HPP
|
||||
|
||||
#include <cfloat>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
|
@ -18,7 +18,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
float_mantissa(FloatType x) {
|
||||
return x / sprout::detail::pow_n(FloatType(FLT_RADIX), sprout::math::float_exponent(x));
|
||||
return x / sprout::detail::pow_n(FloatType(std::numeric_limits<FloatType>::radix), sprout::math::float_exponent(x));
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#ifndef SPROUT_MATH_FLOAT_PAIR_HPP
|
||||
#define SPROUT_MATH_FLOAT_PAIR_HPP
|
||||
|
||||
#include <cfloat>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/detail/pow.hpp>
|
||||
#include <sprout/math/float_exponent.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
#include <sprout/utility/pair/pair.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
|
@ -16,7 +16,7 @@ namespace sprout {
|
|||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<T, int>
|
||||
float_pair_impl(T x, int exp) {
|
||||
return sprout::pair<T, int>(x / sprout::detail::pow_n(T(FLT_RADIX), exp), exp);
|
||||
return sprout::pair<T, int>(x / sprout::detail::pow_n(T(std::numeric_limits<T>::radix), exp), exp);
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <climits>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/array/array.hpp>
|
||||
#include <sprout/cstdlib/abs.hpp>
|
||||
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
||||
# include <sprout/workaround/recursive_function_template.hpp>
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
#ifndef SPROUT_MATH_LOGB_HPP
|
||||
#define SPROUT_MATH_LOGB_HPP
|
||||
|
||||
#include <climits>
|
||||
#include <cfloat>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
#include <sprout/detail/pow.hpp>
|
||||
#include <sprout/math/log_a.hpp>
|
||||
#include <sprout/math/trunc.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
@ -18,28 +18,36 @@ namespace sprout {
|
|||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_neg_lo(T x, T x0, T base, T exp) {
|
||||
return base < 1 ? sprout::math::detail::logb_impl_2_neg_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp - 1)
|
||||
return base < 1 ? sprout::math::detail::logb_impl_2_neg_lo(
|
||||
x, x0 * std::numeric_limits<T>::radix, x / (x0 / std::numeric_limits<T>::radix), exp - 1
|
||||
)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_neg_hi(T x, T x0, T base, T exp) {
|
||||
return !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_neg_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp + 1)
|
||||
return !(base < std::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_2_neg_hi(
|
||||
x, x0 / std::numeric_limits<T>::radix, x / (x0 * std::numeric_limits<T>::radix), exp + 1
|
||||
)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_pos_lo(T x, T x0, T base, T exp) {
|
||||
return base < 1 ? sprout::math::detail::logb_impl_2_pos_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp + 1)
|
||||
return base < 1 ? sprout::math::detail::logb_impl_2_pos_lo(
|
||||
x, x0 * std::numeric_limits<T>::radix, x / (x0 / std::numeric_limits<T>::radix), exp + 1
|
||||
)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2_pos_hi(T x, T x0, T base, T exp) {
|
||||
return !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_pos_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp - 1)
|
||||
return !(base < std::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_2_pos_hi(
|
||||
x, x0 / std::numeric_limits<T>::radix, x / (x0 * std::numeric_limits<T>::radix), exp - 1
|
||||
)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
|
@ -47,11 +55,19 @@ namespace sprout {
|
|||
inline SPROUT_CONSTEXPR T
|
||||
logb_impl_2(T x, T x0, T base, T exp) {
|
||||
return exp < 0
|
||||
? base < 1 ? sprout::math::detail::logb_impl_2_neg_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp - 1)
|
||||
: !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_neg_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp + 1)
|
||||
? base < 1 ? sprout::math::detail::logb_impl_2_neg_lo(
|
||||
x, x0 * std::numeric_limits<T>::radix, x / (x0 / std::numeric_limits<T>::radix), exp - 1
|
||||
)
|
||||
: !(base < std::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_2_neg_hi(
|
||||
x, x0 / std::numeric_limits<T>::radix, x / (x0 * std::numeric_limits<T>::radix), exp + 1
|
||||
)
|
||||
: exp
|
||||
: base < 1 ? sprout::math::detail::logb_impl_2_pos_lo(x, x0 * FLT_RADIX, x / (x0 / FLT_RADIX), exp + 1)
|
||||
: !(base < FLT_RADIX) ? sprout::math::detail::logb_impl_2_pos_hi(x, x0 / FLT_RADIX, x / (x0 * FLT_RADIX), exp - 1)
|
||||
: base < 1 ? sprout::math::detail::logb_impl_2_pos_lo(
|
||||
x, x0 * std::numeric_limits<T>::radix, x / (x0 / std::numeric_limits<T>::radix), exp + 1
|
||||
)
|
||||
: !(base < std::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_2_pos_hi(
|
||||
x, x0 / std::numeric_limits<T>::radix, x / (x0 * std::numeric_limits<T>::radix), exp - 1
|
||||
)
|
||||
: exp
|
||||
;
|
||||
}
|
||||
|
@ -66,7 +82,7 @@ namespace sprout {
|
|||
return std::numeric_limits<std::intmax_t>::max() < exp || std::numeric_limits<std::intmax_t>::min() > exp
|
||||
? SPROUT_MATH_THROW_LARGE_FLOAT_ROUNDING(std::domain_error("trunc: large float rounding."), exp)
|
||||
: sprout::math::detail::logb_impl_1(
|
||||
x, sprout::detail::pow_n(T(FLT_RADIX), static_cast<std::intmax_t>(exp)),
|
||||
x, sprout::detail::pow_n(T(std::numeric_limits<T>::radix), static_cast<std::intmax_t>(exp)),
|
||||
static_cast<T>(static_cast<std::intmax_t>(exp))
|
||||
)
|
||||
;
|
||||
|
@ -78,8 +94,12 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
logb(FloatType x) {
|
||||
return x < 0 ? sprout::math::detail::logb_impl(-x, sprout::math::trunc(sprout::math::log_a(FloatType(FLT_RADIX), -x)))
|
||||
: sprout::math::detail::logb_impl(x, sprout::math::trunc(sprout::math::log_a(FloatType(FLT_RADIX), x)))
|
||||
return x < 0 ? sprout::math::detail::logb_impl(
|
||||
-x, sprout::math::trunc(sprout::math::log_a(FloatType(std::numeric_limits<FloatType>::radix), -x))
|
||||
)
|
||||
: sprout::math::detail::logb_impl(
|
||||
x, sprout::math::trunc(sprout::math::log_a(FloatType(std::numeric_limits<FloatType>::radix), x))
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef SPROUT_MATH_SCALBLN_HPP
|
||||
#define SPROUT_MATH_SCALBLN_HPP
|
||||
|
||||
#include <cfloat>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
|
@ -17,7 +17,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
scalbln(FloatType x, long exp) {
|
||||
return x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp);
|
||||
return x * sprout::detail::pow_n(FloatType(std::numeric_limits<FloatType>::radix), exp);
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef SPROUT_MATH_SCALBN_HPP
|
||||
#define SPROUT_MATH_SCALBN_HPP
|
||||
|
||||
#include <cfloat>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/detail/config.hpp>
|
||||
|
@ -17,7 +17,7 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
scalbn(FloatType x, int exp) {
|
||||
return x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp);
|
||||
return x * sprout::detail::pow_n(FloatType(std::numeric_limits<FloatType>::radix), exp);
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue