1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * _X_cplx_div(z, w) returns z / w with infinities handled according
29*7c478bd9Sstevel@tonic-gate * to C99.
30*7c478bd9Sstevel@tonic-gate *
31*7c478bd9Sstevel@tonic-gate * If z and w are both finite and w is nonzero, _X_cplx_div delivers
32*7c478bd9Sstevel@tonic-gate * the complex quotient q according to the usual formula: let a =
33*7c478bd9Sstevel@tonic-gate * Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x + I * y
34*7c478bd9Sstevel@tonic-gate * where x = (a * c + b * d) / r and y = (b * c - a * d) / r with
35*7c478bd9Sstevel@tonic-gate * r = c * c + d * d. This implementation scales to avoid premature
36*7c478bd9Sstevel@tonic-gate * underflow or overflow.
37*7c478bd9Sstevel@tonic-gate *
38*7c478bd9Sstevel@tonic-gate * If z is neither NaN nor zero and w is zero, or if z is infinite
39*7c478bd9Sstevel@tonic-gate * and w is finite and nonzero, _X_cplx_div delivers an infinite
40*7c478bd9Sstevel@tonic-gate * result. If z is finite and w is infinite, _X_cplx_div delivers
41*7c478bd9Sstevel@tonic-gate * a zero result.
42*7c478bd9Sstevel@tonic-gate *
43*7c478bd9Sstevel@tonic-gate * If z and w are both zero or both infinite, or if either z or w is
44*7c478bd9Sstevel@tonic-gate * a complex NaN, _X_cplx_div delivers NaN + I * NaN. C99 doesn't
45*7c478bd9Sstevel@tonic-gate * specify these cases.
46*7c478bd9Sstevel@tonic-gate *
47*7c478bd9Sstevel@tonic-gate * This implementation can raise spurious underflow, overflow, in-
48*7c478bd9Sstevel@tonic-gate * valid operation, inexact, and division-by-zero exceptions. C99
49*7c478bd9Sstevel@tonic-gate * allows this.
50*7c478bd9Sstevel@tonic-gate */
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #if !defined(i386) && !defined(__i386) && !defined(__amd64)
53*7c478bd9Sstevel@tonic-gate #error This code is for x86 only
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate static union {
57*7c478bd9Sstevel@tonic-gate int i;
58*7c478bd9Sstevel@tonic-gate float f;
59*7c478bd9Sstevel@tonic-gate } inf = {
60*7c478bd9Sstevel@tonic-gate 0x7f800000
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
65*7c478bd9Sstevel@tonic-gate */
66*7c478bd9Sstevel@tonic-gate static int
testinfl(long double x)67*7c478bd9Sstevel@tonic-gate testinfl(long double x)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate union {
70*7c478bd9Sstevel@tonic-gate int i[3];
71*7c478bd9Sstevel@tonic-gate long double e;
72*7c478bd9Sstevel@tonic-gate } xx;
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate xx.e = x;
75*7c478bd9Sstevel@tonic-gate if ((xx.i[2] & 0x7fff) != 0x7fff || ((xx.i[1] << 1) | xx.i[0]) != 0)
76*7c478bd9Sstevel@tonic-gate return (0);
77*7c478bd9Sstevel@tonic-gate return (1 | ((xx.i[2] << 16) >> 31));
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate long double _Complex
_X_cplx_div(long double _Complex z,long double _Complex w)81*7c478bd9Sstevel@tonic-gate _X_cplx_div(long double _Complex z, long double _Complex w)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate long double _Complex v;
84*7c478bd9Sstevel@tonic-gate union {
85*7c478bd9Sstevel@tonic-gate int i[3];
86*7c478bd9Sstevel@tonic-gate long double e;
87*7c478bd9Sstevel@tonic-gate } aa, bb, cc, dd, ss;
88*7c478bd9Sstevel@tonic-gate long double a, b, c, d, r;
89*7c478bd9Sstevel@tonic-gate int ea, eb, ec, ed, ez, ew, es, i, j;
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate * The following is equivalent to
93*7c478bd9Sstevel@tonic-gate *
94*7c478bd9Sstevel@tonic-gate * a = creall(*z); b = cimagl(*z);
95*7c478bd9Sstevel@tonic-gate * c = creall(*w); d = cimagl(*w);
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate a = ((long double *)&z)[0];
98*7c478bd9Sstevel@tonic-gate b = ((long double *)&z)[1];
99*7c478bd9Sstevel@tonic-gate c = ((long double *)&w)[0];
100*7c478bd9Sstevel@tonic-gate d = ((long double *)&w)[1];
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate /* extract exponents to estimate |z| and |w| */
103*7c478bd9Sstevel@tonic-gate aa.e = a;
104*7c478bd9Sstevel@tonic-gate bb.e = b;
105*7c478bd9Sstevel@tonic-gate ea = aa.i[2] & 0x7fff;
106*7c478bd9Sstevel@tonic-gate eb = bb.i[2] & 0x7fff;
107*7c478bd9Sstevel@tonic-gate ez = (ea > eb)? ea : eb;
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate cc.e = c;
110*7c478bd9Sstevel@tonic-gate dd.e = d;
111*7c478bd9Sstevel@tonic-gate ec = cc.i[2] & 0x7fff;
112*7c478bd9Sstevel@tonic-gate ed = dd.i[2] & 0x7fff;
113*7c478bd9Sstevel@tonic-gate ew = (ec > ed)? ec : ed;
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate /* check for special cases */
116*7c478bd9Sstevel@tonic-gate if (ew >= 0x7fff) { /* w is inf or nan */
117*7c478bd9Sstevel@tonic-gate r = 0.0f;
118*7c478bd9Sstevel@tonic-gate i = testinfl(c);
119*7c478bd9Sstevel@tonic-gate j = testinfl(d);
120*7c478bd9Sstevel@tonic-gate if (i | j) { /* w is infinite */
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate * "factor out" infinity, being careful to preserve
123*7c478bd9Sstevel@tonic-gate * signs of finite values
124*7c478bd9Sstevel@tonic-gate */
125*7c478bd9Sstevel@tonic-gate c = i? i : (((cc.i[2] << 16) < 0)? -0.0f : 0.0f);
126*7c478bd9Sstevel@tonic-gate d = j? j : (((dd.i[2] << 16) < 0)? -0.0f : 0.0f);
127*7c478bd9Sstevel@tonic-gate if (ez >= 0x7ffe) {
128*7c478bd9Sstevel@tonic-gate /* scale to avoid overflow below */
129*7c478bd9Sstevel@tonic-gate c *= 0.5f;
130*7c478bd9Sstevel@tonic-gate d *= 0.5f;
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate ((long double *)&v)[0] = (a * c + b * d) * r;
134*7c478bd9Sstevel@tonic-gate ((long double *)&v)[1] = (b * c - a * d) * r;
135*7c478bd9Sstevel@tonic-gate return (v);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate if (ew == 0 && (cc.i[1] | cc.i[0] | dd.i[1] | dd.i[0]) == 0) {
139*7c478bd9Sstevel@tonic-gate /* w is zero; multiply z by 1/Re(w) - I * Im(w) */
140*7c478bd9Sstevel@tonic-gate c = 1.0f / c;
141*7c478bd9Sstevel@tonic-gate i = testinfl(a);
142*7c478bd9Sstevel@tonic-gate j = testinfl(b);
143*7c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */
144*7c478bd9Sstevel@tonic-gate a = i;
145*7c478bd9Sstevel@tonic-gate b = j;
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate ((long double *)&v)[0] = a * c + b * d;
148*7c478bd9Sstevel@tonic-gate ((long double *)&v)[1] = b * c - a * d;
149*7c478bd9Sstevel@tonic-gate return (v);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate if (ez >= 0x7fff) { /* z is inf or nan */
153*7c478bd9Sstevel@tonic-gate i = testinfl(a);
154*7c478bd9Sstevel@tonic-gate j = testinfl(b);
155*7c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */
156*7c478bd9Sstevel@tonic-gate a = i;
157*7c478bd9Sstevel@tonic-gate b = j;
158*7c478bd9Sstevel@tonic-gate r = inf.f;
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate ((long double *)&v)[0] = a * c + b * d;
161*7c478bd9Sstevel@tonic-gate ((long double *)&v)[1] = b * c - a * d;
162*7c478bd9Sstevel@tonic-gate return (v);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate /*
166*7c478bd9Sstevel@tonic-gate * Scale c and d to compute 1/|w|^2 and the real and imaginary
167*7c478bd9Sstevel@tonic-gate * parts of the quotient.
168*7c478bd9Sstevel@tonic-gate */
169*7c478bd9Sstevel@tonic-gate es = ((ew >> 2) - ew) + 0x6ffd;
170*7c478bd9Sstevel@tonic-gate if (ez < 0x0086) { /* |z| < 2^-16249 */
171*7c478bd9Sstevel@tonic-gate if (((ew - 0x3efe) | (0x4083 - ew)) >= 0)
172*7c478bd9Sstevel@tonic-gate es = ((0x4083 - ew) >> 1) + 0x3fff;
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate ss.i[2] = es;
175*7c478bd9Sstevel@tonic-gate ss.i[1] = 0x80000000;
176*7c478bd9Sstevel@tonic-gate ss.i[0] = 0;
177*7c478bd9Sstevel@tonic-gate
178*7c478bd9Sstevel@tonic-gate c *= ss.e;
179*7c478bd9Sstevel@tonic-gate d *= ss.e;
180*7c478bd9Sstevel@tonic-gate r = 1.0f / (c * c + d * d);
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate c *= ss.e;
183*7c478bd9Sstevel@tonic-gate d *= ss.e;
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate ((long double *)&v)[0] = (a * c + b * d) * r;
186*7c478bd9Sstevel@tonic-gate ((long double *)&v)[1] = (b * c - a * d) * r;
187*7c478bd9Sstevel@tonic-gate return (v);
188*7c478bd9Sstevel@tonic-gate }
189