xref: /reactos/sdk/lib/crt/math/libm_sse2/asin.c (revision 84344399)
1 
2 /*******************************************************************************
3 MIT License
4 -----------
5 
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26 
27 #include "libm.h"
28 #include "libm_util.h"
29 
30 #define USE_VAL_WITH_FLAGS
31 #define USE_NAN_WITH_FLAGS
32 #define USE_HANDLE_ERROR
33 #include "libm_inlines.h"
34 #undef USE_NAN_WITH_FLAGS
35 #undef USE_VAL_WITH_FLAGS
36 #undef USE_HANDLE_ERROR
37 
38 #include "libm_errno.h"
39 
40 #ifdef _MSC_VER
41 #pragma function(asin)
42 #endif
43 
44 double FN_PROTOTYPE(asin)(double x)
45 {
46   /* Computes arcsin(x).
47      The argument is first reduced by noting that arcsin(x)
48      is invalid for abs(x) > 1 and arcsin(-x) = -arcsin(x).
49      For denormal and small arguments arcsin(x) = x to machine
50      accuracy. Remaining argument ranges are handled as follows.
51      For abs(x) <= 0.5 use
52      arcsin(x) = x + x^3*R(x^2)
53      where R(x^2) is a rational minimax approximation to
54      (arcsin(x) - x)/x^3.
55      For abs(x) > 0.5 exploit the identity:
56       arcsin(x) = pi/2 - 2*arcsin(sqrt(1-x)/2)
57      together with the above rational approximation, and
58      reconstruct the terms carefully.
59     */
60 
61   /* Some constants and split constants. */
62 
63   static const double
64     piby2_tail  = 6.1232339957367660e-17, /* 0x3c91a62633145c07 */
65     hpiby2_head = 7.8539816339744831e-01, /* 0x3fe921fb54442d18 */
66     piby2       = 1.5707963267948965e+00; /* 0x3ff921fb54442d18 */
67   double u, v, y, s=0.0, r;
68   int xexp, xnan, transform=0;
69 
70   unsigned long long ux, aux, xneg;
71   GET_BITS_DP64(x, ux);
72   aux = ux & ~SIGNBIT_DP64;
73   xneg = (ux & SIGNBIT_DP64);
74   xnan = (aux > PINFBITPATT_DP64);
75   xexp = (int)((ux & EXPBITS_DP64) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
76 
77   /* Special cases */
78 
79   if (xnan)
80     {
81       return _handle_error("asin", OP_ASIN, ux|0x0008000000000000, _DOMAIN,
82                           0, EDOM, x, 0.0, 1);
83     }
84   else if (xexp < -28)
85     { /* y small enough that arcsin(x) = x */
86       return val_with_flags(x, AMD_F_INEXACT);
87     }
88   else if (xexp >= 0)
89     { /* abs(x) >= 1.0 */
90       if (x == 1.0)
91         return val_with_flags(piby2, AMD_F_INEXACT);
92       else if (x == -1.0)
93         return val_with_flags(-piby2, AMD_F_INEXACT);
94       else
95         return _handle_error("asin", OP_ASIN, INDEFBITPATT_DP64, _DOMAIN,
96                             AMD_F_INVALID, EDOM, x, 0.0, 1);
97     }
98 
99   if (xneg) y = -x;
100   else y = x;
101 
102   transform = (xexp >= -1); /* abs(x) >= 0.5 */
103 
104   if (transform)
105     { /* Transform y into the range [0,0.5) */
106       r = 0.5*(1.0 - y);
107       /* VC++ intrinsic call */
108       _mm_store_sd(&s, _mm_sqrt_sd(_mm_setzero_pd(), _mm_load_sd(&r)));
109       y = s;
110     }
111   else
112     r = y*y;
113 
114   /* Use a rational approximation for [0.0, 0.5] */
115 
116   u = r*(0.227485835556935010735943483075 +
117          (-0.445017216867635649900123110649 +
118           (0.275558175256937652532686256258 +
119            (-0.0549989809235685841612020091328 +
120             (0.00109242697235074662306043804220 +
121              0.0000482901920344786991880522822991*r)*r)*r)*r)*r)/
122     (1.36491501334161032038194214209 +
123      (-3.28431505720958658909889444194 +
124       (2.76568859157270989520376345954 +
125        (-0.943639137032492685763471240072 +
126         0.105869422087204370341222318533*r)*r)*r)*r);
127 
128   if (transform)
129     { /* Reconstruct asin carefully in transformed region */
130         {
131           double c, s1, p, q;
132           unsigned long long us;
133           GET_BITS_DP64(s, us);
134           PUT_BITS_DP64(0xffffffff00000000 & us, s1);
135           c = (r-s1*s1)/(s+s1);
136           p = 2.0*s*u - (piby2_tail-2.0*c);
137           q = hpiby2_head - 2.0*s1;
138           v = hpiby2_head - (p-q);
139         }
140     }
141   else
142     {
143       /* Use a temporary variable to prevent VC++ rearranging
144             y + y*u
145          into
146             y * (1 + u)
147          and getting an incorrectly rounded result */
148       double tmp;
149       tmp = y * u;
150       v = y + tmp;
151     }
152 
153   if (xneg) return -v;
154   else return v;
155 }
156