1/* Double-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_f64.h"
22
23static const struct data
24{
25 float64x2_t poly[12];
26 float64x2_t 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_over_2 = V2 (0x1.921fb54442d18p+0),
38 .abs_mask = V2 (0x7fffffffffffffff),
39};
40
41#define AllMask v_u64 (0xffffffffffffffff)
42#define One 0x3ff0000000000000
43#define Small 0x3e50000000000000 /* 2^-12. */
44
45#if WANT_SIMD_EXCEPT
46static float64x2_t VPCS_ATTR NOINLINE
47special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
48{
49 return v_call_f64 (asin, x, y, special);
50}
51#endif
52
53/* Double-precision implementation of vector asin(x).
54
55 For |x| < Small, approximate asin(x) by x. Small = 2^-12 for correct
56 rounding. If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the
57 following approximation.
58
59 For |x| in [Small, 0.5], use an order 11 polynomial P such that the final
60 approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).
61
62 The largest observed error in this region is 1.01 ulps,
63 _ZGVnN2v_asin (0x1.da9735b5a9277p-2) got 0x1.ed78525a927efp-2
64 want 0x1.ed78525a927eep-2.
65
66 For |x| in [0.5, 1.0], use same approximation with a change of variable
67
68 asin(x) = pi/2 - (y + y * z * P(z)), with z = (1-x)/2 and y = sqrt(z).
69
70 The largest observed error in this region is 2.69 ulps,
71 _ZGVnN2v_asin (0x1.044ac9819f573p-1) got 0x1.110d7e85fdd5p-1
72 want 0x1.110d7e85fdd53p-1. */
73float64x2_t VPCS_ATTR V_NAME_D1 (asin) (float64x2_t x)
74{
75 const struct data *d = ptr_barrier (&data);
76
77 float64x2_t ax = vabsq_f64 (x);
78
79#if WANT_SIMD_EXCEPT
80 /* Special values need to be computed with scalar fallbacks so
81 that appropriate exceptions are raised. */
82 uint64x2_t special
83 = vcgtq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (ax), v_u64 (Small)),
84 v_u64 (One - Small));
85 if (__glibc_unlikely (v_any_u64 (special)))
86 return special_case (x, x, AllMask);
87#endif
88
89 uint64x2_t a_lt_half = vcltq_f64 (ax, v_f64 (0.5));
90
91 /* Evaluate polynomial Q(x) = y + y * z * P(z) with
92 z = x ^ 2 and y = |x| , if |x| < 0.5
93 z = (1 - |x|) / 2 and y = sqrt(z), if |x| >= 0.5. */
94 float64x2_t z2 = vbslq_f64 (a_lt_half, vmulq_f64 (x, x),
95 vfmsq_n_f64 (v_f64 (0.5), ax, 0.5));
96 float64x2_t z = vbslq_f64 (a_lt_half, ax, vsqrtq_f64 (z2));
97
98 /* Use a single polynomial approximation P for both intervals. */
99 float64x2_t z4 = vmulq_f64 (z2, z2);
100 float64x2_t z8 = vmulq_f64 (z4, z4);
101 float64x2_t z16 = vmulq_f64 (z8, z8);
102 float64x2_t p = v_estrin_11_f64 (z2, z4, z8, z16, d->poly);
103
104 /* Finalize polynomial: z + z * z2 * P(z2). */
105 p = vfmaq_f64 (z, vmulq_f64 (z, z2), p);
106
107 /* asin(|x|) = Q(|x|) , for |x| < 0.5
108 = pi/2 - 2 Q(|x|), for |x| >= 0.5. */
109 float64x2_t y = vbslq_f64 (a_lt_half, p, vfmsq_n_f64 (d->pi_over_2, p, 2.0));
110
111 /* Copy sign. */
112 return vbslq_f64 (d->abs_mask, y, x);
113}
114

source code of glibc/sysdeps/aarch64/fpu/asin_advsimd.c