1*2fe8fb19SBen Gras /* @(#)e_asin.c 5.1 93/09/24 */
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras * ====================================================
4*2fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*2fe8fb19SBen Gras *
6*2fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
7*2fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
8*2fe8fb19SBen Gras * software is freely granted, provided that this notice
9*2fe8fb19SBen Gras * is preserved.
10*2fe8fb19SBen Gras * ====================================================
11*2fe8fb19SBen Gras */
12*2fe8fb19SBen Gras
13*2fe8fb19SBen Gras #include <sys/cdefs.h>
14*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*2fe8fb19SBen Gras __RCSID("$NetBSD: e_asin.c,v 1.12 2002/05/26 22:01:48 wiz Exp $");
16*2fe8fb19SBen Gras #endif
17*2fe8fb19SBen Gras
18*2fe8fb19SBen Gras /* __ieee754_asin(x)
19*2fe8fb19SBen Gras * Method :
20*2fe8fb19SBen Gras * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
21*2fe8fb19SBen Gras * we approximate asin(x) on [0,0.5] by
22*2fe8fb19SBen Gras * asin(x) = x + x*x^2*R(x^2)
23*2fe8fb19SBen Gras * where
24*2fe8fb19SBen Gras * R(x^2) is a rational approximation of (asin(x)-x)/x^3
25*2fe8fb19SBen Gras * and its remez error is bounded by
26*2fe8fb19SBen Gras * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
27*2fe8fb19SBen Gras *
28*2fe8fb19SBen Gras * For x in [0.5,1]
29*2fe8fb19SBen Gras * asin(x) = pi/2-2*asin(sqrt((1-x)/2))
30*2fe8fb19SBen Gras * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
31*2fe8fb19SBen Gras * then for x>0.98
32*2fe8fb19SBen Gras * asin(x) = pi/2 - 2*(s+s*z*R(z))
33*2fe8fb19SBen Gras * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
34*2fe8fb19SBen Gras * For x<=0.98, let pio4_hi = pio2_hi/2, then
35*2fe8fb19SBen Gras * f = hi part of s;
36*2fe8fb19SBen Gras * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
37*2fe8fb19SBen Gras * and
38*2fe8fb19SBen Gras * asin(x) = pi/2 - 2*(s+s*z*R(z))
39*2fe8fb19SBen Gras * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
40*2fe8fb19SBen Gras * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
41*2fe8fb19SBen Gras *
42*2fe8fb19SBen Gras * Special cases:
43*2fe8fb19SBen Gras * if x is NaN, return x itself;
44*2fe8fb19SBen Gras * if |x|>1, return NaN with invalid signal.
45*2fe8fb19SBen Gras *
46*2fe8fb19SBen Gras */
47*2fe8fb19SBen Gras
48*2fe8fb19SBen Gras
49*2fe8fb19SBen Gras #include "math.h"
50*2fe8fb19SBen Gras #include "math_private.h"
51*2fe8fb19SBen Gras
52*2fe8fb19SBen Gras static const double
53*2fe8fb19SBen Gras one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
54*2fe8fb19SBen Gras huge = 1.000e+300,
55*2fe8fb19SBen Gras pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
56*2fe8fb19SBen Gras pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
57*2fe8fb19SBen Gras pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
58*2fe8fb19SBen Gras /* coefficient for R(x^2) */
59*2fe8fb19SBen Gras pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
60*2fe8fb19SBen Gras pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
61*2fe8fb19SBen Gras pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
62*2fe8fb19SBen Gras pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
63*2fe8fb19SBen Gras pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
64*2fe8fb19SBen Gras pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
65*2fe8fb19SBen Gras qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
66*2fe8fb19SBen Gras qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
67*2fe8fb19SBen Gras qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
68*2fe8fb19SBen Gras qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
69*2fe8fb19SBen Gras
70*2fe8fb19SBen Gras double
__ieee754_asin(double x)71*2fe8fb19SBen Gras __ieee754_asin(double x)
72*2fe8fb19SBen Gras {
73*2fe8fb19SBen Gras double t,w,p,q,c,r,s;
74*2fe8fb19SBen Gras int32_t hx,ix;
75*2fe8fb19SBen Gras
76*2fe8fb19SBen Gras t = 0;
77*2fe8fb19SBen Gras GET_HIGH_WORD(hx,x);
78*2fe8fb19SBen Gras ix = hx&0x7fffffff;
79*2fe8fb19SBen Gras if(ix>= 0x3ff00000) { /* |x|>= 1 */
80*2fe8fb19SBen Gras u_int32_t lx;
81*2fe8fb19SBen Gras GET_LOW_WORD(lx,x);
82*2fe8fb19SBen Gras if(((ix-0x3ff00000)|lx)==0)
83*2fe8fb19SBen Gras /* asin(1)=+-pi/2 with inexact */
84*2fe8fb19SBen Gras return x*pio2_hi+x*pio2_lo;
85*2fe8fb19SBen Gras return (x-x)/(x-x); /* asin(|x|>1) is NaN */
86*2fe8fb19SBen Gras } else if (ix<0x3fe00000) { /* |x|<0.5 */
87*2fe8fb19SBen Gras if(ix<0x3e400000) { /* if |x| < 2**-27 */
88*2fe8fb19SBen Gras if(huge+x>one) return x;/* return x with inexact if x!=0*/
89*2fe8fb19SBen Gras } else
90*2fe8fb19SBen Gras t = x*x;
91*2fe8fb19SBen Gras p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
92*2fe8fb19SBen Gras q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
93*2fe8fb19SBen Gras w = p/q;
94*2fe8fb19SBen Gras return x+x*w;
95*2fe8fb19SBen Gras }
96*2fe8fb19SBen Gras /* 1> |x|>= 0.5 */
97*2fe8fb19SBen Gras w = one-fabs(x);
98*2fe8fb19SBen Gras t = w*0.5;
99*2fe8fb19SBen Gras p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
100*2fe8fb19SBen Gras q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
101*2fe8fb19SBen Gras s = __ieee754_sqrt(t);
102*2fe8fb19SBen Gras if(ix>=0x3FEF3333) { /* if |x| > 0.975 */
103*2fe8fb19SBen Gras w = p/q;
104*2fe8fb19SBen Gras t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
105*2fe8fb19SBen Gras } else {
106*2fe8fb19SBen Gras w = s;
107*2fe8fb19SBen Gras SET_LOW_WORD(w,0);
108*2fe8fb19SBen Gras c = (t-w*w)/(s+w);
109*2fe8fb19SBen Gras r = p/q;
110*2fe8fb19SBen Gras p = 2.0*s*r-(pio2_lo-2.0*c);
111*2fe8fb19SBen Gras q = pio4_hi-2.0*w;
112*2fe8fb19SBen Gras t = pio4_hi-(p-q);
113*2fe8fb19SBen Gras }
114*2fe8fb19SBen Gras if(hx>0) return t; else return -t;
115*2fe8fb19SBen Gras }
116