1 | /* Double-precision AdvSIMD 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 "v_math.h" |
21 | #include "poly_advsimd_f64.h" |
22 | |
23 | static const struct data |
24 | { |
25 | float64x2_t pi_over_2; |
26 | float64x2_t poly[20]; |
27 | } data = { |
28 | /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on |
29 | the interval [2**-1022, 1.0]. */ |
30 | .poly = { V2 (-0x1.5555555555555p-2), V2 (0x1.99999999996c1p-3), |
31 | V2 (-0x1.2492492478f88p-3), V2 (0x1.c71c71bc3951cp-4), |
32 | V2 (-0x1.745d160a7e368p-4), V2 (0x1.3b139b6a88ba1p-4), |
33 | V2 (-0x1.11100ee084227p-4), V2 (0x1.e1d0f9696f63bp-5), |
34 | V2 (-0x1.aebfe7b418581p-5), V2 (0x1.842dbe9b0d916p-5), |
35 | V2 (-0x1.5d30140ae5e99p-5), V2 (0x1.338e31eb2fbbcp-5), |
36 | V2 (-0x1.00e6eece7de8p-5), V2 (0x1.860897b29e5efp-6), |
37 | V2 (-0x1.0051381722a59p-6), V2 (0x1.14e9dc19a4a4ep-7), |
38 | V2 (-0x1.d0062b42fe3bfp-9), V2 (0x1.17739e210171ap-10), |
39 | V2 (-0x1.ab24da7be7402p-13), V2 (0x1.358851160a528p-16), }, |
40 | .pi_over_2 = V2 (0x1.921fb54442d18p+0), |
41 | }; |
42 | |
43 | #define SignMask v_u64 (0x8000000000000000) |
44 | |
45 | /* Special cases i.e. 0, infinity, NaN (fall back to scalar calls). */ |
46 | static float64x2_t VPCS_ATTR NOINLINE |
47 | special_case (float64x2_t y, float64x2_t x, float64x2_t ret, uint64x2_t cmp) |
48 | { |
49 | return v_call2_f64 (atan2, y, x, ret, cmp); |
50 | } |
51 | |
52 | /* Returns 1 if input is the bit representation of 0, infinity or nan. */ |
53 | static inline uint64x2_t |
54 | zeroinfnan (uint64x2_t i) |
55 | { |
56 | /* (2 * i - 1) >= (2 * asuint64 (INFINITY) - 1). */ |
57 | return vcgeq_u64 (vsubq_u64 (vaddq_u64 (i, i), v_u64 (1)), |
58 | v_u64 (2 * asuint64 (INFINITY) - 1)); |
59 | } |
60 | |
61 | /* Fast implementation of vector atan2. |
62 | Maximum observed error is 2.8 ulps: |
63 | _ZGVnN2vv_atan2 (0x1.9651a429a859ap+5, 0x1.953075f4ee26p+5) |
64 | got 0x1.92d628ab678ccp-1 |
65 | want 0x1.92d628ab678cfp-1. */ |
66 | float64x2_t VPCS_ATTR V_NAME_D2 (atan2) (float64x2_t y, float64x2_t x) |
67 | { |
68 | const struct data *data_ptr = ptr_barrier (&data); |
69 | |
70 | uint64x2_t ix = vreinterpretq_u64_f64 (x); |
71 | uint64x2_t iy = vreinterpretq_u64_f64 (y); |
72 | |
73 | uint64x2_t special_cases = vorrq_u64 (zeroinfnan (ix), zeroinfnan (iy)); |
74 | |
75 | uint64x2_t sign_x = vandq_u64 (ix, SignMask); |
76 | uint64x2_t sign_y = vandq_u64 (iy, SignMask); |
77 | uint64x2_t sign_xy = veorq_u64 (sign_x, sign_y); |
78 | |
79 | float64x2_t ax = vabsq_f64 (x); |
80 | float64x2_t ay = vabsq_f64 (y); |
81 | |
82 | uint64x2_t pred_xlt0 = vcltzq_f64 (x); |
83 | uint64x2_t pred_aygtax = vcgtq_f64 (ay, ax); |
84 | |
85 | /* Set up z for call to atan. */ |
86 | float64x2_t n = vbslq_f64 (pred_aygtax, vnegq_f64 (ax), ay); |
87 | float64x2_t d = vbslq_f64 (pred_aygtax, ay, ax); |
88 | float64x2_t z = vdivq_f64 (n, d); |
89 | |
90 | /* Work out the correct shift. */ |
91 | float64x2_t shift = vreinterpretq_f64_u64 ( |
92 | vandq_u64 (pred_xlt0, vreinterpretq_u64_f64 (v_f64 (-2.0)))); |
93 | shift = vbslq_f64 (pred_aygtax, vaddq_f64 (shift, v_f64 (1.0)), shift); |
94 | shift = vmulq_f64 (shift, data_ptr->pi_over_2); |
95 | |
96 | /* Calculate the polynomial approximation. |
97 | Use split Estrin scheme for P(z^2) with deg(P)=19. Use split instead of |
98 | full scheme to avoid underflow in x^16. |
99 | The order 19 polynomial P approximates |
100 | (atan(sqrt(x))-sqrt(x))/x^(3/2). */ |
101 | float64x2_t z2 = vmulq_f64 (z, z); |
102 | float64x2_t x2 = vmulq_f64 (z2, z2); |
103 | float64x2_t x4 = vmulq_f64 (x2, x2); |
104 | float64x2_t x8 = vmulq_f64 (x4, x4); |
105 | float64x2_t ret |
106 | = vfmaq_f64 (v_estrin_7_f64 (z2, x2, x4, data_ptr->poly), |
107 | v_estrin_11_f64 (z2, x2, x4, x8, data_ptr->poly + 8), x8); |
108 | |
109 | /* Finalize. y = shift + z + z^3 * P(z^2). */ |
110 | ret = vfmaq_f64 (z, ret, vmulq_f64 (z2, z)); |
111 | ret = vaddq_f64 (ret, shift); |
112 | |
113 | /* Account for the sign of x and y. */ |
114 | ret = vreinterpretq_f64_u64 ( |
115 | veorq_u64 (vreinterpretq_u64_f64 (ret), sign_xy)); |
116 | |
117 | if (__glibc_unlikely (v_any_u64 (special_cases))) |
118 | return special_case (y, x, ret, special_cases); |
119 | |
120 | return ret; |
121 | } |
122 | |