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 #include <sys/cdefs.h> 17 #if defined(LIBM_SCCS) && !defined(lint) 18 __RCSID("$NetBSD: e_atan2f.c,v 1.5 1997/10/09 11:28:42 lukem Exp $"); 19 #endif 20 21 #include "math.h" 22 #include "math_private.h" 23 24 #ifdef __STDC__ 25 static const float 26 #else 27 static float 28 #endif 29 tiny = 1.0e-30, 30 zero = 0.0, 31 pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */ 32 pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */ 33 pi = 3.1415925026e+00, /* 0x40490fda */ 34 pi_lo = 1.5099578832e-07; /* 0x34222168 */ 35 36 #ifdef __STDC__ 37 float __ieee754_atan2f(float y, float x) 38 #else 39 float __ieee754_atan2f(y,x) 40 float y,x; 41 #endif 42 { 43 float z; 44 int32_t k,m,hx,hy,ix,iy; 45 46 GET_FLOAT_WORD(hx,x); 47 ix = hx&0x7fffffff; 48 GET_FLOAT_WORD(hy,y); 49 iy = hy&0x7fffffff; 50 if((ix>0x7f800000)|| 51 (iy>0x7f800000)) /* x or y is NaN */ 52 return x+y; 53 if(hx==0x3f800000) return atanf(y); /* x=1.0 */ 54 m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ 55 56 /* when y = 0 */ 57 if(iy==0) { 58 switch(m) { 59 case 0: 60 case 1: return y; /* atan(+-0,+anything)=+-0 */ 61 case 2: return pi+tiny;/* atan(+0,-anything) = pi */ 62 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ 63 } 64 } 65 /* when x = 0 */ 66 if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 67 68 /* when x is INF */ 69 if(ix==0x7f800000) { 70 if(iy==0x7f800000) { 71 switch(m) { 72 case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ 73 case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ 74 case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ 75 case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ 76 } 77 } else { 78 switch(m) { 79 case 0: return zero ; /* atan(+...,+INF) */ 80 case 1: return -zero ; /* atan(-...,+INF) */ 81 case 2: return pi+tiny ; /* atan(+...,-INF) */ 82 case 3: return -pi-tiny ; /* atan(-...,-INF) */ 83 } 84 } 85 } 86 /* when y is INF */ 87 if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 88 89 /* compute y/x */ 90 k = (iy-ix)>>23; 91 if(k > 60) z=pi_o_2+(float)0.5*pi_lo; /* |y/x| > 2**60 */ 92 else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ 93 else z=atanf(fabsf(y/x)); /* safe to do y/x */ 94 switch (m) { 95 case 0: return z ; /* atan(+,+) */ 96 case 1: { 97 u_int32_t zh; 98 GET_FLOAT_WORD(zh,z); 99 SET_FLOAT_WORD(z,zh ^ 0x80000000); 100 } 101 return z ; /* atan(-,+) */ 102 case 2: return pi-(z-pi_lo);/* atan(+,-) */ 103 default: /* case 3 */ 104 return (z-pi_lo)-pi;/* atan(-,-) */ 105 } 106 } 107