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 2003 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 * _D_cplx_mul(z, w) returns z * w with infinities handled according
2N/A * to C99.
2N/A *
2N/A * If z and w are both finite, _D_cplx_mul(z, w) delivers the complex
2N/A * product according to the usual formula: let a = Re(z), b = Im(z),
2N/A * c = Re(w), and d = Im(w); then _D_cplx_mul(z, w) delivers x + I * y
2N/A * where x = a * c - b * d and y = a * d + b * c. Note that if both
2N/A * ac and bd overflow, then at least one of ad or bc must also over-
2N/A * flow, and vice versa, so that if one component of the product is
2N/A * NaN, the other is infinite. (Such a value is considered infinite
2N/A * according to C99.)
2N/A *
2N/A * If one of z or w is infinite and the other is either finite nonzero
2N/A * or infinite, _D_cplx_mul delivers an infinite result. If one factor
2N/A * is infinite and the other is zero, _D_cplx_mul delivers NaN + I * NaN.
2N/A * C99 doesn't specify the latter case.
2N/A *
2N/A * C99 also doesn't specify what should happen if either z or w is a
2N/A * complex NaN (i.e., neither finite nor infinite). This implementation
2N/A * delivers NaN + I * NaN in this case.
2N/A *
2N/A * This implementation can raise spurious underflow, overflow, invalid
2N/A * operation, and inexact exceptions. C99 allows this.
2N/A */
2N/A
2N/A#if !defined(sparc) && !defined(__sparc)
2N/A#error This code is for SPARC only
2N/A#endif
2N/A
2N/Astatic union {
2N/A int i[2];
2N/A double d;
2N/A} inf = {
2N/A 0x7ff00000, 0
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/Atestinf(double x)
2N/A{
2N/A union {
2N/A int i[2];
2N/A double d;
2N/A } xx;
2N/A
2N/A xx.d = x;
2N/A return (((((xx.i[0] << 1) - 0xffe00000) | xx.i[1]) == 0)?
2N/A (1 | (xx.i[0] >> 31)) : 0);
2N/A}
2N/A
2N/Adouble _Complex
2N/A_D_cplx_mul(double _Complex z, double _Complex w)
2N/A{
2N/A double _Complex v;
2N/A double a, b, c, d, x, y;
2N/A int recalc, i, j;
2N/A
2N/A /*
2N/A * The following is equivalent to
2N/A *
2N/A * a = creal(z); b = cimag(z);
2N/A * c = creal(w); d = cimag(w);
2N/A */
2N/A a = ((double *)&z)[0];
2N/A b = ((double *)&z)[1];
2N/A c = ((double *)&w)[0];
2N/A d = ((double *)&w)[1];
2N/A
2N/A x = a * c - b * d;
2N/A y = a * d + b * c;
2N/A
2N/A if (x != x && y != y) {
2N/A /*
2N/A * Both x and y are NaN, so z and w can't both be finite.
2N/A * If at least one of z or w is a complex NaN, and neither
2N/A * is infinite, then we might as well deliver NaN + I * NaN.
2N/A * So the only cases to check are when one of z or w is
2N/A * infinite.
2N/A */
2N/A recalc = 0;
2N/A i = testinf(a);
2N/A j = testinf(b);
2N/A if (i | j) { /* z is infinite */
2N/A /* "factor out" infinity */
2N/A a = i;
2N/A b = j;
2N/A recalc = 1;
2N/A }
2N/A i = testinf(c);
2N/A j = testinf(d);
2N/A if (i | j) { /* w is infinite */
2N/A /* "factor out" infinity */
2N/A c = i;
2N/A d = j;
2N/A recalc = 1;
2N/A }
2N/A if (recalc) {
2N/A x = inf.d * (a * c - b * d);
2N/A y = inf.d * (a * d + b * c);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * The following is equivalent to
2N/A *
2N/A * return x + I * y;
2N/A */
2N/A ((double *)&v)[0] = x;
2N/A ((double *)&v)[1] = y;
2N/A return (v);
2N/A}