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

@ -23,6 +23,21 @@
namespace sprout {
namespace math {
namespace detail {
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
inline SPROUT_CONSTEXPR float
builtin_hypot(float x, float y) {
return __builtin_hypotf(x, y);
}
inline SPROUT_CONSTEXPR double
builtin_hypot(double x, double y) {
return __builtin_hypot(x, y);
}
inline SPROUT_CONSTEXPR long double
builtin_hypot(long double x, long double y) {
return __builtin_hypotl(x, y);
}
#endif
template<typename T>
inline SPROUT_CONSTEXPR T
hypot_impl_2(T t, T w) {
@ -41,47 +56,47 @@ namespace sprout {
hypot_impl(T x, T y) {
return sprout::math::detail::hypot_impl_1(sprout::math::fabs(x), sprout::math::fabs(y));
}
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
hypot(FloatType x, FloatType y) {
return y == sprout::numeric_limits<FloatType>::infinity() || y == -sprout::numeric_limits<FloatType>::infinity()
? sprout::numeric_limits<FloatType>::infinity()
: x == sprout::numeric_limits<FloatType>::infinity() || x == -sprout::numeric_limits<FloatType>::infinity()
? sprout::numeric_limits<FloatType>::infinity()
: sprout::math::isnan(y) ? y
: sprout::math::isnan(x) ? x
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: std::hypot(x, y)
#else
: y == 0 ? sprout::math::fabs(x)
: x == 0 ? sprout::math::fabs(y)
: static_cast<FloatType>(sprout::math::detail::hypot_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
hypot(ArithmeticType1 x, ArithmeticType2 y) {
typedef typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type type;
return sprout::math::detail::hypot(static_cast<type>(x), static_cast<type>(y));
}
} // namespace detail
//
// hypot
//
template<
typename FloatType,
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
>
inline SPROUT_CONSTEXPR FloatType
hypot(FloatType x, FloatType y) {
return y == sprout::numeric_limits<FloatType>::infinity() || y == -sprout::numeric_limits<FloatType>::infinity()
? sprout::numeric_limits<FloatType>::infinity()
: x == sprout::numeric_limits<FloatType>::infinity() || x == -sprout::numeric_limits<FloatType>::infinity()
? sprout::numeric_limits<FloatType>::infinity()
: sprout::math::isnan(y) ? y
: sprout::math::isnan(x) ? x
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
: sprout::math::detail::builtin_hypot(x, y)
#else
: y == 0 ? sprout::math::fabs(x)
: x == 0 ? sprout::math::fabs(y)
: static_cast<FloatType>(sprout::math::detail::hypot_impl(
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(x),
static_cast<typename sprout::math::detail::float_compute<FloatType>::type>(y)
))
#endif
;
}
using sprout::math::detail::hypot;
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
hypot(ArithmeticType1 x, ArithmeticType2 y) {
typedef typename sprout::float_promote<ArithmeticType1, ArithmeticType2>::type type;
return sprout::math::hypot(static_cast<type>(x), static_cast<type>(y));
}
} // namespace math
using sprout::math::hypot;