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