1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector atan(x) function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2021-2023, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include "sv_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li #include "poly_sve_f64.h"
12*412f47f9SXin Li
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li float64_t poly[20];
16*412f47f9SXin Li float64_t pi_over_2;
17*412f47f9SXin Li } data = {
18*412f47f9SXin Li /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19*412f47f9SXin Li [2**-1022, 1.0]. */
20*412f47f9SXin Li .poly = { -0x1.5555555555555p-2, 0x1.99999999996c1p-3, -0x1.2492492478f88p-3,
21*412f47f9SXin Li 0x1.c71c71bc3951cp-4, -0x1.745d160a7e368p-4, 0x1.3b139b6a88ba1p-4,
22*412f47f9SXin Li -0x1.11100ee084227p-4, 0x1.e1d0f9696f63bp-5, -0x1.aebfe7b418581p-5,
23*412f47f9SXin Li 0x1.842dbe9b0d916p-5, -0x1.5d30140ae5e99p-5, 0x1.338e31eb2fbbcp-5,
24*412f47f9SXin Li -0x1.00e6eece7de8p-5, 0x1.860897b29e5efp-6, -0x1.0051381722a59p-6,
25*412f47f9SXin Li 0x1.14e9dc19a4a4ep-7, -0x1.d0062b42fe3bfp-9, 0x1.17739e210171ap-10,
26*412f47f9SXin Li -0x1.ab24da7be7402p-13, 0x1.358851160a528p-16, },
27*412f47f9SXin Li .pi_over_2 = 0x1.921fb54442d18p+0,
28*412f47f9SXin Li };
29*412f47f9SXin Li
30*412f47f9SXin Li /* Useful constants. */
31*412f47f9SXin Li #define SignMask (0x8000000000000000)
32*412f47f9SXin Li
33*412f47f9SXin Li /* Fast implementation of SVE atan.
34*412f47f9SXin Li Based on atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
35*412f47f9SXin Li z=1/x and shift = pi/2. Largest errors are close to 1. The maximum observed
36*412f47f9SXin Li error is 2.27 ulps:
37*412f47f9SXin Li _ZGVsMxv_atan (0x1.0005af27c23e9p+0) got 0x1.9225645bdd7c1p-1
38*412f47f9SXin Li want 0x1.9225645bdd7c3p-1. */
SV_NAME_D1(atan)39*412f47f9SXin Li svfloat64_t SV_NAME_D1 (atan) (svfloat64_t x, const svbool_t pg)
40*412f47f9SXin Li {
41*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
42*412f47f9SXin Li
43*412f47f9SXin Li /* No need to trigger special case. Small cases, infs and nans
44*412f47f9SXin Li are supported by our approximation technique. */
45*412f47f9SXin Li svuint64_t ix = svreinterpret_u64 (x);
46*412f47f9SXin Li svuint64_t sign = svand_x (pg, ix, SignMask);
47*412f47f9SXin Li
48*412f47f9SXin Li /* Argument reduction:
49*412f47f9SXin Li y := arctan(x) for x < 1
50*412f47f9SXin Li y := pi/2 + arctan(-1/x) for x > 1
51*412f47f9SXin Li Hence, use z=-1/a if x>=1, otherwise z=a. */
52*412f47f9SXin Li svbool_t red = svacgt (pg, x, 1.0);
53*412f47f9SXin Li /* Avoid dependency in abs(x) in division (and comparison). */
54*412f47f9SXin Li svfloat64_t z = svsel (red, svdivr_x (pg, x, 1.0), x);
55*412f47f9SXin Li /* Use absolute value only when needed (odd powers of z). */
56*412f47f9SXin Li svfloat64_t az = svabs_x (pg, z);
57*412f47f9SXin Li az = svneg_m (az, red, az);
58*412f47f9SXin Li
59*412f47f9SXin Li /* Use split Estrin scheme for P(z^2) with deg(P)=19. */
60*412f47f9SXin Li svfloat64_t z2 = svmul_x (pg, z, z);
61*412f47f9SXin Li svfloat64_t x2 = svmul_x (pg, z2, z2);
62*412f47f9SXin Li svfloat64_t x4 = svmul_x (pg, x2, x2);
63*412f47f9SXin Li svfloat64_t x8 = svmul_x (pg, x4, x4);
64*412f47f9SXin Li
65*412f47f9SXin Li svfloat64_t y
66*412f47f9SXin Li = svmla_x (pg, sv_estrin_7_f64_x (pg, z2, x2, x4, d->poly),
67*412f47f9SXin Li sv_estrin_11_f64_x (pg, z2, x2, x4, x8, d->poly + 8), x8);
68*412f47f9SXin Li
69*412f47f9SXin Li /* y = shift + z + z^3 * P(z^2). */
70*412f47f9SXin Li svfloat64_t z3 = svmul_x (pg, z2, az);
71*412f47f9SXin Li y = svmla_x (pg, az, z3, y);
72*412f47f9SXin Li
73*412f47f9SXin Li /* Apply shift as indicated by `red` predicate. */
74*412f47f9SXin Li y = svadd_m (red, y, d->pi_over_2);
75*412f47f9SXin Li
76*412f47f9SXin Li /* y = atan(x) if x>0, -atan(-x) otherwise. */
77*412f47f9SXin Li y = svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (y), sign));
78*412f47f9SXin Li
79*412f47f9SXin Li return y;
80*412f47f9SXin Li }
81*412f47f9SXin Li
82*412f47f9SXin Li PL_SIG (SV, D, 1, atan, -3.1, 3.1)
83*412f47f9SXin Li PL_TEST_ULP (SV_NAME_D1 (atan), 1.78)
84*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_D1 (atan), 0.0, 1.0, 40000)
85*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_D1 (atan), 1.0, 100.0, 40000)
86*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_D1 (atan), 100, inf, 40000)
87*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_D1 (atan), -0, -inf, 40000)
88