1072a4ba8SAndrew Turner /*
2072a4ba8SAndrew Turner * Double-precision vector atan(x) function.
3072a4ba8SAndrew Turner *
4072a4ba8SAndrew Turner * Copyright (c) 2021-2023, Arm Limited.
5072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6072a4ba8SAndrew Turner */
7072a4ba8SAndrew Turner
8072a4ba8SAndrew Turner #include "v_math.h"
9072a4ba8SAndrew Turner #include "pl_sig.h"
10072a4ba8SAndrew Turner #include "pl_test.h"
11*5a02ffc3SAndrew Turner #include "poly_advsimd_f64.h"
12072a4ba8SAndrew Turner
13*5a02ffc3SAndrew Turner static const struct data
14*5a02ffc3SAndrew Turner {
15*5a02ffc3SAndrew Turner float64x2_t pi_over_2;
16*5a02ffc3SAndrew Turner float64x2_t poly[20];
17*5a02ffc3SAndrew Turner } data = {
18*5a02ffc3SAndrew Turner /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19*5a02ffc3SAndrew Turner [2**-1022, 1.0]. */
20*5a02ffc3SAndrew Turner .poly = { V2 (-0x1.5555555555555p-2), V2 (0x1.99999999996c1p-3),
21*5a02ffc3SAndrew Turner V2 (-0x1.2492492478f88p-3), V2 (0x1.c71c71bc3951cp-4),
22*5a02ffc3SAndrew Turner V2 (-0x1.745d160a7e368p-4), V2 (0x1.3b139b6a88ba1p-4),
23*5a02ffc3SAndrew Turner V2 (-0x1.11100ee084227p-4), V2 (0x1.e1d0f9696f63bp-5),
24*5a02ffc3SAndrew Turner V2 (-0x1.aebfe7b418581p-5), V2 (0x1.842dbe9b0d916p-5),
25*5a02ffc3SAndrew Turner V2 (-0x1.5d30140ae5e99p-5), V2 (0x1.338e31eb2fbbcp-5),
26*5a02ffc3SAndrew Turner V2 (-0x1.00e6eece7de8p-5), V2 (0x1.860897b29e5efp-6),
27*5a02ffc3SAndrew Turner V2 (-0x1.0051381722a59p-6), V2 (0x1.14e9dc19a4a4ep-7),
28*5a02ffc3SAndrew Turner V2 (-0x1.d0062b42fe3bfp-9), V2 (0x1.17739e210171ap-10),
29*5a02ffc3SAndrew Turner V2 (-0x1.ab24da7be7402p-13), V2 (0x1.358851160a528p-16), },
30*5a02ffc3SAndrew Turner .pi_over_2 = V2 (0x1.921fb54442d18p+0),
31*5a02ffc3SAndrew Turner };
32072a4ba8SAndrew Turner
33*5a02ffc3SAndrew Turner #define SignMask v_u64 (0x8000000000000000)
34*5a02ffc3SAndrew Turner #define TinyBound 0x3e10000000000000 /* asuint64(0x1p-30). */
35*5a02ffc3SAndrew Turner #define BigBound 0x4340000000000000 /* asuint64(0x1p53). */
36072a4ba8SAndrew Turner
37072a4ba8SAndrew Turner /* Fast implementation of vector atan.
38072a4ba8SAndrew Turner Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
39072a4ba8SAndrew Turner z=1/x and shift = pi/2. Maximum observed error is 2.27 ulps:
40*5a02ffc3SAndrew Turner _ZGVnN2v_atan (0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1
41072a4ba8SAndrew Turner want 0x1.9225645bdd7c3p-1. */
V_NAME_D1(atan)42*5a02ffc3SAndrew Turner float64x2_t VPCS_ATTR V_NAME_D1 (atan) (float64x2_t x)
43072a4ba8SAndrew Turner {
44*5a02ffc3SAndrew Turner const struct data *d = ptr_barrier (&data);
45*5a02ffc3SAndrew Turner
46072a4ba8SAndrew Turner /* Small cases, infs and nans are supported by our approximation technique,
47072a4ba8SAndrew Turner but do not set fenv flags correctly. Only trigger special case if we need
48072a4ba8SAndrew Turner fenv. */
49*5a02ffc3SAndrew Turner uint64x2_t ix = vreinterpretq_u64_f64 (x);
50*5a02ffc3SAndrew Turner uint64x2_t sign = vandq_u64 (ix, SignMask);
51072a4ba8SAndrew Turner
52072a4ba8SAndrew Turner #if WANT_SIMD_EXCEPT
53*5a02ffc3SAndrew Turner uint64x2_t ia12 = vandq_u64 (ix, v_u64 (0x7ff0000000000000));
54*5a02ffc3SAndrew Turner uint64x2_t special = vcgtq_u64 (vsubq_u64 (ia12, v_u64 (TinyBound)),
55*5a02ffc3SAndrew Turner v_u64 (BigBound - TinyBound));
56072a4ba8SAndrew Turner /* If any lane is special, fall back to the scalar routine for all lanes. */
57072a4ba8SAndrew Turner if (unlikely (v_any_u64 (special)))
58072a4ba8SAndrew Turner return v_call_f64 (atan, x, v_f64 (0), v_u64 (-1));
59072a4ba8SAndrew Turner #endif
60072a4ba8SAndrew Turner
61072a4ba8SAndrew Turner /* Argument reduction:
62072a4ba8SAndrew Turner y := arctan(x) for x < 1
63072a4ba8SAndrew Turner y := pi/2 + arctan(-1/x) for x > 1
64072a4ba8SAndrew Turner Hence, use z=-1/a if x>=1, otherwise z=a. */
65*5a02ffc3SAndrew Turner uint64x2_t red = vcagtq_f64 (x, v_f64 (1.0));
66072a4ba8SAndrew Turner /* Avoid dependency in abs(x) in division (and comparison). */
67*5a02ffc3SAndrew Turner float64x2_t z = vbslq_f64 (red, vdivq_f64 (v_f64 (1.0), x), x);
68*5a02ffc3SAndrew Turner float64x2_t shift = vreinterpretq_f64_u64 (
69*5a02ffc3SAndrew Turner vandq_u64 (red, vreinterpretq_u64_f64 (d->pi_over_2)));
70072a4ba8SAndrew Turner /* Use absolute value only when needed (odd powers of z). */
71*5a02ffc3SAndrew Turner float64x2_t az = vbslq_f64 (
72*5a02ffc3SAndrew Turner SignMask, vreinterpretq_f64_u64 (vandq_u64 (SignMask, red)), z);
73072a4ba8SAndrew Turner
74*5a02ffc3SAndrew Turner /* Calculate the polynomial approximation.
75*5a02ffc3SAndrew Turner Use split Estrin scheme for P(z^2) with deg(P)=19. Use split instead of
76*5a02ffc3SAndrew Turner full scheme to avoid underflow in x^16.
77*5a02ffc3SAndrew Turner The order 19 polynomial P approximates
78*5a02ffc3SAndrew Turner (atan(sqrt(x))-sqrt(x))/x^(3/2). */
79*5a02ffc3SAndrew Turner float64x2_t z2 = vmulq_f64 (z, z);
80*5a02ffc3SAndrew Turner float64x2_t x2 = vmulq_f64 (z2, z2);
81*5a02ffc3SAndrew Turner float64x2_t x4 = vmulq_f64 (x2, x2);
82*5a02ffc3SAndrew Turner float64x2_t x8 = vmulq_f64 (x4, x4);
83*5a02ffc3SAndrew Turner float64x2_t y
84*5a02ffc3SAndrew Turner = vfmaq_f64 (v_estrin_7_f64 (z2, x2, x4, d->poly),
85*5a02ffc3SAndrew Turner v_estrin_11_f64 (z2, x2, x4, x8, d->poly + 8), x8);
86*5a02ffc3SAndrew Turner
87*5a02ffc3SAndrew Turner /* Finalize. y = shift + z + z^3 * P(z^2). */
88*5a02ffc3SAndrew Turner y = vfmaq_f64 (az, y, vmulq_f64 (z2, az));
89*5a02ffc3SAndrew Turner y = vaddq_f64 (y, shift);
90072a4ba8SAndrew Turner
91072a4ba8SAndrew Turner /* y = atan(x) if x>0, -atan(-x) otherwise. */
92*5a02ffc3SAndrew Turner y = vreinterpretq_f64_u64 (veorq_u64 (vreinterpretq_u64_f64 (y), sign));
93072a4ba8SAndrew Turner return y;
94072a4ba8SAndrew Turner }
95072a4ba8SAndrew Turner
96072a4ba8SAndrew Turner PL_SIG (V, D, 1, atan, -10.0, 10.0)
97*5a02ffc3SAndrew Turner PL_TEST_ULP (V_NAME_D1 (atan), 1.78)
98*5a02ffc3SAndrew Turner PL_TEST_EXPECT_FENV (V_NAME_D1 (atan), WANT_SIMD_EXCEPT)
99*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (V_NAME_D1 (atan), 0, 0x1p-30, 10000)
100*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (V_NAME_D1 (atan), -0, -0x1p-30, 1000)
101*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (V_NAME_D1 (atan), 0x1p-30, 0x1p53, 900000)
102*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (V_NAME_D1 (atan), -0x1p-30, -0x1p53, 90000)
103*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (V_NAME_D1 (atan), 0x1p53, inf, 10000)
104*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (V_NAME_D1 (atan), -0x1p53, -inf, 1000)
105