src/libm/e_atan2.c
changeset 4870 d275c95ddc05
child 4874 8288636b861d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/libm/e_atan2.c	Sun Aug 29 16:05:34 2010 -0700
     1.3 @@ -0,0 +1,116 @@
     1.4 +/*
     1.5 + * ====================================================
     1.6 + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
     1.7 + *
     1.8 + * Developed at SunPro, a Sun Microsystems, Inc. business.
     1.9 + * Permission to use, copy, modify, and distribute this
    1.10 + * software is freely granted, provided that this notice
    1.11 + * is preserved.
    1.12 + * ====================================================
    1.13 + */
    1.14 +
    1.15 +/* __ieee754_atan2(y,x)
    1.16 + * Method :
    1.17 + *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
    1.18 + *	2. Reduce x to positive by (if x and y are unexceptional):
    1.19 + *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
    1.20 + *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
    1.21 + *
    1.22 + * Special cases:
    1.23 + *
    1.24 + *	ATAN2((anything), NaN ) is NaN;
    1.25 + *	ATAN2(NAN , (anything) ) is NaN;
    1.26 + *	ATAN2(+-0, +(anything but NaN)) is +-0  ;
    1.27 + *	ATAN2(+-0, -(anything but NaN)) is +-pi ;
    1.28 + *	ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
    1.29 + *	ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
    1.30 + *	ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
    1.31 + *	ATAN2(+-INF,+INF ) is +-pi/4 ;
    1.32 + *	ATAN2(+-INF,-INF ) is +-3pi/4;
    1.33 + *	ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
    1.34 + *
    1.35 + * Constants:
    1.36 + * The hexadecimal values are the intended ones for the following
    1.37 + * constants. The decimal values may be used, provided that the
    1.38 + * compiler will convert from decimal to binary accurately enough
    1.39 + * to produce the hexadecimal values shown.
    1.40 + */
    1.41 +
    1.42 +#include "math.h"
    1.43 +#include "math_private.h"
    1.44 +
    1.45 +static const double
    1.46 +tiny  = 1.0e-300,
    1.47 +zero  = 0.0,
    1.48 +pi_o_4  = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
    1.49 +pi_o_2  = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
    1.50 +pi      = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
    1.51 +pi_lo   = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
    1.52 +
    1.53 +double attribute_hidden __ieee754_atan2(double y, double x)
    1.54 +{
    1.55 +	double z;
    1.56 +	int32_t k,m,hx,hy,ix,iy;
    1.57 +	u_int32_t lx,ly;
    1.58 +
    1.59 +	EXTRACT_WORDS(hx,lx,x);
    1.60 +	ix = hx&0x7fffffff;
    1.61 +	EXTRACT_WORDS(hy,ly,y);
    1.62 +	iy = hy&0x7fffffff;
    1.63 +	if(((ix|((lx|-lx)>>31))>0x7ff00000)||
    1.64 +	   ((iy|((ly|-ly)>>31))>0x7ff00000))	/* x or y is NaN */
    1.65 +	   return x+y;
    1.66 +	if(((hx-0x3ff00000)|lx)==0) return atan(y);   /* x=1.0 */
    1.67 +	m = ((hy>>31)&1)|((hx>>30)&2);	/* 2*sign(x)+sign(y) */
    1.68 +
    1.69 +    /* when y = 0 */
    1.70 +	if((iy|ly)==0) {
    1.71 +	    switch(m) {
    1.72 +		case 0:
    1.73 +		case 1: return y; 	/* atan(+-0,+anything)=+-0 */
    1.74 +		case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
    1.75 +		case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
    1.76 +	    }
    1.77 +	}
    1.78 +    /* when x = 0 */
    1.79 +	if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
    1.80 +
    1.81 +    /* when x is INF */
    1.82 +	if(ix==0x7ff00000) {
    1.83 +	    if(iy==0x7ff00000) {
    1.84 +		switch(m) {
    1.85 +		    case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
    1.86 +		    case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
    1.87 +		    case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
    1.88 +		    case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
    1.89 +		}
    1.90 +	    } else {
    1.91 +		switch(m) {
    1.92 +		    case 0: return  zero  ;	/* atan(+...,+INF) */
    1.93 +		    case 1: return -zero  ;	/* atan(-...,+INF) */
    1.94 +		    case 2: return  pi+tiny  ;	/* atan(+...,-INF) */
    1.95 +		    case 3: return -pi-tiny  ;	/* atan(-...,-INF) */
    1.96 +		}
    1.97 +	    }
    1.98 +	}
    1.99 +    /* when y is INF */
   1.100 +	if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
   1.101 +
   1.102 +    /* compute y/x */
   1.103 +	k = (iy-ix)>>20;
   1.104 +	if(k > 60) z=pi_o_2+0.5*pi_lo; 	/* |y/x| >  2**60 */
   1.105 +	else if(hx<0&&k<-60) z=0.0; 	/* |y|/x < -2**60 */
   1.106 +	else z=atan(fabs(y/x));		/* safe to do y/x */
   1.107 +	switch (m) {
   1.108 +	    case 0: return       z  ;	/* atan(+,+) */
   1.109 +	    case 1: {
   1.110 +	    	      u_int32_t zh;
   1.111 +		      GET_HIGH_WORD(zh,z);
   1.112 +		      SET_HIGH_WORD(z,zh ^ 0x80000000);
   1.113 +		    }
   1.114 +		    return       z  ;	/* atan(-,+) */
   1.115 +	    case 2: return  pi-(z-pi_lo);/* atan(+,-) */
   1.116 +	    default: /* case 3 */
   1.117 +	    	    return  (z-pi_lo)-pi;/* atan(-,-) */
   1.118 +	}
   1.119 +}