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