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