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 * _D_cplx_mul(z, w) returns z * w with infinities handled according
29 * to C99.
30 *
31 * If z and w are both finite, _D_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 _D_cplx_mul(z, w) delivers x + I * y
34 * where x = a * c - b * d and y = a * d + b * c. This implementation
35 * uses extended precision to form these expressions, so none of the
36 * intermediate products can overflow.
37 *
38 * If one of z or w is infinite and the other is either finite nonzero
39 * or infinite, _D_cplx_mul delivers an infinite result. If one factor
40 * is infinite and the other is zero, _D_cplx_mul delivers NaN + I * NaN.
41 * C99 doesn't specify the latter case.
42 *
43 * C99 also doesn't specify what should happen if either z or w is a
44 * complex NaN (i.e., neither finite nor infinite). This implementation
45 * delivers NaN + I * NaN in this case.
46 *
47 * This implementation can raise spurious invalid operation and inexact
48 * exceptions. C99 allows this.
49 */
50
51 #if !defined(i386) && !defined(__i386) && !defined(__amd64)
52 #error This code is for x86 only
53 #endif
54
55 static union {
56 int i;
57 float f;
58 } inf = {
59 0x7f800000
60 };
61
62 /*
63 * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
64 */
65 static int
testinf(double x)66 testinf(double x)
67 {
68 union {
69 int i[2];
70 double d;
71 } xx;
72
73 xx.d = x;
74 return (((((xx.i[1] << 1) - 0xffe00000) | xx.i[0]) == 0)?
75 (1 | (xx.i[1] >> 31)) : 0);
76 }
77
78 double _Complex
_D_cplx_mul(double _Complex z,double _Complex w)79 _D_cplx_mul(double _Complex z, double _Complex w)
80 {
81 double _Complex v = 0;
82 double a, b, c, d;
83 long double x, y;
84 int recalc, i, j;
85
86 /*
87 * The following is equivalent to
88 *
89 * a = creal(z); b = cimag(z);
90 * c = creal(w); d = cimag(w);
91 */
92 /* LINTED alignment */
93 a = ((double *)&z)[0];
94 /* LINTED alignment */
95 b = ((double *)&z)[1];
96 /* LINTED alignment */
97 c = ((double *)&w)[0];
98 /* LINTED alignment */
99 d = ((double *)&w)[1];
100
101 x = (long double)a * c - (long double)b * d;
102 y = (long double)a * d + (long double)b * c;
103
104 if (x != x && y != y) {
105 /*
106 * Both x and y are NaN, so z and w can't both be finite.
107 * If at least one of z or w is a complex NaN, and neither
108 * is infinite, then we might as well deliver NaN + I * NaN.
109 * So the only cases to check are when one of z or w is
110 * infinite.
111 */
112 recalc = 0;
113 i = testinf(a);
114 j = testinf(b);
115 if (i | j) { /* z is infinite */
116 /* "factor out" infinity */
117 a = i;
118 b = j;
119 recalc = 1;
120 }
121 i = testinf(c);
122 j = testinf(d);
123 if (i | j) { /* w is infinite */
124 /* "factor out" infinity */
125 c = i;
126 d = j;
127 recalc = 1;
128 }
129 if (recalc) {
130 x = inf.f * ((long double)a * c - (long double)b * d);
131 y = inf.f * ((long double)a * d + (long double)b * c);
132 }
133 }
134
135 /*
136 * The following is equivalent to
137 *
138 * return x + I * y;
139 */
140 /* LINTED alignment */
141 ((double *)&v)[0] = (double)x;
142 /* LINTED alignment */
143 ((double *)&v)[1] = (double)y;
144 return (v);
145 }
146