1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner * Double-precision acos(x) function.
3*5a02ffc3SAndrew Turner *
4*5a02ffc3SAndrew Turner * Copyright (c) 2023, Arm Limited.
5*5a02ffc3SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*5a02ffc3SAndrew Turner */
7*5a02ffc3SAndrew Turner
8*5a02ffc3SAndrew Turner #include "math_config.h"
9*5a02ffc3SAndrew Turner #include "poly_scalar_f64.h"
10*5a02ffc3SAndrew Turner #include "pl_sig.h"
11*5a02ffc3SAndrew Turner #include "pl_test.h"
12*5a02ffc3SAndrew Turner
13*5a02ffc3SAndrew Turner #define AbsMask (0x7fffffffffffffff)
14*5a02ffc3SAndrew Turner #define Half (0x3fe0000000000000)
15*5a02ffc3SAndrew Turner #define One (0x3ff0000000000000)
16*5a02ffc3SAndrew Turner #define PiOver2 (0x1.921fb54442d18p+0)
17*5a02ffc3SAndrew Turner #define Pi (0x1.921fb54442d18p+1)
18*5a02ffc3SAndrew Turner #define Small (0x3c90000000000000) /* 2^-53. */
19*5a02ffc3SAndrew Turner #define Small16 (0x3c90)
20*5a02ffc3SAndrew Turner #define QNaN (0x7ff8)
21*5a02ffc3SAndrew Turner
22*5a02ffc3SAndrew Turner /* Fast implementation of double-precision acos(x) based on polynomial
23*5a02ffc3SAndrew Turner approximation of double-precision asin(x).
24*5a02ffc3SAndrew Turner
25*5a02ffc3SAndrew Turner For x < Small, approximate acos(x) by pi/2 - x. Small = 2^-53 for correct
26*5a02ffc3SAndrew Turner rounding.
27*5a02ffc3SAndrew Turner
28*5a02ffc3SAndrew Turner For |x| in [Small, 0.5], use the trigonometric identity
29*5a02ffc3SAndrew Turner
30*5a02ffc3SAndrew Turner acos(x) = pi/2 - asin(x)
31*5a02ffc3SAndrew Turner
32*5a02ffc3SAndrew Turner and use an order 11 polynomial P such that the final approximation of asin is
33*5a02ffc3SAndrew Turner an odd polynomial: asin(x) ~ x + x^3 * P(x^2).
34*5a02ffc3SAndrew Turner
35*5a02ffc3SAndrew Turner The largest observed error in this region is 1.18 ulps,
36*5a02ffc3SAndrew Turner acos(0x1.fbab0a7c460f6p-2) got 0x1.0d54d1985c068p+0
37*5a02ffc3SAndrew Turner want 0x1.0d54d1985c069p+0.
38*5a02ffc3SAndrew Turner
39*5a02ffc3SAndrew Turner For |x| in [0.5, 1.0], use the following development of acos(x) near x = 1
40*5a02ffc3SAndrew Turner
41*5a02ffc3SAndrew Turner acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z))
42*5a02ffc3SAndrew Turner
43*5a02ffc3SAndrew Turner where z = (1-x)/2, z is near 0 when x approaches 1, and P contributes to the
44*5a02ffc3SAndrew Turner approximation of asin near 0.
45*5a02ffc3SAndrew Turner
46*5a02ffc3SAndrew Turner The largest observed error in this region is 1.52 ulps,
47*5a02ffc3SAndrew Turner acos(0x1.23d362722f591p-1) got 0x1.edbbedf8a7d6ep-1
48*5a02ffc3SAndrew Turner want 0x1.edbbedf8a7d6cp-1.
49*5a02ffc3SAndrew Turner
50*5a02ffc3SAndrew Turner For x in [-1.0, -0.5], use this other identity to deduce the negative inputs
51*5a02ffc3SAndrew Turner from their absolute value: acos(x) = pi - acos(-x). */
52*5a02ffc3SAndrew Turner double
acos(double x)53*5a02ffc3SAndrew Turner acos (double x)
54*5a02ffc3SAndrew Turner {
55*5a02ffc3SAndrew Turner uint64_t ix = asuint64 (x);
56*5a02ffc3SAndrew Turner uint64_t ia = ix & AbsMask;
57*5a02ffc3SAndrew Turner uint64_t ia16 = ia >> 48;
58*5a02ffc3SAndrew Turner double ax = asdouble (ia);
59*5a02ffc3SAndrew Turner uint64_t sign = ix & ~AbsMask;
60*5a02ffc3SAndrew Turner
61*5a02ffc3SAndrew Turner /* Special values and invalid range. */
62*5a02ffc3SAndrew Turner if (unlikely (ia16 == QNaN))
63*5a02ffc3SAndrew Turner return x;
64*5a02ffc3SAndrew Turner if (ia > One)
65*5a02ffc3SAndrew Turner return __math_invalid (x);
66*5a02ffc3SAndrew Turner if (ia16 < Small16)
67*5a02ffc3SAndrew Turner return PiOver2 - x;
68*5a02ffc3SAndrew Turner
69*5a02ffc3SAndrew Turner /* Evaluate polynomial Q(|x|) = z + z * z2 * P(z2) with
70*5a02ffc3SAndrew Turner z2 = x ^ 2 and z = |x| , if |x| < 0.5
71*5a02ffc3SAndrew Turner z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */
72*5a02ffc3SAndrew Turner double z2 = ax < 0.5 ? x * x : fma (-0.5, ax, 0.5);
73*5a02ffc3SAndrew Turner double z = ax < 0.5 ? ax : sqrt (z2);
74*5a02ffc3SAndrew Turner
75*5a02ffc3SAndrew Turner /* Use a single polynomial approximation P for both intervals. */
76*5a02ffc3SAndrew Turner double z4 = z2 * z2;
77*5a02ffc3SAndrew Turner double z8 = z4 * z4;
78*5a02ffc3SAndrew Turner double z16 = z8 * z8;
79*5a02ffc3SAndrew Turner double p = estrin_11_f64 (z2, z4, z8, z16, __asin_poly);
80*5a02ffc3SAndrew Turner
81*5a02ffc3SAndrew Turner /* Finalize polynomial: z + z * z2 * P(z2). */
82*5a02ffc3SAndrew Turner p = fma (z * z2, p, z);
83*5a02ffc3SAndrew Turner
84*5a02ffc3SAndrew Turner /* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.5
85*5a02ffc3SAndrew Turner = pi - 2 Q(|x|), for -1.0 < x <= -0.5
86*5a02ffc3SAndrew Turner = 2 Q(|x|) , for -0.5 < x < 0.0. */
87*5a02ffc3SAndrew Turner if (ax < 0.5)
88*5a02ffc3SAndrew Turner return PiOver2 - asdouble (asuint64 (p) | sign);
89*5a02ffc3SAndrew Turner
90*5a02ffc3SAndrew Turner return (x <= -0.5) ? fma (-2.0, p, Pi) : 2.0 * p;
91*5a02ffc3SAndrew Turner }
92*5a02ffc3SAndrew Turner
93*5a02ffc3SAndrew Turner PL_SIG (S, D, 1, acos, -1.0, 1.0)
94*5a02ffc3SAndrew Turner PL_TEST_ULP (acos, 1.02)
95*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (acos, 0, Small, 5000)
96*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (acos, Small, 0.5, 50000)
97*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (acos, 0.5, 1.0, 50000)
98*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (acos, 1.0, 0x1p11, 50000)
99*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (acos, 0x1p11, inf, 20000)
100*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (acos, -0, -inf, 20000)
101