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