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 * _F_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, _F_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 _F_cplx_mul(z, w) delivers x + I * y
2N/A * where x = a * c - b * d and y = a * d + b * c. This implementation
2N/A * uses extended precision to form these expressions, so none of the
2N/A * intermediate products can overflow.
2N/A *
2N/A * If one of z or w is infinite and the other is either finite nonzero
2N/A * or infinite, _F_cplx_mul delivers an infinite result. If one factor
2N/A * is infinite and the other is zero, _F_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 invalid operation and inexact
2N/A * exceptions. C99 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/Astatic union {
2N/A int i;
2N/A float f;
2N/A} inf = {
2N/A 0x7f800000
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/Atestinff(float x)
2N/A{
2N/A union {
2N/A int i;
2N/A float f;
2N/A } xx;
2N/A
2N/A xx.f = x;
2N/A return ((((xx.i << 1) - 0xff000000) == 0)? (1 | (xx.i >> 31)) : 0);
2N/A}
2N/A
2N/Afloat _Complex
2N/A_F_cplx_mul(float _Complex z, float _Complex w)
2N/A{
2N/A float _Complex v;
2N/A float a, b, c, d;
2N/A long double x, y;
2N/A int recalc, i, j;
2N/A
2N/A /*
2N/A * The following is equivalent to
2N/A *
2N/A * a = crealf(z); b = cimagf(z);
2N/A * c = crealf(w); d = cimagf(w);
2N/A */
2N/A a = ((float *)&z)[0];
2N/A b = ((float *)&z)[1];
2N/A c = ((float *)&w)[0];
2N/A d = ((float *)&w)[1];
2N/A
2N/A x = (long double)a * c - (long double)b * d;
2N/A y = (long double)a * d + (long double)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 = testinff(a);
2N/A j = testinff(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 = testinff(c);
2N/A j = testinff(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.f * ((long double)a * c - (long double)b * d);
2N/A y = inf.f * ((long double)a * d + (long double)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 ((float *)&v)[0] = (float)x;
2N/A ((float *)&v)[1] = (float)y;
2N/A return (v);
2N/A}