1 /*
2  * Single-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_f32.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 static const struct data
14 {
15   float32x4_t poly[5];
16   float32x4_t pi_over_2f, pif;
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 = { V4 (0x1.55555ep-3), V4 (0x1.33261ap-4), V4 (0x1.70d7dcp-5),
21 	    V4 (0x1.b059dp-6), V4 (0x1.3af7d8p-5) },
22   .pi_over_2f = V4 (0x1.921fb6p+0f),
23   .pif = V4 (0x1.921fb6p+1f),
24 };
25 
26 #define AbsMask 0x7fffffff
27 #define Half 0x3f000000
28 #define One 0x3f800000
29 #define Small 0x32800000 /* 2^-26.  */
30 
31 #if WANT_SIMD_EXCEPT
32 static float32x4_t VPCS_ATTR NOINLINE
33 special_case (float32x4_t x, float32x4_t y, uint32x4_t special)
34 {
35   return v_call_f32 (acosf, x, y, special);
36 }
37 #endif
38 
39 /* Single-precision implementation of vector acos(x).
40 
41    For |x| < Small, approximate acos(x) by pi/2 - x. Small = 2^-26 for correct
42    rounding.
43    If WANT_SIMD_EXCEPT = 0, Small = 0 and we proceed with the following
44    approximation.
45 
46    For |x| in [Small, 0.5], use order 4 polynomial P such that the final
47    approximation of asin is an odd polynomial:
48 
49      acos(x) ~ pi/2 - (x + x^3 P(x^2)).
50 
51     The largest observed error in this region is 1.26 ulps,
52       _ZGVnN4v_acosf (0x1.843bfcp-2) got 0x1.2e934cp+0 want 0x1.2e934ap+0.
53 
54     For |x| in [0.5, 1.0], use same approximation with a change of variable
55 
56       acos(x) = y + y * z * P(z), with  z = (1-x)/2 and y = sqrt(z).
57 
58    The largest observed error in this region is 1.32 ulps,
59    _ZGVnN4v_acosf (0x1.15ba56p-1) got 0x1.feb33p-1
60 			   want 0x1.feb32ep-1.  */
61 float32x4_t VPCS_ATTR V_NAME_F1 (acos) (float32x4_t x)
62 {
63   const struct data *d = ptr_barrier (&data);
64 
65   uint32x4_t ix = vreinterpretq_u32_f32 (x);
66   uint32x4_t ia = vandq_u32 (ix, v_u32 (AbsMask));
67 
68 #if WANT_SIMD_EXCEPT
69   /* A single comparison for One, Small and QNaN.  */
70   uint32x4_t special
71       = vcgtq_u32 (vsubq_u32 (ia, v_u32 (Small)), v_u32 (One - Small));
72   if (unlikely (v_any_u32 (special)))
73     return special_case (x, x, v_u32 (0xffffffff));
74 #endif
75 
76   float32x4_t ax = vreinterpretq_f32_u32 (ia);
77   uint32x4_t a_le_half = vcleq_u32 (ia, v_u32 (Half));
78 
79   /* Evaluate polynomial Q(x) = z + z * z2 * P(z2) with
80      z2 = x ^ 2         and z = |x|     , if |x| < 0.5
81      z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5.  */
82   float32x4_t z2 = vbslq_f32 (a_le_half, vmulq_f32 (x, x),
83 			      vfmsq_n_f32 (v_f32 (0.5), ax, 0.5));
84   float32x4_t z = vbslq_f32 (a_le_half, ax, vsqrtq_f32 (z2));
85 
86   /* Use a single polynomial approximation P for both intervals.  */
87   float32x4_t p = v_horner_4_f32 (z2, d->poly);
88   /* Finalize polynomial: z + z * z2 * P(z2).  */
89   p = vfmaq_f32 (z, vmulq_f32 (z, z2), p);
90 
91   /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for  |x| < 0.5
92 	       = 2 Q(|x|)               , for  0.5 < x < 1.0
93 	       = pi - 2 Q(|x|)          , for -1.0 < x < -0.5.  */
94   float32x4_t y = vbslq_f32 (v_u32 (AbsMask), p, x);
95 
96   uint32x4_t is_neg = vcltzq_f32 (x);
97   float32x4_t off = vreinterpretq_f32_u32 (
98       vandq_u32 (vreinterpretq_u32_f32 (d->pif), is_neg));
99   float32x4_t mul = vbslq_f32 (a_le_half, v_f32 (-1.0), v_f32 (2.0));
100   float32x4_t add = vbslq_f32 (a_le_half, d->pi_over_2f, off);
101 
102   return vfmaq_f32 (add, mul, y);
103 }
104 
105 PL_SIG (V, F, 1, acos, -1.0, 1.0)
106 PL_TEST_ULP (V_NAME_F1 (acos), 0.82)
107 PL_TEST_EXPECT_FENV (V_NAME_F1 (acos), WANT_SIMD_EXCEPT)
108 PL_TEST_INTERVAL (V_NAME_F1 (acos), 0, 0x1p-26, 5000)
109 PL_TEST_INTERVAL (V_NAME_F1 (acos), 0x1p-26, 0.5, 50000)
110 PL_TEST_INTERVAL (V_NAME_F1 (acos), 0.5, 1.0, 50000)
111 PL_TEST_INTERVAL (V_NAME_F1 (acos), 1.0, 0x1p11, 50000)
112 PL_TEST_INTERVAL (V_NAME_F1 (acos), 0x1p11, inf, 20000)
113 PL_TEST_INTERVAL (V_NAME_F1 (acos), -0, -inf, 20000)
114