xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_acosf_1u4.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1 /*
2  * Single-precision SVE acos(x) function.
3  *
4  * Copyright (c) 2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "poly_sve_f32.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 static const struct data
14 {
15   float32_t poly[5];
16   float32_t pi, pi_over_2;
17 } data = {
18   /* Polynomial approximation of  (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x))  on
19      [ 0x1p-24 0x1p-2 ] order = 4 rel error: 0x1.00a23bbp-29 .  */
20   .poly = { 0x1.55555ep-3, 0x1.33261ap-4, 0x1.70d7dcp-5, 0x1.b059dp-6,
21 	    0x1.3af7d8p-5, },
22   .pi = 0x1.921fb6p+1f,
23   .pi_over_2 = 0x1.921fb6p+0f,
24 };
25 
26 /* Single-precision SVE implementation of vector acos(x).
27 
28    For |x| in [0, 0.5], use order 4 polynomial P such that the final
29    approximation of asin is an odd polynomial:
30 
31      acos(x) ~ pi/2 - (x + x^3 P(x^2)).
32 
33     The largest observed error in this region is 1.16 ulps,
34       _ZGVsMxv_acosf(0x1.ffbeccp-2) got 0x1.0c27f8p+0
35 				   want 0x1.0c27f6p+0.
36 
37     For |x| in [0.5, 1.0], use same approximation with a change of variable
38 
39       acos(x) = y + y * z * P(z), with  z = (1-x)/2 and y = sqrt(z).
40 
41    The largest observed error in this region is 1.32 ulps,
42    _ZGVsMxv_acosf (0x1.15ba56p-1) got 0x1.feb33p-1
43 				 want 0x1.feb32ep-1.  */
44 svfloat32_t SV_NAME_F1 (acos) (svfloat32_t x, const svbool_t pg)
45 {
46   const struct data *d = ptr_barrier (&data);
47 
48   svuint32_t sign = svand_x (pg, svreinterpret_u32 (x), 0x80000000);
49   svfloat32_t ax = svabs_x (pg, x);
50   svbool_t a_gt_half = svacgt (pg, x, 0.5);
51 
52   /* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with
53      z2 = x ^ 2         and z = |x|     , if |x| < 0.5
54      z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5.  */
55   svfloat32_t z2 = svsel (a_gt_half, svmls_x (pg, sv_f32 (0.5), ax, 0.5),
56 			  svmul_x (pg, x, x));
57   svfloat32_t z = svsqrt_m (ax, a_gt_half, z2);
58 
59   /* Use a single polynomial approximation P for both intervals.  */
60   svfloat32_t p = sv_horner_4_f32_x (pg, z2, d->poly);
61   /* Finalize polynomial: z + z * z2 * P(z2).  */
62   p = svmla_x (pg, z, svmul_x (pg, z, z2), p);
63 
64   /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for  |x| < 0.5
65 	       = 2 Q(|x|)               , for  0.5 < x < 1.0
66 	       = pi - 2 Q(|x|)          , for -1.0 < x < -0.5.  */
67   svfloat32_t y
68       = svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (p), sign));
69 
70   svbool_t is_neg = svcmplt (pg, x, 0.0);
71   svfloat32_t off = svdup_f32_z (is_neg, d->pi);
72   svfloat32_t mul = svsel (a_gt_half, sv_f32 (2.0), sv_f32 (-1.0));
73   svfloat32_t add = svsel (a_gt_half, off, sv_f32 (d->pi_over_2));
74 
75   return svmla_x (pg, add, mul, y);
76 }
77 
78 PL_SIG (SV, F, 1, acos, -1.0, 1.0)
79 PL_TEST_ULP (SV_NAME_F1 (acos), 0.82)
80 PL_TEST_INTERVAL (SV_NAME_F1 (acos), 0, 0.5, 50000)
81 PL_TEST_INTERVAL (SV_NAME_F1 (acos), 0.5, 1.0, 50000)
82 PL_TEST_INTERVAL (SV_NAME_F1 (acos), 1.0, 0x1p11, 50000)
83 PL_TEST_INTERVAL (SV_NAME_F1 (acos), 0x1p11, inf, 20000)
84 PL_TEST_INTERVAL (SV_NAME_F1 (acos), -0, -inf, 20000)
85