1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

fix: math functions (support for inferior C++14 standard)

add C++14 constexpr modifying algorithms
This commit is contained in:
bolero-MURAKAMI 2013-10-25 12:29:16 +09:00
parent 58cff54e0d
commit c58c9cc0fc
106 changed files with 3465 additions and 2144 deletions

View file

@ -26,76 +26,91 @@
namespace sprout {
namespace math {
namespace detail {
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
inline SPROUT_CONSTEXPR float
builtin_pow(float x, float y) {
return __builtin_powf(x, y);
}
inline SPROUT_CONSTEXPR double
builtin_pow(double x, double y) {
return __builtin_pow(x, y);
}
inline SPROUT_CONSTEXPR long double
builtin_pow(long double x, long double y) {
return __builtin_powl(x, y);
}
#endif
template<typename T>
inline SPROUT_CONSTEXPR T
pow_impl(T x, T y) {
return sprout::math::exp(y * sprout::math::log(x));
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
pow(FloatType x, FloatType y) {
return x == 1 ? FloatType(1)
: y == 0 ? FloatType(1)
: sprout::math::isnan(y) ? y
: sprout::math::isnan(x) ? x
: x == 0
? y < 0
? sprout::math::is_odd(y) ? sprout::math::copysign(sprout::numeric_limits<FloatType>::infinity(), x)
: sprout::numeric_limits<FloatType>::infinity()
: sprout::math::is_odd(y) ? x
: FloatType(0)
: x == -1 && (y == sprout::numeric_limits<FloatType>::infinity() || y == -sprout::numeric_limits<FloatType>::infinity()) ? FloatType(1)
: y == -sprout::numeric_limits<FloatType>::infinity()
? sprout::math::fabs(x) < 1 ? sprout::numeric_limits<FloatType>::infinity()
: FloatType(0)
: y == sprout::numeric_limits<FloatType>::infinity()
? sprout::math::fabs(x) < 1 ? FloatType(0)
: sprout::numeric_limits<FloatType>::infinity()
: x == -sprout::numeric_limits<FloatType>::infinity()
? y < 0
? sprout::math::is_odd(y) ? -FloatType(0)
: FloatType(0)
: sprout::math::is_odd(y) ? -sprout::numeric_limits<FloatType>::infinity()
: sprout::numeric_limits<FloatType>::infinity()
: x == sprout::numeric_limits<FloatType>::infinity()
? y < 0 ? FloatType(0)
: sprout::numeric_limits<FloatType>::infinity()
: x < 0 && !sprout::math::is_integer(y) ? sprout::numeric_limits<FloatType>::quiet_NaN()
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: std::pow(x, y)
#else
: static_cast<FloatType>(sprout::math::detail::pow_impl(
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y)
))
#endif
;
}
template<
typename ArithmeticType1,
typename ArithmeticType2,
typename sprout::enabler_if<
std::is_arithmetic<ArithmeticType1>::value && std::is_arithmetic<ArithmeticType2>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type
pow(ArithmeticType1 x, ArithmeticType2 y) {
typedef typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type type;
return sprout::math::detail::pow(static_cast<type>(x), static_cast<type>(y));
}
} // namespace detail
//
// pow
//
// issue:
// [ !SPROUT_USE_BUILTIN_CMATH_FUNCTION ]
// pow(-0, y) returns -<2D>‡ for y an odd integer < 0.
// # returns +<2B>‡ . ( same as pow(+0, y) )
//
using sprout::math::detail::pow;
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
pow(FloatType x, FloatType y) {
return x == 1 ? FloatType(1)
: y == 0 ? FloatType(1)
: sprout::math::isnan(y) ? y
: sprout::math::isnan(x) ? x
: x == 0
? y < 0
? sprout::math::is_odd(y) ? sprout::math::copysign(sprout::numeric_limits<FloatType>::infinity(), x)
: sprout::numeric_limits<FloatType>::infinity()
: sprout::math::is_odd(y) ? x
: FloatType(0)
: x == -1 && (y == sprout::numeric_limits<FloatType>::infinity() || y == -sprout::numeric_limits<FloatType>::infinity()) ? FloatType(1)
: y == -sprout::numeric_limits<FloatType>::infinity()
? sprout::math::fabs(x) < 1 ? sprout::numeric_limits<FloatType>::infinity()
: FloatType(0)
: y == sprout::numeric_limits<FloatType>::infinity()
? sprout::math::fabs(x) < 1 ? FloatType(0)
: sprout::numeric_limits<FloatType>::infinity()
: x == -sprout::numeric_limits<FloatType>::infinity()
? y < 0
? sprout::math::is_odd(y) ? -FloatType(0)
: FloatType(0)
: sprout::math::is_odd(y) ? -sprout::numeric_limits<FloatType>::infinity()
: sprout::numeric_limits<FloatType>::infinity()
: x == sprout::numeric_limits<FloatType>::infinity()
? y < 0 ? FloatType(0)
: sprout::numeric_limits<FloatType>::infinity()
: x < 0 && !sprout::math::is_integer(y) ? sprout::numeric_limits<FloatType>::quiet_NaN()
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: sprout::math::detail::builtin_pow(x, y)
#else
: static_cast<FloatType>(sprout::math::detail::pow_impl(
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y)
))
#endif
;
}
template<
typename ArithmeticType1,
typename ArithmeticType2,
typename sprout::enabler_if<
std::is_arithmetic<ArithmeticType1>::value && std::is_arithmetic<ArithmeticType2>::value
>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type
pow(ArithmeticType1 x, ArithmeticType2 y) {
typedef typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type type;
return sprout::math::pow(static_cast<type>(x), static_cast<type>(y));
}
} // namespace math
using sprout::math::pow;