xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/atan2.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision vector atan2(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_f64.h"
12*f3087befSAndrew Turner 
13*f3087befSAndrew Turner static const struct data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner   float64_t poly[20];
16*f3087befSAndrew Turner   float64_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**-1022, 1.0].  */
20*f3087befSAndrew Turner   .poly = { -0x1.5555555555555p-2,  0x1.99999999996c1p-3, -0x1.2492492478f88p-3,
21*f3087befSAndrew Turner             0x1.c71c71bc3951cp-4,   -0x1.745d160a7e368p-4, 0x1.3b139b6a88ba1p-4,
22*f3087befSAndrew Turner             -0x1.11100ee084227p-4,  0x1.e1d0f9696f63bp-5, -0x1.aebfe7b418581p-5,
23*f3087befSAndrew Turner             0x1.842dbe9b0d916p-5,   -0x1.5d30140ae5e99p-5, 0x1.338e31eb2fbbcp-5,
24*f3087befSAndrew Turner             -0x1.00e6eece7de8p-5,   0x1.860897b29e5efp-6, -0x1.0051381722a59p-6,
25*f3087befSAndrew Turner             0x1.14e9dc19a4a4ep-7,  -0x1.d0062b42fe3bfp-9, 0x1.17739e210171ap-10,
26*f3087befSAndrew Turner             -0x1.ab24da7be7402p-13, 0x1.358851160a528p-16, },
27*f3087befSAndrew Turner   .pi_over_2 = 0x1.921fb54442d18p+0,
28*f3087befSAndrew Turner };
29*f3087befSAndrew Turner 
30*f3087befSAndrew Turner /* Special cases i.e. 0, infinity, nan (fall back to scalar calls).  */
31*f3087befSAndrew Turner static svfloat64_t NOINLINE
special_case(svfloat64_t y,svfloat64_t x,svfloat64_t ret,const svbool_t cmp)32*f3087befSAndrew Turner special_case (svfloat64_t y, svfloat64_t x, svfloat64_t ret,
33*f3087befSAndrew Turner 	      const svbool_t cmp)
34*f3087befSAndrew Turner {
35*f3087befSAndrew Turner   return sv_call2_f64 (atan2, y, x, ret, cmp);
36*f3087befSAndrew Turner }
37*f3087befSAndrew Turner 
38*f3087befSAndrew Turner /* Returns a predicate indicating true if the input is the bit representation
39*f3087befSAndrew Turner    of 0, infinity or nan.  */
40*f3087befSAndrew Turner static inline svbool_t
zeroinfnan(svuint64_t i,const svbool_t pg)41*f3087befSAndrew Turner zeroinfnan (svuint64_t i, const svbool_t pg)
42*f3087befSAndrew Turner {
43*f3087befSAndrew Turner   return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1),
44*f3087befSAndrew Turner 		  sv_u64 (2 * asuint64 (INFINITY) - 1));
45*f3087befSAndrew Turner }
46*f3087befSAndrew Turner 
47*f3087befSAndrew Turner /* Fast implementation of SVE atan2. Errors are greatest when y and
48*f3087befSAndrew Turner    x are reasonably close together. The greatest observed error is 2.28 ULP:
49*f3087befSAndrew Turner    _ZGVsMxvv_atan2 (-0x1.5915b1498e82fp+732, 0x1.54d11ef838826p+732)
50*f3087befSAndrew Turner    got -0x1.954f42f1fa841p-1 want -0x1.954f42f1fa843p-1.  */
SV_NAME_D2(atan2)51*f3087befSAndrew Turner svfloat64_t SV_NAME_D2 (atan2) (svfloat64_t y, svfloat64_t x,
52*f3087befSAndrew Turner 				const svbool_t pg)
53*f3087befSAndrew Turner {
54*f3087befSAndrew Turner   const struct data *data_ptr = ptr_barrier (&data);
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner   svuint64_t ix = svreinterpret_u64 (x);
57*f3087befSAndrew Turner   svuint64_t iy = svreinterpret_u64 (y);
58*f3087befSAndrew Turner 
59*f3087befSAndrew Turner   svbool_t cmp_x = zeroinfnan (ix, pg);
60*f3087befSAndrew Turner   svbool_t cmp_y = zeroinfnan (iy, pg);
61*f3087befSAndrew Turner   svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y);
62*f3087befSAndrew Turner 
63*f3087befSAndrew Turner   svfloat64_t ax = svabs_x (pg, x);
64*f3087befSAndrew Turner   svfloat64_t ay = svabs_x (pg, y);
65*f3087befSAndrew Turner   svuint64_t iax = svreinterpret_u64 (ax);
66*f3087befSAndrew Turner   svuint64_t iay = svreinterpret_u64 (ay);
67*f3087befSAndrew Turner 
68*f3087befSAndrew Turner   svuint64_t sign_x = sveor_x (pg, ix, iax);
69*f3087befSAndrew Turner   svuint64_t sign_y = sveor_x (pg, iy, iay);
70*f3087befSAndrew Turner   svuint64_t sign_xy = sveor_x (pg, sign_x, sign_y);
71*f3087befSAndrew Turner 
72*f3087befSAndrew Turner   svbool_t pred_aygtax = svcmpgt (pg, ay, ax);
73*f3087befSAndrew Turner 
74*f3087befSAndrew Turner   /* Set up z for call to atan.  */
75*f3087befSAndrew Turner   svfloat64_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay);
76*f3087befSAndrew Turner   svfloat64_t d = svsel (pred_aygtax, ay, ax);
77*f3087befSAndrew Turner   svfloat64_t z = svdiv_x (pg, n, d);
78*f3087befSAndrew Turner 
79*f3087befSAndrew Turner   /* Work out the correct shift.  */
80*f3087befSAndrew Turner   svfloat64_t shift = svreinterpret_f64 (svlsr_x (pg, sign_x, 1));
81*f3087befSAndrew Turner   shift = svsel (pred_aygtax, sv_f64 (1.0), shift);
82*f3087befSAndrew Turner   shift = svreinterpret_f64 (svorr_x (pg, sign_x, svreinterpret_u64 (shift)));
83*f3087befSAndrew Turner   shift = svmul_x (pg, shift, data_ptr->pi_over_2);
84*f3087befSAndrew Turner 
85*f3087befSAndrew Turner   /* Use split Estrin scheme for P(z^2) with deg(P)=19.  */
86*f3087befSAndrew Turner   svfloat64_t z2 = svmul_x (pg, z, z);
87*f3087befSAndrew Turner   svfloat64_t x2 = svmul_x (pg, z2, z2);
88*f3087befSAndrew Turner   svfloat64_t x4 = svmul_x (pg, x2, x2);
89*f3087befSAndrew Turner   svfloat64_t x8 = svmul_x (pg, x4, x4);
90*f3087befSAndrew Turner 
91*f3087befSAndrew Turner   svfloat64_t ret = svmla_x (
92*f3087befSAndrew Turner       pg, sv_estrin_7_f64_x (pg, z2, x2, x4, data_ptr->poly),
93*f3087befSAndrew Turner       sv_estrin_11_f64_x (pg, z2, x2, x4, x8, data_ptr->poly + 8), x8);
94*f3087befSAndrew Turner 
95*f3087befSAndrew Turner   /* y = shift + z + z^3 * P(z^2).  */
96*f3087befSAndrew Turner   svfloat64_t z3 = svmul_x (pg, z2, z);
97*f3087befSAndrew Turner   ret = svmla_x (pg, z, z3, ret);
98*f3087befSAndrew Turner 
99*f3087befSAndrew Turner   ret = svadd_m (pg, ret, shift);
100*f3087befSAndrew Turner 
101*f3087befSAndrew Turner   /* Account for the sign of x and y.  */
102*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, cmp_xy)))
103*f3087befSAndrew Turner     return special_case (
104*f3087befSAndrew Turner 	y, x,
105*f3087befSAndrew Turner 	svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (ret), sign_xy)),
106*f3087befSAndrew Turner 	cmp_xy);
107*f3087befSAndrew Turner   return svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (ret), sign_xy));
108*f3087befSAndrew Turner }
109*f3087befSAndrew Turner 
110*f3087befSAndrew Turner /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h.  */
111*f3087befSAndrew Turner TEST_SIG (SV, D, 2, atan2)
112*f3087befSAndrew Turner TEST_ULP (SV_NAME_D2 (atan2), 1.78)
113*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D2 (atan2))
114*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D2 (atan2), 0.0, 1.0, 40000)
115*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D2 (atan2), 1.0, 100.0, 40000)
116*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D2 (atan2), 100, inf, 40000)
117*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D2 (atan2), -0, -inf, 40000)
118*f3087befSAndrew Turner CLOSE_SVE_ATTR
119