// Copyright 2023 Matt Borland // Copyright 2023 Christopher Kormanyos // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #ifndef BOOST_DECIMAL_DETAIL_CMATH_COSH_HPP #define BOOST_DECIMAL_DETAIL_CMATH_COSH_HPP #include // NOLINT(llvm-include-order) #include #include #include #include #include #ifndef BOOST_DECIMAL_BUILD_MODULE #include #include #endif namespace boost { namespace decimal { namespace detail { template constexpr auto cosh_impl(const T x) noexcept BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T) { const auto fpc = fpclassify(x); constexpr T zero { 0, 0 }; constexpr T one { 1, 0 }; auto result = zero; if (fpc == FP_ZERO) { result = one; } #ifndef BOOST_DECIMAL_FAST_MATH else if (fpc != FP_NORMAL) { if (fpc == FP_INFINITE) { result = abs(x); } else if (fpc == FP_NAN) { result = abs(x); } } #endif else { if (signbit(x)) { result = cosh(-x); } else { if (x < one) { const auto xsq = x * x; result = detail::cosh_series_expansion(xsq); result = fma(result, xsq, one); } else { const auto exp_pos_val = exp(x); constexpr T two { 2, 0 }; result = (exp_pos_val + (one / exp_pos_val)) / two; } } } return result; } } // namespace detail BOOST_DECIMAL_EXPORT template constexpr auto cosh(const T x) noexcept BOOST_DECIMAL_REQUIRES(detail::is_decimal_floating_point_v, T) { using evaluation_type = detail::evaluation_type_t; return static_cast(detail::cosh_impl(static_cast(x))); } } // namespace decimal } // namespace boost #endif // BOOST_DECIMAL_DETAIL_CMATH_COSH_HPP