1 | /* Double-precision SVE atan2 |
2 | |
3 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include "sv_math.h" |
21 | #include "poly_sve_f64.h" |
22 | |
23 | static const struct data |
24 | { |
25 | float64_t poly[20]; |
26 | float64_t pi_over_2; |
27 | } data = { |
28 | /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on |
29 | [2**-1022, 1.0]. */ |
30 | .poly = { -0x1.5555555555555p-2, 0x1.99999999996c1p-3, -0x1.2492492478f88p-3, |
31 | 0x1.c71c71bc3951cp-4, -0x1.745d160a7e368p-4, 0x1.3b139b6a88ba1p-4, |
32 | -0x1.11100ee084227p-4, 0x1.e1d0f9696f63bp-5, -0x1.aebfe7b418581p-5, |
33 | 0x1.842dbe9b0d916p-5, -0x1.5d30140ae5e99p-5, 0x1.338e31eb2fbbcp-5, |
34 | -0x1.00e6eece7de8p-5, 0x1.860897b29e5efp-6, -0x1.0051381722a59p-6, |
35 | 0x1.14e9dc19a4a4ep-7, -0x1.d0062b42fe3bfp-9, 0x1.17739e210171ap-10, |
36 | -0x1.ab24da7be7402p-13, 0x1.358851160a528p-16, }, |
37 | .pi_over_2 = 0x1.921fb54442d18p+0, |
38 | }; |
39 | |
40 | /* Special cases i.e. 0, infinity, nan (fall back to scalar calls). */ |
41 | static svfloat64_t NOINLINE |
42 | special_case (svfloat64_t y, svfloat64_t x, svfloat64_t ret, |
43 | const svbool_t cmp) |
44 | { |
45 | return sv_call2_f64 (f: atan2, x1: y, x2: x, y: ret, cmp); |
46 | } |
47 | |
48 | /* Returns a predicate indicating true if the input is the bit representation |
49 | of 0, infinity or nan. */ |
50 | static inline svbool_t |
51 | zeroinfnan (svuint64_t i, const svbool_t pg) |
52 | { |
53 | return svcmpge (pg, svsub_x (pg, svlsl_x (pg, i, 1), 1), |
54 | sv_u64 (2 * asuint64 (INFINITY) - 1)); |
55 | } |
56 | |
57 | /* Fast implementation of SVE atan2. Errors are greatest when y and |
58 | x are reasonably close together. The greatest observed error is 2.28 ULP: |
59 | _ZGVsMxvv_atan2 (-0x1.5915b1498e82fp+732, 0x1.54d11ef838826p+732) |
60 | got -0x1.954f42f1fa841p-1 want -0x1.954f42f1fa843p-1. */ |
61 | svfloat64_t SV_NAME_D2 (atan2) (svfloat64_t y, svfloat64_t x, const svbool_t pg) |
62 | { |
63 | const struct data *data_ptr = ptr_barrier (&data); |
64 | |
65 | svuint64_t ix = svreinterpret_u64 (x); |
66 | svuint64_t iy = svreinterpret_u64 (y); |
67 | |
68 | svbool_t cmp_x = zeroinfnan (i: ix, pg); |
69 | svbool_t cmp_y = zeroinfnan (i: iy, pg); |
70 | svbool_t cmp_xy = svorr_z (pg, cmp_x, cmp_y); |
71 | |
72 | svfloat64_t ax = svabs_x (pg, x); |
73 | svfloat64_t ay = svabs_x (pg, y); |
74 | svuint64_t iax = svreinterpret_u64 (ax); |
75 | svuint64_t iay = svreinterpret_u64 (ay); |
76 | |
77 | svuint64_t sign_x = sveor_x (pg, ix, iax); |
78 | svuint64_t sign_y = sveor_x (pg, iy, iay); |
79 | svuint64_t sign_xy = sveor_x (pg, sign_x, sign_y); |
80 | |
81 | svbool_t pred_aygtax = svcmpgt (pg, ay, ax); |
82 | |
83 | /* Set up z for call to atan. */ |
84 | svfloat64_t n = svsel (pred_aygtax, svneg_x (pg, ax), ay); |
85 | svfloat64_t d = svsel (pred_aygtax, ay, ax); |
86 | svfloat64_t z = svdiv_x (pg, n, d); |
87 | |
88 | /* Work out the correct shift. */ |
89 | svfloat64_t shift = svreinterpret_f64 (svlsr_x (pg, sign_x, 1)); |
90 | shift = svsel (pred_aygtax, sv_f64 (x: 1.0), shift); |
91 | shift = svreinterpret_f64 (svorr_x (pg, sign_x, svreinterpret_u64 (shift))); |
92 | shift = svmul_x (pg, shift, data_ptr->pi_over_2); |
93 | |
94 | /* Use split Estrin scheme for P(z^2) with deg(P)=19. */ |
95 | svfloat64_t z2 = svmul_x (pg, z, z); |
96 | svfloat64_t x2 = svmul_x (pg, z2, z2); |
97 | svfloat64_t x4 = svmul_x (pg, x2, x2); |
98 | svfloat64_t x8 = svmul_x (pg, x4, x4); |
99 | |
100 | svfloat64_t ret = svmla_x ( |
101 | pg, sv_estrin_7_f64_x (pg, x: z2, x2, x4, poly: data_ptr->poly), |
102 | sv_estrin_11_f64_x (pg, x: z2, x2, x4, x8, poly: data_ptr->poly + 8), x8); |
103 | |
104 | /* y = shift + z + z^3 * P(z^2). */ |
105 | svfloat64_t z3 = svmul_x (pg, z2, z); |
106 | ret = svmla_x (pg, z, z3, ret); |
107 | |
108 | ret = svadd_m (pg, ret, shift); |
109 | |
110 | /* Account for the sign of x and y. */ |
111 | if (__glibc_unlikely (svptest_any (pg, cmp_xy))) |
112 | return special_case ( |
113 | y, x, |
114 | ret: svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (ret), sign_xy)), |
115 | cmp: cmp_xy); |
116 | return svreinterpret_f64 (sveor_x (pg, svreinterpret_u64 (ret), sign_xy)); |
117 | } |
118 | |