1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-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 "atanf_common.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner
12*f3087befSAndrew Turner #define PiOver2 0x1.921fb6p+0f
13*f3087befSAndrew Turner #define AbsMask 0x7fffffff
14*f3087befSAndrew Turner #define TinyBound 0x30800000 /* asuint(0x1p-30). */
15*f3087befSAndrew Turner #define BigBound 0x4e800000 /* asuint(0x1p30). */
16*f3087befSAndrew Turner #define One 0x3f800000
17*f3087befSAndrew Turner
18*f3087befSAndrew Turner /* Approximation of single-precision atan(x) based on
19*f3087befSAndrew Turner atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1]
20*f3087befSAndrew Turner using z=-1/x and shift = pi/2.
21*f3087befSAndrew Turner Maximum error is 2.88 ulps:
22*f3087befSAndrew Turner atanf(0x1.0565ccp+0) got 0x1.97771p-1
23*f3087befSAndrew Turner want 0x1.97770ap-1. */
24*f3087befSAndrew Turner float
atanf(float x)25*f3087befSAndrew Turner atanf (float x)
26*f3087befSAndrew Turner {
27*f3087befSAndrew Turner uint32_t ix = asuint (x);
28*f3087befSAndrew Turner uint32_t sign = ix & ~AbsMask;
29*f3087befSAndrew Turner uint32_t ia = ix & AbsMask;
30*f3087befSAndrew Turner
31*f3087befSAndrew Turner if (unlikely (ia < TinyBound))
32*f3087befSAndrew Turner /* Avoid underflow by returning x. */
33*f3087befSAndrew Turner return x;
34*f3087befSAndrew Turner
35*f3087befSAndrew Turner if (unlikely (ia > BigBound))
36*f3087befSAndrew Turner {
37*f3087befSAndrew Turner if (ia > 0x7f800000)
38*f3087befSAndrew Turner /* Propagate NaN. */
39*f3087befSAndrew Turner return __math_invalidf (x);
40*f3087befSAndrew Turner /* atan(x) rounds to PiOver2 for large x. */
41*f3087befSAndrew Turner return asfloat (asuint (PiOver2) ^ sign);
42*f3087befSAndrew Turner }
43*f3087befSAndrew Turner
44*f3087befSAndrew Turner float z, az, shift;
45*f3087befSAndrew Turner if (ia > One)
46*f3087befSAndrew Turner {
47*f3087befSAndrew Turner /* For x > 1, use atan(x) = pi / 2 + atan(-1 / x). */
48*f3087befSAndrew Turner z = -1.0f / x;
49*f3087befSAndrew Turner shift = PiOver2;
50*f3087befSAndrew Turner /* Use absolute value only when needed (odd powers of z). */
51*f3087befSAndrew Turner az = -fabsf (z);
52*f3087befSAndrew Turner }
53*f3087befSAndrew Turner else
54*f3087befSAndrew Turner {
55*f3087befSAndrew Turner /* For x < 1, approximate atan(x) directly. */
56*f3087befSAndrew Turner z = x;
57*f3087befSAndrew Turner az = asfloat (ia);
58*f3087befSAndrew Turner shift = 0;
59*f3087befSAndrew Turner }
60*f3087befSAndrew Turner
61*f3087befSAndrew Turner /* Calculate polynomial, shift + z + z^3 * P(z^2). */
62*f3087befSAndrew Turner float y = eval_poly (z, az, shift);
63*f3087befSAndrew Turner /* Copy sign. */
64*f3087befSAndrew Turner return asfloat (asuint (y) ^ sign);
65*f3087befSAndrew Turner }
66*f3087befSAndrew Turner
67*f3087befSAndrew Turner TEST_SIG (S, F, 1, atan, -10.0, 10.0)
68*f3087befSAndrew Turner TEST_ULP (atanf, 2.38)
69*f3087befSAndrew Turner TEST_SYM_INTERVAL (atanf, 0, 0x1p-30, 5000)
70*f3087befSAndrew Turner TEST_SYM_INTERVAL (atanf, 0x1p-30, 1, 40000)
71*f3087befSAndrew Turner TEST_SYM_INTERVAL (atanf, 1, 0x1p30, 40000)
72*f3087befSAndrew Turner TEST_SYM_INTERVAL (atanf, 0x1p30, inf, 1000)
73