POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/e_atan2.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* @(#)e_atan2.c 5.1 93/09/24 */
00018 /*
00019  * ====================================================
00020  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00021  *
00022  * Developed at SunPro, a Sun Microsystems, Inc. business.
00023  * Permission to use, copy, modify, and distribute this
00024  * software is freely granted, provided that this notice
00025  * is preserved.
00026  * ====================================================
00027  */
00028 
00029 /* __ieee754_atan2(y,x)
00030  * Method :
00031  *      1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
00032  *      2. Reduce x to positive by (if x and y are unexceptional):
00033  *              ARG (x+iy) = arctan(y/x)           ... if x > 0,
00034  *              ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
00035  *
00036  * Special cases:
00037  *
00038  *      ATAN2((anything), NaN ) is NaN;
00039  *      ATAN2(NAN , (anything) ) is NaN;
00040  *      ATAN2(+-0, +(anything but NaN)) is +-0  ;
00041  *      ATAN2(+-0, -(anything but NaN)) is +-pi ;
00042  *      ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
00043  *      ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
00044  *      ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
00045  *      ATAN2(+-INF,+INF ) is +-pi/4 ;
00046  *      ATAN2(+-INF,-INF ) is +-3pi/4;
00047  *      ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
00048  *
00049  * Constants:
00050  * The hexadecimal values are the intended ones for the following
00051  * constants. The decimal values may be used, provided that the
00052  * compiler will convert from decimal to binary accurately enough
00053  * to produce the hexadecimal values shown.
00054  */
00055 
00056 #ifdef POK_NEEDS_LIBMATH
00057 #include <libm.h>
00058 #include "math_private.h"
00059 
00060 static const double
00061 tiny  = 1.0e-300,
00062 zero  = 0.0,
00063 pi_o_4  = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
00064 pi_o_2  = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
00065 pi      = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
00066 pi_lo   = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
00067 
00068 double
00069 __ieee754_atan2(double y, double x)
00070 {
00071         double z;
00072         int32_t k,m,hx,hy,ix,iy;
00073         uint32_t lx,ly;
00074 
00075         EXTRACT_WORDS(hx,lx,x);
00076         ix = hx&0x7fffffff;
00077         EXTRACT_WORDS(hy,ly,y);
00078         iy = hy&0x7fffffff;
00079         if(((ix|((lx|-lx)>>31))>0x7ff00000)||
00080            ((iy|((ly|-ly)>>31))>0x7ff00000))    /* x or y is NaN */
00081            return x+y;
00082         if(((hx-0x3ff00000)|lx)==0) return atan(y);   /* x=1.0 */
00083         m = ((hy>>31)&1)|((hx>>30)&2);  /* 2*sign(x)+sign(y) */
00084 
00085     /* when y = 0 */
00086         if((iy|ly)==0) {
00087             switch(m) {
00088                 case 0:
00089                 case 1: return y;       /* atan(+-0,+anything)=+-0 */
00090                 case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
00091                 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
00092             }
00093         }
00094     /* when x = 0 */
00095         if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
00096 
00097     /* when x is INF */
00098         if(ix==0x7ff00000) {
00099             if(iy==0x7ff00000) {
00100                 switch(m) {
00101                     case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
00102                     case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
00103                     case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
00104                     case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
00105                 }
00106             } else {
00107                 switch(m) {
00108                     case 0: return  zero  ;     /* atan(+...,+INF) */
00109                     case 1: return -zero  ;     /* atan(-...,+INF) */
00110                     case 2: return  pi+tiny  ;  /* atan(+...,-INF) */
00111                     case 3: return -pi-tiny  ;  /* atan(-...,-INF) */
00112                 }
00113             }
00114         }
00115     /* when y is INF */
00116         if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
00117 
00118     /* compute y/x */
00119         k = (iy-ix)>>20;
00120         if(k > 60) z=pi_o_2+0.5*pi_lo;  /* |y/x| >  2**60 */
00121         else if(hx<0&&k<-60) z=0.0;     /* |y|/x < -2**60 */
00122         else z=atan(fabs(y/x));         /* safe to do y/x */
00123         switch (m) {
00124             case 0: return       z  ;   /* atan(+,+) */
00125             case 1: {
00126                       uint32_t zh;
00127                       GET_HIGH_WORD(zh,z);
00128                       SET_HIGH_WORD(z,zh ^ 0x80000000);
00129                     }
00130                     return       z  ;   /* atan(-,+) */
00131             case 2: return  pi-(z-pi_lo);/* atan(+,-) */
00132             default: /* case 3 */
00133                     return  (z-pi_lo)-pi;/* atan(-,-) */
00134         }
00135 }
00136 #endif