xref: /onnv-gate/usr/src/lib/libc/i386/fp/_X_cplx_mul.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * _X_cplx_mul(z, w) returns z * w with infinities handled according
31*0Sstevel@tonic-gate  * to C99.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * If z and w are both finite, _X_cplx_mul(z, w) delivers the complex
34*0Sstevel@tonic-gate  * product according to the usual formula: let a = Re(z), b = Im(z),
35*0Sstevel@tonic-gate  * c = Re(w), and d = Im(w); then _X_cplx_mul(z, w) delivers x + I * y
36*0Sstevel@tonic-gate  * where x = a * c - b * d and y = a * d + b * c.  Note that if both
37*0Sstevel@tonic-gate  * ac and bd overflow, then at least one of ad or bc must also over-
38*0Sstevel@tonic-gate  * flow, and vice versa, so that if one component of the product is
39*0Sstevel@tonic-gate  * NaN, the other is infinite.  (Such a value is considered infinite
40*0Sstevel@tonic-gate  * according to C99.)
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * If one of z or w is infinite and the other is either finite nonzero
43*0Sstevel@tonic-gate  * or infinite, _X_cplx_mul delivers an infinite result.  If one factor
44*0Sstevel@tonic-gate  * is infinite and the other is zero, _X_cplx_mul delivers NaN + I * NaN.
45*0Sstevel@tonic-gate  * C99 doesn't specify the latter case.
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * C99 also doesn't specify what should happen if either z or w is a
48*0Sstevel@tonic-gate  * complex NaN (i.e., neither finite nor infinite).  This implementation
49*0Sstevel@tonic-gate  * delivers NaN + I * NaN in this case.
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  * This implementation can raise spurious underflow, overflow, invalid
52*0Sstevel@tonic-gate  * operation, and inexact exceptions.  C99 allows this.
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #if !defined(i386) && !defined(__i386) && !defined(__amd64)
56*0Sstevel@tonic-gate #error This code is for x86 only
57*0Sstevel@tonic-gate #endif
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static union {
60*0Sstevel@tonic-gate 	int	i;
61*0Sstevel@tonic-gate 	float	f;
62*0Sstevel@tonic-gate } inf = {
63*0Sstevel@tonic-gate 	0x7f800000
64*0Sstevel@tonic-gate };
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate  * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
68*0Sstevel@tonic-gate  */
69*0Sstevel@tonic-gate static int
testinfl(long double x)70*0Sstevel@tonic-gate testinfl(long double x)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	union {
73*0Sstevel@tonic-gate 		int		i[3];
74*0Sstevel@tonic-gate 		long double	e;
75*0Sstevel@tonic-gate 	} xx;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	xx.e = x;
78*0Sstevel@tonic-gate 	if ((xx.i[2] & 0x7fff) != 0x7fff || ((xx.i[1] << 1) | xx.i[0]) != 0)
79*0Sstevel@tonic-gate 		return (0);
80*0Sstevel@tonic-gate 	return (1 | ((xx.i[2] << 16) >> 31));
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate long double _Complex
_X_cplx_mul(long double _Complex z,long double _Complex w)84*0Sstevel@tonic-gate _X_cplx_mul(long double _Complex z, long double _Complex w)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	long double _Complex	v;
87*0Sstevel@tonic-gate 	long double		a, b, c, d, x, y;
88*0Sstevel@tonic-gate 	int			recalc, i, j;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	/*
91*0Sstevel@tonic-gate 	 * The following is equivalent to
92*0Sstevel@tonic-gate 	 *
93*0Sstevel@tonic-gate 	 *  a = creall(z); b = cimagl(z);
94*0Sstevel@tonic-gate 	 *  c = creall(w); d = cimagl(w);
95*0Sstevel@tonic-gate 	 */
96*0Sstevel@tonic-gate 	a = ((long double *)&z)[0];
97*0Sstevel@tonic-gate 	b = ((long double *)&z)[1];
98*0Sstevel@tonic-gate 	c = ((long double *)&w)[0];
99*0Sstevel@tonic-gate 	d = ((long double *)&w)[1];
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	x = a * c - b * d;
102*0Sstevel@tonic-gate 	y = a * d + b * c;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	if (x != x && y != y) {
105*0Sstevel@tonic-gate 		/*
106*0Sstevel@tonic-gate 		 * Both x and y are NaN, so z and w can't both be finite.
107*0Sstevel@tonic-gate 		 * If at least one of z or w is a complex NaN, and neither
108*0Sstevel@tonic-gate 		 * is infinite, then we might as well deliver NaN + I * NaN.
109*0Sstevel@tonic-gate 		 * So the only cases to check are when one of z or w is
110*0Sstevel@tonic-gate 		 * infinite.
111*0Sstevel@tonic-gate 		 */
112*0Sstevel@tonic-gate 		recalc = 0;
113*0Sstevel@tonic-gate 		i = testinfl(a);
114*0Sstevel@tonic-gate 		j = testinfl(b);
115*0Sstevel@tonic-gate 		if (i | j) { /* z is infinite */
116*0Sstevel@tonic-gate 			/* "factor out" infinity */
117*0Sstevel@tonic-gate 			a = i;
118*0Sstevel@tonic-gate 			b = j;
119*0Sstevel@tonic-gate 			recalc = 1;
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 		i = testinfl(c);
122*0Sstevel@tonic-gate 		j = testinfl(d);
123*0Sstevel@tonic-gate 		if (i | j) { /* w is infinite */
124*0Sstevel@tonic-gate 			/* "factor out" infinity */
125*0Sstevel@tonic-gate 			c = i;
126*0Sstevel@tonic-gate 			d = j;
127*0Sstevel@tonic-gate 			recalc = 1;
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 		if (recalc) {
130*0Sstevel@tonic-gate 			x = inf.f * (a * c - b * d);
131*0Sstevel@tonic-gate 			y = inf.f * (a * d + b * c);
132*0Sstevel@tonic-gate 		}
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	/*
136*0Sstevel@tonic-gate 	 * The following is equivalent to
137*0Sstevel@tonic-gate 	 *
138*0Sstevel@tonic-gate 	 *  return x + I * y;
139*0Sstevel@tonic-gate 	 */
140*0Sstevel@tonic-gate 	((long double *)&v)[0] = x;
141*0Sstevel@tonic-gate 	((long double *)&v)[1] = y;
142*0Sstevel@tonic-gate 	return (v);
143*0Sstevel@tonic-gate }
144