Back to index

glibc  2.9
e_atan2l.c
Go to the documentation of this file.
00001 /* e_atan2l.c -- long double version of e_atan2.c.
00002  * Conversion to long double by Jakub Jelinek, jj@ultra.linux.cz.
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 /* __ieee754_atan2l(y,x)
00017  * Method :
00018  *     1. Reduce y to positive by atan2l(y,x)=-atan2l(-y,x).
00019  *     2. Reduce x to positive by (if x and y are unexceptional):
00020  *            ARG (x+iy) = arctan(y/x)       ... if x > 0,
00021  *            ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
00022  *
00023  * Special cases:
00024  *
00025  *     ATAN2((anything), NaN ) is NaN;
00026  *     ATAN2(NAN , (anything) ) is NaN;
00027  *     ATAN2(+-0, +(anything but NaN)) is +-0  ;
00028  *     ATAN2(+-0, -(anything but NaN)) is +-pi ;
00029  *     ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
00030  *     ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
00031  *     ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
00032  *     ATAN2(+-INF,+INF ) is +-pi/4 ;
00033  *     ATAN2(+-INF,-INF ) is +-3pi/4;
00034  *     ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
00035  *
00036  * Constants:
00037  * The hexadecimal values are the intended ones for the following
00038  * constants. The decimal values may be used, provided that the
00039  * compiler will convert from decimal to binary accurately enough
00040  * to produce the hexadecimal values shown.
00041  */
00042 
00043 #include "math.h"
00044 #include "math_private.h"
00045 
00046 #ifdef __STDC__
00047 static const long double
00048 #else
00049 static long double
00050 #endif
00051 tiny  = 1.0e-4900L,
00052 zero  = 0.0,
00053 pi_o_4  = 7.85398163397448309615660845819875699e-01L, /* 3ffe921fb54442d18469898cc51701b8 */
00054 pi_o_2  = 1.57079632679489661923132169163975140e+00L, /* 3fff921fb54442d18469898cc51701b8 */
00055 pi      = 3.14159265358979323846264338327950280e+00L, /* 4000921fb54442d18469898cc51701b8 */
00056 pi_lo   = 8.67181013012378102479704402604335225e-35L; /* 3f8dcd129024e088a67cc74020bbea64 */
00057 
00058 #ifdef __STDC__
00059        long double __ieee754_atan2l(long double y, long double x)
00060 #else
00061        long double __ieee754_atan2l(y,x)
00062        long double  y,x;
00063 #endif
00064 {
00065        long double z;
00066        int64_t k,m,hx,hy,ix,iy;
00067        u_int64_t lx,ly;
00068 
00069        GET_LDOUBLE_WORDS64(hx,lx,x);
00070        ix = hx&0x7fffffffffffffffLL;
00071        GET_LDOUBLE_WORDS64(hy,ly,y);
00072        iy = hy&0x7fffffffffffffffLL;
00073        if(((ix|((lx|-lx)>>63))>0x7fff000000000000LL)||
00074           ((iy|((ly|-ly)>>63))>0x7fff000000000000LL))   /* x or y is NaN */
00075           return x+y;
00076        if(((hx-0x3fff000000000000LL)|lx)==0) return __atanl(y);   /* x=1.0L */
00077        m = ((hy>>63)&1)|((hx>>62)&2);     /* 2*sign(x)+sign(y) */
00078 
00079     /* when y = 0 */
00080        if((iy|ly)==0) {
00081            switch(m) {
00082               case 0:
00083               case 1: return y;    /* atan(+-0,+anything)=+-0 */
00084               case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
00085               case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
00086            }
00087        }
00088     /* when x = 0 */
00089        if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
00090 
00091     /* when x is INF */
00092        if(ix==0x7fff000000000000LL) {
00093            if(iy==0x7fff000000000000LL) {
00094               switch(m) {
00095                   case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
00096                   case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
00097                   case 2: return  3.0L*pi_o_4+tiny;/*atan(+INF,-INF)*/
00098                   case 3: return -3.0L*pi_o_4-tiny;/*atan(-INF,-INF)*/
00099               }
00100            } else {
00101               switch(m) {
00102                   case 0: return  zero  ; /* atan(+...,+INF) */
00103                   case 1: return -zero  ; /* atan(-...,+INF) */
00104                   case 2: return  pi+tiny  ;     /* atan(+...,-INF) */
00105                   case 3: return -pi-tiny  ;     /* atan(-...,-INF) */
00106               }
00107            }
00108        }
00109     /* when y is INF */
00110        if(iy==0x7fff000000000000LL) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
00111 
00112     /* compute y/x */
00113        k = (iy-ix)>>48;
00114        if(k > 120) z=pi_o_2+0.5L*pi_lo;   /* |y/x| >  2**120 */
00115        else if(hx<0&&k<-120) z=0.0L;             /* |y|/x < -2**120 */
00116        else z=__atanl(fabsl(y/x));        /* safe to do y/x */
00117        switch (m) {
00118            case 0: return       z  ;      /* atan(+,+) */
00119            case 1: {
00120                     u_int64_t zh;
00121                     GET_LDOUBLE_MSW64(zh,z);
00122                     SET_LDOUBLE_MSW64(z,zh ^ 0x8000000000000000ULL);
00123                   }
00124                   return       z  ;       /* atan(-,+) */
00125            case 2: return  pi-(z-pi_lo);/* atan(+,-) */
00126            default: /* case 3 */
00127                   return  (z-pi_lo)-pi;/* atan(-,-) */
00128        }
00129 }