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 /* 28 * _F_cplx_div(z, w) returns z / w with infinities handled according 29 * to C99. 30 * 31 * If z and w are both finite and w is nonzero, _F_cplx_div(z, w) 32 * delivers the complex quotient q according to the usual formula: 33 * let a = Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x + 34 * I * y where x = (a * c + b * d) / r and y = (b * c - a * d) / r 35 * with r = c * c + d * d. This implementation computes intermediate 36 * results in double precision to avoid premature underflow or over- 37 * flow. 38 * 39 * If z is neither NaN nor zero and w is zero, or if z is infinite 40 * and w is finite and nonzero, _F_cplx_div delivers an infinite 41 * result. If z is finite and w is infinite, _F_cplx_div delivers 42 * a zero result. 43 * 44 * If z and w are both zero or both infinite, or if either z or w is 45 * a complex NaN, _F_cplx_div delivers NaN + I * NaN. C99 doesn't 46 * specify these cases. 47 * 48 * This implementation can raise spurious invalid operation, inexact, 49 * and division-by-zero exceptions. C99 allows this. 50 * 51 * Warning: Do not attempt to "optimize" this code by removing multi- 52 * plications by zero. 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 testinff(float x) 71 { 72 union { 73 int i; 74 float f; 75 } xx; 76 77 xx.f = x; 78 return ((((xx.i << 1) - 0xff000000) == 0)? (1 | (xx.i >> 31)) : 0); 79 } 80 81 float _Complex 82 _F_cplx_div(float _Complex z, float _Complex w) 83 { 84 float _Complex v; 85 union { 86 int i; 87 float f; 88 } cc, dd; 89 float a, b, c, d; 90 double r, x, y; 91 int i, j, recalc; 92 93 /* 94 * The following is equivalent to 95 * 96 * a = crealf(z); b = cimagf(z); 97 * c = crealf(w); d = cimagf(w); 98 */ 99 a = ((float *)&z)[0]; 100 b = ((float *)&z)[1]; 101 c = ((float *)&w)[0]; 102 d = ((float *)&w)[1]; 103 104 r = (double)c * c + (double)d * d; 105 106 if (r == 0.0) { 107 /* w is zero; multiply z by 1/Re(w) - I * Im(w) */ 108 c = 1.0f / c; 109 i = testinff(a); 110 j = testinff(b); 111 if (i | j) { /* z is infinite */ 112 a = i; 113 b = j; 114 } 115 ((float *)&v)[0] = a * c + b * d; 116 ((float *)&v)[1] = b * c - a * d; 117 return (v); 118 } 119 120 r = 1.0 / r; 121 x = ((double)a * c + (double)b * d) * r; 122 y = ((double)b * c - (double)a * d) * r; 123 124 if (x != x && y != y) { 125 /* 126 * Both x and y are NaN, so z and w can't both be finite 127 * and nonzero. Since we handled the case w = 0 above, 128 * the only cases to check here are when one of z or w 129 * is infinite. 130 */ 131 r = 1.0; 132 recalc = 0; 133 i = testinff(a); 134 j = testinff(b); 135 if (i | j) { /* z is infinite */ 136 /* "factor out" infinity */ 137 a = i; 138 b = j; 139 r = inf.d; 140 recalc = 1; 141 } 142 i = testinff(c); 143 j = testinff(d); 144 if (i | j) { /* w is infinite */ 145 /* 146 * "factor out" infinity, being careful to preserve 147 * signs of finite values 148 */ 149 cc.f = c; 150 dd.f = d; 151 c = i? i : ((cc.i < 0)? -0.0f : 0.0f); 152 d = j? j : ((dd.i < 0)? -0.0f : 0.0f); 153 r *= 0.0; 154 recalc = 1; 155 } 156 if (recalc) { 157 x = ((double)a * c + (double)b * d) * r; 158 y = ((double)b * c - (double)a * d) * r; 159 } 160 } 161 162 /* 163 * The following is equivalent to 164 * 165 * return x + I * y; 166 */ 167 ((float *)&v)[0] = (float)x; 168 ((float *)&v)[1] = (float)y; 169 return (v); 170 } 171