1/* Double-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_f64.h"
22
23static const struct data
24{
25 float64_t poly[12];
26 float64_t pi, pi_over_2;
27} data = {
28 /* Polynomial approximation of (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x))
29 on [ 0x1p-106, 0x1p-2 ], relative error: 0x1.c3d8e169p-57. */
30 .poly = { 0x1.555555555554ep-3, 0x1.3333333337233p-4, 0x1.6db6db67f6d9fp-5,
31 0x1.f1c71fbd29fbbp-6, 0x1.6e8b264d467d6p-6, 0x1.1c5997c357e9dp-6,
32 0x1.c86a22cd9389dp-7, 0x1.856073c22ebbep-7, 0x1.fd1151acb6bedp-8,
33 0x1.087182f799c1dp-6, -0x1.6602748120927p-7, 0x1.cfa0dd1f9478p-6, },
34 .pi = 0x1.921fb54442d18p+1,
35 .pi_over_2 = 0x1.921fb54442d18p+0,
36};
37
38/* Double-precision SVE implementation of vector acos(x).
39
40 For |x| in [0, 0.5], use an order 11 polynomial P such that the final
41 approximation of asin is an odd polynomial:
42
43 acos(x) ~ pi/2 - (x + x^3 P(x^2)).
44
45 The largest observed error in this region is 1.18 ulps,
46 _ZGVsMxv_acos (0x1.fbc5fe28ee9e3p-2) got 0x1.0d4d0f55667f6p+0
47 want 0x1.0d4d0f55667f7p+0.
48
49 For |x| in [0.5, 1.0], use same approximation with a change of variable
50
51 acos(x) = y + y * z * P(z), with z = (1-x)/2 and y = sqrt(z).
52
53 The largest observed error in this region is 1.52 ulps,
54 _ZGVsMxv_acos (0x1.24024271a500ap-1) got 0x1.ed82df4243f0dp-1
55 want 0x1.ed82df4243f0bp-1. */
56svfloat64_t SV_NAME_D1 (acos) (svfloat64_t x, const svbool_t pg)
57{
58 const struct data *d = ptr_barrier (&data);
59
60 svuint64_t sign = svand_x (pg, svreinterpret_u64 (x), 0x8000000000000000);
61 svfloat64_t ax = svabs_x (pg, x);
62
63 svbool_t a_gt_half = svacgt (pg, x, 0.5);
64
65 /* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with
66 z2 = x ^ 2 and z = |x| , if |x| < 0.5
67 z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */
68 svfloat64_t z2 = svsel (a_gt_half, svmls_x (pg, sv_f64 (x: 0.5), ax, 0.5),
69 svmul_x (pg, x, x));
70 svfloat64_t z = svsqrt_m (ax, a_gt_half, z2);
71
72 /* Use a single polynomial approximation P for both intervals. */
73 svfloat64_t z4 = svmul_x (pg, z2, z2);
74 svfloat64_t z8 = svmul_x (pg, z4, z4);
75 svfloat64_t z16 = svmul_x (pg, z8, z8);
76 svfloat64_t p = sv_estrin_11_f64_x (pg, x: z2, x2: z4, x4: z8, x8: z16, poly: d->poly);
77
78 /* Finalize polynomial: z + z * z2 * P(z2). */
79 p = svmla_x (pg, z, svmul_x (pg, z, z2), p);
80
81 /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.5
82 = 2 Q(|x|) , for 0.5 < x < 1.0
83 = pi - 2 Q(|x|) , for -1.0 < x < -0.5. */
84 svfloat64_t y
85 = svreinterpret_f64 (svorr_x (pg, svreinterpret_u64 (p), sign));
86
87 svbool_t is_neg = svcmplt (pg, x, 0.0);
88 svfloat64_t off = svdup_f64_z (is_neg, d->pi);
89 svfloat64_t mul = svsel (a_gt_half, sv_f64 (x: 2.0), sv_f64 (x: -1.0));
90 svfloat64_t add = svsel (a_gt_half, off, sv_f64 (x: d->pi_over_2));
91
92 return svmla_x (pg, add, mul, y);
93}
94

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