1//===-- Single-precision tanhf16 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_TANHF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_TANHF16_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/fenv_macros.h"
18#include "src/__support/CPP/array.h"
19#include "src/__support/FPUtil/FEnvImpl.h"
20#include "src/__support/FPUtil/FPBits.h"
21#include "src/__support/FPUtil/PolyEval.h"
22#include "src/__support/FPUtil/cast.h"
23#include "src/__support/FPUtil/except_value_utils.h"
24#include "src/__support/FPUtil/multiply_add.h"
25#include "src/__support/FPUtil/nearest_integer.h"
26#include "src/__support/FPUtil/rounding_mode.h"
27#include "src/__support/common.h"
28#include "src/__support/macros/config.h"
29#include "src/__support/macros/optimization.h"
30
31namespace LIBC_NAMESPACE_DECL {
32
33namespace math {
34
35LIBC_INLINE float16 tanhf16(float16 x) {
36 using namespace math::expxf16_internal;
37 using FPBits = fputil::FPBits<float16>;
38 FPBits x_bits(x);
39
40#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
41 constexpr fputil::ExceptValues<float16, 2> TANHF16_EXCEPTS = {.values: {
42 // x = 0x1.f54p+0, tanhf16(x) = 0x1.ecp-1 (RZ)
43 {.input: 0x3fd5U, .rnd_towardzero_result: 0x3bb0U, .rnd_upward_offset: 1U, .rnd_downward_offset: 0U, .rnd_tonearest_offset: 0U},
44 // x = -0x1.f54p+0, tanhf16(x) = -0x1.ecp-1 (RZ)
45 {.input: 0xbfd5U, .rnd_towardzero_result: 0xbbb0U, .rnd_upward_offset: 0U, .rnd_downward_offset: 1U, .rnd_tonearest_offset: 0U},
46 }};
47#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
48
49 uint16_t x_u = x_bits.uintval();
50 uint16_t x_abs = x_u & 0x7fffU;
51
52 // When -2^(-14) <= x <= -2^(-9), or |x| <= 0x1.d2p-4,
53 // or |x| >= atanh(1 - 2^(-11)), or x is NaN.
54 if (LIBC_UNLIKELY(x_abs <= 0x2f48U || x_abs >= 0x4429U)) {
55 // tanh(NaN) = NaN
56 if (x_bits.is_nan()) {
57 if (x_bits.is_signaling_nan()) {
58 fputil::raise_except_if_required(FE_INVALID);
59 return FPBits::quiet_nan().get_val();
60 }
61
62 return x;
63 }
64
65 // When -2^(-14) <= x <= -2^(-9).
66 if (x_u >= 0x8400U && x_u <= 0x9800U) {
67#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
68 return x;
69#else
70 switch (fputil::quick_get_round()) {
71 case FE_TONEAREST:
72 case FE_DOWNWARD:
73 return x;
74 default:
75 return FPBits(static_cast<uint16_t>(x_u - 1U)).get_val();
76 }
77#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
78 }
79
80 // When |x| <= 0x1.d2p-4.
81 if (x_abs <= 0x2f48U) {
82 if (LIBC_UNLIKELY(x_abs == 0))
83 return x;
84
85 float xf = x;
86 float xf_sq = xf * xf;
87 // Degree-7 Taylor expansion generated by Sollya with the following
88 // commands:
89 // > taylor(tanh(x), 7, 0);
90 // > display = hexadecimal;
91 // > // For each coefficient:
92 // > round(/* put coefficient here */, SG, RN);
93 return fputil::cast<float16>(
94 x: xf * fputil::polyeval(x: xf_sq, a0: 0x1p+0f, a: -0x1.555556p-2f, a: 0x1.111112p-3f,
95 a: -0x1.ba1ba2p-5f));
96 }
97
98 // tanh(+/-inf) = +/-1
99 if (x_bits.is_inf())
100 return FPBits::one(sign: x_bits.sign()).get_val();
101
102 // When |x| >= atanh(1 - 2^(-11)).
103 fputil::raise_except_if_required(FE_INEXACT);
104
105#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
106 if (x_abs >= 0x4482U) {
107 return FPBits::one(x_bits.sign()).get_val();
108 }
109#else
110 int rounding_mode = fputil::quick_get_round();
111 if ((rounding_mode == FE_TONEAREST && x_abs >= 0x4482U) ||
112 (rounding_mode == FE_UPWARD && x_bits.is_pos()) ||
113 (rounding_mode == FE_DOWNWARD && x_bits.is_neg())) {
114 return FPBits::one(sign: x_bits.sign()).get_val();
115 }
116#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
117 if (x_bits.is_pos())
118 return fputil::cast<float16>(x: 0x1.ffcp-1);
119 return fputil::cast<float16>(x: -0x1.ffcp-1);
120 }
121
122#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
123 if (auto r = TANHF16_EXCEPTS.lookup(x_bits: x_u); LIBC_UNLIKELY(r.has_value()))
124 return r.value();
125#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
126
127 // For atanh(-1 + 2^(-11)) < x < atanh(1 - 2^(-11)), to compute tanh(x), we
128 // perform the following range reduction: find hi, mid, lo, such that:
129 // x = (hi + mid) * log(2) * 0.5 + lo, in which
130 // hi is an integer,
131 // mid * 2^5 is an integer,
132 // -2^(-5) <= lo < 2^(-5).
133 // In particular,
134 // hi + mid = round(x * log2(e) * 2 * 2^5) * 2^(-5).
135 // Then,
136 // tanh(x) = sinh(x)/cosh(x)
137 // = (e^x - e^(-x)) / (e^x + e^(-x))
138 // = (e^(2x) - 1) / (e^(2x) + 1)
139 // = (2^(hi + mid) * e^(2*lo) - 1) / (2^(hi + mid) * e^(2*lo) + 1)
140 // = (e^(2*lo) - 2^(-hi - mid)) / (e^(2*lo) + 2^(-hi - mid))
141 // We store 2^(-mid) in the lookup table EXP2_MID_5_BITS, and compute
142 // 2^(-hi - mid) by adding -hi to the exponent field of 2^(-mid).
143 // e^lo is computed using a degree-3 minimax polynomial generated by Sollya.
144
145 float xf = x;
146 float kf = fputil::nearest_integer(x: xf * (LOG2F_E * 2.0f * 0x1.0p+5f));
147 int x_hi_mid = -static_cast<int>(kf);
148 unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 5;
149 unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x1f;
150 // lo = x - (hi + mid)
151 // = round(x * log2(e) * 2 * 2^5) * log(2) * 0.5 * (-2^(-5)) + x
152 float lo = fputil::multiply_add(x: kf, y: LOGF_2 * 0.5f * -0x1.0p-5f, z: xf);
153
154 uint32_t exp2_hi_mid_bits =
155 EXP2_MID_5_BITS[x_mid] +
156 static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
157 // exp2_hi_mid = 2^(-hi - mid)
158 float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
159 // Degree-3 minimax polynomial generated by Sollya with the following
160 // commands:
161 // > display = hexadecimal;
162 // > P = fpminimax(expm1(2*x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
163 // > 1 + x * P;
164 float exp_2lo =
165 fputil::polyeval(x: lo, a0: 0x1p+0f, a: 0x1p+1f, a: 0x1.001p+1f, a: 0x1.555ddep+0f);
166 return fputil::cast<float16>(x: (exp_2lo - exp2_hi_mid) /
167 (exp_2lo + exp2_hi_mid));
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_TANHF16_H
177