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