1 | //===-- Double-precision cos 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/cos.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/common.h" |
17 | #include "src/__support/macros/config.h" |
18 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
19 | #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
20 | #include "src/math/generic/range_reduction_double_common.h" |
21 | #include "src/math/generic/sincos_eval.h" |
22 | |
23 | #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
24 | #include "range_reduction_double_fma.h" |
25 | #else |
26 | #include "range_reduction_double_nofma.h" |
27 | #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
28 | |
29 | namespace LIBC_NAMESPACE_DECL { |
30 | |
31 | using DoubleDouble = fputil::DoubleDouble; |
32 | using Float128 = typename fputil::DyadicFloat<128>; |
33 | |
34 | LLVM_LIBC_FUNCTION(double, cos, (double x)) { |
35 | using FPBits = typename fputil::FPBits<double>; |
36 | FPBits xbits(x); |
37 | |
38 | uint16_t x_e = xbits.get_biased_exponent(); |
39 | |
40 | DoubleDouble y; |
41 | unsigned k; |
42 | LargeRangeReduction range_reduction_large{}; |
43 | |
44 | // |x| < 2^16. |
45 | if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) { |
46 | // |x| < 2^-7 |
47 | if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) { |
48 | // |x| < 2^-27 |
49 | if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) { |
50 | // Signed zeros. |
51 | if (LIBC_UNLIKELY(x == 0.0)) |
52 | return 1.0; |
53 | |
54 | // For |x| < 2^-27, |cos(x) - 1| < |x|^2/2 < 2^-54 = ulp(1 - 2^-53)/2. |
55 | return fputil::round_result_slightly_down(1.0); |
56 | } |
57 | // No range reduction needed. |
58 | k = 0; |
59 | y.lo = 0.0; |
60 | y.hi = x; |
61 | } else { |
62 | // Small range reduction. |
63 | k = range_reduction_small(x, y); |
64 | } |
65 | } else { |
66 | // Inf or NaN |
67 | if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) { |
68 | if (xbits.is_signaling_nan()) { |
69 | fputil::raise_except_if_required(FE_INVALID); |
70 | return FPBits::quiet_nan().get_val(); |
71 | } |
72 | // cos(+-Inf) = NaN |
73 | if (xbits.get_mantissa() == 0) { |
74 | fputil::set_errno_if_required(EDOM); |
75 | fputil::raise_except_if_required(FE_INVALID); |
76 | } |
77 | return x + FPBits::quiet_nan().get_val(); |
78 | } |
79 | |
80 | // Large range reduction. |
81 | k = range_reduction_large.fast(x, y); |
82 | } |
83 | |
84 | DoubleDouble sin_y, cos_y; |
85 | |
86 | [[maybe_unused]] double err = generic::sincos_eval(y, sin_y, cos_y); |
87 | |
88 | // Look up sin(k * pi/128) and cos(k * pi/128) |
89 | #ifdef LIBC_MATH_HAS_SMALL_TABLES |
90 | // Memory saving versions. Use 65-entry table. |
91 | auto get_idx_dd = [](unsigned kk) -> DoubleDouble { |
92 | unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
93 | DoubleDouble ans = SIN_K_PI_OVER_128[idx]; |
94 | if (kk & 128) { |
95 | ans.hi = -ans.hi; |
96 | ans.lo = -ans.lo; |
97 | } |
98 | return ans; |
99 | }; |
100 | DoubleDouble msin_k = get_idx_dd(k + 128); |
101 | DoubleDouble cos_k = get_idx_dd(k + 64); |
102 | #else |
103 | // Fast look up version, but needs 256-entry table. |
104 | // -sin(k * pi/128) = sin((k + 128) * pi/128) |
105 | // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128). |
106 | DoubleDouble msin_k = SIN_K_PI_OVER_128[(k + 128) & 255]; |
107 | DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255]; |
108 | #endif // LIBC_MATH_HAS_SMALL_TABLES |
109 | |
110 | // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128). |
111 | // So k is an integer and -pi / 256 <= y <= pi / 256. |
112 | // Then cos(x) = cos((k * pi/128 + y) |
113 | // = cos(y) * cos(k*pi/128) - sin(y) * sin(k*pi/128) |
114 | DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k); |
115 | DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k); |
116 | |
117 | DoubleDouble rr = fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi); |
118 | rr.lo += msin_k_sin_y.lo + cos_k_cos_y.lo; |
119 | |
120 | #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
121 | return rr.hi + rr.lo; |
122 | #else |
123 | |
124 | double rlp = rr.lo + err; |
125 | double rlm = rr.lo - err; |
126 | |
127 | double r_upper = rr.hi + rlp; // (rr.lo + ERR); |
128 | double r_lower = rr.hi + rlm; // (rr.lo - ERR); |
129 | |
130 | // Ziv's rounding test. |
131 | if (LIBC_LIKELY(r_upper == r_lower)) |
132 | return r_upper; |
133 | |
134 | Float128 u_f128, sin_u, cos_u; |
135 | if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) |
136 | u_f128 = range_reduction_small_f128(x); |
137 | else |
138 | u_f128 = range_reduction_large.accurate(); |
139 | |
140 | generic::sincos_eval(u_f128, sin_u, cos_u); |
141 | |
142 | auto get_sin_k = [](unsigned kk) -> Float128 { |
143 | unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
144 | Float128 ans = SIN_K_PI_OVER_128_F128[idx]; |
145 | if (kk & 128) |
146 | ans.sign = Sign::NEG; |
147 | return ans; |
148 | }; |
149 | |
150 | // -sin(k * pi/128) = sin((k + 128) * pi/128) |
151 | // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128). |
152 | Float128 msin_k_f128 = get_sin_k(k + 128); |
153 | Float128 cos_k_f128 = get_sin_k(k + 64); |
154 | |
155 | // cos(x) = cos((k * pi/128 + u) |
156 | // = cos(u) * cos(k*pi/128) - sin(u) * sin(k*pi/128) |
157 | Float128 r = fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u), |
158 | fputil::quick_mul(msin_k_f128, sin_u)); |
159 | |
160 | // TODO: Add assertion if Ziv's accuracy tests fail in debug mode. |
161 | // https://github.com/llvm/llvm-project/issues/96452. |
162 | |
163 | return static_cast<double>(r); |
164 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
165 | } |
166 | |
167 | } // namespace LIBC_NAMESPACE_DECL |
168 | |