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