1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * _D_cplx_div(z, w) returns z / w with infinities handled according
31*0Sstevel@tonic-gate * to C99.
32*0Sstevel@tonic-gate *
33*0Sstevel@tonic-gate * If z and w are both finite and w is nonzero, _D_cplx_div(z, w)
34*0Sstevel@tonic-gate * delivers the complex quotient q according to the usual formula:
35*0Sstevel@tonic-gate * let a = Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x +
36*0Sstevel@tonic-gate * I * y where x = (a * c + b * d) / r and y = (b * c - a * d) / r
37*0Sstevel@tonic-gate * with r = c * c + d * d. This implementation scales to avoid
38*0Sstevel@tonic-gate * premature underflow or overflow.
39*0Sstevel@tonic-gate *
40*0Sstevel@tonic-gate * If z is neither NaN nor zero and w is zero, or if z is infinite
41*0Sstevel@tonic-gate * and w is finite and nonzero, _D_cplx_div delivers an infinite
42*0Sstevel@tonic-gate * result. If z is finite and w is infinite, _D_cplx_div delivers
43*0Sstevel@tonic-gate * a zero result.
44*0Sstevel@tonic-gate *
45*0Sstevel@tonic-gate * If z and w are both zero or both infinite, or if either z or w is
46*0Sstevel@tonic-gate * a complex NaN, _D_cplx_div delivers NaN + I * NaN. C99 doesn't
47*0Sstevel@tonic-gate * specify these cases.
48*0Sstevel@tonic-gate *
49*0Sstevel@tonic-gate * This implementation can raise spurious underflow, overflow, in-
50*0Sstevel@tonic-gate * valid operation, inexact, and division-by-zero exceptions. C99
51*0Sstevel@tonic-gate * allows this.
52*0Sstevel@tonic-gate *
53*0Sstevel@tonic-gate * Warning: Do not attempt to "optimize" this code by removing multi-
54*0Sstevel@tonic-gate * plications by zero.
55*0Sstevel@tonic-gate */
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc)
58*0Sstevel@tonic-gate #error This code is for SPARC only
59*0Sstevel@tonic-gate #endif
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate static union {
62*0Sstevel@tonic-gate int i[2];
63*0Sstevel@tonic-gate double d;
64*0Sstevel@tonic-gate } inf = {
65*0Sstevel@tonic-gate 0x7ff00000, 0
66*0Sstevel@tonic-gate };
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
70*0Sstevel@tonic-gate */
71*0Sstevel@tonic-gate static int
testinf(double x)72*0Sstevel@tonic-gate testinf(double x)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate union {
75*0Sstevel@tonic-gate int i[2];
76*0Sstevel@tonic-gate double d;
77*0Sstevel@tonic-gate } xx;
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate xx.d = x;
80*0Sstevel@tonic-gate return (((((xx.i[0] << 1) - 0xffe00000) | xx.i[1]) == 0)?
81*0Sstevel@tonic-gate (1 | (xx.i[0] >> 31)) : 0);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate double _Complex
_D_cplx_div(double _Complex z,double _Complex w)85*0Sstevel@tonic-gate _D_cplx_div(double _Complex z, double _Complex w)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate double _Complex v;
88*0Sstevel@tonic-gate union {
89*0Sstevel@tonic-gate int i[2];
90*0Sstevel@tonic-gate double d;
91*0Sstevel@tonic-gate } aa, bb, cc, dd, ss;
92*0Sstevel@tonic-gate double a, b, c, d, r;
93*0Sstevel@tonic-gate int ha, hb, hc, hd, hz, hw, hs, i, j;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate * The following is equivalent to
97*0Sstevel@tonic-gate *
98*0Sstevel@tonic-gate * a = creal(z); b = cimag(z);
99*0Sstevel@tonic-gate * c = creal(w); d = cimag(w);
100*0Sstevel@tonic-gate */
101*0Sstevel@tonic-gate a = ((double *)&z)[0];
102*0Sstevel@tonic-gate b = ((double *)&z)[1];
103*0Sstevel@tonic-gate c = ((double *)&w)[0];
104*0Sstevel@tonic-gate d = ((double *)&w)[1];
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate /* extract high-order words to estimate |z| and |w| */
107*0Sstevel@tonic-gate aa.d = a;
108*0Sstevel@tonic-gate bb.d = b;
109*0Sstevel@tonic-gate ha = aa.i[0] & ~0x80000000;
110*0Sstevel@tonic-gate hb = bb.i[0] & ~0x80000000;
111*0Sstevel@tonic-gate hz = (ha > hb)? ha : hb;
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate cc.d = c;
114*0Sstevel@tonic-gate dd.d = d;
115*0Sstevel@tonic-gate hc = cc.i[0] & ~0x80000000;
116*0Sstevel@tonic-gate hd = dd.i[0] & ~0x80000000;
117*0Sstevel@tonic-gate hw = (hc > hd)? hc : hd;
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate /* check for special cases */
120*0Sstevel@tonic-gate if (hw >= 0x7ff00000) { /* w is inf or nan */
121*0Sstevel@tonic-gate r = 0.0;
122*0Sstevel@tonic-gate i = testinf(c);
123*0Sstevel@tonic-gate j = testinf(d);
124*0Sstevel@tonic-gate if (i | j) { /* w is infinite */
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate * "factor out" infinity, being careful to preserve
127*0Sstevel@tonic-gate * signs of finite values
128*0Sstevel@tonic-gate */
129*0Sstevel@tonic-gate c = i? i : ((cc.i[0] < 0)? -0.0 : 0.0);
130*0Sstevel@tonic-gate d = j? j : ((dd.i[0] < 0)? -0.0 : 0.0);
131*0Sstevel@tonic-gate if (hz >= 0x7fe00000) {
132*0Sstevel@tonic-gate /* scale to avoid overflow below */
133*0Sstevel@tonic-gate c *= 0.5;
134*0Sstevel@tonic-gate d *= 0.5;
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate ((double *)&v)[0] = (a * c + b * d) * r;
138*0Sstevel@tonic-gate ((double *)&v)[1] = (b * c - a * d) * r;
139*0Sstevel@tonic-gate return (v);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate if (hw < 0x00100000) {
143*0Sstevel@tonic-gate /*
144*0Sstevel@tonic-gate * This nonsense is needed to work around some SPARC
145*0Sstevel@tonic-gate * implementations of nonstandard mode; if both parts
146*0Sstevel@tonic-gate * of w are subnormal, multiply them by one to force
147*0Sstevel@tonic-gate * them to be flushed to zero when nonstandard mode
148*0Sstevel@tonic-gate * is enabled. Sheesh.
149*0Sstevel@tonic-gate */
150*0Sstevel@tonic-gate cc.d = c = c * 1.0;
151*0Sstevel@tonic-gate dd.d = d = d * 1.0;
152*0Sstevel@tonic-gate hc = cc.i[0] & ~0x80000000;
153*0Sstevel@tonic-gate hd = dd.i[0] & ~0x80000000;
154*0Sstevel@tonic-gate hw = (hc > hd)? hc : hd;
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate if (hw == 0 && (cc.i[1] | dd.i[1]) == 0) {
158*0Sstevel@tonic-gate /* w is zero; multiply z by 1/Re(w) - I * Im(w) */
159*0Sstevel@tonic-gate c = 1.0 / c;
160*0Sstevel@tonic-gate i = testinf(a);
161*0Sstevel@tonic-gate j = testinf(b);
162*0Sstevel@tonic-gate if (i | j) { /* z is infinite */
163*0Sstevel@tonic-gate a = i;
164*0Sstevel@tonic-gate b = j;
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate ((double *)&v)[0] = a * c + b * d;
167*0Sstevel@tonic-gate ((double *)&v)[1] = b * c - a * d;
168*0Sstevel@tonic-gate return (v);
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate if (hz >= 0x7ff00000) { /* z is inf or nan */
172*0Sstevel@tonic-gate r = 1.0;
173*0Sstevel@tonic-gate i = testinf(a);
174*0Sstevel@tonic-gate j = testinf(b);
175*0Sstevel@tonic-gate if (i | j) { /* z is infinite */
176*0Sstevel@tonic-gate a = i;
177*0Sstevel@tonic-gate b = j;
178*0Sstevel@tonic-gate r = inf.d;
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate ((double *)&v)[0] = (a * c + b * d) * r;
181*0Sstevel@tonic-gate ((double *)&v)[1] = (b * c - a * d) * r;
182*0Sstevel@tonic-gate return (v);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * Scale c and d to compute 1/|w|^2 and the real and imaginary
187*0Sstevel@tonic-gate * parts of the quotient.
188*0Sstevel@tonic-gate *
189*0Sstevel@tonic-gate * Note that for any s, if we let c' = sc, d' = sd, c'' = sc',
190*0Sstevel@tonic-gate * and d'' = sd', then
191*0Sstevel@tonic-gate *
192*0Sstevel@tonic-gate * (ac'' + bd'') / (c'^2 + d'^2) = (ac + bd) / (c^2 + d^2)
193*0Sstevel@tonic-gate *
194*0Sstevel@tonic-gate * and similarly for the imaginary part of the quotient. We want
195*0Sstevel@tonic-gate * to choose s such that (i) r := 1/(c'^2 + d'^2) can be computed
196*0Sstevel@tonic-gate * without overflow or harmful underflow, and (ii) (ac'' + bd'')
197*0Sstevel@tonic-gate * and (bc'' - ad'') can be computed without spurious overflow or
198*0Sstevel@tonic-gate * harmful underflow. To avoid unnecessary rounding, we restrict
199*0Sstevel@tonic-gate * s to a power of two.
200*0Sstevel@tonic-gate *
201*0Sstevel@tonic-gate * To satisfy (i), we need to choose s such that max(|c'|,|d'|)
202*0Sstevel@tonic-gate * is not too far from one. To satisfy (ii), we need to choose
203*0Sstevel@tonic-gate * s such that max(|c''|,|d''|) is also not too far from one.
204*0Sstevel@tonic-gate * There is some leeway in our choice, but to keep the logic
205*0Sstevel@tonic-gate * from getting overly complicated, we simply attempt to roughly
206*0Sstevel@tonic-gate * balance these constraints by choosing s so as to make r about
207*0Sstevel@tonic-gate * the same size as max(|c''|,|d''|). This corresponds to choos-
208*0Sstevel@tonic-gate * ing s to be a power of two near |w|^(-3/4).
209*0Sstevel@tonic-gate *
210*0Sstevel@tonic-gate * Regarding overflow, observe that if max(|c''|,|d''|) <= 1/2,
211*0Sstevel@tonic-gate * then the computation of (ac'' + bd'') and (bc'' - ad'') can-
212*0Sstevel@tonic-gate * not overflow; otherwise, the computation of either of these
213*0Sstevel@tonic-gate * values can only incur overflow if the true result would be
214*0Sstevel@tonic-gate * within a factor of two of the overflow threshold. In other
215*0Sstevel@tonic-gate * words, if we bias the choice of s such that at least one of
216*0Sstevel@tonic-gate *
217*0Sstevel@tonic-gate * max(|c''|,|d''|) <= 1/2 or r >= 2
218*0Sstevel@tonic-gate *
219*0Sstevel@tonic-gate * always holds, then no undeserved overflow can occur.
220*0Sstevel@tonic-gate *
221*0Sstevel@tonic-gate * To cope with underflow, note that if r < 2^-53, then any
222*0Sstevel@tonic-gate * intermediate results that underflow are insignificant; either
223*0Sstevel@tonic-gate * they will be added to normal results, rendering the under-
224*0Sstevel@tonic-gate * flow no worse than ordinary roundoff, or they will contribute
225*0Sstevel@tonic-gate * to a final result that is smaller than the smallest subnormal
226*0Sstevel@tonic-gate * number. Therefore, we need only modify the preceding logic
227*0Sstevel@tonic-gate * when z is very small and w is not too far from one. In that
228*0Sstevel@tonic-gate * case, we can reduce the effect of any intermediate underflow
229*0Sstevel@tonic-gate * to no worse than ordinary roundoff error by choosing s so as
230*0Sstevel@tonic-gate * to make max(|c''|,|d''|) large enough that at least one of
231*0Sstevel@tonic-gate * (ac'' + bd'') or (bc'' - ad'') is normal.
232*0Sstevel@tonic-gate */
233*0Sstevel@tonic-gate hs = (((hw >> 2) - hw) + 0x6fd7ffff) & 0xfff00000;
234*0Sstevel@tonic-gate if (hz < 0x07200000) { /* |z| < 2^-909 */
235*0Sstevel@tonic-gate if (((hw - 0x32800000) | (0x47100000 - hw)) >= 0)
236*0Sstevel@tonic-gate hs = (((0x47100000 - hw) >> 1) & 0xfff00000)
237*0Sstevel@tonic-gate + 0x3ff00000;
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate ss.i[0] = hs;
240*0Sstevel@tonic-gate ss.i[1] = 0;
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate c *= ss.d;
243*0Sstevel@tonic-gate d *= ss.d;
244*0Sstevel@tonic-gate r = 1.0 / (c * c + d * d);
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate c *= ss.d;
247*0Sstevel@tonic-gate d *= ss.d;
248*0Sstevel@tonic-gate ((double *)&v)[0] = (a * c + b * d) * r;
249*0Sstevel@tonic-gate ((double *)&v)[1] = (b * c - a * d) * r;
250*0Sstevel@tonic-gate return (v);
251*0Sstevel@tonic-gate }
252