1//===-- Single-precision tan 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#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TANF_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_TANF_H
11
12#include "sincosf_utils.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/except_value_utils.h"
17#include "src/__support/FPUtil/multiply_add.h"
18#include "src/__support/FPUtil/nearest_integer.h"
19#include "src/__support/macros/config.h"
20#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
21#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
22
23namespace LIBC_NAMESPACE_DECL {
24
25namespace math {
26
27namespace tanf_internal {
28
29#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
30// Exceptional cases for tanf.
31LIBC_INLINE_VAR constexpr size_t N_EXCEPTS = 6;
32
33LIBC_INLINE_VAR constexpr fputil::ExceptValues<float, N_EXCEPTS> TANF_EXCEPTS{.values: {
34 // (inputs, RZ output, RU offset, RD offset, RN offset)
35 // x = 0x1.ada6aap27, tan(x) = 0x1.e80304p-3 (RZ)
36 {.input: 0x4d56d355, .rnd_towardzero_result: 0x3e740182, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
37 // x = 0x1.862064p33, tan(x) = -0x1.8dee56p-3 (RZ)
38 {.input: 0x50431032, .rnd_towardzero_result: 0xbe46f72b, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 1},
39 // x = 0x1.af61dap48, tan(x) = 0x1.60d1c6p-2 (RZ)
40 {.input: 0x57d7b0ed, .rnd_towardzero_result: 0x3eb068e3, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
41 // x = 0x1.0088bcp52, tan(x) = 0x1.ca1edp0 (RZ)
42 {.input: 0x5980445e, .rnd_towardzero_result: 0x3fe50f68, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
43 // x = 0x1.f90dfcp72, tan(x) = 0x1.597f9cp-1 (RZ)
44 {.input: 0x63fc86fe, .rnd_towardzero_result: 0x3f2cbfce, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
45 // x = 0x1.a6ce12p86, tan(x) = -0x1.c5612ep-1 (RZ)
46 {.input: 0x6ad36709, .rnd_towardzero_result: 0xbf62b097, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0},
47}};
48#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
49
50} // namespace tanf_internal
51
52LIBC_INLINE float tanf(float x) {
53 using namespace sincosf_utils_internal;
54 using namespace tanf_internal;
55 using FPBits = typename fputil::FPBits<float>;
56 FPBits xbits(x);
57 uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;
58
59 // |x| < pi/32
60 if (LIBC_UNLIKELY(x_abs <= 0x3dc9'0fdbU)) {
61 double xd = static_cast<double>(x);
62
63 // |x| < 0x1.0p-12f
64 if (LIBC_UNLIKELY(x_abs < 0x3980'0000U)) {
65 if (LIBC_UNLIKELY(x_abs == 0U)) {
66 // For signed zeros.
67 return x;
68 }
69 // When |x| < 2^-12, the relative error of the approximation tan(x) ~ x
70 // is:
71 // |tan(x) - x| / |tan(x)| < |x^3| / (3|x|)
72 // = x^2 / 3
73 // < 2^-25
74 // < epsilon(1)/2.
75 // So the correctly rounded values of tan(x) are:
76 // = x + sign(x)*eps(x) if rounding mode = FE_UPWARD and x is positive,
77 // or (rounding mode = FE_DOWNWARD and x is
78 // negative),
79 // = x otherwise.
80 // To simplify the rounding decision and make it more efficient, we use
81 // fma(x, 2^-25, x) instead.
82 // Note: to use the formula x + 2^-25*x to decide the correct rounding, we
83 // do need fma(x, 2^-25, x) to prevent underflow caused by 2^-25*x when
84 // |x| < 2^-125. For targets without FMA instructions, we simply use
85 // double for intermediate results as it is more efficient than using an
86 // emulated version of FMA.
87#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)
88 return fputil::multiply_add(x, 0x1.0p-25f, x);
89#else
90 return static_cast<float>(fputil::multiply_add(x: xd, y: 0x1.0p-25, z: xd));
91#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
92 }
93
94 // |x| < pi/32
95 double xsq = xd * xd;
96
97 // Degree-9 minimax odd polynomial of tan(x) generated by Sollya with:
98 // > P = fpminimax(tan(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/32]);
99 double result =
100 fputil::polyeval(x: xsq, a0: 1.0, a: 0x1.555555553d022p-2, a: 0x1.111111ce442c1p-3,
101 a: 0x1.ba180a6bbdecdp-5, a: 0x1.69c0a88a0b71fp-6);
102 return static_cast<float>(xd * result);
103 }
104
105#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
106 bool x_sign = xbits.uintval() >> 31;
107 // Check for exceptional values
108 if (LIBC_UNLIKELY(x_abs == 0x3f8a1f62U)) {
109 // |x| = 0x1.143ec4p0
110 float sign = x_sign ? -1.0f : 1.0f;
111
112 // volatile is used to prevent compiler (gcc) from optimizing the
113 // computation, making the results incorrect in different rounding modes.
114 volatile float tmp = 0x1.ddf9f4p0f;
115 tmp = fputil::multiply_add(x: sign, y: tmp, z: sign * 0x1.1p-24f);
116
117 return tmp;
118 }
119#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
120
121 // |x| > 0x1.ada6a8p+27f
122 if (LIBC_UNLIKELY(x_abs > 0x4d56'd354U)) {
123 // Inf or NaN
124 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
125 if (xbits.is_signaling_nan()) {
126 fputil::raise_except_if_required(FE_INVALID);
127 return FPBits::quiet_nan().get_val();
128 }
129
130 if (x_abs == 0x7f80'0000U) {
131 fputil::set_errno_if_required(EDOM);
132 fputil::raise_except_if_required(FE_INVALID);
133 }
134 return x + FPBits::quiet_nan().get_val();
135 }
136#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
137 // Other large exceptional values
138 if (auto r = TANF_EXCEPTS.lookup_odd(x_abs, sign: x_sign);
139 LIBC_UNLIKELY(r.has_value()))
140 return r.value();
141#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
142 }
143
144 // For |x| >= pi/32, we use the definition of tan(x) function:
145 // tan(x) = sin(x) / cos(x)
146 // The we follow the same computations of sin(x) and cos(x) as sinf, cosf,
147 // and sincosf.
148
149 double xd = static_cast<double>(x);
150 double sin_k, cos_k, sin_y, cosm1_y;
151
152 sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
153 // tan(x) = sin(x) / cos(x)
154 // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
155 using fputil::multiply_add;
156 return static_cast<float>(
157 multiply_add(x: sin_y, y: cos_k, z: multiply_add(x: cosm1_y, y: sin_k, z: sin_k)) /
158 multiply_add(x: sin_y, y: -sin_k, z: multiply_add(x: cosm1_y, y: cos_k, z: cos_k)));
159}
160
161} // namespace math
162
163} // namespace LIBC_NAMESPACE_DECL
164
165#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANF_H
166