xref: /illumos-gate/usr/src/lib/libc/i386/fp/_F_cplx_mul.c (revision c764c31d)
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  * _F_cplx_mul(z, w) returns z * w with infinities handled according
29  * to C99.
30  *
31  * If z and w are both finite, _F_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 _F_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, _F_cplx_mul delivers an infinite result.  If one factor
40  * is infinite and the other is zero, _F_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
testinff(float x)66 testinff(float x)
67 {
68 	union {
69 		int	i;
70 		float	f;
71 	} xx;
72 
73 	xx.f = x;
74 	return ((((xx.i << 1) - 0xff000000) == 0)? (1 | (xx.i >> 31)) : 0);
75 }
76 
77 float _Complex
_F_cplx_mul(float _Complex z,float _Complex w)78 _F_cplx_mul(float _Complex z, float _Complex w)
79 {
80 	float _Complex	v = 0;
81 	float		a, b, c, d;
82 	long double	x, y;
83 	int		recalc, i, j;
84 
85 	/*
86 	 * The following is equivalent to
87 	 *
88 	 *  a = crealf(z); b = cimagf(z);
89 	 *  c = crealf(w); d = cimagf(w);
90 	 */
91 	a = ((float *)&z)[0];
92 	b = ((float *)&z)[1];
93 	c = ((float *)&w)[0];
94 	d = ((float *)&w)[1];
95 
96 	x = (long double)a * c - (long double)b * d;
97 	y = (long double)a * d + (long double)b * c;
98 
99 	if (x != x && y != y) {
100 		/*
101 		 * Both x and y are NaN, so z and w can't both be finite.
102 		 * If at least one of z or w is a complex NaN, and neither
103 		 * is infinite, then we might as well deliver NaN + I * NaN.
104 		 * So the only cases to check are when one of z or w is
105 		 * infinite.
106 		 */
107 		recalc = 0;
108 		i = testinff(a);
109 		j = testinff(b);
110 		if (i | j) { /* z is infinite */
111 			/* "factor out" infinity */
112 			a = i;
113 			b = j;
114 			recalc = 1;
115 		}
116 		i = testinff(c);
117 		j = testinff(d);
118 		if (i | j) { /* w is infinite */
119 			/* "factor out" infinity */
120 			c = i;
121 			d = j;
122 			recalc = 1;
123 		}
124 		if (recalc) {
125 			x = inf.f * ((long double)a * c - (long double)b * d);
126 			y = inf.f * ((long double)a * d + (long double)b * c);
127 		}
128 	}
129 
130 	/*
131 	 * The following is equivalent to
132 	 *
133 	 *  return x + I * y;
134 	 */
135 	((float *)&v)[0] = (float)x;
136 	((float *)&v)[1] = (float)y;
137 	return (v);
138 }
139