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