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