13f708241SDavid Schultz
23a8617a8SJordan K. Hubbard /*
33a8617a8SJordan K. Hubbard * ====================================================
43a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
53a8617a8SJordan K. Hubbard *
63f708241SDavid Schultz * Developed at SunSoft, a Sun Microsystems, Inc. business.
73a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this
83a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice
93a8617a8SJordan K. Hubbard * is preserved.
103a8617a8SJordan K. Hubbard * ====================================================
113a8617a8SJordan K. Hubbard */
123a8617a8SJordan K. Hubbard
13*99843eb8SSteve Kargl /* asin(x)
143a8617a8SJordan K. Hubbard * Method :
153a8617a8SJordan K. Hubbard * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
163a8617a8SJordan K. Hubbard * we approximate asin(x) on [0,0.5] by
173a8617a8SJordan K. Hubbard * asin(x) = x + x*x^2*R(x^2)
183a8617a8SJordan K. Hubbard * where
193a8617a8SJordan K. Hubbard * R(x^2) is a rational approximation of (asin(x)-x)/x^3
203a8617a8SJordan K. Hubbard * and its remez error is bounded by
213a8617a8SJordan K. Hubbard * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
223a8617a8SJordan K. Hubbard *
233a8617a8SJordan K. Hubbard * For x in [0.5,1]
243a8617a8SJordan K. Hubbard * asin(x) = pi/2-2*asin(sqrt((1-x)/2))
253a8617a8SJordan K. Hubbard * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
263a8617a8SJordan K. Hubbard * then for x>0.98
273a8617a8SJordan K. Hubbard * asin(x) = pi/2 - 2*(s+s*z*R(z))
283a8617a8SJordan K. Hubbard * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
293a8617a8SJordan K. Hubbard * For x<=0.98, let pio4_hi = pio2_hi/2, then
303a8617a8SJordan K. Hubbard * f = hi part of s;
313a8617a8SJordan K. Hubbard * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
323a8617a8SJordan K. Hubbard * and
333a8617a8SJordan K. Hubbard * asin(x) = pi/2 - 2*(s+s*z*R(z))
343a8617a8SJordan K. Hubbard * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
353a8617a8SJordan K. Hubbard * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
363a8617a8SJordan K. Hubbard *
373a8617a8SJordan K. Hubbard * Special cases:
383a8617a8SJordan K. Hubbard * if x is NaN, return x itself;
393a8617a8SJordan K. Hubbard * if |x|>1, return NaN with invalid signal.
403a8617a8SJordan K. Hubbard *
413a8617a8SJordan K. Hubbard */
423a8617a8SJordan K. Hubbard
4317303c62SDavid Schultz #include <float.h>
443a8617a8SJordan K. Hubbard
453a8617a8SJordan K. Hubbard #include "math.h"
463a8617a8SJordan K. Hubbard #include "math_private.h"
473a8617a8SJordan K. Hubbard
483a8617a8SJordan K. Hubbard static const double
493a8617a8SJordan K. Hubbard one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
503a8617a8SJordan K. Hubbard huge = 1.000e+300,
513a8617a8SJordan K. Hubbard pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
523a8617a8SJordan K. Hubbard pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
533a8617a8SJordan K. Hubbard pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
543a8617a8SJordan K. Hubbard /* coefficient for R(x^2) */
553a8617a8SJordan K. Hubbard pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
563a8617a8SJordan K. Hubbard pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
573a8617a8SJordan K. Hubbard pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
583a8617a8SJordan K. Hubbard pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
593a8617a8SJordan K. Hubbard pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
603a8617a8SJordan K. Hubbard pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
613a8617a8SJordan K. Hubbard qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
623a8617a8SJordan K. Hubbard qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
633a8617a8SJordan K. Hubbard qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
643a8617a8SJordan K. Hubbard qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
653a8617a8SJordan K. Hubbard
66a82bbc73SAlfred Perlstein double
asin(double x)67*99843eb8SSteve Kargl asin(double x)
683a8617a8SJordan K. Hubbard {
6951295a4dSJordan K. Hubbard double t=0.0,w,p,q,c,r,s;
703a8617a8SJordan K. Hubbard int32_t hx,ix;
713a8617a8SJordan K. Hubbard GET_HIGH_WORD(hx,x);
723a8617a8SJordan K. Hubbard ix = hx&0x7fffffff;
733a8617a8SJordan K. Hubbard if(ix>= 0x3ff00000) { /* |x|>= 1 */
743a8617a8SJordan K. Hubbard u_int32_t lx;
753a8617a8SJordan K. Hubbard GET_LOW_WORD(lx,x);
763a8617a8SJordan K. Hubbard if(((ix-0x3ff00000)|lx)==0)
773a8617a8SJordan K. Hubbard /* asin(1)=+-pi/2 with inexact */
783a8617a8SJordan K. Hubbard return x*pio2_hi+x*pio2_lo;
793a8617a8SJordan K. Hubbard return (x-x)/(x-x); /* asin(|x|>1) is NaN */
803a8617a8SJordan K. Hubbard } else if (ix<0x3fe00000) { /* |x|<0.5 */
81e044d80dSDavid Schultz if(ix<0x3e500000) { /* if |x| < 2**-26 */
823a8617a8SJordan K. Hubbard if(huge+x>one) return x;/* return x with inexact if x!=0*/
83e142bd0eSDavid Schultz }
843a8617a8SJordan K. Hubbard t = x*x;
853a8617a8SJordan K. Hubbard p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
863a8617a8SJordan K. Hubbard q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
873a8617a8SJordan K. Hubbard w = p/q;
883a8617a8SJordan K. Hubbard return x+x*w;
893a8617a8SJordan K. Hubbard }
903a8617a8SJordan K. Hubbard /* 1> |x|>= 0.5 */
913a8617a8SJordan K. Hubbard w = one-fabs(x);
923a8617a8SJordan K. Hubbard t = w*0.5;
933a8617a8SJordan K. Hubbard p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5)))));
943a8617a8SJordan K. Hubbard q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4)));
953f708241SDavid Schultz s = sqrt(t);
963a8617a8SJordan K. Hubbard if(ix>=0x3FEF3333) { /* if |x| > 0.975 */
973a8617a8SJordan K. Hubbard w = p/q;
983a8617a8SJordan K. Hubbard t = pio2_hi-(2.0*(s+s*w)-pio2_lo);
993a8617a8SJordan K. Hubbard } else {
1003a8617a8SJordan K. Hubbard w = s;
1013a8617a8SJordan K. Hubbard SET_LOW_WORD(w,0);
1023a8617a8SJordan K. Hubbard c = (t-w*w)/(s+w);
1033a8617a8SJordan K. Hubbard r = p/q;
1043a8617a8SJordan K. Hubbard p = 2.0*s*r-(pio2_lo-2.0*c);
1053a8617a8SJordan K. Hubbard q = pio4_hi-2.0*w;
1063a8617a8SJordan K. Hubbard t = pio4_hi-(p-q);
1073a8617a8SJordan K. Hubbard }
1083a8617a8SJordan K. Hubbard if(hx>0) return t; else return -t;
1093a8617a8SJordan K. Hubbard }
11017303c62SDavid Schultz
11117303c62SDavid Schultz #if LDBL_MANT_DIG == 53
11217303c62SDavid Schultz __weak_reference(asin, asinl);
11317303c62SDavid Schultz #endif
114