summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/sparc/fp/_D_cplx_div.c
blob: 7144732968bf6c4b65b9ab9b98dd140327435b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * _D_cplx_div(z, w) returns z / w with infinities handled according
 * to C99.
 *
 * If z and w are both finite and w is nonzero, _D_cplx_div(z, w)
 * delivers the complex quotient q according to the usual formula:
 * let a = Re(z), b = Im(z), c = Re(w), and d = Im(w); then q = x +
 * I * y where x = (a * c + b * d) / r and y = (b * c - a * d) / r
 * with r = c * c + d * d.  This implementation scales to avoid
 * premature underflow or overflow.
 *
 * If z is neither NaN nor zero and w is zero, or if z is infinite
 * and w is finite and nonzero, _D_cplx_div delivers an infinite
 * result.  If z is finite and w is infinite, _D_cplx_div delivers
 * a zero result.
 *
 * If z and w are both zero or both infinite, or if either z or w is
 * a complex NaN, _D_cplx_div delivers NaN + I * NaN.  C99 doesn't
 * specify these cases.
 *
 * This implementation can raise spurious underflow, overflow, in-
 * valid operation, inexact, and division-by-zero exceptions.  C99
 * allows this.
 *
 * Warning: Do not attempt to "optimize" this code by removing multi-
 * plications by zero.
 */

#if !defined(sparc) && !defined(__sparc)
#error This code is for SPARC only
#endif

static union {
	int	i[2];
	double	d;
} inf = {
	0x7ff00000, 0
};

/*
 * Return +1 if x is +Inf, -1 if x is -Inf, and 0 otherwise
 */
static int
testinf(double x)
{
	union {
		int	i[2];
		double	d;
	} xx;

	xx.d = x;
	return (((((xx.i[0] << 1) - 0xffe00000) | xx.i[1]) == 0)?
		(1 | (xx.i[0] >> 31)) : 0);
}

double _Complex
_D_cplx_div(double _Complex z, double _Complex w)
{
	double _Complex	v;
	union {
		int	i[2];
		double	d;
	} aa, bb, cc, dd, ss;
	double		a, b, c, d, r;
	int		ha, hb, hc, hd, hz, hw, hs, i, j;

	/*
	 * The following is equivalent to
	 *
	 *  a = creal(z); b = cimag(z);
	 *  c = creal(w); d = cimag(w);
	 */
	a = ((double *)&z)[0];
	b = ((double *)&z)[1];
	c = ((double *)&w)[0];
	d = ((double *)&w)[1];

	/* extract high-order words to estimate |z| and |w| */
	aa.d = a;
	bb.d = b;
	ha = aa.i[0] & ~0x80000000;
	hb = bb.i[0] & ~0x80000000;
	hz = (ha > hb)? ha : hb;

	cc.d = c;
	dd.d = d;
	hc = cc.i[0] & ~0x80000000;
	hd = dd.i[0] & ~0x80000000;
	hw = (hc > hd)? hc : hd;

	/* check for special cases */
	if (hw >= 0x7ff00000) { /* w is inf or nan */
		r = 0.0;
		i = testinf(c);
		j = testinf(d);
		if (i | j) { /* w is infinite */
			/*
			 * "factor out" infinity, being careful to preserve
			 * signs of finite values
			 */
			c = i? i : ((cc.i[0] < 0)? -0.0 : 0.0);
			d = j? j : ((dd.i[0] < 0)? -0.0 : 0.0);
			if (hz >= 0x7fe00000) {
				/* scale to avoid overflow below */
				c *= 0.5;
				d *= 0.5;
			}
		}
		((double *)&v)[0] = (a * c + b * d) * r;
		((double *)&v)[1] = (b * c - a * d) * r;
		return (v);
	}

	if (hw < 0x00100000) {
		/*
		 * This nonsense is needed to work around some SPARC
		 * implementations of nonstandard mode; if both parts
		 * of w are subnormal, multiply them by one to force
		 * them to be flushed to zero when nonstandard mode
		 * is enabled.  Sheesh.
		 */
		cc.d = c = c * 1.0;
		dd.d = d = d * 1.0;
		hc = cc.i[0] & ~0x80000000;
		hd = dd.i[0] & ~0x80000000;
		hw = (hc > hd)? hc : hd;
	}

	if (hw == 0 && (cc.i[1] | dd.i[1]) == 0) {
		/* w is zero; multiply z by 1/Re(w) - I * Im(w) */
		c = 1.0 / c;
		i = testinf(a);
		j = testinf(b);
		if (i | j) { /* z is infinite */
			a = i;
			b = j;
		}
		((double *)&v)[0] = a * c + b * d;
		((double *)&v)[1] = b * c - a * d;
		return (v);
	}

	if (hz >= 0x7ff00000) { /* z is inf or nan */
		r = 1.0;
		i = testinf(a);
		j = testinf(b);
		if (i | j) { /* z is infinite */
			a = i;
			b = j;
			r = inf.d;
		}
		((double *)&v)[0] = (a * c + b * d) * r;
		((double *)&v)[1] = (b * c - a * d) * r;
		return (v);
	}

	/*
	 * Scale c and d to compute 1/|w|^2 and the real and imaginary
	 * parts of the quotient.
	 *
	 * Note that for any s, if we let c' = sc, d' = sd, c'' = sc',
	 * and d'' = sd', then
	 *
	 *  (ac'' + bd'') / (c'^2 + d'^2) = (ac + bd) / (c^2 + d^2)
	 *
	 * and similarly for the imaginary part of the quotient.  We want
	 * to choose s such that (i) r := 1/(c'^2 + d'^2) can be computed
	 * without overflow or harmful underflow, and (ii) (ac'' + bd'')
	 * and (bc'' - ad'') can be computed without spurious overflow or
	 * harmful underflow.  To avoid unnecessary rounding, we restrict
	 * s to a power of two.
	 *
	 * To satisfy (i), we need to choose s such that max(|c'|,|d'|)
	 * is not too far from one.  To satisfy (ii), we need to choose
	 * s such that max(|c''|,|d''|) is also not too far from one.
	 * There is some leeway in our choice, but to keep the logic
	 * from getting overly complicated, we simply attempt to roughly
	 * balance these constraints by choosing s so as to make r about
	 * the same size as max(|c''|,|d''|).  This corresponds to choos-
	 * ing s to be a power of two near |w|^(-3/4).
	 *
	 * Regarding overflow, observe that if max(|c''|,|d''|) <= 1/2,
	 * then the computation of (ac'' + bd'') and (bc'' - ad'') can-
	 * not overflow; otherwise, the computation of either of these
	 * values can only incur overflow if the true result would be
	 * within a factor of two of the overflow threshold.  In other
	 * words, if we bias the choice of s such that at least one of
	 *
	 *  max(|c''|,|d''|) <= 1/2   or   r >= 2
	 *
	 * always holds, then no undeserved overflow can occur.
	 *
	 * To cope with underflow, note that if r < 2^-53, then any
	 * intermediate results that underflow are insignificant; either
	 * they will be added to normal results, rendering the under-
	 * flow no worse than ordinary roundoff, or they will contribute
	 * to a final result that is smaller than the smallest subnormal
	 * number.  Therefore, we need only modify the preceding logic
	 * when z is very small and w is not too far from one.  In that
	 * case, we can reduce the effect of any intermediate underflow
	 * to no worse than ordinary roundoff error by choosing s so as
	 * to make max(|c''|,|d''|) large enough that at least one of
	 * (ac'' + bd'') or (bc'' - ad'') is normal.
	 */
	hs = (((hw >> 2) - hw) + 0x6fd7ffff) & 0xfff00000;
	if (hz < 0x07200000) { /* |z| < 2^-909 */
		if (((hw - 0x32800000) | (0x47100000 - hw)) >= 0)
			hs = (((0x47100000 - hw) >> 1) & 0xfff00000)
				+ 0x3ff00000;
	}
	ss.i[0] = hs;
	ss.i[1] = 0;

	c *= ss.d;
	d *= ss.d;
	r = 1.0 / (c * c + d * d);

	c *= ss.d;
	d *= ss.d;
	((double *)&v)[0] = (a * c + b * d) * r;
	((double *)&v)[1] = (b * c - a * d) * r;
	return (v);
}