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