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