13f708241SDavid Schultz 23f708241SDavid Schultz /* @(#)e_asin.c 1.3 95/01/18 */ 33a8617a8SJordan K. Hubbard /* 43a8617a8SJordan K. Hubbard * ==================================================== 53a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 63a8617a8SJordan K. Hubbard * 73f708241SDavid Schultz * Developed at SunSoft, a Sun Microsystems, Inc. business. 83a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this 93a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice 103a8617a8SJordan K. Hubbard * is preserved. 113a8617a8SJordan K. Hubbard * ==================================================== 123a8617a8SJordan K. Hubbard */ 133a8617a8SJordan K. Hubbard 145aa554c7SDavid Schultz #include <sys/cdefs.h> 155aa554c7SDavid Schultz __FBSDID("$FreeBSD$"); 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 4717303c62SDavid Schultz #include <float.h> 483a8617a8SJordan K. Hubbard 493a8617a8SJordan K. Hubbard #include "math.h" 503a8617a8SJordan K. Hubbard #include "math_private.h" 513a8617a8SJordan K. Hubbard 523a8617a8SJordan K. Hubbard static const double 533a8617a8SJordan K. Hubbard one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ 543a8617a8SJordan K. Hubbard huge = 1.000e+300, 553a8617a8SJordan K. Hubbard pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */ 563a8617a8SJordan K. Hubbard pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */ 573a8617a8SJordan K. Hubbard pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */ 583a8617a8SJordan K. Hubbard /* coefficient for R(x^2) */ 593a8617a8SJordan K. Hubbard pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */ 603a8617a8SJordan K. Hubbard pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */ 613a8617a8SJordan K. Hubbard pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */ 623a8617a8SJordan K. Hubbard pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */ 633a8617a8SJordan K. Hubbard pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */ 643a8617a8SJordan K. Hubbard pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */ 653a8617a8SJordan K. Hubbard qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */ 663a8617a8SJordan K. Hubbard qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */ 673a8617a8SJordan K. Hubbard qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */ 683a8617a8SJordan K. Hubbard qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */ 693a8617a8SJordan K. Hubbard 70a82bbc73SAlfred Perlstein double 713819e840SPeter Wemm __ieee754_asin(double x) 723a8617a8SJordan K. Hubbard { 7351295a4dSJordan K. Hubbard double t=0.0,w,p,q,c,r,s; 743a8617a8SJordan K. Hubbard int32_t hx,ix; 753a8617a8SJordan K. Hubbard GET_HIGH_WORD(hx,x); 763a8617a8SJordan K. Hubbard ix = hx&0x7fffffff; 773a8617a8SJordan K. Hubbard if(ix>= 0x3ff00000) { /* |x|>= 1 */ 783a8617a8SJordan K. Hubbard u_int32_t lx; 793a8617a8SJordan K. Hubbard GET_LOW_WORD(lx,x); 803a8617a8SJordan K. Hubbard if(((ix-0x3ff00000)|lx)==0) 813a8617a8SJordan K. Hubbard /* asin(1)=+-pi/2 with inexact */ 823a8617a8SJordan K. Hubbard return x*pio2_hi+x*pio2_lo; 833a8617a8SJordan K. Hubbard return (x-x)/(x-x); /* asin(|x|>1) is NaN */ 843a8617a8SJordan K. Hubbard } else if (ix<0x3fe00000) { /* |x|<0.5 */ 85*e044d80dSDavid Schultz if(ix<0x3e500000) { /* if |x| < 2**-26 */ 863a8617a8SJordan K. Hubbard if(huge+x>one) return x;/* return x with inexact if x!=0*/ 87e142bd0eSDavid Schultz } 883a8617a8SJordan K. Hubbard t = x*x; 893a8617a8SJordan K. Hubbard p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); 903a8617a8SJordan K. Hubbard q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); 913a8617a8SJordan K. Hubbard w = p/q; 923a8617a8SJordan K. Hubbard return x+x*w; 933a8617a8SJordan K. Hubbard } 943a8617a8SJordan K. Hubbard /* 1> |x|>= 0.5 */ 953a8617a8SJordan K. Hubbard w = one-fabs(x); 963a8617a8SJordan K. Hubbard t = w*0.5; 973a8617a8SJordan K. Hubbard p = t*(pS0+t*(pS1+t*(pS2+t*(pS3+t*(pS4+t*pS5))))); 983a8617a8SJordan K. Hubbard q = one+t*(qS1+t*(qS2+t*(qS3+t*qS4))); 993f708241SDavid Schultz s = sqrt(t); 1003a8617a8SJordan K. Hubbard if(ix>=0x3FEF3333) { /* if |x| > 0.975 */ 1013a8617a8SJordan K. Hubbard w = p/q; 1023a8617a8SJordan K. Hubbard t = pio2_hi-(2.0*(s+s*w)-pio2_lo); 1033a8617a8SJordan K. Hubbard } else { 1043a8617a8SJordan K. Hubbard w = s; 1053a8617a8SJordan K. Hubbard SET_LOW_WORD(w,0); 1063a8617a8SJordan K. Hubbard c = (t-w*w)/(s+w); 1073a8617a8SJordan K. Hubbard r = p/q; 1083a8617a8SJordan K. Hubbard p = 2.0*s*r-(pio2_lo-2.0*c); 1093a8617a8SJordan K. Hubbard q = pio4_hi-2.0*w; 1103a8617a8SJordan K. Hubbard t = pio4_hi-(p-q); 1113a8617a8SJordan K. Hubbard } 1123a8617a8SJordan K. Hubbard if(hx>0) return t; else return -t; 1133a8617a8SJordan K. Hubbard } 11417303c62SDavid Schultz 11517303c62SDavid Schultz #if LDBL_MANT_DIG == 53 11617303c62SDavid Schultz __weak_reference(asin, asinl); 11717303c62SDavid Schultz #endif 118