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