xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/experimental/asinf_2u5.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision asin(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2023-2024, Arm Limited.
5*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner  */
7*f3087befSAndrew Turner 
8*f3087befSAndrew Turner #include "poly_scalar_f32.h"
9*f3087befSAndrew Turner #include "math_config.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner 
13*f3087befSAndrew Turner #define AbsMask 0x7fffffff
14*f3087befSAndrew Turner #define Half 0x3f000000
15*f3087befSAndrew Turner #define One 0x3f800000
16*f3087befSAndrew Turner #define PiOver2f 0x1.921fb6p+0f
17*f3087befSAndrew Turner #define Small 0x39800000 /* 2^-12.  */
18*f3087befSAndrew Turner #define Small12 0x398
19*f3087befSAndrew Turner #define QNaN 0x7fc
20*f3087befSAndrew Turner 
21*f3087befSAndrew Turner /* Fast implementation of single-precision asin(x) based on polynomial
22*f3087befSAndrew Turner    approximation.
23*f3087befSAndrew Turner 
24*f3087befSAndrew Turner    For x < Small, approximate asin(x) by x. Small = 2^-12 for correct rounding.
25*f3087befSAndrew Turner 
26*f3087befSAndrew Turner    For x in [Small, 0.5], use order 4 polynomial P such that the final
27*f3087befSAndrew Turner    approximation is an odd polynomial: asin(x) ~ x + x^3 P(x^2).
28*f3087befSAndrew Turner 
29*f3087befSAndrew Turner    The largest observed error in this region is 0.83 ulps,
30*f3087befSAndrew Turner      asinf(0x1.ea00f4p-2) got 0x1.fef15ep-2 want 0x1.fef15cp-2.
31*f3087befSAndrew Turner 
32*f3087befSAndrew Turner    No cheap approximation can be obtained near x = 1, since the function is not
33*f3087befSAndrew Turner    continuously differentiable on 1.
34*f3087befSAndrew Turner 
35*f3087befSAndrew Turner    For x in [0.5, 1.0], we use a method based on a trigonometric identity
36*f3087befSAndrew Turner 
37*f3087befSAndrew Turner      asin(x) = pi/2 - acos(x)
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner    and a generalized power series expansion of acos(y) near y=1, that reads as
40*f3087befSAndrew Turner 
41*f3087befSAndrew Turner      acos(y)/sqrt(2y) ~ 1 + 1/12 * y + 3/160 * y^2 + ... (1)
42*f3087befSAndrew Turner 
43*f3087befSAndrew Turner    The Taylor series of asin(z) near z = 0, reads as
44*f3087befSAndrew Turner 
45*f3087befSAndrew Turner      asin(z) ~ z + z^3 P(z^2) = z + z^3 * (1/6 + 3/40 z^2 + ...).
46*f3087befSAndrew Turner 
47*f3087befSAndrew Turner    Therefore, (1) can be written in terms of P(y/2) or even asin(y/2)
48*f3087befSAndrew Turner 
49*f3087befSAndrew Turner      acos(y) ~ sqrt(2y) (1 + y/2 * P(y/2)) = 2 * sqrt(y/2) (1 + y/2 * P(y/2)
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner    Hence, if we write z = (1-x)/2, z is near 0 when x approaches 1 and
52*f3087befSAndrew Turner 
53*f3087befSAndrew Turner      asin(x) ~ pi/2 - acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z)).
54*f3087befSAndrew Turner 
55*f3087befSAndrew Turner    The largest observed error in this region is 2.41 ulps,
56*f3087befSAndrew Turner      asinf(0x1.00203ep-1) got 0x1.0c3a64p-1 want 0x1.0c3a6p-1.  */
57*f3087befSAndrew Turner float
asinf(float x)58*f3087befSAndrew Turner asinf (float x)
59*f3087befSAndrew Turner {
60*f3087befSAndrew Turner   uint32_t ix = asuint (x);
61*f3087befSAndrew Turner   uint32_t ia = ix & AbsMask;
62*f3087befSAndrew Turner   uint32_t ia12 = ia >> 20;
63*f3087befSAndrew Turner   float ax = asfloat (ia);
64*f3087befSAndrew Turner   uint32_t sign = ix & ~AbsMask;
65*f3087befSAndrew Turner 
66*f3087befSAndrew Turner   /* Special values and invalid range.  */
67*f3087befSAndrew Turner   if (unlikely (ia12 == QNaN))
68*f3087befSAndrew Turner     return x;
69*f3087befSAndrew Turner   if (ia > One)
70*f3087befSAndrew Turner     return __math_invalidf (x);
71*f3087befSAndrew Turner   if (ia12 < Small12)
72*f3087befSAndrew Turner     return x;
73*f3087befSAndrew Turner 
74*f3087befSAndrew Turner   /* Evaluate polynomial Q(x) = y + y * z * P(z) with
75*f3087befSAndrew Turner      z2 = x ^ 2         and z = |x|     , if |x| < 0.5
76*f3087befSAndrew Turner      z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5.  */
77*f3087befSAndrew Turner   float z2 = ax < 0.5 ? x * x : fmaf (-0.5f, ax, 0.5f);
78*f3087befSAndrew Turner   float z = ax < 0.5 ? ax : sqrtf (z2);
79*f3087befSAndrew Turner 
80*f3087befSAndrew Turner   /* Use a single polynomial approximation P for both intervals.  */
81*f3087befSAndrew Turner   float p = horner_4_f32 (z2, __asinf_poly);
82*f3087befSAndrew Turner   /* Finalize polynomial: z + z * z2 * P(z2).  */
83*f3087befSAndrew Turner   p = fmaf (z * z2, p, z);
84*f3087befSAndrew Turner 
85*f3087befSAndrew Turner   /* asin(|x|) = Q(|x|)         , for |x| < 0.5
86*f3087befSAndrew Turner 	       = pi/2 - 2 Q(|x|), for |x| >= 0.5.  */
87*f3087befSAndrew Turner   float y = ax < 0.5 ? p : fmaf (-2.0f, p, PiOver2f);
88*f3087befSAndrew Turner 
89*f3087befSAndrew Turner   /* Copy sign.  */
90*f3087befSAndrew Turner   return asfloat (asuint (y) | sign);
91*f3087befSAndrew Turner }
92*f3087befSAndrew Turner 
93*f3087befSAndrew Turner TEST_SIG (S, F, 1, asin, -1.0, 1.0)
94*f3087befSAndrew Turner TEST_ULP (asinf, 1.91)
95*f3087befSAndrew Turner TEST_INTERVAL (asinf, 0, Small, 5000)
96*f3087befSAndrew Turner TEST_INTERVAL (asinf, Small, 0.5, 50000)
97*f3087befSAndrew Turner TEST_INTERVAL (asinf, 0.5, 1.0, 50000)
98*f3087befSAndrew Turner TEST_INTERVAL (asinf, 1.0, 0x1p11, 50000)
99*f3087befSAndrew Turner TEST_INTERVAL (asinf, 0x1p11, inf, 20000)
100*f3087befSAndrew Turner TEST_INTERVAL (asinf, -0, -inf, 20000)
101