4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * _D_cplx_div(z, w) returns z / w with infinities handled according
33 * If z and w are both finite and w is nonzero, _D_cplx_div(z, w)
34 * delivers the complex quotient q according to the usual formula:
35 * let a = Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x +
36 * I * y where x = (a * c + b * d) / r and y = (b * c - a * d) / r
37 * with r = c * c + d * d. This implementation computes intermediate
38 * results in extended precision to avoid premature underflow or over-
41 * If z is neither NaN nor zero and w is zero, or if z is infinite
42 * and w is finite and nonzero, _D_cplx_div delivers an infinite
43 * result. If z is finite and w is infinite, _D_cplx_div delivers
46 * If z and w are both zero or both infinite, or if either z or w is
47 * a complex NaN, _D_cplx_div delivers NaN + I * NaN. C99 doesn't
48 * specify these cases.
50 * This implementation can raise spurious invalid operation, inexact,
51 * and division-by-zero exceptions. C99 allows this.
53 * Warning: Do not attempt to "optimize" this code by removing multi-
57 #if !defined(i386) && !defined(__i386) && !defined(__amd64)
58 #error This code is for x86 only
69 * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
80 return (((((xx
.i
[1] << 1) - 0xffe00000) | xx
.i
[0]) == 0)?
81 (1 | (xx
.i
[1] >> 31)) : 0);
85 _D_cplx_div(double _Complex z
, double _Complex w
)
97 * The following is equivalent to
99 * a = creal(z); b = cimag(z);
100 * c = creal(w); d = cimag(w);
102 /* LINTED alignment */
103 a
= ((double *)&z
)[0];
104 /* LINTED alignment */
105 b
= ((double *)&z
)[1];
106 /* LINTED alignment */
107 c
= ((double *)&w
)[0];
108 /* LINTED alignment */
109 d
= ((double *)&w
)[1];
111 r
= (long double)c
* c
+ (long double)d
* d
;
114 /* w is zero; multiply z by 1/Re(w) - I * Im(w) */
118 if (i
| j
) { /* z is infinite */
122 /* LINTED alignment */
123 ((double *)&v
)[0] = a
* c
+ b
* d
;
124 /* LINTED alignment */
125 ((double *)&v
)[1] = b
* c
- a
* d
;
130 x
= ((long double)a
* c
+ (long double)b
* d
) * r
;
131 y
= ((long double)b
* c
- (long double)a
* d
) * r
;
133 if (x
!= x
&& y
!= y
) {
135 * Both x and y are NaN, so z and w can't both be finite
136 * and nonzero. Since we handled the case w = 0 above,
137 * the only cases to check here are when one of z or w
144 if (i
| j
) { /* z is infinite */
145 /* "factor out" infinity */
153 if (i
| j
) { /* w is infinite */
155 * "factor out" infinity, being careful to preserve
156 * signs of finite values
160 c
= i
? i
: ((cc
.i
[1] < 0)? -0.0f
: 0.0f
);
161 d
= j
? j
: ((dd
.i
[1] < 0)? -0.0f
: 0.0f
);
166 x
= ((long double)a
* c
+ (long double)b
* d
) * r
;
167 y
= ((long double)b
* c
- (long double)a
* d
) * r
;
172 * The following is equivalent to
176 /* LINTED alignment */
177 ((double *)&v
)[0] = (double)x
;
178 /* LINTED alignment */
179 ((double *)&v
)[1] = (double)y
;