e_atan2.c revision 181074
1254721Semaste 2254721Semaste/* @(#)e_atan2.c 1.3 95/01/18 */ 3254721Semaste/* 4254721Semaste * ==================================================== 5254721Semaste * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6254721Semaste * 7254721Semaste * Developed at SunSoft, a Sun Microsystems, Inc. business. 8254721Semaste * Permission to use, copy, modify, and distribute this 9254721Semaste * software is freely granted, provided that this notice 10254721Semaste * is preserved. 11254721Semaste * ==================================================== 12254721Semaste * 13254721Semaste */ 14254721Semaste 15254721Semaste#include <sys/cdefs.h> 16254721Semaste__FBSDID("$FreeBSD: head/lib/msun/src/e_atan2.c 181074 2008-07-31 22:41:26Z das $"); 17254721Semaste 18254721Semaste/* __ieee754_atan2(y,x) 19254721Semaste * Method : 20254721Semaste * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). 21254721Semaste * 2. Reduce x to positive by (if x and y are unexceptional): 22254721Semaste * ARG (x+iy) = arctan(y/x) ... if x > 0, 23254721Semaste * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, 24254721Semaste * 25254721Semaste * Special cases: 26254721Semaste * 27254721Semaste * ATAN2((anything), NaN ) is NaN; 28254721Semaste * ATAN2(NAN , (anything) ) is NaN; 29254721Semaste * ATAN2(+-0, +(anything but NaN)) is +-0 ; 30254721Semaste * ATAN2(+-0, -(anything but NaN)) is +-pi ; 31254721Semaste * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; 32254721Semaste * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; 33254721Semaste * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; 34254721Semaste * ATAN2(+-INF,+INF ) is +-pi/4 ; 35254721Semaste * ATAN2(+-INF,-INF ) is +-3pi/4; 36254721Semaste * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; 37254721Semaste * 38254721Semaste * Constants: 39254721Semaste * The hexadecimal values are the intended ones for the following 40254721Semaste * constants. The decimal values may be used, provided that the 41254721Semaste * compiler will convert from decimal to binary accurately enough 42254721Semaste * to produce the hexadecimal values shown. 43254721Semaste */ 44254721Semaste 45254721Semaste#include <float.h> 46254721Semaste 47254721Semaste#include "math.h" 48254721Semaste#include "math_private.h" 49254721Semaste 50254721Semastestatic volatile double 51254721Semastetiny = 1.0e-300; 52254721Semastestatic const double 53254721Semastezero = 0.0, 54254721Semastepi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ 55254721Semastepi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ 56254721Semastepi = 3.1415926535897931160E+00; /* 0x400921FB, 0x54442D18 */ 57254721Semastestatic volatile double 58254721Semastepi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ 59254721Semaste 60254721Semastedouble 61254721Semaste__ieee754_atan2(double y, double x) 62254721Semaste{ 63254721Semaste double z; 64254721Semaste int32_t k,m,hx,hy,ix,iy; 65254721Semaste u_int32_t lx,ly; 66254721Semaste 67254721Semaste EXTRACT_WORDS(hx,lx,x); 68254721Semaste ix = hx&0x7fffffff; 69254721Semaste EXTRACT_WORDS(hy,ly,y); 70254721Semaste iy = hy&0x7fffffff; 71254721Semaste if(((ix|((lx|-lx)>>31))>0x7ff00000)|| 72254721Semaste ((iy|((ly|-ly)>>31))>0x7ff00000)) /* x or y is NaN */ 73254721Semaste return x+y; 74254721Semaste if((hx-0x3ff00000|lx)==0) return atan(y); /* x=1.0 */ 75254721Semaste m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ 76254721Semaste 77254721Semaste /* when y = 0 */ 78254721Semaste if((iy|ly)==0) { 79254721Semaste switch(m) { 80254721Semaste case 0: 81254721Semaste case 1: return y; /* atan(+-0,+anything)=+-0 */ 82254721Semaste case 2: return pi+tiny;/* atan(+0,-anything) = pi */ 83254721Semaste case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ 84254721Semaste } 85254721Semaste } 86254721Semaste /* when x = 0 */ 87254721Semaste if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 88254721Semaste 89254721Semaste /* when x is INF */ 90254721Semaste if(ix==0x7ff00000) { 91254721Semaste if(iy==0x7ff00000) { 92254721Semaste switch(m) { 93254721Semaste case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ 94254721Semaste case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ 95254721Semaste case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ 96254721Semaste case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ 97254721Semaste } 98254721Semaste } else { 99254721Semaste switch(m) { 100254721Semaste case 0: return zero ; /* atan(+...,+INF) */ 101254721Semaste case 1: return -zero ; /* atan(-...,+INF) */ 102254721Semaste case 2: return pi+tiny ; /* atan(+...,-INF) */ 103254721Semaste case 3: return -pi-tiny ; /* atan(-...,-INF) */ 104254721Semaste } 105254721Semaste } 106254721Semaste } 107254721Semaste /* when y is INF */ 108254721Semaste if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; 109254721Semaste 110254721Semaste /* compute y/x */ 111254721Semaste k = (iy-ix)>>20; 112254721Semaste if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ 113254721Semaste else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ 114254721Semaste else z=atan(fabs(y/x)); /* safe to do y/x */ 115254721Semaste switch (m) { 116254721Semaste case 0: return z ; /* atan(+,+) */ 117254721Semaste case 1: { 118254721Semaste u_int32_t zh; 119254721Semaste GET_HIGH_WORD(zh,z); 120254721Semaste SET_HIGH_WORD(z,zh ^ 0x80000000); 121254721Semaste } 122254721Semaste return z ; /* atan(-,+) */ 123254721Semaste case 2: return pi-(z-pi_lo);/* atan(+,-) */ 124254721Semaste default: /* case 3 */ 125254721Semaste return (z-pi_lo)-pi;/* atan(-,-) */ 126254721Semaste } 127254721Semaste} 128254721Semaste 129254721Semaste#if LDBL_MANT_DIG == 53 130254721Semaste__weak_reference(atan2, atan2l); 131254721Semaste#endif 132254721Semaste