mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
replace implementation <cmath> functions to Sprout.Math.Functions
This commit is contained in:
parent
6ccd6e1cf4
commit
6bb2d0fb87
21 changed files with 212 additions and 106 deletions
42
sprout/math/abs.hpp
Normal file
42
sprout/math/abs.hpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef SPROUT_MATH_ABS_HPP
|
||||
#define SPROUT_MATH_ABS_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <cmath>
|
||||
#endif
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
abs(FloatType x) {
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
abs(FloatType x) {
|
||||
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
using std::abs;
|
||||
# else
|
||||
using sprout::math::detail::abs;
|
||||
# endif
|
||||
return abs(x);
|
||||
}
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::abs;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_ABS_HPP
|
|
@ -21,6 +21,7 @@ namespace sprout {
|
|||
inline SPROUT_CONSTEXPR FloatType
|
||||
acosh(FloatType x) {
|
||||
return x < 1 ? std::numeric_limits<FloatType>::quiet_NaN()
|
||||
: x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: sprout::math::detail::log(x + sprout::math::detail::sqrt(x * x - 1))
|
||||
;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef SPROUT_MATH_ASINH_HPP
|
||||
#define SPROUT_MATH_ASINH_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/log.hpp>
|
||||
|
@ -19,7 +20,9 @@ namespace sprout {
|
|||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
asinh(FloatType x) {
|
||||
return sprout::math::detail::log(x + sprout::math::detail::sqrt(x * x + 1))
|
||||
return x == std::numeric_limits<FloatType>::infinity() ? std::numeric_limits<FloatType>::infinity()
|
||||
: x == -std::numeric_limits<FloatType>::infinity() ? -std::numeric_limits<FloatType>::infinity()
|
||||
: sprout::math::detail::log(x + sprout::math::detail::sqrt(x * x + 1))
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define SPROUT_MATH_COS_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/factorial.hpp>
|
||||
|
@ -33,12 +34,16 @@ namespace sprout {
|
|||
inline SPROUT_CONSTEXPR FloatType
|
||||
cos(FloatType x) {
|
||||
typedef double type;
|
||||
return static_cast<FloatType>(sprout::math::detail::cos_impl(
|
||||
static_cast<type>(x),
|
||||
type(1),
|
||||
1,
|
||||
static_cast<type>(x) * static_cast<type>(x)
|
||||
));
|
||||
return x == std::numeric_limits<FloatType>::infinity()
|
||||
|| x == -std::numeric_limits<FloatType>::infinity()
|
||||
? std::numeric_limits<FloatType>::quiet_NaN()
|
||||
: static_cast<FloatType>(sprout::math::detail::cos_impl(
|
||||
static_cast<type>(x),
|
||||
type(1),
|
||||
1,
|
||||
static_cast<type>(x) * static_cast<type>(x)
|
||||
))
|
||||
;
|
||||
}
|
||||
|
||||
template<
|
||||
|
|
|
@ -11,15 +11,6 @@
|
|||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
abs(FloatType x) {
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
|
@ -39,20 +30,6 @@ namespace sprout {
|
|||
}
|
||||
} // namespace detail
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
abs(FloatType x) {
|
||||
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
using std::abs;
|
||||
# else
|
||||
using sprout::math::detail::abs;
|
||||
# endif
|
||||
return abs(x);
|
||||
}
|
||||
|
||||
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
using std::fabs;
|
||||
# else
|
||||
|
@ -60,7 +37,6 @@ namespace sprout {
|
|||
# endif
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::abs;
|
||||
using sprout::math::fabs;
|
||||
} // namespace sprout
|
||||
|
||||
|
|
55
sprout/math/fma.hpp
Normal file
55
sprout/math/fma.hpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
#ifndef SPROUT_MATH_FMA_HPP
|
||||
#define SPROUT_MATH_FMA_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/float_promote.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <cmath>
|
||||
#endif
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
fma(FloatType x, FloatType y, FloatType z) {
|
||||
return x * y + z;
|
||||
}
|
||||
|
||||
template<
|
||||
typename ArithmeticType1,
|
||||
typename ArithmeticType2,
|
||||
typename ArithmeticType3,
|
||||
typename sprout::enabler_if<
|
||||
std::is_arithmetic<ArithmeticType1>::value
|
||||
&& std::is_arithmetic<ArithmeticType2>::value
|
||||
&& std::is_arithmetic<ArithmeticType3>::value
|
||||
>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR typename sprout::math::float_promote<
|
||||
ArithmeticType1, ArithmeticType2, ArithmeticType3
|
||||
>::type
|
||||
fma(ArithmeticType1 x, ArithmeticType2 y, ArithmeticType3 z) {
|
||||
typedef typename sprout::math::float_promote<
|
||||
ArithmeticType1, ArithmeticType2, ArithmeticType3
|
||||
>::type type;
|
||||
return sprout::math::detail::fma(static_cast<type>(x), static_cast<type>(y), static_cast<type>(z));
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
# if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
using std::fma;
|
||||
# else
|
||||
using sprout::math::detail::fma;
|
||||
# endif
|
||||
} // namespace math
|
||||
|
||||
using sprout::math::fma;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_FMA_HPP
|
|
@ -1,7 +1,9 @@
|
|||
#ifndef SPROUT_MATH_OPERATIONS_HPP
|
||||
#define SPROUT_MATH_OPERATIONS_HPP
|
||||
|
||||
#include <sprout/math/abs.hpp>
|
||||
#include <sprout/math/fabs.hpp>
|
||||
#include <sprout/math/fma.hpp>
|
||||
#include <sprout/math/fmax.hpp>
|
||||
#include <sprout/math/fmin.hpp>
|
||||
#include <sprout/math/fdim.hpp>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue