xref: /titanic_51/usr/src/lib/libm/common/Q/hypotl.c (revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af)
125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis  * CDDL HEADER START
325c28e83SPiotr Jasiukajtis  *
425c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis  *
825c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis  * and limitations under the License.
1225c28e83SPiotr Jasiukajtis  *
1325c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis  *
1925c28e83SPiotr Jasiukajtis  * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis  */
2125c28e83SPiotr Jasiukajtis 
2225c28e83SPiotr Jasiukajtis /*
2325c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2425c28e83SPiotr Jasiukajtis  */
2525c28e83SPiotr Jasiukajtis /*
2625c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2725c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
2825c28e83SPiotr Jasiukajtis  */
2925c28e83SPiotr Jasiukajtis 
30*ddc0e0b5SRichard Lowe #pragma weak __hypotl = hypotl
3125c28e83SPiotr Jasiukajtis 
3225c28e83SPiotr Jasiukajtis /*
3325c28e83SPiotr Jasiukajtis  * long double hypotl(long double x, long double y);
3425c28e83SPiotr Jasiukajtis  * Method :
3525c28e83SPiotr Jasiukajtis  *	If z=x*x+y*y has error less than sqrt(2)/2 ulp than sqrt(z) has
3625c28e83SPiotr Jasiukajtis  *	error less than 1 ulp.
3725c28e83SPiotr Jasiukajtis  *	So, compute sqrt(x*x+y*y) with some care as follows:
3825c28e83SPiotr Jasiukajtis  *	Assume x>y>0;
3925c28e83SPiotr Jasiukajtis  *	1. save and set rounding to round-to-nearest
4025c28e83SPiotr Jasiukajtis  *	2. if x > 2y  use
4125c28e83SPiotr Jasiukajtis  *		x1*x1+(y*y+(x2*(x+x2))) for x*x+y*y
4225c28e83SPiotr Jasiukajtis  *	where x1 = x with lower 64 bits cleared, x2 = x-x1; else
4325c28e83SPiotr Jasiukajtis  *	3. if x <= 2y use
4425c28e83SPiotr Jasiukajtis  *		t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
4525c28e83SPiotr Jasiukajtis  *	where t1 = 2x with lower 64 bits cleared, t2 = 2x-t1, y1= y with
4625c28e83SPiotr Jasiukajtis  *	lower 64 bits chopped, y2 = y-y1.
4725c28e83SPiotr Jasiukajtis  *
4825c28e83SPiotr Jasiukajtis  *	NOTE: DO NOT remove parenthsis!
4925c28e83SPiotr Jasiukajtis  *
5025c28e83SPiotr Jasiukajtis  * Special cases:
5125c28e83SPiotr Jasiukajtis  *	hypot(x,y) is INF if x or y is +INF or -INF; else
5225c28e83SPiotr Jasiukajtis  *	hypot(x,y) is NAN if x or y is NAN.
5325c28e83SPiotr Jasiukajtis  *
5425c28e83SPiotr Jasiukajtis  * Accuracy:
5525c28e83SPiotr Jasiukajtis  * 	hypot(x,y) returns sqrt(x^2+y^2) with error less than 1 ulps (units
5625c28e83SPiotr Jasiukajtis  *	in the last place)
5725c28e83SPiotr Jasiukajtis  */
5825c28e83SPiotr Jasiukajtis 
5925c28e83SPiotr Jasiukajtis #include "libm.h"
6025c28e83SPiotr Jasiukajtis #include "longdouble.h"
6125c28e83SPiotr Jasiukajtis 
6225c28e83SPiotr Jasiukajtis extern enum fp_direction_type __swapRD(enum fp_direction_type);
6325c28e83SPiotr Jasiukajtis 
6425c28e83SPiotr Jasiukajtis static const long double zero = 0.0L, one = 1.0L;
6525c28e83SPiotr Jasiukajtis 
6625c28e83SPiotr Jasiukajtis long double
6725c28e83SPiotr Jasiukajtis hypotl(long double x, long double y) {
6825c28e83SPiotr Jasiukajtis 	int n0, n1, n2, n3;
6925c28e83SPiotr Jasiukajtis 	long double t1, t2, y1, y2, w;
7025c28e83SPiotr Jasiukajtis 	int *px = (int *) &x, *py = (int *) &y;
7125c28e83SPiotr Jasiukajtis 	int *pt1 = (int *) &t1, *py1 = (int *) &y1;
7225c28e83SPiotr Jasiukajtis 	enum fp_direction_type rd;
7325c28e83SPiotr Jasiukajtis 	int j, k, nx, ny, nz;
7425c28e83SPiotr Jasiukajtis 
7525c28e83SPiotr Jasiukajtis 	if ((*(int *) &one) != 0) {	/* determine word ordering */
7625c28e83SPiotr Jasiukajtis 		n0 = 0;
7725c28e83SPiotr Jasiukajtis 		n1 = 1;
7825c28e83SPiotr Jasiukajtis 		n2 = 2;
7925c28e83SPiotr Jasiukajtis 		n3 = 3;
8025c28e83SPiotr Jasiukajtis 	} else {
8125c28e83SPiotr Jasiukajtis 		n0 = 3;
8225c28e83SPiotr Jasiukajtis 		n1 = 2;
8325c28e83SPiotr Jasiukajtis 		n2 = 1;
8425c28e83SPiotr Jasiukajtis 		n3 = 0;
8525c28e83SPiotr Jasiukajtis 	}
8625c28e83SPiotr Jasiukajtis 
8725c28e83SPiotr Jasiukajtis 	px[n0] &= 0x7fffffff;	/* clear sign bit of x and y */
8825c28e83SPiotr Jasiukajtis 	py[n0] &= 0x7fffffff;
8925c28e83SPiotr Jasiukajtis 	k = 0x7fff0000;
9025c28e83SPiotr Jasiukajtis 	nx = px[n0] & k;	/* exponent of x and y */
9125c28e83SPiotr Jasiukajtis 	ny = py[n0] & k;
9225c28e83SPiotr Jasiukajtis 	if (ny > nx) {
9325c28e83SPiotr Jasiukajtis 		w = x;
9425c28e83SPiotr Jasiukajtis 		x = y;
9525c28e83SPiotr Jasiukajtis 		y = w;
9625c28e83SPiotr Jasiukajtis 		nz = ny;
9725c28e83SPiotr Jasiukajtis 		ny = nx;
9825c28e83SPiotr Jasiukajtis 		nx = nz;
9925c28e83SPiotr Jasiukajtis 	}			/* force x > y */
10025c28e83SPiotr Jasiukajtis 	if ((nx - ny) >= 0x00730000)
10125c28e83SPiotr Jasiukajtis 		return (x + y);	/* x/y >= 2**116 */
10225c28e83SPiotr Jasiukajtis 	if (nx < 0x5ff30000 && ny > 0x205b0000) {	/* medium x,y */
10325c28e83SPiotr Jasiukajtis 		/* save and set RD to Rounding to nearest */
10425c28e83SPiotr Jasiukajtis 		rd = __swapRD(fp_nearest);
10525c28e83SPiotr Jasiukajtis 		w = x - y;
10625c28e83SPiotr Jasiukajtis 		if (w > y) {
10725c28e83SPiotr Jasiukajtis 			pt1[n0] = px[n0];
10825c28e83SPiotr Jasiukajtis 			pt1[n1] = px[n1];
10925c28e83SPiotr Jasiukajtis 			pt1[n2] = pt1[n3] = 0;
11025c28e83SPiotr Jasiukajtis 			t2 = x - t1;
11125c28e83SPiotr Jasiukajtis 			x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
11225c28e83SPiotr Jasiukajtis 		} else {
11325c28e83SPiotr Jasiukajtis 			x = x + x;
11425c28e83SPiotr Jasiukajtis 			py1[n0] = py[n0];
11525c28e83SPiotr Jasiukajtis 			py1[n1] = py[n1];
11625c28e83SPiotr Jasiukajtis 			py1[n2] = py1[n3] = 0;
11725c28e83SPiotr Jasiukajtis 			y2 = y - y1;
11825c28e83SPiotr Jasiukajtis 			pt1[n0] = px[n0];
11925c28e83SPiotr Jasiukajtis 			pt1[n1] = px[n1];
12025c28e83SPiotr Jasiukajtis 			pt1[n2] = pt1[n3] = 0;
12125c28e83SPiotr Jasiukajtis 			t2 = x - t1;
12225c28e83SPiotr Jasiukajtis 			x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
12325c28e83SPiotr Jasiukajtis 		}
12425c28e83SPiotr Jasiukajtis 		if (rd != fp_nearest)
12525c28e83SPiotr Jasiukajtis 			(void) __swapRD(rd);	/* restore rounding mode */
12625c28e83SPiotr Jasiukajtis 		return (x);
12725c28e83SPiotr Jasiukajtis 	} else {
12825c28e83SPiotr Jasiukajtis 		if (nx == k || ny == k) {	/* x or y is INF or NaN */
12925c28e83SPiotr Jasiukajtis 			if (isinfl(x))
13025c28e83SPiotr Jasiukajtis 				t2 = x;
13125c28e83SPiotr Jasiukajtis 			else if (isinfl(y))
13225c28e83SPiotr Jasiukajtis 				t2 = y;
13325c28e83SPiotr Jasiukajtis 			else
13425c28e83SPiotr Jasiukajtis 				t2 = x + y;	/* invalid if x or y is sNaN */
13525c28e83SPiotr Jasiukajtis 			return (t2);
13625c28e83SPiotr Jasiukajtis 		}
13725c28e83SPiotr Jasiukajtis 		if (ny == 0) {
13825c28e83SPiotr Jasiukajtis 			if (y == zero || x == zero)
13925c28e83SPiotr Jasiukajtis 				return (x + y);
14025c28e83SPiotr Jasiukajtis 			t1 = scalbnl(one, 16381);
14125c28e83SPiotr Jasiukajtis 			x *= t1;
14225c28e83SPiotr Jasiukajtis 			y *= t1;
14325c28e83SPiotr Jasiukajtis 			return (scalbnl(one, -16381) * hypotl(x, y));
14425c28e83SPiotr Jasiukajtis 		}
14525c28e83SPiotr Jasiukajtis 		j = nx - 0x3fff0000;
14625c28e83SPiotr Jasiukajtis 		px[n0] -= j;
14725c28e83SPiotr Jasiukajtis 		py[n0] -= j;
14825c28e83SPiotr Jasiukajtis 		pt1[n0] = nx;
14925c28e83SPiotr Jasiukajtis 		pt1[n1] = pt1[n2] = pt1[n3] = 0;
15025c28e83SPiotr Jasiukajtis 		return (t1 * hypotl(x, y));
15125c28e83SPiotr Jasiukajtis 	}
15225c28e83SPiotr Jasiukajtis }
153