13a8617a8SJordan K. Hubbard /* @(#)e_asin.c 5.1 93/09/24 */ 23a8617a8SJordan K. Hubbard /* 33a8617a8SJordan K. Hubbard * ==================================================== 43a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 53a8617a8SJordan K. Hubbard * 63a8617a8SJordan K. Hubbard * Developed at SunPro, 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 133a8617a8SJordan K. Hubbard #ifndef lint 141130b656SJordan K. Hubbard static char rcsid[] = "$FreeBSD$"; 153a8617a8SJordan K. Hubbard #endif 163a8617a8SJordan K. Hubbard 173a8617a8SJordan K. Hubbard /* __ieee754_asin(x) 183a8617a8SJordan K. Hubbard * Method : 193a8617a8SJordan K. Hubbard * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ... 203a8617a8SJordan K. Hubbard * we approximate asin(x) on [0,0.5] by 213a8617a8SJordan K. Hubbard * asin(x) = x + x*x^2*R(x^2) 223a8617a8SJordan K. Hubbard * where 233a8617a8SJordan K. Hubbard * R(x^2) is a rational approximation of (asin(x)-x)/x^3 243a8617a8SJordan K. Hubbard * and its remez error is bounded by 253a8617a8SJordan K. Hubbard * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75) 263a8617a8SJordan K. Hubbard * 273a8617a8SJordan K. Hubbard * For x in [0.5,1] 283a8617a8SJordan K. Hubbard * asin(x) = pi/2-2*asin(sqrt((1-x)/2)) 293a8617a8SJordan K. Hubbard * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2; 303a8617a8SJordan K. Hubbard * then for x>0.98 313a8617a8SJordan K. Hubbard * asin(x) = pi/2 - 2*(s+s*z*R(z)) 323a8617a8SJordan K. Hubbard * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) 333a8617a8SJordan K. Hubbard * For x<=0.98, let pio4_hi = pio2_hi/2, then 343a8617a8SJordan K. Hubbard * f = hi part of s; 353a8617a8SJordan K. Hubbard * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z) 363a8617a8SJordan K. Hubbard * and 373a8617a8SJordan K. Hubbard * asin(x) = pi/2 - 2*(s+s*z*R(z)) 383a8617a8SJordan K. Hubbard * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) 393a8617a8SJordan K. Hubbard * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c)) 403a8617a8SJordan K. Hubbard * 413a8617a8SJordan K. Hubbard * Special cases: 423a8617a8SJordan K. Hubbard * if x is NaN, return x itself; 433a8617a8SJordan K. Hubbard * if |x|>1, return NaN with invalid signal. 443a8617a8SJordan K. Hubbard * 453a8617a8SJordan K. Hubbard */ 463a8617a8SJordan K. Hubbard 473a8617a8SJordan K. Hubbard 483a8617a8SJordan K. Hubbard #include "math.h" 493a8617a8SJordan K. Hubbard #include "math_private.h" 503a8617a8SJordan K. Hubbard 513a8617a8SJordan K. Hubbard #ifdef __STDC__ 523a8617a8SJordan K. Hubbard static const double 533a8617a8SJordan K. Hubbard #else 543a8617a8SJordan K. Hubbard static double 553a8617a8SJordan K. Hubbard #endif 563a8617a8SJordan K. Hubbard one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ 573a8617a8SJordan K. Hubbard huge = 1.000e+300, 583a8617a8SJordan K. Hubbard pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ 593a8617a8SJordan K. Hubbard pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ 603a8617a8SJordan K. Hubbard pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */ 613a8617a8SJordan K. Hubbard /* coefficient for R(x^2) */ 623a8617a8SJordan K. Hubbard pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ 633a8617a8SJordan K. Hubbard pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ 643a8617a8SJordan K. Hubbard pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ 653a8617a8SJordan K. Hubbard pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ 663a8617a8SJordan K. Hubbard pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ 673a8617a8SJordan K. Hubbard pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ 683a8617a8SJordan K. Hubbard qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ 693a8617a8SJordan K. Hubbard qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ 703a8617a8SJordan K. Hubbard qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ 713a8617a8SJordan K. Hubbard qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ 723a8617a8SJordan K. Hubbard 733a8617a8SJordan K. Hubbard #ifdef __STDC__ 74dab159e3SBruce Evans double __generic___ieee754_asin(double x) 753a8617a8SJordan K. Hubbard #else 76dab159e3SBruce Evans double __generic___ieee754_asin(x) 773a8617a8SJordan K. Hubbard double x; 783a8617a8SJordan K. Hubbard #endif 793a8617a8SJordan K. Hubbard { 8051295a4dSJordan K. Hubbard double t=0.0,w,p,q,c,r,s; 813a8617a8SJordan K. Hubbard int32_t hx,ix; 823a8617a8SJordan K. Hubbard GET_HIGH_WORD(hx,x); 833a8617a8SJordan K. Hubbard ix = hx&0x7fffffff; 843a8617a8SJordan K. Hubbard if(ix>= 0x3ff00000) { /* |x|>= 1 */ 853a8617a8SJordan K. Hubbard u_int32_t lx; 863a8617a8SJordan K. Hubbard GET_LOW_WORD(lx,x); 873a8617a8SJordan K. Hubbard if(((ix-0x3ff00000)|lx)==0) 883a8617a8SJordan K. Hubbard /* asin(1)=+-pi/2 with inexact */ 893a8617a8SJordan K. Hubbard return x*pio2_hi+x*pio2_lo; 903a8617a8SJordan K. Hubbard return (x-x)/(x-x); /* asin(|x|>1) is NaN */ 913a8617a8SJordan K. Hubbard } else if (ix<0x3fe00000) { /* |x|<0.5 */ 923a8617a8SJordan K. Hubbard if(ix<0x3e400000) { /* if |x| < 2**-27 */ 933a8617a8SJordan K. Hubbard if(huge+x>one) return x;/* return x with inexact if x!=0*/ 943a8617a8SJordan K. Hubbard } else 953a8617a8SJordan K. Hubbard t = x*x; 963a8617a8SJordan K. Hubbard p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); 973a8617a8SJordan K. Hubbard q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); 983a8617a8SJordan K. Hubbard w = p/q; 993a8617a8SJordan K. Hubbard return x+x*w; 1003a8617a8SJordan K. Hubbard } 1013a8617a8SJordan K. Hubbard /* 1> |x|>= 0.5 */ 1023a8617a8SJordan K. Hubbard w = one-fabs(x); 1033a8617a8SJordan K. Hubbard t = w*0.5; 1043a8617a8SJordan K. Hubbard p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); 1053a8617a8SJordan K. Hubbard q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); 1063a8617a8SJordan K. Hubbard s = sqrt(t); 1073a8617a8SJordan K. Hubbard if(ix>=0x3FEF3333) { /* if |x| > 0.975 */ 1083a8617a8SJordan K. Hubbard w = p/q; 1093a8617a8SJordan K. Hubbard t = pio2_hi-(2.0*(s+s*w)-pio2_lo); 1103a8617a8SJordan K. Hubbard } else { 1113a8617a8SJordan K. Hubbard w = s; 1123a8617a8SJordan K. Hubbard SET_LOW_WORD(w,0); 1133a8617a8SJordan K. Hubbard c = (t-w*w)/(s+w); 1143a8617a8SJordan K. Hubbard r = p/q; 1153a8617a8SJordan K. Hubbard p = 2.0*s*r-(pio2_lo-2.0*c); 1163a8617a8SJordan K. Hubbard q = pio4_hi-2.0*w; 1173a8617a8SJordan K. Hubbard t = pio4_hi-(p-q); 1183a8617a8SJordan K. Hubbard } 1193a8617a8SJordan K. Hubbard if(hx>0) return t; else return -t; 1203a8617a8SJordan K. Hubbard } 121