Back to index

lightning-sunbird  0.9+nobinonly
e_atan2.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)e_atan2.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  *
00051  */
00052 
00053 /* __ieee754_atan2(y,x)
00054  * Method :
00055  *     1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
00056  *     2. Reduce x to positive by (if x and y are unexceptional): 
00057  *            ARG (x+iy) = arctan(y/x)       ... if x > 0,
00058  *            ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
00059  *
00060  * Special cases:
00061  *
00062  *     ATAN2((anything), NaN ) is NaN;
00063  *     ATAN2(NAN , (anything) ) is NaN;
00064  *     ATAN2(+-0, +(anything but NaN)) is +-0  ;
00065  *     ATAN2(+-0, -(anything but NaN)) is +-pi ;
00066  *     ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
00067  *     ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
00068  *     ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
00069  *     ATAN2(+-INF,+INF ) is +-pi/4 ;
00070  *     ATAN2(+-INF,-INF ) is +-3pi/4;
00071  *     ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
00072  *
00073  * Constants:
00074  * The hexadecimal values are the intended ones for the following 
00075  * constants. The decimal values may be used, provided that the 
00076  * compiler will convert from decimal to binary accurately enough 
00077  * to produce the hexadecimal values shown.
00078  */
00079 
00080 #include "fdlibm.h"
00081 
00082 #ifdef __STDC__
00083 static const double 
00084 #else
00085 static double 
00086 #endif
00087 tiny  = 1.0e-300,
00088 zero  = 0.0,
00089 pi_o_4  = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
00090 pi_o_2  = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
00091 pi      = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
00092 pi_lo   = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
00093 
00094 #ifdef __STDC__
00095        double __ieee754_atan2(double y, double x)
00096 #else
00097        double __ieee754_atan2(y,x)
00098        double  y,x;
00099 #endif
00100 {  
00101         fd_twoints ux, uy, uz;
00102        double z;
00103        int k,m,hx,hy,ix,iy;
00104        unsigned lx,ly;
00105 
00106         ux.d = x; uy.d = y;
00107        hx = __HI(ux); ix = hx&0x7fffffff;
00108        lx = __LO(ux);
00109        hy = __HI(uy); iy = hy&0x7fffffff;
00110        ly = __LO(uy);
00111        if(((ix|((lx|-(int)lx)>>31))>0x7ff00000)||
00112           ((iy|((ly|-(int)ly)>>31))>0x7ff00000)) /* x or y is NaN */
00113           return x+y;
00114        if(((hx-0x3ff00000)|lx)==0) return fd_atan(y);   /* x=1.0 */
00115        m = ((hy>>31)&1)|((hx>>30)&2);     /* 2*sign(x)+sign(y) */
00116 
00117     /* when y = 0 */
00118        if((iy|ly)==0) {
00119            switch(m) {
00120               case 0: 
00121               case 1: return y;    /* atan(+-0,+anything)=+-0 */
00122               case 2: return  pi+tiny;/* atan(+0,-anything) = pi */
00123               case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
00124            }
00125        }
00126     /* when x = 0 */
00127        if((ix|lx)==0) return (hy<0)?  -pi_o_2-tiny: pi_o_2+tiny;
00128            
00129     /* when x is INF */
00130        if(ix==0x7ff00000) {
00131            if(iy==0x7ff00000) {
00132               switch(m) {
00133                   case 0: return  pi_o_4+tiny;/* atan(+INF,+INF) */
00134                   case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */
00135                   case 2: return  3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/
00136                   case 3: return -3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/
00137               }
00138            } else {
00139               switch(m) {
00140                   case 0: return  zero  ; /* atan(+...,+INF) */
00141                   case 1: return -zero  ; /* atan(-...,+INF) */
00142                   case 2: return  pi+tiny  ;     /* atan(+...,-INF) */
00143                   case 3: return -pi-tiny  ;     /* atan(-...,-INF) */
00144               }
00145            }
00146        }
00147     /* when y is INF */
00148        if(iy==0x7ff00000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny;
00149 
00150     /* compute y/x */
00151        k = (iy-ix)>>20;
00152        if(k > 60) z=pi_o_2+0.5*pi_lo;     /* |y/x| >  2**60 */
00153        else if(hx<0&&k<-60) z=0.0;        /* |y|/x < -2**60 */
00154        else z=fd_atan(fd_fabs(y/x));             /* safe to do y/x */
00155        switch (m) {
00156            case 0: return       z  ;      /* atan(+,+) */
00157            case 1: uz.d = z;
00158                     __HI(uz) ^= 0x80000000;
00159                     z = uz.d;
00160                   return       z  ;       /* atan(-,+) */
00161            case 2: return  pi-(z-pi_lo);/* atan(+,-) */
00162            default: /* case 3 */
00163                   return  (z-pi_lo)-pi;/* atan(-,-) */
00164        }
00165 }