2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
9 * ====================================================
13 static char rcsid
[] = "$FreeBSD: src/lib/msun/src/e_atan2l.c,v 1.3 2008/08/02 19:17:00 das Exp $";
20 #include "math_private.h"
22 static const volatile long double
23 tiny
__attribute__ ((__section__(".rodata,\"a\" " SECTIONCOMMENT
))) = 1.0e-300;
24 static const long double
28 /* XXX Work around the fact that gcc truncates long double constants on i386 */
29 static const volatile double
30 pi1
__attribute__ ((__section__(".rodata,\"a\" " SECTIONCOMMENT
))) = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */
31 pi2
__attribute__ ((__section__(".rodata,\"a\" " SECTIONCOMMENT
))) = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
32 #define pi ((long double)pi1 + pi2)
34 static const long double
35 pi
= 3.14159265358979323846264338327950280e+00L;
39 atan2l(long double y
, long double x
)
41 union IEEEl2bits ux
, uy
;
44 int16_t exptx
, expsignx
, expty
, expsigny
;
47 expsigny
= uy
.xbits
.expsign
;
48 expty
= expsigny
& 0x7fff;
50 expsignx
= ux
.xbits
.expsign
;
51 exptx
= expsignx
& 0x7fff;
53 if ((exptx
==BIAS
+LDBL_MAX_EXP
&&
54 ((ux
.bits
.manh
&~LDBL_NBIT
)|ux
.bits
.manl
)!=0) || /* x is NaN */
55 (expty
==BIAS
+LDBL_MAX_EXP
&&
56 ((uy
.bits
.manh
&~LDBL_NBIT
)|uy
.bits
.manl
)!=0)) /* y is NaN */
58 if (expsignx
==BIAS
&& ((ux
.bits
.manh
&~LDBL_NBIT
)|ux
.bits
.manl
)==0)
59 return atanl(y
); /* x=1.0 */
60 m
= ((expsigny
>>15)&1)|((expsignx
>>14)&2); /* 2*sign(x)+sign(y) */
63 if(expty
==0 && ((uy
.bits
.manh
&~LDBL_NBIT
)|uy
.bits
.manl
)==0) {
66 case 1: return y
; /* atan(+-0,+anything)=+-0 */
67 case 2: return pi
+tiny
;/* atan(+0,-anything) = pi */
68 case 3: return -pi
-tiny
;/* atan(-0,-anything) =-pi */
72 if(exptx
==0 && ((ux
.bits
.manh
&~LDBL_NBIT
)|ux
.bits
.manl
)==0)
73 return (expsigny
<0)? -pio2_hi
-tiny
: pio2_hi
+tiny
;
76 if(exptx
==BIAS
+LDBL_MAX_EXP
) {
77 if(expty
==BIAS
+LDBL_MAX_EXP
) {
79 case 0: return pio2_hi
*0.5+tiny
;/* atan(+INF,+INF) */
80 case 1: return -pio2_hi
*0.5-tiny
;/* atan(-INF,+INF) */
81 case 2: return 1.5*pio2_hi
+tiny
;/*atan(+INF,-INF)*/
82 case 3: return -1.5*pio2_hi
-tiny
;/*atan(-INF,-INF)*/
86 case 0: return zero
; /* atan(+...,+INF) */
87 case 1: return -zero
; /* atan(-...,+INF) */
88 case 2: return pi
+tiny
; /* atan(+...,-INF) */
89 case 3: return -pi
-tiny
; /* atan(-...,-INF) */
94 if(expty
==BIAS
+LDBL_MAX_EXP
)
95 return (expsigny
<0)? -pio2_hi
-tiny
: pio2_hi
+tiny
;
99 if(k
> LDBL_MANT_DIG
+2) { /* |y/x| huge */
103 else if(expsignx
<0&&k
<-LDBL_MANT_DIG
-2) z
=0.0; /* |y/x| tiny, x<0 */
104 else z
=atanl(fabsl(y
/x
)); /* safe to do y/x */
106 case 0: return z
; /* atan(+,+) */
107 case 1: return -z
; /* atan(-,+) */
108 case 2: return pi
-(z
-pi_lo
);/* atan(+,-) */
109 default: /* case 3 */
110 return (z
-pi_lo
)-pi
;/* atan(-,-) */