xref: /onnv-gate/usr/src/lib/libc/sparc/fp/_Q_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 2003 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  * On SPARC V8, _Q_cplx_div(v, z, w) sets *v = *z / *w with infin-
31*0Sstevel@tonic-gate  * ities handling according to C99.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * On SPARC V9, _Q_cplx_div(z, w) returns *z / *w with infinities
34*0Sstevel@tonic-gate  * handled according to C99.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * If z and w are both finite and w is nonzero, _Q_cplx_div delivers
37*0Sstevel@tonic-gate  * the complex quotient q according to the usual formula: let a =
38*0Sstevel@tonic-gate  * Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x + I * y
39*0Sstevel@tonic-gate  * where x = (a * c + b * d) / r and y = (b * c - a * d) / r with
40*0Sstevel@tonic-gate  * r = c * c + d * d.  This implementation scales to avoid premature
41*0Sstevel@tonic-gate  * underflow or overflow.
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * If z is neither NaN nor zero and w is zero, or if z is infinite
44*0Sstevel@tonic-gate  * and w is finite and nonzero, _Q_cplx_div delivers an infinite
45*0Sstevel@tonic-gate  * result.  If z is finite and w is infinite, _Q_cplx_div delivers
46*0Sstevel@tonic-gate  * a zero result.
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  * If z and w are both zero or both infinite, or if either z or w is
49*0Sstevel@tonic-gate  * a complex NaN, _Q_cplx_div delivers NaN + I * NaN.  C99 doesn't
50*0Sstevel@tonic-gate  * specify these cases.
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  * This implementation can raise spurious underflow, overflow, in-
53*0Sstevel@tonic-gate  * valid operation, inexact, and division-by-zero exceptions.  C99
54*0Sstevel@tonic-gate  * allows this.
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #if !defined(sparc) && !defined(__sparc)
58*0Sstevel@tonic-gate #error This code is for SPARC only
59*0Sstevel@tonic-gate #endif
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate static union {
62*0Sstevel@tonic-gate 	int		i[4];
63*0Sstevel@tonic-gate 	long double	q;
64*0Sstevel@tonic-gate } inf = {
65*0Sstevel@tonic-gate 	0x7fff0000, 0, 0, 0
66*0Sstevel@tonic-gate };
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static int
testinfl(long double x)72*0Sstevel@tonic-gate testinfl(long double x)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	union {
75*0Sstevel@tonic-gate 		int		i[4];
76*0Sstevel@tonic-gate 		long double	q;
77*0Sstevel@tonic-gate 	} xx;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	xx.q = x;
80*0Sstevel@tonic-gate 	return (((((xx.i[0] << 1) - 0xfffe0000) | xx.i[1] | xx.i[2] | xx.i[3])
81*0Sstevel@tonic-gate 		== 0)? (1 | (xx.i[0] >> 31)) : 0);
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #ifdef __sparcv9
85*0Sstevel@tonic-gate long double _Complex
_Q_cplx_div(const long double _Complex * z,const long double _Complex * w)86*0Sstevel@tonic-gate _Q_cplx_div(const long double _Complex *z, const long double _Complex *w)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	long double _Complex	v;
89*0Sstevel@tonic-gate #else
90*0Sstevel@tonic-gate void
91*0Sstevel@tonic-gate _Q_cplx_div(long double _Complex *v, const long double _Complex *z,
92*0Sstevel@tonic-gate 	const long double _Complex *w)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate 	union {
96*0Sstevel@tonic-gate 		int		i[4];
97*0Sstevel@tonic-gate 		long double	q;
98*0Sstevel@tonic-gate 	} aa, bb, cc, dd, ss;
99*0Sstevel@tonic-gate 	long double	a, b, c, d, r;
100*0Sstevel@tonic-gate 	int		ha, hb, hc, hd, hz, hw, hs, i, j;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	/*
103*0Sstevel@tonic-gate 	 * The following is equivalent to
104*0Sstevel@tonic-gate 	 *
105*0Sstevel@tonic-gate 	 *  a = creall(*z); b = cimagl(*z);
106*0Sstevel@tonic-gate 	 *  c = creall(*w); d = cimagl(*w);
107*0Sstevel@tonic-gate 	 */
108*0Sstevel@tonic-gate 	a = ((long double *)z)[0];
109*0Sstevel@tonic-gate 	b = ((long double *)z)[1];
110*0Sstevel@tonic-gate 	c = ((long double *)w)[0];
111*0Sstevel@tonic-gate 	d = ((long double *)w)[1];
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	/* extract high-order words to estimate |z| and |w| */
114*0Sstevel@tonic-gate 	aa.q = a;
115*0Sstevel@tonic-gate 	bb.q = b;
116*0Sstevel@tonic-gate 	ha = aa.i[0] & ~0x80000000;
117*0Sstevel@tonic-gate 	hb = bb.i[0] & ~0x80000000;
118*0Sstevel@tonic-gate 	hz = (ha > hb)? ha : hb;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	cc.q = c;
121*0Sstevel@tonic-gate 	dd.q = d;
122*0Sstevel@tonic-gate 	hc = cc.i[0] & ~0x80000000;
123*0Sstevel@tonic-gate 	hd = dd.i[0] & ~0x80000000;
124*0Sstevel@tonic-gate 	hw = (hc > hd)? hc : hd;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/* check for special cases */
127*0Sstevel@tonic-gate 	if (hw >= 0x7fff0000) { /* w is inf or nan */
128*0Sstevel@tonic-gate 		r = 0.0l;
129*0Sstevel@tonic-gate 		i = testinfl(c);
130*0Sstevel@tonic-gate 		j = testinfl(d);
131*0Sstevel@tonic-gate 		if (i | j) { /* w is infinite */
132*0Sstevel@tonic-gate 			/*
133*0Sstevel@tonic-gate 			 * "factor out" infinity, being careful to preserve
134*0Sstevel@tonic-gate 			 * signs of finite values
135*0Sstevel@tonic-gate 			 */
136*0Sstevel@tonic-gate 			c = i? i : ((cc.i[0] < 0)? -0.0l : 0.0l);
137*0Sstevel@tonic-gate 			d = j? j : ((dd.i[0] < 0)? -0.0l : 0.0l);
138*0Sstevel@tonic-gate 			if (hz >= 0x7ffe0000) {
139*0Sstevel@tonic-gate 				/* scale to avoid overflow below */
140*0Sstevel@tonic-gate 				c *= 0.5l;
141*0Sstevel@tonic-gate 				d *= 0.5l;
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 		goto done;
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (hw == 0 && (cc.i[1] | cc.i[2] | cc.i[3] |
148*0Sstevel@tonic-gate 		dd.i[1] | dd.i[2] | dd.i[3]) == 0) {
149*0Sstevel@tonic-gate 		/* w is zero; multiply z by 1/Re(w) - I * Im(w) */
150*0Sstevel@tonic-gate 		r = 1.0l;
151*0Sstevel@tonic-gate 		c = 1.0l / c;
152*0Sstevel@tonic-gate 		i = testinfl(a);
153*0Sstevel@tonic-gate 		j = testinfl(b);
154*0Sstevel@tonic-gate 		if (i | j) { /* z is infinite */
155*0Sstevel@tonic-gate 			a = i;
156*0Sstevel@tonic-gate 			b = j;
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 		goto done;
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	if (hz >= 0x7fff0000) { /* z is inf or nan */
162*0Sstevel@tonic-gate 		r = 1.0l;
163*0Sstevel@tonic-gate 		i = testinfl(a);
164*0Sstevel@tonic-gate 		j = testinfl(b);
165*0Sstevel@tonic-gate 		if (i | j) { /* z is infinite */
166*0Sstevel@tonic-gate 			a = i;
167*0Sstevel@tonic-gate 			b = j;
168*0Sstevel@tonic-gate 			r = inf.q;
169*0Sstevel@tonic-gate 		}
170*0Sstevel@tonic-gate 		goto done;
171*0Sstevel@tonic-gate 	}
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	/*
174*0Sstevel@tonic-gate 	 * Scale c and d to compute 1/|w|^2 and the real and imaginary
175*0Sstevel@tonic-gate 	 * parts of the quotient.
176*0Sstevel@tonic-gate 	 */
177*0Sstevel@tonic-gate 	hs = (((hw >> 2) - hw) + 0x6ffd7fff) & 0xffff0000;
178*0Sstevel@tonic-gate 	if (hz < 0x00ea0000) { /* |z| < 2^-16149 */
179*0Sstevel@tonic-gate 		if (((hw - 0x3e380000) | (0x40e90000 - hw)) >= 0)
180*0Sstevel@tonic-gate 			hs = (((0x40e90000 - hw) >> 1) & 0xffff0000)
181*0Sstevel@tonic-gate 				+ 0x3fff0000;
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	ss.i[0] = hs;
184*0Sstevel@tonic-gate 	ss.i[1] = ss.i[2] = ss.i[3] = 0;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	c *= ss.q;
187*0Sstevel@tonic-gate 	d *= ss.q;
188*0Sstevel@tonic-gate 	r = 1.0l / (c * c + d * d);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	c *= ss.q;
191*0Sstevel@tonic-gate 	d *= ss.q;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate done:
194*0Sstevel@tonic-gate #ifdef __sparcv9
195*0Sstevel@tonic-gate 	((long double *)&v)[0] = (a * c + b * d) * r;
196*0Sstevel@tonic-gate 	((long double *)&v)[1] = (b * c - a * d) * r;
197*0Sstevel@tonic-gate 	return (v);
198*0Sstevel@tonic-gate #else
199*0Sstevel@tonic-gate 	((long double *)v)[0] = (a * c + b * d) * r;
200*0Sstevel@tonic-gate 	((long double *)v)[1] = (b * c - a * d) * r;
201*0Sstevel@tonic-gate #endif
202*0Sstevel@tonic-gate }
203