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