xref: /freebsd/contrib/arm-optimized-routines/pl/math/acosf_1u4.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*
2  * Single-precision 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 "poly_scalar_f32.h"
9 #include "math_config.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #define AbsMask (0x7fffffff)
14 #define Half (0x3f000000)
15 #define One (0x3f800000)
16 #define PiOver2f (0x1.921fb6p+0f)
17 #define Pif (0x1.921fb6p+1f)
18 #define Small (0x32800000) /* 2^-26.  */
19 #define Small12 (0x328)
20 #define QNaN (0x7fc)
21 
22 /* Fast implementation of single-precision acos(x) based on polynomial
23    approximation of single-precision asin(x).
24 
25    For x < Small, approximate acos(x) by pi/2 - x. Small = 2^-26 for correct
26    rounding.
27 
28    For |x| in [Small, 0.5], use the trigonometric identity
29 
30      acos(x) = pi/2 - asin(x)
31 
32    and use an order 4 polynomial P such that the final approximation of asin is
33    an odd polynomial: asin(x) ~ x + x^3 * P(x^2).
34 
35    The largest observed error in this region is 1.16 ulps,
36      acosf(0x1.ffbeccp-2) got 0x1.0c27f8p+0 want 0x1.0c27f6p+0.
37 
38    For |x| in [0.5, 1.0], use the following development of acos(x) near x = 1
39 
40      acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z))
41 
42    where z = (1-x)/2, z is near 0 when x approaches 1, and P contributes to the
43    approximation of asin near 0.
44 
45    The largest observed error in this region is 1.32 ulps,
46      acosf(0x1.15ba56p-1) got 0x1.feb33p-1 want 0x1.feb32ep-1.
47 
48    For x in [-1.0, -0.5], use this other identity to deduce the negative inputs
49    from their absolute value.
50 
51      acos(x) = pi - acos(-x)
52 
53    The largest observed error in this region is 1.28 ulps,
54      acosf(-0x1.002072p-1) got 0x1.0c1e84p+1 want 0x1.0c1e82p+1.  */
55 float
56 acosf (float x)
57 {
58   uint32_t ix = asuint (x);
59   uint32_t ia = ix & AbsMask;
60   uint32_t ia12 = ia >> 20;
61   float ax = asfloat (ia);
62   uint32_t sign = ix & ~AbsMask;
63 
64   /* Special values and invalid range.  */
65   if (unlikely (ia12 == QNaN))
66     return x;
67   if (ia > One)
68     return __math_invalidf (x);
69   if (ia12 < Small12)
70     return PiOver2f - x;
71 
72   /* Evaluate polynomial Q(|x|) = z + z * z2 * P(z2) with
73      z2 = x ^ 2         and z = |x|     , if |x| < 0.5
74      z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5.  */
75   float z2 = ax < 0.5 ? x * x : fmaf (-0.5f, ax, 0.5f);
76   float z = ax < 0.5 ? ax : sqrtf (z2);
77 
78   /* Use a single polynomial approximation P for both intervals.  */
79   float p = horner_4_f32 (z2, __asinf_poly);
80   /* Finalize polynomial: z + z * z2 * P(z2).  */
81   p = fmaf (z * z2, p, z);
82 
83   /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.5
84 	       = pi - 2 Q(|x|), for -1.0 < x <= -0.5
85 	       = 2 Q(|x|)     , for -0.5 < x < 0.0.  */
86   if (ax < 0.5)
87     return PiOver2f - asfloat (asuint (p) | sign);
88 
89   return (x <= -0.5) ? fmaf (-2.0f, p, Pif) : 2.0f * p;
90 }
91 
92 PL_SIG (S, F, 1, acos, -1.0, 1.0)
93 PL_TEST_ULP (acosf, 0.82)
94 PL_TEST_INTERVAL (acosf, 0, Small, 5000)
95 PL_TEST_INTERVAL (acosf, Small, 0.5, 50000)
96 PL_TEST_INTERVAL (acosf, 0.5, 1.0, 50000)
97 PL_TEST_INTERVAL (acosf, 1.0, 0x1p11, 50000)
98 PL_TEST_INTERVAL (acosf, 0x1p11, inf, 20000)
99 PL_TEST_INTERVAL (acosf, -0, -inf, 20000)
100