1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision vector atan2f(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 /* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */
26*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t y,svfloat32_t x,svfloat32_t ret,const svbool_t cmp)27*f3087befSAndrew Turner special_case (svfloat32_t y, svfloat32_t x, svfloat32_t ret,
28*f3087befSAndrew Turner const svbool_t cmp)
29*f3087befSAndrew Turner {
30*f3087befSAndrew Turner return sv_call2_f32 (atan2f, y, x, ret, cmp);
31*f3087befSAndrew Turner }
32*f3087befSAndrew Turner
33*f3087befSAndrew Turner /* Returns a predicate indicating true if the input is the bit representation
34*f3087befSAndrew Turner of 0, infinity or nan. */
35*f3087befSAndrew Turner static inline svbool_t
zeroinfnan(svuint32_t i,const svbool_t pg)36*f3087befSAndrew Turner zeroinfnan (svuint32_t i, const svbool_t pg)
37*f3087befSAndrew Turner {
38*f3087befSAndrew Turner return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1),
39*f3087befSAndrew Turner sv_u32 (2 * 0x7f800000lu - 1));
40*f3087befSAndrew Turner }
41*f3087befSAndrew Turner
42*f3087befSAndrew Turner /* Fast implementation of SVE atan2f based on atan(x) ~ shift + z + z^3 *
43*f3087befSAndrew Turner P(z^2) with reduction to [0,1] using z=1/x and shift = pi/2. Maximum
44*f3087befSAndrew Turner observed error is 2.95 ULP:
45*f3087befSAndrew Turner _ZGVsMxvv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1
46*f3087befSAndrew Turner want 0x1.967f00p-1. */
SV_NAME_F2(atan2)47*f3087befSAndrew Turner svfloat32_t SV_NAME_F2 (atan2) (svfloat32_t y, svfloat32_t x,
48*f3087befSAndrew Turner const svbool_t pg)
49*f3087befSAndrew Turner {
50*f3087befSAndrew Turner const struct data *data_ptr = ptr_barrier (&data);
51*f3087befSAndrew Turner
52*f3087befSAndrew Turner svuint32_t ix = svreinterpret_u32 (x);
53*f3087befSAndrew Turner svuint32_t iy = svreinterpret_u32 (y);
54*f3087befSAndrew Turner
55*f3087befSAndrew Turner svbool_t cmp_x = zeroinfnan (ix, pg);
56*f3087befSAndrew Turner svbool_t cmp_y = zeroinfnan (iy, pg);
57*f3087befSAndrew Turner svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y);
58*f3087befSAndrew Turner
59*f3087befSAndrew Turner svfloat32_t ax = svabs_x (pg, x);
60*f3087befSAndrew Turner svfloat32_t ay = svabs_x (pg, y);
61*f3087befSAndrew Turner svuint32_t iax = svreinterpret_u32 (ax);
62*f3087befSAndrew Turner svuint32_t iay = svreinterpret_u32 (ay);
63*f3087befSAndrew Turner
64*f3087befSAndrew Turner svuint32_t sign_x = sveor_x (pg, ix, iax);
65*f3087befSAndrew Turner svuint32_t sign_y = sveor_x (pg, iy, iay);
66*f3087befSAndrew Turner svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y);
67*f3087befSAndrew Turner
68*f3087befSAndrew Turner svbool_t pred_aygtax = svcmpgt (pg, ay, ax);
69*f3087befSAndrew Turner
70*f3087befSAndrew Turner /* Set up z for call to atan. */
71*f3087befSAndrew Turner svfloat32_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay);
72*f3087befSAndrew Turner svfloat32_t d = svsel (pred_aygtax, ay, ax);
73*f3087befSAndrew Turner svfloat32_t z = svdiv_x (pg, n, d);
74*f3087befSAndrew Turner
75*f3087befSAndrew Turner /* Work out the correct shift. */
76*f3087befSAndrew Turner svfloat32_t shift = svreinterpret_f32 (svlsr_x (pg, sign_x, 1));
77*f3087befSAndrew Turner shift = svsel (pred_aygtax, sv_f32 (1.0), shift);
78*f3087befSAndrew Turner shift = svreinterpret_f32 (svorr_x (pg, sign_x, svreinterpret_u32 (shift)));
79*f3087befSAndrew Turner shift = svmul_x (pg, shift, sv_f32 (data_ptr->pi_over_2));
80*f3087befSAndrew Turner
81*f3087befSAndrew Turner /* Use pure Estrin scheme for P(z^2) with deg(P)=7. */
82*f3087befSAndrew Turner svfloat32_t z2 = svmul_x (pg, z, z);
83*f3087befSAndrew Turner svfloat32_t z4 = svmul_x (pg, z2, z2);
84*f3087befSAndrew Turner svfloat32_t z8 = svmul_x (pg, z4, z4);
85*f3087befSAndrew Turner
86*f3087befSAndrew Turner svfloat32_t ret = sv_estrin_7_f32_x (pg, z2, z4, z8, data_ptr->poly);
87*f3087befSAndrew Turner
88*f3087befSAndrew Turner /* ret = shift + z + z^3 * P(z^2). */
89*f3087befSAndrew Turner svfloat32_t z3 = svmul_x (pg, z2, z);
90*f3087befSAndrew Turner ret = svmla_x (pg, z, z3, ret);
91*f3087befSAndrew Turner
92*f3087befSAndrew Turner ret = svadd_m (pg, ret, shift);
93*f3087befSAndrew Turner
94*f3087befSAndrew Turner /* Account for the sign of x and y. */
95*f3087befSAndrew Turner
96*f3087befSAndrew Turner if (unlikely (svptest_any (pg, cmp_xy)))
97*f3087befSAndrew Turner return special_case (
98*f3087befSAndrew Turner y, x,
99*f3087befSAndrew Turner svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)),
100*f3087befSAndrew Turner cmp_xy);
101*f3087befSAndrew Turner
102*f3087befSAndrew Turner return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy));
103*f3087befSAndrew Turner }
104*f3087befSAndrew Turner
105*f3087befSAndrew Turner /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */
106*f3087befSAndrew Turner TEST_SIG (SV, F, 2, atan2)
107*f3087befSAndrew Turner TEST_ULP (SV_NAME_F2 (atan2), 2.45)
108*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F2 (atan2))
109*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F2 (atan2), 0.0, 1.0, 40000)
110*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F2 (atan2), 1.0, 100.0, 40000)
111*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F2 (atan2), 100, inf, 40000)
112*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_F2 (atan2), -0, -inf, 40000)
113*f3087befSAndrew Turner CLOSE_SVE_ATTR
114