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 * _F_cplx_div(z, w) returns z / w with infinities handled according
33 * If z and w are both finite and w is nonzero, _F_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, _F_cplx_div delivers an infinite
43 * result. If z is finite and w is infinite, _F_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, _F_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) - 0xff000000) == 0)? (1 | (xx
.i
>> 31)) : 0);
84 _F_cplx_div(float _Complex z
, float _Complex w
)
96 * The following is equivalent to
98 * a = crealf(z); b = cimagf(z);
99 * c = crealf(w); d = cimagf(w);
101 a
= ((float *)&z
)[0];
102 b
= ((float *)&z
)[1];
103 c
= ((float *)&w
)[0];
104 d
= ((float *)&w
)[1];
106 r
= (long double)c
* c
+ (long double)d
* d
;
109 /* w is zero; multiply z by 1/Re(w) - I * Im(w) */
113 if (i
| j
) { /* z is infinite */
117 ((float *)&v
)[0] = a
* c
+ b
* d
;
118 ((float *)&v
)[1] = b
* c
- a
* d
;
123 x
= ((long double)a
* c
+ (long double)b
* d
) * r
;
124 y
= ((long double)b
* c
- (long double)a
* d
) * r
;
126 if (x
!= x
&& y
!= y
) {
128 * Both x and y are NaN, so z and w can't both be finite
129 * and nonzero. Since we handled the case w = 0 above,
130 * the only cases to check here are when one of z or w
137 if (i
| j
) { /* z is infinite */
138 /* "factor out" infinity */
146 if (i
| j
) { /* w is infinite */
148 * "factor out" infinity, being careful to preserve
149 * signs of finite values
153 c
= i
? i
: ((cc
.i
< 0)? -0.0f
: 0.0f
);
154 d
= j
? j
: ((dd
.i
< 0)? -0.0f
: 0.0f
);
159 x
= ((long double)a
* c
+ (long double)b
* d
) * r
;
160 y
= ((long double)b
* c
- (long double)a
* d
) * r
;
165 * The following is equivalent to
169 ((float *)&v
)[0] = (float)x
;
170 ((float *)&v
)[1] = (float)y
;