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