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
16namespace LIBC_NAMESPACE {
17
18// PI and PI / 2
19constexpr double M_MATH_PI = 0x1.921fb54442d18p+1;
20constexpr double M_MATH_PI_2 = 0x1.921fb54442d18p+0;
21
22extern double ATAN_COEFFS[17][9];
23
24// For |x| <= 1/32 and 0 <= i <= 16, return Q(x) such that:
25// Q(x) ~ (atan(x + i/16) - atan(i/16)) / x.
26LIBC_INLINE double atan_eval(double x, int i) {
27 double x2 = x * x;
28
29 double c0 = fputil::multiply_add(x, y: ATAN_COEFFS[i][2], z: ATAN_COEFFS[i][1]);
30 double c1 = fputil::multiply_add(x, y: ATAN_COEFFS[i][4], z: ATAN_COEFFS[i][3]);
31 double c2 = fputil::multiply_add(x, y: ATAN_COEFFS[i][6], z: ATAN_COEFFS[i][5]);
32 double c3 = fputil::multiply_add(x, y: ATAN_COEFFS[i][8], z: ATAN_COEFFS[i][7]);
33
34 double x4 = x2 * x2;
35 double d1 = fputil::multiply_add(x: x2, y: c1, z: c0);
36 double d2 = fputil::multiply_add(x: x2, y: c3, z: c2);
37 double p = fputil::multiply_add(x: x4, y: d2, z: d1);
38 return p;
39}
40
41// > Q = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20|],
42// [|1, D...|], [0, 0.5]);
43constexpr double ASIN_COEFFS[10] = {0x1.5555555540fa1p-3, 0x1.333333512edc2p-4,
44 0x1.6db6cc1541b31p-5, 0x1.f1caff324770ep-6,
45 0x1.6e43899f5f4f4p-6, 0x1.1f847cf652577p-6,
46 0x1.9b60f47f87146p-7, 0x1.259e2634c494fp-6,
47 -0x1.df946fa875ddp-8, 0x1.02311ecf99c28p-5};
48
49// Evaluate P(x^2) - 1, where P(x^2) ~ asin(x)/x
50LIBC_INLINE double asin_eval(double xsq) {
51 double x4 = xsq * xsq;
52 double r1 = fputil::polyeval(x: x4, a0: ASIN_COEFFS[0], a: ASIN_COEFFS[2],
53 a: ASIN_COEFFS[4], a: ASIN_COEFFS[6], a: ASIN_COEFFS[8]);
54 double r2 = fputil::polyeval(x: x4, a0: ASIN_COEFFS[1], a: ASIN_COEFFS[3],
55 a: ASIN_COEFFS[5], a: ASIN_COEFFS[7], a: ASIN_COEFFS[9]);
56 return fputil::multiply_add(x: xsq, y: r2, z: r1);
57}
58
59} // namespace LIBC_NAMESPACE
60
61#endif // LLVM_LIBC_SRC_MATH_GENERIC_INV_TRIGF_UTILS_H
62

source code of libc/src/math/generic/inv_trigf_utils.h