POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/e_atan2f.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_atan2f.c -- float version of e_atan2.c.
00018  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00019  */
00020 
00021 /*
00022  * ====================================================
00023  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00024  *
00025  * Developed at SunPro, a Sun Microsystems, Inc. business.
00026  * Permission to use, copy, modify, and distribute this
00027  * software is freely granted, provided that this notice
00028  * is preserved.
00029  * ====================================================
00030  */
00031 
00032 #ifdef POK_NEEDS_LIBMATH
00033 #include <libm.h>
00034 #include "math_private.h"
00035 
00036 static const float
00037 tiny  = 1.0e-30,
00038 zero  = 0.0,
00039 pi_o_4  = 7.8539818525e-01, /* 0x3f490fdb */
00040 pi_o_2  = 1.5707963705e+00, /* 0x3fc90fdb */
00041 pi      = 3.1415925026e+00, /* 0x40490fda */
00042 pi_lo   = 1.5099578832e-07; /* 0x34222168 */
00043 
00044 float
00045 __ieee754_atan2f(float y, float x)
00046 {
00047         float z;
00048         int32_t k,m,hx,hy,ix,iy;
00049 
00050         GET_FLOAT_WORD(hx,x);
00051         ix = hx&0x7fffffff;
00052         GET_FLOAT_WORD(hy,y);
00053         iy = hy&0x7fffffff;
00054         if((ix>0x7f800000)||
00055            (iy>0x7f800000))     /* x or y is NaN */
00056            return x+y;
00057         if(hx==0x3f800000) return atanf(y);   /* x=1.0 */
00058         m = ((hy>>31)&1)|((hx>>30)&2);  /* 2*sign(x)+sign(y) */
00059 
00060     /* when y = 0 */
00061         if(iy==0) {
00062             switch(m) {
00063                 case 0:
00064                 case 1: return y;       /* atan(+-0,+anything)=+-0 */
00065                 case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
00066                 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
00067             }
00068         }
00069     /* when x = 0 */
00070         if(ix==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
00071 
00072     /* when x is INF */
00073         if(ix==0x7f800000) {
00074             if(iy==0x7f800000) {
00075                 switch(m) {
00076                     case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
00077                     case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
00078                     case 2: return  (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
00079                     case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
00080                 }
00081             } else {
00082                 switch(m) {
00083                     case 0: return  zero  ;     /* atan(+...,+INF) */
00084                     case 1: return -zero  ;     /* atan(-...,+INF) */
00085                     case 2: return  pi+tiny  ;  /* atan(+...,-INF) */
00086                     case 3: return -pi-tiny  ;  /* atan(-...,-INF) */
00087                 }
00088             }
00089         }
00090     /* when y is INF */
00091         if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
00092 
00093     /* compute y/x */
00094         k = (iy-ix)>>23;
00095         if(k > 60) z=pi_o_2+(float)0.5*pi_lo;   /* |y/x| >  2**60 */
00096         else if(hx<0&&k<-60) z=0.0;     /* |y|/x < -2**60 */
00097         else z=atanf(fabsf(y/x));       /* safe to do y/x */
00098         switch (m) {
00099             case 0: return       z  ;   /* atan(+,+) */
00100             case 1: {
00101                       uint32_t zh;
00102                       GET_FLOAT_WORD(zh,z);
00103                       SET_FLOAT_WORD(z,zh ^ 0x80000000);
00104                     }
00105                     return       z  ;   /* atan(-,+) */
00106             case 2: return  pi-(z-pi_lo);/* atan(+,-) */
00107             default: /* case 3 */
00108                     return  (z-pi_lo)-pi;/* atan(-,-) */
00109         }
00110 }
00111 #endif