| 1 | //===-- Implementation header for coshf -------------------------*- C++ -*-===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H |
| 11 | |
| 12 | #include "sinhfcoshf_utils.h" |
| 13 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | #include "src/__support/FPUtil/FPBits.h" |
| 15 | #include "src/__support/FPUtil/rounding_mode.h" |
| 16 | #include "src/__support/macros/config.h" |
| 17 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
| 18 | |
| 19 | namespace LIBC_NAMESPACE_DECL { |
| 20 | |
| 21 | namespace math { |
| 22 | |
| 23 | LIBC_INLINE float coshf(float x) { |
| 24 | using namespace sinhfcoshf_internal; |
| 25 | using FPBits = typename fputil::FPBits<float>; |
| 26 | |
| 27 | FPBits xbits(x); |
| 28 | xbits.set_sign(Sign::POS); |
| 29 | x = xbits.get_val(); |
| 30 | |
| 31 | uint32_t x_u = xbits.uintval(); |
| 32 | |
| 33 | // When |x| >= 90, or x is inf or nan |
| 34 | if (LIBC_UNLIKELY(x_u >= 0x42b4'0000U || x_u <= 0x3280'0000U)) { |
| 35 | // |x| <= 2^-26 |
| 36 | if (x_u <= 0x3280'0000U) { |
| 37 | return 1.0f + x; |
| 38 | } |
| 39 | |
| 40 | if (xbits.is_inf_or_nan()) |
| 41 | return x + FPBits::inf().get_val(); |
| 42 | |
| 43 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 44 | int rounding = fputil::quick_get_round(); |
| 45 | if (LIBC_UNLIKELY(rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)) |
| 46 | return FPBits::max_normal().get_val(); |
| 47 | #endif |
| 48 | |
| 49 | fputil::set_errno_if_required(ERANGE); |
| 50 | fputil::raise_except_if_required(FE_OVERFLOW); |
| 51 | |
| 52 | return x + FPBits::inf().get_val(); |
| 53 | } |
| 54 | |
| 55 | // TODO: We should be able to reduce the latency and reciprocal throughput |
| 56 | // further by using a low degree (maybe 3-7 ?) minimax polynomial for small |
| 57 | // but not too small inputs, such as |x| < 2^-2, or |x| < 2^-3. |
| 58 | |
| 59 | // cosh(x) = (e^x + e^(-x)) / 2. |
| 60 | return static_cast<float>(exp_pm_eval</*is_sinh*/ false>(x)); |
| 61 | } |
| 62 | |
| 63 | } // namespace math |
| 64 | |
| 65 | } // namespace LIBC_NAMESPACE_DECL |
| 66 | |
| 67 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF_H |
| 68 |