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