13f708241SDavid Schultz
23a8617a8SJordan K. Hubbard /*
33a8617a8SJordan K. Hubbard * ====================================================
43a8617a8SJordan K. Hubbard * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
53a8617a8SJordan K. Hubbard *
63f708241SDavid Schultz * Developed at SunSoft, a Sun Microsystems, Inc. business.
73a8617a8SJordan K. Hubbard * Permission to use, copy, modify, and distribute this
83a8617a8SJordan K. Hubbard * software is freely granted, provided that this notice
93a8617a8SJordan K. Hubbard * is preserved.
103a8617a8SJordan K. Hubbard * ====================================================
113a8617a8SJordan K. Hubbard */
123a8617a8SJordan K. Hubbard
13*99843eb8SSteve Kargl /* hypot(x,y)
143a8617a8SJordan K. Hubbard *
153a8617a8SJordan K. Hubbard * Method :
163a8617a8SJordan K. Hubbard * If (assume round-to-nearest) z=x*x+y*y
173a8617a8SJordan K. Hubbard * has error less than sqrt(2)/2 ulp, than
183a8617a8SJordan K. Hubbard * sqrt(z) has error less than 1 ulp (exercise).
193a8617a8SJordan K. Hubbard *
203a8617a8SJordan K. Hubbard * So, compute sqrt(x*x+y*y) with some care as
213a8617a8SJordan K. Hubbard * follows to get the error below 1 ulp:
223a8617a8SJordan K. Hubbard *
233a8617a8SJordan K. Hubbard * Assume x>y>0;
243a8617a8SJordan K. Hubbard * (if possible, set rounding to round-to-nearest)
253a8617a8SJordan K. Hubbard * 1. if x > 2y use
263a8617a8SJordan K. Hubbard * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
273a8617a8SJordan K. Hubbard * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
283a8617a8SJordan K. Hubbard * 2. if x <= 2y use
293a8617a8SJordan K. Hubbard * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
303a8617a8SJordan K. Hubbard * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
313a8617a8SJordan K. Hubbard * y1= y with lower 32 bits chopped, y2 = y-y1.
323a8617a8SJordan K. Hubbard *
333a8617a8SJordan K. Hubbard * NOTE: scaling may be necessary if some argument is too
343a8617a8SJordan K. Hubbard * large or too tiny
353a8617a8SJordan K. Hubbard *
363a8617a8SJordan K. Hubbard * Special cases:
373a8617a8SJordan K. Hubbard * hypot(x,y) is INF if x or y is +INF or -INF; else
383a8617a8SJordan K. Hubbard * hypot(x,y) is NAN if x or y is NAN.
393a8617a8SJordan K. Hubbard *
403a8617a8SJordan K. Hubbard * Accuracy:
413a8617a8SJordan K. Hubbard * hypot(x,y) returns sqrt(x^2+y^2) with error less
423a8617a8SJordan K. Hubbard * than 1 ulps (units in the last place)
433a8617a8SJordan K. Hubbard */
443a8617a8SJordan K. Hubbard
45a641fc76SDavid Schultz #include <float.h>
46a641fc76SDavid Schultz
473a8617a8SJordan K. Hubbard #include "math.h"
483a8617a8SJordan K. Hubbard #include "math_private.h"
493a8617a8SJordan K. Hubbard
5059b19ff1SAlfred Perlstein double
hypot(double x,double y)51*99843eb8SSteve Kargl hypot(double x, double y)
523a8617a8SJordan K. Hubbard {
53e595c01fSDavid Schultz double a,b,t1,t2,y1,y2,w;
543a8617a8SJordan K. Hubbard int32_t j,k,ha,hb;
553a8617a8SJordan K. Hubbard
563a8617a8SJordan K. Hubbard GET_HIGH_WORD(ha,x);
573a8617a8SJordan K. Hubbard ha &= 0x7fffffff;
583a8617a8SJordan K. Hubbard GET_HIGH_WORD(hb,y);
593a8617a8SJordan K. Hubbard hb &= 0x7fffffff;
603a8617a8SJordan K. Hubbard if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
6142ee187cSBruce Evans a = fabs(a);
6242ee187cSBruce Evans b = fabs(b);
633a8617a8SJordan K. Hubbard if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
643a8617a8SJordan K. Hubbard k=0;
653a8617a8SJordan K. Hubbard if(ha > 0x5f300000) { /* a>2**500 */
663a8617a8SJordan K. Hubbard if(ha >= 0x7ff00000) { /* Inf or NaN */
673a8617a8SJordan K. Hubbard u_int32_t low;
683365b45eSBruce Evans /* Use original arg order iff result is NaN; quieten sNaNs. */
696f1b8a07SBruce Evans w = fabsl(x+0.0L)-fabs(y+0);
703a8617a8SJordan K. Hubbard GET_LOW_WORD(low,a);
713a8617a8SJordan K. Hubbard if(((ha&0xfffff)|low)==0) w = a;
723a8617a8SJordan K. Hubbard GET_LOW_WORD(low,b);
733a8617a8SJordan K. Hubbard if(((hb^0x7ff00000)|low)==0) w = b;
743a8617a8SJordan K. Hubbard return w;
753a8617a8SJordan K. Hubbard }
763a8617a8SJordan K. Hubbard /* scale a and b by 2**-600 */
773a8617a8SJordan K. Hubbard ha -= 0x25800000; hb -= 0x25800000; k += 600;
783a8617a8SJordan K. Hubbard SET_HIGH_WORD(a,ha);
793a8617a8SJordan K. Hubbard SET_HIGH_WORD(b,hb);
803a8617a8SJordan K. Hubbard }
813a8617a8SJordan K. Hubbard if(hb < 0x20b00000) { /* b < 2**-500 */
823a8617a8SJordan K. Hubbard if(hb <= 0x000fffff) { /* subnormal b or 0 */
833a8617a8SJordan K. Hubbard u_int32_t low;
843a8617a8SJordan K. Hubbard GET_LOW_WORD(low,b);
853a8617a8SJordan K. Hubbard if((hb|low)==0) return a;
863a8617a8SJordan K. Hubbard t1=0;
873a8617a8SJordan K. Hubbard SET_HIGH_WORD(t1,0x7fd00000); /* t1=2^1022 */
883a8617a8SJordan K. Hubbard b *= t1;
893a8617a8SJordan K. Hubbard a *= t1;
903a8617a8SJordan K. Hubbard k -= 1022;
913a8617a8SJordan K. Hubbard } else { /* scale a and b by 2^600 */
923a8617a8SJordan K. Hubbard ha += 0x25800000; /* a *= 2^600 */
933a8617a8SJordan K. Hubbard hb += 0x25800000; /* b *= 2^600 */
943a8617a8SJordan K. Hubbard k -= 600;
953a8617a8SJordan K. Hubbard SET_HIGH_WORD(a,ha);
963a8617a8SJordan K. Hubbard SET_HIGH_WORD(b,hb);
973a8617a8SJordan K. Hubbard }
983a8617a8SJordan K. Hubbard }
993a8617a8SJordan K. Hubbard /* medium size a and b */
1003a8617a8SJordan K. Hubbard w = a-b;
1013a8617a8SJordan K. Hubbard if (w>b) {
1023a8617a8SJordan K. Hubbard t1 = 0;
1033a8617a8SJordan K. Hubbard SET_HIGH_WORD(t1,ha);
1043a8617a8SJordan K. Hubbard t2 = a-t1;
1053f708241SDavid Schultz w = sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
1063a8617a8SJordan K. Hubbard } else {
1073a8617a8SJordan K. Hubbard a = a+a;
1083a8617a8SJordan K. Hubbard y1 = 0;
1093a8617a8SJordan K. Hubbard SET_HIGH_WORD(y1,hb);
1103a8617a8SJordan K. Hubbard y2 = b - y1;
1113a8617a8SJordan K. Hubbard t1 = 0;
1123a8617a8SJordan K. Hubbard SET_HIGH_WORD(t1,ha+0x00100000);
1133a8617a8SJordan K. Hubbard t2 = a - t1;
1143f708241SDavid Schultz w = sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
1153a8617a8SJordan K. Hubbard }
1163a8617a8SJordan K. Hubbard if(k!=0) {
11783fa0a28SDimitry Andric t1 = 0.0;
11883fa0a28SDimitry Andric SET_HIGH_WORD(t1,(1023+k)<<20);
1193a8617a8SJordan K. Hubbard return t1*w;
1203a8617a8SJordan K. Hubbard } else return w;
1213a8617a8SJordan K. Hubbard }
122a641fc76SDavid Schultz
123a641fc76SDavid Schultz #if LDBL_MANT_DIG == 53
124a641fc76SDavid Schultz __weak_reference(hypot, hypotl);
125a641fc76SDavid Schultz #endif
126