xref: /illumos-gate/usr/src/lib/libc/i386/fp/_D_cplx_mul.c (revision c764c31d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * _D_cplx_mul(z, w) returns z * w with infinities handled according
297c478bd9Sstevel@tonic-gate  * to C99.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * If z and w are both finite, _D_cplx_mul(z, w) delivers the complex
327c478bd9Sstevel@tonic-gate  * product according to the usual formula: let a = Re(z), b = Im(z),
337c478bd9Sstevel@tonic-gate  * c = Re(w), and d = Im(w); then _D_cplx_mul(z, w) delivers x + I * y
347c478bd9Sstevel@tonic-gate  * where x = a * c - b * d and y = a * d + b * c.  This implementation
357c478bd9Sstevel@tonic-gate  * uses extended precision to form these expressions, so none of the
367c478bd9Sstevel@tonic-gate  * intermediate products can overflow.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * If one of z or w is infinite and the other is either finite nonzero
397c478bd9Sstevel@tonic-gate  * or infinite, _D_cplx_mul delivers an infinite result.  If one factor
407c478bd9Sstevel@tonic-gate  * is infinite and the other is zero, _D_cplx_mul delivers NaN + I * NaN.
417c478bd9Sstevel@tonic-gate  * C99 doesn't specify the latter case.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * C99 also doesn't specify what should happen if either z or w is a
447c478bd9Sstevel@tonic-gate  * complex NaN (i.e., neither finite nor infinite).  This implementation
457c478bd9Sstevel@tonic-gate  * delivers NaN + I * NaN in this case.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * This implementation can raise spurious invalid operation and inexact
487c478bd9Sstevel@tonic-gate  * exceptions.  C99 allows this.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #if !defined(i386) && !defined(__i386) && !defined(__amd64)
527c478bd9Sstevel@tonic-gate #error This code is for x86 only
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static union {
567c478bd9Sstevel@tonic-gate 	int	i;
577c478bd9Sstevel@tonic-gate 	float	f;
587c478bd9Sstevel@tonic-gate } inf = {
597c478bd9Sstevel@tonic-gate 	0x7f800000
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static int
testinf(double x)667c478bd9Sstevel@tonic-gate testinf(double x)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	union {
697c478bd9Sstevel@tonic-gate 		int	i[2];
707c478bd9Sstevel@tonic-gate 		double	d;
717c478bd9Sstevel@tonic-gate 	} xx;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	xx.d = x;
747c478bd9Sstevel@tonic-gate 	return (((((xx.i[1] << 1) - 0xffe00000) | xx.i[0]) == 0)?
75*c764c31dSToomas Soome 	    (1 | (xx.i[1] >> 31)) : 0);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate double _Complex
_D_cplx_mul(double _Complex z,double _Complex w)797c478bd9Sstevel@tonic-gate _D_cplx_mul(double _Complex z, double _Complex w)
807c478bd9Sstevel@tonic-gate {
81*c764c31dSToomas Soome 	double _Complex	v = 0;
827c478bd9Sstevel@tonic-gate 	double		a, b, c, d;
837c478bd9Sstevel@tonic-gate 	long double	x, y;
847c478bd9Sstevel@tonic-gate 	int		recalc, i, j;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * The following is equivalent to
887c478bd9Sstevel@tonic-gate 	 *
897c478bd9Sstevel@tonic-gate 	 *  a = creal(z); b = cimag(z);
907c478bd9Sstevel@tonic-gate 	 *  c = creal(w); d = cimag(w);
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
937c478bd9Sstevel@tonic-gate 	a = ((double *)&z)[0];
947c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
957c478bd9Sstevel@tonic-gate 	b = ((double *)&z)[1];
967c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
977c478bd9Sstevel@tonic-gate 	c = ((double *)&w)[0];
987c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
997c478bd9Sstevel@tonic-gate 	d = ((double *)&w)[1];
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	x = (long double)a * c - (long double)b * d;
1027c478bd9Sstevel@tonic-gate 	y = (long double)a * d + (long double)b * c;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (x != x && y != y) {
1057c478bd9Sstevel@tonic-gate 		/*
1067c478bd9Sstevel@tonic-gate 		 * Both x and y are NaN, so z and w can't both be finite.
1077c478bd9Sstevel@tonic-gate 		 * If at least one of z or w is a complex NaN, and neither
1087c478bd9Sstevel@tonic-gate 		 * is infinite, then we might as well deliver NaN + I * NaN.
1097c478bd9Sstevel@tonic-gate 		 * So the only cases to check are when one of z or w is
1107c478bd9Sstevel@tonic-gate 		 * infinite.
1117c478bd9Sstevel@tonic-gate 		 */
1127c478bd9Sstevel@tonic-gate 		recalc = 0;
1137c478bd9Sstevel@tonic-gate 		i = testinf(a);
1147c478bd9Sstevel@tonic-gate 		j = testinf(b);
1157c478bd9Sstevel@tonic-gate 		if (i | j) { /* z is infinite */
1167c478bd9Sstevel@tonic-gate 			/* "factor out" infinity */
1177c478bd9Sstevel@tonic-gate 			a = i;
1187c478bd9Sstevel@tonic-gate 			b = j;
1197c478bd9Sstevel@tonic-gate 			recalc = 1;
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		i = testinf(c);
1227c478bd9Sstevel@tonic-gate 		j = testinf(d);
1237c478bd9Sstevel@tonic-gate 		if (i | j) { /* w is infinite */
1247c478bd9Sstevel@tonic-gate 			/* "factor out" infinity */
1257c478bd9Sstevel@tonic-gate 			c = i;
1267c478bd9Sstevel@tonic-gate 			d = j;
1277c478bd9Sstevel@tonic-gate 			recalc = 1;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		if (recalc) {
1307c478bd9Sstevel@tonic-gate 			x = inf.f * ((long double)a * c - (long double)b * d);
1317c478bd9Sstevel@tonic-gate 			y = inf.f * ((long double)a * d + (long double)b * c);
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * The following is equivalent to
1377c478bd9Sstevel@tonic-gate 	 *
1387c478bd9Sstevel@tonic-gate 	 *  return x + I * y;
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
1417c478bd9Sstevel@tonic-gate 	((double *)&v)[0] = (double)x;
1427c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
1437c478bd9Sstevel@tonic-gate 	((double *)&v)[1] = (double)y;
1447c478bd9Sstevel@tonic-gate 	return (v);
1457c478bd9Sstevel@tonic-gate }
146