e_atan2l.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* @(#)e_atan2.c 1.3 95/01/18 */
  2. /* FreeBSD: head/lib/msun/src/e_atan2.c 176451 2008-02-22 02:30:36Z das */
  3. /*
  4. * ====================================================
  5. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  6. *
  7. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  8. * Permission to use, copy, modify, and distribute this
  9. * software is freely granted, provided that this notice
  10. * is preserved.
  11. * ====================================================
  12. *
  13. */
  14. #include <sys/cdefs.h>
  15. /*
  16. * See comments in e_atan2.c.
  17. * Converted to long double by David Schultz <das@FreeBSD.ORG>.
  18. */
  19. #include <float.h>
  20. #include "invtrig.h"
  21. #include "openlibm.h"
  22. #include "math_private.h"
  23. static volatile long double
  24. tiny = 1.0e-300;
  25. static const long double
  26. zero = 0.0;
  27. #ifdef __i386__
  28. /* XXX Work around the fact that gcc truncates long double constants on i386 */
  29. static volatile double
  30. pi1 = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */
  31. pi2 = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
  32. #define pi ((long double)pi1 + pi2)
  33. #else
  34. static const long double
  35. pi = 3.14159265358979323846264338327950280e+00L;
  36. #endif
  37. long double
  38. atan2l(long double y, long double x)
  39. {
  40. union IEEEl2bits ux, uy;
  41. long double z;
  42. int32_t k,m;
  43. int16_t exptx, expsignx, expty, expsigny;
  44. uy.e = y;
  45. expsigny = uy.xbits.expsign;
  46. expty = expsigny & 0x7fff;
  47. ux.e = x;
  48. expsignx = ux.xbits.expsign;
  49. exptx = expsignx & 0x7fff;
  50. if ((exptx==BIAS+LDBL_MAX_EXP &&
  51. ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
  52. (expty==BIAS+LDBL_MAX_EXP &&
  53. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* y is NaN */
  54. return x+y;
  55. if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
  56. return atanl(y); /* x=1.0 */
  57. m = ((expsigny>>15)&1)|((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
  58. /* when y = 0 */
  59. if(expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
  60. switch(m) {
  61. case 0:
  62. case 1: return y; /* atan(+-0,+anything)=+-0 */
  63. case 2: return pi+tiny;/* atan(+0,-anything) = pi */
  64. case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
  65. }
  66. }
  67. /* when x = 0 */
  68. if(exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
  69. return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
  70. /* when x is INF */
  71. if(exptx==BIAS+LDBL_MAX_EXP) {
  72. if(expty==BIAS+LDBL_MAX_EXP) {
  73. switch(m) {
  74. case 0: return pio2_hi*0.5+tiny;/* atan(+INF,+INF) */
  75. case 1: return -pio2_hi*0.5-tiny;/* atan(-INF,+INF) */
  76. case 2: return 1.5*pio2_hi+tiny;/*atan(+INF,-INF)*/
  77. case 3: return -1.5*pio2_hi-tiny;/*atan(-INF,-INF)*/
  78. }
  79. } else {
  80. switch(m) {
  81. case 0: return zero ; /* atan(+...,+INF) */
  82. case 1: return -zero ; /* atan(-...,+INF) */
  83. case 2: return pi+tiny ; /* atan(+...,-INF) */
  84. case 3: return -pi-tiny ; /* atan(-...,-INF) */
  85. }
  86. }
  87. }
  88. /* when y is INF */
  89. if(expty==BIAS+LDBL_MAX_EXP)
  90. return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
  91. /* compute y/x */
  92. k = expty-exptx;
  93. if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
  94. z=pio2_hi+pio2_lo;
  95. m&=1;
  96. }
  97. else if(expsignx<0&&k<-LDBL_MANT_DIG-2) z=0.0; /* |y/x| tiny, x<0 */
  98. else z=atanl(fabsl(y/x)); /* safe to do y/x */
  99. switch (m) {
  100. case 0: return z ; /* atan(+,+) */
  101. case 1: return -z ; /* atan(-,+) */
  102. case 2: return pi-(z-pi_lo);/* atan(+,-) */
  103. default: /* case 3 */
  104. return (z-pi_lo)-pi;/* atan(-,-) */
  105. }
  106. }