xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_atan2f_3u.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-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_f32.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li   float32x4_t poly[8];
16*412f47f9SXin Li   float32x4_t pi_over_2;
17*412f47f9SXin Li } data = {
18*412f47f9SXin Li   /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19*412f47f9SXin Li      [2**-128, 1.0].
20*412f47f9SXin Li      Generated using fpminimax between FLT_MIN and 1.  */
21*412f47f9SXin Li   .poly = { V4 (-0x1.55555p-2f), V4 (0x1.99935ep-3f), V4 (-0x1.24051ep-3f),
22*412f47f9SXin Li 	    V4 (0x1.bd7368p-4f), V4 (-0x1.491f0ep-4f), V4 (0x1.93a2c0p-5f),
23*412f47f9SXin Li 	    V4 (-0x1.4c3c60p-6f), V4 (0x1.01fd88p-8f) },
24*412f47f9SXin Li   .pi_over_2 = V4 (0x1.921fb6p+0f),
25*412f47f9SXin Li };
26*412f47f9SXin Li 
27*412f47f9SXin Li #define SignMask v_u32 (0x80000000)
28*412f47f9SXin Li 
29*412f47f9SXin Li /* Special cases i.e. 0, infinity and nan (fall back to scalar calls).  */
30*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t y,float32x4_t x,float32x4_t ret,uint32x4_t cmp)31*412f47f9SXin Li special_case (float32x4_t y, float32x4_t x, float32x4_t ret, uint32x4_t cmp)
32*412f47f9SXin Li {
33*412f47f9SXin Li   return v_call2_f32 (atan2f, y, x, ret, cmp);
34*412f47f9SXin Li }
35*412f47f9SXin Li 
36*412f47f9SXin Li /* Returns 1 if input is the bit representation of 0, infinity or nan.  */
37*412f47f9SXin Li static inline uint32x4_t
zeroinfnan(uint32x4_t i)38*412f47f9SXin Li zeroinfnan (uint32x4_t i)
39*412f47f9SXin Li {
40*412f47f9SXin Li   /* 2 * i - 1 >= 2 * 0x7f800000lu - 1.  */
41*412f47f9SXin Li   return vcgeq_u32 (vsubq_u32 (vmulq_n_u32 (i, 2), v_u32 (1)),
42*412f47f9SXin Li 		    v_u32 (2 * 0x7f800000lu - 1));
43*412f47f9SXin Li }
44*412f47f9SXin Li 
45*412f47f9SXin Li /* Fast implementation of vector atan2f. Maximum observed error is
46*412f47f9SXin Li    2.95 ULP in [0x1.9300d6p+6 0x1.93c0c6p+6] x [0x1.8c2dbp+6 0x1.8cea6p+6]:
47*412f47f9SXin Li    _ZGVnN4vv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1
48*412f47f9SXin Li 						 want 0x1.967f00p-1.  */
V_NAME_F2(atan2)49*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F2 (atan2) (float32x4_t y, float32x4_t x)
50*412f47f9SXin Li {
51*412f47f9SXin Li   const struct data *data_ptr = ptr_barrier (&data);
52*412f47f9SXin Li 
53*412f47f9SXin Li   uint32x4_t ix = vreinterpretq_u32_f32 (x);
54*412f47f9SXin Li   uint32x4_t iy = vreinterpretq_u32_f32 (y);
55*412f47f9SXin Li 
56*412f47f9SXin Li   uint32x4_t special_cases = vorrq_u32 (zeroinfnan (ix), zeroinfnan (iy));
57*412f47f9SXin Li 
58*412f47f9SXin Li   uint32x4_t sign_x = vandq_u32 (ix, SignMask);
59*412f47f9SXin Li   uint32x4_t sign_y = vandq_u32 (iy, SignMask);
60*412f47f9SXin Li   uint32x4_t sign_xy = veorq_u32 (sign_x, sign_y);
61*412f47f9SXin Li 
62*412f47f9SXin Li   float32x4_t ax = vabsq_f32 (x);
63*412f47f9SXin Li   float32x4_t ay = vabsq_f32 (y);
64*412f47f9SXin Li 
65*412f47f9SXin Li   uint32x4_t pred_xlt0 = vcltzq_f32 (x);
66*412f47f9SXin Li   uint32x4_t pred_aygtax = vcgtq_f32 (ay, ax);
67*412f47f9SXin Li 
68*412f47f9SXin Li   /* Set up z for call to atanf.  */
69*412f47f9SXin Li   float32x4_t n = vbslq_f32 (pred_aygtax, vnegq_f32 (ax), ay);
70*412f47f9SXin Li   float32x4_t d = vbslq_f32 (pred_aygtax, ay, ax);
71*412f47f9SXin Li   float32x4_t z = vdivq_f32 (n, d);
72*412f47f9SXin Li 
73*412f47f9SXin Li   /* Work out the correct shift.  */
74*412f47f9SXin Li   float32x4_t shift = vreinterpretq_f32_u32 (
75*412f47f9SXin Li       vandq_u32 (pred_xlt0, vreinterpretq_u32_f32 (v_f32 (-2.0f))));
76*412f47f9SXin Li   shift = vbslq_f32 (pred_aygtax, vaddq_f32 (shift, v_f32 (1.0f)), shift);
77*412f47f9SXin Li   shift = vmulq_f32 (shift, data_ptr->pi_over_2);
78*412f47f9SXin Li 
79*412f47f9SXin Li   /* Calculate the polynomial approximation.
80*412f47f9SXin Li      Use 2-level Estrin scheme for P(z^2) with deg(P)=7. However,
81*412f47f9SXin Li      a standard implementation using z8 creates spurious underflow
82*412f47f9SXin Li      in the very last fma (when z^8 is small enough).
83*412f47f9SXin Li      Therefore, we split the last fma into a mul and an fma.
84*412f47f9SXin Li      Horner and single-level Estrin have higher errors that exceed
85*412f47f9SXin Li      threshold.  */
86*412f47f9SXin Li   float32x4_t z2 = vmulq_f32 (z, z);
87*412f47f9SXin Li   float32x4_t z4 = vmulq_f32 (z2, z2);
88*412f47f9SXin Li 
89*412f47f9SXin Li   float32x4_t ret = vfmaq_f32 (
90*412f47f9SXin Li       v_pairwise_poly_3_f32 (z2, z4, data_ptr->poly), z4,
91*412f47f9SXin Li       vmulq_f32 (z4, v_pairwise_poly_3_f32 (z2, z4, data_ptr->poly + 4)));
92*412f47f9SXin Li 
93*412f47f9SXin Li   /* y = shift + z * P(z^2).  */
94*412f47f9SXin Li   ret = vaddq_f32 (vfmaq_f32 (z, ret, vmulq_f32 (z2, z)), shift);
95*412f47f9SXin Li 
96*412f47f9SXin Li   /* Account for the sign of y.  */
97*412f47f9SXin Li   ret = vreinterpretq_f32_u32 (
98*412f47f9SXin Li       veorq_u32 (vreinterpretq_u32_f32 (ret), sign_xy));
99*412f47f9SXin Li 
100*412f47f9SXin Li   if (unlikely (v_any_u32 (special_cases)))
101*412f47f9SXin Li     {
102*412f47f9SXin Li       return special_case (y, x, ret, special_cases);
103*412f47f9SXin Li     }
104*412f47f9SXin Li 
105*412f47f9SXin Li   return ret;
106*412f47f9SXin Li }
107*412f47f9SXin Li 
108*412f47f9SXin Li /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h.  */
109*412f47f9SXin Li PL_SIG (V, F, 2, atan2)
110*412f47f9SXin Li PL_TEST_ULP (V_NAME_F2 (atan2), 2.46)
111*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F2 (atan2), -10.0, 10.0, 50000)
112*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F2 (atan2), -1.0, 1.0, 40000)
113*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F2 (atan2), 0.0, 1.0, 40000)
114*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F2 (atan2), 1.0, 100.0, 40000)
115*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F2 (atan2), 1e6, 1e32, 40000)
116