2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 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 09:14:50 +00:00
|
|
|
#ifndef SPROUT_MATH_LOGB_HPP
|
|
|
|
#define SPROUT_MATH_LOGB_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2013-02-07 09:14:50 +00:00
|
|
|
#include <sprout/detail/pow.hpp>
|
2013-04-25 06:36:19 +00:00
|
|
|
#include <sprout/math/detail/config.hpp>
|
|
|
|
#include <sprout/math/detail/float_compute.hpp>
|
2013-05-04 15:06:30 +00:00
|
|
|
#include <sprout/math/isnan.hpp>
|
2013-02-07 09:14:50 +00:00
|
|
|
#include <sprout/math/log_a.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/math/trunc.hpp>
|
2013-02-10 02:54:54 +00:00
|
|
|
#include <sprout/math/itrunc.hpp>
|
2013-02-07 09:14:50 +00:00
|
|
|
#include <sprout/type_traits/enabler_if.hpp>
|
2013-04-25 06:36:19 +00:00
|
|
|
#include <sprout/type_traits/float_promote.hpp>
|
2013-02-07 09:14:50 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace math {
|
|
|
|
namespace detail {
|
2013-10-25 03:29:16 +00:00
|
|
|
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
|
|
|
inline SPROUT_CONSTEXPR float
|
|
|
|
builtin_logb(float x) {
|
|
|
|
return __builtin_logbf(x);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR double
|
|
|
|
builtin_logb(double x) {
|
|
|
|
return __builtin_logb(x);
|
|
|
|
}
|
|
|
|
inline SPROUT_CONSTEXPR long double
|
|
|
|
builtin_logb(long double x) {
|
|
|
|
return __builtin_logbl(x);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-07 09:14:50 +00:00
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_3_neg_lo(T x, T x0, T base, T exp) {
|
|
|
|
return base < 1 ? sprout::math::detail::logb_impl_3_neg_lo(
|
2013-08-06 15:15:09 +00:00
|
|
|
x, x0 * sprout::numeric_limits<T>::radix, x / (x0 / sprout::numeric_limits<T>::radix), exp - 1
|
2013-02-07 14:12:57 +00:00
|
|
|
)
|
2013-02-07 09:14:50 +00:00
|
|
|
: exp
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_3_neg_hi(T x, T x0, T base, T exp) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return !(base < sprout::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_3_neg_hi(
|
|
|
|
x, x0 / sprout::numeric_limits<T>::radix, x / (x0 * sprout::numeric_limits<T>::radix), exp + 1
|
2013-02-07 14:12:57 +00:00
|
|
|
)
|
2013-02-07 09:14:50 +00:00
|
|
|
: exp
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_3_pos_lo(T x, T x0, T base, T exp) {
|
|
|
|
return base < 1 ? sprout::math::detail::logb_impl_3_pos_lo(
|
2013-08-06 15:15:09 +00:00
|
|
|
x, x0 * sprout::numeric_limits<T>::radix, x / (x0 / sprout::numeric_limits<T>::radix), exp - 1
|
2013-02-07 14:12:57 +00:00
|
|
|
)
|
2013-02-07 09:14:50 +00:00
|
|
|
: exp
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_3_pos_hi(T x, T x0, T base, T exp) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return !(base < sprout::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_3_pos_hi(
|
|
|
|
x, x0 / sprout::numeric_limits<T>::radix, x / (x0 * sprout::numeric_limits<T>::radix), exp + 1
|
2013-02-07 14:12:57 +00:00
|
|
|
)
|
2013-02-07 09:14:50 +00:00
|
|
|
: exp
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_3(T x, T x0, T base, T exp) {
|
|
|
|
return x < 1
|
|
|
|
? base < 1 ? sprout::math::detail::logb_impl_3_neg_lo(
|
2013-08-06 15:15:09 +00:00
|
|
|
x, x0 * sprout::numeric_limits<T>::radix, x / (x0 / sprout::numeric_limits<T>::radix), exp - 1
|
2013-02-07 14:12:57 +00:00
|
|
|
)
|
2013-08-06 15:15:09 +00:00
|
|
|
: !(base < sprout::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_3_neg_hi(
|
|
|
|
x, x0 / sprout::numeric_limits<T>::radix, x / (x0 * sprout::numeric_limits<T>::radix), exp + 1
|
2013-04-25 06:36:19 +00:00
|
|
|
)
|
2013-02-07 09:14:50 +00:00
|
|
|
: exp
|
2013-04-25 06:36:19 +00:00
|
|
|
: base < 1 ? sprout::math::detail::logb_impl_3_pos_lo(
|
2013-08-06 15:15:09 +00:00
|
|
|
x, x0 * sprout::numeric_limits<T>::radix, x / (x0 / sprout::numeric_limits<T>::radix), exp - 1
|
2013-02-07 14:12:57 +00:00
|
|
|
)
|
2013-08-06 15:15:09 +00:00
|
|
|
: !(base < sprout::numeric_limits<T>::radix) ? sprout::math::detail::logb_impl_3_pos_hi(
|
|
|
|
x, x0 / sprout::numeric_limits<T>::radix, x / (x0 * sprout::numeric_limits<T>::radix), exp + 1
|
2013-04-25 06:36:19 +00:00
|
|
|
)
|
2013-02-07 09:14:50 +00:00
|
|
|
: exp
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_2(T x, T x0, T exp) {
|
|
|
|
return sprout::math::detail::logb_impl_3(x, x0, x / x0, exp);
|
2013-02-07 09:14:50 +00:00
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
2013-04-25 06:36:19 +00:00
|
|
|
logb_impl_1(T x, T exp) {
|
|
|
|
return sprout::math::detail::logb_impl_2(
|
2013-08-06 15:15:09 +00:00
|
|
|
x, sprout::detail::pow_n(T(sprout::numeric_limits<T>::radix), sprout::math::itrunc<std::intmax_t>(exp)), exp
|
2013-02-10 02:54:54 +00:00
|
|
|
);
|
2013-02-07 09:14:50 +00:00
|
|
|
}
|
2013-04-25 06:36:19 +00:00
|
|
|
template<typename T>
|
|
|
|
inline SPROUT_CONSTEXPR T
|
|
|
|
logb_impl(T x) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return x < 0 ? sprout::math::detail::logb_impl_1(-x, sprout::math::trunc(sprout::math::log_a(T(sprout::numeric_limits<T>::radix), -x)))
|
|
|
|
: sprout::math::detail::logb_impl_1(x, sprout::math::trunc(sprout::math::log_a(T(sprout::numeric_limits<T>::radix), x)))
|
2013-04-25 06:36:19 +00:00
|
|
|
;
|
|
|
|
}
|
2013-10-25 03:29:16 +00:00
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// logb
|
|
|
|
//
|
|
|
|
template<
|
|
|
|
typename FloatType,
|
|
|
|
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR FloatType
|
|
|
|
logb(FloatType x) {
|
|
|
|
return sprout::math::isnan(x) ? x
|
|
|
|
: x == 0 ? -sprout::numeric_limits<FloatType>::infinity()
|
2013-04-25 06:36:19 +00:00
|
|
|
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
2013-04-25 15:25:35 +00:00
|
|
|
# if defined(__GNUC__)
|
2013-10-25 03:29:16 +00:00
|
|
|
: x == -sprout::numeric_limits<FloatType>::infinity()
|
|
|
|
? sprout::numeric_limits<FloatType>::infinity()
|
2013-04-25 15:25:35 +00:00
|
|
|
# endif
|
2013-10-25 03:29:16 +00:00
|
|
|
: sprout::math::detail::builtin_logb(x)
|
2013-04-25 06:36:19 +00:00
|
|
|
#else
|
2013-10-25 03:29:16 +00:00
|
|
|
: x == sprout::numeric_limits<FloatType>::infinity() || x == -sprout::numeric_limits<FloatType>::infinity()
|
|
|
|
? sprout::numeric_limits<FloatType>::infinity()
|
|
|
|
: static_cast<FloatType>(sprout::math::detail::logb_impl(static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x)))
|
2013-04-25 06:36:19 +00:00
|
|
|
#endif
|
2013-10-25 03:29:16 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<
|
|
|
|
typename IntType,
|
|
|
|
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR double
|
|
|
|
logb(IntType x) {
|
|
|
|
return sprout::math::logb(static_cast<double>(x));
|
|
|
|
}
|
2013-02-07 09:14:50 +00:00
|
|
|
} // namespace math
|
|
|
|
|
|
|
|
using sprout::math::logb;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_MATH_LOGB_HPP
|