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