1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * _F_cplx_div(z, w) returns z / w with infinities handled according 31*7c478bd9Sstevel@tonic-gate * to C99. 32*7c478bd9Sstevel@tonic-gate * 33*7c478bd9Sstevel@tonic-gate * If z and w are both finite and w is nonzero, _F_cplx_div(z, w) 34*7c478bd9Sstevel@tonic-gate * delivers the complex quotient q according to the usual formula: 35*7c478bd9Sstevel@tonic-gate * let a = Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x + 36*7c478bd9Sstevel@tonic-gate * I * y where x = (a * c + b * d) / r and y = (b * c - a * d) / r 37*7c478bd9Sstevel@tonic-gate * with r = c * c + d * d. This implementation computes intermediate 38*7c478bd9Sstevel@tonic-gate * results in double precision to avoid premature underflow or over- 39*7c478bd9Sstevel@tonic-gate * flow. 40*7c478bd9Sstevel@tonic-gate * 41*7c478bd9Sstevel@tonic-gate * If z is neither NaN nor zero and w is zero, or if z is infinite 42*7c478bd9Sstevel@tonic-gate * and w is finite and nonzero, _F_cplx_div delivers an infinite 43*7c478bd9Sstevel@tonic-gate * result. If z is finite and w is infinite, _F_cplx_div delivers 44*7c478bd9Sstevel@tonic-gate * a zero result. 45*7c478bd9Sstevel@tonic-gate * 46*7c478bd9Sstevel@tonic-gate * If z and w are both zero or both infinite, or if either z or w is 47*7c478bd9Sstevel@tonic-gate * a complex NaN, _F_cplx_div delivers NaN + I * NaN. C99 doesn't 48*7c478bd9Sstevel@tonic-gate * specify these cases. 49*7c478bd9Sstevel@tonic-gate * 50*7c478bd9Sstevel@tonic-gate * This implementation can raise spurious invalid operation, inexact, 51*7c478bd9Sstevel@tonic-gate * and division-by-zero exceptions. C99 allows this. 52*7c478bd9Sstevel@tonic-gate * 53*7c478bd9Sstevel@tonic-gate * Warning: Do not attempt to "optimize" this code by removing multi- 54*7c478bd9Sstevel@tonic-gate * plications by zero. 55*7c478bd9Sstevel@tonic-gate */ 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc) 58*7c478bd9Sstevel@tonic-gate #error This code is for SPARC only 59*7c478bd9Sstevel@tonic-gate #endif 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate static union { 62*7c478bd9Sstevel@tonic-gate int i[2]; 63*7c478bd9Sstevel@tonic-gate double d; 64*7c478bd9Sstevel@tonic-gate } inf = { 65*7c478bd9Sstevel@tonic-gate 0x7ff00000, 0 66*7c478bd9Sstevel@tonic-gate }; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate static int 72*7c478bd9Sstevel@tonic-gate testinff(float x) 73*7c478bd9Sstevel@tonic-gate { 74*7c478bd9Sstevel@tonic-gate union { 75*7c478bd9Sstevel@tonic-gate int i; 76*7c478bd9Sstevel@tonic-gate float f; 77*7c478bd9Sstevel@tonic-gate } xx; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate xx.f = x; 80*7c478bd9Sstevel@tonic-gate return ((((xx.i << 1) - 0xff000000) == 0)? (1 | (xx.i >> 31)) : 0); 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate float _Complex 84*7c478bd9Sstevel@tonic-gate _F_cplx_div(float _Complex z, float _Complex w) 85*7c478bd9Sstevel@tonic-gate { 86*7c478bd9Sstevel@tonic-gate float _Complex v; 87*7c478bd9Sstevel@tonic-gate union { 88*7c478bd9Sstevel@tonic-gate int i; 89*7c478bd9Sstevel@tonic-gate float f; 90*7c478bd9Sstevel@tonic-gate } cc, dd; 91*7c478bd9Sstevel@tonic-gate float a, b, c, d; 92*7c478bd9Sstevel@tonic-gate double r, x, y; 93*7c478bd9Sstevel@tonic-gate int i, j, recalc; 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate /* 96*7c478bd9Sstevel@tonic-gate * The following is equivalent to 97*7c478bd9Sstevel@tonic-gate * 98*7c478bd9Sstevel@tonic-gate * a = crealf(z); b = cimagf(z); 99*7c478bd9Sstevel@tonic-gate * c = crealf(w); d = cimagf(w); 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate a = ((float *)&z)[0]; 102*7c478bd9Sstevel@tonic-gate b = ((float *)&z)[1]; 103*7c478bd9Sstevel@tonic-gate c = ((float *)&w)[0]; 104*7c478bd9Sstevel@tonic-gate d = ((float *)&w)[1]; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate r = (double)c * c + (double)d * d; 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate if (r == 0.0) { 109*7c478bd9Sstevel@tonic-gate /* w is zero; multiply z by 1/Re(w) - I * Im(w) */ 110*7c478bd9Sstevel@tonic-gate c = 1.0f / c; 111*7c478bd9Sstevel@tonic-gate i = testinff(a); 112*7c478bd9Sstevel@tonic-gate j = testinff(b); 113*7c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */ 114*7c478bd9Sstevel@tonic-gate a = i; 115*7c478bd9Sstevel@tonic-gate b = j; 116*7c478bd9Sstevel@tonic-gate } 117*7c478bd9Sstevel@tonic-gate ((float *)&v)[0] = a * c + b * d; 118*7c478bd9Sstevel@tonic-gate ((float *)&v)[1] = b * c - a * d; 119*7c478bd9Sstevel@tonic-gate return (v); 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate r = 1.0 / r; 123*7c478bd9Sstevel@tonic-gate x = ((double)a * c + (double)b * d) * r; 124*7c478bd9Sstevel@tonic-gate y = ((double)b * c - (double)a * d) * r; 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate if (x != x && y != y) { 127*7c478bd9Sstevel@tonic-gate /* 128*7c478bd9Sstevel@tonic-gate * Both x and y are NaN, so z and w can't both be finite 129*7c478bd9Sstevel@tonic-gate * and nonzero. Since we handled the case w = 0 above, 130*7c478bd9Sstevel@tonic-gate * the only cases to check here are when one of z or w 131*7c478bd9Sstevel@tonic-gate * is infinite. 132*7c478bd9Sstevel@tonic-gate */ 133*7c478bd9Sstevel@tonic-gate r = 1.0; 134*7c478bd9Sstevel@tonic-gate recalc = 0; 135*7c478bd9Sstevel@tonic-gate i = testinff(a); 136*7c478bd9Sstevel@tonic-gate j = testinff(b); 137*7c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */ 138*7c478bd9Sstevel@tonic-gate /* "factor out" infinity */ 139*7c478bd9Sstevel@tonic-gate a = i; 140*7c478bd9Sstevel@tonic-gate b = j; 141*7c478bd9Sstevel@tonic-gate r = inf.d; 142*7c478bd9Sstevel@tonic-gate recalc = 1; 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate i = testinff(c); 145*7c478bd9Sstevel@tonic-gate j = testinff(d); 146*7c478bd9Sstevel@tonic-gate if (i | j) { /* w is infinite */ 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * "factor out" infinity, being careful to preserve 149*7c478bd9Sstevel@tonic-gate * signs of finite values 150*7c478bd9Sstevel@tonic-gate */ 151*7c478bd9Sstevel@tonic-gate cc.f = c; 152*7c478bd9Sstevel@tonic-gate dd.f = d; 153*7c478bd9Sstevel@tonic-gate c = i? i : ((cc.i < 0)? -0.0f : 0.0f); 154*7c478bd9Sstevel@tonic-gate d = j? j : ((dd.i < 0)? -0.0f : 0.0f); 155*7c478bd9Sstevel@tonic-gate r *= 0.0; 156*7c478bd9Sstevel@tonic-gate recalc = 1; 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate if (recalc) { 159*7c478bd9Sstevel@tonic-gate x = ((double)a * c + (double)b * d) * r; 160*7c478bd9Sstevel@tonic-gate y = ((double)b * c - (double)a * d) * r; 161*7c478bd9Sstevel@tonic-gate } 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate /* 165*7c478bd9Sstevel@tonic-gate * The following is equivalent to 166*7c478bd9Sstevel@tonic-gate * 167*7c478bd9Sstevel@tonic-gate * return x + I * y; 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate ((float *)&v)[0] = (float)x; 170*7c478bd9Sstevel@tonic-gate ((float *)&v)[1] = (float)y; 171*7c478bd9Sstevel@tonic-gate return (v); 172*7c478bd9Sstevel@tonic-gate } 173