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

source code of libc/src/math/generic/cosf.cpp