2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * _X_cplx_div_ix(b, w) returns (I * b) / w with infinities handled
2N/A * according to C99.
2N/A *
2N/A * If b and w are both finite and w is nonzero, _X_cplx_div_ix de-
2N/A * livers the complex quotient q according to the usual formula: let
2N/A * c = Re(w), and d = Im(w); then q = x + I * y where x = (b * d) / r
2N/A * and y = (b * c) / r with r = c * c + d * d. This implementation
2N/A * scales to avoid premature underflow or overflow.
2N/A *
2N/A * If b is neither NaN nor zero and w is zero, or if b is infinite
2N/A * and w is finite and nonzero, _X_cplx_div_ix delivers an infinite
2N/A * result. If b is finite and w is infinite, _X_cplx_div_ix delivers
2N/A * a zero result.
2N/A *
2N/A * If b and w are both zero or both infinite, or if either b or w is
2N/A * NaN, _X_cplx_div_ix delivers NaN + I * NaN. C99 doesn't specify
2N/A * these cases.
2N/A *
2N/A * This implementation can raise spurious underflow, overflow, in-
2N/A * valid operation, inexact, and division-by-zero exceptions. C99
2N/A * allows this.
2N/A */
2N/A
2N/A#if !defined(i386) && !defined(__i386) && !defined(__amd64)
2N/A#error This code is for x86 only
2N/A#endif
2N/A
2N/A/*
2N/A * scl[i].e = 2^(4080*(4-i)) for i = 0, ..., 9
2N/A */
2N/Astatic const union {
2N/A unsigned int i[3];
2N/A long double e;
2N/A} scl[9] = {
2N/A { 0, 0x80000000, 0x7fbf },
2N/A { 0, 0x80000000, 0x6fcf },
2N/A { 0, 0x80000000, 0x5fdf },
2N/A { 0, 0x80000000, 0x4fef },
2N/A { 0, 0x80000000, 0x3fff },
2N/A { 0, 0x80000000, 0x300f },
2N/A { 0, 0x80000000, 0x201f },
2N/A { 0, 0x80000000, 0x102f },
2N/A { 0, 0x80000000, 0x003f }
2N/A};
2N/A
2N/A/*
2N/A * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
2N/A */
2N/Astatic int
2N/Atestinfl(long double x)
2N/A{
2N/A union {
2N/A int i[3];
2N/A long double e;
2N/A } xx;
2N/A
2N/A xx.e = x;
2N/A if ((xx.i[2] & 0x7fff) != 0x7fff || ((xx.i[1] << 1) | xx.i[0]) != 0)
2N/A return (0);
2N/A return (1 | ((xx.i[2] << 16) >> 31));
2N/A}
2N/A
2N/Along double _Complex
2N/A_X_cplx_div_ix(long double b, long double _Complex w)
2N/A{
2N/A long double _Complex v;
2N/A union {
2N/A int i[3];
2N/A long double e;
2N/A } bb, cc, dd;
2N/A long double c, d, sc, sd, r;
2N/A int eb, ec, ed, ew, i, j;
2N/A
2N/A /*
2N/A * The following is equivalent to
2N/A *
2N/A * c = creall(*w); d = cimagl(*w);
2N/A */
2N/A c = ((long double *)&w)[0];
2N/A d = ((long double *)&w)[1];
2N/A
2N/A /* extract exponents to estimate |z| and |w| */
2N/A bb.e = b;
2N/A eb = bb.i[2] & 0x7fff;
2N/A
2N/A cc.e = c;
2N/A dd.e = d;
2N/A ec = cc.i[2] & 0x7fff;
2N/A ed = dd.i[2] & 0x7fff;
2N/A ew = (ec > ed)? ec : ed;
2N/A
2N/A /* check for special cases */
2N/A if (ew >= 0x7fff) { /* w is inf or nan */
2N/A i = testinfl(c);
2N/A j = testinfl(d);
2N/A if (i | j) { /* w is infinite */
2N/A c = ((cc.i[2] << 16) < 0)? -0.0f : 0.0f;
2N/A d = ((dd.i[2] << 16) < 0)? -0.0f : 0.0f;
2N/A } else /* w is nan */
2N/A b += c + d;
2N/A ((long double *)&v)[0] = b * d;
2N/A ((long double *)&v)[1] = b * c;
2N/A return (v);
2N/A }
2N/A
2N/A if (ew == 0 && (cc.i[1] | cc.i[0] | dd.i[1] | dd.i[0]) == 0) {
2N/A /* w is zero; multiply b by 1/Re(w) - I * Im(w) */
2N/A c = 1.0f / c;
2N/A j = testinfl(b);
2N/A if (j) { /* b is infinite */
2N/A b = j;
2N/A }
2N/A ((long double *)&v)[0] = (b == 0.0f)? b * c : b * d;
2N/A ((long double *)&v)[1] = b * c;
2N/A return (v);
2N/A }
2N/A
2N/A if (eb >= 0x7fff) { /* a is inf or nan */
2N/A ((long double *)&v)[0] = b * d;
2N/A ((long double *)&v)[1] = b * c;
2N/A return (v);
2N/A }
2N/A
2N/A /*
2N/A * Compute the real and imaginary parts of the quotient,
2N/A * scaling to avoid overflow or underflow.
2N/A */
2N/A ew = (ew - 0x3800) >> 12;
2N/A sc = c * scl[ew + 4].e;
2N/A sd = d * scl[ew + 4].e;
2N/A r = sc * sc + sd * sd;
2N/A
2N/A eb = (eb - 0x3800) >> 12;
2N/A b = (b * scl[eb + 4].e) / r;
2N/A eb -= (ew + ew);
2N/A
2N/A ec = (ec - 0x3800) >> 12;
2N/A c = (c * scl[ec + 4].e) * b;
2N/A ec += eb;
2N/A
2N/A ed = (ed - 0x3800) >> 12;
2N/A d = (d * scl[ed + 4].e) * b;
2N/A ed += eb;
2N/A
2N/A /* compensate for scaling */
2N/A sc = scl[3].e; /* 2^4080 */
2N/A if (ec < 0) {
2N/A ec = -ec;
2N/A sc = scl[5].e; /* 2^-4080 */
2N/A }
2N/A while (ec--)
2N/A c *= sc;
2N/A
2N/A sd = scl[3].e;
2N/A if (ed < 0) {
2N/A ed = -ed;
2N/A sd = scl[5].e;
2N/A }
2N/A while (ed--)
2N/A d *= sd;
2N/A
2N/A ((long double *)&v)[0] = d;
2N/A ((long double *)&v)[1] = c;
2N/A return (v);
2N/A}