base/math/e_hypot.c

Go to the documentation of this file.
00001 /* @(#)e_hypot.c 5.1 93/09/24 */
00002 /*
00003  * ====================================================
00004  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00005  *
00006  * Developed at SunPro, a Sun Microsystems, Inc. business.
00007  * Permission to use, copy, modify, and distribute this
00008  * software is freely granted, provided that this notice 
00009  * is preserved.
00010  * ====================================================
00011  */
00012 
00013 #if defined(LIBM_SCCS) && !defined(lint)
00014 static char rcsid[] = "$NetBSD: e_hypot.c,v 1.9 1995/05/12 04:57:27 jtc Exp $";
00015 #endif
00016 
00017 /* __ieee754_hypot(x,y)
00018  *
00019  * Method :                  
00020  *  If (assume round-to-nearest) z=x*x+y*y 
00021  *  has error less than sqrt(2)/2 ulp, than 
00022  *  sqrt(z) has error less than 1 ulp (exercise).
00023  *
00024  *  So, compute sqrt(x*x+y*y) with some care as 
00025  *  follows to get the error below 1 ulp:
00026  *
00027  *  Assume x>y>0;
00028  *  (if possible, set rounding to round-to-nearest)
00029  *  1. if x > 2y  use
00030  *      x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
00031  *  where x1 = x with lower 32 bits cleared, x2 = x-x1; else
00032  *  2. if x <= 2y use
00033  *      t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
00034  *  where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, 
00035  *  y1= y with lower 32 bits chopped, y2 = y-y1.
00036  *      
00037  *  NOTE: scaling may be necessary if some argument is too 
00038  *        large or too tiny
00039  *
00040  * Special cases:
00041  *  hypot(x,y) is INF if x or y is +INF or -INF; else
00042  *  hypot(x,y) is NAN if x or y is NAN.
00043  *
00044  * Accuracy:
00045  *  hypot(x,y) returns sqrt(x^2+y^2) with error less 
00046  *  than 1 ulps (units in the last place) 
00047  */
00048 
00049 #include "math.h"
00050 #include "mathP.h"
00051 
00052 #ifdef __STDC__
00053     double __ieee754_hypot(double x, double y)
00054 #else
00055     double __ieee754_hypot(x,y)
00056     double x, y;
00057 #endif
00058 {
00059     double a=x,b=y,t1,t2,y1,y2,w;
00060     int32_t j,k,ha,hb;
00061 
00062     GET_HIGH_WORD(ha,x);
00063     ha &= 0x7fffffff;
00064     GET_HIGH_WORD(hb,y);
00065     hb &= 0x7fffffff;
00066     if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
00067     SET_HIGH_WORD(a,ha);    /* a <- |a| */
00068     SET_HIGH_WORD(b,hb);    /* b <- |b| */
00069     if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
00070     k=0;
00071     if(ha > 0x5f300000) {   /* a>2**500 */
00072        if(ha >= 0x7ff00000) {   /* Inf or NaN */
00073            u_int32_t low;
00074            w = a+b;         /* for sNaN */
00075            GET_LOW_WORD(low,a);
00076            if(((ha&0xfffff)|low)==0) w = a;
00077            GET_LOW_WORD(low,b);
00078            if(((hb^0x7ff00000)|low)==0) w = b;
00079            return w;
00080        }
00081        /* scale a and b by 2**-600 */
00082        ha -= 0x25800000; hb -= 0x25800000;  k += 600;
00083        SET_HIGH_WORD(a,ha);
00084        SET_HIGH_WORD(b,hb);
00085     }
00086     if(hb < 0x20b00000) {   /* b < 2**-500 */
00087         if(hb <= 0x000fffff) {  /* subnormal b or 0 */  
00088             u_int32_t low;
00089         GET_LOW_WORD(low,b);
00090         if((hb|low)==0) return a;
00091         t1=0;
00092         SET_HIGH_WORD(t1,0x7fd00000);   /* t1=2^1022 */
00093         b *= t1;
00094         a *= t1;
00095         k -= 1022;
00096         } else {        /* scale a and b by 2^600 */
00097             ha += 0x25800000;   /* a *= 2^600 */
00098         hb += 0x25800000;   /* b *= 2^600 */
00099         k -= 600;
00100         SET_HIGH_WORD(a,ha);
00101         SET_HIGH_WORD(b,hb);
00102         }
00103     }
00104     /* medium size a and b */
00105     w = a-b;
00106     if (w>b) {
00107         t1 = 0;
00108         SET_HIGH_WORD(t1,ha);
00109         t2 = a-t1;
00110         w  = __ieee754_sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
00111     } else {
00112         a  = a+a;
00113         y1 = 0;
00114         SET_HIGH_WORD(y1,hb);
00115         y2 = b - y1;
00116         t1 = 0;
00117         SET_HIGH_WORD(t1,ha+0x00100000);
00118         t2 = a - t1;
00119         w  = __ieee754_sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
00120     }
00121     if(k!=0) {
00122         u_int32_t high;
00123         t1 = 1.0;
00124         GET_HIGH_WORD(high,t1);
00125         SET_HIGH_WORD(t1,high+(k<<20));
00126         return t1*w;
00127     } else return w;
00128 }

Generated on Tue Feb 2 17:46:05 2010 for RTAI API by  doxygen 1.4.7