e_atan2.c revision 22808
12116Sjkh/* @(#)e_atan2.c 5.1 93/09/24 */ 22116Sjkh/* 32116Sjkh * ==================================================== 42116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 52116Sjkh * 62116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business. 72116Sjkh * Permission to use, copy, modify, and distribute this 88870Srgrimes * software is freely granted, provided that this notice 92116Sjkh * is preserved. 102116Sjkh * ==================================================== 112116Sjkh */ 122116Sjkh 132116Sjkh#ifndef lint 1421673Sjkhstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/e_atan2.c 22808 1997-02-16 18:26:31Z bde $"; 152116Sjkh#endif 162116Sjkh 172116Sjkh/* __ieee754_atan2(y,x) 182116Sjkh * Method : 192116Sjkh * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). 208870Srgrimes * 2. Reduce x to positive by (if x and y are unexceptional): 212116Sjkh * ARG (x+iy) = arctan(y/x) ... if x > 0, 222116Sjkh * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, 232116Sjkh * 242116Sjkh * Special cases: 252116Sjkh * 262116Sjkh * ATAN2((anything), NaN ) is NaN; 272116Sjkh * ATAN2(NAN , (anything) ) is NaN; 282116Sjkh * ATAN2(+-0, +(anything but NaN)) is +-0 ; 292116Sjkh * ATAN2(+-0, -(anything but NaN)) is +-pi ; 302116Sjkh * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; 312116Sjkh * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; 322116Sjkh * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; 332116Sjkh * ATAN2(+-INF,+INF ) is +-pi/4 ; 342116Sjkh * ATAN2(+-INF,-INF ) is +-3pi/4; 352116Sjkh * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; 362116Sjkh * 372116Sjkh * Constants: 388870Srgrimes * The hexadecimal values are the intended ones for the following 398870Srgrimes * constants. The decimal values may be used, provided that the 408870Srgrimes * compiler will convert from decimal to binary accurately enough 412116Sjkh * to produce the hexadecimal values shown. 422116Sjkh */ 432116Sjkh 442116Sjkh#include "math.h" 452116Sjkh#include "math_private.h" 462116Sjkh 472116Sjkh#ifdef __STDC__ 488870Srgrimesstatic const double 492116Sjkh#else 508870Srgrimesstatic double 512116Sjkh#endif 522116Sjkhtiny = 1.0e-300, 532116Sjkhzero = 0.0, 542116Sjkhpi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ 552116Sjkhpi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ 562116Sjkhpi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ 572116Sjkhpi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ 582116Sjkh 592116Sjkh#ifdef __STDC__ 6022808Sbde double __generic___ieee754_atan2(double y, double x) 612116Sjkh#else 6222808Sbde double __generic___ieee754_atan2(y,x) 632116Sjkh double y,x; 642116Sjkh#endif 658870Srgrimes{ 662116Sjkh double z; 672116Sjkh int32_t k,m,hx,hy,ix,iy; 682116Sjkh u_int32_t lx,ly; 692116Sjkh 702116Sjkh EXTRACT_WORDS(hx,lx,x); 712116Sjkh ix = hx&0x7fffffff; 722116Sjkh EXTRACT_WORDS(hy,ly,y); 732116Sjkh iy = hy&0x7fffffff; 742116Sjkh if(((ix|((lx|-lx)>>31))>0x7ff00000)|| 752116Sjkh ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ 762116Sjkh return x+y; 7717141Sjkh if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */ 782116Sjkh m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ 792116Sjkh 802116Sjkh /* when y = 0 */ 812116Sjkh if((iy|ly)==0) { 822116Sjkh switch(m) { 838870Srgrimes case 0: 842116Sjkh case 1: return y; /* atan(+-0,+anything)=+-0 */ 852116Sjkh case 2: return pi+tiny;/* atan(+0,-anything) = pi */ 862116Sjkh case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ 872116Sjkh } 882116Sjkh } 892116Sjkh /* when x = 0 */ 902116Sjkh if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 918870Srgrimes 922116Sjkh /* when x is INF */ 932116Sjkh if(ix==0x7ff00000) { 942116Sjkh if(iy==0x7ff00000) { 952116Sjkh switch(m) { 962116Sjkh case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ 972116Sjkh case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ 982116Sjkh case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ 992116Sjkh case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ 1002116Sjkh } 1012116Sjkh } else { 1022116Sjkh switch(m) { 1032116Sjkh case 0: return zero ; /* atan(+...,+INF) */ 1042116Sjkh case 1: return -zero ; /* atan(-...,+INF) */ 1052116Sjkh case 2: return pi+tiny ; /* atan(+...,-INF) */ 1062116Sjkh case 3: return -pi-tiny ; /* atan(-...,-INF) */ 1072116Sjkh } 1082116Sjkh } 1092116Sjkh } 1102116Sjkh /* when y is INF */ 1112116Sjkh if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 1122116Sjkh 1132116Sjkh /* compute y/x */ 1142116Sjkh k = (iy-ix)>>20; 1152116Sjkh if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ 1162116Sjkh else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ 1172116Sjkh else z=atan(fabs(y/x)); /* safe to do y/x */ 1182116Sjkh switch (m) { 1192116Sjkh case 0: return z ; /* atan(+,+) */ 1202116Sjkh case 1: { 1212116Sjkh u_int32_t zh; 1222116Sjkh GET_HIGH_WORD(zh,z); 1232116Sjkh SET_HIGH_WORD(z,zh ^ 0x80000000); 1242116Sjkh } 1252116Sjkh return z ; /* atan(-,+) */ 1262116Sjkh case 2: return pi-(z-pi_lo);/* atan(+,-) */ 1272116Sjkh default: /* case 3 */ 1282116Sjkh return (z-pi_lo)-pi;/* atan(-,-) */ 1292116Sjkh } 1302116Sjkh} 131