1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-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 "atanf_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.921fb6p+1f)
16*f3087befSAndrew Turner #define PiOver2 (0x1.921fb6p+0f)
17*f3087befSAndrew Turner #define PiOver4 (0x1.921fb6p-1f)
18*f3087befSAndrew Turner #define SignMask (0x80000000)
19*f3087befSAndrew Turner
20*f3087befSAndrew Turner /* We calculate atan2f by P(n/d), where n and d are similar to the input
21*f3087befSAndrew Turner arguments, and P is a polynomial. The polynomial may underflow.
22*f3087befSAndrew Turner POLY_UFLOW_BOUND is the lower bound of the difference in exponents of n and
23*f3087befSAndrew Turner d for which P underflows, and is used to special-case such inputs. */
24*f3087befSAndrew Turner #define POLY_UFLOW_BOUND 24
25*f3087befSAndrew Turner
26*f3087befSAndrew Turner static inline int32_t
biased_exponent(float f)27*f3087befSAndrew Turner biased_exponent (float f)
28*f3087befSAndrew Turner {
29*f3087befSAndrew Turner uint32_t fi = asuint (f);
30*f3087befSAndrew Turner int32_t ex = (int32_t) ((fi & 0x7f800000) >> 23);
31*f3087befSAndrew Turner if (unlikely (ex == 0))
32*f3087befSAndrew Turner {
33*f3087befSAndrew Turner /* Subnormal case - we still need to get the exponent right for subnormal
34*f3087befSAndrew Turner numbers as division may take us back inside the normal range. */
35*f3087befSAndrew Turner return ex - __builtin_clz (fi << 9);
36*f3087befSAndrew Turner }
37*f3087befSAndrew Turner return ex;
38*f3087befSAndrew Turner }
39*f3087befSAndrew Turner
40*f3087befSAndrew Turner /* Fast implementation of scalar atan2f. Largest observed error is
41*f3087befSAndrew Turner 2.88ulps in [99.0, 101.0] x [99.0, 101.0]:
42*f3087befSAndrew Turner atan2f(0x1.9332d8p+6, 0x1.8cb6c4p+6) got 0x1.964646p-1
43*f3087befSAndrew Turner want 0x1.964640p-1. */
44*f3087befSAndrew Turner float
atan2f(float y,float x)45*f3087befSAndrew Turner atan2f (float y, float x)
46*f3087befSAndrew Turner {
47*f3087befSAndrew Turner uint32_t ix = asuint (x);
48*f3087befSAndrew Turner uint32_t iy = asuint (y);
49*f3087befSAndrew Turner
50*f3087befSAndrew Turner uint32_t sign_x = ix & SignMask;
51*f3087befSAndrew Turner uint32_t sign_y = iy & SignMask;
52*f3087befSAndrew Turner
53*f3087befSAndrew Turner uint32_t iax = ix & ~SignMask;
54*f3087befSAndrew Turner uint32_t iay = iy & ~SignMask;
55*f3087befSAndrew Turner
56*f3087befSAndrew Turner /* x or y is NaN. */
57*f3087befSAndrew Turner if ((iax > 0x7f800000) || (iay > 0x7f800000))
58*f3087befSAndrew Turner return x + y;
59*f3087befSAndrew Turner
60*f3087befSAndrew Turner /* m = 2 * sign(x) + sign(y). */
61*f3087befSAndrew Turner uint32_t m = ((iy >> 31) & 1) | ((ix >> 30) & 2);
62*f3087befSAndrew Turner
63*f3087befSAndrew Turner /* The following follows glibc ieee754 implementation, except
64*f3087befSAndrew Turner that we do not use +-tiny shifts (non-nearest rounding mode). */
65*f3087befSAndrew Turner
66*f3087befSAndrew Turner int32_t exp_diff = biased_exponent (x) - biased_exponent (y);
67*f3087befSAndrew Turner
68*f3087befSAndrew Turner /* Special case for (x, y) either on or very close to the x axis. Either y =
69*f3087befSAndrew Turner 0, or y is tiny and x is huge (difference in exponents >=
70*f3087befSAndrew Turner POLY_UFLOW_BOUND). In the second case, we only want to use this special
71*f3087befSAndrew Turner case when x is negative (i.e. quadrants 2 or 3). */
72*f3087befSAndrew Turner if (unlikely (iay == 0 || (exp_diff >= POLY_UFLOW_BOUND && m >= 2)))
73*f3087befSAndrew Turner {
74*f3087befSAndrew Turner switch (m)
75*f3087befSAndrew Turner {
76*f3087befSAndrew Turner case 0:
77*f3087befSAndrew Turner case 1:
78*f3087befSAndrew Turner return y; /* atan(+-0,+anything)=+-0. */
79*f3087befSAndrew Turner case 2:
80*f3087befSAndrew Turner return Pi; /* atan(+0,-anything) = pi. */
81*f3087befSAndrew Turner case 3:
82*f3087befSAndrew Turner return -Pi; /* atan(-0,-anything) =-pi. */
83*f3087befSAndrew Turner }
84*f3087befSAndrew Turner }
85*f3087befSAndrew Turner /* Special case for (x, y) either on or very close to the y axis. Either x =
86*f3087befSAndrew Turner 0, or x is tiny and y is huge (difference in exponents >=
87*f3087befSAndrew Turner POLY_UFLOW_BOUND). */
88*f3087befSAndrew Turner if (unlikely (iax == 0 || exp_diff <= -POLY_UFLOW_BOUND))
89*f3087befSAndrew Turner return sign_y ? -PiOver2 : PiOver2;
90*f3087befSAndrew Turner
91*f3087befSAndrew Turner /* x is INF. */
92*f3087befSAndrew Turner if (iax == 0x7f800000)
93*f3087befSAndrew Turner {
94*f3087befSAndrew Turner if (iay == 0x7f800000)
95*f3087befSAndrew Turner {
96*f3087befSAndrew Turner switch (m)
97*f3087befSAndrew Turner {
98*f3087befSAndrew Turner case 0:
99*f3087befSAndrew Turner return PiOver4; /* atan(+INF,+INF). */
100*f3087befSAndrew Turner case 1:
101*f3087befSAndrew Turner return -PiOver4; /* atan(-INF,+INF). */
102*f3087befSAndrew Turner case 2:
103*f3087befSAndrew Turner return 3.0f * PiOver4; /* atan(+INF,-INF). */
104*f3087befSAndrew Turner case 3:
105*f3087befSAndrew Turner return -3.0f * PiOver4; /* atan(-INF,-INF). */
106*f3087befSAndrew Turner }
107*f3087befSAndrew Turner }
108*f3087befSAndrew Turner else
109*f3087befSAndrew Turner {
110*f3087befSAndrew Turner switch (m)
111*f3087befSAndrew Turner {
112*f3087befSAndrew Turner case 0:
113*f3087befSAndrew Turner return 0.0f; /* atan(+...,+INF). */
114*f3087befSAndrew Turner case 1:
115*f3087befSAndrew Turner return -0.0f; /* atan(-...,+INF). */
116*f3087befSAndrew Turner case 2:
117*f3087befSAndrew Turner return Pi; /* atan(+...,-INF). */
118*f3087befSAndrew Turner case 3:
119*f3087befSAndrew Turner return -Pi; /* atan(-...,-INF). */
120*f3087befSAndrew Turner }
121*f3087befSAndrew Turner }
122*f3087befSAndrew Turner }
123*f3087befSAndrew Turner /* y is INF. */
124*f3087befSAndrew Turner if (iay == 0x7f800000)
125*f3087befSAndrew Turner return sign_y ? -PiOver2 : PiOver2;
126*f3087befSAndrew Turner
127*f3087befSAndrew Turner uint32_t sign_xy = sign_x ^ sign_y;
128*f3087befSAndrew Turner
129*f3087befSAndrew Turner float ax = asfloat (iax);
130*f3087befSAndrew Turner float ay = asfloat (iay);
131*f3087befSAndrew Turner
132*f3087befSAndrew Turner bool pred_aygtax = (ay > ax);
133*f3087befSAndrew Turner
134*f3087befSAndrew Turner /* Set up z for call to atanf. */
135*f3087befSAndrew Turner float n = pred_aygtax ? -ax : ay;
136*f3087befSAndrew Turner float d = pred_aygtax ? ay : ax;
137*f3087befSAndrew Turner float z = n / d;
138*f3087befSAndrew Turner
139*f3087befSAndrew Turner float ret;
140*f3087befSAndrew Turner if (unlikely (m < 2 && exp_diff >= POLY_UFLOW_BOUND))
141*f3087befSAndrew Turner {
142*f3087befSAndrew Turner /* If (x, y) is very close to x axis and x is positive, the polynomial
143*f3087befSAndrew Turner will underflow and evaluate to z. */
144*f3087befSAndrew Turner ret = z;
145*f3087befSAndrew Turner }
146*f3087befSAndrew Turner else
147*f3087befSAndrew Turner {
148*f3087befSAndrew Turner /* Work out the correct shift. */
149*f3087befSAndrew Turner float shift = sign_x ? -2.0f : 0.0f;
150*f3087befSAndrew Turner shift = pred_aygtax ? shift + 1.0f : shift;
151*f3087befSAndrew Turner shift *= PiOver2;
152*f3087befSAndrew Turner
153*f3087befSAndrew Turner ret = eval_poly (z, z, shift);
154*f3087befSAndrew Turner }
155*f3087befSAndrew Turner
156*f3087befSAndrew Turner /* Account for the sign of x and y. */
157*f3087befSAndrew Turner return asfloat (asuint (ret) ^ sign_xy);
158*f3087befSAndrew Turner }
159*f3087befSAndrew Turner
160*f3087befSAndrew Turner /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */
161*f3087befSAndrew Turner TEST_SIG (S, F, 2, atan2)
162*f3087befSAndrew Turner TEST_ULP (atan2f, 2.4)
163*f3087befSAndrew Turner TEST_INTERVAL (atan2f, -10.0, 10.0, 50000)
164*f3087befSAndrew Turner TEST_INTERVAL (atan2f, -1.0, 1.0, 40000)
165*f3087befSAndrew Turner TEST_INTERVAL (atan2f, 0.0, 1.0, 40000)
166*f3087befSAndrew Turner TEST_INTERVAL (atan2f, 1.0, 100.0, 40000)
167*f3087befSAndrew Turner TEST_INTERVAL (atan2f, 1e6, 1e32, 40000)
168