/*============================================================================= Copyright (c) 2011-2014 Bolero MURAKAMI 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) =============================================================================*/ #ifndef SPROUT_MATH_LOGB2_HPP #define SPROUT_MATH_LOGB2_HPP #include #include #include #include #include #if SPROUT_FLT_RADIX_IS_2 # include #else # include # include # include # include # include # include # include # include #endif namespace sprout { namespace math { #if SPROUT_FLT_RADIX_IS_2 // // logb2 // template< typename FloatType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR FloatType logb2(FloatType x) { return sprout::math::logb(x); } template< typename IntType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR double logb2(IntType x) { return sprout::math::logb(x); } #else namespace detail { template inline SPROUT_CONSTEXPR T logb2_impl_3_neg_lo(T x, T x0, T base, T exp) { return base < 1 ? sprout::math::detail::logb2_impl_3_neg_lo( x, x0 * 2, x / (x0 / 2), exp - 1 ) : exp ; } template inline SPROUT_CONSTEXPR T logb2_impl_3_neg_hi(T x, T x0, T base, T exp) { return !(base < 2) ? sprout::math::detail::logb2_impl_3_neg_hi( x, x0 / 2, x / (x0 * 2), exp + 1 ) : exp ; } template inline SPROUT_CONSTEXPR T logb2_impl_3_pos_lo(T x, T x0, T base, T exp) { return base < 1 ? sprout::math::detail::logb2_impl_3_pos_lo( x, x0 * 2, x / (x0 / 2), exp - 1 ) : exp ; } template inline SPROUT_CONSTEXPR T logb2_impl_3_pos_hi(T x, T x0, T base, T exp) { return !(base < 2) ? sprout::math::detail::logb2_impl_3_pos_hi( x, x0 / 2, x / (x0 * 2), exp + 1 ) : exp ; } template inline SPROUT_CONSTEXPR T logb2_impl_3(T x, T x0, T base, T exp) { return x < 1 ? base < 1 ? sprout::math::detail::logb2_impl_3_neg_lo( x, x0 * 2, x / (x0 / 2), exp - 1 ) : !(base < 2) ? sprout::math::detail::logb2_impl_3_neg_hi( x, x0 / 2, x / (x0 * 2), exp + 1 ) : exp : base < 1 ? sprout::math::detail::logb2_impl_3_pos_lo( x, x0 * 2, x / (x0 / 2), exp - 1 ) : !(base < 2) ? sprout::math::detail::logb2_impl_3_pos_hi( x, x0 / 2, x / (x0 * 2), exp + 1 ) : exp ; } template inline SPROUT_CONSTEXPR T logb2_impl_2(T x, T x0, T exp) { return sprout::math::detail::logb2_impl_3(x, x0, x / x0, exp); } template inline SPROUT_CONSTEXPR T logb2_impl_1(T x, T exp) { return sprout::math::detail::logb2_impl_2( x, sprout::detail::pow_n(T(2), sprout::math::itrunc(exp)), exp ); } template 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))) ; } } // namespace detail // // logb2 // template< typename FloatType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR FloatType logb2(FloatType x) { return sprout::math::isnan(x) ? x : x == 0 ? -sprout::numeric_limits::infinity() : x == sprout::numeric_limits::infinity() || x == -sprout::numeric_limits::infinity() ? sprout::numeric_limits::infinity() : static_cast(sprout::math::detail::logb2_impl(static_cast::type>(x))) ; } template< typename IntType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR double logb2(IntType x) { return sprout::math::logb2(static_cast(x)); } #endif } // namespace math using sprout::math::logb2; } // namespace sprout #endif // #ifndef SPROUT_MATH_LOGB2_HPP