1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-12-03 21:15:42 +00:00
Sprout/sprout/math/ilogb.hpp

73 lines
2 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2013 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)
=============================================================================*/
2013-02-07 09:14:50 +00:00
#ifndef SPROUT_MATH_ILOGB_HPP
#define SPROUT_MATH_ILOGB_HPP
#include <climits>
#include <cfloat>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/math/logb.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/math/iszero.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
namespace math {
namespace detail {
2013-04-25 06:36:19 +00:00
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
inline SPROUT_CONSTEXPR int
builtin_ilogb(float x) {
return __builtin_ilogbf(x);
}
inline SPROUT_CONSTEXPR int
builtin_ilogb(double x) {
return __builtin_ilogb(x);
2013-02-07 09:14:50 +00:00
}
inline SPROUT_CONSTEXPR int
builtin_ilogb(long double x) {
return __builtin_ilogbl(x);
2013-02-07 09:14:50 +00:00
}
#endif
2013-02-07 09:14:50 +00:00
} // namespace detail
//
// ilogb
//
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR int
ilogb(FloatType x) {
return sprout::math::iszero(x) ? FP_ILOGB0
: sprout::math::isinf(x) ? INT_MAX
: sprout::math::isnan(x) ? FP_ILOGBNAN
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: sprout::math::detail::builtin_ilogb(x)
#else
: static_cast<int>(sprout::math::logb(x))
#endif
;
}
template<
typename IntType,
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR int
ilogb(IntType x) {
return sprout::math::ilogb(static_cast<double>(x));
}
2013-02-07 09:14:50 +00:00
} // namespace math
using sprout::math::ilogb;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_ILOGB_HPP