[Workflow] Roll back some settings since they caused more issues
[llvm-project.git] / libc / src / math / generic / sinf.cpp
blob01d2f70e73568bb152e514b1aea56d2c8a3b115a
1 //===-- Single-precision sin 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 //===----------------------------------------------------------------------===//
9 #include "src/math/sinf.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/PolyEval.h"
15 #include "src/__support/FPUtil/multiply_add.h"
16 #include "src/__support/FPUtil/rounding_mode.h"
17 #include "src/__support/common.h"
18 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
19 #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
21 #include <errno.h>
23 #if defined(LIBC_TARGET_CPU_HAS_FMA)
24 #include "range_reduction_fma.h"
25 #else
26 #include "range_reduction.h"
27 #endif
29 namespace __llvm_libc {
31 LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
32 using FPBits = typename fputil::FPBits<float>;
33 FPBits xbits(x);
35 uint32_t x_u = xbits.uintval();
36 uint32_t x_abs = x_u & 0x7fff'ffffU;
37 double xd = static_cast<double>(x);
39 // Range reduction:
40 // For |x| > pi/32, we perform range reduction as follows:
41 // Find k and y such that:
42 // x = (k + y) * pi/32
43 // k is an integer
44 // |y| < 0.5
45 // For small range (|x| < 2^45 when FMA instructions are available, 2^22
46 // otherwise), this is done by performing:
47 // k = round(x * 32/pi)
48 // y = x * 32/pi - k
49 // For large range, we will omit all the higher parts of 32/pi such that the
50 // least significant bits of their full products with x are larger than 63,
51 // since sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x).
53 // When FMA instructions are not available, we store the digits of 32/pi in
54 // chunks of 28-bit precision. This will make sure that the products:
55 // x * THIRTYTWO_OVER_PI_28[i] are all exact.
56 // When FMA instructions are available, we simply store the digits of 32/pi in
57 // chunks of doubles (53-bit of precision).
58 // So when multiplying by the largest values of single precision, the
59 // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80. By the
60 // worst-case analysis of range reduction, |y| >= 2^-38, so this should give
61 // us more than 40 bits of accuracy. For the worst-case estimation of range
62 // reduction, see for instances:
63 // Elementary Functions by J-M. Muller, Chapter 11,
64 // Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,
65 // Chapter 10.2.
67 // Once k and y are computed, we then deduce the answer by the sine of sum
68 // formula:
69 // sin(x) = sin((k + y)*pi/32)
70 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
71 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
72 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
73 // computed using degree-7 and degree-6 minimax polynomials generated by
74 // Sollya respectively.
76 // |x| <= pi/16
77 if (LIBC_UNLIKELY(x_abs <= 0x3e49'0fdbU)) {
79 // |x| < 0x1.d12ed2p-12f
80 if (LIBC_UNLIKELY(x_abs < 0x39e8'9769U)) {
81 if (LIBC_UNLIKELY(x_abs == 0U)) {
82 // For signed zeros.
83 return x;
85 // When |x| < 2^-12, the relative error of the approximation sin(x) ~ x
86 // is:
87 // |sin(x) - x| / |sin(x)| < |x^3| / (6|x|)
88 // = x^2 / 6
89 // < 2^-25
90 // < epsilon(1)/2.
91 // So the correctly rounded values of sin(x) are:
92 // = x - sign(x)*eps(x) if rounding mode = FE_TOWARDZERO,
93 // or (rounding mode = FE_UPWARD and x is
94 // negative),
95 // = x otherwise.
96 // To simplify the rounding decision and make it more efficient, we use
97 // fma(x, -2^-25, x) instead.
98 // An exhaustive test shows that this formula work correctly for all
99 // rounding modes up to |x| < 0x1.c555dep-11f.
100 // Note: to use the formula x - 2^-25*x to decide the correct rounding, we
101 // do need fma(x, -2^-25, x) to prevent underflow caused by -2^-25*x when
102 // |x| < 2^-125. For targets without FMA instructions, we simply use
103 // double for intermediate results as it is more efficient than using an
104 // emulated version of FMA.
105 #if defined(LIBC_TARGET_CPU_HAS_FMA)
106 return fputil::multiply_add(x, -0x1.0p-25f, x);
107 #else
108 return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, xd));
109 #endif // LIBC_TARGET_CPU_HAS_FMA
112 // |x| < pi/16.
113 double xsq = xd * xd;
115 // Degree-9 polynomial approximation:
116 // sin(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
117 // = x (1 + a_3 x^2 + ... + a_9 x^8)
118 // = x * P(x^2)
119 // generated by Sollya with the following commands:
120 // > display = hexadecimal;
121 // > Q = fpminimax(sin(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
122 double result =
123 fputil::polyeval(xsq, 1.0, -0x1.55555555554c6p-3, 0x1.1111111085e65p-7,
124 -0x1.a019f70fb4d4fp-13, 0x1.718d179815e74p-19);
125 return static_cast<float>(xd * result);
128 if (LIBC_UNLIKELY(x_abs == 0x4619'9998U)) { // x = 0x1.33333p13
129 float r = -0x1.63f4bap-2f;
130 int rounding = fputil::quick_get_round();
131 bool sign = xbits.get_sign();
132 if ((rounding == FE_DOWNWARD && !sign) || (rounding == FE_UPWARD && sign))
133 r = -0x1.63f4bcp-2f;
134 return xbits.get_sign() ? -r : r;
137 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
138 if (x_abs == 0x7f80'0000U) {
139 fputil::set_errno_if_required(EDOM);
140 fputil::raise_except_if_required(FE_INVALID);
142 return x + FPBits::build_quiet_nan(0);
145 // Combine the results with the sine of sum formula:
146 // sin(x) = sin((k + y)*pi/32)
147 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
148 // = sin_y * cos_k + (1 + cosm1_y) * sin_k
149 // = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
150 double sin_k, cos_k, sin_y, cosm1_y;
152 sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
154 return static_cast<float>(fputil::multiply_add(
155 sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
158 } // namespace __llvm_libc