1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Double-precision atan(x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2022-2024, Arm Limited.
5*f3087befSAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner */
7*f3087befSAndrew Turner
8*f3087befSAndrew Turner #include "test_sig.h"
9*f3087befSAndrew Turner #include "test_defs.h"
10*f3087befSAndrew Turner #include "atan_common.h"
11*f3087befSAndrew Turner
12*f3087befSAndrew Turner #define AbsMask 0x7fffffffffffffff
13*f3087befSAndrew Turner #define PiOver2 0x1.921fb54442d18p+0
14*f3087befSAndrew Turner #define TinyBound 0x3e1 /* top12(asuint64(0x1p-30)). */
15*f3087befSAndrew Turner #define BigBound 0x434 /* top12(asuint64(0x1p53)). */
16*f3087befSAndrew Turner #define OneTop 0x3ff
17*f3087befSAndrew Turner
18*f3087befSAndrew Turner /* Fast implementation of double-precision atan.
19*f3087befSAndrew Turner Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
20*f3087befSAndrew Turner z=1/x and shift = pi/2. Maximum observed error is 2.27 ulps:
21*f3087befSAndrew Turner atan(0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1
22*f3087befSAndrew Turner want 0x1.9225645bdd7c3p-1. */
23*f3087befSAndrew Turner double
atan(double x)24*f3087befSAndrew Turner atan (double x)
25*f3087befSAndrew Turner {
26*f3087befSAndrew Turner uint64_t ix = asuint64 (x);
27*f3087befSAndrew Turner uint64_t sign = ix & ~AbsMask;
28*f3087befSAndrew Turner uint64_t ia = ix & AbsMask;
29*f3087befSAndrew Turner uint32_t ia12 = ia >> 52;
30*f3087befSAndrew Turner
31*f3087befSAndrew Turner if (unlikely (ia12 >= BigBound || ia12 < TinyBound))
32*f3087befSAndrew Turner {
33*f3087befSAndrew Turner if (ia12 < TinyBound)
34*f3087befSAndrew Turner /* Avoid underflow by returning x. */
35*f3087befSAndrew Turner return x;
36*f3087befSAndrew Turner if (ia > 0x7ff0000000000000)
37*f3087befSAndrew Turner /* Propagate NaN. */
38*f3087befSAndrew Turner return __math_invalid (x);
39*f3087befSAndrew Turner /* atan(x) rounds to PiOver2 for large x. */
40*f3087befSAndrew Turner return asdouble (asuint64 (PiOver2) ^ sign);
41*f3087befSAndrew Turner }
42*f3087befSAndrew Turner
43*f3087befSAndrew Turner double z, az, shift;
44*f3087befSAndrew Turner if (ia12 >= OneTop)
45*f3087befSAndrew Turner {
46*f3087befSAndrew Turner /* For x > 1, use atan(x) = pi / 2 + atan(-1 / x). */
47*f3087befSAndrew Turner z = -1.0 / x;
48*f3087befSAndrew Turner shift = PiOver2;
49*f3087befSAndrew Turner /* Use absolute value only when needed (odd powers of z). */
50*f3087befSAndrew Turner az = -fabs (z);
51*f3087befSAndrew Turner }
52*f3087befSAndrew Turner else
53*f3087befSAndrew Turner {
54*f3087befSAndrew Turner /* For x < 1, approximate atan(x) directly. */
55*f3087befSAndrew Turner z = x;
56*f3087befSAndrew Turner shift = 0;
57*f3087befSAndrew Turner az = asdouble (ia);
58*f3087befSAndrew Turner }
59*f3087befSAndrew Turner
60*f3087befSAndrew Turner /* Calculate polynomial, shift + z + z^3 * P(z^2). */
61*f3087befSAndrew Turner double y = eval_poly (z, az, shift);
62*f3087befSAndrew Turner /* Copy sign. */
63*f3087befSAndrew Turner return asdouble (asuint64 (y) ^ sign);
64*f3087befSAndrew Turner }
65*f3087befSAndrew Turner
66*f3087befSAndrew Turner TEST_SIG (S, D, 1, atan, -10.0, 10.0)
67*f3087befSAndrew Turner TEST_ULP (atan, 1.78)
68*f3087befSAndrew Turner TEST_INTERVAL (atan, 0, 0x1p-30, 10000)
69*f3087befSAndrew Turner TEST_INTERVAL (atan, -0, -0x1p-30, 1000)
70*f3087befSAndrew Turner TEST_INTERVAL (atan, 0x1p-30, 0x1p53, 900000)
71*f3087befSAndrew Turner TEST_INTERVAL (atan, -0x1p-30, -0x1p53, 90000)
72*f3087befSAndrew Turner TEST_INTERVAL (atan, 0x1p53, inf, 10000)
73*f3087befSAndrew Turner TEST_INTERVAL (atan, -0x1p53, -inf, 1000)
74