1176491Smarcel/* $NetBSD: fpu_sqrt.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */ 2176491Smarcel 3176491Smarcel/* 4176491Smarcel * Copyright (c) 1992, 1993 5176491Smarcel * The Regents of the University of California. All rights reserved. 6176491Smarcel * 7176491Smarcel * This software was developed by the Computer Systems Engineering group 8176491Smarcel * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9176491Smarcel * contributed to Berkeley. 10176491Smarcel * 11176491Smarcel * All advertising materials mentioning features or use of this software 12176491Smarcel * must display the following acknowledgement: 13176491Smarcel * This product includes software developed by the University of 14176491Smarcel * California, Lawrence Berkeley Laboratory. 15176491Smarcel * 16176491Smarcel * Redistribution and use in source and binary forms, with or without 17176491Smarcel * modification, are permitted provided that the following conditions 18176491Smarcel * are met: 19176491Smarcel * 1. Redistributions of source code must retain the above copyright 20176491Smarcel * notice, this list of conditions and the following disclaimer. 21176491Smarcel * 2. Redistributions in binary form must reproduce the above copyright 22176491Smarcel * notice, this list of conditions and the following disclaimer in the 23176491Smarcel * documentation and/or other materials provided with the distribution. 24176491Smarcel * 3. Neither the name of the University nor the names of its contributors 25176491Smarcel * may be used to endorse or promote products derived from this software 26176491Smarcel * without specific prior written permission. 27176491Smarcel * 28176491Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29176491Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30176491Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31176491Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32176491Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33176491Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34176491Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35176491Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36176491Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37176491Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38176491Smarcel * SUCH DAMAGE. 39176491Smarcel * 40176491Smarcel * @(#)fpu_sqrt.c 8.1 (Berkeley) 6/11/93 41176491Smarcel */ 42176491Smarcel 43176491Smarcel/* 44176491Smarcel * Perform an FPU square root (return sqrt(x)). 45176491Smarcel */ 46176491Smarcel 47176491Smarcel#include <sys/cdefs.h> 48176491Smarcel__FBSDID("$FreeBSD$"); 49176491Smarcel 50178030Sgrehan#include <sys/types.h> 51176491Smarcel#include <sys/systm.h> 52176491Smarcel 53176491Smarcel#include <machine/fpu.h> 54176491Smarcel#include <machine/reg.h> 55176491Smarcel 56176491Smarcel#include <powerpc/fpu/fpu_arith.h> 57176491Smarcel#include <powerpc/fpu/fpu_emu.h> 58176491Smarcel 59176491Smarcel/* 60176491Smarcel * Our task is to calculate the square root of a floating point number x0. 61176491Smarcel * This number x normally has the form: 62176491Smarcel * 63176491Smarcel * exp 64176491Smarcel * x = mant * 2 (where 1 <= mant < 2 and exp is an integer) 65176491Smarcel * 66176491Smarcel * This can be left as it stands, or the mantissa can be doubled and the 67176491Smarcel * exponent decremented: 68176491Smarcel * 69176491Smarcel * exp-1 70176491Smarcel * x = (2 * mant) * 2 (where 2 <= 2 * mant < 4) 71176491Smarcel * 72176491Smarcel * If the exponent `exp' is even, the square root of the number is best 73176491Smarcel * handled using the first form, and is by definition equal to: 74176491Smarcel * 75176491Smarcel * exp/2 76176491Smarcel * sqrt(x) = sqrt(mant) * 2 77176491Smarcel * 78176491Smarcel * If exp is odd, on the other hand, it is convenient to use the second 79176491Smarcel * form, giving: 80176491Smarcel * 81176491Smarcel * (exp-1)/2 82176491Smarcel * sqrt(x) = sqrt(2 * mant) * 2 83176491Smarcel * 84176491Smarcel * In the first case, we have 85176491Smarcel * 86176491Smarcel * 1 <= mant < 2 87176491Smarcel * 88176491Smarcel * and therefore 89176491Smarcel * 90176491Smarcel * sqrt(1) <= sqrt(mant) < sqrt(2) 91176491Smarcel * 92176491Smarcel * while in the second case we have 93176491Smarcel * 94176491Smarcel * 2 <= 2*mant < 4 95176491Smarcel * 96176491Smarcel * and therefore 97176491Smarcel * 98176491Smarcel * sqrt(2) <= sqrt(2*mant) < sqrt(4) 99176491Smarcel * 100176491Smarcel * so that in any case, we are sure that 101176491Smarcel * 102176491Smarcel * sqrt(1) <= sqrt(n * mant) < sqrt(4), n = 1 or 2 103176491Smarcel * 104176491Smarcel * or 105176491Smarcel * 106176491Smarcel * 1 <= sqrt(n * mant) < 2, n = 1 or 2. 107176491Smarcel * 108176491Smarcel * This root is therefore a properly formed mantissa for a floating 109176491Smarcel * point number. The exponent of sqrt(x) is either exp/2 or (exp-1)/2 110176491Smarcel * as above. This leaves us with the problem of finding the square root 111176491Smarcel * of a fixed-point number in the range [1..4). 112176491Smarcel * 113176491Smarcel * Though it may not be instantly obvious, the following square root 114176491Smarcel * algorithm works for any integer x of an even number of bits, provided 115176491Smarcel * that no overflows occur: 116176491Smarcel * 117176491Smarcel * let q = 0 118176491Smarcel * for k = NBITS-1 to 0 step -1 do -- for each digit in the answer... 119176491Smarcel * x *= 2 -- multiply by radix, for next digit 120176491Smarcel * if x >= 2q + 2^k then -- if adding 2^k does not 121176491Smarcel * x -= 2q + 2^k -- exceed the correct root, 122176491Smarcel * q += 2^k -- add 2^k and adjust x 123176491Smarcel * fi 124176491Smarcel * done 125176491Smarcel * sqrt = q / 2^(NBITS/2) -- (and any remainder is in x) 126176491Smarcel * 127176491Smarcel * If NBITS is odd (so that k is initially even), we can just add another 128176491Smarcel * zero bit at the top of x. Doing so means that q is not going to acquire 129176491Smarcel * a 1 bit in the first trip around the loop (since x0 < 2^NBITS). If the 130176491Smarcel * final value in x is not needed, or can be off by a factor of 2, this is 131176491Smarcel * equivalant to moving the `x *= 2' step to the bottom of the loop: 132176491Smarcel * 133176491Smarcel * for k = NBITS-1 to 0 step -1 do if ... fi; x *= 2; done 134176491Smarcel * 135176491Smarcel * and the result q will then be sqrt(x0) * 2^floor(NBITS / 2). 136176491Smarcel * (Since the algorithm is destructive on x, we will call x's initial 137176491Smarcel * value, for which q is some power of two times its square root, x0.) 138176491Smarcel * 139176491Smarcel * If we insert a loop invariant y = 2q, we can then rewrite this using 140176491Smarcel * C notation as: 141176491Smarcel * 142176491Smarcel * q = y = 0; x = x0; 143176491Smarcel * for (k = NBITS; --k >= 0;) { 144176491Smarcel * #if (NBITS is even) 145176491Smarcel * x *= 2; 146176491Smarcel * #endif 147176491Smarcel * t = y + (1 << k); 148176491Smarcel * if (x >= t) { 149176491Smarcel * x -= t; 150176491Smarcel * q += 1 << k; 151176491Smarcel * y += 1 << (k + 1); 152176491Smarcel * } 153176491Smarcel * #if (NBITS is odd) 154176491Smarcel * x *= 2; 155176491Smarcel * #endif 156176491Smarcel * } 157176491Smarcel * 158176491Smarcel * If x0 is fixed point, rather than an integer, we can simply alter the 159176491Smarcel * scale factor between q and sqrt(x0). As it happens, we can easily arrange 160176491Smarcel * for the scale factor to be 2**0 or 1, so that sqrt(x0) == q. 161176491Smarcel * 162176491Smarcel * In our case, however, x0 (and therefore x, y, q, and t) are multiword 163176491Smarcel * integers, which adds some complication. But note that q is built one 164176491Smarcel * bit at a time, from the top down, and is not used itself in the loop 165176491Smarcel * (we use 2q as held in y instead). This means we can build our answer 166176491Smarcel * in an integer, one word at a time, which saves a bit of work. Also, 167176491Smarcel * since 1 << k is always a `new' bit in q, 1 << k and 1 << (k+1) are 168176491Smarcel * `new' bits in y and we can set them with an `or' operation rather than 169176491Smarcel * a full-blown multiword add. 170176491Smarcel * 171176491Smarcel * We are almost done, except for one snag. We must prove that none of our 172176491Smarcel * intermediate calculations can overflow. We know that x0 is in [1..4) 173176491Smarcel * and therefore the square root in q will be in [1..2), but what about x, 174176491Smarcel * y, and t? 175176491Smarcel * 176176491Smarcel * We know that y = 2q at the beginning of each loop. (The relation only 177176491Smarcel * fails temporarily while y and q are being updated.) Since q < 2, y < 4. 178176491Smarcel * The sum in t can, in our case, be as much as y+(1<<1) = y+2 < 6, and. 179176491Smarcel * Furthermore, we can prove with a bit of work that x never exceeds y by 180176491Smarcel * more than 2, so that even after doubling, 0 <= x < 8. (This is left as 181176491Smarcel * an exercise to the reader, mostly because I have become tired of working 182176491Smarcel * on this comment.) 183176491Smarcel * 184176491Smarcel * If our floating point mantissas (which are of the form 1.frac) occupy 185176491Smarcel * B+1 bits, our largest intermediary needs at most B+3 bits, or two extra. 186176491Smarcel * In fact, we want even one more bit (for a carry, to avoid compares), or 187176491Smarcel * three extra. There is a comment in fpu_emu.h reminding maintainers of 188176491Smarcel * this, so we have some justification in assuming it. 189176491Smarcel */ 190176491Smarcelstruct fpn * 191176491Smarcelfpu_sqrt(struct fpemu *fe) 192176491Smarcel{ 193176491Smarcel struct fpn *x = &fe->fe_f1; 194176491Smarcel u_int bit, q, tt; 195176491Smarcel u_int x0, x1, x2, x3; 196176491Smarcel u_int y0, y1, y2, y3; 197176491Smarcel u_int d0, d1, d2, d3; 198176491Smarcel int e; 199176491Smarcel FPU_DECL_CARRY; 200176491Smarcel 201176491Smarcel /* 202176491Smarcel * Take care of special cases first. In order: 203176491Smarcel * 204176491Smarcel * sqrt(NaN) = NaN 205176491Smarcel * sqrt(+0) = +0 206176491Smarcel * sqrt(-0) = -0 207176491Smarcel * sqrt(x < 0) = NaN (including sqrt(-Inf)) 208176491Smarcel * sqrt(+Inf) = +Inf 209176491Smarcel * 210176491Smarcel * Then all that remains are numbers with mantissas in [1..2). 211176491Smarcel */ 212176491Smarcel DPRINTF(FPE_REG, ("fpu_sqer:\n")); 213176491Smarcel DUMPFPN(FPE_REG, x); 214176491Smarcel DPRINTF(FPE_REG, ("=>\n")); 215176491Smarcel if (ISNAN(x)) { 216176491Smarcel fe->fe_cx |= FPSCR_VXSNAN; 217176491Smarcel DUMPFPN(FPE_REG, x); 218176491Smarcel return (x); 219176491Smarcel } 220176491Smarcel if (ISZERO(x)) { 221176491Smarcel fe->fe_cx |= FPSCR_ZX; 222176491Smarcel x->fp_class = FPC_INF; 223176491Smarcel DUMPFPN(FPE_REG, x); 224176491Smarcel return (x); 225176491Smarcel } 226176491Smarcel if (x->fp_sign) { 227176491Smarcel return (fpu_newnan(fe)); 228176491Smarcel } 229176491Smarcel if (ISINF(x)) { 230176491Smarcel fe->fe_cx |= FPSCR_VXSQRT; 231176491Smarcel DUMPFPN(FPE_REG, 0); 232176491Smarcel return (0); 233176491Smarcel } 234176491Smarcel 235176491Smarcel /* 236176491Smarcel * Calculate result exponent. As noted above, this may involve 237176491Smarcel * doubling the mantissa. We will also need to double x each 238176491Smarcel * time around the loop, so we define a macro for this here, and 239176491Smarcel * we break out the multiword mantissa. 240176491Smarcel */ 241176491Smarcel#ifdef FPU_SHL1_BY_ADD 242176491Smarcel#define DOUBLE_X { \ 243176491Smarcel FPU_ADDS(x3, x3, x3); FPU_ADDCS(x2, x2, x2); \ 244176491Smarcel FPU_ADDCS(x1, x1, x1); FPU_ADDC(x0, x0, x0); \ 245176491Smarcel} 246176491Smarcel#else 247176491Smarcel#define DOUBLE_X { \ 248176491Smarcel x0 = (x0 << 1) | (x1 >> 31); x1 = (x1 << 1) | (x2 >> 31); \ 249176491Smarcel x2 = (x2 << 1) | (x3 >> 31); x3 <<= 1; \ 250176491Smarcel} 251176491Smarcel#endif 252176491Smarcel#if (FP_NMANT & 1) != 0 253176491Smarcel# define ODD_DOUBLE DOUBLE_X 254176491Smarcel# define EVEN_DOUBLE /* nothing */ 255176491Smarcel#else 256176491Smarcel# define ODD_DOUBLE /* nothing */ 257176491Smarcel# define EVEN_DOUBLE DOUBLE_X 258176491Smarcel#endif 259176491Smarcel x0 = x->fp_mant[0]; 260176491Smarcel x1 = x->fp_mant[1]; 261176491Smarcel x2 = x->fp_mant[2]; 262176491Smarcel x3 = x->fp_mant[3]; 263176491Smarcel e = x->fp_exp; 264176491Smarcel if (e & 1) /* exponent is odd; use sqrt(2mant) */ 265176491Smarcel DOUBLE_X; 266176491Smarcel /* THE FOLLOWING ASSUMES THAT RIGHT SHIFT DOES SIGN EXTENSION */ 267176491Smarcel x->fp_exp = e >> 1; /* calculates (e&1 ? (e-1)/2 : e/2 */ 268176491Smarcel 269176491Smarcel /* 270176491Smarcel * Now calculate the mantissa root. Since x is now in [1..4), 271176491Smarcel * we know that the first trip around the loop will definitely 272176491Smarcel * set the top bit in q, so we can do that manually and start 273176491Smarcel * the loop at the next bit down instead. We must be sure to 274176491Smarcel * double x correctly while doing the `known q=1.0'. 275176491Smarcel * 276176491Smarcel * We do this one mantissa-word at a time, as noted above, to 277261455Seadler * save work. To avoid `(1U << 31) << 1', we also do the top bit 278176491Smarcel * outside of each per-word loop. 279176491Smarcel * 280176491Smarcel * The calculation `t = y + bit' breaks down into `t0 = y0, ..., 281176491Smarcel * t3 = y3, t? |= bit' for the appropriate word. Since the bit 282176491Smarcel * is always a `new' one, this means that three of the `t?'s are 283176491Smarcel * just the corresponding `y?'; we use `#define's here for this. 284176491Smarcel * The variable `tt' holds the actual `t?' variable. 285176491Smarcel */ 286176491Smarcel 287176491Smarcel /* calculate q0 */ 288176491Smarcel#define t0 tt 289176491Smarcel bit = FP_1; 290176491Smarcel EVEN_DOUBLE; 291176491Smarcel /* if (x >= (t0 = y0 | bit)) { */ /* always true */ 292176491Smarcel q = bit; 293176491Smarcel x0 -= bit; 294176491Smarcel y0 = bit << 1; 295176491Smarcel /* } */ 296176491Smarcel ODD_DOUBLE; 297176491Smarcel while ((bit >>= 1) != 0) { /* for remaining bits in q0 */ 298176491Smarcel EVEN_DOUBLE; 299176491Smarcel t0 = y0 | bit; /* t = y + bit */ 300176491Smarcel if (x0 >= t0) { /* if x >= t then */ 301176491Smarcel x0 -= t0; /* x -= t */ 302176491Smarcel q |= bit; /* q += bit */ 303176491Smarcel y0 |= bit << 1; /* y += bit << 1 */ 304176491Smarcel } 305176491Smarcel ODD_DOUBLE; 306176491Smarcel } 307176491Smarcel x->fp_mant[0] = q; 308176491Smarcel#undef t0 309176491Smarcel 310176491Smarcel /* calculate q1. note (y0&1)==0. */ 311176491Smarcel#define t0 y0 312176491Smarcel#define t1 tt 313176491Smarcel q = 0; 314176491Smarcel y1 = 0; 315176491Smarcel bit = 1 << 31; 316176491Smarcel EVEN_DOUBLE; 317176491Smarcel t1 = bit; 318176491Smarcel FPU_SUBS(d1, x1, t1); 319176491Smarcel FPU_SUBC(d0, x0, t0); /* d = x - t */ 320176491Smarcel if ((int)d0 >= 0) { /* if d >= 0 (i.e., x >= t) then */ 321176491Smarcel x0 = d0, x1 = d1; /* x -= t */ 322176491Smarcel q = bit; /* q += bit */ 323176491Smarcel y0 |= 1; /* y += bit << 1 */ 324176491Smarcel } 325176491Smarcel ODD_DOUBLE; 326176491Smarcel while ((bit >>= 1) != 0) { /* for remaining bits in q1 */ 327176491Smarcel EVEN_DOUBLE; /* as before */ 328176491Smarcel t1 = y1 | bit; 329176491Smarcel FPU_SUBS(d1, x1, t1); 330176491Smarcel FPU_SUBC(d0, x0, t0); 331176491Smarcel if ((int)d0 >= 0) { 332176491Smarcel x0 = d0, x1 = d1; 333176491Smarcel q |= bit; 334176491Smarcel y1 |= bit << 1; 335176491Smarcel } 336176491Smarcel ODD_DOUBLE; 337176491Smarcel } 338176491Smarcel x->fp_mant[1] = q; 339176491Smarcel#undef t1 340176491Smarcel 341176491Smarcel /* calculate q2. note (y1&1)==0; y0 (aka t0) is fixed. */ 342176491Smarcel#define t1 y1 343176491Smarcel#define t2 tt 344176491Smarcel q = 0; 345176491Smarcel y2 = 0; 346176491Smarcel bit = 1 << 31; 347176491Smarcel EVEN_DOUBLE; 348176491Smarcel t2 = bit; 349176491Smarcel FPU_SUBS(d2, x2, t2); 350176491Smarcel FPU_SUBCS(d1, x1, t1); 351176491Smarcel FPU_SUBC(d0, x0, t0); 352176491Smarcel if ((int)d0 >= 0) { 353176491Smarcel x0 = d0, x1 = d1, x2 = d2; 354176491Smarcel q |= bit; 355176491Smarcel y1 |= 1; /* now t1, y1 are set in concrete */ 356176491Smarcel } 357176491Smarcel ODD_DOUBLE; 358176491Smarcel while ((bit >>= 1) != 0) { 359176491Smarcel EVEN_DOUBLE; 360176491Smarcel t2 = y2 | bit; 361176491Smarcel FPU_SUBS(d2, x2, t2); 362176491Smarcel FPU_SUBCS(d1, x1, t1); 363176491Smarcel FPU_SUBC(d0, x0, t0); 364176491Smarcel if ((int)d0 >= 0) { 365176491Smarcel x0 = d0, x1 = d1, x2 = d2; 366176491Smarcel q |= bit; 367176491Smarcel y2 |= bit << 1; 368176491Smarcel } 369176491Smarcel ODD_DOUBLE; 370176491Smarcel } 371176491Smarcel x->fp_mant[2] = q; 372176491Smarcel#undef t2 373176491Smarcel 374176491Smarcel /* calculate q3. y0, t0, y1, t1 all fixed; y2, t2, almost done. */ 375176491Smarcel#define t2 y2 376176491Smarcel#define t3 tt 377176491Smarcel q = 0; 378176491Smarcel y3 = 0; 379176491Smarcel bit = 1 << 31; 380176491Smarcel EVEN_DOUBLE; 381176491Smarcel t3 = bit; 382176491Smarcel FPU_SUBS(d3, x3, t3); 383176491Smarcel FPU_SUBCS(d2, x2, t2); 384176491Smarcel FPU_SUBCS(d1, x1, t1); 385176491Smarcel FPU_SUBC(d0, x0, t0); 386176491Smarcel ODD_DOUBLE; 387176491Smarcel if ((int)d0 >= 0) { 388176491Smarcel x0 = d0, x1 = d1, x2 = d2; 389176491Smarcel q |= bit; 390176491Smarcel y2 |= 1; 391176491Smarcel } 392176491Smarcel while ((bit >>= 1) != 0) { 393176491Smarcel EVEN_DOUBLE; 394176491Smarcel t3 = y3 | bit; 395176491Smarcel FPU_SUBS(d3, x3, t3); 396176491Smarcel FPU_SUBCS(d2, x2, t2); 397176491Smarcel FPU_SUBCS(d1, x1, t1); 398176491Smarcel FPU_SUBC(d0, x0, t0); 399176491Smarcel if ((int)d0 >= 0) { 400176491Smarcel x0 = d0, x1 = d1, x2 = d2; 401176491Smarcel q |= bit; 402176491Smarcel y3 |= bit << 1; 403176491Smarcel } 404176491Smarcel ODD_DOUBLE; 405176491Smarcel } 406176491Smarcel x->fp_mant[3] = q; 407176491Smarcel 408176491Smarcel /* 409176491Smarcel * The result, which includes guard and round bits, is exact iff 410176491Smarcel * x is now zero; any nonzero bits in x represent sticky bits. 411176491Smarcel */ 412176491Smarcel x->fp_sticky = x0 | x1 | x2 | x3; 413176491Smarcel DUMPFPN(FPE_REG, x); 414176491Smarcel return (x); 415176491Smarcel} 416