1*ecf4f95cSwldfngrs //===-- Half-precision tan(x) function ------------------------------------===// 2*ecf4f95cSwldfngrs // 3*ecf4f95cSwldfngrs // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*ecf4f95cSwldfngrs // See https://llvm.org/LICENSE.txt for license information. 5*ecf4f95cSwldfngrs // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception. 6*ecf4f95cSwldfngrs // 7*ecf4f95cSwldfngrs //===----------------------------------------------------------------------===// 8*ecf4f95cSwldfngrs 9*ecf4f95cSwldfngrs #include "src/math/tanf16.h" 10*ecf4f95cSwldfngrs #include "hdr/errno_macros.h" 11*ecf4f95cSwldfngrs #include "hdr/fenv_macros.h" 12*ecf4f95cSwldfngrs #include "sincosf16_utils.h" 13*ecf4f95cSwldfngrs #include "src/__support/FPUtil/FEnvImpl.h" 14*ecf4f95cSwldfngrs #include "src/__support/FPUtil/FPBits.h" 15*ecf4f95cSwldfngrs #include "src/__support/FPUtil/cast.h" 16*ecf4f95cSwldfngrs #include "src/__support/FPUtil/except_value_utils.h" 17*ecf4f95cSwldfngrs #include "src/__support/FPUtil/multiply_add.h" 18*ecf4f95cSwldfngrs #include "src/__support/macros/optimization.h" 19*ecf4f95cSwldfngrs 20*ecf4f95cSwldfngrs namespace LIBC_NAMESPACE_DECL { 21*ecf4f95cSwldfngrs 22*ecf4f95cSwldfngrs constexpr size_t N_EXCEPTS = 9; 23*ecf4f95cSwldfngrs 24*ecf4f95cSwldfngrs constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANF16_EXCEPTS{{ 25*ecf4f95cSwldfngrs // (input, RZ output, RU offset, RD offset, RN offset) 26*ecf4f95cSwldfngrs {0x2894, 0x2894, 1, 0, 1}, 27*ecf4f95cSwldfngrs {0x3091, 0x3099, 1, 0, 0}, 28*ecf4f95cSwldfngrs {0x3098, 0x30a0, 1, 0, 0}, 29*ecf4f95cSwldfngrs {0x55ed, 0x3911, 1, 0, 0}, 30*ecf4f95cSwldfngrs {0x607b, 0xc638, 0, 1, 1}, 31*ecf4f95cSwldfngrs {0x674e, 0x3b7d, 1, 0, 0}, 32*ecf4f95cSwldfngrs {0x6807, 0x4014, 1, 0, 1}, 33*ecf4f95cSwldfngrs {0x6f4d, 0xbe19, 0, 1, 1}, 34*ecf4f95cSwldfngrs {0x7330, 0xcb62, 0, 1, 0}, 35*ecf4f95cSwldfngrs }}; 36*ecf4f95cSwldfngrs 37*ecf4f95cSwldfngrs LLVM_LIBC_FUNCTION(float16, tanf16, (float16 x)) { 38*ecf4f95cSwldfngrs using FPBits = fputil::FPBits<float16>; 39*ecf4f95cSwldfngrs FPBits xbits(x); 40*ecf4f95cSwldfngrs 41*ecf4f95cSwldfngrs uint16_t x_u = xbits.uintval(); 42*ecf4f95cSwldfngrs uint16_t x_abs = x_u & 0x7fff; 43*ecf4f95cSwldfngrs bool x_sign = x_u >> 15; 44*ecf4f95cSwldfngrs float xf = x; 45*ecf4f95cSwldfngrs 46*ecf4f95cSwldfngrs // Handle exceptional values 47*ecf4f95cSwldfngrs if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign); 48*ecf4f95cSwldfngrs LIBC_UNLIKELY(r.has_value())) 49*ecf4f95cSwldfngrs return r.value(); 50*ecf4f95cSwldfngrs 51*ecf4f95cSwldfngrs // |x| <= 0x1.d1p-5 52*ecf4f95cSwldfngrs if (LIBC_UNLIKELY(x_abs <= 0x2b44)) { 53*ecf4f95cSwldfngrs // |x| <= 0x1.398p-11 54*ecf4f95cSwldfngrs if (LIBC_UNLIKELY(x_abs <= 0x10e6)) { 55*ecf4f95cSwldfngrs // tan(+/-0) = +/-0 56*ecf4f95cSwldfngrs if (LIBC_UNLIKELY(x_abs == 0)) 57*ecf4f95cSwldfngrs return x; 58*ecf4f95cSwldfngrs 59*ecf4f95cSwldfngrs int rounding = fputil::quick_get_round(); 60*ecf4f95cSwldfngrs 61*ecf4f95cSwldfngrs // Exhaustive tests show that, when: 62*ecf4f95cSwldfngrs // x > 0, and rounding upward or 63*ecf4f95cSwldfngrs // x < 0, and rounding downward then, 64*ecf4f95cSwldfngrs // tan(x) = x * 2^-11 + x 65*ecf4f95cSwldfngrs if ((xbits.is_pos() && rounding == FE_UPWARD) || 66*ecf4f95cSwldfngrs (xbits.is_neg() && rounding == FE_DOWNWARD)) 67*ecf4f95cSwldfngrs return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf)); 68*ecf4f95cSwldfngrs return x; 69*ecf4f95cSwldfngrs } 70*ecf4f95cSwldfngrs 71*ecf4f95cSwldfngrs float xsq = xf * xf; 72*ecf4f95cSwldfngrs 73*ecf4f95cSwldfngrs // Degree-6 minimax odd polynomial of tan(x) generated by Sollya with: 74*ecf4f95cSwldfngrs // > P = fpminimax(tan(x)/x, [|0, 2, 4, 6|], [|1, SG...|], [0, pi/32]); 75*ecf4f95cSwldfngrs float result = fputil::polyeval(xsq, 0x1p0f, 0x1.555556p-2f, 0x1.110ee4p-3f, 76*ecf4f95cSwldfngrs 0x1.be80f6p-5f); 77*ecf4f95cSwldfngrs 78*ecf4f95cSwldfngrs return fputil::cast<float16>(xf * result); 79*ecf4f95cSwldfngrs } 80*ecf4f95cSwldfngrs 81*ecf4f95cSwldfngrs // tan(+/-inf) = NaN, and tan(NaN) = NaN 82*ecf4f95cSwldfngrs if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { 83*ecf4f95cSwldfngrs // x = +/-inf 84*ecf4f95cSwldfngrs if (x_abs == 0x7c00) { 85*ecf4f95cSwldfngrs fputil::set_errno_if_required(EDOM); 86*ecf4f95cSwldfngrs fputil::raise_except_if_required(FE_INVALID); 87*ecf4f95cSwldfngrs } 88*ecf4f95cSwldfngrs 89*ecf4f95cSwldfngrs return x + FPBits::quiet_nan().get_val(); 90*ecf4f95cSwldfngrs } 91*ecf4f95cSwldfngrs 92*ecf4f95cSwldfngrs // Range reduction: 93*ecf4f95cSwldfngrs // For |x| > pi/32, we perform range reduction as follows: 94*ecf4f95cSwldfngrs // Find k and y such that: 95*ecf4f95cSwldfngrs // x = (k + y) * pi/32; 96*ecf4f95cSwldfngrs // k is an integer, |y| < 0.5 97*ecf4f95cSwldfngrs // 98*ecf4f95cSwldfngrs // This is done by performing: 99*ecf4f95cSwldfngrs // k = round(x * 32/pi) 100*ecf4f95cSwldfngrs // y = x * 32/pi - k 101*ecf4f95cSwldfngrs // 102*ecf4f95cSwldfngrs // Once k and y are computed, we then deduce the answer by the formula: 103*ecf4f95cSwldfngrs // tan(x) = sin(x) / cos(x) 104*ecf4f95cSwldfngrs // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) 105*ecf4f95cSwldfngrs float sin_k, cos_k, sin_y, cosm1_y; 106*ecf4f95cSwldfngrs sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); 107*ecf4f95cSwldfngrs 108*ecf4f95cSwldfngrs // Note that, cosm1_y = cos_y - 1: 109*ecf4f95cSwldfngrs using fputil::multiply_add; 110*ecf4f95cSwldfngrs return fputil::cast<float16>( 111*ecf4f95cSwldfngrs multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / 112*ecf4f95cSwldfngrs multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); 113*ecf4f95cSwldfngrs } 114*ecf4f95cSwldfngrs 115*ecf4f95cSwldfngrs } // namespace LIBC_NAMESPACE_DECL 116