1 /* e_atan2l.c -- long double version of e_atan2.c.
2  */
3 
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunPro, 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 /* __ieee754_atan2l(y,x)
16  * Method :
17  *	1. Reduce y to positive by atan2l(y,x)=-atan2l(-y,x).
18  *	2. Reduce x to positive by (if x and y are unexceptional):
19  *		ARG (x+iy) = arctan(y/x)	   ... if x > 0,
20  *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
21  *
22  * Special cases:
23  *
24  *	ATAN2((anything), NaN ) is NaN;
25  *	ATAN2(NAN , (anything) ) is NaN;
26  *	ATAN2(+-0, +(anything but NaN)) is +-0  ;
27  *	ATAN2(+-0, -(anything but NaN)) is +-pi ;
28  *	ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
29  *	ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
30  *	ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
31  *	ATAN2(+-INF,+INF ) is +-pi/4 ;
32  *	ATAN2(+-INF,-INF ) is +-3pi/4;
33  *	ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
34  *
35  * Constants:
36  * The hexadecimal values are the intended ones for the following
37  * constants. The decimal values may be used, provided that the
38  * compiler will convert from decimal to binary accurately enough
39  * to produce the hexadecimal values shown.
40  */
41 
42 #include <math.h>
43 #include <math_private.h>
44 #include <libm-alias-finite.h>
45 
46 static const long double
47 tiny  = 1.0e-300L,
48 zero  = 0.0,
49 pi_o_4  = 7.85398163397448309615660845819875699e-01L, /* 3ffe921fb54442d18469898cc51701b8 */
50 pi_o_2  = 1.57079632679489661923132169163975140e+00L, /* 3fff921fb54442d18469898cc51701b8 */
51 pi      = 3.14159265358979323846264338327950280e+00L, /* 4000921fb54442d18469898cc51701b8 */
52 pi_lo   = 8.67181013012378102479704402604335225e-35L; /* 3f8dcd129024e088a67cc74020bbea64 */
53 
54 long double
__ieee754_atan2l(long double y,long double x)55 __ieee754_atan2l(long double y, long double x)
56 {
57 	long double z;
58 	int64_t k,m,hx,hy,ix,iy;
59 	uint64_t lx;
60 	double xhi, xlo, yhi;
61 
62 	ldbl_unpack (x, &xhi, &xlo);
63 	EXTRACT_WORDS64 (hx, xhi);
64 	EXTRACT_WORDS64 (lx, xlo);
65 	ix = hx&0x7fffffffffffffffLL;
66 	yhi = ldbl_high (y);
67 	EXTRACT_WORDS64 (hy, yhi);
68 	iy = hy&0x7fffffffffffffffLL;
69 	if(((ix)>0x7ff0000000000000LL)||
70 	   ((iy)>0x7ff0000000000000LL))	/* x or y is NaN */
71 	   return x+y;
72 	if(((hx-0x3ff0000000000000LL))==0
73 	   && (lx&0x7fffffffffffffff)==0) return __atanl(y);   /* x=1.0L */
74 	m = ((hy>>63)&1)|((hx>>62)&2);	/* 2*sign(x)+sign(y) */
75 
76     /* when y = 0 */
77 	if(iy==0) {
78 	    switch(m) {
79 		case 0:
80 		case 1: return y;	/* atan(+-0,+anything)=+-0 */
81 		case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
82 		case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
83 	    }
84 	}
85     /* when x = 0 */
86 	if(ix==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
87 
88     /* when x is INF */
89 	if(ix==0x7ff0000000000000LL) {
90 	    if(iy==0x7ff0000000000000LL) {
91 		switch(m) {
92 		    case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
93 		    case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
94 		    case 2: return  3.0L*pi_o_4+tiny;/*atan(+INF,-INF)*/
95 		    case 3: return -3.0L*pi_o_4-tiny;/*atan(-INF,-INF)*/
96 		}
97 	    } else {
98 		switch(m) {
99 		    case 0: return  zero  ;	/* atan(+...,+INF) */
100 		    case 1: return -zero  ;	/* atan(-...,+INF) */
101 		    case 2: return  pi+tiny  ;	/* atan(+...,-INF) */
102 		    case 3: return -pi-tiny  ;	/* atan(-...,-INF) */
103 		}
104 	    }
105 	}
106     /* when y is INF */
107 	if(iy==0x7ff0000000000000LL) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
108 
109     /* compute y/x */
110 	k = (iy-ix)>>52;
111 	if(k > 120) z=pi_o_2+0.5L*pi_lo;	/* |y/x| >  2**120 */
112 	else if(hx<0&&k<-120) z=0.0L;		/* |y|/x < -2**120 */
113 	else z=__atanl(fabsl(y/x));		/* safe to do y/x */
114 	switch (m) {
115 	    case 0: return       z  ;	/* atan(+,+) */
116 	    case 1: return      -z  ;	/* atan(-,+) */
117 	    case 2: return  pi-(z-pi_lo);/* atan(+,-) */
118 	    default: /* case 3 */
119 		    return  (z-pi_lo)-pi;/* atan(-,-) */
120 	}
121 }
122 libm_alias_finite (__ieee754_atan2l, __atan2l)
123