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

source code of libc/src/math/generic/tanhf16.cpp