1 /*
2 * Double-precision asin(x) function.
3 *
4 * Copyright (c) 2023-2024, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "poly_scalar_f64.h"
9 #include "math_config.h"
10 #include "test_sig.h"
11 #include "test_defs.h"
12
13 #define AbsMask 0x7fffffffffffffff
14 #define Half 0x3fe0000000000000
15 #define One 0x3ff0000000000000
16 #define PiOver2 0x1.921fb54442d18p+0
17 #define Small 0x3e50000000000000 /* 2^-26. */
18 #define Small16 0x3e50
19 #define QNaN 0x7ff8
20
21 /* Fast implementation of double-precision asin(x) based on polynomial
22 approximation.
23
24 For x < Small, approximate asin(x) by x. Small = 2^-26 for correct rounding.
25
26 For x in [Small, 0.5], use an order 11 polynomial P such that the final
27 approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).
28
29 The largest observed error in this region is 1.01 ulps,
30 asin(0x1.da9735b5a9277p-2) got 0x1.ed78525a927efp-2
31 want 0x1.ed78525a927eep-2.
32
33 No cheap approximation can be obtained near x = 1, since the function is not
34 continuously differentiable on 1.
35
36 For x in [0.5, 1.0], we use a method based on a trigonometric identity
37
38 asin(x) = pi/2 - acos(x)
39
40 and a generalized power series expansion of acos(y) near y=1, that reads as
41
42 acos(y)/sqrt(2y) ~ 1 + 1/12 * y + 3/160 * y^2 + ... (1)
43
44 The Taylor series of asin(z) near z = 0, reads as
45
46 asin(z) ~ z + z^3 P(z^2) = z + z^3 * (1/6 + 3/40 z^2 + ...).
47
48 Therefore, (1) can be written in terms of P(y/2) or even asin(y/2)
49
50 acos(y) ~ sqrt(2y) (1 + y/2 * P(y/2)) = 2 * sqrt(y/2) (1 + y/2 * P(y/2)
51
52 Hence, if we write z = (1-x)/2, z is near 0 when x approaches 1 and
53
54 asin(x) ~ pi/2 - acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z)).
55
56 The largest observed error in this region is 2.69 ulps,
57 asin(0x1.044e8cefee301p-1) got 0x1.1111dd54ddf96p-1
58 want 0x1.1111dd54ddf99p-1. */
59 double
asin(double x)60 asin (double x)
61 {
62 uint64_t ix = asuint64 (x);
63 uint64_t ia = ix & AbsMask;
64 uint64_t ia16 = ia >> 48;
65 double ax = asdouble (ia);
66 uint64_t sign = ix & ~AbsMask;
67
68 /* Special values and invalid range. */
69 if (unlikely (ia16 == QNaN))
70 return x;
71 if (ia > One)
72 return __math_invalid (x);
73 if (ia16 < Small16)
74 return x;
75
76 /* Evaluate polynomial Q(x) = y + y * z * P(z) with
77 z2 = x ^ 2 and z = |x| , if |x| < 0.5
78 z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */
79 double z2 = ax < 0.5 ? x * x : fma (-0.5, ax, 0.5);
80 double z = ax < 0.5 ? ax : sqrt (z2);
81
82 /* Use a single polynomial approximation P for both intervals. */
83 double z4 = z2 * z2;
84 double z8 = z4 * z4;
85 double z16 = z8 * z8;
86 double p = estrin_11_f64 (z2, z4, z8, z16, __asin_poly);
87
88 /* Finalize polynomial: z + z * z2 * P(z2). */
89 p = fma (z * z2, p, z);
90
91 /* asin(|x|) = Q(|x|) , for |x| < 0.5
92 = pi/2 - 2 Q(|x|), for |x| >= 0.5. */
93 double y = ax < 0.5 ? p : fma (-2.0, p, PiOver2);
94
95 /* Copy sign. */
96 return asdouble (asuint64 (y) | sign);
97 }
98
99 TEST_SIG (S, D, 1, asin, -1.0, 1.0)
100 TEST_ULP (asin, 2.20)
101 TEST_INTERVAL (asin, 0, Small, 5000)
102 TEST_INTERVAL (asin, Small, 0.5, 50000)
103 TEST_INTERVAL (asin, 0.5, 1.0, 50000)
104 TEST_INTERVAL (asin, 1.0, 0x1p11, 50000)
105 TEST_INTERVAL (asin, 0x1p11, inf, 20000)
106 TEST_INTERVAL (asin, -0, -inf, 20000)
107