1 /* 2 * Copyright (c) 1992 The Regents of the University of California. 3 * 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 * %sccs.include.redist.c% 10 * 11 * @(#)fpu_sqrt.c 7.1 (Berkeley) 07/13/92 12 * 13 * from: $Header: fpu_sqrt.c,v 1.2 92/06/17 05:41:34 torek Exp $ 14 */ 15 16 /* 17 * Perform an FPU square root (return sqrt(x)). 18 */ 19 20 #include "sys/types.h" 21 22 #include "machine/reg.h" 23 24 #include "fpu_arith.h" 25 #include "fpu_emu.h" 26 27 /* 28 * Our task is to calculate the square root of a floating point number x0. 29 * This number x normally has the form: 30 * 31 * exp 32 * x = mant * 2 (where 1 <= mant < 2 and exp is an integer) 33 * 34 * This can be left as it stands, or the mantissa can be doubled and the 35 * exponent decremented: 36 * 37 * exp-1 38 * x = (2 * mant) * 2 (where 2 <= 2 * mant < 4) 39 * 40 * If the exponent `exp' is even, the square root of the number is best 41 * handled using the first form, and is by definition equal to: 42 * 43 * exp/2 44 * sqrt(x) = sqrt(mant) * 2 45 * 46 * If exp is odd, on the other hand, it is convenient to use the second 47 * form, giving: 48 * 49 * (exp-1)/2 50 * sqrt(x) = sqrt(2 * mant) * 2 51 * 52 * In the first case, we have 53 * 54 * 1 <= mant < 2 55 * 56 * and therefore 57 * 58 * sqrt(1) <= sqrt(mant) < sqrt(2) 59 * 60 * while in the second case we have 61 * 62 * 2 <= 2*mant < 4 63 * 64 * and therefore 65 * 66 * sqrt(2) <= sqrt(2*mant) < sqrt(4) 67 * 68 * so that in any case, we are sure that 69 * 70 * sqrt(1) <= sqrt(n * mant) < sqrt(4), n = 1 or 2 71 * 72 * or 73 * 74 * 1 <= sqrt(n * mant) < 2, n = 1 or 2. 75 * 76 * This root is therefore a properly formed mantissa for a floating 77 * point number. The exponent of sqrt(x) is either exp/2 or (exp-1)/2 78 * as above. This leaves us with the problem of finding the square root 79 * of a fixed-point number in the range [1..4). 80 * 81 * Though it may not be instantly obvious, the following square root 82 * algorithm works for any integer x of an even number of bits, provided 83 * that no overflows occur: 84 * 85 * let q = 0 86 * for k = NBITS-1 to 0 step -1 do -- for each digit in the answer... 87 * x *= 2 -- multiply by radix, for next digit 88 * if x >= 2q + 2^k then -- if adding 2^k does not 89 * x -= 2q + 2^k -- exceed the correct root, 90 * q += 2^k -- add 2^k and adjust x 91 * fi 92 * done 93 * sqrt = q / 2^(NBITS/2) -- (and any remainder is in x) 94 * 95 * If NBITS is odd (so that k is initially even), we can just add another 96 * zero bit at the top of x. Doing so means that q is not going to acquire 97 * a 1 bit in the first trip around the loop (since x0 < 2^NBITS). If the 98 * final value in x is not needed, or can be off by a factor of 2, this is 99 * equivalant to moving the `x *= 2' step to the bottom of the loop: 100 * 101 * for k = NBITS-1 to 0 step -1 do if ... fi; x *= 2; done 102 * 103 * and the result q will then be sqrt(x0) * 2^floor(NBITS / 2). 104 * (Since the algorithm is destructive on x, we will call x's initial 105 * value, for which q is some power of two times its square root, x0.) 106 * 107 * If we insert a loop invariant y = 2q, we can then rewrite this using 108 * C notation as: 109 * 110 * q = y = 0; x = x0; 111 * for (k = NBITS; --k >= 0;) { 112 * #if (NBITS is even) 113 * x *= 2; 114 * #endif 115 * t = y + (1 << k); 116 * if (x >= t) { 117 * x -= t; 118 * q += 1 << k; 119 * y += 1 << (k + 1); 120 * } 121 * #if (NBITS is odd) 122 * x *= 2; 123 * #endif 124 * } 125 * 126 * If x0 is fixed point, rather than an integer, we can simply alter the 127 * scale factor between q and sqrt(x0). As it happens, we can easily arrange 128 * for the scale factor to be 2**0 or 1, so that sqrt(x0) == q. 129 * 130 * In our case, however, x0 (and therefore x, y, q, and t) are multiword 131 * integers, which adds some complication. But note that q is built one 132 * bit at a time, from the top down, and is not used itself in the loop 133 * (we use 2q as held in y instead). This means we can build our answer 134 * in an integer, one word at a time, which saves a bit of work. Also, 135 * since 1 << k is always a `new' bit in q, 1 << k and 1 << (k+1) are 136 * `new' bits in y and we can set them with an `or' operation rather than 137 * a full-blown multiword add. 138 * 139 * We are almost done, except for one snag. We must prove that none of our 140 * intermediate calculations can overflow. We know that x0 is in [1..4) 141 * and therefore the square root in q will be in [1..2), but what about x, 142 * y, and t? 143 * 144 * We know that y = 2q at the beginning of each loop. (The relation only 145 * fails temporarily while y and q are being updated.) Since q < 2, y < 4. 146 * The sum in t can, in our case, be as much as y+(1<<1) = y+2 < 6, and. 147 * Furthermore, we can prove with a bit of work that x never exceeds y by 148 * more than 2, so that even after doubling, 0 <= x < 8. (This is left as 149 * an exercise to the reader, mostly because I have become tired of working 150 * on this comment.) 151 * 152 * If our floating point mantissas (which are of the form 1.frac) occupy 153 * B+1 bits, our largest intermediary needs at most B+3 bits, or two extra. 154 * In fact, we want even one more bit (for a carry, to avoid compares), or 155 * three extra. There is a comment in fpu_emu.h reminding maintainers of 156 * this, so we have some justification in assuming it. 157 */ 158 struct fpn * 159 fpu_sqrt(fe) 160 struct fpemu *fe; 161 { 162 register struct fpn *x = &fe->fe_f1; 163 register u_int bit, q, tt; 164 register u_int x0, x1, x2, x3; 165 register u_int y0, y1, y2, y3; 166 register u_int d0, d1, d2, d3; 167 register int e; 168 169 /* 170 * Take care of special cases first. In order: 171 * 172 * sqrt(NaN) = NaN 173 * sqrt(+0) = +0 174 * sqrt(-0) = -0 175 * sqrt(x < 0) = NaN (including sqrt(-Inf)) 176 * sqrt(+Inf) = +Inf 177 * 178 * Then all that remains are numbers with mantissas in [1..2). 179 */ 180 if (ISNAN(x) || ISZERO(x)) 181 return (x); 182 if (x->fp_sign) 183 return (fpu_newnan(fe)); 184 if (ISINF(x)) 185 return (x); 186 187 /* 188 * Calculate result exponent. As noted above, this may involve 189 * doubling the mantissa. We will also need to double x each 190 * time around the loop, so we define a macro for this here, and 191 * we break out the multiword mantissa. 192 */ 193 #ifdef FPU_SHL1_BY_ADD 194 #define DOUBLE_X { \ 195 FPU_ADDS(x3, x3, x3); FPU_ADDCS(x2, x2, x2); \ 196 FPU_ADDCS(x1, x1, x1); FPU_ADDC(x0, x0, x0); \ 197 } 198 #else 199 #define DOUBLE_X { \ 200 x0 = (x0 << 1) | (x1 >> 31); x1 = (x1 << 1) | (x2 >> 31); \ 201 x2 = (x2 << 1) | (x3 >> 31); x3 <<= 1; \ 202 } 203 #endif 204 #if (FP_NMANT & 1) != 0 205 # define ODD_DOUBLE DOUBLE_X 206 # define EVEN_DOUBLE /* nothing */ 207 #else 208 # define ODD_DOUBLE /* nothing */ 209 # define EVEN_DOUBLE DOUBLE_X 210 #endif 211 x0 = x->fp_mant[0]; 212 x1 = x->fp_mant[1]; 213 x2 = x->fp_mant[2]; 214 x3 = x->fp_mant[3]; 215 e = x->fp_exp; 216 if (e & 1) /* exponent is odd; use sqrt(2mant) */ 217 DOUBLE_X; 218 /* THE FOLLOWING ASSUMES THAT RIGHT SHIFT DOES SIGN EXTENSION */ 219 x->fp_exp = e >> 1; /* calculates (e&1 ? (e-1)/2 : e/2 */ 220 221 /* 222 * Now calculate the mantissa root. Since x is now in [1..4), 223 * we know that the first trip around the loop will definitely 224 * set the top bit in q, so we can do that manually and start 225 * the loop at the next bit down instead. We must be sure to 226 * double x correctly while doing the `known q=1.0'. 227 * 228 * We do this one mantissa-word at a time, as noted above, to 229 * save work. To avoid `(1 << 31) << 1', we also do the top bit 230 * outside of each per-word loop. 231 * 232 * The calculation `t = y + bit' breaks down into `t0 = y0, ..., 233 * t3 = y3, t? |= bit' for the appropriate word. Since the bit 234 * is always a `new' one, this means that three of the `t?'s are 235 * just the corresponding `y?'; we use `#define's here for this. 236 * The variable `tt' holds the actual `t?' variable. 237 */ 238 239 /* calculate q0 */ 240 #define t0 tt 241 bit = FP_1; 242 EVEN_DOUBLE; 243 /* if (x >= (t0 = y0 | bit)) { */ /* always true */ 244 q = bit; 245 x0 -= bit; 246 y0 = bit << 1; 247 /* } */ 248 ODD_DOUBLE; 249 while ((bit >>= 1) != 0) { /* for remaining bits in q0 */ 250 EVEN_DOUBLE; 251 t0 = y0 | bit; /* t = y + bit */ 252 if (x0 >= t0) { /* if x >= t then */ 253 x0 -= t0; /* x -= t */ 254 q |= bit; /* q += bit */ 255 y0 |= bit << 1; /* y += bit << 1 */ 256 } 257 ODD_DOUBLE; 258 } 259 x->fp_mant[0] = q; 260 #undef t0 261 262 /* calculate q1. note (y0&1)==0. */ 263 #define t0 y0 264 #define t1 tt 265 q = 0; 266 y1 = 0; 267 bit = 1 << 31; 268 EVEN_DOUBLE; 269 t1 = bit; 270 FPU_SUBS(d1, x1, t1); 271 FPU_SUBC(d0, x0, t0); /* d = x - t */ 272 if ((int)d0 >= 0) { /* if d >= 0 (i.e., x >= t) then */ 273 x0 = d0, x1 = d1; /* x -= t */ 274 q = bit; /* q += bit */ 275 y0 |= 1; /* y += bit << 1 */ 276 } 277 ODD_DOUBLE; 278 while ((bit >>= 1) != 0) { /* for remaining bits in q1 */ 279 EVEN_DOUBLE; /* as before */ 280 t1 = y1 | bit; 281 FPU_SUBS(d1, x1, t1); 282 FPU_SUBC(d0, x0, t0); 283 if ((int)d0 >= 0) { 284 x0 = d0, x1 = d1; 285 q |= bit; 286 y1 |= bit << 1; 287 } 288 ODD_DOUBLE; 289 } 290 x->fp_mant[1] = q; 291 #undef t1 292 293 /* calculate q2. note (y1&1)==0; y0 (aka t0) is fixed. */ 294 #define t1 y1 295 #define t2 tt 296 q = 0; 297 y2 = 0; 298 bit = 1 << 31; 299 EVEN_DOUBLE; 300 t2 = bit; 301 FPU_SUBS(d2, x2, t2); 302 FPU_SUBCS(d1, x1, t1); 303 FPU_SUBC(d0, x0, t0); 304 if ((int)d0 >= 0) { 305 x0 = d0, x1 = d1, x2 = d2; 306 q |= bit; 307 y1 |= 1; /* now t1, y1 are set in concrete */ 308 } 309 ODD_DOUBLE; 310 while ((bit >>= 1) != 0) { 311 EVEN_DOUBLE; 312 t2 = y2 | bit; 313 FPU_SUBS(d2, x2, t2); 314 FPU_SUBCS(d1, x1, t1); 315 FPU_SUBC(d0, x0, t0); 316 if ((int)d0 >= 0) { 317 x0 = d0, x1 = d1, x2 = d2; 318 q |= bit; 319 y2 |= bit << 1; 320 } 321 ODD_DOUBLE; 322 } 323 x->fp_mant[2] = q; 324 #undef t2 325 326 /* calculate q3. y0, t0, y1, t1 all fixed; y2, t2, almost done. */ 327 #define t2 y2 328 #define t3 tt 329 q = 0; 330 y3 = 0; 331 bit = 1 << 31; 332 EVEN_DOUBLE; 333 t3 = bit; 334 FPU_SUBS(d3, x3, t3); 335 FPU_SUBCS(d2, x2, t2); 336 FPU_SUBCS(d1, x1, t1); 337 FPU_SUBC(d0, x0, t0); 338 ODD_DOUBLE; 339 if ((int)d0 >= 0) { 340 x0 = d0, x1 = d1, x2 = d2; 341 q |= bit; 342 y2 |= 1; 343 } 344 while ((bit >>= 1) != 0) { 345 EVEN_DOUBLE; 346 t3 = y3 | bit; 347 FPU_SUBS(d3, x3, t3); 348 FPU_SUBCS(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 y3 |= bit << 1; 355 } 356 ODD_DOUBLE; 357 } 358 x->fp_mant[3] = q; 359 360 /* 361 * The result, which includes guard and round bits, is exact iff 362 * x is now zero; any nonzero bits in x represent sticky bits. 363 */ 364 x->fp_sticky = x0 | x1 | x2 | x3; 365 return (x); 366 } 367