1 | //===-- Double-precision sincos 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 | #include "src/math/sincos.h" |
10 | #include "hdr/errno_macros.h" |
11 | #include "src/__support/FPUtil/FEnvImpl.h" |
12 | #include "src/__support/FPUtil/FPBits.h" |
13 | #include "src/__support/FPUtil/double_double.h" |
14 | #include "src/__support/FPUtil/dyadic_float.h" |
15 | #include "src/__support/FPUtil/except_value_utils.h" |
16 | #include "src/__support/FPUtil/multiply_add.h" |
17 | #include "src/__support/FPUtil/rounding_mode.h" |
18 | #include "src/__support/common.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 | #include "src/math/generic/range_reduction_double_common.h" |
23 | #include "src/math/generic/sincos_eval.h" |
24 | |
25 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
26 | #include "range_reduction_double_fma.h" |
27 | #else |
28 | #include "range_reduction_double_nofma.h" |
29 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
30 | |
31 | namespace LIBC_NAMESPACE_DECL { |
32 | |
33 | using DoubleDouble = fputil::DoubleDouble; |
34 | using Float128 = typename fputil::DyadicFloat<128>; |
35 | |
36 | LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) { |
37 | using FPBits = typename fputil::FPBits<double>; |
38 | FPBits xbits(x); |
39 | |
40 | uint16_t x_e = xbits.get_biased_exponent(); |
41 | |
42 | DoubleDouble y; |
43 | unsigned k; |
44 | LargeRangeReduction range_reduction_large{}; |
45 | |
46 | // |x| < 2^16 |
47 | if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) { |
48 | // |x| < 2^-7 |
49 | if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) { |
50 | // |x| < 2^-27 |
51 | if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) { |
52 | // Signed zeros. |
53 | if (LIBC_UNLIKELY(x == 0.0)) { |
54 | *sin_x = x; |
55 | *cos_x = 1.0; |
56 | return; |
57 | } |
58 | |
59 | // For |x| < 2^-27, max(|sin(x) - x|, |cos(x) - 1|) < ulp(x)/2. |
60 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
61 | *sin_x = fputil::multiply_add(x, -0x1.0p-54, x); |
62 | *cos_x = fputil::multiply_add(x, -x, 1.0); |
63 | #else |
64 | *cos_x = fputil::round_result_slightly_down(1.0); |
65 | |
66 | if (LIBC_UNLIKELY(x_e < 4)) { |
67 | int rounding_mode = fputil::quick_get_round(); |
68 | if (rounding_mode == FE_TOWARDZERO || |
69 | (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) || |
70 | (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD)) |
71 | *sin_x = FPBits(xbits.uintval() - 1).get_val(); |
72 | } |
73 | *sin_x = fputil::multiply_add(x, -0x1.0p-54, x); |
74 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
75 | return; |
76 | } |
77 | // No range reduction needed. |
78 | k = 0; |
79 | y.lo = 0.0; |
80 | y.hi = x; |
81 | } else { |
82 | // Small range reduction. |
83 | k = range_reduction_small(x, y); |
84 | } |
85 | } else { |
86 | // Inf or NaN |
87 | if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) { |
88 | if (xbits.is_signaling_nan()) { |
89 | fputil::raise_except_if_required(FE_INVALID); |
90 | *sin_x = *cos_x = FPBits::quiet_nan().get_val(); |
91 | return; |
92 | } |
93 | |
94 | // sin(+-Inf) = NaN |
95 | if (xbits.get_mantissa() == 0) { |
96 | fputil::set_errno_if_required(EDOM); |
97 | fputil::raise_except_if_required(FE_INVALID); |
98 | } |
99 | *sin_x = *cos_x = x + FPBits::quiet_nan().get_val(); |
100 | return; |
101 | } |
102 | |
103 | // Large range reduction. |
104 | k = range_reduction_large.fast(x, y); |
105 | } |
106 | |
107 | DoubleDouble sin_y, cos_y; |
108 | |
109 | [[maybe_unused]] double err = generic::sincos_eval(y, sin_y, cos_y); |
110 | |
111 | // Look up sin(k * pi/128) and cos(k * pi/128) |
112 | #ifdef LIBC_MATH_HAS_SMALL_TABLES |
113 | // Memory saving versions. Use 65-entry table. |
114 | auto get_idx_dd = [](unsigned kk) -> DoubleDouble { |
115 | unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
116 | DoubleDouble ans = SIN_K_PI_OVER_128[idx]; |
117 | if (kk & 128) { |
118 | ans.hi = -ans.hi; |
119 | ans.lo = -ans.lo; |
120 | } |
121 | return ans; |
122 | }; |
123 | DoubleDouble sin_k = get_idx_dd(k); |
124 | DoubleDouble cos_k = get_idx_dd(k + 64); |
125 | #else |
126 | // Fast look up version, but needs 256-entry table. |
127 | // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128). |
128 | DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255]; |
129 | DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255]; |
130 | #endif // LIBC_MATH_HAS_SMALL_TABLES |
131 | |
132 | DoubleDouble msin_k{-sin_k.lo, -sin_k.hi}; |
133 | |
134 | // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128). |
135 | // So k is an integer and -pi / 256 <= y <= pi / 256. |
136 | // Then sin(x) = sin((k * pi/128 + y) |
137 | // = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128) |
138 | DoubleDouble sin_k_cos_y = fputil::quick_mult(cos_y, sin_k); |
139 | DoubleDouble cos_k_sin_y = fputil::quick_mult(sin_y, cos_k); |
140 | // cos(x) = cos((k * pi/128 + y) |
141 | // = cos(y) * cos(k*pi/128) - sin(y) * sin(k*pi/128) |
142 | DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k); |
143 | DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k); |
144 | |
145 | DoubleDouble sin_dd = |
146 | fputil::exact_add<false>(sin_k_cos_y.hi, cos_k_sin_y.hi); |
147 | DoubleDouble cos_dd = |
148 | fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi); |
149 | sin_dd.lo += sin_k_cos_y.lo + cos_k_sin_y.lo; |
150 | cos_dd.lo += msin_k_sin_y.lo + cos_k_cos_y.lo; |
151 | |
152 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
153 | *sin_x = sin_dd.hi + sin_dd.lo; |
154 | *cos_x = cos_dd.hi + cos_dd.lo; |
155 | return; |
156 | #else |
157 | // Accurate test and pass for correctly rounded implementation. |
158 | |
159 | double sin_lp = sin_dd.lo + err; |
160 | double sin_lm = sin_dd.lo - err; |
161 | double cos_lp = cos_dd.lo + err; |
162 | double cos_lm = cos_dd.lo - err; |
163 | |
164 | double sin_upper = sin_dd.hi + sin_lp; |
165 | double sin_lower = sin_dd.hi + sin_lm; |
166 | double cos_upper = cos_dd.hi + cos_lp; |
167 | double cos_lower = cos_dd.hi + cos_lm; |
168 | |
169 | // Ziv's rounding test. |
170 | if (LIBC_LIKELY(sin_upper == sin_lower && cos_upper == cos_lower)) { |
171 | *sin_x = sin_upper; |
172 | *cos_x = cos_upper; |
173 | return; |
174 | } |
175 | |
176 | Float128 u_f128, sin_u, cos_u; |
177 | if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) |
178 | u_f128 = range_reduction_small_f128(x); |
179 | else |
180 | u_f128 = range_reduction_large.accurate(); |
181 | |
182 | generic::sincos_eval(u_f128, sin_u, cos_u); |
183 | |
184 | auto get_sin_k = [](unsigned kk) -> Float128 { |
185 | unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
186 | Float128 ans = SIN_K_PI_OVER_128_F128[idx]; |
187 | if (kk & 128) |
188 | ans.sign = Sign::NEG; |
189 | return ans; |
190 | }; |
191 | |
192 | // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128). |
193 | Float128 sin_k_f128 = get_sin_k(k); |
194 | Float128 cos_k_f128 = get_sin_k(k + 64); |
195 | Float128 msin_k_f128 = get_sin_k(k + 128); |
196 | |
197 | // TODO: Add assertion if Ziv's accuracy tests fail in debug mode. |
198 | // https://github.com/llvm/llvm-project/issues/96452. |
199 | |
200 | if (sin_upper == sin_lower) |
201 | *sin_x = sin_upper; |
202 | else |
203 | // sin(x) = sin((k * pi/128 + u) |
204 | // = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128) |
205 | *sin_x = static_cast<double>( |
206 | fputil::quick_add(fputil::quick_mul(sin_k_f128, cos_u), |
207 | fputil::quick_mul(cos_k_f128, sin_u))); |
208 | |
209 | if (cos_upper == cos_lower) |
210 | *cos_x = cos_upper; |
211 | else |
212 | // cos(x) = cos((k * pi/128 + u) |
213 | // = cos(u) * cos(k*pi/128) - sin(u) * sin(k*pi/128) |
214 | *cos_x = static_cast<double>( |
215 | fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u), |
216 | fputil::quick_mul(msin_k_f128, sin_u))); |
217 | |
218 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
219 | } |
220 | |
221 | } // namespace LIBC_NAMESPACE_DECL |
222 | |