Back to index

glibc  2.9
e_atan2f.c
Go to the documentation of this file.
00001 /* e_atan2f.c -- float version of e_atan2.c.
00002  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00003  */
00004 
00005 /*
00006  * ====================================================
00007  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00008  *
00009  * Developed at SunPro, a Sun Microsystems, Inc. business.
00010  * Permission to use, copy, modify, and distribute this
00011  * software is freely granted, provided that this notice
00012  * is preserved.
00013  * ====================================================
00014  */
00015 
00016 #if defined(LIBM_SCCS) && !defined(lint)
00017 static char rcsid[] = "$NetBSD: e_atan2f.c,v 1.4 1995/05/10 20:44:53 jtc Exp $";
00018 #endif
00019 
00020 #include "math.h"
00021 #include "math_private.h"
00022 
00023 #ifdef __STDC__
00024 static const float
00025 #else
00026 static float
00027 #endif
00028 tiny  = 1.0e-30,
00029 zero  = 0.0,
00030 pi_o_4  = 7.8539818525e-01,  /* 0x3f490fdb */
00031 pi_o_2  = 1.5707963705e+00,  /* 0x3fc90fdb */
00032 pi      = 3.1415927410e+00,  /* 0x40490fdb */
00033 pi_lo   = -8.7422776573e-08; /* 0xb3bbbd2e */
00034 
00035 #ifdef __STDC__
00036        float __ieee754_atan2f(float y, float x)
00037 #else
00038        float __ieee754_atan2f(y,x)
00039        float  y,x;
00040 #endif
00041 {
00042        float z;
00043        int32_t k,m,hx,hy,ix,iy;
00044 
00045        GET_FLOAT_WORD(hx,x);
00046        ix = hx&0x7fffffff;
00047        GET_FLOAT_WORD(hy,y);
00048        iy = hy&0x7fffffff;
00049        if((ix>0x7f800000)||
00050           (iy>0x7f800000))  /* x or y is NaN */
00051           return x+y;
00052        if(hx==0x3f800000) return __atanf(y);   /* x=1.0 */
00053        m = ((hy>>31)&1)|((hx>>30)&2);     /* 2*sign(x)+sign(y) */
00054 
00055     /* when y = 0 */
00056        if(iy==0) {
00057            switch(m) {
00058               case 0:
00059               case 1: return y;    /* atan(+-0,+anything)=+-0 */
00060               case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
00061               case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
00062            }
00063        }
00064     /* when x = 0 */
00065        if(ix==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
00066 
00067     /* when x is INF */
00068        if(ix==0x7f800000) {
00069            if(iy==0x7f800000) {
00070               switch(m) {
00071                   case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
00072                   case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
00073                   case 2: return  (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
00074                   case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
00075               }
00076            } else {
00077               switch(m) {
00078                   case 0: return  zero  ; /* atan(+...,+INF) */
00079                   case 1: return -zero  ; /* atan(-...,+INF) */
00080                   case 2: return  pi+tiny  ;     /* atan(+...,-INF) */
00081                   case 3: return -pi-tiny  ;     /* atan(-...,-INF) */
00082               }
00083            }
00084        }
00085     /* when y is INF */
00086        if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
00087 
00088     /* compute y/x */
00089        k = (iy-ix)>>23;
00090        if(k > 60) z=pi_o_2+(float)0.5*pi_lo;     /* |y/x| >  2**60 */
00091        else if(hx<0&&k<-60) z=0.0;        /* |y|/x < -2**60 */
00092        else z=__atanf(fabsf(y/x)); /* safe to do y/x */
00093        switch (m) {
00094            case 0: return       z  ;      /* atan(+,+) */
00095            case 1: {
00096                     u_int32_t zh;
00097                     GET_FLOAT_WORD(zh,z);
00098                     SET_FLOAT_WORD(z,zh ^ 0x80000000);
00099                   }
00100                   return       z  ;       /* atan(-,+) */
00101            case 2: return  pi-(z-pi_lo);/* atan(+,-) */
00102            default: /* case 3 */
00103                   return  (z-pi_lo)-pi;/* atan(-,-) */
00104        }
00105 }