1 /* Single-precision SVE inverse cos
3 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include "poly_sve_f32.h"
23 static const struct data
26 float32_t pi
, pi_over_2
;
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,
33 .pi_over_2
= 0x1.921fb6p
+0f
,
36 /* Single-precision SVE implementation of vector acos(x).
38 For |x| in [0, 0.5], use order 4 polynomial P such that the final
39 approximation of asin is an odd polynomial:
41 acos(x) ~ pi/2 - (x + x^3 P(x^2)).
43 The largest observed error in this region is 1.16 ulps,
44 _ZGVsMxv_acosf(0x1.ffbeccp-2) got 0x1.0c27f8p+0
47 For |x| in [0.5, 1.0], use same approximation with a change of variable
49 acos(x) = y + y * z * P(z), with z = (1-x)/2 and y = sqrt(z).
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
)
56 const struct data
*d
= ptr_barrier (&data
);
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);
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 (0.5), ax
, 0.5),
67 svfloat32_t z
= svsqrt_m (ax
, a_gt_half
, z2
);
69 /* Use a single polynomial approximation P for both intervals. */
70 svfloat32_t p
= sv_horner_4_f32_x (pg
, z2
, d
->poly
);
71 /* Finalize polynomial: z + z * z2 * P(z2). */
72 p
= svmla_x (pg
, z
, svmul_x (pg
, z
, z2
), p
);
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. */
78 = svreinterpret_f32 (svorr_x (pg
, svreinterpret_u32 (p
), sign
));
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 (2.0), sv_f32 (-1.0));
83 svfloat32_t add
= svsel (a_gt_half
, off
, sv_f32 (d
->pi_over_2
));
85 return svmla_x (pg
, add
, mul
, y
);