1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/math/logb2.hpp

150 lines
4.5 KiB
C++
Raw Normal View History

2013-02-10 02:54:54 +00:00
#ifndef SPROUT_MATH_LOGB2_HPP
#define SPROUT_MATH_LOGB2_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/type_traits/enabler_if.hpp>
2013-04-25 06:36:19 +00:00
#include <sprout/type_traits/float_promote.hpp>
2013-02-10 02:54:54 +00:00
#if SPROUT_FLT_RADIX_IS_2
# include <sprout/math/logb.hpp>
#else
# include <cstdint>
2013-02-14 09:02:43 +00:00
# include <limits>
2013-02-10 02:54:54 +00:00
# include <sprout/detail/pow.hpp>
2013-04-25 06:36:19 +00:00
# include <sprout/math/detail/float_compute.hpp>
2013-05-04 15:06:30 +00:00
# include <sprout/math/isnan.hpp>
2013-02-10 02:54:54 +00:00
# include <sprout/math/log_a.hpp>
# include <sprout/math/trunc.hpp>
# include <sprout/math/itrunc.hpp>
#endif
namespace sprout {
namespace math {
namespace detail {
#if SPROUT_FLT_RADIX_IS_2
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
logb2(FloatType x) {
2013-04-25 06:36:19 +00:00
return sprout::math::logb(x);
2013-02-10 02:54:54 +00:00
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
logb2(IntType x) {
2013-04-25 06:36:19 +00:00
return sprout::math::logb(x);
2013-02-10 02:54:54 +00:00
}
#else
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_3_neg_lo(T x, T x0, T base, T exp) {
return base < 1 ? sprout::math::detail::logb2_impl_3_neg_lo(
2013-02-10 02:54:54 +00:00
x, x0 * 2, x / (x0 / 2), exp - 1
)
: exp
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_3_neg_hi(T x, T x0, T base, T exp) {
return !(base < 2) ? sprout::math::detail::logb2_impl_3_neg_hi(
2013-02-10 02:54:54 +00:00
x, x0 / 2, x / (x0 * 2), exp + 1
)
: exp
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_3_pos_lo(T x, T x0, T base, T exp) {
return base < 1 ? sprout::math::detail::logb2_impl_3_pos_lo(
2013-05-04 15:06:30 +00:00
x, x0 * 2, x / (x0 / 2), exp - 1
2013-02-10 02:54:54 +00:00
)
: exp
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_3_pos_hi(T x, T x0, T base, T exp) {
return !(base < 2) ? sprout::math::detail::logb2_impl_3_pos_hi(
2013-05-04 15:06:30 +00:00
x, x0 / 2, x / (x0 * 2), exp + 1
2013-02-10 02:54:54 +00:00
)
: exp
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_3(T x, T x0, T base, T exp) {
return x < 1
? base < 1 ? sprout::math::detail::logb2_impl_3_neg_lo(
2013-02-10 02:54:54 +00:00
x, x0 * 2, x / (x0 / 2), exp - 1
)
2013-04-25 06:36:19 +00:00
: !(base < 2) ? sprout::math::detail::logb2_impl_3_neg_hi(
2013-02-10 02:54:54 +00:00
x, x0 / 2, x / (x0 * 2), exp + 1
)
: exp
2013-04-25 06:36:19 +00:00
: base < 1 ? sprout::math::detail::logb2_impl_3_pos_lo(
2013-05-04 15:06:30 +00:00
x, x0 * 2, x / (x0 / 2), exp - 1
2013-02-10 02:54:54 +00:00
)
2013-04-25 06:36:19 +00:00
: !(base < 2) ? sprout::math::detail::logb2_impl_3_pos_hi(
2013-05-04 15:06:30 +00:00
x, x0 / 2, x / (x0 * 2), exp + 1
2013-02-10 02:54:54 +00:00
)
: exp
;
}
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_2(T x, T x0, T exp) {
return sprout::math::detail::logb2_impl_3(x, x0, x / x0, exp);
2013-02-10 02:54:54 +00:00
}
template<typename T>
inline SPROUT_CONSTEXPR T
2013-04-25 06:36:19 +00:00
logb2_impl_1(T x, T exp) {
return sprout::math::detail::logb2_impl_2(
x, sprout::detail::pow_n(T(2), sprout::math::itrunc<std::intmax_t>(exp)), exp
2013-02-10 02:54:54 +00:00
);
}
2013-04-25 06:36:19 +00:00
template<typename T>
inline SPROUT_CONSTEXPR T
logb2_impl(T x) {
return x < 0 ? sprout::math::detail::logb2_impl_1(-x, sprout::math::trunc(sprout::math::log_a(T(2), -x)))
: sprout::math::detail::logb2_impl_1(x, sprout::math::trunc(sprout::math::log_a(T(2), x)))
;
}
2013-02-10 02:54:54 +00:00
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
logb2(FloatType x) {
2013-05-04 15:06:30 +00:00
return sprout::math::isnan(x) ? x
: x == 0 ? -std::numeric_limits<FloatType>::infinity()
2013-02-14 09:02:43 +00:00
: x == std::numeric_limits<FloatType>::infinity() || x == -std::numeric_limits<FloatType>::infinity()
? std::numeric_limits<FloatType>::infinity()
2013-04-25 06:36:19 +00:00
: static_cast<FloatType>(sprout::math::detail::logb2_impl(static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x)))
2013-02-10 02:54:54 +00:00
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR double
logb2(IntType x) {
return sprout::math::detail::logb2(static_cast<double>(x));
}
#endif
} // namespace detail
using sprout::math::detail::logb2;
} // namespace math
using sprout::math::logb2;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_LOGB2_HPP