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.
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.
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]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma weak __hypotl = hypotl
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:
39 * 1. save and set rounding to round-to-nearest
41 * x1*x1+(y*y+(x2*(x+x2))) for x*x+y*y
42 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
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 32 bits cleared, y2 = y-y1.
48 * NOTE: DO NOT remove parenthsis!
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.
55 * hypot(x,y) returns sqrt(x^2+y^2) with error less than 1 ulps (units
62 extern enum fp_direction_type
__swap87RD(enum fp_direction_type
);
67 hypotl(long double x
, long double y
) {
68 long double t1
, t2
, y1
, y2
, w
;
69 int *px
= (int *) &x
, *py
= (int *) &y
;
70 int *pt1
= (int *) &t1
, *py1
= (int *) &y1
;
71 enum fp_direction_type rd
;
74 px
[2] &= 0x7fff; /* clear sign bit and padding bits of x and y */
76 nx
= px
[2]; /* biased exponent of x and y */
85 } /* force nx >= ny */
87 return (x
+ y
); /* x / y >= 2**65 */
88 if (nx
< 0x5ff3 && ny
> 0x205b) { /* medium x,y */
89 /* save and set RD to Rounding to nearest */
90 rd
= __swap87RD(fp_nearest
);
97 x
= sqrtl(t1
* t1
- (y
* (-y
) - t2
* (x
+ t1
)));
108 x
= sqrtl(t1
* y1
- (w
* (-w
) - (t2
* y1
+ y2
* x
)));
110 if (rd
!= fp_nearest
)
111 __swap87RD(rd
); /* restore rounding mode */
114 if (nx
== k
|| ny
== k
) { /* x or y is INF or NaN */
115 /* since nx >= ny; nx is always k within this block */
116 if (px
[1] == 0x80000000 && px
[0] == 0)
118 else if (ny
== k
&& py
[1] == 0x80000000 && py
[0] == 0)
124 if (y
== 0.L
|| x
== 0.L
)
126 pt1
[2] = 0x3fff + 16381;
129 py1
[2] = 0x3fff - 16381;
134 return (y1
* hypotl(x
, y
));
142 return (t1
* hypotl(x
, y
));