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