slouken@4870: /* slouken@4870: * ==================================================== slouken@4870: * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. slouken@4870: * slouken@4870: * Developed at SunPro, a Sun Microsystems, Inc. business. slouken@4870: * Permission to use, copy, modify, and distribute this slouken@4870: * software is freely granted, provided that this notice slouken@4870: * is preserved. slouken@4870: * ==================================================== slouken@4870: */ slouken@4870: slouken@4870: /* __ieee754_atan2(y,x) slouken@4870: * Method : slouken@4870: * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). slouken@4870: * 2. Reduce x to positive by (if x and y are unexceptional): slouken@4870: * ARG (x+iy) = arctan(y/x) ... if x > 0, slouken@4870: * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, slouken@4870: * slouken@4870: * Special cases: slouken@4870: * slouken@4870: * ATAN2((anything), NaN ) is NaN; slouken@4870: * ATAN2(NAN , (anything) ) is NaN; slouken@4870: * ATAN2(+-0, +(anything but NaN)) is +-0 ; slouken@4870: * ATAN2(+-0, -(anything but NaN)) is +-pi ; slouken@4870: * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; slouken@4870: * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; slouken@4870: * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; slouken@4870: * ATAN2(+-INF,+INF ) is +-pi/4 ; slouken@4870: * ATAN2(+-INF,-INF ) is +-3pi/4; slouken@4870: * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; slouken@4870: * slouken@4870: * Constants: slouken@4870: * The hexadecimal values are the intended ones for the following slouken@4870: * constants. The decimal values may be used, provided that the slouken@4870: * compiler will convert from decimal to binary accurately enough slouken@4870: * to produce the hexadecimal values shown. slouken@4870: */ slouken@4870: slouken@6044: #include "math_libm.h" slouken@4870: #include "math_private.h" slouken@4870: slouken@4870: static const double slouken@4870: tiny = 1.0e-300, slouken@4870: zero = 0.0, slouken@4870: pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */ slouken@4870: pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */ slouken@4870: pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */ slouken@4870: pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */ slouken@4870: slouken@4870: double attribute_hidden __ieee754_atan2(double y, double x) slouken@4870: { slouken@4870: double z; slouken@4870: int32_t k,m,hx,hy,ix,iy; slouken@4870: u_int32_t lx,ly; slouken@4870: slouken@4870: EXTRACT_WORDS(hx,lx,x); slouken@4870: ix = hx&0x7fffffff; slouken@4870: EXTRACT_WORDS(hy,ly,y); slouken@4870: iy = hy&0x7fffffff; slouken@11685: if(((ix|((lx|-(int32_t)lx)>>31))>0x7ff00000)|| slouken@11685: ((iy|((ly|-(int32_t)ly)>>31))>0x7ff00000)) /* x or y is NaN */ slouken@4870: return x+y; slouken@4870: if(((hx-0x3ff00000)|lx)==0) return atan(y); /* x=1.0 */ slouken@4870: m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ slouken@4870: slouken@4870: /* when y = 0 */ slouken@4870: if((iy|ly)==0) { slouken@4870: switch(m) { slouken@4870: case 0: slouken@4870: case 1: return y; /* atan(+-0,+anything)=+-0 */ slouken@4870: case 2: return pi+tiny;/* atan(+0,-anything) = pi */ slouken@4870: case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ slouken@4870: } slouken@4870: } slouken@4870: /* when x = 0 */ slouken@4870: if((ix|lx)==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; slouken@4870: slouken@4870: /* when x is INF */ slouken@4870: if(ix==0x7ff00000) { slouken@4870: if(iy==0x7ff00000) { slouken@4870: switch(m) { slouken@4870: case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ slouken@4870: case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ slouken@11683: case 2: return 3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ slouken@11683: case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ slouken@4870: } slouken@4870: } else { slouken@4870: switch(m) { slouken@4870: case 0: return zero ; /* atan(+...,+INF) */ slouken@4870: case 1: return -zero ; /* atan(-...,+INF) */ slouken@4870: case 2: return pi+tiny ; /* atan(+...,-INF) */ slouken@4870: case 3: return -pi-tiny ; /* atan(-...,-INF) */ slouken@4870: } slouken@4870: } slouken@4870: } slouken@4870: /* when y is INF */ slouken@4870: if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; slouken@4870: slouken@4870: /* compute y/x */ slouken@4870: k = (iy-ix)>>20; slouken@4870: if(k > 60) z=pi_o_2+0.5*pi_lo; /* |y/x| > 2**60 */ slouken@4870: else if(hx<0&&k<-60) z=0.0; /* |y|/x < -2**60 */ slouken@4870: else z=atan(fabs(y/x)); /* safe to do y/x */ slouken@4870: switch (m) { slouken@4870: case 0: return z ; /* atan(+,+) */ slouken@4870: case 1: { slouken@4870: u_int32_t zh; slouken@4870: GET_HIGH_WORD(zh,z); slouken@4870: SET_HIGH_WORD(z,zh ^ 0x80000000); slouken@4870: } slouken@4870: return z ; /* atan(-,+) */ slouken@4870: case 2: return pi-(z-pi_lo);/* atan(+,-) */ slouken@4870: default: /* case 3 */ slouken@4870: return (z-pi_lo)-pi;/* atan(-,-) */ slouken@4870: } slouken@4870: } slouken@11683: slouken@11683: /* slouken@11683: * wrapper atan2(y,x) slouken@11683: */ slouken@11683: #ifndef _IEEE_LIBM slouken@11683: double atan2(double y, double x) slouken@11683: { slouken@11683: double z = __ieee754_atan2(y, x); slouken@11683: if (_LIB_VERSION == _IEEE_ || isnan(x) || isnan(y)) slouken@11683: return z; slouken@11683: if (x == 0.0 && y == 0.0) slouken@11683: return __kernel_standard(y,x,3); /* atan2(+-0,+-0) */ slouken@11683: return z; slouken@11683: } slouken@11683: #else slouken@11683: strong_alias(__ieee754_atan2, atan2) slouken@11683: #endif slouken@11683: libm_hidden_def(atan2)