| 1 | //===-- Half-precision sinh(x) function -----------------------------------===// |
| 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_SINHF16_H |
| 10 | #define LLVM_LIBC_SRC___SUPPORT_MATH_SINHF16_H |
| 11 | |
| 12 | #include "include/llvm-libc-macros/float16-macros.h" |
| 13 | |
| 14 | #ifdef LIBC_TYPES_HAS_FLOAT16 |
| 15 | |
| 16 | #include "hdr/fenv_macros.h" |
| 17 | #include "src/__support/FPUtil/FEnvImpl.h" |
| 18 | #include "src/__support/FPUtil/FPBits.h" |
| 19 | #include "src/__support/FPUtil/except_value_utils.h" |
| 20 | #include "src/__support/FPUtil/rounding_mode.h" |
| 21 | #include "src/__support/common.h" |
| 22 | #include "src/__support/macros/config.h" |
| 23 | #include "src/__support/macros/optimization.h" |
| 24 | #include "src/__support/math/expxf16_utils.h" |
| 25 | |
| 26 | namespace LIBC_NAMESPACE_DECL { |
| 27 | |
| 28 | namespace math { |
| 29 | |
| 30 | LIBC_INLINE constexpr float16 sinhf16(float16 x) { |
| 31 | |
| 32 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 33 | constexpr fputil::ExceptValues<float16, 17> SINHF16_EXCEPTS_POS = {.values: { |
| 34 | // x = 0x1.714p-5, sinhf16(x) = 0x1.714p-5 (RZ) |
| 35 | {.input: 0x29c5U, .rnd_towardzero_result: 0x29c5U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 36 | // x = 0x1.25p-4, sinhf16(x) = 0x1.25p-4 (RZ) |
| 37 | {.input: 0x2c94U, .rnd_towardzero_result: 0x2c94U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 38 | // x = 0x1.f5p-4, sinhf16(x) = 0x1.f64p-4 (RZ) |
| 39 | {.input: 0x2fd4U, .rnd_towardzero_result: 0x2fd9U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 40 | // x = 0x1.b1cp-3, sinhf16(x) = 0x1.b4cp-3 (RZ) |
| 41 | {.input: 0x32c7U, .rnd_towardzero_result: 0x32d3U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 42 | // x = 0x1.6e8p-2, sinhf16(x) = 0x1.764p-2 (RZ) |
| 43 | {.input: 0x35baU, .rnd_towardzero_result: 0x35d9U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 44 | // x = 0x1.6b4p-1, sinhf16(x) = 0x1.8a4p-1 (RZ) |
| 45 | {.input: 0x39adU, .rnd_towardzero_result: 0x3a29U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 46 | // x = 0x1.a58p-1, sinhf16(x) = 0x1.d68p-1 (RZ) |
| 47 | {.input: 0x3a96U, .rnd_towardzero_result: 0x3b5aU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 48 | // x = 0x1.574p+0, sinhf16(x) = 0x1.c78p+0 (RZ) |
| 49 | {.input: 0x3d5dU, .rnd_towardzero_result: 0x3f1eU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 50 | // x = 0x1.648p+1, sinhf16(x) = 0x1.024p+3 (RZ) |
| 51 | {.input: 0x4192U, .rnd_towardzero_result: 0x4809U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 52 | // x = 0x1.cdcp+1, sinhf16(x) = 0x1.26cp+4 (RZ) |
| 53 | {.input: 0x4337U, .rnd_towardzero_result: 0x4c9bU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 54 | // x = 0x1.d0cp+1, sinhf16(x) = 0x1.2d8p+4 (RZ) |
| 55 | {.input: 0x4343U, .rnd_towardzero_result: 0x4cb6U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 56 | // x = 0x1.018p+2, sinhf16(x) = 0x1.bfp+4 (RZ) |
| 57 | {.input: 0x4406U, .rnd_towardzero_result: 0x4efcU, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 58 | // x = 0x1.2fcp+2, sinhf16(x) = 0x1.cc4p+5 (RZ) |
| 59 | {.input: 0x44bfU, .rnd_towardzero_result: 0x5331U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 60 | // x = 0x1.4ecp+2, sinhf16(x) = 0x1.75cp+6 (RZ) |
| 61 | {.input: 0x453bU, .rnd_towardzero_result: 0x55d7U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U}, |
| 62 | // x = 0x1.8a4p+2, sinhf16(x) = 0x1.d94p+7 (RZ) |
| 63 | {.input: 0x4629U, .rnd_towardzero_result: 0x5b65U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 64 | // x = 0x1.5fp+3, sinhf16(x) = 0x1.c54p+14 (RZ) |
| 65 | {.input: 0x497cU, .rnd_towardzero_result: 0x7715U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 66 | // x = 0x1.3c8p+1, sinhf16(x) = 0x1.78ap+2 (RZ) |
| 67 | {.input: 0x40f2U, .rnd_towardzero_result: 0x45e2U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 1U}, |
| 68 | }}; |
| 69 | |
| 70 | constexpr fputil::ExceptValues<float16, 13> SINHF16_EXCEPTS_NEG = {.values: { |
| 71 | // x = -0x1.714p-5, sinhf16(x) = -0x1.714p-5 (RZ) |
| 72 | {.input: 0xa9c5U, .rnd_towardzero_result: 0xa9c5U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 73 | // x = -0x1.25p-4, sinhf16(x) = -0x1.25p-4 (RZ) |
| 74 | {.input: 0xac94U, .rnd_towardzero_result: 0xac94U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 75 | // x = -0x1.f5p-4, sinhf16(x) = -0x1.f64p-4 (RZ) |
| 76 | {.input: 0xafd4U, .rnd_towardzero_result: 0xafd9U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U}, |
| 77 | // x = -0x1.6e8p-2, sinhf16(x) = -0x1.764p-2 (RZ) |
| 78 | {.input: 0xb5baU, .rnd_towardzero_result: 0xb5d9U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 79 | // x = -0x1.a58p-1, sinhf16(x) = -0x1.d68p-1 (RZ) |
| 80 | {.input: 0xba96U, .rnd_towardzero_result: 0xbb5aU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 81 | // x = -0x1.cdcp+1, sinhf16(x) = -0x1.26cp+4 (RZ) |
| 82 | {.input: 0xc337U, .rnd_towardzero_result: 0xcc9bU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U}, |
| 83 | // x = -0x1.d0cp+1, sinhf16(x) = -0x1.2d8p+4 (RZ) |
| 84 | {.input: 0xc343U, .rnd_towardzero_result: 0xccb6U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 85 | // x = -0x1.018p+2, sinhf16(x) = -0x1.bfp+4 (RZ) |
| 86 | {.input: 0xc406U, .rnd_towardzero_result: 0xcefcU, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U}, |
| 87 | // x = -0x1.2fcp+2, sinhf16(x) = -0x1.cc4p+5 (RZ) |
| 88 | {.input: 0xc4bfU, .rnd_towardzero_result: 0xd331U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 89 | // x = -0x1.4ecp+2, sinhf16(x) = -0x1.75cp+6 (RZ) |
| 90 | {.input: 0xc53bU, .rnd_towardzero_result: 0xd5d7U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U}, |
| 91 | // x = -0x1.8a4p+2, sinhf16(x) = -0x1.d94p+7 (RZ) |
| 92 | {.input: 0xc629U, .rnd_towardzero_result: 0xdb65U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 93 | // x = -0x1.5fp+3, sinhf16(x) = -0x1.c54p+14 (RZ) |
| 94 | {.input: 0xc97cU, .rnd_towardzero_result: 0xf715U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 95 | // x = -0x1.3c8p+1, sinhf16(x) = -0x1.78ap+2 (RZ) |
| 96 | {.input: 0xc0f2U, .rnd_towardzero_result: 0xc5e2U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 1U}, |
| 97 | }}; |
| 98 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 99 | |
| 100 | using namespace math::expxf16_internal; |
| 101 | using FPBits = fputil::FPBits<float16>; |
| 102 | FPBits x_bits(x); |
| 103 | |
| 104 | uint16_t x_u = x_bits.uintval(); |
| 105 | uint16_t x_abs = x_u & 0x7fffU; |
| 106 | |
| 107 | // When |x| < 2^(-14), or -2^(-14) <= x <= -2^(-9), or |x| >= asinh(2^16), or |
| 108 | // x is NaN. |
| 109 | if (LIBC_UNLIKELY(x_abs < 0x0400U || (x_u >= 0x8400U && x_u <= 0xa400U) || |
| 110 | x_abs >= 0x49e5U)) { |
| 111 | // sinh(NaN) = NaN |
| 112 | if (x_bits.is_nan()) { |
| 113 | if (x_bits.is_signaling_nan()) { |
| 114 | fputil::raise_except_if_required(FE_INVALID); |
| 115 | return FPBits::quiet_nan().get_val(); |
| 116 | } |
| 117 | |
| 118 | return x; |
| 119 | } |
| 120 | |
| 121 | // sinh(+/-0) = sinh(+/-0) |
| 122 | if (x_abs == 0U) |
| 123 | return FPBits::zero(sign: x_bits.sign()).get_val(); |
| 124 | |
| 125 | // When 0 < |x| < 2^(-14), the correction to x is nonzero but less than |
| 126 | // half a subnormal ulp, so select the adjacent half value directly. Even |
| 127 | // when that is the minimum normal, the result rounded to 11 bits with an |
| 128 | // unbounded exponent is below 2^(-14), so underflow is always signaled. |
| 129 | if (x_abs < 0x0400U) { |
| 130 | uint16_t out = x_u; |
| 131 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 132 | int mode = fputil::quick_get_round(); |
| 133 | if ((x_bits.is_pos() && mode == FE_UPWARD) || |
| 134 | (x_bits.is_neg() && mode == FE_DOWNWARD)) |
| 135 | out = static_cast<uint16_t>(x_u + 1); |
| 136 | #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 137 | fputil::set_errno_if_required(ERANGE); |
| 138 | fputil::raise_underflow_except_if_required<float16>(); |
| 139 | return FPBits(out).get_val(); |
| 140 | } |
| 141 | |
| 142 | // When |x| >= asinh(2^16). |
| 143 | if (x_abs >= 0x49e5U) { |
| 144 | // sinh(+/-inf) = +/-inf |
| 145 | if (x_bits.is_inf()) |
| 146 | return FPBits::inf(sign: x_bits.sign()).get_val(); |
| 147 | |
| 148 | return fputil::set_overflow<float16>(x_bits.sign()); |
| 149 | } |
| 150 | |
| 151 | // When -2^(-14) <= x <= -2^(-9). |
| 152 | #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 153 | if (fputil::fenv_is_round_down()) |
| 154 | return FPBits(static_cast<uint16_t>(x_u + 1)).get_val(); |
| 155 | #endif |
| 156 | return FPBits(static_cast<uint16_t>(x_u)).get_val(); |
| 157 | } |
| 158 | |
| 159 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 160 | if (x_bits.is_pos()) { |
| 161 | if (auto r = SINHF16_EXCEPTS_POS.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value())) |
| 162 | return r.value(); |
| 163 | } else { |
| 164 | if (auto r = SINHF16_EXCEPTS_NEG.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value())) |
| 165 | return r.value(); |
| 166 | } |
| 167 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| 168 | |
| 169 | return eval_sinh_or_cosh</*IsSinh=*/true>(x); |
| 170 | } |
| 171 | |
| 172 | } // namespace math |
| 173 | |
| 174 | } // namespace LIBC_NAMESPACE_DECL |
| 175 | |
| 176 | #endif // LIBC_TYPES_HAS_FLOAT16 |
| 177 | |
| 178 | #endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINHF16_H |
| 179 | |