1//===----------------------------------------------------------------------===//
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/// \file
10/// Implementation header for sin.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SIN_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_SIN_H
16
17#include "range_reduction_double_common.h"
18#include "sincos_eval.h"
19#include "src/__support/FPUtil/FEnvImpl.h"
20#include "src/__support/FPUtil/FPBits.h"
21#include "src/__support/FPUtil/double_double.h"
22#include "src/__support/FPUtil/dyadic_float.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
25#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
26
27#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
28#include "range_reduction_double_fma.h"
29#else
30#include "range_reduction_double_nofma.h"
31#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
32
33namespace LIBC_NAMESPACE_DECL {
34
35namespace math {
36
37#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
38LIBC_INLINE double
39sin_accurate(double x, uint16_t x_e, unsigned k,
40 const range_reduction_double_internal::LargeRangeReduction
41 &range_reduction_large) {
42 using namespace math::range_reduction_double_internal;
43 using FPBits = typename fputil::FPBits<double>;
44
45 DFloat128 u_f128, sin_u, cos_u;
46 if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT))
47 u_f128 = range_reduction_small_f128(x);
48 else
49 u_f128 = range_reduction_large.accurate();
50
51 math::sincos_eval_internal::sincos_eval(u: u_f128, sin_u, cos_u);
52
53 auto get_sin_k = [](unsigned kk) -> DFloat128 {
54 unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
55 DFloat128 ans = SIN_K_PI_OVER_128_F128[idx];
56 if (kk & 128)
57 ans.sign = Sign::NEG;
58 return ans;
59 };
60
61 // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
62 DFloat128 sin_k_f128 = get_sin_k(k);
63 DFloat128 cos_k_f128 = get_sin_k(k + 64);
64
65 // sin(x) = sin(k * pi/128 + u)
66 // = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128)
67 DFloat128 r = fputil::quick_add(a: fputil::quick_mul(a: sin_k_f128, b: cos_u),
68 b: fputil::quick_mul(a: cos_k_f128, b: sin_u));
69
70 // TODO: Add assertion if Ziv's accuracy tests fail in debug mode.
71 // https://github.com/llvm/llvm-project/issues/96452.
72
73 return static_cast<double>(r);
74}
75#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
76
77LIBC_INLINE double sin(double x) {
78 using namespace math::range_reduction_double_internal;
79 using FPBits = typename fputil::FPBits<double>;
80 FPBits xbits(x);
81
82 uint16_t x_e = xbits.get_biased_exponent();
83
84 DoubleDouble y;
85 unsigned k = 0;
86 LargeRangeReduction range_reduction_large{};
87
88 // |x| < 2^16
89 if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {
90 // |x| < 2^-4
91 if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 4)) {
92 // |x| < 2^-26, |sin(x) - x| < ulp(x)/2.
93 if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 26)) {
94 // Signed zeros.
95 if (LIBC_UNLIKELY(x == 0.0))
96 return x + x; // Make sure it works with FTZ/DAZ.
97
98#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
99 return fputil::multiply_add(x, -0x1.0p-54, x);
100#else
101 if (LIBC_UNLIKELY(x_e < 4)) {
102#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
103 int rounding_mode = fputil::quick_get_round();
104 if (rounding_mode == FE_TOWARDZERO ||
105 (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) ||
106 (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD))
107 return FPBits(xbits.uintval() - 1).get_val();
108#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
109 }
110 return fputil::multiply_add(x, y: -0x1.0p-54, z: x);
111#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
112 }
113 // No range reduction needed.
114
115 // Use degree-9 polynomial approximation:
116 // sin(x) ~ x + a1 * x^3 + a2 * x^5 + a3 * x^7 + a4 * x^9
117 // ~ x + x^3 * Q(x^2).
118 // > P = fpminimax(sin(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, 2^-4]);
119 // > dirtyinfnorm((sin(x) - x*P)/sin(x), [-2^-4, 2^-4]);
120 // 0x1.3c2e...p-69
121 // > P;
122 constexpr double COEFFS[] = {-0x1.5555555555555p-3, 0x1.111111110f491p-7,
123 -0x1.a01a00e16af3ep-13,
124 0x1.71c24233f1bafp-19};
125 double x_sq = x * x;
126 double c0 = fputil::multiply_add(x: x_sq, y: COEFFS[1], z: COEFFS[0]);
127 double c1 = fputil::multiply_add(x: x_sq, y: COEFFS[3], z: COEFFS[2]);
128 double x4 = x_sq * x_sq;
129 double x3 = x * x_sq;
130 double r_lo = fputil::multiply_add(x: x4, y: c1, z: c0) * x3;
131
132#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
133 return x + r_lo;
134#else
135 // Overall errors <= ulp(x^3) + |x| * 2^-68.
136 double err = fputil::multiply_add(x: x_sq, y: 0x1.0p-52, z: 0x1.0p-68);
137 double r_lo_u = fputil::multiply_add(x, y: err, z: r_lo);
138 double r_lo_l = fputil::multiply_add(x: -x, y: err, z: r_lo);
139 double r_upper = x + r_lo_u;
140 double r_lower = x + r_lo_l;
141
142 if (LIBC_LIKELY(r_upper == r_lower))
143 return r_upper;
144
145 k = range_reduction_small(x, u&: y);
146 return sin_accurate(x, x_e, k, range_reduction_large);
147#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
148 } else {
149 // Small range reduction.
150 k = range_reduction_small(x, u&: y);
151 }
152 } else {
153 // Inf or NaN
154 if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {
155 // sin(+-Inf) = NaN
156 if (xbits.is_signaling_nan()) {
157 fputil::raise_except_if_required(FE_INVALID);
158 return FPBits::quiet_nan().get_val();
159 }
160
161 if (xbits.get_mantissa() == 0) {
162 fputil::set_errno_if_required(EDOM);
163 fputil::raise_except_if_required(FE_INVALID);
164 }
165 return x + FPBits::quiet_nan().get_val();
166 }
167
168 // Large range reduction.
169 k = range_reduction_large.fast(x, u&: y);
170 }
171
172 DoubleDouble sin_y, cos_y;
173
174 [[maybe_unused]] double err =
175 math::sincos_eval_internal::sincos_eval(u: y, sin_u&: sin_y, cos_u&: cos_y);
176
177 // Look up sin(k * pi/128) and cos(k * pi/128)
178#ifdef LIBC_MATH_HAS_SMALL_TABLES
179 // Memory saving versions. Use 65-entry table.
180 auto get_idx_dd = [](unsigned kk) -> DoubleDouble {
181 unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
182 DoubleDouble ans = SIN_K_PI_OVER_128[idx];
183 if (kk & 128) {
184 ans.hi = -ans.hi;
185 ans.lo = -ans.lo;
186 }
187 return ans;
188 };
189 DoubleDouble sin_k = get_idx_dd(k);
190 DoubleDouble cos_k = get_idx_dd(k + 64);
191#else
192 // Fast look up version, but needs 256-entry table.
193 // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
194 DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255];
195 DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255];
196#endif
197
198 // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128).
199 // So k is an integer and -pi / 256 <= y <= pi / 256.
200 // Then sin(x) = sin((k * pi/128 + y)
201 // = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128)
202 DoubleDouble sin_k_cos_y = fputil::quick_mult(a: cos_y, b: sin_k);
203 DoubleDouble cos_k_sin_y = fputil::quick_mult(a: sin_y, b: cos_k);
204 // When k != 0 mod 128,
205 // |sin( k * pi/128 )| > pi/128 - epsilon > |y| >= |sin(y)|,
206 // and cos(y) > 1 - pi/128. So we can use Fast2Sum for the addition:
207 // sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128).
208 DoubleDouble rr = fputil::exact_add(a: sin_k_cos_y.hi, b: cos_k_sin_y.hi);
209 rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
210
211#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
212 return rr.hi + rr.lo;
213#else
214 // Accurate test and pass for correctly rounded implementation.
215
216 double rlp = rr.lo + err;
217 double rlm = rr.lo - err;
218
219 double r_upper = rr.hi + rlp; // (rr.lo + ERR);
220 double r_lower = rr.hi + rlm; // (rr.lo - ERR);
221
222 // Ziv's rounding test.
223 if (LIBC_LIKELY(r_upper == r_lower))
224 return r_upper;
225
226 return sin_accurate(x, x_e, k, range_reduction_large);
227#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
228}
229
230} // namespace math
231
232} // namespace LIBC_NAMESPACE_DECL
233
234#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SIN_H
235