atanl.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "__invtrigl.h"
  24. static const long double huge = 1.0e300;
  25. long double atanl(long double x)
  26. {
  27. union IEEEl2bits u;
  28. long double w,s1,s2,z;
  29. int id;
  30. int16_t expsign, expt;
  31. int32_t expman;
  32. u.e = x;
  33. expsign = u.xbits.expsign;
  34. expt = expsign & 0x7fff;
  35. if (expt >= ATAN_CONST) { /* if |x| is large, atan(x)~=pi/2 */
  36. if (expt == BIAS + LDBL_MAX_EXP &&
  37. ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)!=0) /* NaN */
  38. return x+x;
  39. if (expsign > 0)
  40. return atanhi[3]+atanlo[3];
  41. else
  42. return -atanhi[3]-atanlo[3];
  43. }
  44. /* Extract the exponent and the first few bits of the mantissa. */
  45. /* XXX There should be a more convenient way to do this. */
  46. expman = (expt << 8) | ((u.bits.manh >> (MANH_SIZE - 9)) & 0xff);
  47. if (expman < ((BIAS - 2) << 8) + 0xc0) { /* |x| < 0.4375 */
  48. if (expt < ATAN_LINEAR) { /* if |x| is small, atanl(x)~=x */
  49. /* raise inexact */
  50. if (huge+x > 1.0)
  51. return x;
  52. }
  53. id = -1;
  54. } else {
  55. x = fabsl(x);
  56. if (expman < (BIAS << 8) + 0x30) { /* |x| < 1.1875 */
  57. if (expman < ((BIAS - 1) << 8) + 0x60) { /* 7/16 <= |x| < 11/16 */
  58. id = 0;
  59. x = (2.0*x-1.0)/(2.0+x);
  60. } else { /* 11/16 <= |x| < 19/16 */
  61. id = 1;
  62. x = (x-1.0)/(x+1.0);
  63. }
  64. } else {
  65. if (expman < ((BIAS + 1) << 8) + 0x38) { /* |x| < 2.4375 */
  66. id = 2;
  67. x = (x-1.5)/(1.0+1.5*x);
  68. } else { /* 2.4375 <= |x| < 2^ATAN_CONST */
  69. id = 3;
  70. x = -1.0/x;
  71. }
  72. }
  73. }
  74. /* end of argument reduction */
  75. z = x*x;
  76. w = z*z;
  77. /* break sum aT[i]z**(i+1) into odd and even poly */
  78. s1 = z*T_even(w);
  79. s2 = w*T_odd(w);
  80. if (id < 0)
  81. return x - x*(s1+s2);
  82. z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
  83. return expsign < 0 ? -z : z;
  84. }
  85. #endif