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