xref: /aosp_15_r20/external/llvm-libc/src/math/generic/inv_trigf_utils.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Single-precision general inverse trigonometric functions ----------===//
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 #ifndef LLVM_LIBC_SRC_MATH_GENERIC_INV_TRIGF_UTILS_H
10 #define LLVM_LIBC_SRC_MATH_GENERIC_INV_TRIGF_UTILS_H
11 
12 #include "src/__support/FPUtil/PolyEval.h"
13 #include "src/__support/FPUtil/multiply_add.h"
14 #include "src/__support/common.h"
15 #include "src/__support/macros/config.h"
16 
17 namespace LIBC_NAMESPACE_DECL {
18 
19 // PI and PI / 2
20 constexpr double M_MATH_PI = 0x1.921fb54442d18p+1;
21 constexpr double M_MATH_PI_2 = 0x1.921fb54442d18p+0;
22 
23 extern double ATAN_COEFFS[17][9];
24 
25 // For |x| <= 1/32 and 0 <= i <= 16, return Q(x) such that:
26 //   Q(x) ~ (atan(x + i/16) - atan(i/16)) / x.
atan_eval(double x,int i)27 LIBC_INLINE double atan_eval(double x, int i) {
28   double x2 = x * x;
29 
30   double c0 = fputil::multiply_add(x, ATAN_COEFFS[i][2], ATAN_COEFFS[i][1]);
31   double c1 = fputil::multiply_add(x, ATAN_COEFFS[i][4], ATAN_COEFFS[i][3]);
32   double c2 = fputil::multiply_add(x, ATAN_COEFFS[i][6], ATAN_COEFFS[i][5]);
33   double c3 = fputil::multiply_add(x, ATAN_COEFFS[i][8], ATAN_COEFFS[i][7]);
34 
35   double x4 = x2 * x2;
36   double d1 = fputil::multiply_add(x2, c1, c0);
37   double d2 = fputil::multiply_add(x2, c3, c2);
38   double p = fputil::multiply_add(x4, d2, d1);
39   return p;
40 }
41 
42 // > Q = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20|],
43 //                 [|1, D...|], [0, 0.5]);
44 constexpr double ASIN_COEFFS[10] = {0x1.5555555540fa1p-3, 0x1.333333512edc2p-4,
45                                     0x1.6db6cc1541b31p-5, 0x1.f1caff324770ep-6,
46                                     0x1.6e43899f5f4f4p-6, 0x1.1f847cf652577p-6,
47                                     0x1.9b60f47f87146p-7, 0x1.259e2634c494fp-6,
48                                     -0x1.df946fa875ddp-8, 0x1.02311ecf99c28p-5};
49 
50 // Evaluate P(x^2) - 1, where P(x^2) ~ asin(x)/x
asin_eval(double xsq)51 LIBC_INLINE double asin_eval(double xsq) {
52   double x4 = xsq * xsq;
53   double r1 = fputil::polyeval(x4, ASIN_COEFFS[0], ASIN_COEFFS[2],
54                                ASIN_COEFFS[4], ASIN_COEFFS[6], ASIN_COEFFS[8]);
55   double r2 = fputil::polyeval(x4, ASIN_COEFFS[1], ASIN_COEFFS[3],
56                                ASIN_COEFFS[5], ASIN_COEFFS[7], ASIN_COEFFS[9]);
57   return fputil::multiply_add(xsq, r2, r1);
58 }
59 
60 } // namespace LIBC_NAMESPACE_DECL
61 
62 #endif // LLVM_LIBC_SRC_MATH_GENERIC_INV_TRIGF_UTILS_H
63