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