/*============================================================================= Copyright (c) 2011-2019 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_SQRT_HPP #define SPROUT_MATH_SQRT_HPP #include #include #include #include #include #include #include namespace sprout { namespace math { namespace detail { #if SPROUT_USE_BUILTIN_CMATH_FUNCTION inline SPROUT_CONSTEXPR float builtin_sqrt(float x) { return __builtin_sqrtf(x); } inline SPROUT_CONSTEXPR double builtin_sqrt(double x) { return __builtin_sqrt(x); } inline SPROUT_CONSTEXPR long double builtin_sqrt(long double x) { return __builtin_sqrtl(x); } #endif template inline SPROUT_CONSTEXPR T sqrt_impl_2(T x, T s, T s2) { return !(s < s2) ? s2 : sprout::math::detail::sqrt_impl_2(x, (x / s + s) / 2, s) ; } template inline SPROUT_CONSTEXPR T sqrt_impl_1(T x, T s) { return sprout::math::detail::sqrt_impl_2(x, (x / s + s) / 2, s); } template inline SPROUT_CONSTEXPR T sqrt_impl(T x) { return sprout::math::detail::sqrt_impl_1(x, x > 1 ? x : T(1)); } } // namespace detail // // sqrt // template< typename FloatType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR FloatType sqrt(FloatType x) { return sprout::math::isnan(x) ? x : x == sprout::numeric_limits::infinity() ? sprout::numeric_limits::infinity() : x < 0 ? -sprout::numeric_limits::quiet_NaN() #if SPROUT_USE_BUILTIN_CMATH_FUNCTION : sprout::math::detail::builtin_sqrt(x) #else : x == 0 ? x : static_cast(sprout::math::detail::sqrt_impl(static_cast::type>(x))) #endif ; } template< typename IntType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR double sqrt(IntType x) { return sprout::math::sqrt(static_cast(x)); } } // namespace math using sprout::math::sqrt; } // namespace sprout #endif // #ifndef SPROUT_MATH_SQRT_HPP