xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/atan2f.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
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 "test_sig.h"
10 #include "test_defs.h"
11 #include "sv_poly_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
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
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.  */
47 svfloat32_t SV_NAME_F2 (atan2) (svfloat32_t y, svfloat32_t x,
48 				const svbool_t pg)
49 {
50   const struct data *data_ptr = ptr_barrier (&data);
51 
52   svuint32_t ix = svreinterpret_u32 (x);
53   svuint32_t iy = svreinterpret_u32 (y);
54 
55   svbool_t cmp_x = zeroinfnan (ix, pg);
56   svbool_t cmp_y = zeroinfnan (iy, pg);
57   svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y);
58 
59   svfloat32_t ax = svabs_x (pg, x);
60   svfloat32_t ay = svabs_x (pg, y);
61   svuint32_t iax = svreinterpret_u32 (ax);
62   svuint32_t iay = svreinterpret_u32 (ay);
63 
64   svuint32_t sign_x = sveor_x (pg, ix, iax);
65   svuint32_t sign_y = sveor_x (pg, iy, iay);
66   svuint32_t sign_xy = sveor_x (pg, sign_x, sign_y);
67 
68   svbool_t pred_aygtax = svcmpgt (pg, ay, ax);
69 
70   /* Set up z for call to atan.  */
71   svfloat32_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay);
72   svfloat32_t d = svsel (pred_aygtax, ay, ax);
73   svfloat32_t z = svdiv_x (pg, n, d);
74 
75   /* Work out the correct shift.  */
76   svfloat32_t shift = svreinterpret_f32 (svlsr_x (pg, sign_x, 1));
77   shift = svsel (pred_aygtax, sv_f32 (1.0), shift);
78   shift = svreinterpret_f32 (svorr_x (pg, sign_x, svreinterpret_u32 (shift)));
79   shift = svmul_x (pg, shift, sv_f32 (data_ptr->pi_over_2));
80 
81   /* Use pure Estrin scheme for P(z^2) with deg(P)=7.  */
82   svfloat32_t z2 = svmul_x (pg, z, z);
83   svfloat32_t z4 = svmul_x (pg, z2, z2);
84   svfloat32_t z8 = svmul_x (pg, z4, z4);
85 
86   svfloat32_t ret = sv_estrin_7_f32_x (pg, z2, z4, z8, data_ptr->poly);
87 
88   /* ret = shift + z + z^3 * P(z^2).  */
89   svfloat32_t z3 = svmul_x (pg, z2, z);
90   ret = svmla_x (pg, z, z3, ret);
91 
92   ret = svadd_m (pg, ret, shift);
93 
94   /* Account for the sign of x and y.  */
95 
96   if (unlikely (svptest_any (pg, cmp_xy)))
97     return special_case (
98 	y, x,
99 	svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy)),
100 	cmp_xy);
101 
102   return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (ret), sign_xy));
103 }
104 
105 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h.  */
106 TEST_SIG (SV, F, 2, atan2)
107 TEST_ULP (SV_NAME_F2 (atan2), 2.45)
108 TEST_DISABLE_FENV (SV_NAME_F2 (atan2))
109 TEST_INTERVAL (SV_NAME_F2 (atan2), 0.0, 1.0, 40000)
110 TEST_INTERVAL (SV_NAME_F2 (atan2), 1.0, 100.0, 40000)
111 TEST_INTERVAL (SV_NAME_F2 (atan2), 100, inf, 40000)
112 TEST_INTERVAL (SV_NAME_F2 (atan2), -0, -inf, 40000)
113 CLOSE_SVE_ATTR
114