xref: /titanic_51/usr/src/lib/libm/common/C/hypot.c (revision b59c4a48daf5a1863ecac763711b497b2f8321e4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #if defined(ELFOBJ)
31 #pragma weak hypot = __hypot
32 #endif
33 
34 /* INDENT OFF */
35 /*
36  * Hypot(x, y)
37  * by K.C. Ng for SUN 4.0 libm, updated 3/11/2003.
38  * Method :
39  * A. When rounding is rounded-to-nearest:
40  *	If z = x * x + y * y has error less than sqrt(2) / 2 ulp than
41  *	sqrt(z) has error less than 1 ulp.
42  *	So, compute sqrt(x*x+y*y) with some care as follows:
43  *	Assume x > y > 0;
44  *	1. Check whether save and set rounding to round-to-nearest
45  *	2. if x > 2y  use
46  *		xh*xh+(y*y+((x-xh)*(x+xh))) for x*x+y*y
47  *	where xh = x with lower 32 bits cleared;  else
48  *	3. if x <= 2y use
49  *		x2h*yh+((x-y)*(x-y)+(x2h*(y-yh)+(x2-x2h)*y))
50  *	where x2 = 2*x, x2h = 2x with lower 32 bits cleared, yh = y with
51  *	lower 32 bits chopped.
52  *
53  * B. When rounding is not rounded-to-nearest:
54  *	The following (magic) formula will yield an error less than 1 ulp.
55  *	z = sqrt(x * x + y * y)
56  *		hypot(x, y) = x + (y / ((x + z) / y))
57  *
58  * NOTE: DO NOT remove parenthsis!
59  *
60  * Special cases:
61  *	hypot(x, y) is INF if x or y is +INF or -INF; else
62  *	hypot(x, y) is NAN if x or y is NAN.
63  *
64  * Accuracy:
65  * 	hypot(x, y) returns sqrt(x^2+y^2) with error less than 1 ulps
66  *	(units in the last place)
67  */
68 
69 #include "libm.h"
70 
71 static const double
72 	zero = 0.0,
73 	onep1u = 1.00000000000000022204e+00,	/* 0x3ff00000 1 = 1+2**-52 */
74 	twom53 = 1.11022302462515654042e-16,	/* 0x3ca00000 0 = 2**-53 */
75 	twom768 = 6.441148769597133308e-232,	/* 2^-768 */
76 	two768  = 1.552518092300708935e+231;	/* 2^768 */
77 
78 /* INDENT ON */
79 
80 double
81 hypot(double x, double y) {
82 	double xh, yh, w, ax, ay;
83 	int i, j, nx, ny, ix, iy, iscale = 0;
84 	unsigned lx, ly;
85 
86 	ix = ((int *) &x)[HIWORD] & ~0x80000000;
87 	lx = ((int *) &x)[LOWORD];
88 	iy = ((int *) &y)[HIWORD] & ~0x80000000;
89 	ly = ((int *) &y)[LOWORD];
90 /*
91  * Force ax = |x| ~>~ ay = |y|
92  */
93 	if (iy > ix) {
94 		ax = fabs(y);
95 		ay = fabs(x);
96 		i = ix;
97 		ix = iy;
98 		iy = i;
99 		i = lx;
100 		lx = ly;
101 		ly = i;
102 	} else {
103 		ax = fabs(x);
104 		ay = fabs(y);
105 	}
106 	nx = ix >> 20;
107 	ny = iy >> 20;
108 	j  = nx - ny;
109 /*
110  * x >= 2^500 (x*x or y*y may overflow)
111  */
112 	if (nx >= 0x5f3) {
113 		if (nx == 0x7ff) {	/* inf or NaN, signal of sNaN */
114 			if (((ix - 0x7ff00000) | lx) == 0)
115 				return (ax == ay ? ay : ax);
116 			else if (((iy - 0x7ff00000) | ly) == 0)
117 				return (ay == ax ? ax : ay);
118 			else
119 				return (ax * ay);	/* + -> * for Cheetah */
120 		} else if (j > 32) {	/* x >> y */
121 			if (j <= 53)
122 				ay *= twom53;
123 			ax += ay;
124 			if (((int *) &ax)[HIWORD] == 0x7ff00000)
125 				ax = _SVID_libm_err(x, y, 4);
126 			return (ax);
127 		}
128 		ax *= twom768;
129 		ay *= twom768;
130 		iscale = 2;
131 		ix -= 768 << 20;
132 		iy -= 768 << 20;
133 	}
134 /*
135  * y < 2^-450 (x*x or y*y may underflow)
136  */
137 	else if (ny < 0x23d) {
138 		if ((ix | lx) == 0)
139 			return (ay);
140 		if ((iy | ly) == 0)
141 			return (ax);
142 		if (j > 53) 		/* x >> y */
143 			return (ax + ay);
144 		iscale = 1;
145 		ax *= two768;
146 		ay *= two768;
147 		if (nx == 0) {
148 			if (ax == zero)	/* guard subnormal flush to zero */
149 				return (ax);
150 			ix = ((int *) &ax)[HIWORD];
151 		} else
152 			ix += 768 << 20;
153 		if (ny == 0) {
154 			if (ay == zero)	/* guard subnormal flush to zero */
155 				return (ax * twom768);
156 			iy = ((int *) &ay)[HIWORD];
157 		} else
158 			iy += 768 << 20;
159 		j = (ix >> 20) - (iy >> 20);
160 		if (j > 32) {		/* x >> y */
161 			if (j <= 53)
162 				ay *= twom53;
163 			return ((ax + ay) * twom768);
164 		}
165 	} else if (j > 32) {		/* x >> y */
166 		if (j <= 53)
167 			ay *= twom53;
168 		return (ax + ay);
169 	}
170 /*
171  * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32
172  * First check rounding mode by comparing onep1u*onep1u with onep1u+twom53.
173  * Make sure the computation is done at run-time.
174  */
175 	if (((lx | ly) << 5) == 0) {
176 		ay = ay * ay;
177 		ax += ay / (ax + sqrt(ax * ax + ay));
178 	} else
179 	if (onep1u * onep1u != onep1u + twom53) {
180 	/* round-to-zero, positive, negative mode */
181 	/* magic formula with less than an ulp error */
182 		w = sqrt(ax * ax + ay * ay);
183 		ax += ay / ((ax + w) / ay);
184 	} else {
185 	/* round-to-nearest mode */
186 		w = ax - ay;
187 		if (w > ay) {
188 			((int *) &xh)[HIWORD] = ix;
189 			((int *) &xh)[LOWORD] = 0;
190 			ay = ay * ay + (ax - xh) * (ax + xh);
191 			ax = sqrt(xh * xh + ay);
192 		} else {
193 			ax = ax + ax;
194 			((int *) &xh)[HIWORD] = ix + 0x00100000;
195 			((int *) &xh)[LOWORD] = 0;
196 			((int *) &yh)[HIWORD] = iy;
197 			((int *) &yh)[LOWORD] = 0;
198 			ay = w * w + ((ax - xh) * yh + (ay - yh) * ax);
199 			ax = sqrt(xh * yh + ay);
200 		}
201 	}
202 	if (iscale > 0) {
203 		if (iscale == 1)
204 			ax *= twom768;
205 		else {
206 			ax *= two768;	/* must generate side effect here */
207 			if (((int *) &ax)[HIWORD] == 0x7ff00000)
208 				ax = _SVID_libm_err(x, y, 4);
209 		}
210 	}
211 	return (ax);
212 }
213