xref: /freebsd/lib/msun/src/e_atan2f.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /* e_atan2f.c -- float version of e_atan2.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #ifndef lint
17 static char rcsid[] = "$FreeBSD$";
18 #endif
19 
20 #include "math.h"
21 #include "math_private.h"
22 
23 static const float
24 tiny  = 1.0e-30,
25 zero  = 0.0,
26 pi_o_4  = 7.8539818525e-01, /* 0x3f490fdb */
27 pi_o_2  = 1.5707963705e+00, /* 0x3fc90fdb */
28 pi      = 3.1415927410e+00, /* 0x40490fdb */
29 pi_lo   = -8.7422776573e-08; /* 0xb3bbbd2e */
30 
31 float
32 __ieee754_atan2f(float y, float x)
33 {
34 	float z;
35 	int32_t k,m,hx,hy,ix,iy;
36 
37 	GET_FLOAT_WORD(hx,x);
38 	ix = hx&0x7fffffff;
39 	GET_FLOAT_WORD(hy,y);
40 	iy = hy&0x7fffffff;
41 	if((ix>0x7f800000)||
42 	   (iy>0x7f800000))	/* x or y is NaN */
43 	   return x+y;
44 	if(hx==0x3f800000) return atanf(y);   /* x=1.0 */
45 	m = ((hy>>31)&1)|((hx>>30)&2);	/* 2*sign(x)+sign(y) */
46 
47     /* when y = 0 */
48 	if(iy==0) {
49 	    switch(m) {
50 		case 0:
51 		case 1: return y; 	/* atan(+-0,+anything)=+-0 */
52 		case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
53 		case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
54 	    }
55 	}
56     /* when x = 0 */
57 	if(ix==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
58 
59     /* when x is INF */
60 	if(ix==0x7f800000) {
61 	    if(iy==0x7f800000) {
62 		switch(m) {
63 		    case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
64 		    case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
65 		    case 2: return  (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
66 		    case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
67 		}
68 	    } else {
69 		switch(m) {
70 		    case 0: return  zero  ;	/* atan(+...,+INF) */
71 		    case 1: return -zero  ;	/* atan(-...,+INF) */
72 		    case 2: return  pi+tiny  ;	/* atan(+...,-INF) */
73 		    case 3: return -pi-tiny  ;	/* atan(-...,-INF) */
74 		}
75 	    }
76 	}
77     /* when y is INF */
78 	if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
79 
80     /* compute y/x */
81 	k = (iy-ix)>>23;
82 	if(k > 60) z=pi_o_2+(float)0.5*pi_lo; 	/* |y/x| >  2**60 */
83 	else if(hx<0&&k<-60) z=0.0; 	/* |y|/x < -2**60 */
84 	else z=atanf(fabsf(y/x));	/* safe to do y/x */
85 	switch (m) {
86 	    case 0: return       z  ;	/* atan(+,+) */
87 	    case 1: {
88 	    	      u_int32_t zh;
89 		      GET_FLOAT_WORD(zh,z);
90 		      SET_FLOAT_WORD(z,zh ^ 0x80000000);
91 		    }
92 		    return       z  ;	/* atan(-,+) */
93 	    case 2: return  pi-(z-pi_lo);/* atan(+,-) */
94 	    default: /* case 3 */
95 	    	    return  (z-pi_lo)-pi;/* atan(-,-) */
96 	}
97 }
98