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 __hypot = hypot
35 * by K.C. Ng for SUN 4.0 libm, updated 3/11/2003.
37 * A. When rounding is rounded-to-nearest:
38 * If z = x * x + y * y has error less than sqrt(2) / 2 ulp than
39 * sqrt(z) has error less than 1 ulp.
40 * So, compute sqrt(x*x+y*y) with some care as follows:
42 * 1. Check whether save and set rounding to round-to-nearest
44 * xh*xh+(y*y+((x-xh)*(x+xh))) for x*x+y*y
45 * where xh = x with lower 32 bits cleared; else
47 * x2h*yh+((x-y)*(x-y)+(x2h*(y-yh)+(x2-x2h)*y))
48 * where x2 = 2*x, x2h = 2x with lower 32 bits cleared, yh = y with
49 * lower 32 bits chopped.
51 * B. When rounding is not rounded-to-nearest:
52 * The following (magic) formula will yield an error less than 1 ulp.
53 * z = sqrt(x * x + y * y)
54 * hypot(x, y) = x + (y / ((x + z) / y))
56 * NOTE: DO NOT remove parenthsis!
59 * hypot(x, y) is INF if x or y is +INF or -INF; else
60 * hypot(x, y) is NAN if x or y is NAN.
63 * hypot(x, y) returns sqrt(x^2+y^2) with error less than 1 ulps
64 * (units in the last place)
71 onep1u
= 1.00000000000000022204e+00, /* 0x3ff00000 1 = 1+2**-52 */
72 twom53
= 1.11022302462515654042e-16, /* 0x3ca00000 0 = 2**-53 */
73 twom768
= 6.441148769597133308e-232, /* 2^-768 */
74 two768
= 1.552518092300708935e+231; /* 2^768 */
79 hypot(double x
, double y
) {
80 double xh
, yh
, w
, ax
, ay
;
81 int i
, j
, nx
, ny
, ix
, iy
, iscale
= 0;
84 ix
= ((int *) &x
)[HIWORD
] & ~0x80000000;
85 lx
= ((int *) &x
)[LOWORD
];
86 iy
= ((int *) &y
)[HIWORD
] & ~0x80000000;
87 ly
= ((int *) &y
)[LOWORD
];
89 * Force ax = |x| ~>~ ay = |y|
108 * x >= 2^500 (x*x or y*y may overflow)
111 if (nx
== 0x7ff) { /* inf or NaN, signal of sNaN */
112 if (((ix
- 0x7ff00000) | lx
) == 0)
113 return (ax
== ay
? ay
: ax
);
114 else if (((iy
- 0x7ff00000) | ly
) == 0)
115 return (ay
== ax
? ax
: ay
);
117 return (ax
* ay
); /* + -> * for Cheetah */
118 } else if (j
> 32) { /* x >> y */
122 if (((int *) &ax
)[HIWORD
] == 0x7ff00000)
123 ax
= _SVID_libm_err(x
, y
, 4);
133 * y < 2^-450 (x*x or y*y may underflow)
135 else if (ny
< 0x23d) {
140 if (j
> 53) /* x >> y */
146 if (ax
== zero
) /* guard subnormal flush to zero */
148 ix
= ((int *) &ax
)[HIWORD
];
152 if (ay
== zero
) /* guard subnormal flush to zero */
153 return (ax
* twom768
);
154 iy
= ((int *) &ay
)[HIWORD
];
157 j
= (ix
>> 20) - (iy
>> 20);
158 if (j
> 32) { /* x >> y */
161 return ((ax
+ ay
) * twom768
);
163 } else if (j
> 32) { /* x >> y */
169 * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32
170 * First check rounding mode by comparing onep1u*onep1u with onep1u+twom53.
171 * Make sure the computation is done at run-time.
173 if (((lx
| ly
) << 5) == 0) {
175 ax
+= ay
/ (ax
+ sqrt(ax
* ax
+ ay
));
177 if (onep1u
* onep1u
!= onep1u
+ twom53
) {
178 /* round-to-zero, positive, negative mode */
179 /* magic formula with less than an ulp error */
180 w
= sqrt(ax
* ax
+ ay
* ay
);
181 ax
+= ay
/ ((ax
+ w
) / ay
);
183 /* round-to-nearest mode */
186 ((int *) &xh
)[HIWORD
] = ix
;
187 ((int *) &xh
)[LOWORD
] = 0;
188 ay
= ay
* ay
+ (ax
- xh
) * (ax
+ xh
);
189 ax
= sqrt(xh
* xh
+ ay
);
192 ((int *) &xh
)[HIWORD
] = ix
+ 0x00100000;
193 ((int *) &xh
)[LOWORD
] = 0;
194 ((int *) &yh
)[HIWORD
] = iy
;
195 ((int *) &yh
)[LOWORD
] = 0;
196 ay
= w
* w
+ ((ax
- xh
) * yh
+ (ay
- yh
) * ax
);
197 ax
= sqrt(xh
* yh
+ ay
);
204 ax
*= two768
; /* must generate side effect here */
205 if (((int *) &ax
)[HIWORD
] == 0x7ff00000)
206 ax
= _SVID_libm_err(x
, y
, 4);