xref: /llvm-project/libc/src/math/generic/tanf.cpp (revision 46944b0cbc9a9d8daad0182c40fcd3560bc9ca35)
182d6e770STue Ly //===-- Single-precision tan function -------------------------------------===//
282d6e770STue Ly //
382d6e770STue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
482d6e770STue Ly // See https://llvm.org/LICENSE.txt for license information.
582d6e770STue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
682d6e770STue Ly //
782d6e770STue Ly //===----------------------------------------------------------------------===//
882d6e770STue Ly 
982d6e770STue Ly #include "src/math/tanf.h"
10e15b2da4STue Ly #include "sincosf_utils.h"
1182d6e770STue Ly #include "src/__support/FPUtil/FEnvImpl.h"
1282d6e770STue Ly #include "src/__support/FPUtil/FPBits.h"
1382d6e770STue Ly #include "src/__support/FPUtil/PolyEval.h"
1482d6e770STue Ly #include "src/__support/FPUtil/except_value_utils.h"
1582d6e770STue Ly #include "src/__support/FPUtil/multiply_add.h"
1682d6e770STue Ly #include "src/__support/FPUtil/nearest_integer.h"
1782d6e770STue Ly #include "src/__support/common.h"
18*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
19737e1cd1SGuillaume Chatelet #include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY
20737e1cd1SGuillaume Chatelet #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
2182d6e770STue Ly 
22*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
2382d6e770STue Ly 
2482d6e770STue Ly // Exceptional cases for tanf.
25e15b2da4STue Ly constexpr size_t N_EXCEPTS = 6;
2682d6e770STue Ly 
27e15b2da4STue Ly constexpr fputil::ExceptValues<float, N_EXCEPTS> TANF_EXCEPTS{{
28a4d48e3bSTue Ly     // (inputs, RZ output, RU offset, RD offset, RN offset)
29e15b2da4STue Ly     // x = 0x1.ada6aap27, tan(x) = 0x1.e80304p-3 (RZ)
30e15b2da4STue Ly     {0x4d56d355, 0x3e740182, 1, 0, 0},
31e15b2da4STue Ly     // x = 0x1.862064p33, tan(x) = -0x1.8dee56p-3 (RZ)
32e15b2da4STue Ly     {0x50431032, 0xbe46f72b, 0, 1, 1},
33a4d48e3bSTue Ly     // x = 0x1.af61dap48, tan(x) = 0x1.60d1c6p-2 (RZ)
34a4d48e3bSTue Ly     {0x57d7b0ed, 0x3eb068e3, 1, 0, 1},
35e15b2da4STue Ly     // x = 0x1.0088bcp52, tan(x) = 0x1.ca1edp0 (RZ)
36e15b2da4STue Ly     {0x5980445e, 0x3fe50f68, 1, 0, 0},
37e15b2da4STue Ly     // x = 0x1.f90dfcp72, tan(x) = 0x1.597f9cp-1 (RZ)
38e15b2da4STue Ly     {0x63fc86fe, 0x3f2cbfce, 1, 0, 0},
39a4d48e3bSTue Ly     // x = 0x1.a6ce12p86, tan(x) = -0x1.c5612ep-1 (RZ)
40a4d48e3bSTue Ly     {0x6ad36709, 0xbf62b097, 0, 1, 0},
4182d6e770STue Ly }};
4282d6e770STue Ly 
4382d6e770STue Ly LLVM_LIBC_FUNCTION(float, tanf, (float x)) {
4482d6e770STue Ly   using FPBits = typename fputil::FPBits<float>;
4582d6e770STue Ly   FPBits xbits(x);
46e15b2da4STue Ly   bool x_sign = xbits.uintval() >> 31;
47e15b2da4STue Ly   uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;
4882d6e770STue Ly 
4982d6e770STue Ly   // |x| < pi/32
5029f8e076SGuillaume Chatelet   if (LIBC_UNLIKELY(x_abs <= 0x3dc9'0fdbU)) {
5182d6e770STue Ly     double xd = static_cast<double>(x);
5282d6e770STue Ly 
5382d6e770STue Ly     // |x| < 0x1.0p-12f
5429f8e076SGuillaume Chatelet     if (LIBC_UNLIKELY(x_abs < 0x3980'0000U)) {
5529f8e076SGuillaume Chatelet       if (LIBC_UNLIKELY(x_abs == 0U)) {
5682d6e770STue Ly         // For signed zeros.
5782d6e770STue Ly         return x;
5882d6e770STue Ly       }
5982d6e770STue Ly       // When |x| < 2^-12, the relative error of the approximation tan(x) ~ x
6082d6e770STue Ly       // is:
6182d6e770STue Ly       //   |tan(x) - x| / |tan(x)| < |x^3| / (3|x|)
6282d6e770STue Ly       //                           = x^2 / 3
6382d6e770STue Ly       //                           < 2^-25
6482d6e770STue Ly       //                           < epsilon(1)/2.
6582d6e770STue Ly       // So the correctly rounded values of tan(x) are:
6682d6e770STue Ly       //   = x + sign(x)*eps(x) if rounding mode = FE_UPWARD and x is positive,
6782d6e770STue Ly       //                        or (rounding mode = FE_DOWNWARD and x is
6882d6e770STue Ly       //                        negative),
6982d6e770STue Ly       //   = x otherwise.
7082d6e770STue Ly       // To simplify the rounding decision and make it more efficient, we use
7182d6e770STue Ly       //   fma(x, 2^-25, x) instead.
7282d6e770STue Ly       // Note: to use the formula x + 2^-25*x to decide the correct rounding, we
7382d6e770STue Ly       // do need fma(x, 2^-25, x) to prevent underflow caused by 2^-25*x when
7482d6e770STue Ly       // |x| < 2^-125. For targets without FMA instructions, we simply use
7582d6e770STue Ly       // double for intermediate results as it is more efficient than using an
7682d6e770STue Ly       // emulated version of FMA.
77a2569a76SGuillaume Chatelet #if defined(LIBC_TARGET_CPU_HAS_FMA)
7882d6e770STue Ly       return fputil::multiply_add(x, 0x1.0p-25f, x);
7982d6e770STue Ly #else
8082d6e770STue Ly       return static_cast<float>(fputil::multiply_add(xd, 0x1.0p-25, xd));
81a2569a76SGuillaume Chatelet #endif // LIBC_TARGET_CPU_HAS_FMA
8282d6e770STue Ly     }
8382d6e770STue Ly 
8482d6e770STue Ly     // |x| < pi/32
8582d6e770STue Ly     double xsq = xd * xd;
8682d6e770STue Ly 
8782d6e770STue Ly     // Degree-9 minimax odd polynomial of tan(x) generated by Sollya with:
8882d6e770STue Ly     // > P = fpminimax(tan(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/32]);
8982d6e770STue Ly     double result =
9082d6e770STue Ly         fputil::polyeval(xsq, 1.0, 0x1.555555553d022p-2, 0x1.111111ce442c1p-3,
9182d6e770STue Ly                          0x1.ba180a6bbdecdp-5, 0x1.69c0a88a0b71fp-6);
927d11a592SAlex Brachet     return static_cast<float>(xd * result);
9382d6e770STue Ly   }
9482d6e770STue Ly 
95e15b2da4STue Ly   // Check for exceptional values
9629f8e076SGuillaume Chatelet   if (LIBC_UNLIKELY(x_abs == 0x3f8a1f62U)) {
97e15b2da4STue Ly     // |x| = 0x1.143ec4p0
98e15b2da4STue Ly     float sign = x_sign ? -1.0f : 1.0f;
99e15b2da4STue Ly 
100135cea49STue Ly     // volatile is used to prevent compiler (gcc) from optimizing the
101135cea49STue Ly     // computation, making the results incorrect in different rounding modes.
102135cea49STue Ly     volatile float tmp = 0x1.ddf9f4p0f;
103135cea49STue Ly     tmp = fputil::multiply_add(sign, tmp, sign * 0x1.1p-24f);
104135cea49STue Ly 
105135cea49STue Ly     return tmp;
106e15b2da4STue Ly   }
107e15b2da4STue Ly 
108e15b2da4STue Ly   // |x| > 0x1.ada6a8p+27f
10929f8e076SGuillaume Chatelet   if (LIBC_UNLIKELY(x_abs > 0x4d56'd354U)) {
11082d6e770STue Ly     // Inf or NaN
11129f8e076SGuillaume Chatelet     if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
11282d6e770STue Ly       if (x_abs == 0x7f80'0000U) {
1130aa9593cSTue Ly         fputil::set_errno_if_required(EDOM);
1140aa9593cSTue Ly         fputil::raise_except_if_required(FE_INVALID);
11582d6e770STue Ly       }
116ace383dfSGuillaume Chatelet       return x + FPBits::quiet_nan().get_val();
11782d6e770STue Ly     }
118e15b2da4STue Ly     // Other large exceptional values
119e15b2da4STue Ly     if (auto r = TANF_EXCEPTS.lookup_odd(x_abs, x_sign);
12029f8e076SGuillaume Chatelet         LIBC_UNLIKELY(r.has_value()))
121a4d48e3bSTue Ly       return r.value();
12282d6e770STue Ly   }
12382d6e770STue Ly 
124e15b2da4STue Ly   // For |x| >= pi/32, we use the definition of tan(x) function:
125e15b2da4STue Ly   //   tan(x) = sin(x) / cos(x)
126e15b2da4STue Ly   // The we follow the same computations of sin(x) and cos(x) as sinf, cosf,
127e15b2da4STue Ly   // and sincosf.
12882d6e770STue Ly 
129e15b2da4STue Ly   double xd = static_cast<double>(x);
130e15b2da4STue Ly   double sin_k, cos_k, sin_y, cosm1_y;
13182d6e770STue Ly 
132e15b2da4STue Ly   sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
133e15b2da4STue Ly   // tan(x) = sin(x) / cos(x)
134e15b2da4STue Ly   //        = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
135e15b2da4STue Ly   using fputil::multiply_add;
1367d11a592SAlex Brachet   return static_cast<float>(
1377d11a592SAlex Brachet       multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
1387d11a592SAlex Brachet       multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
13982d6e770STue Ly }
14082d6e770STue Ly 
141*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
142