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 * On SPARC V8, _Q_cplx_mul(v, z, w) sets *v = *z * *w with infinities 31*7c478bd9Sstevel@tonic-gate * handled according to C99. 32*7c478bd9Sstevel@tonic-gate * 33*7c478bd9Sstevel@tonic-gate * On SPARC V9, _Q_cplx_mul(z, w) returns *z * *w with infinities 34*7c478bd9Sstevel@tonic-gate * handled according to C99. 35*7c478bd9Sstevel@tonic-gate * 36*7c478bd9Sstevel@tonic-gate * If z and w are both finite, _Q_cplx_mul delivers the complex 37*7c478bd9Sstevel@tonic-gate * product according to the usual formula: let a = Re(z), b = Im(z), 38*7c478bd9Sstevel@tonic-gate * c = Re(w), and d = Im(w); then _Q_cplx_mul delivers x + I * y 39*7c478bd9Sstevel@tonic-gate * where x = a * c - b * d and y = a * d + b * c. Note that if both 40*7c478bd9Sstevel@tonic-gate * ac and bd overflow, then at least one of ad or bc must also over- 41*7c478bd9Sstevel@tonic-gate * flow, and vice versa, so that if one component of the product is 42*7c478bd9Sstevel@tonic-gate * NaN, the other is infinite. (Such a value is considered infinite 43*7c478bd9Sstevel@tonic-gate * according to C99.) 44*7c478bd9Sstevel@tonic-gate * 45*7c478bd9Sstevel@tonic-gate * If one of z or w is infinite and the other is either finite nonzero 46*7c478bd9Sstevel@tonic-gate * or infinite, _Q_cplx_mul delivers an infinite result. If one factor 47*7c478bd9Sstevel@tonic-gate * is infinite and the other is zero, _Q_cplx_mul delivers NaN + I * NaN. 48*7c478bd9Sstevel@tonic-gate * C99 doesn't specify the latter case. 49*7c478bd9Sstevel@tonic-gate * 50*7c478bd9Sstevel@tonic-gate * C99 also doesn't specify what should happen if either z or w is a 51*7c478bd9Sstevel@tonic-gate * complex NaN (i.e., neither finite nor infinite). This implementation 52*7c478bd9Sstevel@tonic-gate * delivers NaN + I * NaN in this case. 53*7c478bd9Sstevel@tonic-gate * 54*7c478bd9Sstevel@tonic-gate * This implementation can raise spurious underflow, overflow, invalid 55*7c478bd9Sstevel@tonic-gate * operation, and inexact exceptions. C99 allows this. 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc) 59*7c478bd9Sstevel@tonic-gate #error This code is for SPARC only 60*7c478bd9Sstevel@tonic-gate #endif 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate static union { 63*7c478bd9Sstevel@tonic-gate int i[4]; 64*7c478bd9Sstevel@tonic-gate long double q; 65*7c478bd9Sstevel@tonic-gate } inf = { 66*7c478bd9Sstevel@tonic-gate 0x7fff0000, 0, 0, 0 67*7c478bd9Sstevel@tonic-gate }; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate /* 70*7c478bd9Sstevel@tonic-gate * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise 71*7c478bd9Sstevel@tonic-gate */ 72*7c478bd9Sstevel@tonic-gate static int 73*7c478bd9Sstevel@tonic-gate testinfl(long double x) 74*7c478bd9Sstevel@tonic-gate { 75*7c478bd9Sstevel@tonic-gate union { 76*7c478bd9Sstevel@tonic-gate int i[4]; 77*7c478bd9Sstevel@tonic-gate long double q; 78*7c478bd9Sstevel@tonic-gate } xx; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate xx.q = x; 81*7c478bd9Sstevel@tonic-gate return (((((xx.i[0] << 1) - 0xfffe0000) | xx.i[1] | xx.i[2] | xx.i[3]) 82*7c478bd9Sstevel@tonic-gate == 0)? (1 | (xx.i[0] >> 31)) : 0); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate #ifdef __sparcv9 86*7c478bd9Sstevel@tonic-gate long double _Complex 87*7c478bd9Sstevel@tonic-gate _Q_cplx_mul(const long double _Complex *z, const long double _Complex *w) 88*7c478bd9Sstevel@tonic-gate { 89*7c478bd9Sstevel@tonic-gate long double _Complex v; 90*7c478bd9Sstevel@tonic-gate #else 91*7c478bd9Sstevel@tonic-gate void 92*7c478bd9Sstevel@tonic-gate _Q_cplx_mul(long double _Complex *v, const long double _Complex *z, 93*7c478bd9Sstevel@tonic-gate const long double _Complex *w) 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate #endif 96*7c478bd9Sstevel@tonic-gate long double a, b, c, d, x, y; 97*7c478bd9Sstevel@tonic-gate int recalc, i, j; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * The following is equivalent to 101*7c478bd9Sstevel@tonic-gate * 102*7c478bd9Sstevel@tonic-gate * a = creall(*z); b = cimagl(*z); 103*7c478bd9Sstevel@tonic-gate * c = creall(*w); d = cimagl(*w); 104*7c478bd9Sstevel@tonic-gate */ 105*7c478bd9Sstevel@tonic-gate a = ((long double *)z)[0]; 106*7c478bd9Sstevel@tonic-gate b = ((long double *)z)[1]; 107*7c478bd9Sstevel@tonic-gate c = ((long double *)w)[0]; 108*7c478bd9Sstevel@tonic-gate d = ((long double *)w)[1]; 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate x = a * c - b * d; 111*7c478bd9Sstevel@tonic-gate y = a * d + b * c; 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate if (x != x && y != y) { 114*7c478bd9Sstevel@tonic-gate /* 115*7c478bd9Sstevel@tonic-gate * Both x and y are NaN, so z and w can't both be finite. 116*7c478bd9Sstevel@tonic-gate * If at least one of z or w is a complex NaN, and neither 117*7c478bd9Sstevel@tonic-gate * is infinite, then we might as well deliver NaN + I * NaN. 118*7c478bd9Sstevel@tonic-gate * So the only cases to check are when one of z or w is 119*7c478bd9Sstevel@tonic-gate * infinite. 120*7c478bd9Sstevel@tonic-gate */ 121*7c478bd9Sstevel@tonic-gate recalc = 0; 122*7c478bd9Sstevel@tonic-gate i = testinfl(a); 123*7c478bd9Sstevel@tonic-gate j = testinfl(b); 124*7c478bd9Sstevel@tonic-gate if (i | j) { /* z is infinite */ 125*7c478bd9Sstevel@tonic-gate /* "factor out" infinity */ 126*7c478bd9Sstevel@tonic-gate a = i; 127*7c478bd9Sstevel@tonic-gate b = j; 128*7c478bd9Sstevel@tonic-gate recalc = 1; 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate i = testinfl(c); 131*7c478bd9Sstevel@tonic-gate j = testinfl(d); 132*7c478bd9Sstevel@tonic-gate if (i | j) { /* w is infinite */ 133*7c478bd9Sstevel@tonic-gate /* "factor out" infinity */ 134*7c478bd9Sstevel@tonic-gate c = i; 135*7c478bd9Sstevel@tonic-gate d = j; 136*7c478bd9Sstevel@tonic-gate recalc = 1; 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate if (recalc) { 139*7c478bd9Sstevel@tonic-gate x = inf.q * (a * c - b * d); 140*7c478bd9Sstevel@tonic-gate y = inf.q * (a * d + b * c); 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate #ifdef __sparcv9 145*7c478bd9Sstevel@tonic-gate ((long double *)&v)[0] = x; 146*7c478bd9Sstevel@tonic-gate ((long double *)&v)[1] = y; 147*7c478bd9Sstevel@tonic-gate return (v); 148*7c478bd9Sstevel@tonic-gate #else 149*7c478bd9Sstevel@tonic-gate ((long double *)v)[0] = x; 150*7c478bd9Sstevel@tonic-gate ((long double *)v)[1] = y; 151*7c478bd9Sstevel@tonic-gate #endif 152*7c478bd9Sstevel@tonic-gate } 153