1bbb75554SSiva Chandra //===-- Single-precision sin function -------------------------------------===//
2bbb75554SSiva Chandra //
3bbb75554SSiva Chandra // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bbb75554SSiva Chandra // See https://llvm.org/LICENSE.txt for license information.
5bbb75554SSiva Chandra // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bbb75554SSiva Chandra //
7bbb75554SSiva Chandra //===----------------------------------------------------------------------===//
8bbb75554SSiva Chandra 
9bbb75554SSiva Chandra #include "src/math/sinf.h"
10d883a4adSTue Ly #include "src/__support/FPUtil/BasicOperations.h"
11d883a4adSTue Ly #include "src/__support/FPUtil/FEnvImpl.h"
12d883a4adSTue Ly #include "src/__support/FPUtil/FPBits.h"
13d883a4adSTue Ly #include "src/__support/FPUtil/PolyEval.h"
14d883a4adSTue Ly #include "src/__support/FPUtil/except_value_utils.h"
15d883a4adSTue Ly #include "src/__support/FPUtil/multiply_add.h"
16bbb75554SSiva Chandra #include "src/__support/common.h"
17bbb75554SSiva Chandra 
18d883a4adSTue Ly #include <errno.h>
19d883a4adSTue Ly 
20d883a4adSTue Ly #if defined(LIBC_TARGET_HAS_FMA)
21d883a4adSTue Ly #include "range_reduction_fma.h"
22d883a4adSTue Ly // using namespace __llvm_libc::fma;
23d883a4adSTue Ly using __llvm_libc::fma::FAST_PASS_BOUND;
24d883a4adSTue Ly using __llvm_libc::fma::large_range_reduction;
25d883a4adSTue Ly using __llvm_libc::fma::LargeExcepts;
26d883a4adSTue Ly using __llvm_libc::fma::N_EXCEPT_LARGE;
27d883a4adSTue Ly using __llvm_libc::fma::N_EXCEPT_SMALL;
28d883a4adSTue Ly using __llvm_libc::fma::small_range_reduction;
29d883a4adSTue Ly using __llvm_libc::fma::SmallExcepts;
30d883a4adSTue Ly #else
31d883a4adSTue Ly #include "range_reduction.h"
32d883a4adSTue Ly // using namespace __llvm_libc::generic;
33d883a4adSTue Ly using __llvm_libc::generic::FAST_PASS_BOUND;
34d883a4adSTue Ly using __llvm_libc::generic::large_range_reduction;
35d883a4adSTue Ly using __llvm_libc::generic::LargeExcepts;
36d883a4adSTue Ly using __llvm_libc::generic::N_EXCEPT_LARGE;
37d883a4adSTue Ly using __llvm_libc::generic::N_EXCEPT_SMALL;
38d883a4adSTue Ly using __llvm_libc::generic::small_range_reduction;
39d883a4adSTue Ly using __llvm_libc::generic::SmallExcepts;
40d883a4adSTue Ly #endif
41bbb75554SSiva Chandra 
42bbb75554SSiva Chandra namespace __llvm_libc {
43bbb75554SSiva Chandra 
44d883a4adSTue Ly LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
45d883a4adSTue Ly   using FPBits = typename fputil::FPBits<float>;
46d883a4adSTue Ly   FPBits xbits(x);
47bbb75554SSiva Chandra 
48d883a4adSTue Ly   uint32_t x_u = xbits.uintval();
49d883a4adSTue Ly   uint32_t x_abs = x_u & 0x7fff'ffffU;
50d883a4adSTue Ly   double xd, y;
51bbb75554SSiva Chandra 
52d883a4adSTue Ly   // Range reduction:
53d883a4adSTue Ly   // For |x| > pi/16, we perform range reduction as follows:
54d883a4adSTue Ly   // Find k and y such that:
55d883a4adSTue Ly   //   x = (k + y) * pi
56d883a4adSTue Ly   //   k is an integer
57d883a4adSTue Ly   //   |y| < 0.5
58d883a4adSTue Ly   // For small range (|x| < 2^50 when FMA instructions are available, 2^26
59d883a4adSTue Ly   // otherwise), this is done by performing:
60d883a4adSTue Ly   //   k = round(x * 1/pi)
61d883a4adSTue Ly   //   y = x * 1/pi - k
62d883a4adSTue Ly   // For large range, we will omit all the higher parts of 1/pi such that the
63d883a4adSTue Ly   // least significant bits of their full products with x are larger than 1,
64d883a4adSTue Ly   // since sin(x + i * 2pi) = sin(x).
65d883a4adSTue Ly   //
66d883a4adSTue Ly   // When FMA instructions are not available, we store the digits of 1/pi in
67d883a4adSTue Ly   // chunks of 28-bit precision.  This will make sure that the products:
68d883a4adSTue Ly   //   x * ONE_OVER_PI_28[i] are all exact.
69d883a4adSTue Ly   // When FMA instructions are available, we simply store the digits of 1/pi in
70d883a4adSTue Ly   // chunks of doubles (53-bit of precision).
71d883a4adSTue Ly   // So when multiplying by the largest values of single precision, the
72d883a4adSTue Ly   // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80.  By the
73d883a4adSTue Ly   // worst-case analysis of range reduction, |y| >= 2^-38, so this should give
74d883a4adSTue Ly   // us more than 40 bits of accuracy. For the worst-case estimation of range
75d883a4adSTue Ly   // reduction, see for instances:
76d883a4adSTue Ly   //   Elementary Functions by J-M. Muller, Chapter 11,
77d883a4adSTue Ly   //   Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,
78d883a4adSTue Ly   //   Chapter 10.2.
79d883a4adSTue Ly   //
80d883a4adSTue Ly   // Once k and y are computed, we then deduce the answer by the sine of sum
81d883a4adSTue Ly   // formula:
82d883a4adSTue Ly   //   sin(x) = sin((k + y)*pi)
83d883a4adSTue Ly   //          = sin(y*pi) * cos(k*pi) + cos(y*pi) * sin(k*pi)
84d883a4adSTue Ly   //          = (-1)^(k & 1) * sin(y*pi)
85d883a4adSTue Ly   //          ~ (-1)^(k & 1) * y * P(y^2)
86d883a4adSTue Ly   // where y*P(y^2) is a degree-15 minimax polynomial generated by Sollya
87d883a4adSTue Ly   // with: > Q = fpminimax(sin(x*pi)/x, [|0, 2, 4, 6, 8, 10, 12, 14|],
88d883a4adSTue Ly   // [|D...|], [0, 0.5]);
89d883a4adSTue Ly 
90d883a4adSTue Ly   // |x| <= pi/16
91d883a4adSTue Ly   if (x_abs <= 0x3e49'0fdbU) {
92d883a4adSTue Ly     xd = static_cast<double>(x);
93d883a4adSTue Ly 
94d883a4adSTue Ly     // |x| < 0x1.d12ed2p-12f
95d883a4adSTue Ly     if (x_abs < 0x39e8'9769U) {
96d883a4adSTue Ly       if (unlikely(x_abs == 0U)) {
97d883a4adSTue Ly         // For signed zeros.
98d883a4adSTue Ly         return x;
99d883a4adSTue Ly       }
100d883a4adSTue Ly       // When |x| < 2^-12, the relative error of the approximation sin(x) ~ x
101d883a4adSTue Ly       // is:
102d883a4adSTue Ly       //   |sin(x) - x| / |sin(x)| < |x^3| / (6|x|)
103d883a4adSTue Ly       //                           = x^2 / 6
104d883a4adSTue Ly       //                           < 2^-25
105d883a4adSTue Ly       //                           < epsilon(1)/2.
106d883a4adSTue Ly       // So the correctly rounded values of sin(x) are:
107d883a4adSTue Ly       //   = x - sign(x)*eps(x) if rounding mode = FE_TOWARDZERO,
108d883a4adSTue Ly       //                        or (rounding mode = FE_UPWARD and x is
109d883a4adSTue Ly       //                        negative),
110d883a4adSTue Ly       //   = x otherwise.
111d883a4adSTue Ly       // To simplify the rounding decision and make it more efficient, we use
112d883a4adSTue Ly       //   fma(x, -2^-25, x) instead.
113d883a4adSTue Ly       // An exhaustive test shows that this formula work correctly for all
114d883a4adSTue Ly       // rounding modes up to |x| < 0x1.c555dep-11f.
115d883a4adSTue Ly       // Note: to use the formula x - 2^-25*x to decide the correct rounding, we
116d883a4adSTue Ly       // do need fma(x, -2^-25, x) to prevent underflow caused by -2^-25*x when
117d883a4adSTue Ly       // |x| < 2^-125. For targets without FMA instructions, we simply use
118d883a4adSTue Ly       // double for intermediate results as it is more efficient than using an
119d883a4adSTue Ly       // emulated version of FMA.
120d883a4adSTue Ly #if defined(LIBC_TARGET_HAS_FMA)
121d883a4adSTue Ly       return fputil::multiply_add(x, -0x1.0p-25f, x);
122d883a4adSTue Ly #else
123d883a4adSTue Ly       return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, xd));
124d883a4adSTue Ly #endif // LIBC_TARGET_HAS_FMA
125bbb75554SSiva Chandra     }
126bbb75554SSiva Chandra 
127d883a4adSTue Ly     // |x| < pi/16.
128d883a4adSTue Ly     double xsq = xd * xd;
129bbb75554SSiva Chandra 
130d883a4adSTue Ly     // Degree-9 polynomial approximation:
131d883a4adSTue Ly     //   sin(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
132d883a4adSTue Ly     //          = x (1 + a_3 x^2 + ... + a_9 x^8)
133d883a4adSTue Ly     //          = x * P(x^2)
134d883a4adSTue Ly     // generated by Sollya with the following commands:
135d883a4adSTue Ly     // > display = hexadecimal;
136d883a4adSTue Ly     // > Q = fpminimax(sin(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
137d883a4adSTue Ly     double result =
138d883a4adSTue Ly         fputil::polyeval(xsq, 1.0, -0x1.55555555554c6p-3, 0x1.1111111085e65p-7,
139d883a4adSTue Ly                          -0x1.a019f70fb4d4fp-13, 0x1.718d179815e74p-19);
140d883a4adSTue Ly     return xd * result;
141bbb75554SSiva Chandra   }
142bbb75554SSiva Chandra 
143d883a4adSTue Ly   bool x_sign = xbits.get_sign();
144d883a4adSTue Ly 
145d883a4adSTue Ly   int64_t k;
146d883a4adSTue Ly   xd = static_cast<double>(x);
147d883a4adSTue Ly 
148d883a4adSTue Ly   if (x_abs < FAST_PASS_BOUND) {
149d883a4adSTue Ly     using ExceptChecker =
150d883a4adSTue Ly         typename fputil::ExceptionChecker<float, N_EXCEPT_SMALL>;
151d883a4adSTue Ly     {
152d883a4adSTue Ly       float result;
153d883a4adSTue Ly       if (ExceptChecker::check_odd_func(SmallExcepts, x_abs, x_sign, result)) {
154d883a4adSTue Ly         return result;
155d883a4adSTue Ly       }
156d883a4adSTue Ly     }
157d883a4adSTue Ly 
158d883a4adSTue Ly     k = small_range_reduction(xd, y);
159d883a4adSTue Ly   } else {
160d883a4adSTue Ly     // x is inf or nan.
161d883a4adSTue Ly     if (unlikely(x_abs >= 0x7f80'0000U)) {
162*83193a5eSTue Ly       if (x_abs == 0x7f80'0000U) {
163d883a4adSTue Ly         errno = EDOM;
164*83193a5eSTue Ly         fputil::set_except(FE_INVALID);
165*83193a5eSTue Ly       }
166d883a4adSTue Ly       return x +
167d883a4adSTue Ly              FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1));
168d883a4adSTue Ly     }
169d883a4adSTue Ly 
170d883a4adSTue Ly     using ExceptChecker =
171d883a4adSTue Ly         typename fputil::ExceptionChecker<float, N_EXCEPT_LARGE>;
172d883a4adSTue Ly     {
173d883a4adSTue Ly       float result;
174d883a4adSTue Ly       if (ExceptChecker::check_odd_func(LargeExcepts, x_abs, x_sign, result))
175d883a4adSTue Ly         return result;
176d883a4adSTue Ly     }
177d883a4adSTue Ly 
178d883a4adSTue Ly     k = large_range_reduction(xd, xbits.get_exponent(), y);
179d883a4adSTue Ly   }
180d883a4adSTue Ly 
181d883a4adSTue Ly   // After range reduction, k = round(x / pi) and y = (x/pi) - k.
182d883a4adSTue Ly   // So k is an integer and -0.5 <= y <= 0.5.
183d883a4adSTue Ly   // Then sin(x) = sin(y*pi + k*pi)
184d883a4adSTue Ly   //             = (-1)^(k & 1) * sin(y*pi)
185d883a4adSTue Ly   //             ~ (-1)^(k & 1) * y * P(y^2)
186d883a4adSTue Ly   // where y*P(y^2) is a degree-15 minimax polynomial generated by Sollya
187d883a4adSTue Ly   // with: > P = fpminimax(sin(x*pi)/x, [|0, 2, 4, 6, 8, 10, 12, 14|],
188d883a4adSTue Ly   // [|D...|], [0, 0.5]);
189d883a4adSTue Ly 
190d883a4adSTue Ly   constexpr double SIGN[2] = {1.0, -1.0};
191d883a4adSTue Ly 
192d883a4adSTue Ly   double ysq = y * y;
193d883a4adSTue Ly   double result =
194d883a4adSTue Ly       y * fputil::polyeval(ysq, 0x1.921fb54442d17p1, -0x1.4abbce625bd4bp2,
195d883a4adSTue Ly                            0x1.466bc67750a3fp1, -0x1.32d2cce1612b5p-1,
196d883a4adSTue Ly                            0x1.507832417bce6p-4, -0x1.e3062119b6071p-8,
197d883a4adSTue Ly                            0x1.e89c7aa14122dp-12, -0x1.625b1709dece6p-16);
198d883a4adSTue Ly 
199d883a4adSTue Ly   return SIGN[k & 1] * result;
200d883a4adSTue Ly   // }
201bbb75554SSiva Chandra }
202bbb75554SSiva Chandra 
203bbb75554SSiva Chandra } // namespace __llvm_libc
204