e_asin.c 3.7 KB

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