xref: /onnv-gate/usr/src/lib/libc/i386/fp/_X_cplx_div.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_div(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 and w is nonzero, _X_cplx_div delivers
34*0Sstevel@tonic-gate  * the complex quotient q according to the usual formula: let a =
35*0Sstevel@tonic-gate  * Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x + I * y
36*0Sstevel@tonic-gate  * where x = (a * c + b * d) / r and y = (b * c - a * d) / r with
37*0Sstevel@tonic-gate  * r = c * c + d * d.  This implementation scales to avoid premature
38*0Sstevel@tonic-gate  * underflow or overflow.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * If z is neither NaN nor zero and w is zero, or if z is infinite
41*0Sstevel@tonic-gate  * and w is finite and nonzero, _X_cplx_div delivers an infinite
42*0Sstevel@tonic-gate  * result.  If z is finite and w is infinite, _X_cplx_div delivers
43*0Sstevel@tonic-gate  * a zero result.
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  * If z and w are both zero or both infinite, or if either z or w is
46*0Sstevel@tonic-gate  * a complex NaN, _X_cplx_div delivers NaN + I * NaN.  C99 doesn't
47*0Sstevel@tonic-gate  * specify these cases.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * This implementation can raise spurious underflow, overflow, in-
50*0Sstevel@tonic-gate  * valid operation, inexact, and division-by-zero exceptions.  C99
51*0Sstevel@tonic-gate  * allows this.
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate #if !defined(i386) && !defined(__i386) && !defined(__amd64)
55*0Sstevel@tonic-gate #error This code is for x86 only
56*0Sstevel@tonic-gate #endif
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static union {
59*0Sstevel@tonic-gate 	int	i;
60*0Sstevel@tonic-gate 	float	f;
61*0Sstevel@tonic-gate } inf = {
62*0Sstevel@tonic-gate 	0x7f800000
63*0Sstevel@tonic-gate };
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate static int
testinfl(long double x)69*0Sstevel@tonic-gate testinfl(long double x)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	union {
72*0Sstevel@tonic-gate 		int		i[3];
73*0Sstevel@tonic-gate 		long double	e;
74*0Sstevel@tonic-gate 	} xx;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	xx.e = x;
77*0Sstevel@tonic-gate 	if ((xx.i[2] & 0x7fff) != 0x7fff || ((xx.i[1] << 1) | xx.i[0]) != 0)
78*0Sstevel@tonic-gate 		return (0);
79*0Sstevel@tonic-gate 	return (1 | ((xx.i[2] << 16) >> 31));
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate long double _Complex
_X_cplx_div(long double _Complex z,long double _Complex w)83*0Sstevel@tonic-gate _X_cplx_div(long double _Complex z, long double _Complex w)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	long double _Complex	v;
86*0Sstevel@tonic-gate 	union {
87*0Sstevel@tonic-gate 		int		i[3];
88*0Sstevel@tonic-gate 		long double	e;
89*0Sstevel@tonic-gate 	} aa, bb, cc, dd, ss;
90*0Sstevel@tonic-gate 	long double	a, b, c, d, r;
91*0Sstevel@tonic-gate 	int		ea, eb, ec, ed, ez, ew, es, i, j;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * The following is equivalent to
95*0Sstevel@tonic-gate 	 *
96*0Sstevel@tonic-gate 	 *  a = creall(*z); b = cimagl(*z);
97*0Sstevel@tonic-gate 	 *  c = creall(*w); d = cimagl(*w);
98*0Sstevel@tonic-gate 	 */
99*0Sstevel@tonic-gate 	a = ((long double *)&z)[0];
100*0Sstevel@tonic-gate 	b = ((long double *)&z)[1];
101*0Sstevel@tonic-gate 	c = ((long double *)&w)[0];
102*0Sstevel@tonic-gate 	d = ((long double *)&w)[1];
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/* extract exponents to estimate |z| and |w| */
105*0Sstevel@tonic-gate 	aa.e = a;
106*0Sstevel@tonic-gate 	bb.e = b;
107*0Sstevel@tonic-gate 	ea = aa.i[2] & 0x7fff;
108*0Sstevel@tonic-gate 	eb = bb.i[2] & 0x7fff;
109*0Sstevel@tonic-gate 	ez = (ea > eb)? ea : eb;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	cc.e = c;
112*0Sstevel@tonic-gate 	dd.e = d;
113*0Sstevel@tonic-gate 	ec = cc.i[2] & 0x7fff;
114*0Sstevel@tonic-gate 	ed = dd.i[2] & 0x7fff;
115*0Sstevel@tonic-gate 	ew = (ec > ed)? ec : ed;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* check for special cases */
118*0Sstevel@tonic-gate 	if (ew >= 0x7fff) { /* w is inf or nan */
119*0Sstevel@tonic-gate 		r = 0.0f;
120*0Sstevel@tonic-gate 		i = testinfl(c);
121*0Sstevel@tonic-gate 		j = testinfl(d);
122*0Sstevel@tonic-gate 		if (i | j) { /* w is infinite */
123*0Sstevel@tonic-gate 			/*
124*0Sstevel@tonic-gate 			 * "factor out" infinity, being careful to preserve
125*0Sstevel@tonic-gate 			 * signs of finite values
126*0Sstevel@tonic-gate 			 */
127*0Sstevel@tonic-gate 			c = i? i : (((cc.i[2] << 16) < 0)? -0.0f : 0.0f);
128*0Sstevel@tonic-gate 			d = j? j : (((dd.i[2] << 16) < 0)? -0.0f : 0.0f);
129*0Sstevel@tonic-gate 			if (ez >= 0x7ffe) {
130*0Sstevel@tonic-gate 				/* scale to avoid overflow below */
131*0Sstevel@tonic-gate 				c *= 0.5f;
132*0Sstevel@tonic-gate 				d *= 0.5f;
133*0Sstevel@tonic-gate 			}
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 		((long double *)&v)[0] = (a * c + b * d) * r;
136*0Sstevel@tonic-gate 		((long double *)&v)[1] = (b * c - a * d) * r;
137*0Sstevel@tonic-gate 		return (v);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (ew == 0 && (cc.i[1] | cc.i[0] | dd.i[1] | dd.i[0]) == 0) {
141*0Sstevel@tonic-gate 		/* w is zero; multiply z by 1/Re(w) - I * Im(w) */
142*0Sstevel@tonic-gate 		c = 1.0f / c;
143*0Sstevel@tonic-gate 		i = testinfl(a);
144*0Sstevel@tonic-gate 		j = testinfl(b);
145*0Sstevel@tonic-gate 		if (i | j) { /* z is infinite */
146*0Sstevel@tonic-gate 			a = i;
147*0Sstevel@tonic-gate 			b = j;
148*0Sstevel@tonic-gate 		}
149*0Sstevel@tonic-gate 		((long double *)&v)[0] = a * c + b * d;
150*0Sstevel@tonic-gate 		((long double *)&v)[1] = b * c - a * d;
151*0Sstevel@tonic-gate 		return (v);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if (ez >= 0x7fff) { /* z is inf or nan */
155*0Sstevel@tonic-gate 		i = testinfl(a);
156*0Sstevel@tonic-gate 		j = testinfl(b);
157*0Sstevel@tonic-gate 		if (i | j) { /* z is infinite */
158*0Sstevel@tonic-gate 			a = i;
159*0Sstevel@tonic-gate 			b = j;
160*0Sstevel@tonic-gate 			r = inf.f;
161*0Sstevel@tonic-gate 		}
162*0Sstevel@tonic-gate 		((long double *)&v)[0] = a * c + b * d;
163*0Sstevel@tonic-gate 		((long double *)&v)[1] = b * c - a * d;
164*0Sstevel@tonic-gate 		return (v);
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * Scale c and d to compute 1/|w|^2 and the real and imaginary
169*0Sstevel@tonic-gate 	 * parts of the quotient.
170*0Sstevel@tonic-gate 	 */
171*0Sstevel@tonic-gate 	es = ((ew >> 2) - ew) + 0x6ffd;
172*0Sstevel@tonic-gate 	if (ez < 0x0086) { /* |z| < 2^-16249 */
173*0Sstevel@tonic-gate 		if (((ew - 0x3efe) | (0x4083 - ew)) >= 0)
174*0Sstevel@tonic-gate 			es = ((0x4083 - ew) >> 1) + 0x3fff;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 	ss.i[2] = es;
177*0Sstevel@tonic-gate 	ss.i[1] = 0x80000000;
178*0Sstevel@tonic-gate 	ss.i[0] = 0;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	c *= ss.e;
181*0Sstevel@tonic-gate 	d *= ss.e;
182*0Sstevel@tonic-gate 	r = 1.0f / (c * c + d * d);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	c *= ss.e;
185*0Sstevel@tonic-gate 	d *= ss.e;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	((long double *)&v)[0] = (a * c + b * d) * r;
188*0Sstevel@tonic-gate 	((long double *)&v)[1] = (b * c - a * d) * r;
189*0Sstevel@tonic-gate 	return (v);
190*0Sstevel@tonic-gate }
191