1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * _D_cplx_mul(z, w) returns z * w with infinities handled according 31 * to C99. 32 * 33 * If z and w are both finite, _D_cplx_mul(z, w) delivers the complex 34 * product according to the usual formula: let a = Re(z), b = Im(z), 35 * c = Re(w), and d = Im(w); then _D_cplx_mul(z, w) delivers x + I * y 36 * where x = a * c - b * d and y = a * d + b * c. Note that if both 37 * ac and bd overflow, then at least one of ad or bc must also over- 38 * flow, and vice versa, so that if one component of the product is 39 * NaN, the other is infinite. (Such a value is considered infinite 40 * according to C99.) 41 * 42 * If one of z or w is infinite and the other is either finite nonzero 43 * or infinite, _D_cplx_mul delivers an infinite result. If one factor 44 * is infinite and the other is zero, _D_cplx_mul delivers NaN + I * NaN. 45 * C99 doesn't specify the latter case. 46 * 47 * C99 also doesn't specify what should happen if either z or w is a 48 * complex NaN (i.e., neither finite nor infinite). This implementation 49 * delivers NaN + I * NaN in this case. 50 * 51 * This implementation can raise spurious underflow, overflow, invalid 52 * operation, and inexact exceptions. C99 allows this. 53 */ 54 55 #if !defined(sparc) && !defined(__sparc) 56 #error This code is for SPARC only 57 #endif 58 59 static union { 60 int i[2]; 61 double d; 62 } inf = { 63 0x7ff00000, 0 64 }; 65 66 /* 67 * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise 68 */ 69 static int 70 testinf(double x) 71 { 72 union { 73 int i[2]; 74 double d; 75 } xx; 76 77 xx.d = x; 78 return (((((xx.i[0] << 1) - 0xffe00000) | xx.i[1]) == 0)? 79 (1 | (xx.i[0] >> 31)) : 0); 80 } 81 82 double _Complex 83 _D_cplx_mul(double _Complex z, double _Complex w) 84 { 85 double _Complex v; 86 double a, b, c, d, x, y; 87 int recalc, i, j; 88 89 /* 90 * The following is equivalent to 91 * 92 * a = creal(z); b = cimag(z); 93 * c = creal(w); d = cimag(w); 94 */ 95 a = ((double *)&z)[0]; 96 b = ((double *)&z)[1]; 97 c = ((double *)&w)[0]; 98 d = ((double *)&w)[1]; 99 100 x = a * c - b * d; 101 y = a * d + b * c; 102 103 if (x != x && y != y) { 104 /* 105 * Both x and y are NaN, so z and w can't both be finite. 106 * If at least one of z or w is a complex NaN, and neither 107 * is infinite, then we might as well deliver NaN + I * NaN. 108 * So the only cases to check are when one of z or w is 109 * infinite. 110 */ 111 recalc = 0; 112 i = testinf(a); 113 j = testinf(b); 114 if (i | j) { /* z is infinite */ 115 /* "factor out" infinity */ 116 a = i; 117 b = j; 118 recalc = 1; 119 } 120 i = testinf(c); 121 j = testinf(d); 122 if (i | j) { /* w is infinite */ 123 /* "factor out" infinity */ 124 c = i; 125 d = j; 126 recalc = 1; 127 } 128 if (recalc) { 129 x = inf.d * (a * c - b * d); 130 y = inf.d * (a * d + b * c); 131 } 132 } 133 134 /* 135 * The following is equivalent to 136 * 137 * return x + I * y; 138 */ 139 ((double *)&v)[0] = x; 140 ((double *)&v)[1] = y; 141 return (v); 142 } 143