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