atanl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_atanl.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. /*
  13. * See comments in atan.c.
  14. * Converted to long double by David Schultz <[email protected]>.
  15. */
  16. #include "libm.h"
  17. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  18. long double atanl(long double x)
  19. {
  20. return atan(x);
  21. }
  22. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  23. #if LDBL_MANT_DIG == 64
  24. #define EXPMAN(u) ((u.i.se & 0x7fff)<<8 | (u.i.m>>55 & 0xff))
  25. static const long double atanhi[] = {
  26. 4.63647609000806116202e-01L,
  27. 7.85398163397448309628e-01L,
  28. 9.82793723247329067960e-01L,
  29. 1.57079632679489661926e+00L,
  30. };
  31. static const long double atanlo[] = {
  32. 1.18469937025062860669e-20L,
  33. -1.25413940316708300586e-20L,
  34. 2.55232234165405176172e-20L,
  35. -2.50827880633416601173e-20L,
  36. };
  37. static const long double aT[] = {
  38. 3.33333333333333333017e-01L,
  39. -1.99999999999999632011e-01L,
  40. 1.42857142857046531280e-01L,
  41. -1.11111111100562372733e-01L,
  42. 9.09090902935647302252e-02L,
  43. -7.69230552476207730353e-02L,
  44. 6.66661718042406260546e-02L,
  45. -5.88158892835030888692e-02L,
  46. 5.25499891539726639379e-02L,
  47. -4.70119845393155721494e-02L,
  48. 4.03539201366454414072e-02L,
  49. -2.91303858419364158725e-02L,
  50. 1.24822046299269234080e-02L,
  51. };
  52. static long double T_even(long double x)
  53. {
  54. return aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] +
  55. x * (aT[8] + x * (aT[10] + x * aT[12])))));
  56. }
  57. static long double T_odd(long double x)
  58. {
  59. return aT[1] + x * (aT[3] + x * (aT[5] + x * (aT[7] +
  60. x * (aT[9] + x * aT[11]))));
  61. }
  62. #elif LDBL_MANT_DIG == 113
  63. #define EXPMAN(u) ((u.i.se & 0x7fff)<<8 | u.i.top>>8)
  64. const long double atanhi[] = {
  65. 4.63647609000806116214256231461214397e-01L,
  66. 7.85398163397448309615660845819875699e-01L,
  67. 9.82793723247329067985710611014666038e-01L,
  68. 1.57079632679489661923132169163975140e+00L,
  69. };
  70. const long double atanlo[] = {
  71. 4.89509642257333492668618435220297706e-36L,
  72. 2.16795253253094525619926100651083806e-35L,
  73. -2.31288434538183565909319952098066272e-35L,
  74. 4.33590506506189051239852201302167613e-35L,
  75. };
  76. const long double aT[] = {
  77. 3.33333333333333333333333333333333125e-01L,
  78. -1.99999999999999999999999999999180430e-01L,
  79. 1.42857142857142857142857142125269827e-01L,
  80. -1.11111111111111111111110834490810169e-01L,
  81. 9.09090909090909090908522355708623681e-02L,
  82. -7.69230769230769230696553844935357021e-02L,
  83. 6.66666666666666660390096773046256096e-02L,
  84. -5.88235294117646671706582985209643694e-02L,
  85. 5.26315789473666478515847092020327506e-02L,
  86. -4.76190476189855517021024424991436144e-02L,
  87. 4.34782608678695085948531993458097026e-02L,
  88. -3.99999999632663469330634215991142368e-02L,
  89. 3.70370363987423702891250829918659723e-02L,
  90. -3.44827496515048090726669907612335954e-02L,
  91. 3.22579620681420149871973710852268528e-02L,
  92. -3.03020767654269261041647570626778067e-02L,
  93. 2.85641979882534783223403715930946138e-02L,
  94. -2.69824879726738568189929461383741323e-02L,
  95. 2.54194698498808542954187110873675769e-02L,
  96. -2.35083879708189059926183138130183215e-02L,
  97. 2.04832358998165364349957325067131428e-02L,
  98. -1.54489555488544397858507248612362957e-02L,
  99. 8.64492360989278761493037861575248038e-03L,
  100. -2.58521121597609872727919154569765469e-03L,
  101. };
  102. static long double T_even(long double x)
  103. {
  104. return (aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] + x * (aT[8] +
  105. x * (aT[10] + x * (aT[12] + x * (aT[14] + x * (aT[16] +
  106. x * (aT[18] + x * (aT[20] + x * aT[22])))))))))));
  107. }
  108. static long double T_odd(long double x)
  109. {
  110. return (aT[1] + x * (aT[3] + x * (aT[5] + x * (aT[7] + x * (aT[9] +
  111. x * (aT[11] + x * (aT[13] + x * (aT[15] + x * (aT[17] +
  112. x * (aT[19] + x * (aT[21] + x * aT[23])))))))))));
  113. }
  114. #endif
  115. long double atanl(long double x)
  116. {
  117. union ldshape u = {x};
  118. long double w, s1, s2, z;
  119. int id;
  120. unsigned e = u.i.se & 0x7fff;
  121. unsigned sign = u.i.se >> 15;
  122. unsigned expman;
  123. if (e >= 0x3fff + LDBL_MANT_DIG + 1) { /* if |x| is large, atan(x)~=pi/2 */
  124. if (isnan(x))
  125. return x;
  126. return sign ? -atanhi[3] : atanhi[3];
  127. }
  128. /* Extract the exponent and the first few bits of the mantissa. */
  129. expman = EXPMAN(u);
  130. if (expman < ((0x3fff - 2) << 8) + 0xc0) { /* |x| < 0.4375 */
  131. if (e < 0x3fff - (LDBL_MANT_DIG+1)/2) { /* if |x| is small, atanl(x)~=x */
  132. /* raise underflow if subnormal */
  133. if (e == 0)
  134. FORCE_EVAL((float)x);
  135. return x;
  136. }
  137. id = -1;
  138. } else {
  139. x = fabsl(x);
  140. if (expman < (0x3fff << 8) + 0x30) { /* |x| < 1.1875 */
  141. if (expman < ((0x3fff - 1) << 8) + 0x60) { /* 7/16 <= |x| < 11/16 */
  142. id = 0;
  143. x = (2.0*x-1.0)/(2.0+x);
  144. } else { /* 11/16 <= |x| < 19/16 */
  145. id = 1;
  146. x = (x-1.0)/(x+1.0);
  147. }
  148. } else {
  149. if (expman < ((0x3fff + 1) << 8) + 0x38) { /* |x| < 2.4375 */
  150. id = 2;
  151. x = (x-1.5)/(1.0+1.5*x);
  152. } else { /* 2.4375 <= |x| */
  153. id = 3;
  154. x = -1.0/x;
  155. }
  156. }
  157. }
  158. /* end of argument reduction */
  159. z = x*x;
  160. w = z*z;
  161. /* break sum aT[i]z**(i+1) into odd and even poly */
  162. s1 = z*T_even(w);
  163. s2 = w*T_odd(w);
  164. if (id < 0)
  165. return x - x*(s1+s2);
  166. z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
  167. return sign ? -z : z;
  168. }
  169. #endif