atanf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*****************************************************************************/
  2. /*****************************************************************************/
  3. // atanf from musl-0.9.15
  4. /*****************************************************************************/
  5. /*****************************************************************************/
  6. /* origin: FreeBSD /usr/src/lib/msun/src/s_atanf.c */
  7. /*
  8. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  9. */
  10. /*
  11. * ====================================================
  12. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  13. *
  14. * Developed at SunPro, a Sun Microsystems, Inc. business.
  15. * Permission to use, copy, modify, and distribute this
  16. * software is freely granted, provided that this notice
  17. * is preserved.
  18. * ====================================================
  19. */
  20. #include "libm.h"
  21. static const float atanhi[] = {
  22. 4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */
  23. 7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */
  24. 9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */
  25. 1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */
  26. };
  27. static const float atanlo[] = {
  28. 5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */
  29. 3.7748947079e-08, /* atan(1.0)lo 0x33222168 */
  30. 3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */
  31. 7.5497894159e-08, /* atan(inf)lo 0x33a22168 */
  32. };
  33. static const float aT[] = {
  34. 3.3333328366e-01,
  35. -1.9999158382e-01,
  36. 1.4253635705e-01,
  37. -1.0648017377e-01,
  38. 6.1687607318e-02,
  39. };
  40. float atanf(float x)
  41. {
  42. float_t w,s1,s2,z;
  43. uint32_t ix,sign;
  44. int id;
  45. GET_FLOAT_WORD(ix, x);
  46. sign = ix>>31;
  47. ix &= 0x7fffffff;
  48. if (ix >= 0x4c800000) { /* if |x| >= 2**26 */
  49. if (isnan(x))
  50. return x;
  51. z = atanhi[3] + 0x1p-120f;
  52. return sign ? -z : z;
  53. }
  54. if (ix < 0x3ee00000) { /* |x| < 0.4375 */
  55. if (ix < 0x39800000) { /* |x| < 2**-12 */
  56. if (ix < 0x00800000)
  57. /* raise underflow for subnormal x */
  58. FORCE_EVAL(x*x);
  59. return x;
  60. }
  61. id = -1;
  62. } else {
  63. x = fabsf(x);
  64. if (ix < 0x3f980000) { /* |x| < 1.1875 */
  65. if (ix < 0x3f300000) { /* 7/16 <= |x| < 11/16 */
  66. id = 0;
  67. x = (2.0f*x - 1.0f)/(2.0f + x);
  68. } else { /* 11/16 <= |x| < 19/16 */
  69. id = 1;
  70. x = (x - 1.0f)/(x + 1.0f);
  71. }
  72. } else {
  73. if (ix < 0x401c0000) { /* |x| < 2.4375 */
  74. id = 2;
  75. x = (x - 1.5f)/(1.0f + 1.5f*x);
  76. } else { /* 2.4375 <= |x| < 2**26 */
  77. id = 3;
  78. x = -1.0f/x;
  79. }
  80. }
  81. }
  82. /* end of argument reduction */
  83. z = x*x;
  84. w = z*z;
  85. /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
  86. s1 = z*(aT[0]+w*(aT[2]+w*aT[4]));
  87. s2 = w*(aT[1]+w*aT[3]);
  88. if (id < 0)
  89. return x - x*(s1+s2);
  90. z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
  91. return sign ? -z : z;
  92. }