POK
|
00001 /* 00002 * POK header 00003 * 00004 * The following file is a part of the POK project. Any modification should 00005 * made according to the POK licence. You CANNOT use this file or a part of 00006 * this file is this part of a file for your own project 00007 * 00008 * For more information on the POK licence, please see our LICENCE FILE 00009 * 00010 * Please follow the coding guidelines described in doc/CODING_GUIDELINES 00011 * 00012 * Copyright (c) 2007-2009 POK team 00013 * 00014 * Created by julien on Fri Jan 30 14:41:34 2009 00015 */ 00016 00017 /* @(#)e_hypot.c 5.1 93/09/24 */ 00018 /* 00019 * ==================================================== 00020 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00021 * 00022 * Developed at SunPro, a Sun Microsystems, Inc. business. 00023 * Permission to use, copy, modify, and distribute this 00024 * software is freely granted, provided that this notice 00025 * is preserved. 00026 * ==================================================== 00027 */ 00028 00029 /* __ieee754_hypot(x,y) 00030 * 00031 * Method : 00032 * If (assume round-to-nearest) z=x*x+y*y 00033 * has error less than sqrt(2)/2 ulp, than 00034 * sqrt(z) has error less than 1 ulp (exercise). 00035 * 00036 * So, compute sqrt(x*x+y*y) with some care as 00037 * follows to get the error below 1 ulp: 00038 * 00039 * Assume x>y>0; 00040 * (if possible, set rounding to round-to-nearest) 00041 * 1. if x > 2y use 00042 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y 00043 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else 00044 * 2. if x <= 2y use 00045 * t1*yy1+((x-y)*(x-y)+(t1*y2+t2*y)) 00046 * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, 00047 * yy1= y with lower 32 bits chopped, y2 = y-yy1. 00048 * 00049 * NOTE: scaling may be necessary if some argument is too 00050 * large or too tiny 00051 * 00052 * Special cases: 00053 * hypot(x,y) is INF if x or y is +INF or -INF; else 00054 * hypot(x,y) is NAN if x or y is NAN. 00055 * 00056 * Accuracy: 00057 * hypot(x,y) returns sqrt(x^2+y^2) with error less 00058 * than 1 ulps (units in the last place) 00059 */ 00060 00061 #ifdef POK_NEEDS_LIBMATH 00062 00063 #include "math_private.h" 00064 00065 double 00066 __ieee754_hypot(double x, double y) 00067 { 00068 double a=x,b=y,t1,t2,yy1,y2,w; 00069 int32_t j,k,ha,hb; 00070 00071 GET_HIGH_WORD(ha,x); 00072 ha &= 0x7fffffff; 00073 GET_HIGH_WORD(hb,y); 00074 hb &= 0x7fffffff; 00075 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} 00076 SET_HIGH_WORD(a,ha); /* a <- |a| */ 00077 SET_HIGH_WORD(b,hb); /* b <- |b| */ 00078 if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */ 00079 k=0; 00080 if(ha > 0x5f300000) { /* a>2**500 */ 00081 if(ha >= 0x7ff00000) { /* Inf or NaN */ 00082 uint32_t low; 00083 w = a+b; /* for sNaN */ 00084 GET_LOW_WORD(low,a); 00085 if(((ha&0xfffff)|low)==0) w = a; 00086 GET_LOW_WORD(low,b); 00087 if(((hb^0x7ff00000)|low)==0) w = b; 00088 return w; 00089 } 00090 /* scale a and b by 2**-600 */ 00091 ha -= 0x25800000; hb -= 0x25800000; k += 600; 00092 SET_HIGH_WORD(a,ha); 00093 SET_HIGH_WORD(b,hb); 00094 } 00095 if(hb < 0x20b00000) { /* b < 2**-500 */ 00096 if(hb <= 0x000fffff) { /* subnormal b or 0 */ 00097 uint32_t low; 00098 GET_LOW_WORD(low,b); 00099 if((hb|low)==0) return a; 00100 t1=0; 00101 SET_HIGH_WORD(t1,0x7fd00000); /* t1=2^1022 */ 00102 b *= t1; 00103 a *= t1; 00104 k -= 1022; 00105 } else { /* scale a and b by 2^600 */ 00106 ha += 0x25800000; /* a *= 2^600 */ 00107 hb += 0x25800000; /* b *= 2^600 */ 00108 k -= 600; 00109 SET_HIGH_WORD(a,ha); 00110 SET_HIGH_WORD(b,hb); 00111 } 00112 } 00113 /* medium size a and b */ 00114 w = a-b; 00115 if (w>b) { 00116 t1 = 0; 00117 SET_HIGH_WORD(t1,ha); 00118 t2 = a-t1; 00119 w = __ieee754_sqrt(t1*t1-(b*(-b)-t2*(a+t1))); 00120 } else { 00121 a = a+a; 00122 yy1 = 0; 00123 SET_HIGH_WORD(yy1,hb); 00124 y2 = b - yy1; 00125 t1 = 0; 00126 SET_HIGH_WORD(t1,ha+0x00100000); 00127 t2 = a - t1; 00128 w = __ieee754_sqrt(t1*yy1-(w*(-w)-(t1*y2+t2*b))); 00129 } 00130 if(k!=0) { 00131 uint32_t high; 00132 t1 = 1.0; 00133 GET_HIGH_WORD(high,t1); 00134 SET_HIGH_WORD(t1,high+(k<<20)); 00135 return t1*w; 00136 } else return w; 00137 } 00138 00139 #endif 00140