e_atan2f.c 2.7 KB

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