xref: /titanic_50/usr/src/lib/libm/common/LD/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  * hypotl(x,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 32 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 32 bits cleared, 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 
6125c28e83SPiotr Jasiukajtis #if defined(__x86)
6225c28e83SPiotr Jasiukajtis extern enum fp_direction_type __swap87RD(enum fp_direction_type);
6325c28e83SPiotr Jasiukajtis 
6425c28e83SPiotr Jasiukajtis #define	k	0x7fff
6525c28e83SPiotr Jasiukajtis 
6625c28e83SPiotr Jasiukajtis long double
hypotl(long double x,long double y)6725c28e83SPiotr Jasiukajtis hypotl(long double x, long double y) {
6825c28e83SPiotr Jasiukajtis 	long double t1, t2, y1, y2, w;
6925c28e83SPiotr Jasiukajtis 	int *px = (int *) &x, *py = (int *) &y;
7025c28e83SPiotr Jasiukajtis 	int *pt1 = (int *) &t1, *py1 = (int *) &y1;
7125c28e83SPiotr Jasiukajtis 	enum fp_direction_type rd;
7225c28e83SPiotr Jasiukajtis 	int j, nx, ny, nz;
7325c28e83SPiotr Jasiukajtis 
7425c28e83SPiotr Jasiukajtis 	px[2] &= 0x7fff;	/* clear sign bit and padding bits of x and y */
7525c28e83SPiotr Jasiukajtis 	py[2] &= 0x7fff;
7625c28e83SPiotr Jasiukajtis 	nx = px[2];		/* biased exponent of x and y */
7725c28e83SPiotr Jasiukajtis 	ny = py[2];
7825c28e83SPiotr Jasiukajtis 	if (ny > nx) {
7925c28e83SPiotr Jasiukajtis 		w = x;
8025c28e83SPiotr Jasiukajtis 		x = y;
8125c28e83SPiotr Jasiukajtis 		y = w;
8225c28e83SPiotr Jasiukajtis 		nz = ny;
8325c28e83SPiotr Jasiukajtis 		ny = nx;
8425c28e83SPiotr Jasiukajtis 		nx = nz;
8525c28e83SPiotr Jasiukajtis 	}			/* force nx >= ny */
8625c28e83SPiotr Jasiukajtis 	if (nx - ny >= 66)
8725c28e83SPiotr Jasiukajtis 		return (x + y);	/* x / y >= 2**65 */
8825c28e83SPiotr Jasiukajtis 	if (nx < 0x5ff3 && ny > 0x205b) {	/* medium x,y */
8925c28e83SPiotr Jasiukajtis 		/* save and set RD to Rounding to nearest */
9025c28e83SPiotr Jasiukajtis 		rd = __swap87RD(fp_nearest);
9125c28e83SPiotr Jasiukajtis 		w = x - y;
9225c28e83SPiotr Jasiukajtis 		if (w > y) {
9325c28e83SPiotr Jasiukajtis 			pt1[2] = px[2];
9425c28e83SPiotr Jasiukajtis 			pt1[1] = px[1];
9525c28e83SPiotr Jasiukajtis 			pt1[0] = 0;
9625c28e83SPiotr Jasiukajtis 			t2 = x - t1;
9725c28e83SPiotr Jasiukajtis 			x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
9825c28e83SPiotr Jasiukajtis 		} else {
9925c28e83SPiotr Jasiukajtis 			x += x;
10025c28e83SPiotr Jasiukajtis 			py1[2] = py[2];
10125c28e83SPiotr Jasiukajtis 			py1[1] = py[1];
10225c28e83SPiotr Jasiukajtis 			py1[0] = 0;
10325c28e83SPiotr Jasiukajtis 			y2 = y - y1;
10425c28e83SPiotr Jasiukajtis 			pt1[2] = px[2];
10525c28e83SPiotr Jasiukajtis 			pt1[1] = px[1];
10625c28e83SPiotr Jasiukajtis 			pt1[0] = 0;
10725c28e83SPiotr Jasiukajtis 			t2 = x - t1;
10825c28e83SPiotr Jasiukajtis 			x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
10925c28e83SPiotr Jasiukajtis 		}
11025c28e83SPiotr Jasiukajtis 		if (rd != fp_nearest)
11125c28e83SPiotr Jasiukajtis 			__swap87RD(rd);	/* restore rounding mode */
11225c28e83SPiotr Jasiukajtis 		return (x);
11325c28e83SPiotr Jasiukajtis 	} else {
11425c28e83SPiotr Jasiukajtis 		if (nx == k || ny == k) {	/* x or y is INF or NaN */
11525c28e83SPiotr Jasiukajtis 			/* since nx >= ny; nx is always k within this block */
11625c28e83SPiotr Jasiukajtis 			if (px[1] == 0x80000000 && px[0] == 0)
11725c28e83SPiotr Jasiukajtis 				return (x);
11825c28e83SPiotr Jasiukajtis 			else if (ny == k && py[1] == 0x80000000 && py[0] == 0)
11925c28e83SPiotr Jasiukajtis 				return (y);
12025c28e83SPiotr Jasiukajtis 			else
12125c28e83SPiotr Jasiukajtis 				return (x + y);
12225c28e83SPiotr Jasiukajtis 		}
12325c28e83SPiotr Jasiukajtis 		if (ny == 0) {
12425c28e83SPiotr Jasiukajtis 			if (y == 0.L || x == 0.L)
12525c28e83SPiotr Jasiukajtis 				return (x + y);
12625c28e83SPiotr Jasiukajtis 			pt1[2] = 0x3fff + 16381;
12725c28e83SPiotr Jasiukajtis 			pt1[1] = 0x80000000;
12825c28e83SPiotr Jasiukajtis 			pt1[0] = 0;
12925c28e83SPiotr Jasiukajtis 			py1[2] = 0x3fff - 16381;
13025c28e83SPiotr Jasiukajtis 			py1[1] = 0x80000000;
13125c28e83SPiotr Jasiukajtis 			py1[0] = 0;
13225c28e83SPiotr Jasiukajtis 			x *= t1;
13325c28e83SPiotr Jasiukajtis 			y *= t1;
13425c28e83SPiotr Jasiukajtis 			return (y1 * hypotl(x, y));
13525c28e83SPiotr Jasiukajtis 		}
13625c28e83SPiotr Jasiukajtis 		j = nx - 0x3fff;
13725c28e83SPiotr Jasiukajtis 		px[2] -= j;
13825c28e83SPiotr Jasiukajtis 		py[2] -= j;
13925c28e83SPiotr Jasiukajtis 		pt1[2] = nx;
14025c28e83SPiotr Jasiukajtis 		pt1[1] = 0x80000000;
14125c28e83SPiotr Jasiukajtis 		pt1[0] = 0;
14225c28e83SPiotr Jasiukajtis 		return (t1 * hypotl(x, y));
14325c28e83SPiotr Jasiukajtis 	}
14425c28e83SPiotr Jasiukajtis }
14525c28e83SPiotr Jasiukajtis #endif
146