1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-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_f32.h"
12*412f47f9SXin Li
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li float32_t poly[8];
16*412f47f9SXin Li float32_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**-128, 1.0]. */
20*412f47f9SXin Li .poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f,
21*412f47f9SXin Li -0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f },
22*412f47f9SXin Li .pi_over_2 = 0x1.921fb6p+0f,
23*412f47f9SXin Li };
24*412f47f9SXin Li
25*412f47f9SXin Li #define SignMask (0x80000000)
26*412f47f9SXin Li
27*412f47f9SXin Li /* Fast implementation of SVE atanf based on
28*412f47f9SXin Li atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
29*412f47f9SXin Li z=-1/x and shift = pi/2.
30*412f47f9SXin Li Largest observed error is 2.9 ULP, close to +/-1.0:
31*412f47f9SXin Li _ZGVsMxv_atanf (0x1.0468f6p+0) got -0x1.967f06p-1
32*412f47f9SXin Li want -0x1.967fp-1. */
SV_NAME_F1(atan)33*412f47f9SXin Li svfloat32_t SV_NAME_F1 (atan) (svfloat32_t x, const svbool_t pg)
34*412f47f9SXin Li {
35*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
36*412f47f9SXin Li
37*412f47f9SXin Li /* No need to trigger special case. Small cases, infs and nans
38*412f47f9SXin Li are supported by our approximation technique. */
39*412f47f9SXin Li svuint32_t ix = svreinterpret_u32 (x);
40*412f47f9SXin Li svuint32_t sign = svand_x (pg, ix, SignMask);
41*412f47f9SXin Li
42*412f47f9SXin Li /* Argument reduction:
43*412f47f9SXin Li y := arctan(x) for x < 1
44*412f47f9SXin Li y := pi/2 + arctan(-1/x) for x > 1
45*412f47f9SXin Li Hence, use z=-1/a if x>=1, otherwise z=a. */
46*412f47f9SXin Li svbool_t red = svacgt (pg, x, 1.0f);
47*412f47f9SXin Li /* Avoid dependency in abs(x) in division (and comparison). */
48*412f47f9SXin Li svfloat32_t z = svsel (red, svdiv_x (pg, sv_f32 (1.0f), x), x);
49*412f47f9SXin Li /* Use absolute value only when needed (odd powers of z). */
50*412f47f9SXin Li svfloat32_t az = svabs_x (pg, z);
51*412f47f9SXin Li az = svneg_m (az, red, az);
52*412f47f9SXin Li
53*412f47f9SXin Li /* Use split Estrin scheme for P(z^2) with deg(P)=7. */
54*412f47f9SXin Li svfloat32_t z2 = svmul_x (pg, z, z);
55*412f47f9SXin Li svfloat32_t z4 = svmul_x (pg, z2, z2);
56*412f47f9SXin Li svfloat32_t z8 = svmul_x (pg, z4, z4);
57*412f47f9SXin Li
58*412f47f9SXin Li svfloat32_t y = sv_estrin_7_f32_x (pg, z2, z4, z8, d->poly);
59*412f47f9SXin Li
60*412f47f9SXin Li /* y = shift + z + z^3 * P(z^2). */
61*412f47f9SXin Li svfloat32_t z3 = svmul_x (pg, z2, az);
62*412f47f9SXin Li y = svmla_x (pg, az, z3, y);
63*412f47f9SXin Li
64*412f47f9SXin Li /* Apply shift as indicated by 'red' predicate. */
65*412f47f9SXin Li y = svadd_m (red, y, sv_f32 (d->pi_over_2));
66*412f47f9SXin Li
67*412f47f9SXin Li /* y = atan(x) if x>0, -atan(-x) otherwise. */
68*412f47f9SXin Li return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (y), sign));
69*412f47f9SXin Li }
70*412f47f9SXin Li
71*412f47f9SXin Li PL_SIG (SV, F, 1, atan, -3.1, 3.1)
72*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (atan), 2.9)
73*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (atan), 0.0, 1.0, 40000)
74*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (atan), 1.0, 100.0, 40000)
75*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (atan), 100, inf, 40000)
76*412f47f9SXin Li PL_TEST_INTERVAL (SV_NAME_F1 (atan), -0, -inf, 40000)
77