1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Double-precision scalar atan2(x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2021-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 <stdbool.h>
9*f3087befSAndrew Turner
10*f3087befSAndrew Turner #include "atan_common.h"
11*f3087befSAndrew Turner #include "math_config.h"
12*f3087befSAndrew Turner #include "test_sig.h"
13*f3087befSAndrew Turner #include "test_defs.h"
14*f3087befSAndrew Turner
15*f3087befSAndrew Turner #define Pi (0x1.921fb54442d18p+1)
16*f3087befSAndrew Turner #define PiOver2 (0x1.921fb54442d18p+0)
17*f3087befSAndrew Turner #define PiOver4 (0x1.921fb54442d18p-1)
18*f3087befSAndrew Turner #define SignMask (0x8000000000000000)
19*f3087befSAndrew Turner #define ExpMask (0x7ff0000000000000)
20*f3087befSAndrew Turner
21*f3087befSAndrew Turner /* We calculate atan2 by P(n/d), where n and d are similar to the input
22*f3087befSAndrew Turner arguments, and P is a polynomial. Evaluating P(x) requires calculating x^8,
23*f3087befSAndrew Turner which may underflow if n and d have very different magnitude.
24*f3087befSAndrew Turner POW8_EXP_UFLOW_BOUND is the lower bound of the difference in exponents of n
25*f3087befSAndrew Turner and d for which P underflows, and is used to special-case such inputs. */
26*f3087befSAndrew Turner #define POW8_EXP_UFLOW_BOUND 62
27*f3087befSAndrew Turner
28*f3087befSAndrew Turner static inline int64_t
biased_exponent(double f)29*f3087befSAndrew Turner biased_exponent (double f)
30*f3087befSAndrew Turner {
31*f3087befSAndrew Turner uint64_t fi = asuint64 (f);
32*f3087befSAndrew Turner return (fi & ExpMask) >> 52;
33*f3087befSAndrew Turner }
34*f3087befSAndrew Turner
35*f3087befSAndrew Turner /* Fast implementation of scalar atan2. Largest errors are when y and x are
36*f3087befSAndrew Turner close together. The greatest observed error is 2.28 ULP:
37*f3087befSAndrew Turner atan2(-0x1.5915b1498e82fp+732, 0x1.54d11ef838826p+732)
38*f3087befSAndrew Turner got -0x1.954f42f1fa841p-1 want -0x1.954f42f1fa843p-1. */
39*f3087befSAndrew Turner double
atan2(double y,double x)40*f3087befSAndrew Turner atan2 (double y, double x)
41*f3087befSAndrew Turner {
42*f3087befSAndrew Turner uint64_t ix = asuint64 (x);
43*f3087befSAndrew Turner uint64_t iy = asuint64 (y);
44*f3087befSAndrew Turner
45*f3087befSAndrew Turner uint64_t sign_x = ix & SignMask;
46*f3087befSAndrew Turner uint64_t sign_y = iy & SignMask;
47*f3087befSAndrew Turner
48*f3087befSAndrew Turner uint64_t iax = ix & ~SignMask;
49*f3087befSAndrew Turner uint64_t iay = iy & ~SignMask;
50*f3087befSAndrew Turner
51*f3087befSAndrew Turner bool xisnan = isnan (x);
52*f3087befSAndrew Turner if (unlikely (isnan (y) && !xisnan))
53*f3087befSAndrew Turner return __math_invalid (y);
54*f3087befSAndrew Turner if (unlikely (xisnan))
55*f3087befSAndrew Turner return __math_invalid (x);
56*f3087befSAndrew Turner
57*f3087befSAndrew Turner /* m = 2 * sign(x) + sign(y). */
58*f3087befSAndrew Turner uint32_t m = ((iy >> 63) & 1) | ((ix >> 62) & 2);
59*f3087befSAndrew Turner
60*f3087befSAndrew Turner int64_t exp_diff = biased_exponent (x) - biased_exponent (y);
61*f3087befSAndrew Turner
62*f3087befSAndrew Turner /* y = 0. */
63*f3087befSAndrew Turner if (iay == 0)
64*f3087befSAndrew Turner {
65*f3087befSAndrew Turner switch (m)
66*f3087befSAndrew Turner {
67*f3087befSAndrew Turner case 0:
68*f3087befSAndrew Turner case 1:
69*f3087befSAndrew Turner return y; /* atan(+-0,+anything)=+-0. */
70*f3087befSAndrew Turner case 2:
71*f3087befSAndrew Turner return Pi; /* atan(+0,-anything) = pi. */
72*f3087befSAndrew Turner case 3:
73*f3087befSAndrew Turner return -Pi; /* atan(-0,-anything) =-pi. */
74*f3087befSAndrew Turner }
75*f3087befSAndrew Turner }
76*f3087befSAndrew Turner /* Special case for (x, y) either on or very close to the y axis. Either x =
77*f3087befSAndrew Turner 0, or y is much larger than x (difference in exponents >=
78*f3087befSAndrew Turner POW8_EXP_UFLOW_BOUND). */
79*f3087befSAndrew Turner if (unlikely (iax == 0 || exp_diff <= -POW8_EXP_UFLOW_BOUND))
80*f3087befSAndrew Turner return sign_y ? -PiOver2 : PiOver2;
81*f3087befSAndrew Turner
82*f3087befSAndrew Turner /* Special case for either x is INF or (x, y) is very close to x axis and x
83*f3087befSAndrew Turner is negative. */
84*f3087befSAndrew Turner if (unlikely (iax == 0x7ff0000000000000
85*f3087befSAndrew Turner || (exp_diff >= POW8_EXP_UFLOW_BOUND && m >= 2)))
86*f3087befSAndrew Turner {
87*f3087befSAndrew Turner if (iay == 0x7ff0000000000000)
88*f3087befSAndrew Turner {
89*f3087befSAndrew Turner switch (m)
90*f3087befSAndrew Turner {
91*f3087befSAndrew Turner case 0:
92*f3087befSAndrew Turner return PiOver4; /* atan(+INF,+INF). */
93*f3087befSAndrew Turner case 1:
94*f3087befSAndrew Turner return -PiOver4; /* atan(-INF,+INF). */
95*f3087befSAndrew Turner case 2:
96*f3087befSAndrew Turner return 3.0 * PiOver4; /* atan(+INF,-INF). */
97*f3087befSAndrew Turner case 3:
98*f3087befSAndrew Turner return -3.0 * PiOver4; /* atan(-INF,-INF). */
99*f3087befSAndrew Turner }
100*f3087befSAndrew Turner }
101*f3087befSAndrew Turner else
102*f3087befSAndrew Turner {
103*f3087befSAndrew Turner switch (m)
104*f3087befSAndrew Turner {
105*f3087befSAndrew Turner case 0:
106*f3087befSAndrew Turner return 0.0; /* atan(+...,+INF). */
107*f3087befSAndrew Turner case 1:
108*f3087befSAndrew Turner return -0.0; /* atan(-...,+INF). */
109*f3087befSAndrew Turner case 2:
110*f3087befSAndrew Turner return Pi; /* atan(+...,-INF). */
111*f3087befSAndrew Turner case 3:
112*f3087befSAndrew Turner return -Pi; /* atan(-...,-INF). */
113*f3087befSAndrew Turner }
114*f3087befSAndrew Turner }
115*f3087befSAndrew Turner }
116*f3087befSAndrew Turner /* y is INF. */
117*f3087befSAndrew Turner if (iay == 0x7ff0000000000000)
118*f3087befSAndrew Turner return sign_y ? -PiOver2 : PiOver2;
119*f3087befSAndrew Turner
120*f3087befSAndrew Turner uint64_t sign_xy = sign_x ^ sign_y;
121*f3087befSAndrew Turner
122*f3087befSAndrew Turner double ax = asdouble (iax);
123*f3087befSAndrew Turner double ay = asdouble (iay);
124*f3087befSAndrew Turner uint64_t pred_aygtax = (ay > ax);
125*f3087befSAndrew Turner
126*f3087befSAndrew Turner /* Set up z for call to atan. */
127*f3087befSAndrew Turner double n = pred_aygtax ? -ax : ay;
128*f3087befSAndrew Turner double d = pred_aygtax ? ay : ax;
129*f3087befSAndrew Turner double z = n / d;
130*f3087befSAndrew Turner
131*f3087befSAndrew Turner double ret;
132*f3087befSAndrew Turner if (unlikely (m < 2 && exp_diff >= POW8_EXP_UFLOW_BOUND))
133*f3087befSAndrew Turner {
134*f3087befSAndrew Turner /* If (x, y) is very close to x axis and x is positive, the polynomial
135*f3087befSAndrew Turner will underflow and evaluate to z. */
136*f3087befSAndrew Turner ret = z;
137*f3087befSAndrew Turner }
138*f3087befSAndrew Turner else
139*f3087befSAndrew Turner {
140*f3087befSAndrew Turner /* Work out the correct shift. */
141*f3087befSAndrew Turner double shift = sign_x ? -2.0 : 0.0;
142*f3087befSAndrew Turner shift = pred_aygtax ? shift + 1.0 : shift;
143*f3087befSAndrew Turner shift *= PiOver2;
144*f3087befSAndrew Turner
145*f3087befSAndrew Turner ret = eval_poly (z, z, shift);
146*f3087befSAndrew Turner }
147*f3087befSAndrew Turner
148*f3087befSAndrew Turner /* Account for the sign of x and y. */
149*f3087befSAndrew Turner return asdouble (asuint64 (ret) ^ sign_xy);
150*f3087befSAndrew Turner }
151*f3087befSAndrew Turner
152*f3087befSAndrew Turner /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */
153*f3087befSAndrew Turner TEST_SIG (S, D, 2, atan2)
154*f3087befSAndrew Turner TEST_ULP (atan2, 1.78)
155*f3087befSAndrew Turner TEST_INTERVAL (atan2, -10.0, 10.0, 50000)
156*f3087befSAndrew Turner TEST_INTERVAL (atan2, -1.0, 1.0, 40000)
157*f3087befSAndrew Turner TEST_INTERVAL (atan2, 0.0, 1.0, 40000)
158*f3087befSAndrew Turner TEST_INTERVAL (atan2, 1.0, 100.0, 40000)
159*f3087befSAndrew Turner TEST_INTERVAL (atan2, 1e6, 1e32, 40000)
160