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