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]
22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #pragma weak __cabs = cabs
32 #include "complex_wrapper.h"
35 * If C were the only standard we cared about, cabs could just call
36 * hypot. Unfortunately, various other standards say that hypot must
37 * call matherr and/or set errno to ERANGE when the result overflows.
38 * Since cabs should do neither of these things, we have to either
39 * make hypot a wrapper on another internal function or duplicate
40 * the hypot implementation here. I've chosen to do the latter.
45 onep1u
= 1.00000000000000022204e+00, /* 0x3ff00000 1 = 1+2**-52 */
46 twom53
= 1.11022302462515654042e-16, /* 0x3ca00000 0 = 2**-53 */
47 twom768
= 6.441148769597133308e-232, /* 2^-768 */
48 two768
= 1.552518092300708935e+231; /* 2^768 */
53 double x
, y
, xh
, yh
, w
, ax
, ay
;
54 int i
, j
, nx
, ny
, ix
, iy
, iscale
= 0;
60 ix
= ((int *)&x
)[HIWORD
] & ~0x80000000;
61 lx
= ((int *)&x
)[LOWORD
];
62 iy
= ((int *)&y
)[HIWORD
] & ~0x80000000;
63 ly
= ((int *)&y
)[LOWORD
];
65 /* force ax = |x| ~>~ ay = |y| */
84 /* x >= 2^500 (x*x or y*y may overflow) */
86 /* inf or NaN, signal of sNaN */
87 if (((ix
- 0x7ff00000) | lx
) == 0)
88 return ((ax
== ay
)? ay
: ax
);
89 else if (((iy
- 0x7ff00000) | ly
) == 0)
90 return ((ay
== ax
)? ax
: ay
);
105 } else if (ny
< 0x23d) {
106 /* y < 2^-450 (x*x or y*y may underflow) */
111 if (j
> 53) /* x >> y */
117 if (ax
== zero
) /* guard subnormal flush to zero */
119 ix
= ((int *)&ax
)[HIWORD
];
124 if (ay
== zero
) /* guard subnormal flush to zero */
125 return (ax
* twom768
);
126 iy
= ((int *)&ay
)[HIWORD
];
130 j
= (ix
>> 20) - (iy
>> 20);
135 return ((ax
+ ay
) * twom768
);
145 * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32.
146 * First check rounding mode by comparing onep1u*onep1u with onep1u
147 * + twom53. Make sure the computation is done at run-time.
149 if (((lx
| ly
) << 5) == 0) {
151 ax
+= ay
/ (ax
+ sqrt(ax
* ax
+ ay
));
152 } else if (onep1u
* onep1u
!= onep1u
+ twom53
) {
153 /* round-to-zero, positive, negative mode */
154 /* magic formula with less than an ulp error */
155 w
= sqrt(ax
* ax
+ ay
* ay
);
156 ax
+= ay
/ ((ax
+ w
) / ay
);
158 /* round-to-nearest mode */
161 ((int *)&xh
)[HIWORD
] = ix
;
162 ((int *)&xh
)[LOWORD
] = 0;
163 ay
= ay
* ay
+ (ax
- xh
) * (ax
+ xh
);
164 ax
= sqrt(xh
* xh
+ ay
);
167 ((int *)&xh
)[HIWORD
] = ix
+ 0x00100000;
168 ((int *)&xh
)[LOWORD
] = 0;
169 ((int *)&yh
)[HIWORD
] = iy
;
170 ((int *)&yh
)[LOWORD
] = 0;
171 ay
= w
* w
+ ((ax
- xh
) * yh
+ (ay
- yh
) * ax
);
172 ax
= sqrt(xh
* yh
+ ay
);
179 ax
*= two768
; /* must generate side effect here */