e_atan2l.c 3.5 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 "cdefs-compat.h"
  15. //__FBSDID("$FreeBSD: src/lib/msun/src/e_atan2l.c,v 1.3 2008/08/02 19:17:00 das Exp $");
  16. /*
  17. * See comments in e_atan2.c.
  18. * Converted to long double by David Schultz <[email protected]>.
  19. */
  20. #include <float.h>
  21. #include "invtrig.h"
  22. #include "openlibm.h"
  23. #include "math_private.h"
  24. static volatile long double
  25. tiny = 1.0e-300;
  26. static const long double
  27. zero = 0.0;
  28. #ifdef __i386__
  29. /* XXX Work around the fact that gcc truncates long double constants on i386 */
  30. static volatile double
  31. pi1 = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */
  32. pi2 = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
  33. #define pi ((long double)pi1 + pi2)
  34. #else
  35. static const long double
  36. pi = 3.14159265358979323846264338327950280e+00L;
  37. #endif
  38. DLLEXPORT long double
  39. atan2l(long double y, long double x)
  40. {
  41. union IEEEl2bits ux, uy;
  42. long double z;
  43. int32_t k,m;
  44. int16_t exptx, expsignx, expty, expsigny;
  45. uy.e = y;
  46. expsigny = uy.xbits.expsign;
  47. expty = expsigny & 0x7fff;
  48. ux.e = x;
  49. expsignx = ux.xbits.expsign;
  50. exptx = expsignx & 0x7fff;
  51. if ((exptx==BIAS+LDBL_MAX_EXP &&
  52. ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
  53. (expty==BIAS+LDBL_MAX_EXP &&
  54. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* y is NaN */
  55. return x+y;
  56. if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
  57. return atanl(y); /* x=1.0 */
  58. m = ((expsigny>>15)&1)|((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
  59. /* when y = 0 */
  60. if(expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
  61. switch(m) {
  62. case 0:
  63. case 1: return y; /* atan(+-0,+anything)=+-0 */
  64. case 2: return pi+tiny;/* atan(+0,-anything) = pi */
  65. case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
  66. }
  67. }
  68. /* when x = 0 */
  69. if(exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
  70. return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
  71. /* when x is INF */
  72. if(exptx==BIAS+LDBL_MAX_EXP) {
  73. if(expty==BIAS+LDBL_MAX_EXP) {
  74. switch(m) {
  75. case 0: return pio2_hi*0.5+tiny;/* atan(+INF,+INF) */
  76. case 1: return -pio2_hi*0.5-tiny;/* atan(-INF,+INF) */
  77. case 2: return 1.5*pio2_hi+tiny;/*atan(+INF,-INF)*/
  78. case 3: return -1.5*pio2_hi-tiny;/*atan(-INF,-INF)*/
  79. }
  80. } else {
  81. switch(m) {
  82. case 0: return zero ; /* atan(+...,+INF) */
  83. case 1: return -zero ; /* atan(-...,+INF) */
  84. case 2: return pi+tiny ; /* atan(+...,-INF) */
  85. case 3: return -pi-tiny ; /* atan(-...,-INF) */
  86. }
  87. }
  88. }
  89. /* when y is INF */
  90. if(expty==BIAS+LDBL_MAX_EXP)
  91. return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
  92. /* compute y/x */
  93. k = expty-exptx;
  94. if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
  95. z=pio2_hi+pio2_lo;
  96. m&=1;
  97. }
  98. else if(expsignx<0&&k<-LDBL_MANT_DIG-2) z=0.0; /* |y/x| tiny, x<0 */
  99. else z=atanl(fabsl(y/x)); /* safe to do y/x */
  100. switch (m) {
  101. case 0: return z ; /* atan(+,+) */
  102. case 1: return -z ; /* atan(-,+) */
  103. case 2: return pi-(z-pi_lo);/* atan(+,-) */
  104. default: /* case 3 */
  105. return (z-pi_lo)-pi;/* atan(-,-) */
  106. }
  107. }