1
0

atan2l.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. *
  12. */
  13. /*
  14. * See comments in atan2.c.
  15. * Converted to long double by David Schultz <[email protected]>.
  16. */
  17. #include "libm.h"
  18. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  19. long double atan2l(long double y, long double x)
  20. {
  21. return atan2(y, x);
  22. }
  23. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  24. #include "__invtrigl.h"
  25. long double atan2l(long double y, long double x)
  26. {
  27. union IEEEl2bits ux, uy;
  28. long double z;
  29. int32_t k,m;
  30. int16_t exptx, expsignx, expty, expsigny;
  31. uy.e = y;
  32. expsigny = uy.xbits.expsign;
  33. expty = expsigny & 0x7fff;
  34. ux.e = x;
  35. expsignx = ux.xbits.expsign;
  36. exptx = expsignx & 0x7fff;
  37. if ((exptx==0x7fff &&
  38. ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
  39. (expty==0x7fff &&
  40. ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* y is NaN */
  41. return x+y;
  42. if (expsignx==0x3fff && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0) /* x=1.0 */
  43. return atanl(y);
  44. m = ((expsigny>>15)&1) | ((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
  45. /* when y = 0 */
  46. if (expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
  47. switch(m) {
  48. case 0:
  49. case 1: return y; /* atan(+-0,+anything)=+-0 */
  50. case 2: return 2*pio2_hi+0x1p-120f; /* atan(+0,-anything) = pi */
  51. case 3: return -2*pio2_hi-0x1p-120f; /* atan(-0,-anything) =-pi */
  52. }
  53. }
  54. /* when x = 0 */
  55. if (exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
  56. return expsigny < 0 ? -pio2_hi-0x1p-120f : pio2_hi+0x1p-120f;
  57. /* when x is INF */
  58. if (exptx == 0x7fff) {
  59. if (expty == 0x7fff) {
  60. switch(m) {
  61. case 0: return pio2_hi*0.5+0x1p-120f; /* atan(+INF,+INF) */
  62. case 1: return -pio2_hi*0.5-0x1p-120f; /* atan(-INF,+INF) */
  63. case 2: return 1.5*pio2_hi+0x1p-120f; /* atan(+INF,-INF) */
  64. case 3: return -1.5*pio2_hi-0x1p-120f; /* atan(-INF,-INF) */
  65. }
  66. } else {
  67. switch(m) {
  68. case 0: return 0.0; /* atan(+...,+INF) */
  69. case 1: return -0.0; /* atan(-...,+INF) */
  70. case 2: return 2*pio2_hi+0x1p-120f; /* atan(+...,-INF) */
  71. case 3: return -2*pio2_hi-0x1p-120f; /* atan(-...,-INF) */
  72. }
  73. }
  74. }
  75. /* when y is INF */
  76. if (expty == 0x7fff)
  77. return expsigny < 0 ? -pio2_hi-0x1p-120f : pio2_hi+0x1p-120f;
  78. /* compute y/x */
  79. k = expty-exptx;
  80. if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
  81. z = pio2_hi+0x1p-120f;
  82. m &= 1;
  83. } else if (expsignx < 0 && k < -LDBL_MANT_DIG-2) /* |y/x| tiny, x<0 */
  84. z = 0.0;
  85. else /* safe to do y/x */
  86. z = atanl(fabsl(y/x));
  87. switch (m) {
  88. case 0: return z; /* atan(+,+) */
  89. case 1: return -z; /* atan(-,+) */
  90. case 2: return 2*pio2_hi-(z-2*pio2_lo); /* atan(+,-) */
  91. default: /* case 3 */
  92. return (z-2*pio2_lo)-2*pio2_hi; /* atan(-,-) */
  93. }
  94. }
  95. #endif