1 /*
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25
26 /* __ieee754_asin(x)
27  * Method :
28  *      Since  asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
29  *      we approximate asin(x) on [0,0.5] by
30  *              asin(x) = x + x*x^2*R(x^2)
31  *      where
32  *              R(x^2) is a rational approximation of (asin(x)-x)/x^3
33  *      and its remez error is bounded by
34  *              |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
35  *
36  *      For x in [0.5,1]
37  *              asin(x) = pi/2-2*asin(sqrt((1-x)/2))
38  *      Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
39  *      then for x>0.98
40  *              asin(x) = pi/2 - 2*(s+s*z*R(z))
41  *                      = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
42  *      For x<=0.98, let pio4_hi = pio2_hi/2, then
43  *              f = hi part of s;
44  *              c = sqrt(z) - f = (z-f*f)/(s+f)         ...f+c=sqrt(z)
45  *      and
46  *              asin(x) = pi/2 - 2*(s+s*z*R(z))
47  *                      = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
48  *                      = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
49  *
50  * Special cases:
51  *      if x is NaN, return x itself;
52  *      if |x|>1, return NaN with invalid signal.
53  *
54  */
55
56
57 #include "fdlibm.h"
58
59 #ifdef __STDC__
60 static const double
61 #else
62 static double
63 #endif
64 one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
65 huge =  1.000e+300,
66 pio2_hi =  1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
67 pio2_lo =  6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
68 pio4_hi =  7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
69         /* coefficient for R(x^2) */
70 pS0 =  1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
71 pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
72 pS2 =  2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
73 pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
74 pS4 =  7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
75 pS5 =  3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
76 qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
77 qS2 =  2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
78 qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
79 qS4 =  7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
80
81 #ifdef __STDC__
82         double __ieee754_asin(double x)
83 #else
84         double __ieee754_asin(x)
85         double x;
86 #endif
87 {
88         double t=0,w,p,q,c,r,s;
89         int hx,ix;
90         hx = __HI(x);
91         ix = hx&0x7fffffff;
92         if(ix>= 0x3ff00000) {           /* |x|>= 1 */
93             if(((ix-0x3ff00000)|__LO(x))==0)
94                     /* asin(1)=+-pi/2 with inexact */
95                 return x*pio2_hi+x*pio2_lo;
96             return (x-x)/(x-x);         /* asin(|x|>1) is NaN */
97         } else if (ix<0x3fe00000) {     /* |x|<0.5 */
98             if(ix<0x3e400000) {         /* if |x| < 2**-27 */
99                 if(huge+x>one) return x;/* return x with inexact if x!=0*/
100             } else
101                 t = x*x;
102                 p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
103                 q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
104                 w = p/q;
105                 return x+x*w;
106         }
107         /* 1> |x|>= 0.5 */
108         w = one-fabs(x);
109         t = w*0.5;
110         p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
111         q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
112         s = sqrt(t);
113         if(ix>=0x3FEF3333) {    /* if |x| > 0.975 */
114             w = p/q;
115             t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
116         } else {
117             w  = s;
118             __LO(w) = 0;
119             c  = (t-w*w)/(s+w);
120             r  = p/q;
121             p  = 2.0*s*r-(pio2_lo-2.0*c);
122             q  = pio4_hi-2.0*w;
123             t  = pio4_hi-(p-q);
124         }
125         if(hx>0) return t; else return -t;
126 }