17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * On SPARC V8, _Q_cplx_div(v, z, w) sets *v = *z / *w with infin-
297c478bd9Sstevel@tonic-gate * ities handling according to C99.
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * On SPARC V9, _Q_cplx_div(z, w) returns *z / *w with infinities
327c478bd9Sstevel@tonic-gate * handled according to C99.
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * If z and w are both finite and w is nonzero, _Q_cplx_div delivers
357c478bd9Sstevel@tonic-gate * the complex quotient q according to the usual formula: let a =
367c478bd9Sstevel@tonic-gate * Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x + I * y
377c478bd9Sstevel@tonic-gate * where x = (a * c + b * d) / r and y = (b * c - a * d) / r with
387c478bd9Sstevel@tonic-gate * r = c * c + d * d. This implementation scales to avoid premature
397c478bd9Sstevel@tonic-gate * underflow or overflow.
407c478bd9Sstevel@tonic-gate *
417c478bd9Sstevel@tonic-gate * If z is neither NaN nor zero and w is zero, or if z is infinite
427c478bd9Sstevel@tonic-gate * and w is finite and nonzero, _Q_cplx_div delivers an infinite
437c478bd9Sstevel@tonic-gate * result. If z is finite and w is infinite, _Q_cplx_div delivers
447c478bd9Sstevel@tonic-gate * a zero result.
457c478bd9Sstevel@tonic-gate *
467c478bd9Sstevel@tonic-gate * If z and w are both zero or both infinite, or if either z or w is
477c478bd9Sstevel@tonic-gate * a complex NaN, _Q_cplx_div delivers NaN + I * NaN. C99 doesn't
487c478bd9Sstevel@tonic-gate * specify these cases.
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate * This implementation can raise spurious underflow, overflow, in-
517c478bd9Sstevel@tonic-gate * valid operation, inexact, and division-by-zero exceptions. C99
527c478bd9Sstevel@tonic-gate * allows this.
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc)
567c478bd9Sstevel@tonic-gate #error This code is for SPARC only
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate static union {
607c478bd9Sstevel@tonic-gate int i[4];
617c478bd9Sstevel@tonic-gate long double q;
627c478bd9Sstevel@tonic-gate } inf = {
637c478bd9Sstevel@tonic-gate 0x7fff0000, 0, 0, 0
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate static int
testinfl(long double x)707c478bd9Sstevel@tonic-gate testinfl(long double x)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate union {
737c478bd9Sstevel@tonic-gate int i[4];
747c478bd9Sstevel@tonic-gate long double q;
757c478bd9Sstevel@tonic-gate } xx;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate xx.q = x;
787c478bd9Sstevel@tonic-gate return (((((xx.i[0] << 1) - 0xfffe0000) | xx.i[1] | xx.i[2] | xx.i[3])
797c478bd9Sstevel@tonic-gate == 0)? (1 | (xx.i[0] >> 31)) : 0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate #ifdef __sparcv9
837c478bd9Sstevel@tonic-gate long double _Complex
_Q_cplx_div(const long double _Complex * z,const long double _Complex * w)847c478bd9Sstevel@tonic-gate _Q_cplx_div(const long double _Complex *z, const long double _Complex *w)
857c478bd9Sstevel@tonic-gate {
86*1d9fea2aSToomas Soome long double _Complex v = 0;
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate void
897c478bd9Sstevel@tonic-gate _Q_cplx_div(long double _Complex *v, const long double _Complex *z,
907c478bd9Sstevel@tonic-gate const long double _Complex *w)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate union {
947c478bd9Sstevel@tonic-gate int i[4];
957c478bd9Sstevel@tonic-gate long double q;
967c478bd9Sstevel@tonic-gate } aa, bb, cc, dd, ss;
977c478bd9Sstevel@tonic-gate long double a, b, c, d, r;
987c478bd9Sstevel@tonic-gate int ha, hb, hc, hd, hz, hw, hs, i, j;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * The following is equivalent to
1027c478bd9Sstevel@tonic-gate *
1037c478bd9Sstevel@tonic-gate * a = creall(*z); b = cimagl(*z);
1047c478bd9Sstevel@tonic-gate * c = creall(*w); d = cimagl(*w);
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate a = ((long double *)z)[0];
1077c478bd9Sstevel@tonic-gate b = ((long double *)z)[1];
1087c478bd9Sstevel@tonic-gate c = ((long double *)w)[0];
1097c478bd9Sstevel@tonic-gate d = ((long double *)w)[1];
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /* extract high-order words to estimate |z| and |w| */
1127c478bd9Sstevel@tonic-gate aa.q = a;
1137c478bd9Sstevel@tonic-gate bb.q = b;
1147c478bd9Sstevel@tonic-gate ha = aa.i[0] & ~0x80000000;
1157c478bd9Sstevel@tonic-gate hb = bb.i[0] & ~0x80000000;
1167c478bd9Sstevel@tonic-gate hz = (ha > hb)? ha : hb;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate cc.q = c;
1197c478bd9Sstevel@tonic-gate dd.q = d;
1207c478bd9Sstevel@tonic-gate hc = cc.i[0] & ~0x80000000;
1217c478bd9Sstevel@tonic-gate hd = dd.i[0] & ~0x80000000;
1227c478bd9Sstevel@tonic-gate hw = (hc > hd)? hc : hd;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /* check for special cases */
1257c478bd9Sstevel@tonic-gate if (hw >= 0x7fff0000) { /* w is inf or nan */
1267c478bd9Sstevel@tonic-gate r = 0.0l;
1277c478bd9Sstevel@tonic-gate i = testinfl(c);
1287c478bd9Sstevel@tonic-gate j = testinfl(d);
1297c478bd9Sstevel@tonic-gate if (i | j) { /* w is infinite */
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate * "factor out" infinity, being careful to preserve
1327c478bd9Sstevel@tonic-gate * signs of finite values
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate c = i? i : ((cc.i[0] < 0)? -0.0l : 0.0l);
1357c478bd9Sstevel@tonic-gate d = j? j : ((dd.i[0] < 0)? -0.0l : 0.0l);
1367c478bd9Sstevel@tonic-gate if (hz >= 0x7ffe0000) {
1377c478bd9Sstevel@tonic-gate /* scale to avoid overflow below */
1387c478bd9Sstevel@tonic-gate c *= 0.5l;
1397c478bd9Sstevel@tonic-gate d *= 0.5l;
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate goto done;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate if (hw == 0 && (cc.i[1] | cc.i[2] | cc.i[3] |
1467c478bd9Sstevel@tonic-gate dd.i[1] | dd.i[2] | dd.i[3]) == 0) {
1477c478bd9Sstevel@tonic-gate /* w is zero; multiply z by 1/Re(w) - I * Im(w) */
1487c478bd9Sstevel@tonic-gate r = 1.0l;
1497c478bd9Sstevel@tonic-gate c = 1.0l / c;
1507c478bd9Sstevel@tonic-gate i = testinfl(a);
1517c478bd9Sstevel@tonic-gate j = testinfl(b);
1527c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */
1537c478bd9Sstevel@tonic-gate a = i;
1547c478bd9Sstevel@tonic-gate b = j;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate goto done;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (hz >= 0x7fff0000) { /* z is inf or nan */
1607c478bd9Sstevel@tonic-gate r = 1.0l;
1617c478bd9Sstevel@tonic-gate i = testinfl(a);
1627c478bd9Sstevel@tonic-gate j = testinfl(b);
1637c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */
1647c478bd9Sstevel@tonic-gate a = i;
1657c478bd9Sstevel@tonic-gate b = j;
1667c478bd9Sstevel@tonic-gate r = inf.q;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate goto done;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate * Scale c and d to compute 1/|w|^2 and the real and imaginary
1737c478bd9Sstevel@tonic-gate * parts of the quotient.
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate hs = (((hw >> 2) - hw) + 0x6ffd7fff) & 0xffff0000;
1767c478bd9Sstevel@tonic-gate if (hz < 0x00ea0000) { /* |z| < 2^-16149 */
1777c478bd9Sstevel@tonic-gate if (((hw - 0x3e380000) | (0x40e90000 - hw)) >= 0)
1787c478bd9Sstevel@tonic-gate hs = (((0x40e90000 - hw) >> 1) & 0xffff0000)
1797c478bd9Sstevel@tonic-gate + 0x3fff0000;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate ss.i[0] = hs;
1827c478bd9Sstevel@tonic-gate ss.i[1] = ss.i[2] = ss.i[3] = 0;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate c *= ss.q;
1857c478bd9Sstevel@tonic-gate d *= ss.q;
1867c478bd9Sstevel@tonic-gate r = 1.0l / (c * c + d * d);
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate c *= ss.q;
1897c478bd9Sstevel@tonic-gate d *= ss.q;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate done:
1927c478bd9Sstevel@tonic-gate #ifdef __sparcv9
1937c478bd9Sstevel@tonic-gate ((long double *)&v)[0] = (a * c + b * d) * r;
1947c478bd9Sstevel@tonic-gate ((long double *)&v)[1] = (b * c - a * d) * r;
1957c478bd9Sstevel@tonic-gate return (v);
1967c478bd9Sstevel@tonic-gate #else
1977c478bd9Sstevel@tonic-gate ((long double *)v)[0] = (a * c + b * d) * r;
1987c478bd9Sstevel@tonic-gate ((long double *)v)[1] = (b * c - a * d) * r;
1997c478bd9Sstevel@tonic-gate #endif
2007c478bd9Sstevel@tonic-gate }
201