xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/sv_atan2f_3u.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1 /*
2  * Single-precision vector atan2f(x) function.
3  *
4  * Copyright (c) 2021-2024, 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_f32.h"
12 
13 static const struct data
14 {
15   float32_t poly[8];
16   float32_t pi_over_2;
17 } data = {
18   /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
19      [2**-128, 1.0].  */
20   .poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f,
21 	    -0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f },
22   .pi_over_2 = 0x1.921fb6p+0f,
23 };
24 
25 /* Special cases i.e. 0, infinity, nan (fall back to scalar calls).  */
26 static svfloat32_t NOINLINE
special_case(svfloat32_t y,svfloat32_t x,svfloat32_t ret,const svbool_t cmp)27 special_case (svfloat32_t y, svfloat32_t x, svfloat32_t ret,
28 	      const svbool_t cmp)
29 {
30   return sv_call2_f32 (atan2f, y, x, ret, cmp);
31 }
32 
33 /* Returns a predicate indicating true if the input is the bit representation
34    of 0, infinity or nan.  */
35 static inline svbool_t
zeroinfnan(svuint32_t i,const svbool_t pg)36 zeroinfnan (svuint32_t i, const svbool_t pg)
37 {
38   return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1),
39 		  sv_u32 (2 * 0x7f800000lu - 1));
40 }
41 
42 /* Fast implementation of SVE atan2f based on atan(x) ~ shift + z + z^3 *
43    P(z^2) with reduction to [0,1] using z=1/x and shift = pi/2. Maximum
44    observed error is 2.95 ULP:
45    _ZGVsMxvv_atan2f (0x1.93836cp+6, 0x1.8cae1p+6) got 0x1.967f06p-1
46 						 want 0x1.967f00p-1.  */
SV_NAME_F2(atan2)47 svfloat32_t SV_NAME_F2 (atan2) (svfloat32_t y, svfloat32_t x, const svbool_t pg)
48 {
49   const struct data *data_ptr = ptr_barrier (&data);
50 
51   svuint32_t ix = svreinterpret_u32 (x);
52   svuint32_t iy = svreinterpret_u32 (y);
53 
54   svbool_t cmp_x = zeroinfnan (ix, pg);
55   svbool_t cmp_y = zeroinfnan (iy, pg);
56   svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y);
57 
58   svfloat32_t ax = svabs_x (pg, x);
59   svfloat32_t ay = svabs_x (pg, y);
60   svuint32_t iax = svreinterpret_u32 (ax);
61   svuint32_t iay = svreinterpret_u32 (ay);
62 
63   svuint32_t sign_x = sveor_x (pg, ix, iax);
64   svuint32_t sign_y = sveor_x (pg, iy, iay);
65   svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y);
66 
67   svbool_t pred_aygtax = svcmpgt (pg, ay, ax);
68 
69   /* Set up z for call to atan.  */
70   svfloat32_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay);
71   svfloat32_t d = svsel (pred_aygtax, ay, ax);
72   svfloat32_t z = svdiv_x (pg, n, d);
73 
74   /* Work out the correct shift.  */
75   svfloat32_t shift = svreinterpret_f32 (svlsr_x (pg, sign_x, 1));
76   shift = svsel (pred_aygtax, sv_f32 (1.0), shift);
77   shift = svreinterpret_f32 (svorr_x (pg, sign_x, svreinterpret_u32 (shift)));
78   shift = svmul_x (pg, shift, sv_f32 (data_ptr->pi_over_2));
79 
80   /* Use pure Estrin scheme for P(z^2) with deg(P)=7.  */
81   svfloat32_t z2 = svmul_x (pg, z, z);
82   svfloat32_t z4 = svmul_x (pg, z2, z2);
83   svfloat32_t z8 = svmul_x (pg, z4, z4);
84 
85   svfloat32_t ret = sv_estrin_7_f32_x (pg, z2, z4, z8, data_ptr->poly);
86 
87   /* ret = shift + z + z^3 * P(z^2).  */
88   svfloat32_t z3 = svmul_x (pg, z2, z);
89   ret = svmla_x (pg, z, z3, ret);
90 
91   ret = svadd_m (pg, ret, shift);
92 
93   /* Account for the sign of x and y.  */
94 
95   if (unlikely (svptest_any (pg, cmp_xy)))
96     return special_case (
97 	y, x,
98 	svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)),
99 	cmp_xy);
100 
101   return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy));
102 }
103 
104 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h.  */
105 PL_SIG (SV, F, 2, atan2)
106 PL_TEST_ULP (SV_NAME_F2 (atan2), 2.45)
107 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), 0.0, 1.0, 40000)
108 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), 1.0, 100.0, 40000)
109 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), 100, inf, 40000)
110 PL_TEST_INTERVAL (SV_NAME_F2 (atan2), -0, -inf, 40000)
111