e_atan2f.c revision 181204
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 181204 2008-08-02 19:17:00Z das $"); 182116Sjkh 192116Sjkh#include "math.h" 202116Sjkh#include "math_private.h" 212116Sjkh 22181062Sdasstatic volatile float 23181062Sdastiny = 1.0e-30; 248870Srgrimesstatic const float 252116Sjkhzero = 0.0, 262116Sjkhpi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */ 272116Sjkhpi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */ 28181062Sdaspi = 3.1415927410e+00; /* 0x40490fdb */ 29181062Sdasstatic volatile float 30130003Sbdepi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */ 312116Sjkh 3297407Salfredfloat 3397407Salfred__ieee754_atan2f(float y, float x) 348870Srgrimes{ 352116Sjkh float z; 362116Sjkh int32_t k,m,hx,hy,ix,iy; 372116Sjkh 382116Sjkh GET_FLOAT_WORD(hx,x); 392116Sjkh ix = hx&0x7fffffff; 402116Sjkh GET_FLOAT_WORD(hy,y); 412116Sjkh iy = hy&0x7fffffff; 422116Sjkh if((ix>0x7f800000)|| 432116Sjkh (iy>0x7f800000)) /* x or y is NaN */ 442116Sjkh return x+y; 452116Sjkh if(hx==0x3f800000) return atanf(y); /* x=1.0 */ 462116Sjkh m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ 472116Sjkh 482116Sjkh /* when y = 0 */ 492116Sjkh if(iy==0) { 502116Sjkh switch(m) { 518870Srgrimes case 0: 522116Sjkh case 1: return y; /* atan(+-0,+anything)=+-0 */ 532116Sjkh case 2: return pi+tiny;/* atan(+0,-anything) = pi */ 542116Sjkh case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ 552116Sjkh } 562116Sjkh } 572116Sjkh /* when x = 0 */ 582116Sjkh if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 598870Srgrimes 602116Sjkh /* when x is INF */ 612116Sjkh if(ix==0x7f800000) { 622116Sjkh if(iy==0x7f800000) { 632116Sjkh switch(m) { 642116Sjkh case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ 652116Sjkh case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ 662116Sjkh case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ 672116Sjkh case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ 682116Sjkh } 692116Sjkh } else { 702116Sjkh switch(m) { 712116Sjkh case 0: return zero ; /* atan(+...,+INF) */ 722116Sjkh case 1: return -zero ; /* atan(-...,+INF) */ 732116Sjkh case 2: return pi+tiny ; /* atan(+...,-INF) */ 742116Sjkh case 3: return -pi-tiny ; /* atan(-...,-INF) */ 752116Sjkh } 762116Sjkh } 772116Sjkh } 782116Sjkh /* when y is INF */ 792116Sjkh if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 802116Sjkh 812116Sjkh /* compute y/x */ 822116Sjkh k = (iy-ix)>>23; 83181204Sdas if(k > 26) { /* |y/x| > 2**26 */ 84181204Sdas z=pi_o_2+(float)0.5*pi_lo; 85181204Sdas m&=1; 86181204Sdas } 87181204Sdas else if(hx<0&&k<-26) z=0.0; /* 0 > |y|/x > -2**-26 */ 882116Sjkh else z=atanf(fabsf(y/x)); /* safe to do y/x */ 892116Sjkh switch (m) { 902116Sjkh case 0: return z ; /* atan(+,+) */ 91181204Sdas case 1: 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