1 | /* Single-precision AdvSIMD 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 "v_math.h" |
21 | #include "poly_advsimd_f32.h" |
22 | |
23 | static const struct data |
24 | { |
25 | float32x4_t poly[5]; |
26 | float32x4_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 = { V4 (0x1.55555ep-3), V4 (0x1.33261ap-4), V4 (0x1.70d7dcp-5), |
31 | V4 (0x1.b059dp-6), V4 (0x1.3af7d8p-5) }, |
32 | .pi_over_2f = V4 (0x1.921fb6p+0f), |
33 | }; |
34 | |
35 | #define AbsMask 0x7fffffff |
36 | #define Half 0x3f000000 |
37 | #define One 0x3f800000 |
38 | #define Small 0x39800000 /* 2^-12. */ |
39 | |
40 | #if WANT_SIMD_EXCEPT |
41 | static float32x4_t VPCS_ATTR NOINLINE |
42 | special_case (float32x4_t x, float32x4_t y, uint32x4_t special) |
43 | { |
44 | return v_call_f32 (asinf, x, y, special); |
45 | } |
46 | #endif |
47 | |
48 | /* Single-precision implementation of vector asin(x). |
49 | |
50 | For |x| < Small, approximate asin(x) by x. Small = 2^-12 for correct |
51 | rounding. If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the |
52 | following approximation. |
53 | |
54 | For |x| in [Small, 0.5], use order 4 polynomial P such that the final |
55 | approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2). |
56 | |
57 | The largest observed error in this region is 0.83 ulps, |
58 | _ZGVnN4v_asinf (0x1.ea00f4p-2) got 0x1.fef15ep-2 want 0x1.fef15cp-2. |
59 | |
60 | For |x| in [0.5, 1.0], use same approximation with a change of variable |
61 | |
62 | asin(x) = pi/2 - (y + y * z * P(z)), with z = (1-x)/2 and y = sqrt(z). |
63 | |
64 | The largest observed error in this region is 2.41 ulps, |
65 | _ZGVnN4v_asinf (0x1.00203ep-1) got 0x1.0c3a64p-1 want 0x1.0c3a6p-1. */ |
66 | float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (asin) (float32x4_t x) |
67 | { |
68 | const struct data *d = ptr_barrier (&data); |
69 | |
70 | uint32x4_t ix = vreinterpretq_u32_f32 (x); |
71 | uint32x4_t ia = vandq_u32 (ix, v_u32 (AbsMask)); |
72 | |
73 | #if WANT_SIMD_EXCEPT |
74 | /* Special values need to be computed with scalar fallbacks so |
75 | that appropriate fp exceptions are raised. */ |
76 | uint32x4_t special |
77 | = vcgtq_u32 (vsubq_u32 (ia, v_u32 (Small)), v_u32 (One - Small)); |
78 | if (__glibc_unlikely (v_any_u32 (special))) |
79 | return special_case (x, x, v_u32 (0xffffffff)); |
80 | #endif |
81 | |
82 | float32x4_t ax = vreinterpretq_f32_u32 (ia); |
83 | uint32x4_t a_lt_half = vcltq_u32 (ia, v_u32 (Half)); |
84 | |
85 | /* Evaluate polynomial Q(x) = y + y * z * P(z) with |
86 | z = x ^ 2 and y = |x| , if |x| < 0.5 |
87 | z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5. */ |
88 | float32x4_t z2 = vbslq_f32 (a_lt_half, vmulq_f32 (x, x), |
89 | vfmsq_n_f32 (v_f32 (0.5), ax, 0.5)); |
90 | float32x4_t z = vbslq_f32 (a_lt_half, ax, vsqrtq_f32 (z2)); |
91 | |
92 | /* Use a single polynomial approximation P for both intervals. */ |
93 | float32x4_t p = v_horner_4_f32 (z2, d->poly); |
94 | /* Finalize polynomial: z + z * z2 * P(z2). */ |
95 | p = vfmaq_f32 (z, vmulq_f32 (z, z2), p); |
96 | |
97 | /* asin(|x|) = Q(|x|) , for |x| < 0.5 |
98 | = pi/2 - 2 Q(|x|), for |x| >= 0.5. */ |
99 | float32x4_t y |
100 | = vbslq_f32 (a_lt_half, p, vfmsq_n_f32 (d->pi_over_2f, p, 2.0)); |
101 | |
102 | /* Copy sign. */ |
103 | return vbslq_f32 (v_u32 (AbsMask), y, x); |
104 | } |
105 | libmvec_hidden_def (V_NAME_F1 (asin)) |
106 | HALF_WIDTH_ALIAS_F1 (asin) |
107 | |