1 /* @(#)e_hypot.c 5.1 93/09/24 */ 2 /* 3 * ==================================================== 4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 * 6 * Developed at SunPro, a Sun Microsystems, Inc. business. 7 * Permission to use, copy, modify, and distribute this 8 * software is freely granted, provided that this notice 9 * is preserved. 10 * ==================================================== 11 */ 12 13 /* hypotl(x,y) 14 * 15 * Method : 16 * If (assume round-to-nearest) z=x*x+y*y 17 * has error less than sqrtl(2)/2 ulp, than 18 * sqrtl(z) has error less than 1 ulp (exercise). 19 * 20 * So, compute sqrtl(x*x+y*y) with some care as 21 * follows to get the error below 1 ulp: 22 * 23 * Assume x>y>0; 24 * (if possible, set rounding to round-to-nearest) 25 * 1. if x > 2y use 26 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y 27 * where x1 = x with lower 64 bits cleared, x2 = x-x1; else 28 * 2. if x <= 2y use 29 * t1*yy1+((x-y)*(x-y)+(t1*y2+t2*y)) 30 * where t1 = 2x with lower 64 bits cleared, t2 = 2x-t1, 31 * yy1= y with lower 64 bits chopped, y2 = y-yy1. 32 * 33 * NOTE: scaling may be necessary if some argument is too 34 * large or too tiny 35 * 36 * Special cases: 37 * hypotl(x,y) is INF if x or y is +INF or -INF; else 38 * hypotl(x,y) is NAN if x or y is NAN. 39 * 40 * Accuracy: 41 * hypotl(x,y) returns sqrtl(x^2+y^2) with error less 42 * than 1 ulps (units in the last place) 43 */ 44 45 #include <math.h> 46 47 #include "math_private.h" 48 49 long double 50 hypotl(long double x, long double y) 51 { 52 long double a,b,t1,t2,yy1,y2,w; 53 int64_t j,k,ha,hb; 54 55 GET_LDOUBLE_MSW64(ha,x); 56 ha &= 0x7fffffffffffffffLL; 57 GET_LDOUBLE_MSW64(hb,y); 58 hb &= 0x7fffffffffffffffLL; 59 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} 60 SET_LDOUBLE_MSW64(a,ha); /* a <- |a| */ 61 SET_LDOUBLE_MSW64(b,hb); /* b <- |b| */ 62 if((ha-hb)>0x78000000000000LL) {return a+b;} /* x/y > 2**120 */ 63 k=0; 64 if(ha > 0x5f3f000000000000LL) { /* a>2**8000 */ 65 if(ha >= 0x7fff000000000000LL) { /* Inf or NaN */ 66 u_int64_t low; 67 w = a+b; /* for sNaN */ 68 GET_LDOUBLE_LSW64(low,a); 69 if(((ha&0xffffffffffffLL)|low)==0) w = a; 70 GET_LDOUBLE_LSW64(low,b); 71 if(((hb^0x7fff000000000000LL)|low)==0) w = b; 72 return w; 73 } 74 /* scale a and b by 2**-9600 */ 75 ha -= 0x2580000000000000LL; 76 hb -= 0x2580000000000000LL; k += 9600; 77 SET_LDOUBLE_MSW64(a,ha); 78 SET_LDOUBLE_MSW64(b,hb); 79 } 80 if(hb < 0x20bf000000000000LL) { /* b < 2**-8000 */ 81 if(hb <= 0x0000ffffffffffffLL) { /* subnormal b or 0 */ 82 u_int64_t low; 83 GET_LDOUBLE_LSW64(low,b); 84 if((hb|low)==0) return a; 85 t1=0; 86 SET_LDOUBLE_MSW64(t1,0x7ffd000000000000LL); /* t1=2^16382 */ 87 b *= t1; 88 a *= t1; 89 k -= 16382; 90 } else { /* scale a and b by 2^9600 */ 91 ha += 0x2580000000000000LL; /* a *= 2^9600 */ 92 hb += 0x2580000000000000LL; /* b *= 2^9600 */ 93 k -= 9600; 94 SET_LDOUBLE_MSW64(a,ha); 95 SET_LDOUBLE_MSW64(b,hb); 96 } 97 } 98 /* medium size a and b */ 99 w = a-b; 100 if (w>b) { 101 t1 = 0; 102 SET_LDOUBLE_MSW64(t1,ha); 103 t2 = a-t1; 104 w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1))); 105 } else { 106 a = a+a; 107 yy1 = 0; 108 SET_LDOUBLE_MSW64(yy1,hb); 109 y2 = b - yy1; 110 t1 = 0; 111 SET_LDOUBLE_MSW64(t1,ha+0x0001000000000000LL); 112 t2 = a - t1; 113 w = sqrtl(t1*yy1-(w*(-w)-(t1*y2+t2*b))); 114 } 115 if(k!=0) { 116 u_int64_t high; 117 t1 = 1.0L; 118 GET_LDOUBLE_MSW64(high,t1); 119 SET_LDOUBLE_MSW64(t1,high+(k<<48)); 120 return t1*w; 121 } else return w; 122 } 123 DEF_STD(hypotl); 124