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/// Double-precision implementation of cosf.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_COSF_DOUBLE_EVAL_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_COSF_DOUBLE_EVAL_H
16
17#include "src/__support/FPUtil/FEnvImpl.h"
18#include "src/__support/FPUtil/FPBits.h"
19#include "src/__support/FPUtil/except_value_utils.h"
20#include "src/__support/FPUtil/multiply_add.h"
21#include "src/__support/macros/config.h"
22#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
23#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
24
25#include "sincosf_utils.h"
26
27namespace LIBC_NAMESPACE_DECL {
28
29namespace math {
30namespace double_eval {
31
32LIBC_INLINE float cosf(float x) {
33 using namespace sincosf_utils_internal;
34
35#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
36 // Exceptional cases for cosf.
37 constexpr size_t N_EXCEPTS = 6;
38
39 constexpr fputil::ExceptValues<float, N_EXCEPTS> COSF_EXCEPTS{.values: {
40 // (inputs, RZ output, RU offset, RD offset, RN offset)
41 // x = 0x1.64a032p43, cos(x) = 0x1.9d4ba4p-1 (RZ)
42 {.input: 0x55325019, .rnd_towardzero_result: 0x3f4ea5d2, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
43 // x = 0x1.4555p51, cos(x) = 0x1.115d7cp-1 (RZ)
44 {.input: 0x5922aa80, .rnd_towardzero_result: 0x3f08aebe, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
45 // x = 0x1.48a858p54, cos(x) = 0x1.f48148p-2 (RZ)
46 {.input: 0x5aa4542c, .rnd_towardzero_result: 0x3efa40a4, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
47 // x = 0x1.3170fp63, cos(x) = 0x1.fe2976p-1 (RZ)
48 {.input: 0x5f18b878, .rnd_towardzero_result: 0x3f7f14bb, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
49 // x = 0x1.2b9622p67, cos(x) = 0x1.f0285cp-1 (RZ)
50 {.input: 0x6115cb11, .rnd_towardzero_result: 0x3f78142e, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
51 // x = 0x1.ddebdep120, cos(x) = 0x1.114438p-1 (RZ)
52 {.input: 0x7beef5ef, .rnd_towardzero_result: 0x3f08a21c, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
53 }};
54#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
55
56 using FPBits = typename fputil::FPBits<float>;
57
58 FPBits xbits(x);
59 xbits.set_sign(Sign::POS);
60
61 uint32_t x_abs = xbits.uintval();
62
63 // Range reduction:
64 // For |x| > pi/16, we perform range reduction as follows:
65 // Find k and y such that:
66 // x = (k + y) * pi/32
67 // k is an integer
68 // |y| < 0.5
69 // For small range (|x| < 2^45 when FMA instructions are available, 2^22
70 // otherwise), this is done by performing:
71 // k = round(x * 32/pi)
72 // y = x * 32/pi - k
73 // For large range, we will omit all the higher parts of 16/pi such that the
74 // least significant bits of their full products with x are larger than 63,
75 // since cos((k + y + 64*i) * pi/32) = cos(x + i * 2pi) = cos(x).
76 //
77 // When FMA instructions are not available, we store the digits of 32/pi in
78 // chunks of 28-bit precision. This will make sure that the products:
79 // x * THIRTYTWO_OVER_PI_28[i] are all exact.
80 // When FMA instructions are available, we simply store the digits of 32/pi in
81 // chunks of doubles (53-bit of precision).
82 // So when multiplying by the largest values of single precision, the
83 // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80. By the
84 // worst-case analysis of range reduction, |y| >= 2^-38, so this should give
85 // us more than 40 bits of accuracy. For the worst-case estimation of range
86 // reduction, see for instances:
87 // Elementary Functions by J-M. Muller, Chapter 11,
88 // Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,
89 // Chapter 10.2.
90 //
91 // Once k and y are computed, we then deduce the answer by the cosine of sum
92 // formula:
93 // cos(x) = cos((k + y)*pi/32)
94 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
95 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed
96 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
97 // computed using degree-7 and degree-6 minimax polynomials generated by
98 // Sollya respectively.
99
100#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
101 // |x| < 0x1.0p-12f
102 if (LIBC_UNLIKELY(x_abs < 0x3980'0000U)) {
103 // When |x| < 2^-12, the relative error of the approximation cos(x) ~ 1
104 // is:
105 // |cos(x) - 1| < |x^2 / 2| = 2^-25 < epsilon(1)/2.
106 // So the correctly rounded values of cos(x) are:
107 // = 1 - eps(x) if rounding mode = FE_TOWARDZERO or FE_DOWWARD,
108 // = 1 otherwise.
109 // To simplify the rounding decision and make it more efficient and to
110 // prevent compiler to perform constant folding, we use
111 // fma(x, -2^-25, 1) instead.
112 // Note: to use the formula 1 - 2^-25*x to decide the correct rounding, we
113 // do need fma(x, -2^-25, 1) to prevent underflow caused by -2^-25*x when
114 // |x| < 2^-125. For targets without FMA instructions, we simply use
115 // double for intermediate results as it is more efficient than using an
116 // emulated version of FMA.
117#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)
118 return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f);
119#else // !LIBC_TARGET_CPU_HAS_FMA_FLOAT
120 double xd = static_cast<double>(xbits.get_val());
121 return static_cast<float>(fputil::multiply_add(x: xd, y: -0x1.0p-25, z: 1.0));
122#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
123 }
124
125 if (auto r = COSF_EXCEPTS.lookup(x_bits: x_abs); LIBC_UNLIKELY(r.has_value()))
126 return r.value();
127#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
128
129 // x is inf or nan.
130 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
131 if (xbits.is_signaling_nan()) {
132 fputil::raise_except_if_required(FE_INVALID);
133 return FPBits::quiet_nan().get_val();
134 }
135
136 if (x_abs == 0x7f80'0000U) {
137 fputil::set_errno_if_required(EDOM);
138 fputil::raise_except_if_required(FE_INVALID);
139 }
140 return x + FPBits::quiet_nan().get_val();
141 }
142
143 double xd = static_cast<double>(xbits.get_val());
144 // Combine the results with the sine of sum formula:
145 // cos(x) = cos((k + y)*pi/32)
146 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
147 // = cosm1_y * cos_k + sin_y * sin_k
148 // = (cosm1_y * cos_k + cos_k) + sin_y * sin_k
149 double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
150
151 sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
152
153 return static_cast<float>(fputil::multiply_add(
154 x: sin_y, y: -sin_k, z: fputil::multiply_add(x: cosm1_y, y: cos_k, z: cos_k)));
155}
156
157} // namespace double_eval
158} // namespace math
159
160} // namespace LIBC_NAMESPACE_DECL
161
162#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSF_DOUBLE_EVAL_H
163