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 * _F_cplx_div(z, w) returns z / w with infinities handled according
2N/A * to C99.
2N/A *
2N/A * If z and w are both finite and w is nonzero, _F_cplx_div(z, w)
2N/A * delivers the complex quotient q according to the usual formula:
2N/A * let a = Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x +
2N/A * I * y where x = (a * c + b * d) / r and y = (b * c - a * d) / r
2N/A * with r = c * c + d * d. This implementation computes intermediate
2N/A * results in double precision to avoid premature underflow or over-
2N/A * flow.
2N/A *
2N/A * If z is neither NaN nor zero and w is zero, or if z is infinite
2N/A * and w is finite and nonzero, _F_cplx_div delivers an infinite
2N/A * result. If z is finite and w is infinite, _F_cplx_div delivers
2N/A * a zero result.
2N/A *
2N/A * If z and w are both zero or both infinite, or if either z or w is
2N/A * a complex NaN, _F_cplx_div delivers NaN + I * NaN. C99 doesn't
2N/A * specify these cases.
2N/A *
2N/A * This implementation can raise spurious invalid operation, inexact,
2N/A * and division-by-zero exceptions. C99 allows this.
2N/A *
2N/A * Warning: Do not attempt to "optimize" this code by removing multi-
2N/A * plications by zero.
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/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_div(float _Complex z, float _Complex w)
2N/A{
2N/A float _Complex v;
2N/A union {
2N/A int i;
2N/A float f;
2N/A } cc, dd;
2N/A float a, b, c, d;
2N/A double r, x, y;
2N/A int i, j, recalc;
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 r = (double)c * c + (double)d * d;
2N/A
2N/A if (r == 0.0) {
2N/A /* w is zero; multiply z by 1/Re(w) - I * Im(w) */
2N/A c = 1.0f / c;
2N/A i = testinff(a);
2N/A j = testinff(b);
2N/A if (i | j) { /* z is infinite */
2N/A a = i;
2N/A b = j;
2N/A }
2N/A ((float *)&v)[0] = a * c + b * d;
2N/A ((float *)&v)[1] = b * c - a * d;
2N/A return (v);
2N/A }
2N/A
2N/A r = 1.0 / r;
2N/A x = ((double)a * c + (double)b * d) * r;
2N/A y = ((double)b * c - (double)a * d) * r;
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 * and nonzero. Since we handled the case w = 0 above,
2N/A * the only cases to check here are when one of z or w
2N/A * is infinite.
2N/A */
2N/A r = 1.0;
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 r = inf.d;
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 /*
2N/A * "factor out" infinity, being careful to preserve
2N/A * signs of finite values
2N/A */
2N/A cc.f = c;
2N/A dd.f = d;
2N/A c = i? i : ((cc.i < 0)? -0.0f : 0.0f);
2N/A d = j? j : ((dd.i < 0)? -0.0f : 0.0f);
2N/A r *= 0.0;
2N/A recalc = 1;
2N/A }
2N/A if (recalc) {
2N/A x = ((double)a * c + (double)b * d) * r;
2N/A y = ((double)b * c - (double)a * d) * r;
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}