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