1 2 /* 3 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 /* __ieee754_hypot(x,y) 28 * 29 * Method : 30 * If (assume round-to-nearest) z=x*x+y*y 31 * has error less than sqrt(2)/2 ulp, than 32 * sqrt(z) has error less than 1 ulp (exercise). 33 * 34 * So, compute sqrt(x*x+y*y) with some care as 35 * follows to get the error below 1 ulp: 36 * 37 * Assume x>y>0; 38 * (if possible, set rounding to round-to-nearest) 39 * 1. if x > 2y use 40 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y 41 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else 42 * 2. if x <= 2y use 43 * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y)) 44 * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, 45 * y1= y with lower 32 bits chopped, y2 = y-y1. 46 * 47 * NOTE: scaling may be necessary if some argument is too 48 * large or too tiny 49 * 50 * Special cases: 51 * hypot(x,y) is INF if x or y is +INF or -INF; else 52 * hypot(x,y) is NAN if x or y is NAN. 53 * 54 * Accuracy: 55 * hypot(x,y) returns sqrt(x^2+y^2) with error less 56 * than 1 ulps (units in the last place) 57 */ 58 59 #include "fdlibm.h" 60 61 #ifdef __STDC__ 62 double __ieee754_hypot(double x, double y) 63 #else 64 double __ieee754_hypot(x,y) 65 double x, y; 66 #endif 67 { 68 double a=x,b=y,t1,t2,y1,y2,w; 69 int j,k,ha,hb; 70 71 ha = __HI(x)&0x7fffffff; /* high word of x */ 72 hb = __HI(y)&0x7fffffff; /* high word of y */ 73 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} 74 __HI(a) = ha; /* a <- |a| */ 75 __HI(b) = hb; /* b <- |b| */ 76 if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */ 77 k=0; 78 if(ha > 0x5f300000) { /* a>2**500 */ 79 if(ha >= 0x7ff00000) { /* Inf or NaN */ 80 w = a+b; /* for sNaN */ 81 if(((ha&0xfffff)|__LO(a))==0) w = a; 82 if(((hb^0x7ff00000)|__LO(b))==0) w = b; 83 return w; 84 } 85 /* scale a and b by 2**-600 */ 86 ha -= 0x25800000; hb -= 0x25800000; k += 600; 87 __HI(a) = ha; 88 __HI(b) = hb; 89 } 90 if(hb < 0x20b00000) { /* b < 2**-500 */ 91 if(hb <= 0x000fffff) { /* subnormal b or 0 */ 92 if((hb|(__LO(b)))==0) return a; 93 t1=0; 94 __HI(t1) = 0x7fd00000; /* t1=2^1022 */ 95 b *= t1; 96 a *= t1; 97 k -= 1022; 98 } else { /* scale a and b by 2^600 */ 99 ha += 0x25800000; /* a *= 2^600 */ 100 hb += 0x25800000; /* b *= 2^600 */ 101 k -= 600; 102 __HI(a) = ha; 103 __HI(b) = hb; 104 } 105 } 106 /* medium size a and b */ 107 w = a-b; 108 if (w>b) { 109 t1 = 0; 110 __HI(t1) = ha; 111 t2 = a-t1; 112 w = sqrt(t1*t1-(b*(-b)-t2*(a+t1))); 113 } else { 114 a = a+a; 115 y1 = 0; 116 __HI(y1) = hb; 117 y2 = b - y1; 118 t1 = 0; 119 __HI(t1) = ha+0x00100000; 120 t2 = a - t1; 121 w = sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b))); 122 } 123 if(k!=0) { 124 t1 = 1.0; 125 __HI(t1) += (k<<20); 126 return t1*w; 127 } else return w; 128 }