1 | /* Single-precision SVE 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 "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, pi_over_2; |
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 = 0x1.921fb6p+1f, |
33 | .pi_over_2 = 0x1.921fb6p+0f, |
34 | }; |
35 | |
36 | /* Single-precision SVE implementation of vector acos(x). |
37 | |
38 | For |x| in [0, 0.5], use order 4 polynomial P such that the final |
39 | approximation of asin is an odd polynomial: |
40 | |
41 | acos(x) ~ pi/2 - (x + x^3 P(x^2)). |
42 | |
43 | The largest observed error in this region is 1.16 ulps, |
44 | _ZGVsMxv_acosf(0x1.ffbeccp-2) got 0x1.0c27f8p+0 |
45 | want 0x1.0c27f6p+0. |
46 | |
47 | For |x| in [0.5, 1.0], use same approximation with a change of variable |
48 | |
49 | acos(x) = y + y * z * P(z), with z = (1-x)/2 and y = sqrt(z). |
50 | |
51 | The largest observed error in this region is 1.32 ulps, |
52 | _ZGVsMxv_acosf (0x1.15ba56p-1) got 0x1.feb33p-1 |
53 | want 0x1.feb32ep-1. */ |
54 | svfloat32_t SV_NAME_F1 (acos) (svfloat32_t x, const svbool_t pg) |
55 | { |
56 | const struct data *d = ptr_barrier (&data); |
57 | |
58 | svuint32_t sign = svand_x (pg, svreinterpret_u32 (x), 0x80000000); |
59 | svfloat32_t ax = svabs_x (pg, x); |
60 | svbool_t a_gt_half = svacgt (pg, x, 0.5); |
61 | |
62 | /* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with |
63 | z2 = x ^ 2 and z = |x| , if |x| < 0.5 |
64 | z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */ |
65 | svfloat32_t z2 = svsel (a_gt_half, svmls_x (pg, sv_f32 (x: 0.5), ax, 0.5), |
66 | svmul_x (pg, x, x)); |
67 | svfloat32_t z = svsqrt_m (ax, a_gt_half, z2); |
68 | |
69 | /* Use a single polynomial approximation P for both intervals. */ |
70 | svfloat32_t p = sv_horner_4_f32_x (pg, x: z2, poly: d->poly); |
71 | /* Finalize polynomial: z + z * z2 * P(z2). */ |
72 | p = svmla_x (pg, z, svmul_x (pg, z, z2), p); |
73 | |
74 | /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.5 |
75 | = 2 Q(|x|) , for 0.5 < x < 1.0 |
76 | = pi - 2 Q(|x|) , for -1.0 < x < -0.5. */ |
77 | svfloat32_t y |
78 | = svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (p), sign)); |
79 | |
80 | svbool_t is_neg = svcmplt (pg, x, 0.0); |
81 | svfloat32_t off = svdup_f32_z (is_neg, d->pi); |
82 | svfloat32_t mul = svsel (a_gt_half, sv_f32 (x: 2.0), sv_f32 (x: -1.0)); |
83 | svfloat32_t add = svsel (a_gt_half, off, sv_f32 (x: d->pi_over_2)); |
84 | |
85 | return svmla_x (pg, add, mul, y); |
86 | } |
87 | |