e_atan2f.c revision 176451
12116Sjkh/* e_atan2f.c -- float version of e_atan2.c. 22116Sjkh * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 32116Sjkh */ 42116Sjkh 52116Sjkh/* 62116Sjkh * ==================================================== 72116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 82116Sjkh * 92116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 102116Sjkh * Permission to use, copy, modify, and distribute this 118870Srgrimes * software is freely granted, provided that this notice 122116Sjkh * is preserved. 132116Sjkh * ==================================================== 142116Sjkh */ 152116Sjkh 16176451Sdas#include <sys/cdefs.h> 17176451Sdas__FBSDID("$FreeBSD: head/lib/msun/src/e_atan2f.c 176451 2008-02-22 02:30:36Z das $"); 182116Sjkh 192116Sjkh#include "math.h" 202116Sjkh#include "math_private.h" 212116Sjkh 228870Srgrimesstatic const float 232116Sjkhtiny = 1.0e-30, 242116Sjkhzero = 0.0, 252116Sjkhpi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */ 262116Sjkhpi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */ 27130003Sbdepi = 3.1415927410e+00, /* 0x40490fdb */ 28130003Sbdepi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */ 292116Sjkh 3097407Salfredfloat 3197407Salfred__ieee754_atan2f(float y, float x) 328870Srgrimes{ 332116Sjkh float z; 342116Sjkh int32_t k,m,hx,hy,ix,iy; 352116Sjkh 362116Sjkh GET_FLOAT_WORD(hx,x); 372116Sjkh ix = hx&0x7fffffff; 382116Sjkh GET_FLOAT_WORD(hy,y); 392116Sjkh iy = hy&0x7fffffff; 402116Sjkh if((ix>0x7f800000)|| 412116Sjkh (iy>0x7f800000)) /* x or y is NaN */ 422116Sjkh return x+y; 432116Sjkh if(hx==0x3f800000) return atanf(y); /* x=1.0 */ 442116Sjkh m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ 452116Sjkh 462116Sjkh /* when y = 0 */ 472116Sjkh if(iy==0) { 482116Sjkh switch(m) { 498870Srgrimes case 0: 502116Sjkh case 1: return y; /* atan(+-0,+anything)=+-0 */ 512116Sjkh case 2: return pi+tiny;/* atan(+0,-anything) = pi */ 522116Sjkh case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ 532116Sjkh } 542116Sjkh } 552116Sjkh /* when x = 0 */ 562116Sjkh if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 578870Srgrimes 582116Sjkh /* when x is INF */ 592116Sjkh if(ix==0x7f800000) { 602116Sjkh if(iy==0x7f800000) { 612116Sjkh switch(m) { 622116Sjkh case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ 632116Sjkh case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ 642116Sjkh case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ 652116Sjkh case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ 662116Sjkh } 672116Sjkh } else { 682116Sjkh switch(m) { 692116Sjkh case 0: return zero ; /* atan(+...,+INF) */ 702116Sjkh case 1: return -zero ; /* atan(-...,+INF) */ 712116Sjkh case 2: return pi+tiny ; /* atan(+...,-INF) */ 722116Sjkh case 3: return -pi-tiny ; /* atan(-...,-INF) */ 732116Sjkh } 742116Sjkh } 752116Sjkh } 762116Sjkh /* when y is INF */ 772116Sjkh if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 782116Sjkh 792116Sjkh /* compute y/x */ 802116Sjkh k = (iy-ix)>>23; 812116Sjkh if(k > 60) z=pi_o_2+(float)0.5*pi_lo; /* |y/x| > 2**60 */ 822116Sjkh else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ 832116Sjkh else z=atanf(fabsf(y/x)); /* safe to do y/x */ 842116Sjkh switch (m) { 852116Sjkh case 0: return z ; /* atan(+,+) */ 862116Sjkh case 1: { 872116Sjkh u_int32_t zh; 882116Sjkh GET_FLOAT_WORD(zh,z); 892116Sjkh SET_FLOAT_WORD(z,zh ^ 0x80000000); 902116Sjkh } 912116Sjkh return z ; /* atan(-,+) */ 922116Sjkh case 2: return pi-(z-pi_lo);/* atan(+,-) */ 932116Sjkh default: /* case 3 */ 942116Sjkh return (z-pi_lo)-pi;/* atan(-,-) */ 952116Sjkh } 962116Sjkh} 97