| 1 | //===----------------------------------------------------------------------===// |
| 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 | /// \file |
| 10 | /// Double-precision implementation of cosh. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSH_DOUBLE_EVAL_H |
| 15 | #define LLVM_LIBC_SRC___SUPPORT_MATH_COSH_DOUBLE_EVAL_H |
| 16 | |
| 17 | #include "src/__support/FPUtil/FPBits.h" |
| 18 | #include "src/__support/FPUtil/except_value_utils.h" |
| 19 | #include "src/__support/macros/attributes.h" |
| 20 | #include "src/__support/macros/config.h" |
| 21 | #include "src/__support/macros/optimization.h" |
| 22 | #include "src/__support/math/sinhcosh_utils.h" |
| 23 | |
| 24 | namespace LIBC_NAMESPACE_DECL { |
| 25 | namespace math { |
| 26 | namespace double_eval { |
| 27 | |
| 28 | // Bound constants (with biased exponent and mantissa bits): |
| 29 | // - Tiny bound: 2^-27 = 0x1.0p-27 |
| 30 | LIBC_INLINE_VAR constexpr uint64_t COSH_TINY_BOUND = 0x3e40'0000'0000'0000ULL; |
| 31 | // - Overflow bound: |
| 32 | // > round(acosh(2^1023 * (2 - 2^-52)), D, RU) = 0x1.633ce8fb9f87ep+9 |
| 33 | // > round(acosh(2^1023 * (2 - 2^-52)), D, RD) = 0x1.633ce8fb9f87dp+9 |
| 34 | // > round(cosh(0x1.633ce8fb9f87dp+9), D, RN) = 0x1.ffffffffffd3bp+1023 |
| 35 | // > round(cosh(0x1.633ce8fb9f87ep+9), D, RN) = +inf |
| 36 | LIBC_INLINE_VAR constexpr uint64_t COSH_OVERFLOW_BOUND = |
| 37 | 0x4086'33ce'8fb9'f87eULL; |
| 38 | |
| 39 | // Handle tiny (|x| <= 2^-27), overflow (|x| >= 0x1.633ce8fb9f87ep+9), inf, and |
| 40 | // nan for cosh(x): |
| 41 | LIBC_NOINLINE LIBC_INLINE double cosh_exceptional(double x) { |
| 42 | using FPBits = fputil::FPBits<double>; |
| 43 | FPBits xbits(x); |
| 44 | |
| 45 | // Tiny and subnormal range: |x| <= 2^-27. |
| 46 | // For 0 < |x| <= 2^-27: |
| 47 | // 0 < cosh(x) - 1 ~ x^2 / 2 < 2^-55 < 0.5 * ulp(1.0), |
| 48 | // so cosh(x) rounds to 1.0 + 2^-52 when rounding mode is FE_UPWARD and to |
| 49 | // 1.0 otherwise, with FE_INEXACT raised for all x != 0. |
| 50 | if (xbits.abs().uintval() <= COSH_TINY_BOUND) { |
| 51 | if (LIBC_UNLIKELY(x == 0.0)) |
| 52 | return 1.0; |
| 53 | return fputil::round_result_slightly_up(value_rn: 1.0); |
| 54 | } |
| 55 | |
| 56 | if (xbits.is_nan()) |
| 57 | return x + 1.0; |
| 58 | |
| 59 | if (xbits.is_inf()) |
| 60 | return FPBits::inf().get_val(); |
| 61 | |
| 62 | return fputil::set_overflow<double>(); |
| 63 | } |
| 64 | |
| 65 | LIBC_ALWAYS_INLINE double cosh(double x) { |
| 66 | using FPBits = fputil::FPBits<double>; |
| 67 | FPBits xbits(x); |
| 68 | |
| 69 | uint64_t x_abs = xbits.abs().uintval(); |
| 70 | |
| 71 | if (LIBC_UNLIKELY(x_abs <= COSH_TINY_BOUND || x_abs >= COSH_OVERFLOW_BOUND)) |
| 72 | return cosh_exceptional(x); |
| 73 | |
| 74 | return exp_pm_eval(x: xbits.abs().get_val(), /*is_sinh=*/is_sinh: false); |
| 75 | } |
| 76 | |
| 77 | } // namespace double_eval |
| 78 | } // namespace math |
| 79 | } // namespace LIBC_NAMESPACE_DECL |
| 80 | |
| 81 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSH_DOUBLE_EVAL_H |
| 82 | |