atan2f.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2f.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  4. */
  5. /*
  6. * ====================================================
  7. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * Developed at SunPro, a Sun Microsystems, Inc. business.
  10. * Permission to use, copy, modify, and distribute this
  11. * software is freely granted, provided that this notice
  12. * is preserved.
  13. * ====================================================
  14. */
  15. #include "libm.h"
  16. static const volatile float
  17. tiny = 1.0e-30;
  18. static const float
  19. zero = 0.0,
  20. pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */
  21. pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */
  22. pi = 3.1415927410e+00; /* 0x40490fdb */
  23. static const volatile float
  24. pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */
  25. float atan2f(float y, float x)
  26. {
  27. float z;
  28. int32_t k,m,hx,hy,ix,iy;
  29. GET_FLOAT_WORD(hx, x);
  30. ix = hx & 0x7fffffff;
  31. GET_FLOAT_WORD(hy, y);
  32. iy = hy & 0x7fffffff;
  33. if (ix > 0x7f800000 || iy > 0x7f800000) /* x or y is NaN */
  34. return x+y;
  35. if (hx == 0x3f800000) /* x=1.0 */
  36. return atanf(y);
  37. m = ((hy>>31)&1) | ((hx>>30)&2); /* 2*sign(x)+sign(y) */
  38. /* when y = 0 */
  39. if (iy == 0) {
  40. switch (m) {
  41. case 0:
  42. case 1: return y; /* atan(+-0,+anything)=+-0 */
  43. case 2: return pi+tiny; /* atan(+0,-anything) = pi */
  44. case 3: return -pi-tiny; /* atan(-0,-anything) =-pi */
  45. }
  46. }
  47. /* when x = 0 */
  48. if (ix == 0)
  49. return hy < 0 ? -pi_o_2-tiny : pi_o_2+tiny;
  50. /* when x is INF */
  51. if (ix == 0x7f800000) {
  52. if (iy == 0x7f800000) {
  53. switch (m) {
  54. case 0: return pi_o_4+tiny; /* atan(+INF,+INF) */
  55. case 1: return -pi_o_4-tiny; /* atan(-INF,+INF) */
  56. case 2: return 3.0f*pi_o_4+tiny; /*atan(+INF,-INF)*/
  57. case 3: return -3.0f*pi_o_4-tiny; /*atan(-INF,-INF)*/
  58. }
  59. } else {
  60. switch (m) {
  61. case 0: return zero; /* atan(+...,+INF) */
  62. case 1: return -zero; /* atan(-...,+INF) */
  63. case 2: return pi+tiny; /* atan(+...,-INF) */
  64. case 3: return -pi-tiny; /* atan(-...,-INF) */
  65. }
  66. }
  67. }
  68. /* when y is INF */
  69. if (iy == 0x7f800000)
  70. return hy < 0 ? -pi_o_2-tiny : pi_o_2+tiny;
  71. /* compute y/x */
  72. k = (iy-ix)>>23;
  73. if (k > 26) { /* |y/x| > 2**26 */
  74. z = pi_o_2 + 0.5f*pi_lo;
  75. m &= 1;
  76. } else if (k < -26 && hx < 0) /* 0 > |y|/x > -2**-26 */
  77. z = 0.0;
  78. else /* safe to do y/x */
  79. z = atanf(fabsf(y/x));
  80. switch (m) {
  81. case 0: return z; /* atan(+,+) */
  82. case 1: return -z; /* atan(-,+) */
  83. case 2: return pi - (z-pi_lo); /* atan(+,-) */
  84. default: /* case 3 */
  85. return (z-pi_lo) - pi; /* atan(-,-) */
  86. }
  87. }