1 /*
2  * Single-precision asin(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 Small (0x39800000) /* 2^-12.  */
18 #define Small12 (0x398)
19 #define QNaN (0x7fc)
20 
21 /* Fast implementation of single-precision asin(x) based on polynomial
22    approximation.
23 
24    For x < Small, approximate asin(x) by x. Small = 2^-12 for correct rounding.
25 
26    For x in [Small, 0.5], use order 4 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 0.83 ulps,
30      asinf(0x1.ea00f4p-2) got 0x1.fef15ep-2 want 0x1.fef15cp-2.
31 
32    No cheap approximation can be obtained near x = 1, since the function is not
33    continuously differentiable on 1.
34 
35    For x in [0.5, 1.0], we use a method based on a trigonometric identity
36 
37      asin(x) = pi/2 - acos(x)
38 
39    and a generalized power series expansion of acos(y) near y=1, that reads as
40 
41      acos(y)/sqrt(2y) ~ 1 + 1/12 * y + 3/160 * y^2 + ... (1)
42 
43    The Taylor series of asin(z) near z = 0, reads as
44 
45      asin(z) ~ z + z^3 P(z^2) = z + z^3 * (1/6 + 3/40 z^2 + ...).
46 
47    Therefore, (1) can be written in terms of P(y/2) or even asin(y/2)
48 
49      acos(y) ~ sqrt(2y) (1 + y/2 * P(y/2)) = 2 * sqrt(y/2) (1 + y/2 * P(y/2)
50 
51    Hence, if we write z = (1-x)/2, z is near 0 when x approaches 1 and
52 
53      asin(x) ~ pi/2 - acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z)).
54 
55    The largest observed error in this region is 2.41 ulps,
56      asinf(0x1.00203ep-1) got 0x1.0c3a64p-1 want 0x1.0c3a6p-1.  */
57 float
58 asinf (float x)
59 {
60   uint32_t ix = asuint (x);
61   uint32_t ia = ix & AbsMask;
62   uint32_t ia12 = ia >> 20;
63   float ax = asfloat (ia);
64   uint32_t sign = ix & ~AbsMask;
65 
66   /* Special values and invalid range.  */
67   if (unlikely (ia12 == QNaN))
68     return x;
69   if (ia > One)
70     return __math_invalidf (x);
71   if (ia12 < Small12)
72     return x;
73 
74   /* Evaluate polynomial Q(x) = y + y * z * P(z) with
75      z2 = x ^ 2         and z = |x|     , if |x| < 0.5
76      z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5.  */
77   float z2 = ax < 0.5 ? x * x : fmaf (-0.5f, ax, 0.5f);
78   float z = ax < 0.5 ? ax : sqrtf (z2);
79 
80   /* Use a single polynomial approximation P for both intervals.  */
81   float p = horner_4_f32 (z2, __asinf_poly);
82   /* Finalize polynomial: z + z * z2 * P(z2).  */
83   p = fmaf (z * z2, p, z);
84 
85   /* asin(|x|) = Q(|x|)         , for |x| < 0.5
86 	       = pi/2 - 2 Q(|x|), for |x| >= 0.5.  */
87   float y = ax < 0.5 ? p : fmaf (-2.0f, p, PiOver2f);
88 
89   /* Copy sign.  */
90   return asfloat (asuint (y) | sign);
91 }
92 
93 PL_SIG (S, F, 1, asin, -1.0, 1.0)
94 PL_TEST_ULP (asinf, 1.91)
95 PL_TEST_INTERVAL (asinf, 0, Small, 5000)
96 PL_TEST_INTERVAL (asinf, Small, 0.5, 50000)
97 PL_TEST_INTERVAL (asinf, 0.5, 1.0, 50000)
98 PL_TEST_INTERVAL (asinf, 1.0, 0x1p11, 50000)
99 PL_TEST_INTERVAL (asinf, 0x1p11, inf, 20000)
100 PL_TEST_INTERVAL (asinf, -0, -inf, 20000)
101