1//===-- Single-precision tanh 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/tanhf.h"
10#include "src/__support/FPUtil/FPBits.h"
11#include "src/__support/FPUtil/PolyEval.h"
12#include "src/__support/FPUtil/multiply_add.h"
13#include "src/__support/FPUtil/nearest_integer.h"
14#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
15#include "src/__support/macros/properties/cpu_features.h"
16#include "src/math/generic/explogxf.h"
17
18namespace LIBC_NAMESPACE {
19
20// 2^6 * log2(e)
21constexpr double LOG2_E_EXP2_6 = ExpBase::LOG2_B * 2.0;
22
23LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
24 using FPBits = typename fputil::FPBits<float>;
25 FPBits xbits(x);
26 uint32_t x_abs = xbits.abs().uintval();
27
28 const int sign_index = xbits.is_neg() ? 1 : 0;
29
30 // When |x| >= 15, or x is inf or nan, or |x| <= 0.078125
31 if (LIBC_UNLIKELY((x_abs >= 0x4170'0000U) || (x_abs <= 0x3da0'0000U))) {
32 if (x_abs <= 0x3da0'0000U) {
33 // |x| <= 0.078125
34 if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
35 // |x| <= 2^-26
36 return (x_abs != 0)
37 ? static_cast<float>(x - 0x1.5555555555555p-2 * x * x * x)
38 : x;
39 }
40
41 const double TAYLOR[] = {-0x1.5555555555555p-2, 0x1.1111111111111p-3,
42 -0x1.ba1ba1ba1ba1cp-5, 0x1.664f4882c10fap-6,
43 -0x1.226e355e6c23dp-7};
44 double xdbl = x;
45 double x2 = xdbl * xdbl;
46 // Taylor polynomial.
47 double x4 = x2 * x2;
48 double c0 = x2 * TAYLOR[0];
49 double c1 = fputil::multiply_add(x: x2, y: TAYLOR[2], z: TAYLOR[1]);
50 double c2 = fputil::multiply_add(x: x2, y: TAYLOR[4], z: TAYLOR[3]);
51 double pe = fputil::polyeval(x: x4, a0: c0, a: c1, a: c2);
52
53 return static_cast<float>(fputil::multiply_add(x: xdbl, y: pe, z: xdbl));
54 }
55
56 // |x| >= 15
57 if (LIBC_UNLIKELY(xbits.is_nan()))
58 return x + 1.0f; // sNaN to qNaN + signal
59
60 constexpr float SIGNS[2][2] = {{1.0f, -0x1.0p-25f}, {-1.0f, 0x1.0p-25f}};
61
62 if (LIBC_UNLIKELY(xbits.is_inf()))
63 return SIGNS[sign_index][0];
64
65 return SIGNS[sign_index][0] + SIGNS[sign_index][1];
66 }
67
68 // Range reduction: e^(2x) = 2^(hi + mid) * e^lo
69 // Let k = round( x * 2^6 * log2(e)),
70 // So k = (hi + mid) * 2^5
71 // Then lo = 2x - (hi + mid) * log(2) = 2x - k * 2^-5 * log(2).
72
73 double xd = static_cast<double>(x);
74 // k = round( x* 2^6 * log2(e) )
75 double k;
76 // mk = -k
77 int mk;
78#ifdef LIBC_TARGET_CPU_HAS_NEAREST_INT
79 k = fputil::nearest_integer(x: xd * LOG2_E_EXP2_6);
80 mk = -static_cast<int>(k);
81#else
82 constexpr double HALF_WAY[2] = {-0.5, 0.5};
83
84 mk = static_cast<int>(
85 fputil::multiply_add(xd, -LOG2_E_EXP2_6, HALF_WAY[sign_index]));
86 k = static_cast<double>(-mk);
87#endif // LIBC_TARGET_CPU_HAS_NEAREST_INT
88 // -hi = floor(-k * 2^(-MID_BITS))
89 // exp_mhi = shift -hi to the exponent field of double precision.
90 int64_t exp_mhi = static_cast<int64_t>(mk >> ExpBase::MID_BITS)
91 << fputil::FPBits<double>::FRACTION_LEN;
92 // mh = 2^(-hi - mid)
93 int64_t mh_bits = ExpBase::EXP_2_MID[mk & ExpBase::MID_MASK] + exp_mhi;
94 double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
95 // dx = lo/2 = x - (hi + mid) * log(2)/2 = x - k * 2^-6 * log(2)
96 double dx = fputil::multiply_add(
97 x: k, y: ExpBase::M_LOGB_2_LO * 0.5,
98 z: fputil::multiply_add(x: k, y: ExpBase::M_LOGB_2_HI * 0.5, z: xd));
99
100 // > P = fpminimax(expm1(2*x)/x, 4, [|D...|], [-log(2)/128, log(2)/128]);
101 constexpr double COEFFS[] = {0x1.ffffffffe5bc8p0, 0x1.555555555cd67p0,
102 0x1.5555c2a9b48b4p-1, 0x1.11112a0e34bdbp-2};
103
104 double dx2 = dx * dx;
105 double c0 = fputil::multiply_add(x: dx, y: 2.0, z: 1.0);
106 double c1 = fputil::multiply_add(x: dx, y: COEFFS[1], z: COEFFS[0]);
107 double c2 = fputil::multiply_add(x: dx, y: COEFFS[3], z: COEFFS[2]);
108 double r = fputil::polyeval(x: dx2, a0: c0, a: c1, a: c2);
109
110 // tanh(x) = sinh(x) / cosh(x)
111 // = (e^x - e^(-x)) / (e^x + e^(-x))
112 // = (e^(2x) - 1) / (e^(2x) + 1)
113 // = (2^(hi + mid) * e^lo - 1) / (2^(hi + mid) * e^lo + 1)
114 // = (e^lo - 2^(-hi - mid)) / (e^lo + 2^(-hi - mid))
115 // = (r - mh) / (r + mh)
116 return static_cast<float>((r - mh) / (r + mh));
117}
118
119} // namespace LIBC_NAMESPACE
120

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