fpu_sqrt.c revision 92986
1/*
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 *	This product includes software developed by the University of
12 *	California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 *    must display the following acknowledgement:
24 *	This product includes software developed by the University of
25 *	California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 *    may be used to endorse or promote products derived from this software
28 *    without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 *	@(#)fpu_sqrt.c	8.1 (Berkeley) 6/11/93
43 *	$NetBSD: fpu_sqrt.c,v 1.2 1994/11/20 20:52:46 deraadt Exp $
44 */
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/lib/libc/sparc64/fpu/fpu_sqrt.c 92986 2002-03-22 21:53:29Z obrien $");
48
49/*
50 * Perform an FPU square root (return sqrt(x)).
51 */
52
53#include <sys/types.h>
54
55#include <machine/frame.h>
56#include <machine/fp.h>
57
58#include "fpu_arith.h"
59#include "fpu_emu.h"
60#include "fpu_extern.h"
61
62/*
63 * Our task is to calculate the square root of a floating point number x0.
64 * This number x normally has the form:
65 *
66 *		    exp
67 *	x = mant * 2		(where 1 <= mant < 2 and exp is an integer)
68 *
69 * This can be left as it stands, or the mantissa can be doubled and the
70 * exponent decremented:
71 *
72 *			  exp-1
73 *	x = (2 * mant) * 2	(where 2 <= 2 * mant < 4)
74 *
75 * If the exponent `exp' is even, the square root of the number is best
76 * handled using the first form, and is by definition equal to:
77 *
78 *				exp/2
79 *	sqrt(x) = sqrt(mant) * 2
80 *
81 * If exp is odd, on the other hand, it is convenient to use the second
82 * form, giving:
83 *
84 *				    (exp-1)/2
85 *	sqrt(x) = sqrt(2 * mant) * 2
86 *
87 * In the first case, we have
88 *
89 *	1 <= mant < 2
90 *
91 * and therefore
92 *
93 *	sqrt(1) <= sqrt(mant) < sqrt(2)
94 *
95 * while in the second case we have
96 *
97 *	2 <= 2*mant < 4
98 *
99 * and therefore
100 *
101 *	sqrt(2) <= sqrt(2*mant) < sqrt(4)
102 *
103 * so that in any case, we are sure that
104 *
105 *	sqrt(1) <= sqrt(n * mant) < sqrt(4),	n = 1 or 2
106 *
107 * or
108 *
109 *	1 <= sqrt(n * mant) < 2,		n = 1 or 2.
110 *
111 * This root is therefore a properly formed mantissa for a floating
112 * point number.  The exponent of sqrt(x) is either exp/2 or (exp-1)/2
113 * as above.  This leaves us with the problem of finding the square root
114 * of a fixed-point number in the range [1..4).
115 *
116 * Though it may not be instantly obvious, the following square root
117 * algorithm works for any integer x of an even number of bits, provided
118 * that no overflows occur:
119 *
120 *	let q = 0
121 *	for k = NBITS-1 to 0 step -1 do -- for each digit in the answer...
122 *		x *= 2			-- multiply by radix, for next digit
123 *		if x >= 2q + 2^k then	-- if adding 2^k does not
124 *			x -= 2q + 2^k	-- exceed the correct root,
125 *			q += 2^k	-- add 2^k and adjust x
126 *		fi
127 *	done
128 *	sqrt = q / 2^(NBITS/2)		-- (and any remainder is in x)
129 *
130 * If NBITS is odd (so that k is initially even), we can just add another
131 * zero bit at the top of x.  Doing so means that q is not going to acquire
132 * a 1 bit in the first trip around the loop (since x0 < 2^NBITS).  If the
133 * final value in x is not needed, or can be off by a factor of 2, this is
134 * equivalant to moving the `x *= 2' step to the bottom of the loop:
135 *
136 *	for k = NBITS-1 to 0 step -1 do if ... fi; x *= 2; done
137 *
138 * and the result q will then be sqrt(x0) * 2^floor(NBITS / 2).
139 * (Since the algorithm is destructive on x, we will call x's initial
140 * value, for which q is some power of two times its square root, x0.)
141 *
142 * If we insert a loop invariant y = 2q, we can then rewrite this using
143 * C notation as:
144 *
145 *	q = y = 0; x = x0;
146 *	for (k = NBITS; --k >= 0;) {
147 * #if (NBITS is even)
148 *		x *= 2;
149 * #endif
150 *		t = y + (1 << k);
151 *		if (x >= t) {
152 *			x -= t;
153 *			q += 1 << k;
154 *			y += 1 << (k + 1);
155 *		}
156 * #if (NBITS is odd)
157 *		x *= 2;
158 * #endif
159 *	}
160 *
161 * If x0 is fixed point, rather than an integer, we can simply alter the
162 * scale factor between q and sqrt(x0).  As it happens, we can easily arrange
163 * for the scale factor to be 2**0 or 1, so that sqrt(x0) == q.
164 *
165 * In our case, however, x0 (and therefore x, y, q, and t) are multiword
166 * integers, which adds some complication.  But note that q is built one
167 * bit at a time, from the top down, and is not used itself in the loop
168 * (we use 2q as held in y instead).  This means we can build our answer
169 * in an integer, one word at a time, which saves a bit of work.  Also,
170 * since 1 << k is always a `new' bit in q, 1 << k and 1 << (k+1) are
171 * `new' bits in y and we can set them with an `or' operation rather than
172 * a full-blown multiword add.
173 *
174 * We are almost done, except for one snag.  We must prove that none of our
175 * intermediate calculations can overflow.  We know that x0 is in [1..4)
176 * and therefore the square root in q will be in [1..2), but what about x,
177 * y, and t?
178 *
179 * We know that y = 2q at the beginning of each loop.  (The relation only
180 * fails temporarily while y and q are being updated.)  Since q < 2, y < 4.
181 * The sum in t can, in our case, be as much as y+(1<<1) = y+2 < 6, and.
182 * Furthermore, we can prove with a bit of work that x never exceeds y by
183 * more than 2, so that even after doubling, 0 <= x < 8.  (This is left as
184 * an exercise to the reader, mostly because I have become tired of working
185 * on this comment.)
186 *
187 * If our floating point mantissas (which are of the form 1.frac) occupy
188 * B+1 bits, our largest intermediary needs at most B+3 bits, or two extra.
189 * In fact, we want even one more bit (for a carry, to avoid compares), or
190 * three extra.  There is a comment in fpu_emu.h reminding maintainers of
191 * this, so we have some justification in assuming it.
192 */
193struct fpn *
194__fpu_sqrt(fe)
195	struct fpemu *fe;
196{
197	struct fpn *x = &fe->fe_f1;
198	u_int bit, q, tt;
199	u_int x0, x1, x2, x3;
200	u_int y0, y1, y2, y3;
201	u_int d0, d1, d2, d3;
202	int e;
203
204	/*
205	 * Take care of special cases first.  In order:
206	 *
207	 *	sqrt(NaN) = NaN
208	 *	sqrt(+0) = +0
209	 *	sqrt(-0) = -0
210	 *	sqrt(x < 0) = NaN	(including sqrt(-Inf))
211	 *	sqrt(+Inf) = +Inf
212	 *
213	 * Then all that remains are numbers with mantissas in [1..2).
214	 */
215	if (ISNAN(x) || ISZERO(x))
216		return (x);
217	if (x->fp_sign)
218		return (__fpu_newnan(fe));
219	if (ISINF(x))
220		return (x);
221
222	/*
223	 * Calculate result exponent.  As noted above, this may involve
224	 * doubling the mantissa.  We will also need to double x each
225	 * time around the loop, so we define a macro for this here, and
226	 * we break out the multiword mantissa.
227	 */
228#ifdef FPU_SHL1_BY_ADD
229#define	DOUBLE_X { \
230	FPU_ADDS(x3, x3, x3); FPU_ADDCS(x2, x2, x2); \
231	FPU_ADDCS(x1, x1, x1); FPU_ADDC(x0, x0, x0); \
232}
233#else
234#define	DOUBLE_X { \
235	x0 = (x0 << 1) | (x1 >> 31); x1 = (x1 << 1) | (x2 >> 31); \
236	x2 = (x2 << 1) | (x3 >> 31); x3 <<= 1; \
237}
238#endif
239#if (FP_NMANT & 1) != 0
240# define ODD_DOUBLE	DOUBLE_X
241# define EVEN_DOUBLE	/* nothing */
242#else
243# define ODD_DOUBLE	/* nothing */
244# define EVEN_DOUBLE	DOUBLE_X
245#endif
246	x0 = x->fp_mant[0];
247	x1 = x->fp_mant[1];
248	x2 = x->fp_mant[2];
249	x3 = x->fp_mant[3];
250	e = x->fp_exp;
251	if (e & 1)		/* exponent is odd; use sqrt(2mant) */
252		DOUBLE_X;
253	/* THE FOLLOWING ASSUMES THAT RIGHT SHIFT DOES SIGN EXTENSION */
254	x->fp_exp = e >> 1;	/* calculates (e&1 ? (e-1)/2 : e/2 */
255
256	/*
257	 * Now calculate the mantissa root.  Since x is now in [1..4),
258	 * we know that the first trip around the loop will definitely
259	 * set the top bit in q, so we can do that manually and start
260	 * the loop at the next bit down instead.  We must be sure to
261	 * double x correctly while doing the `known q=1.0'.
262	 *
263	 * We do this one mantissa-word at a time, as noted above, to
264	 * save work.  To avoid `(1 << 31) << 1', we also do the top bit
265	 * outside of each per-word loop.
266	 *
267	 * The calculation `t = y + bit' breaks down into `t0 = y0, ...,
268	 * t3 = y3, t? |= bit' for the appropriate word.  Since the bit
269	 * is always a `new' one, this means that three of the `t?'s are
270	 * just the corresponding `y?'; we use `#define's here for this.
271	 * The variable `tt' holds the actual `t?' variable.
272	 */
273
274	/* calculate q0 */
275#define	t0 tt
276	bit = FP_1;
277	EVEN_DOUBLE;
278	/* if (x >= (t0 = y0 | bit)) { */	/* always true */
279		q = bit;
280		x0 -= bit;
281		y0 = bit << 1;
282	/* } */
283	ODD_DOUBLE;
284	while ((bit >>= 1) != 0) {	/* for remaining bits in q0 */
285		EVEN_DOUBLE;
286		t0 = y0 | bit;		/* t = y + bit */
287		if (x0 >= t0) {		/* if x >= t then */
288			x0 -= t0;	/*	x -= t */
289			q |= bit;	/*	q += bit */
290			y0 |= bit << 1;	/*	y += bit << 1 */
291		}
292		ODD_DOUBLE;
293	}
294	x->fp_mant[0] = q;
295#undef t0
296
297	/* calculate q1.  note (y0&1)==0. */
298#define t0 y0
299#define t1 tt
300	q = 0;
301	y1 = 0;
302	bit = 1 << 31;
303	EVEN_DOUBLE;
304	t1 = bit;
305	FPU_SUBS(d1, x1, t1);
306	FPU_SUBC(d0, x0, t0);		/* d = x - t */
307	if ((int)d0 >= 0) {		/* if d >= 0 (i.e., x >= t) then */
308		x0 = d0, x1 = d1;	/*	x -= t */
309		q = bit;		/*	q += bit */
310		y0 |= 1;		/*	y += bit << 1 */
311	}
312	ODD_DOUBLE;
313	while ((bit >>= 1) != 0) {	/* for remaining bits in q1 */
314		EVEN_DOUBLE;		/* as before */
315		t1 = y1 | bit;
316		FPU_SUBS(d1, x1, t1);
317		FPU_SUBC(d0, x0, t0);
318		if ((int)d0 >= 0) {
319			x0 = d0, x1 = d1;
320			q |= bit;
321			y1 |= bit << 1;
322		}
323		ODD_DOUBLE;
324	}
325	x->fp_mant[1] = q;
326#undef t1
327
328	/* calculate q2.  note (y1&1)==0; y0 (aka t0) is fixed. */
329#define t1 y1
330#define t2 tt
331	q = 0;
332	y2 = 0;
333	bit = 1 << 31;
334	EVEN_DOUBLE;
335	t2 = bit;
336	FPU_SUBS(d2, x2, t2);
337	FPU_SUBCS(d1, x1, t1);
338	FPU_SUBC(d0, x0, t0);
339	if ((int)d0 >= 0) {
340		x0 = d0, x1 = d1, x2 = d2;
341		q |= bit;
342		y1 |= 1;		/* now t1, y1 are set in concrete */
343	}
344	ODD_DOUBLE;
345	while ((bit >>= 1) != 0) {
346		EVEN_DOUBLE;
347		t2 = y2 | bit;
348		FPU_SUBS(d2, x2, t2);
349		FPU_SUBCS(d1, x1, t1);
350		FPU_SUBC(d0, x0, t0);
351		if ((int)d0 >= 0) {
352			x0 = d0, x1 = d1, x2 = d2;
353			q |= bit;
354			y2 |= bit << 1;
355		}
356		ODD_DOUBLE;
357	}
358	x->fp_mant[2] = q;
359#undef t2
360
361	/* calculate q3.  y0, t0, y1, t1 all fixed; y2, t2, almost done. */
362#define t2 y2
363#define t3 tt
364	q = 0;
365	y3 = 0;
366	bit = 1 << 31;
367	EVEN_DOUBLE;
368	t3 = bit;
369	FPU_SUBS(d3, x3, t3);
370	FPU_SUBCS(d2, x2, t2);
371	FPU_SUBCS(d1, x1, t1);
372	FPU_SUBC(d0, x0, t0);
373	ODD_DOUBLE;
374	if ((int)d0 >= 0) {
375		x0 = d0, x1 = d1, x2 = d2;
376		q |= bit;
377		y2 |= 1;
378	}
379	while ((bit >>= 1) != 0) {
380		EVEN_DOUBLE;
381		t3 = y3 | bit;
382		FPU_SUBS(d3, x3, t3);
383		FPU_SUBCS(d2, x2, t2);
384		FPU_SUBCS(d1, x1, t1);
385		FPU_SUBC(d0, x0, t0);
386		if ((int)d0 >= 0) {
387			x0 = d0, x1 = d1, x2 = d2;
388			q |= bit;
389			y3 |= bit << 1;
390		}
391		ODD_DOUBLE;
392	}
393	x->fp_mant[3] = q;
394
395	/*
396	 * The result, which includes guard and round bits, is exact iff
397	 * x is now zero; any nonzero bits in x represent sticky bits.
398	 */
399	x->fp_sticky = x0 | x1 | x2 | x3;
400	return (x);
401}
402