xref: /titanic_52/usr/src/lib/libm/common/Q/hypotl.c (revision 263f549e5da8b32c4922f586afb365b8ae388a6c)
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 #pragma weak __hypotl = hypotl
31 
32 /*
33  * long double hypotl(long double x, long double y);
34  * Method :
35  *	If z=x*x+y*y has error less than sqrt(2)/2 ulp than sqrt(z) has
36  *	error less than 1 ulp.
37  *	So, compute sqrt(x*x+y*y) with some care as follows:
38  *	Assume x>y>0;
39  *	1. save and set rounding to round-to-nearest
40  *	2. if x > 2y  use
41  *		x1*x1+(y*y+(x2*(x+x2))) for x*x+y*y
42  *	where x1 = x with lower 64 bits cleared, x2 = x-x1; else
43  *	3. if x <= 2y use
44  *		t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
45  *	where t1 = 2x with lower 64 bits cleared, t2 = 2x-t1, y1= y with
46  *	lower 64 bits chopped, y2 = y-y1.
47  *
48  *	NOTE: DO NOT remove parenthsis!
49  *
50  * Special cases:
51  *	hypot(x,y) is INF if x or y is +INF or -INF; else
52  *	hypot(x,y) is NAN if x or y is NAN.
53  *
54  * Accuracy:
55  * 	hypot(x,y) returns sqrt(x^2+y^2) with error less than 1 ulps (units
56  *	in the last place)
57  */
58 
59 #include "libm.h"
60 #include "longdouble.h"
61 
62 extern enum fp_direction_type __swapRD(enum fp_direction_type);
63 
64 static const long double zero = 0.0L, one = 1.0L;
65 
66 long double
67 hypotl(long double x, long double y) {
68 	int n0, n1, n2, n3;
69 	long double t1, t2, y1, y2, w;
70 	int *px = (int *) &x, *py = (int *) &y;
71 	int *pt1 = (int *) &t1, *py1 = (int *) &y1;
72 	enum fp_direction_type rd;
73 	int j, k, nx, ny, nz;
74 
75 	if ((*(int *) &one) != 0) {	/* determine word ordering */
76 		n0 = 0;
77 		n1 = 1;
78 		n2 = 2;
79 		n3 = 3;
80 	} else {
81 		n0 = 3;
82 		n1 = 2;
83 		n2 = 1;
84 		n3 = 0;
85 	}
86 
87 	px[n0] &= 0x7fffffff;	/* clear sign bit of x and y */
88 	py[n0] &= 0x7fffffff;
89 	k = 0x7fff0000;
90 	nx = px[n0] & k;	/* exponent of x and y */
91 	ny = py[n0] & k;
92 	if (ny > nx) {
93 		w = x;
94 		x = y;
95 		y = w;
96 		nz = ny;
97 		ny = nx;
98 		nx = nz;
99 	}			/* force x > y */
100 	if ((nx - ny) >= 0x00730000)
101 		return (x + y);	/* x/y >= 2**116 */
102 	if (nx < 0x5ff30000 && ny > 0x205b0000) {	/* medium x,y */
103 		/* save and set RD to Rounding to nearest */
104 		rd = __swapRD(fp_nearest);
105 		w = x - y;
106 		if (w > y) {
107 			pt1[n0] = px[n0];
108 			pt1[n1] = px[n1];
109 			pt1[n2] = pt1[n3] = 0;
110 			t2 = x - t1;
111 			x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
112 		} else {
113 			x = x + x;
114 			py1[n0] = py[n0];
115 			py1[n1] = py[n1];
116 			py1[n2] = py1[n3] = 0;
117 			y2 = y - y1;
118 			pt1[n0] = px[n0];
119 			pt1[n1] = px[n1];
120 			pt1[n2] = pt1[n3] = 0;
121 			t2 = x - t1;
122 			x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
123 		}
124 		if (rd != fp_nearest)
125 			(void) __swapRD(rd);	/* restore rounding mode */
126 		return (x);
127 	} else {
128 		if (nx == k || ny == k) {	/* x or y is INF or NaN */
129 			if (isinfl(x))
130 				t2 = x;
131 			else if (isinfl(y))
132 				t2 = y;
133 			else
134 				t2 = x + y;	/* invalid if x or y is sNaN */
135 			return (t2);
136 		}
137 		if (ny == 0) {
138 			if (y == zero || x == zero)
139 				return (x + y);
140 			t1 = scalbnl(one, 16381);
141 			x *= t1;
142 			y *= t1;
143 			return (scalbnl(one, -16381) * hypotl(x, y));
144 		}
145 		j = nx - 0x3fff0000;
146 		px[n0] -= j;
147 		py[n0] -= j;
148 		pt1[n0] = nx;
149 		pt1[n1] = pt1[n2] = pt1[n3] = 0;
150 		return (t1 * hypotl(x, y));
151 	}
152 }
153