Skip to content

Commit 3d0beca

Browse files
committed
improve atanpif16 implementation
1 parent bdae514 commit 3d0beca

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

libc/src/math/generic/atanpif16.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ namespace LIBC_NAMESPACE_DECL {
2323
// >>> from sympy import *
2424
// >>> import math
2525
// >>> x = symbols('x')
26-
// >>> print(series(atan(x)/math.pi, x, 0, 21))
26+
// >>> print(series(atan(x)/math.pi, x, 0, 17))
2727
//
2828
// Output:
2929
// 0.318309886183791*x - 0.106103295394597*x**3 + 0.0636619772367581*x**5 -
3030
// 0.0454728408833987*x**7 + 0.0353677651315323*x**9 - 0.0289372623803446*x**11
31-
// + 0.0244853758602916*x**13 - 0.0212206590789194*x**15 +
32-
// 0.0187241109519877*x**17 - 0.01675315190441*x**19 + O(x**21)
31+
// + 0.0244853758602916*x**13 - 0.0212206590789194*x**15 + O(x**17)
3332
//
3433
// We will assign this 19-degree Taylor polynomial as g(x). This polynomial
3534
// approximation is accurate for arctan(x)/pi when |x| is in the range [0, 0.5].
@@ -86,13 +85,17 @@ LLVM_LIBC_FUNCTION(float16, atanpif16, (float16 x)) {
8685
}
8786

8887
if (LIBC_UNLIKELY(xbits.is_zero())) {
89-
return x; //
88+
return x;
9089
}
9190

9291
double x_abs = fputil::cast<double>(xbits.abs().get_val());
9392

93+
if (LIBC_UNLIKELY(x_abs == 1.0)) {
94+
return signed_result(0.25);
95+
}
96+
9497
// polynomial coefficients for atan(x)/pi taylor series
95-
// generated using sympy: series(atan(x)/pi, x, 0, 21)
98+
// generated using sympy: series(atan(x)/pi, x, 0, 17)
9699
constexpr double POLY_COEFFS[] = {
97100
0x1.45f306dc9c889p-2, // x^1: 1/pi
98101
-0x1.b2995e7b7b60bp-4, // x^3: -1/(3*pi)
@@ -102,17 +105,14 @@ LLVM_LIBC_FUNCTION(float16, atanpif16, (float16 x)) {
102105
-0x1.da1bace3cc68ep-6, // x^11: -1/(11*pi)
103106
0x1.912b1c2336cf2p-6, // x^13: 1/(13*pi)
104107
-0x1.5bade52f95e7p-6, // x^15: -1/(15*pi)
105-
0x1.32c69d0bde9e8p-6, // x^17: 1/(17*pi)
106-
-0x1.127bcfe232f8cp-6, // x^19: -1/(19*pi)
107108
};
108109

109110
// evaluate atan(x)/pi using polynomial approximation, valid for |x| <= 0.5
110111
constexpr auto atanpi_eval = [](double x) -> double {
111112
double xx = x * x;
112113
return x * fputil::polyeval(xx, POLY_COEFFS[0], POLY_COEFFS[1],
113114
POLY_COEFFS[2], POLY_COEFFS[3], POLY_COEFFS[4],
114-
POLY_COEFFS[5], POLY_COEFFS[6], POLY_COEFFS[7],
115-
POLY_COEFFS[8], POLY_COEFFS[9]);
115+
POLY_COEFFS[5], POLY_COEFFS[6], POLY_COEFFS[7]);
116116
};
117117

118118
// Case 1: |x| <= 0.5 - Direct polynomial evaluation

0 commit comments

Comments
 (0)