xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_atan2_3u.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
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 "v_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11 #include "poly_advsimd_f64.h"
12 
13 static const struct data
14 {
15   float64x2_t pi_over_2;
16   float64x2_t poly[20];
17 } data = {
18   /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19      the interval [2**-1022, 1.0].  */
20   .poly = { V2 (-0x1.5555555555555p-2),	 V2 (0x1.99999999996c1p-3),
21 	    V2 (-0x1.2492492478f88p-3),	 V2 (0x1.c71c71bc3951cp-4),
22 	    V2 (-0x1.745d160a7e368p-4),	 V2 (0x1.3b139b6a88ba1p-4),
23 	    V2 (-0x1.11100ee084227p-4),	 V2 (0x1.e1d0f9696f63bp-5),
24 	    V2 (-0x1.aebfe7b418581p-5),	 V2 (0x1.842dbe9b0d916p-5),
25 	    V2 (-0x1.5d30140ae5e99p-5),	 V2 (0x1.338e31eb2fbbcp-5),
26 	    V2 (-0x1.00e6eece7de8p-5),	 V2 (0x1.860897b29e5efp-6),
27 	    V2 (-0x1.0051381722a59p-6),	 V2 (0x1.14e9dc19a4a4ep-7),
28 	    V2 (-0x1.d0062b42fe3bfp-9),	 V2 (0x1.17739e210171ap-10),
29 	    V2 (-0x1.ab24da7be7402p-13), V2 (0x1.358851160a528p-16), },
30   .pi_over_2 = V2 (0x1.921fb54442d18p+0),
31 };
32 
33 #define SignMask v_u64 (0x8000000000000000)
34 
35 /* Special cases i.e. 0, infinity, NaN (fall back to scalar calls).  */
36 static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t y,float64x2_t x,float64x2_t ret,uint64x2_t cmp)37 special_case (float64x2_t y, float64x2_t x, float64x2_t ret, uint64x2_t cmp)
38 {
39   return v_call2_f64 (atan2, y, x, ret, cmp);
40 }
41 
42 /* Returns 1 if input is the bit representation of 0, infinity or nan.  */
43 static inline uint64x2_t
zeroinfnan(uint64x2_t i)44 zeroinfnan (uint64x2_t i)
45 {
46   /* (2 * i - 1) >= (2 * asuint64 (INFINITY) - 1).  */
47   return vcgeq_u64 (vsubq_u64 (vaddq_u64 (i, i), v_u64 (1)),
48 		    v_u64 (2 * asuint64 (INFINITY) - 1));
49 }
50 
51 /* Fast implementation of vector atan2.
52    Maximum observed error is 2.8 ulps:
53    _ZGVnN2vv_atan2 (0x1.9651a429a859ap+5, 0x1.953075f4ee26p+5)
54 	got 0x1.92d628ab678ccp-1
55        want 0x1.92d628ab678cfp-1.  */
V_NAME_D2(atan2)56 float64x2_t VPCS_ATTR V_NAME_D2 (atan2) (float64x2_t y, float64x2_t x)
57 {
58   const struct data *data_ptr = ptr_barrier (&data);
59 
60   uint64x2_t ix = vreinterpretq_u64_f64 (x);
61   uint64x2_t iy = vreinterpretq_u64_f64 (y);
62 
63   uint64x2_t special_cases = vorrq_u64 (zeroinfnan (ix), zeroinfnan (iy));
64 
65   uint64x2_t sign_x = vandq_u64 (ix, SignMask);
66   uint64x2_t sign_y = vandq_u64 (iy, SignMask);
67   uint64x2_t sign_xy = veorq_u64 (sign_x, sign_y);
68 
69   float64x2_t ax = vabsq_f64 (x);
70   float64x2_t ay = vabsq_f64 (y);
71 
72   uint64x2_t pred_xlt0 = vcltzq_f64 (x);
73   uint64x2_t pred_aygtax = vcgtq_f64 (ay, ax);
74 
75   /* Set up z for call to atan.  */
76   float64x2_t n = vbslq_f64 (pred_aygtax, vnegq_f64 (ax), ay);
77   float64x2_t d = vbslq_f64 (pred_aygtax, ay, ax);
78   float64x2_t z = vdivq_f64 (n, d);
79 
80   /* Work out the correct shift.  */
81   float64x2_t shift = vreinterpretq_f64_u64 (
82       vandq_u64 (pred_xlt0, vreinterpretq_u64_f64 (v_f64 (-2.0))));
83   shift = vbslq_f64 (pred_aygtax, vaddq_f64 (shift, v_f64 (1.0)), shift);
84   shift = vmulq_f64 (shift, data_ptr->pi_over_2);
85 
86   /* Calculate the polynomial approximation.
87      Use split Estrin scheme for P(z^2) with deg(P)=19. Use split instead of
88      full scheme to avoid underflow in x^16.
89      The order 19 polynomial P approximates
90      (atan(sqrt(x))-sqrt(x))/x^(3/2).  */
91   float64x2_t z2 = vmulq_f64 (z, z);
92   float64x2_t x2 = vmulq_f64 (z2, z2);
93   float64x2_t x4 = vmulq_f64 (x2, x2);
94   float64x2_t x8 = vmulq_f64 (x4, x4);
95   float64x2_t ret
96       = vfmaq_f64 (v_estrin_7_f64 (z2, x2, x4, data_ptr->poly),
97 		   v_estrin_11_f64 (z2, x2, x4, x8, data_ptr->poly + 8), x8);
98 
99   /* Finalize. y = shift + z + z^3 * P(z^2).  */
100   ret = vfmaq_f64 (z, ret, vmulq_f64 (z2, z));
101   ret = vaddq_f64 (ret, shift);
102 
103   /* Account for the sign of x and y.  */
104   ret = vreinterpretq_f64_u64 (
105       veorq_u64 (vreinterpretq_u64_f64 (ret), sign_xy));
106 
107   if (unlikely (v_any_u64 (special_cases)))
108     return special_case (y, x, ret, special_cases);
109 
110   return ret;
111 }
112 
113 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h.  */
114 PL_SIG (V, D, 2, atan2)
115 // TODO tighten this once __v_atan2 is fixed
116 PL_TEST_ULP (V_NAME_D2 (atan2), 2.9)
117 PL_TEST_INTERVAL (V_NAME_D2 (atan2), -10.0, 10.0, 50000)
118 PL_TEST_INTERVAL (V_NAME_D2 (atan2), -1.0, 1.0, 40000)
119 PL_TEST_INTERVAL (V_NAME_D2 (atan2), 0.0, 1.0, 40000)
120 PL_TEST_INTERVAL (V_NAME_D2 (atan2), 1.0, 100.0, 40000)
121 PL_TEST_INTERVAL (V_NAME_D2 (atan2), 1e6, 1e32, 40000)
122