1 /*
2  * Double-precision vector 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 "v_math.h"
9 #include "poly_advsimd_f64.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 static const struct data
14 {
15   float64x2_t poly[12];
16   float64x2_t pi, pi_over_2;
17   uint64x2_t abs_mask;
18 } data = {
19   /* Polynomial approximation of  (asin(sqrt(x)) - sqrt(x)) / (x * sqrt(x))
20      on [ 0x1p-106, 0x1p-2 ], relative error: 0x1.c3d8e169p-57.  */
21   .poly = { V2 (0x1.555555555554ep-3), V2 (0x1.3333333337233p-4),
22 	    V2 (0x1.6db6db67f6d9fp-5), V2 (0x1.f1c71fbd29fbbp-6),
23 	    V2 (0x1.6e8b264d467d6p-6), V2 (0x1.1c5997c357e9dp-6),
24 	    V2 (0x1.c86a22cd9389dp-7), V2 (0x1.856073c22ebbep-7),
25 	    V2 (0x1.fd1151acb6bedp-8), V2 (0x1.087182f799c1dp-6),
26 	    V2 (-0x1.6602748120927p-7), V2 (0x1.cfa0dd1f9478p-6), },
27   .pi = V2 (0x1.921fb54442d18p+1),
28   .pi_over_2 = V2 (0x1.921fb54442d18p+0),
29   .abs_mask = V2 (0x7fffffffffffffff),
30 };
31 
32 #define AllMask v_u64 (0xffffffffffffffff)
33 #define Oneu (0x3ff0000000000000)
34 #define Small (0x3e50000000000000) /* 2^-53.  */
35 
36 #if WANT_SIMD_EXCEPT
37 static float64x2_t VPCS_ATTR NOINLINE
38 special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
39 {
40   return v_call_f64 (acos, x, y, special);
41 }
42 #endif
43 
44 /* Double-precision implementation of vector acos(x).
45 
46    For |x| < Small, approximate acos(x) by pi/2 - x. Small = 2^-53 for correct
47    rounding.
48    If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the following
49    approximation.
50 
51    For |x| in [Small, 0.5], use an order 11 polynomial P such that the final
52    approximation of asin is an odd polynomial:
53 
54      acos(x) ~ pi/2 - (x + x^3 P(x^2)).
55 
56    The largest observed error in this region is 1.18 ulps,
57    _ZGVnN2v_acos (0x1.fbab0a7c460f6p-2) got 0x1.0d54d1985c068p+0
58 				       want 0x1.0d54d1985c069p+0.
59 
60    For |x| in [0.5, 1.0], use same approximation with a change of variable
61 
62      acos(x) = y + y * z * P(z), with  z = (1-x)/2 and y = sqrt(z).
63 
64    The largest observed error in this region is 1.52 ulps,
65    _ZGVnN2v_acos (0x1.23d362722f591p-1) got 0x1.edbbedf8a7d6ep-1
66 				       want 0x1.edbbedf8a7d6cp-1.  */
67 float64x2_t VPCS_ATTR V_NAME_D1 (acos) (float64x2_t x)
68 {
69   const struct data *d = ptr_barrier (&data);
70 
71   float64x2_t ax = vabsq_f64 (x);
72 
73 #if WANT_SIMD_EXCEPT
74   /* A single comparison for One, Small and QNaN.  */
75   uint64x2_t special
76       = vcgtq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (ax), v_u64 (Small)),
77 		   v_u64 (Oneu - Small));
78   if (unlikely (v_any_u64 (special)))
79     return special_case (x, x, AllMask);
80 #endif
81 
82   uint64x2_t a_le_half = vcleq_f64 (ax, v_f64 (0.5));
83 
84   /* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with
85      z2 = x ^ 2         and z = |x|     , if |x| < 0.5
86      z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5.  */
87   float64x2_t z2 = vbslq_f64 (a_le_half, vmulq_f64 (x, x),
88 			      vfmaq_f64 (v_f64 (0.5), v_f64 (-0.5), ax));
89   float64x2_t z = vbslq_f64 (a_le_half, ax, vsqrtq_f64 (z2));
90 
91   /* Use a single polynomial approximation P for both intervals.  */
92   float64x2_t z4 = vmulq_f64 (z2, z2);
93   float64x2_t z8 = vmulq_f64 (z4, z4);
94   float64x2_t z16 = vmulq_f64 (z8, z8);
95   float64x2_t p = v_estrin_11_f64 (z2, z4, z8, z16, d->poly);
96 
97   /* Finalize polynomial: z + z * z2 * P(z2).  */
98   p = vfmaq_f64 (z, vmulq_f64 (z, z2), p);
99 
100   /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for  |x| < 0.5
101 	       = 2 Q(|x|)               , for  0.5 < x < 1.0
102 	       = pi - 2 Q(|x|)          , for -1.0 < x < -0.5.  */
103   float64x2_t y = vbslq_f64 (d->abs_mask, p, x);
104 
105   uint64x2_t is_neg = vcltzq_f64 (x);
106   float64x2_t off = vreinterpretq_f64_u64 (
107       vandq_u64 (is_neg, vreinterpretq_u64_f64 (d->pi)));
108   float64x2_t mul = vbslq_f64 (a_le_half, v_f64 (-1.0), v_f64 (2.0));
109   float64x2_t add = vbslq_f64 (a_le_half, d->pi_over_2, off);
110 
111   return vfmaq_f64 (add, mul, y);
112 }
113 
114 PL_SIG (V, D, 1, acos, -1.0, 1.0)
115 PL_TEST_ULP (V_NAME_D1 (acos), 1.02)
116 PL_TEST_EXPECT_FENV (V_NAME_D1 (acos), WANT_SIMD_EXCEPT)
117 PL_TEST_INTERVAL (V_NAME_D1 (acos), 0, Small, 5000)
118 PL_TEST_INTERVAL (V_NAME_D1 (acos), Small, 0.5, 50000)
119 PL_TEST_INTERVAL (V_NAME_D1 (acos), 0.5, 1.0, 50000)
120 PL_TEST_INTERVAL (V_NAME_D1 (acos), 1.0, 0x1p11, 50000)
121 PL_TEST_INTERVAL (V_NAME_D1 (acos), 0x1p11, inf, 20000)
122 PL_TEST_INTERVAL (V_NAME_D1 (acos), -0, -inf, 20000)
123