e_atan2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /* __ieee754_atan2(y,x)
  12. * Method :
  13. * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
  14. * 2. Reduce x to positive by (if x and y are unexceptional):
  15. * ARG (x+iy) = arctan(y/x) ... if x > 0,
  16. * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
  17. *
  18. * Special cases:
  19. *
  20. * ATAN2((anything), NaN ) is NaN;
  21. * ATAN2(NAN , (anything) ) is NaN;
  22. * ATAN2(+-0, +(anything but NaN)) is +-0 ;
  23. * ATAN2(+-0, -(anything but NaN)) is +-pi ;
  24. * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
  25. * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
  26. * ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
  27. * ATAN2(+-INF,+INF ) is +-pi/4 ;
  28. * ATAN2(+-INF,-INF ) is +-3pi/4;
  29. * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
  30. *
  31. * Constants:
  32. * The hexadecimal values are the intended ones for the following
  33. * constants. The decimal values may be used, provided that the
  34. * compiler will convert from decimal to binary accurately enough
  35. * to produce the hexadecimal values shown.
  36. */
  37. #include "math.h"
  38. #include "math_private.h"
  39. static const double
  40. tiny = 1.0e-300,
  41. zero = 0.0,
  42. pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
  43. pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
  44. pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
  45. pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
  46. double __ieee754_atan2(double y, double x)
  47. {
  48. double z;
  49. int32_t k,m,hx,hy,ix,iy;
  50. u_int32_t lx,ly;
  51. EXTRACT_WORDS(hx,lx,x);
  52. ix = hx&0x7fffffff;
  53. EXTRACT_WORDS(hy,ly,y);
  54. iy = hy&0x7fffffff;
  55. if(((ix|((lx|-lx)>>31))>0x7ff00000)||
  56. ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */
  57. return x+y;
  58. if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */
  59. m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */
  60. /* when y = 0 */
  61. if((iy|ly)==0) {
  62. switch(m) {
  63. case 0:
  64. case 1: return y; /* atan(+-0,+anything)=+-0 */
  65. case 2: return pi+tiny;/* atan(+0,-anything) = pi */
  66. case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
  67. }
  68. }
  69. /* when x = 0 */
  70. if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  71. /* when x is INF */
  72. if(ix==0x7ff00000) {
  73. if(iy==0x7ff00000) {
  74. switch(m) {
  75. case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */
  76. case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
  77. case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
  78. case 3: return -3.0*pi_o_4-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(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
  91. /* compute y/x */
  92. k = (iy-ix)>>20;
  93. if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */
  94. else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */
  95. else z=atan(fabs(y/x)); /* safe to do y/x */
  96. switch (m) {
  97. case 0: return z ; /* atan(+,+) */
  98. case 1: {
  99. u_int32_t zh;
  100. GET_HIGH_WORD(zh,z);
  101. SET_HIGH_WORD(z,zh ^ 0x80000000);
  102. }
  103. return z ; /* atan(-,+) */
  104. case 2: return pi-(z-pi_lo);/* atan(+,-) */
  105. default: /* case 3 */
  106. return (z-pi_lo)-pi;/* atan(-,-) */
  107. }
  108. }
  109. /*
  110. * wrapper atan2(y,x)
  111. */
  112. #ifndef _IEEE_LIBM
  113. double atan2(double y, double x)
  114. {
  115. double z = __ieee754_atan2(y, x);
  116. if (_LIB_VERSION == _IEEE_ || isnan(x) || isnan(y))
  117. return z;
  118. if (x == 0.0 && y == 0.0)
  119. return __kernel_standard(y,x,3); /* atan2(+-0,+-0) */
  120. return z;
  121. }
  122. #else
  123. strong_alias(__ieee754_atan2, atan2)
  124. #endif
  125. libm_hidden_def(atan2)