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