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