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 "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11
12 #if SV_SUPPORTED
13
14 #include "sv_atan_common.h"
15
16 /* Useful constants. */
17 #define PiOver2 sv_f64 (0x1.921fb54442d18p+0)
18 #define SignMask sv_u64 (0x8000000000000000)
19
20 /* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */
21 __attribute__ ((noinline)) static sv_f64_t
specialcase(sv_f64_t y,sv_f64_t x,sv_f64_t ret,const svbool_t cmp)22 specialcase (sv_f64_t y, sv_f64_t x, sv_f64_t ret, const svbool_t cmp)
23 {
24 return sv_call2_f64 (atan2, y, x, ret, cmp);
25 }
26
27 /* Returns a predicate indicating true if the input is the bit representation of
28 0, infinity or nan. */
29 static inline svbool_t
zeroinfnan(sv_u64_t i,const svbool_t pg)30 zeroinfnan (sv_u64_t i, const svbool_t pg)
31 {
32 return svcmpge_u64 (pg, svsub_n_u64_x (pg, svlsl_n_u64_x (pg, i, 1), 1),
33 sv_u64 (2 * asuint64 (INFINITY) - 1));
34 }
35
36 /* Fast implementation of SVE atan2. Errors are greatest when y and
37 x are reasonably close together. The greatest observed error is 2.28 ULP:
38 sv_atan2(-0x1.5915b1498e82fp+732, 0x1.54d11ef838826p+732)
39 got -0x1.954f42f1fa841p-1 want -0x1.954f42f1fa843p-1. */
40 sv_f64_t
__sv_atan2_x(sv_f64_t y,sv_f64_t x,const svbool_t pg)41 __sv_atan2_x (sv_f64_t y, sv_f64_t x, const svbool_t pg)
42 {
43 sv_u64_t ix = sv_as_u64_f64 (x);
44 sv_u64_t iy = sv_as_u64_f64 (y);
45
46 svbool_t cmp_x = zeroinfnan (ix, pg);
47 svbool_t cmp_y = zeroinfnan (iy, pg);
48 svbool_t cmp_xy = svorr_b_z (pg, cmp_x, cmp_y);
49
50 sv_u64_t sign_x = svand_u64_x (pg, ix, SignMask);
51 sv_u64_t sign_y = svand_u64_x (pg, iy, SignMask);
52 sv_u64_t sign_xy = sveor_u64_x (pg, sign_x, sign_y);
53
54 sv_f64_t ax = svabs_f64_x (pg, x);
55 sv_f64_t ay = svabs_f64_x (pg, y);
56
57 svbool_t pred_xlt0 = svcmplt_f64 (pg, x, sv_f64 (0.0));
58 svbool_t pred_aygtax = svcmpgt_f64 (pg, ay, ax);
59
60 /* Set up z for call to atan. */
61 sv_f64_t n = svsel_f64 (pred_aygtax, svneg_f64_x (pg, ax), ay);
62 sv_f64_t d = svsel_f64 (pred_aygtax, ay, ax);
63 sv_f64_t z = svdiv_f64_x (pg, n, d);
64
65 /* Work out the correct shift. */
66 sv_f64_t shift = svsel_f64 (pred_xlt0, sv_f64 (-2.0), sv_f64 (0.0));
67 shift = svsel_f64 (pred_aygtax, svadd_n_f64_x (pg, shift, 1.0), shift);
68 shift = svmul_f64_x (pg, shift, PiOver2);
69
70 sv_f64_t ret = __sv_atan_common (pg, pg, z, z, shift);
71
72 /* Account for the sign of x and y. */
73 ret = sv_as_f64_u64 (sveor_u64_x (pg, sv_as_u64_f64 (ret), sign_xy));
74
75 if (unlikely (svptest_any (pg, cmp_xy)))
76 {
77 return specialcase (y, x, ret, cmp_xy);
78 }
79
80 return ret;
81 }
82
83 PL_ALIAS (__sv_atan2_x, _ZGVsMxvv_atan2)
84
85 /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h. */
86 PL_SIG (SV, D, 2, atan2)
87 PL_TEST_ULP (__sv_atan2, 1.78)
88 PL_TEST_INTERVAL (__sv_atan2, -10.0, 10.0, 50000)
89 PL_TEST_INTERVAL (__sv_atan2, -1.0, 1.0, 40000)
90 PL_TEST_INTERVAL (__sv_atan2, 0.0, 1.0, 40000)
91 PL_TEST_INTERVAL (__sv_atan2, 1.0, 100.0, 40000)
92 PL_TEST_INTERVAL (__sv_atan2, 1e6, 1e32, 40000)
93 #endif
94