xref: /minix/lib/libm/src/e_hypot.c (revision 2fe8fb19)
1*2fe8fb19SBen Gras /* @(#)e_hypot.c 5.1 93/09/24 */
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras  * ====================================================
4*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
8*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
9*2fe8fb19SBen Gras  * is preserved.
10*2fe8fb19SBen Gras  * ====================================================
11*2fe8fb19SBen Gras  */
12*2fe8fb19SBen Gras 
13*2fe8fb19SBen Gras #include <sys/cdefs.h>
14*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*2fe8fb19SBen Gras __RCSID("$NetBSD: e_hypot.c,v 1.13 2008/04/25 22:21:53 christos Exp $");
16*2fe8fb19SBen Gras #endif
17*2fe8fb19SBen Gras 
18*2fe8fb19SBen Gras /* __ieee754_hypot(x,y)
19*2fe8fb19SBen Gras  *
20*2fe8fb19SBen Gras  * Method :
21*2fe8fb19SBen Gras  *	If (assume round-to-nearest) z=x*x+y*y
22*2fe8fb19SBen Gras  *	has error less than sqrt(2)/2 ulp, than
23*2fe8fb19SBen Gras  *	sqrt(z) has error less than 1 ulp (exercise).
24*2fe8fb19SBen Gras  *
25*2fe8fb19SBen Gras  *	So, compute sqrt(x*x+y*y) with some care as
26*2fe8fb19SBen Gras  *	follows to get the error below 1 ulp:
27*2fe8fb19SBen Gras  *
28*2fe8fb19SBen Gras  *	Assume x>y>0;
29*2fe8fb19SBen Gras  *	(if possible, set rounding to round-to-nearest)
30*2fe8fb19SBen Gras  *	1. if x > 2y  use
31*2fe8fb19SBen Gras  *		x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
32*2fe8fb19SBen Gras  *	where x1 = x with lower 32 bits cleared, x2 = x-x1; else
33*2fe8fb19SBen Gras  *	2. if x <= 2y use
34*2fe8fb19SBen Gras  *		t1*yy1+((x-y)*(x-y)+(t1*y2+t2*y))
35*2fe8fb19SBen Gras  *	where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
36*2fe8fb19SBen Gras  *	yy1= y with lower 32 bits chopped, y2 = y-yy1.
37*2fe8fb19SBen Gras  *
38*2fe8fb19SBen Gras  *	NOTE: scaling may be necessary if some argument is too
39*2fe8fb19SBen Gras  *	      large or too tiny
40*2fe8fb19SBen Gras  *
41*2fe8fb19SBen Gras  * Special cases:
42*2fe8fb19SBen Gras  *	hypot(x,y) is INF if x or y is +INF or -INF; else
43*2fe8fb19SBen Gras  *	hypot(x,y) is NAN if x or y is NAN.
44*2fe8fb19SBen Gras  *
45*2fe8fb19SBen Gras  * Accuracy:
46*2fe8fb19SBen Gras  * 	hypot(x,y) returns sqrt(x^2+y^2) with error less
47*2fe8fb19SBen Gras  * 	than 1 ulps (units in the last place)
48*2fe8fb19SBen Gras  */
49*2fe8fb19SBen Gras 
50*2fe8fb19SBen Gras #include "math.h"
51*2fe8fb19SBen Gras #include "math_private.h"
52*2fe8fb19SBen Gras 
53*2fe8fb19SBen Gras double
__ieee754_hypot(double x,double y)54*2fe8fb19SBen Gras __ieee754_hypot(double x, double y)
55*2fe8fb19SBen Gras {
56*2fe8fb19SBen Gras 	double a=x,b=y,t1,t2,yy1,y2,w;
57*2fe8fb19SBen Gras 	int32_t j,k,ha,hb;
58*2fe8fb19SBen Gras 
59*2fe8fb19SBen Gras 	GET_HIGH_WORD(ha,x);
60*2fe8fb19SBen Gras 	ha &= 0x7fffffff;
61*2fe8fb19SBen Gras 	GET_HIGH_WORD(hb,y);
62*2fe8fb19SBen Gras 	hb &= 0x7fffffff;
63*2fe8fb19SBen Gras 	if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
64*2fe8fb19SBen Gras 	SET_HIGH_WORD(a,ha);	/* a <- |a| */
65*2fe8fb19SBen Gras 	SET_HIGH_WORD(b,hb);	/* b <- |b| */
66*2fe8fb19SBen Gras 	if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
67*2fe8fb19SBen Gras 	k=0;
68*2fe8fb19SBen Gras 	if(ha > 0x5f300000) {	/* a>2**500 */
69*2fe8fb19SBen Gras 	   if(ha >= 0x7ff00000) {	/* Inf or NaN */
70*2fe8fb19SBen Gras 	       u_int32_t low;
71*2fe8fb19SBen Gras 	       w = a+b;			/* for sNaN */
72*2fe8fb19SBen Gras 	       GET_LOW_WORD(low,a);
73*2fe8fb19SBen Gras 	       if(((ha&0xfffff)|low)==0) w = a;
74*2fe8fb19SBen Gras 	       GET_LOW_WORD(low,b);
75*2fe8fb19SBen Gras 	       if(((hb^0x7ff00000)|low)==0) w = b;
76*2fe8fb19SBen Gras 	       return w;
77*2fe8fb19SBen Gras 	   }
78*2fe8fb19SBen Gras 	   /* scale a and b by 2**-600 */
79*2fe8fb19SBen Gras 	   ha -= 0x25800000; hb -= 0x25800000;	k += 600;
80*2fe8fb19SBen Gras 	   SET_HIGH_WORD(a,ha);
81*2fe8fb19SBen Gras 	   SET_HIGH_WORD(b,hb);
82*2fe8fb19SBen Gras 	}
83*2fe8fb19SBen Gras 	if(hb < 0x20b00000) {	/* b < 2**-500 */
84*2fe8fb19SBen Gras 	    if(hb <= 0x000fffff) {	/* subnormal b or 0 */
85*2fe8fb19SBen Gras 	        u_int32_t low;
86*2fe8fb19SBen Gras 		GET_LOW_WORD(low,b);
87*2fe8fb19SBen Gras 		if((hb|low)==0) return a;
88*2fe8fb19SBen Gras 		t1=0;
89*2fe8fb19SBen Gras 		SET_HIGH_WORD(t1,0x7fd00000);	/* t1=2^1022 */
90*2fe8fb19SBen Gras 		b *= t1;
91*2fe8fb19SBen Gras 		a *= t1;
92*2fe8fb19SBen Gras 		k -= 1022;
93*2fe8fb19SBen Gras 	    } else {		/* scale a and b by 2^600 */
94*2fe8fb19SBen Gras 	        ha += 0x25800000; 	/* a *= 2^600 */
95*2fe8fb19SBen Gras 		hb += 0x25800000;	/* b *= 2^600 */
96*2fe8fb19SBen Gras 		k -= 600;
97*2fe8fb19SBen Gras 		SET_HIGH_WORD(a,ha);
98*2fe8fb19SBen Gras 		SET_HIGH_WORD(b,hb);
99*2fe8fb19SBen Gras 	    }
100*2fe8fb19SBen Gras 	}
101*2fe8fb19SBen Gras     /* medium size a and b */
102*2fe8fb19SBen Gras 	w = a-b;
103*2fe8fb19SBen Gras 	if (w>b) {
104*2fe8fb19SBen Gras 	    t1 = 0;
105*2fe8fb19SBen Gras 	    SET_HIGH_WORD(t1,ha);
106*2fe8fb19SBen Gras 	    t2 = a-t1;
107*2fe8fb19SBen Gras 	    w  = __ieee754_sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
108*2fe8fb19SBen Gras 	} else {
109*2fe8fb19SBen Gras 	    a  = a+a;
110*2fe8fb19SBen Gras 	    yy1 = 0;
111*2fe8fb19SBen Gras 	    SET_HIGH_WORD(yy1,hb);
112*2fe8fb19SBen Gras 	    y2 = b - yy1;
113*2fe8fb19SBen Gras 	    t1 = 0;
114*2fe8fb19SBen Gras 	    SET_HIGH_WORD(t1,ha+0x00100000);
115*2fe8fb19SBen Gras 	    t2 = a - t1;
116*2fe8fb19SBen Gras 	    w  = __ieee754_sqrt(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
117*2fe8fb19SBen Gras 	}
118*2fe8fb19SBen Gras 	if(k!=0) {
119*2fe8fb19SBen Gras 	    u_int32_t high;
120*2fe8fb19SBen Gras 	    t1 = 1.0;
121*2fe8fb19SBen Gras 	    GET_HIGH_WORD(high,t1);
122*2fe8fb19SBen Gras 	    SET_HIGH_WORD(t1,high+(k<<20));
123*2fe8fb19SBen Gras 	    return t1*w;
124*2fe8fb19SBen Gras 	} else return w;
125*2fe8fb19SBen Gras }
126