Back to index

lightning-sunbird  0.9+nobinonly
e_hypot.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_hypot.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 /* __ieee754_hypot(x,y)
00053  *
00054  * Method :                  
00055  *     If (assume round-to-nearest) z=x*x+y*y 
00056  *     has error less than sqrt(2)/2 ulp, than 
00057  *     sqrt(z) has error less than 1 ulp (exercise).
00058  *
00059  *     So, compute sqrt(x*x+y*y) with some care as 
00060  *     follows to get the error below 1 ulp:
00061  *
00062  *     Assume x>y>0;
00063  *     (if possible, set rounding to round-to-nearest)
00064  *     1. if x > 2y  use
00065  *            x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
00066  *     where x1 = x with lower 32 bits cleared, x2 = x-x1; else
00067  *     2. if x <= 2y use
00068  *            t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
00069  *     where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, 
00070  *     y1= y with lower 32 bits chopped, y2 = y-y1.
00071  *            
00072  *     NOTE: scaling may be necessary if some argument is too 
00073  *           large or too tiny
00074  *
00075  * Special cases:
00076  *     hypot(x,y) is INF if x or y is +INF or -INF; else
00077  *     hypot(x,y) is NAN if x or y is NAN.
00078  *
00079  * Accuracy:
00080  *     hypot(x,y) returns sqrt(x^2+y^2) with error less 
00081  *     than 1 ulps (units in the last place) 
00082  */
00083 
00084 #include "fdlibm.h"
00085 
00086 #ifdef __STDC__
00087        double __ieee754_hypot(double x, double y)
00088 #else
00089        double __ieee754_hypot(x,y)
00090        double x, y;
00091 #endif
00092 {
00093         fd_twoints ux, uy;
00094        double a=x,b=y,t1,t2,y1,y2,w;
00095        int j,k,ha,hb;
00096         
00097         ux.d = x; uy.d = y;
00098        ha = __HI(ux)&0x7fffffff;   /* high word of  x */
00099        hb = __HI(uy)&0x7fffffff;   /* high word of  y */
00100        if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
00101         ux.d = a; uy.d = b;
00102        __HI(ux) = ha;       /* a <- |a| */
00103        __HI(uy) = hb;       /* b <- |b| */
00104         a = ux.d; b = uy.d;
00105        if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
00106        k=0;
00107        if(ha > 0x5f300000) {       /* a>2**500 */
00108           if(ha >= 0x7ff00000) {   /* Inf or NaN */
00109               w = a+b;                    /* for sNaN */
00110                ux.d = a; uy.d = b;
00111               if(((ha&0xfffff)|__LO(ux))==0) w = a;
00112               if(((hb^0x7ff00000)|__LO(uy))==0) w = b;
00113               return w;
00114           }
00115           /* scale a and b by 2**-600 */
00116           ha -= 0x25800000; hb -= 0x25800000;    k += 600;
00117            ux.d = a; uy.d = b;
00118           __HI(ux) = ha;
00119           __HI(uy) = hb;
00120            a = ux.d; b = uy.d;
00121        }
00122        if(hb < 0x20b00000) {       /* b < 2**-500 */
00123            if(hb <= 0x000fffff) {  /* subnormal b or 0 */      
00124                 uy.d = b;
00125               if((hb|(__LO(uy)))==0) return a;
00126               t1=0;
00127                 ux.d = t1;
00128               __HI(ux) = 0x7fd00000;      /* t1=2^1022 */
00129                 t1 = ux.d;
00130               b *= t1;
00131               a *= t1;
00132               k -= 1022;
00133            } else {         /* scale a and b by 2^600 */
00134                ha += 0x25800000;   /* a *= 2^600 */
00135               hb += 0x25800000;    /* b *= 2^600 */
00136               k -= 600;
00137                 ux.d = a; uy.d = b;
00138               __HI(ux) = ha;
00139               __HI(uy) = hb;
00140                 a = ux.d; b = uy.d;
00141            }
00142        }
00143     /* medium size a and b */
00144        w = a-b;
00145        if (w>b) {
00146            t1 = 0;
00147             ux.d = t1;
00148            __HI(ux) = ha;
00149             t1 = ux.d;
00150            t2 = a-t1;
00151            w  = fd_sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
00152        } else {
00153            a  = a+a;
00154            y1 = 0;
00155             ux.d = y1;
00156            __HI(ux) = hb;
00157             y1 = ux.d;
00158            y2 = b - y1;
00159            t1 = 0;
00160             ux.d = t1;
00161            __HI(ux) = ha+0x00100000;
00162             t1 = ux.d;
00163            t2 = a - t1;
00164            w  = fd_sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
00165        }
00166        if(k!=0) {
00167            t1 = 1.0;
00168             ux.d = t1;
00169            __HI(ux) += (k<<20);
00170             t1 = ux.d;
00171            return t1*w;
00172        } else return w;
00173 }