1 | /* Double-precision AdvSIMD inverse cos |
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 poly[12]; |
26 | float64x2_t pi, pi_over_2; |
27 | uint64x2_t abs_mask; |
28 | } data = { |
29 | /* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x)) |
30 | on [ 0x1p-106, 0x1p-2 ], relative error: 0x1.c3d8e169p-57. */ |
31 | .poly = { V2 (0x1.555555555554ep-3), V2 (0x1.3333333337233p-4), |
32 | V2 (0x1.6db6db67f6d9fp-5), V2 (0x1.f1c71fbd29fbbp-6), |
33 | V2 (0x1.6e8b264d467d6p-6), V2 (0x1.1c5997c357e9dp-6), |
34 | V2 (0x1.c86a22cd9389dp-7), V2 (0x1.856073c22ebbep-7), |
35 | V2 (0x1.fd1151acb6bedp-8), V2 (0x1.087182f799c1dp-6), |
36 | V2 (-0x1.6602748120927p-7), V2 (0x1.cfa0dd1f9478p-6), }, |
37 | .pi = V2 (0x1.921fb54442d18p+1), |
38 | .pi_over_2 = V2 (0x1.921fb54442d18p+0), |
39 | .abs_mask = V2 (0x7fffffffffffffff), |
40 | }; |
41 | |
42 | #define AllMask v_u64 (0xffffffffffffffff) |
43 | #define Oneu 0x3ff0000000000000 |
44 | #define Small 0x3e50000000000000 /* 2^-53. */ |
45 | |
46 | #if WANT_SIMD_EXCEPT |
47 | static float64x2_t VPCS_ATTR NOINLINE |
48 | special_case (float64x2_t x, float64x2_t y, uint64x2_t special) |
49 | { |
50 | return v_call_f64 (acos, x, y, special); |
51 | } |
52 | #endif |
53 | |
54 | /* Double-precision implementation of vector acos(x). |
55 | |
56 | For |x| < Small, approximate acos(x) by pi/2 - x. Small = 2^-53 for correct |
57 | rounding. |
58 | If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the following |
59 | approximation. |
60 | |
61 | For |x| in [Small, 0.5], use an order 11 polynomial P such that the final |
62 | approximation of asin is an odd polynomial: |
63 | |
64 | acos(x) ~ pi/2 - (x + x^3 P(x^2)). |
65 | |
66 | The largest observed error in this region is 1.18 ulps, |
67 | _ZGVnN2v_acos (0x1.fbab0a7c460f6p-2) got 0x1.0d54d1985c068p+0 |
68 | want 0x1.0d54d1985c069p+0. |
69 | |
70 | For |x| in [0.5, 1.0], use same approximation with a change of variable |
71 | |
72 | acos(x) = y + y * z * P(z), with z = (1-x)/2 and y = sqrt(z). |
73 | |
74 | The largest observed error in this region is 1.52 ulps, |
75 | _ZGVnN2v_acos (0x1.23d362722f591p-1) got 0x1.edbbedf8a7d6ep-1 |
76 | want 0x1.edbbedf8a7d6cp-1. */ |
77 | float64x2_t VPCS_ATTR V_NAME_D1 (acos) (float64x2_t x) |
78 | { |
79 | const struct data *d = ptr_barrier (&data); |
80 | |
81 | float64x2_t ax = vabsq_f64 (x); |
82 | |
83 | #if WANT_SIMD_EXCEPT |
84 | /* A single comparison for One, Small and QNaN. */ |
85 | uint64x2_t special |
86 | = vcgtq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (ax), v_u64 (Small)), |
87 | v_u64 (Oneu - Small)); |
88 | if (__glibc_unlikely (v_any_u64 (special))) |
89 | return special_case (x, x, AllMask); |
90 | #endif |
91 | |
92 | uint64x2_t a_le_half = vcleq_f64 (ax, v_f64 (0.5)); |
93 | |
94 | /* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with |
95 | z2 = x ^ 2 and z = |x| , if |x| < 0.5 |
96 | z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */ |
97 | float64x2_t z2 = vbslq_f64 (a_le_half, vmulq_f64 (x, x), |
98 | vfmaq_f64 (v_f64 (0.5), v_f64 (-0.5), ax)); |
99 | float64x2_t z = vbslq_f64 (a_le_half, ax, vsqrtq_f64 (z2)); |
100 | |
101 | /* Use a single polynomial approximation P for both intervals. */ |
102 | float64x2_t z4 = vmulq_f64 (z2, z2); |
103 | float64x2_t z8 = vmulq_f64 (z4, z4); |
104 | float64x2_t z16 = vmulq_f64 (z8, z8); |
105 | float64x2_t p = v_estrin_11_f64 (z2, z4, z8, z16, d->poly); |
106 | |
107 | /* Finalize polynomial: z + z * z2 * P(z2). */ |
108 | p = vfmaq_f64 (z, vmulq_f64 (z, z2), p); |
109 | |
110 | /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.5 |
111 | = 2 Q(|x|) , for 0.5 < x < 1.0 |
112 | = pi - 2 Q(|x|) , for -1.0 < x < -0.5. */ |
113 | float64x2_t y = vbslq_f64 (d->abs_mask, p, x); |
114 | |
115 | uint64x2_t is_neg = vcltzq_f64 (x); |
116 | float64x2_t off = vreinterpretq_f64_u64 ( |
117 | vandq_u64 (is_neg, vreinterpretq_u64_f64 (d->pi))); |
118 | float64x2_t mul = vbslq_f64 (a_le_half, v_f64 (-1.0), v_f64 (2.0)); |
119 | float64x2_t add = vbslq_f64 (a_le_half, d->pi_over_2, off); |
120 | |
121 | return vfmaq_f64 (add, mul, y); |
122 | } |
123 | |