| 1 | //===-- Implementation header for coshf16 -----------------------*- 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_COSHF16_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H |
| 11 | |
| 12 | #include "include/llvm-libc-macros/float16-macros.h" |
| 13 | |
| 14 | #ifdef LIBC_TYPES_HAS_FLOAT16 |
| 15 | |
| 16 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 17 | #include "src/__support/FPUtil/FPBits.h" |
| 18 | #include "src/__support/FPUtil/except_value_utils.h" |
| 19 | #include "src/__support/macros/config.h" |
| 20 | #include "src/__support/macros/optimization.h" |
| 21 | #include "src/__support/math/expxf16_utils.h" |
| 22 | |
| 23 | namespace LIBC_NAMESPACE_DECL { |
| 24 | |
| 25 | namespace math { |
| 26 | |
| 27 | LIBC_INLINE constexpr float16 coshf16(float16 x) { |
| 28 | |
| 29 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 30 | constexpr fputil::ExceptValues<float16, 9> COSHF16_EXCEPTS_POS = {.values: { |
| 31 | // x = 0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ) |
| 32 | {.input: 0x29a8U, .rnd_towardzero_result: 0x3c00U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 33 | // x = 0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ) |
| 34 | {.input: 0x3e31U, .rnd_towardzero_result: 0x40eaU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 35 | // x = 0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ) |
| 36 | {.input: 0x3e65U, .rnd_towardzero_result: 0x4126U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 37 | // x = 0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ) |
| 38 | {.input: 0x3ed8U, .rnd_towardzero_result: 0x41b6U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 39 | // x = 0x1.aap+1, coshf16(x) = 0x1.be8p+3 (RZ) |
| 40 | {.input: 0x42a8U, .rnd_towardzero_result: 0x4afaU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 41 | // x = 0x1.cc4p+1, coshf16(x) = 0x1.23cp+4 (RZ) |
| 42 | {.input: 0x4331U, .rnd_towardzero_result: 0x4c8fU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 43 | // x = 0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ) |
| 44 | {.input: 0x44a2U, .rnd_towardzero_result: 0x526dU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 45 | // x = 0x1.958p+2, coshf16(x) = 0x1.1a4p+8 (RZ) |
| 46 | {.input: 0x4656U, .rnd_towardzero_result: 0x5c69U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 47 | // x = 0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ) |
| 48 | {.input: 0x497cU, .rnd_towardzero_result: 0x7715U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 49 | }}; |
| 50 | |
| 51 | constexpr fputil::ExceptValues<float16, 6> COSHF16_EXCEPTS_NEG = {.values: { |
| 52 | // x = -0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ) |
| 53 | {.input: 0xa9a8U, .rnd_towardzero_result: 0x3c00U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 54 | // x = -0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ) |
| 55 | {.input: 0xbed8U, .rnd_towardzero_result: 0x41b6U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 56 | // x = -0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ) |
| 57 | {.input: 0xc4a2U, .rnd_towardzero_result: 0x526dU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 58 | // x = -0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ) |
| 59 | {.input: 0xc97cU, .rnd_towardzero_result: 0x7715U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 60 | // x = -0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ) |
| 61 | {.input: 0xbe31U, .rnd_towardzero_result: 0x40eaU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 62 | // x = -0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ) |
| 63 | {.input: 0xbe65U, .rnd_towardzero_result: 0x4126U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 64 | }}; |
| 65 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 66 | |
| 67 | using namespace expxf16_internal; |
| 68 | using FPBits = fputil::FPBits<float16>; |
| 69 | FPBits x_bits(x); |
| 70 | |
| 71 | uint16_t x_u = x_bits.uintval(); |
| 72 | uint16_t x_abs = x_u & 0x7fffU; |
| 73 | |
| 74 | // When |x| >= acosh(2^16), or x is NaN. |
| 75 | if (LIBC_UNLIKELY(x_abs >= 0x49e5U)) { |
| 76 | // cosh(NaN) = NaN |
| 77 | if (x_bits.is_nan()) { |
| 78 | if (x_bits.is_signaling_nan()) { |
| 79 | fputil::raise_except_if_required(FE_INVALID); |
| 80 | return FPBits::quiet_nan().get_val(); |
| 81 | } |
| 82 | |
| 83 | return x; |
| 84 | } |
| 85 | |
| 86 | // When |x| >= acosh(2^16). |
| 87 | if (x_abs >= 0x49e5U) { |
| 88 | // cosh(+/-inf) = +inf |
| 89 | if (x_bits.is_inf()) |
| 90 | return FPBits::inf().get_val(); |
| 91 | |
| 92 | return fputil::set_overflow<float16>(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 97 | if (x_bits.is_pos()) { |
| 98 | if (auto r = COSHF16_EXCEPTS_POS.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value())) |
| 99 | return r.value(); |
| 100 | } else { |
| 101 | if (auto r = COSHF16_EXCEPTS_NEG.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value())) |
| 102 | return r.value(); |
| 103 | } |
| 104 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 105 | |
| 106 | return eval_sinh_or_cosh</*IsSinh=*/false>(x); |
| 107 | } |
| 108 | |
| 109 | } // namespace math |
| 110 | |
| 111 | } // namespace LIBC_NAMESPACE_DECL |
| 112 | |
| 113 | #endif // LIBC_TYPES_HAS_FLOAT16 |
| 114 | |
| 115 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H |
| 116 | |