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