1 
2 /* @(#)e_atan2.c 1.3 95/01/18 */
3 /* FreeBSD: head/lib/msun/src/e_atan2.c 176451 2008-02-22 02:30:36Z das */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunSoft, 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  */
15 
16 
17 /*
18  * See comments in e_atan2.c.
19  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
20  */
21 
22 
23 #include "invtrig.h"
24 
25 static const long double tiny = 1.0e-300l;
26 static const long double zero = 0.0l;
27 static const long double pi   = _M_PI_L;
28 
29 long double
atan2l(long double y,long double x)30 atan2l(long double y, long double x)
31 {
32 	union IEEEl2bits ux, uy;
33 	long double z;
34 	int32_t k,m;
35 	int16_t exptx, expsignx, expty, expsigny;
36 
37 	uy.e = y;
38 	expsigny = uy.xbits.expsign;
39 	expty = expsigny & 0x7fff;
40 	ux.e = x;
41 	expsignx = ux.xbits.expsign;
42 	exptx = expsignx & 0x7fff;
43 
44 	if ((exptx==BIAS+LDBL_MAX_EXP &&
45 	     ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) ||	/* x is NaN */
46 	    (expty==BIAS+LDBL_MAX_EXP &&
47 	     ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0))	/* y is NaN */
48 	    return x+y;
49 	if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
50 	    return atanl(y);					/* x=1.0 */
51 	m = ((expsigny>>15)&1)|((expsignx>>14)&2);	/* 2*sign(x)+sign(y) */
52 
53     /* when y = 0 */
54 	if(expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
55 	    switch(m) {
56 		case 0:
57 		case 1: return y; 	/* atan(+-0,+anything)=+-0 */
58 		case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
59 		case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
60 	    }
61 	}
62     /* when x = 0 */
63 	if(exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
64 	    return (expsigny<0)?  -pio2_hi-tiny: pio2_hi+tiny;
65 
66     /* when x is INF */
67 	if(exptx==BIAS+LDBL_MAX_EXP) {
68 	    if(expty==BIAS+LDBL_MAX_EXP) {
69 		switch(m) {
70 		    case 0: return  pio2_hi*0.5l+tiny;/* atan(+INF,+INF) */
71 		    case 1: return -pio2_hi*0.5l-tiny;/* atan(-INF,+INF) */
72 		    case 2: return  1.5l*pio2_hi+tiny;/*atan(+INF,-INF)*/
73 		    case 3: return -1.5l*pio2_hi-tiny;/*atan(-INF,-INF)*/
74 		}
75 	    } else {
76 		switch(m) {
77 		    case 0: return  zero  ;	/* atan(+...,+INF) */
78 		    case 1: return -zero  ;	/* atan(-...,+INF) */
79 		    case 2: return  pi+tiny  ;	/* atan(+...,-INF) */
80 		    case 3: return -pi-tiny  ;	/* atan(-...,-INF) */
81 		}
82 	    }
83 	}
84     /* when y is INF */
85 	if(expty==BIAS+LDBL_MAX_EXP)
86 	    return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
87 
88     /* compute y/x */
89 	k = expty-exptx;
90 	if(k > LDBL_MANT_DIG+2) {			/* |y/x| huge */
91 	    z=pio2_hi+pio2_lo;
92 	    m&=1;
93 	}
94 	else if(expsignx<0&&k<-LDBL_MANT_DIG-2) z=0.0l; 	/* |y/x| tiny, x<0 */
95 	else z=atanl(check_uflowl(fabsl(y/x)));		/* safe to do y/x */
96 	switch (m) {
97 	    case 0: return       z  ;	/* atan(+,+) */
98 	    case 1: return      -z  ;	/* atan(-,+) */
99 	    case 2: return  pi-(z-pi_lo);/* atan(+,-) */
100 	    default: /* case 3 */
101 	    	    return  (z-pi_lo)-pi;/* atan(-,-) */
102 	}
103 }
104 
105 #if __LDBL_MANT_DIG__ == 113
106 #if defined(_HAVE_ALIAS_ATTRIBUTE)
107 #ifndef __clang__
108 #pragma GCC diagnostic ignored "-Wmissing-attributes"
109 #endif
110 __strong_reference(atan2l, __atan2ieee128);
111 #endif
112 #endif
113