• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Single-precision sincos 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/sincosf.h"
10 #include "sincosf_utils.h"
11 #include "src/__support/FPUtil/FEnvImpl.h"
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "src/__support/FPUtil/multiply_add.h"
14 #include "src/__support/FPUtil/rounding_mode.h"
15 #include "src/__support/common.h"
16 #include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY
17 #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
18 
19 #include <errno.h>
20 
21 namespace LIBC_NAMESPACE {
22 
23 // Exceptional values
24 static constexpr int N_EXCEPTS = 6;
25 
26 static constexpr uint32_t EXCEPT_INPUTS[N_EXCEPTS] = {
27     0x46199998, // x = 0x1.33333p13   x
28     0x55325019, // x = 0x1.64a032p43  x
29     0x5922aa80, // x = 0x1.4555p51    x
30     0x5f18b878, // x = 0x1.3170fp63   x
31     0x6115cb11, // x = 0x1.2b9622p67  x
32     0x7beef5ef, // x = 0x1.ddebdep120 x
33 };
34 
35 static constexpr uint32_t EXCEPT_OUTPUTS_SIN[N_EXCEPTS][4] = {
36     {0xbeb1fa5d, 0, 1, 0}, // x = 0x1.33333p13, sin(x) = -0x1.63f4bap-2 (RZ)
37     {0xbf171adf, 0, 1, 1}, // x = 0x1.64a032p43, sin(x) = -0x1.2e35bep-1 (RZ)
38     {0xbf587521, 0, 1, 1}, // x = 0x1.4555p51, sin(x) = -0x1.b0ea42p-1 (RZ)
39     {0x3dad60f6, 1, 0, 1}, // x = 0x1.3170fp63, sin(x) = 0x1.5ac1ecp-4 (RZ)
40     {0xbe7cc1e0, 0, 1, 1}, // x = 0x1.2b9622p67, sin(x) = -0x1.f983cp-3 (RZ)
41     {0xbf587d1b, 0, 1, 1}, // x = 0x1.ddebdep120, sin(x) = -0x1.b0fa36p-1 (RZ)
42 };
43 
44 static constexpr uint32_t EXCEPT_OUTPUTS_COS[N_EXCEPTS][4] = {
45     {0xbf70090b, 0, 1, 0}, // x = 0x1.33333p13, cos(x) = -0x1.e01216p-1 (RZ)
46     {0x3f4ea5d2, 1, 0, 0}, // x = 0x1.64a032p43, cos(x) = 0x1.9d4ba4p-1 (RZ)
47     {0x3f08aebe, 1, 0, 1}, // x = 0x1.4555p51, cos(x) = 0x1.115d7cp-1 (RZ)
48     {0x3f7f14bb, 1, 0, 0}, // x = 0x1.3170fp63, cos(x) = 0x1.fe2976p-1 (RZ)
49     {0x3f78142e, 1, 0, 1}, // x = 0x1.2b9622p67, cos(x) = 0x1.f0285cp-1 (RZ)
50     {0x3f08a21c, 1, 0, 0}, // x = 0x1.ddebdep120, cos(x) = 0x1.114438p-1 (RZ)
51 };
52 
53 LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
54   using FPBits = typename fputil::FPBits<float>;
55   FPBits xbits(x);
56 
57   uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU;
58   double xd = static_cast<double>(x);
59 
60   // Range reduction:
61   // For |x| >= 2^-12, we perform range reduction as follows:
62   // Find k and y such that:
63   //   x = (k + y) * pi/32
64   //   k is an integer
65   //   |y| < 0.5
66   // For small range (|x| < 2^45 when FMA instructions are available, 2^22
67   // otherwise), this is done by performing:
68   //   k = round(x * 32/pi)
69   //   y = x * 32/pi - k
70   // For large range, we will omit all the higher parts of 32/pi such that the
71   // least significant bits of their full products with x are larger than 63,
72   // since:
73   //     sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x), and
74   //     cos((k + y + 64*i) * pi/32) = cos(x + i * 2pi) = cos(x).
75   //
76   // When FMA instructions are not available, we store the digits of 32/pi in
77   // chunks of 28-bit precision.  This will make sure that the products:
78   //   x * THIRTYTWO_OVER_PI_28[i] are all exact.
79   // When FMA instructions are available, we simply store the digits of326/pi in
80   // chunks of doubles (53-bit of precision).
81   // So when multiplying by the largest values of single precision, the
82   // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80.  By the
83   // worst-case analysis of range reduction, |y| >= 2^-38, so this should give
84   // us more than 40 bits of accuracy. For the worst-case estimation of range
85   // reduction, see for instances:
86   //   Elementary Functions by J-M. Muller, Chapter 11,
87   //   Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,
88   //   Chapter 10.2.
89   //
90   // Once k and y are computed, we then deduce the answer by the sine and cosine
91   // of sum formulas:
92   //   sin(x) = sin((k + y)*pi/32)
93   //          = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
94   //   cos(x) = cos((k + y)*pi/32)
95   //          = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
96   // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed
97   // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
98   // computed using degree-7 and degree-6 minimax polynomials generated by
99   // Sollya respectively.
100 
101   // |x| < 0x1.0p-12f
102   if (LIBC_UNLIKELY(x_abs < 0x3980'0000U)) {
103     if (LIBC_UNLIKELY(x_abs == 0U)) {
104       // For signed zeros.
105       *sinp = x;
106       *cosp = 1.0f;
107       return;
108     }
109     // When |x| < 2^-12, the relative errors of the approximations
110     //   sin(x) ~ x, cos(x) ~ 1
111     // are:
112     //   |sin(x) - x| / |sin(x)| < |x^3| / (6|x|)
113     //                           = x^2 / 6
114     //                           < 2^-25
115     //                           < epsilon(1)/2.
116     //   |cos(x) - 1| < |x^2 / 2| = 2^-25 < epsilon(1)/2.
117     // So the correctly rounded values of sin(x) and cos(x) are:
118     //   sin(x) = x - sign(x)*eps(x) if rounding mode = FE_TOWARDZERO,
119     //                        or (rounding mode = FE_UPWARD and x is
120     //                        negative),
121     //          = x otherwise.
122     //   cos(x) = 1 - eps(x) if rounding mode = FE_TOWARDZERO or FE_DOWWARD,
123     //          = 1 otherwise.
124     // To simplify the rounding decision and make it more efficient and to
125     // prevent compiler to perform constant folding, we use
126     //   sin(x) = fma(x, -2^-25, x),
127     //   cos(x) = fma(x*0.5f, -x, 1)
128     // instead.
129     // Note: to use the formula x - 2^-25*x to decide the correct rounding, we
130     // do need fma(x, -2^-25, x) to prevent underflow caused by -2^-25*x when
131     // |x| < 2^-125. For targets without FMA instructions, we simply use
132     // double for intermediate results as it is more efficient than using an
133     // emulated version of FMA.
134 #if defined(LIBC_TARGET_CPU_HAS_FMA)
135     *sinp = fputil::multiply_add(x, -0x1.0p-25f, x);
136     *cosp = fputil::multiply_add(FPBits(x_abs).get_val(), -0x1.0p-25f, 1.0f);
137 #else
138     *sinp = static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, xd));
139     *cosp = static_cast<float>(fputil::multiply_add(
140         static_cast<double>(FPBits(x_abs).get_val()), -0x1.0p-25, 1.0));
141 #endif // LIBC_TARGET_CPU_HAS_FMA
142     return;
143   }
144 
145   // x is inf or nan.
146   if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
147     if (x_abs == 0x7f80'0000U) {
148       fputil::set_errno_if_required(EDOM);
149       fputil::raise_except_if_required(FE_INVALID);
150     }
151     *sinp = FPBits::quiet_nan().get_val();
152     *cosp = *sinp;
153     return;
154   }
155 
156   // Check exceptional values.
157   for (int i = 0; i < N_EXCEPTS; ++i) {
158     if (LIBC_UNLIKELY(x_abs == EXCEPT_INPUTS[i])) {
159       uint32_t s = EXCEPT_OUTPUTS_SIN[i][0]; // FE_TOWARDZERO
160       uint32_t c = EXCEPT_OUTPUTS_COS[i][0]; // FE_TOWARDZERO
161       bool x_sign = x < 0;
162       switch (fputil::quick_get_round()) {
163       case FE_UPWARD:
164         s += x_sign ? EXCEPT_OUTPUTS_SIN[i][2] : EXCEPT_OUTPUTS_SIN[i][1];
165         c += EXCEPT_OUTPUTS_COS[i][1];
166         break;
167       case FE_DOWNWARD:
168         s += x_sign ? EXCEPT_OUTPUTS_SIN[i][1] : EXCEPT_OUTPUTS_SIN[i][2];
169         c += EXCEPT_OUTPUTS_COS[i][2];
170         break;
171       case FE_TONEAREST:
172         s += EXCEPT_OUTPUTS_SIN[i][3];
173         c += EXCEPT_OUTPUTS_COS[i][3];
174         break;
175       }
176       *sinp = x_sign ? -FPBits(s).get_val() : FPBits(s).get_val();
177       *cosp = FPBits(c).get_val();
178 
179       return;
180     }
181   }
182 
183   // Combine the results with the sine and cosine of sum formulas:
184   //   sin(x) = sin((k + y)*pi/32)
185   //          = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
186   //          = sin_y * cos_k + (1 + cosm1_y) * sin_k
187   //          = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
188   //   cos(x) = cos((k + y)*pi/32)
189   //          = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
190   //          = cosm1_y * cos_k + sin_y * sin_k
191   //          = (cosm1_y * cos_k + cos_k) + sin_y * sin_k
192   double sin_k, cos_k, sin_y, cosm1_y;
193 
194   sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
195 
196   *sinp = static_cast<float>(fputil::multiply_add(
197       sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
198   *cosp = static_cast<float>(fputil::multiply_add(
199       sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k)));
200 }
201 
202 } // namespace LIBC_NAMESPACE
203