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 2003 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 * _D_cplx_div_ix(b, w) returns (I * b) / w with infinities handled
29*7c478bd9Sstevel@tonic-gate * according to C99.
30*7c478bd9Sstevel@tonic-gate *
31*7c478bd9Sstevel@tonic-gate * If b and w are both finite and w is nonzero, _D_cplx_div_ix(b, w)
32*7c478bd9Sstevel@tonic-gate * delivers the complex quotient q according to the usual formula:
33*7c478bd9Sstevel@tonic-gate * let c = Re(w), and d = Im(w); then q = x + I * y where x = (b * d)
34*7c478bd9Sstevel@tonic-gate * / r and y = (b * c) / r with r = c * c + d * d. This implementa-
35*7c478bd9Sstevel@tonic-gate * tion scales to avoid premature underflow or overflow.
36*7c478bd9Sstevel@tonic-gate *
37*7c478bd9Sstevel@tonic-gate * If b is neither NaN nor zero and w is zero, or if b is infinite
38*7c478bd9Sstevel@tonic-gate * and w is finite and nonzero, _D_cplx_div_ix delivers an infinite
39*7c478bd9Sstevel@tonic-gate * result. If b is finite and w is infinite, _D_cplx_div_ix delivers
40*7c478bd9Sstevel@tonic-gate * a zero result.
41*7c478bd9Sstevel@tonic-gate *
42*7c478bd9Sstevel@tonic-gate * If b and w are both zero or both infinite, or if either b or w is
43*7c478bd9Sstevel@tonic-gate * NaN, _D_cplx_div_ix delivers NaN + I * NaN. C99 doesn't specify
44*7c478bd9Sstevel@tonic-gate * these cases.
45*7c478bd9Sstevel@tonic-gate *
46*7c478bd9Sstevel@tonic-gate * This implementation can raise spurious underflow, overflow, in-
47*7c478bd9Sstevel@tonic-gate * valid operation, inexact, and division-by-zero exceptions. C99
48*7c478bd9Sstevel@tonic-gate * allows this.
49*7c478bd9Sstevel@tonic-gate *
50*7c478bd9Sstevel@tonic-gate * Warning: Do not attempt to "optimize" this code by removing multi-
51*7c478bd9Sstevel@tonic-gate * plications by zero.
52*7c478bd9Sstevel@tonic-gate */
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc)
55*7c478bd9Sstevel@tonic-gate #error This code is for SPARC only
56*7c478bd9Sstevel@tonic-gate #endif
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate * scl[i].d = 2^(250*(4-i)) for i = 0, ..., 9
60*7c478bd9Sstevel@tonic-gate */
61*7c478bd9Sstevel@tonic-gate static const union {
62*7c478bd9Sstevel@tonic-gate int i[2];
63*7c478bd9Sstevel@tonic-gate double d;
64*7c478bd9Sstevel@tonic-gate } scl[9] = {
65*7c478bd9Sstevel@tonic-gate { 0x7e700000, 0 },
66*7c478bd9Sstevel@tonic-gate { 0x6ed00000, 0 },
67*7c478bd9Sstevel@tonic-gate { 0x5f300000, 0 },
68*7c478bd9Sstevel@tonic-gate { 0x4f900000, 0 },
69*7c478bd9Sstevel@tonic-gate { 0x3ff00000, 0 },
70*7c478bd9Sstevel@tonic-gate { 0x30500000, 0 },
71*7c478bd9Sstevel@tonic-gate { 0x20b00000, 0 },
72*7c478bd9Sstevel@tonic-gate { 0x11100000, 0 },
73*7c478bd9Sstevel@tonic-gate { 0x01700000, 0 }
74*7c478bd9Sstevel@tonic-gate };
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate /*
77*7c478bd9Sstevel@tonic-gate * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
78*7c478bd9Sstevel@tonic-gate */
79*7c478bd9Sstevel@tonic-gate static int
testinf(double x)80*7c478bd9Sstevel@tonic-gate testinf(double x)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate union {
83*7c478bd9Sstevel@tonic-gate int i[2];
84*7c478bd9Sstevel@tonic-gate double d;
85*7c478bd9Sstevel@tonic-gate } xx;
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate xx.d = x;
88*7c478bd9Sstevel@tonic-gate return (((((xx.i[0] << 1) - 0xffe00000) | xx.i[1]) == 0)?
89*7c478bd9Sstevel@tonic-gate (1 | (xx.i[0] >> 31)) : 0);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate double _Complex
_D_cplx_div_ix(double b,double _Complex w)93*7c478bd9Sstevel@tonic-gate _D_cplx_div_ix(double b, double _Complex w)
94*7c478bd9Sstevel@tonic-gate {
95*7c478bd9Sstevel@tonic-gate double _Complex v;
96*7c478bd9Sstevel@tonic-gate union {
97*7c478bd9Sstevel@tonic-gate int i[2];
98*7c478bd9Sstevel@tonic-gate double d;
99*7c478bd9Sstevel@tonic-gate } bb, cc, dd;
100*7c478bd9Sstevel@tonic-gate double c, d, sc, sd, r;
101*7c478bd9Sstevel@tonic-gate int hb, hc, hd, hw, i, j;
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate * The following is equivalent to
105*7c478bd9Sstevel@tonic-gate *
106*7c478bd9Sstevel@tonic-gate * c = creal(w); d = cimag(w);
107*7c478bd9Sstevel@tonic-gate */
108*7c478bd9Sstevel@tonic-gate c = ((double *)&w)[0];
109*7c478bd9Sstevel@tonic-gate d = ((double *)&w)[1];
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate /* extract high-order words to estimate |b| and |w| */
112*7c478bd9Sstevel@tonic-gate bb.d = b;
113*7c478bd9Sstevel@tonic-gate hb = bb.i[0] & ~0x80000000;
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate cc.d = c;
116*7c478bd9Sstevel@tonic-gate dd.d = d;
117*7c478bd9Sstevel@tonic-gate hc = cc.i[0] & ~0x80000000;
118*7c478bd9Sstevel@tonic-gate hd = dd.i[0] & ~0x80000000;
119*7c478bd9Sstevel@tonic-gate hw = (hc > hd)? hc : hd;
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate /* check for special cases */
122*7c478bd9Sstevel@tonic-gate if (hw >= 0x7ff00000) { /* w is inf or nan */
123*7c478bd9Sstevel@tonic-gate i = testinf(c);
124*7c478bd9Sstevel@tonic-gate j = testinf(d);
125*7c478bd9Sstevel@tonic-gate if (i | j) { /* w is infinite */
126*7c478bd9Sstevel@tonic-gate c = (cc.i[0] < 0)? -0.0 : 0.0;
127*7c478bd9Sstevel@tonic-gate d = (dd.i[0] < 0)? -0.0 : 0.0;
128*7c478bd9Sstevel@tonic-gate } else /* w is nan */
129*7c478bd9Sstevel@tonic-gate b *= c * d;
130*7c478bd9Sstevel@tonic-gate ((double *)&v)[0] = b * d;
131*7c478bd9Sstevel@tonic-gate ((double *)&v)[1] = b * c;
132*7c478bd9Sstevel@tonic-gate return (v);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate if (hw < 0x00100000) {
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate * This nonsense is needed to work around some SPARC
138*7c478bd9Sstevel@tonic-gate * implementations of nonstandard mode; if both parts
139*7c478bd9Sstevel@tonic-gate * of w are subnormal, multiply them by one to force
140*7c478bd9Sstevel@tonic-gate * them to be flushed to zero when nonstandard mode
141*7c478bd9Sstevel@tonic-gate * is enabled. Sheesh.
142*7c478bd9Sstevel@tonic-gate */
143*7c478bd9Sstevel@tonic-gate cc.d = c = c * 1.0;
144*7c478bd9Sstevel@tonic-gate dd.d = d = d * 1.0;
145*7c478bd9Sstevel@tonic-gate hc = cc.i[0] & ~0x80000000;
146*7c478bd9Sstevel@tonic-gate hd = dd.i[0] & ~0x80000000;
147*7c478bd9Sstevel@tonic-gate hw = (hc > hd)? hc : hd;
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate if (hw == 0 && (cc.i[1] | dd.i[1]) == 0) {
151*7c478bd9Sstevel@tonic-gate /* w is zero; multiply b by 1/Re(w) - I * Im(w) */
152*7c478bd9Sstevel@tonic-gate c = 1.0 / c;
153*7c478bd9Sstevel@tonic-gate j = testinf(b);
154*7c478bd9Sstevel@tonic-gate if (j) { /* b is infinite */
155*7c478bd9Sstevel@tonic-gate b = j;
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate ((double *)&v)[0] = (b == 0.0)? b * c : b * d;
158*7c478bd9Sstevel@tonic-gate ((double *)&v)[1] = b * c;
159*7c478bd9Sstevel@tonic-gate return (v);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate if (hb >= 0x7ff00000) { /* a is inf or nan */
163*7c478bd9Sstevel@tonic-gate ((double *)&v)[0] = b * d;
164*7c478bd9Sstevel@tonic-gate ((double *)&v)[1] = b * c;
165*7c478bd9Sstevel@tonic-gate return (v);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate * Compute the real and imaginary parts of the quotient,
170*7c478bd9Sstevel@tonic-gate * scaling to avoid overflow or underflow.
171*7c478bd9Sstevel@tonic-gate */
172*7c478bd9Sstevel@tonic-gate hw = (hw - 0x38000000) >> 28;
173*7c478bd9Sstevel@tonic-gate sc = c * scl[hw + 4].d;
174*7c478bd9Sstevel@tonic-gate sd = d * scl[hw + 4].d;
175*7c478bd9Sstevel@tonic-gate r = sc * sc + sd * sd;
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate hb = (hb - 0x38000000) >> 28;
178*7c478bd9Sstevel@tonic-gate b = (b * scl[hb + 4].d) / r;
179*7c478bd9Sstevel@tonic-gate hb -= (hw + hw);
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate hc = (hc - 0x38000000) >> 28;
182*7c478bd9Sstevel@tonic-gate c = (c * scl[hc + 4].d) * b;
183*7c478bd9Sstevel@tonic-gate hc += hb;
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate hd = (hd - 0x38000000) >> 28;
186*7c478bd9Sstevel@tonic-gate d = (d * scl[hd + 4].d) * b;
187*7c478bd9Sstevel@tonic-gate hd += hb;
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate /* compensate for scaling */
190*7c478bd9Sstevel@tonic-gate sc = scl[3].d; /* 2^250 */
191*7c478bd9Sstevel@tonic-gate if (hc < 0) {
192*7c478bd9Sstevel@tonic-gate hc = -hc;
193*7c478bd9Sstevel@tonic-gate sc = scl[5].d; /* 2^-250 */
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate while (hc--)
196*7c478bd9Sstevel@tonic-gate c *= sc;
197*7c478bd9Sstevel@tonic-gate
198*7c478bd9Sstevel@tonic-gate sd = scl[3].d;
199*7c478bd9Sstevel@tonic-gate if (hd < 0) {
200*7c478bd9Sstevel@tonic-gate hd = -hd;
201*7c478bd9Sstevel@tonic-gate sd = scl[5].d;
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate while (hd--)
204*7c478bd9Sstevel@tonic-gate d *= sd;
205*7c478bd9Sstevel@tonic-gate
206*7c478bd9Sstevel@tonic-gate ((double *)&v)[0] = d;
207*7c478bd9Sstevel@tonic-gate ((double *)&v)[1] = c;
208*7c478bd9Sstevel@tonic-gate return (v);
209*7c478bd9Sstevel@tonic-gate }
210