xref: /aosp_15_r20/external/llvm-libc/src/math/generic/cos.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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
24 #include "range_reduction_double_fma.h"
25 #else
26 #include "range_reduction_double_nofma.h"
27 #endif // LIBC_TARGET_CPU_HAS_FMA
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       // sin(+-Inf) = NaN
69       if (xbits.get_mantissa() == 0) {
70         fputil::set_errno_if_required(EDOM);
71         fputil::raise_except_if_required(FE_INVALID);
72       }
73       return x + FPBits::quiet_nan().get_val();
74     }
75 
76     // Large range reduction.
77     k = range_reduction_large.fast(x, y);
78   }
79 
80   DoubleDouble sin_y, cos_y;
81 
82   [[maybe_unused]] double err = generic::sincos_eval(y, sin_y, cos_y);
83 
84   // Look up sin(k * pi/128) and cos(k * pi/128)
85 #ifdef LIBC_MATH_HAS_SMALL_TABLES
86   // Memory saving versions.  Use 65-entry table.
__anon30977e8a0102(unsigned kk) 87   auto get_idx_dd = [](unsigned kk) -> DoubleDouble {
88     unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
89     DoubleDouble ans = SIN_K_PI_OVER_128[idx];
90     if (kk & 128) {
91       ans.hi = -ans.hi;
92       ans.lo = -ans.lo;
93     }
94     return ans;
95   };
96   DoubleDouble msin_k = get_idx_dd(k + 128);
97   DoubleDouble cos_k = get_idx_dd(k + 64);
98 #else
99   // Fast look up version, but needs 256-entry table.
100   // -sin(k * pi/128) = sin((k + 128) * pi/128)
101   // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
102   DoubleDouble msin_k = SIN_K_PI_OVER_128[(k + 128) & 255];
103   DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255];
104 #endif // LIBC_MATH_HAS_SMALL_TABLES
105 
106   // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128).
107   // So k is an integer and -pi / 256 <= y <= pi / 256.
108   // Then cos(x) = cos((k * pi/128 + y)
109   //             = cos(y) * cos(k*pi/128) - sin(y) * sin(k*pi/128)
110   DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k);
111   DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k);
112 
113   DoubleDouble rr = fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi);
114   rr.lo += msin_k_sin_y.lo + cos_k_cos_y.lo;
115 
116 #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
117   return rr.hi + rr.lo;
118 #else
119 
120   double rlp = rr.lo + err;
121   double rlm = rr.lo - err;
122 
123   double r_upper = rr.hi + rlp; // (rr.lo + ERR);
124   double r_lower = rr.hi + rlm; // (rr.lo - ERR);
125 
126   // Ziv's rounding test.
127   if (LIBC_LIKELY(r_upper == r_lower))
128     return r_upper;
129 
130   Float128 u_f128, sin_u, cos_u;
131   if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT))
132     u_f128 = range_reduction_small_f128(x);
133   else
134     u_f128 = range_reduction_large.accurate();
135 
136   generic::sincos_eval(u_f128, sin_u, cos_u);
137 
__anon30977e8a0202(unsigned kk) 138   auto get_sin_k = [](unsigned kk) -> Float128 {
139     unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);
140     Float128 ans = SIN_K_PI_OVER_128_F128[idx];
141     if (kk & 128)
142       ans.sign = Sign::NEG;
143     return ans;
144   };
145 
146   // -sin(k * pi/128) = sin((k + 128) * pi/128)
147   // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
148   Float128 msin_k_f128 = get_sin_k(k + 128);
149   Float128 cos_k_f128 = get_sin_k(k + 64);
150 
151   // cos(x) = cos((k * pi/128 + u)
152   //        = cos(u) * cos(k*pi/128) - sin(u) * sin(k*pi/128)
153   Float128 r = fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u),
154                                  fputil::quick_mul(msin_k_f128, sin_u));
155 
156   // TODO: Add assertion if Ziv's accuracy tests fail in debug mode.
157   // https://github.com/llvm/llvm-project/issues/96452.
158 
159   return static_cast<double>(r);
160 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
161 }
162 
163 } // namespace LIBC_NAMESPACE_DECL
164