1 | /* Single-precision SVE inverse sin |
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_f32.h" |
22 | |
23 | static const struct data |
24 | { |
25 | float32_t poly[5]; |
26 | float32_t pi_over_2f; |
27 | } data = { |
28 | /* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x)) on |
29 | [ 0x1p-24 0x1p-2 ] order = 4 rel error: 0x1.00a23bbp-29 . */ |
30 | .poly = { 0x1.55555ep-3, 0x1.33261ap-4, 0x1.70d7dcp-5, 0x1.b059dp-6, |
31 | 0x1.3af7d8p-5, }, |
32 | .pi_over_2f = 0x1.921fb6p+0f, |
33 | }; |
34 | |
35 | /* Single-precision SVE implementation of vector asin(x). |
36 | |
37 | For |x| in [0, 0.5], use order 4 polynomial P such that the final |
38 | approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2). |
39 | |
40 | The largest observed error in this region is 0.83 ulps, |
41 | _ZGVsMxv_asinf (0x1.ea00f4p-2) got 0x1.fef15ep-2 |
42 | want 0x1.fef15cp-2. |
43 | |
44 | For |x| in [0.5, 1.0], use same approximation with a change of variable |
45 | |
46 | asin(x) = pi/2 - (y + y * z * P(z)), with z = (1-x)/2 and y = sqrt(z). |
47 | |
48 | The largest observed error in this region is 2.41 ulps, |
49 | _ZGVsMxv_asinf (-0x1.00203ep-1) got -0x1.0c3a64p-1 |
50 | want -0x1.0c3a6p-1. */ |
51 | svfloat32_t SV_NAME_F1 (asin) (svfloat32_t x, const svbool_t pg) |
52 | { |
53 | const struct data *d = ptr_barrier (&data); |
54 | |
55 | svuint32_t sign = svand_x (pg, svreinterpret_u32 (x), 0x80000000); |
56 | |
57 | svfloat32_t ax = svabs_x (pg, x); |
58 | svbool_t a_ge_half = svacge (pg, x, 0.5); |
59 | |
60 | /* Evaluate polynomial Q(x) = y + y * z * P(z) with |
61 | z = x ^ 2 and y = |x| , if |x| < 0.5 |
62 | z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5. */ |
63 | svfloat32_t z2 = svsel (a_ge_half, svmls_x (pg, sv_f32 (x: 0.5), ax, 0.5), |
64 | svmul_x (pg, x, x)); |
65 | svfloat32_t z = svsqrt_m (ax, a_ge_half, z2); |
66 | |
67 | /* Use a single polynomial approximation P for both intervals. */ |
68 | svfloat32_t p = sv_horner_4_f32_x (pg, x: z2, poly: d->poly); |
69 | /* Finalize polynomial: z + z * z2 * P(z2). */ |
70 | p = svmla_x (pg, z, svmul_x (pg, z, z2), p); |
71 | |
72 | /* asin(|x|) = Q(|x|) , for |x| < 0.5 |
73 | = pi/2 - 2 Q(|x|), for |x| >= 0.5. */ |
74 | svfloat32_t y = svmad_m (a_ge_half, p, sv_f32 (x: -2.0), d->pi_over_2f); |
75 | |
76 | /* Copy sign. */ |
77 | return svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (y), sign)); |
78 | } |
79 | |