atan2l.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, 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. /*
  14. * See comments in atan2.c.
  15. * Converted to long double by David Schultz <[email protected]>.
  16. */
  17. #include "libm.h"
  18. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  19. long double atan2l(long double y, long double x)
  20. {
  21. return atan2(y, x);
  22. }
  23. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  24. #include "__invtrigl.h"
  25. static const volatile long double
  26. tiny = 1.0e-300;
  27. static const long double
  28. pi = 3.14159265358979323846264338327950280e+00L;
  29. long double atan2l(long double y, long double x)
  30. {
  31. union IEEEl2bits ux, uy;
  32. long double z;
  33. int32_t k,m;
  34. int16_t exptx, expsignx, expty, expsigny;
  35. uy.e = y;
  36. expsigny = uy.xbits.expsign;
  37. expty = expsigny & 0x7fff;
  38. ux.e = x;
  39. expsignx = ux.xbits.expsign;
  40. exptx = expsignx & 0x7fff;
  41. if ((exptx==BIAS+LDBL_MAX_EXP &&
  42. ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
  43. (expty==BIAS+LDBL_MAX_EXP &&
  44. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* y is NaN */
  45. return x+y;
  46. if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0) /* x=1.0 */
  47. return atanl(y);
  48. m = ((expsigny>>15)&1) | ((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
  49. /* when y = 0 */
  50. if (expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
  51. switch(m) {
  52. case 0:
  53. case 1: return y; /* atan(+-0,+anything)=+-0 */
  54. case 2: return pi+tiny; /* atan(+0,-anything) = pi */
  55. case 3: return -pi-tiny; /* atan(-0,-anything) =-pi */
  56. }
  57. }
  58. /* when x = 0 */
  59. if (exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
  60. return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
  61. /* when x is INF */
  62. if (exptx == BIAS+LDBL_MAX_EXP) {
  63. if (expty == BIAS+LDBL_MAX_EXP) {
  64. switch(m) {
  65. case 0: return pio2_hi*0.5+tiny; /* atan(+INF,+INF) */
  66. case 1: return -pio2_hi*0.5-tiny; /* atan(-INF,+INF) */
  67. case 2: return 1.5*pio2_hi+tiny; /*atan(+INF,-INF)*/
  68. case 3: return -1.5*pio2_hi-tiny; /*atan(-INF,-INF)*/
  69. }
  70. } else {
  71. switch(m) {
  72. case 0: return 0.0; /* atan(+...,+INF) */
  73. case 1: return -0.0; /* atan(-...,+INF) */
  74. case 2: return pi+tiny; /* atan(+...,-INF) */
  75. case 3: return -pi-tiny; /* atan(-...,-INF) */
  76. }
  77. }
  78. }
  79. /* when y is INF */
  80. if (expty == BIAS+LDBL_MAX_EXP)
  81. return expsigny < 0 ? -pio2_hi-tiny : pio2_hi+tiny;
  82. /* compute y/x */
  83. k = expty-exptx;
  84. if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
  85. z = pio2_hi+pio2_lo;
  86. m &= 1;
  87. } else if (expsignx < 0 && k < -LDBL_MANT_DIG-2) /* |y/x| tiny, x<0 */
  88. z = 0.0;
  89. else /* safe to do y/x */
  90. z = atanl(fabsl(y/x));
  91. switch (m) {
  92. case 0: return z; /* atan(+,+) */
  93. case 1: return -z; /* atan(-,+) */
  94. case 2: return pi - (z-pi_lo); /* atan(+,-) */
  95. default: /* case 3 */
  96. return (z-pi_lo) - pi; /* atan(-,-) */
  97. }
  98. }
  99. #endif