1*6244ddccSmiod /* $OpenBSD: fpu_sqrt.c,v 1.7 2024/03/29 21:02:11 miod Exp $ */
202b90beaSjason
302b90beaSjason /*
402b90beaSjason * Copyright (c) 1992, 1993
502b90beaSjason * The Regents of the University of California. All rights reserved.
602b90beaSjason *
702b90beaSjason * This software was developed by the Computer Systems Engineering group
802b90beaSjason * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
902b90beaSjason * contributed to Berkeley.
1002b90beaSjason *
1102b90beaSjason * All advertising materials mentioning features or use of this software
1202b90beaSjason * must display the following acknowledgement:
1302b90beaSjason * This product includes software developed by the University of
1402b90beaSjason * California, Lawrence Berkeley Laboratory.
1502b90beaSjason *
1602b90beaSjason * Redistribution and use in source and binary forms, with or without
1702b90beaSjason * modification, are permitted provided that the following conditions
1802b90beaSjason * are met:
1902b90beaSjason * 1. Redistributions of source code must retain the above copyright
2002b90beaSjason * notice, this list of conditions and the following disclaimer.
2102b90beaSjason * 2. Redistributions in binary form must reproduce the above copyright
2202b90beaSjason * notice, this list of conditions and the following disclaimer in the
2302b90beaSjason * documentation and/or other materials provided with the distribution.
2402b90beaSjason * 3. All advertising materials mentioning features or use of this software
2502b90beaSjason * must display the following acknowledgement:
2602b90beaSjason * This product includes software developed by the University of
2702b90beaSjason * California, Berkeley and its contributors.
2802b90beaSjason * 4. Neither the name of the University nor the names of its contributors
2902b90beaSjason * may be used to endorse or promote products derived from this software
3002b90beaSjason * without specific prior written permission.
3102b90beaSjason *
3202b90beaSjason * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3302b90beaSjason * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3402b90beaSjason * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3502b90beaSjason * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3602b90beaSjason * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3702b90beaSjason * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3802b90beaSjason * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3902b90beaSjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4002b90beaSjason * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4102b90beaSjason * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4202b90beaSjason * SUCH DAMAGE.
4302b90beaSjason *
4402b90beaSjason * @(#)fpu_sqrt.c 8.1 (Berkeley) 6/11/93
4502b90beaSjason * $NetBSD: fpu_sqrt.c,v 1.2 1994/11/20 20:52:46 deraadt Exp $
4602b90beaSjason */
4702b90beaSjason
4802b90beaSjason /*
4902b90beaSjason * Perform an FPU square root (return sqrt(x)).
5002b90beaSjason */
5102b90beaSjason
5202b90beaSjason #include <sys/types.h>
5302b90beaSjason
5402b90beaSjason #include "fpu_arith.h"
5502b90beaSjason #include "fpu_emu.h"
5602b90beaSjason #include "fpu_extern.h"
5702b90beaSjason
5802b90beaSjason /*
5902b90beaSjason * Our task is to calculate the square root of a floating point number x0.
6002b90beaSjason * This number x normally has the form:
6102b90beaSjason *
6202b90beaSjason * exp
6302b90beaSjason * x = mant * 2 (where 1 <= mant < 2 and exp is an integer)
6402b90beaSjason *
6502b90beaSjason * This can be left as it stands, or the mantissa can be doubled and the
6602b90beaSjason * exponent decremented:
6702b90beaSjason *
6802b90beaSjason * exp-1
6902b90beaSjason * x = (2 * mant) * 2 (where 2 <= 2 * mant < 4)
7002b90beaSjason *
7102b90beaSjason * If the exponent `exp' is even, the square root of the number is best
7202b90beaSjason * handled using the first form, and is by definition equal to:
7302b90beaSjason *
7402b90beaSjason * exp/2
7502b90beaSjason * sqrt(x) = sqrt(mant) * 2
7602b90beaSjason *
7702b90beaSjason * If exp is odd, on the other hand, it is convenient to use the second
7802b90beaSjason * form, giving:
7902b90beaSjason *
8002b90beaSjason * (exp-1)/2
8102b90beaSjason * sqrt(x) = sqrt(2 * mant) * 2
8202b90beaSjason *
8302b90beaSjason * In the first case, we have
8402b90beaSjason *
8502b90beaSjason * 1 <= mant < 2
8602b90beaSjason *
8702b90beaSjason * and therefore
8802b90beaSjason *
8902b90beaSjason * sqrt(1) <= sqrt(mant) < sqrt(2)
9002b90beaSjason *
9102b90beaSjason * while in the second case we have
9202b90beaSjason *
9302b90beaSjason * 2 <= 2*mant < 4
9402b90beaSjason *
9502b90beaSjason * and therefore
9602b90beaSjason *
9702b90beaSjason * sqrt(2) <= sqrt(2*mant) < sqrt(4)
9802b90beaSjason *
9902b90beaSjason * so that in any case, we are sure that
10002b90beaSjason *
10102b90beaSjason * sqrt(1) <= sqrt(n * mant) < sqrt(4), n = 1 or 2
10202b90beaSjason *
10302b90beaSjason * or
10402b90beaSjason *
10502b90beaSjason * 1 <= sqrt(n * mant) < 2, n = 1 or 2.
10602b90beaSjason *
10702b90beaSjason * This root is therefore a properly formed mantissa for a floating
10802b90beaSjason * point number. The exponent of sqrt(x) is either exp/2 or (exp-1)/2
10902b90beaSjason * as above. This leaves us with the problem of finding the square root
11002b90beaSjason * of a fixed-point number in the range [1..4).
11102b90beaSjason *
11202b90beaSjason * Though it may not be instantly obvious, the following square root
11302b90beaSjason * algorithm works for any integer x of an even number of bits, provided
11402b90beaSjason * that no overflows occur:
11502b90beaSjason *
11602b90beaSjason * let q = 0
11702b90beaSjason * for k = NBITS-1 to 0 step -1 do -- for each digit in the answer...
11802b90beaSjason * x *= 2 -- multiply by radix, for next digit
11902b90beaSjason * if x >= 2q + 2^k then -- if adding 2^k does not
12002b90beaSjason * x -= 2q + 2^k -- exceed the correct root,
12102b90beaSjason * q += 2^k -- add 2^k and adjust x
12202b90beaSjason * fi
12302b90beaSjason * done
12402b90beaSjason * sqrt = q / 2^(NBITS/2) -- (and any remainder is in x)
12502b90beaSjason *
12602b90beaSjason * If NBITS is odd (so that k is initially even), we can just add another
12702b90beaSjason * zero bit at the top of x. Doing so means that q is not going to acquire
12802b90beaSjason * a 1 bit in the first trip around the loop (since x0 < 2^NBITS). If the
12902b90beaSjason * final value in x is not needed, or can be off by a factor of 2, this is
1302c53affbSjmc * equivalent to moving the `x *= 2' step to the bottom of the loop:
13102b90beaSjason *
13202b90beaSjason * for k = NBITS-1 to 0 step -1 do if ... fi; x *= 2; done
13302b90beaSjason *
13402b90beaSjason * and the result q will then be sqrt(x0) * 2^floor(NBITS / 2).
13502b90beaSjason * (Since the algorithm is destructive on x, we will call x's initial
13602b90beaSjason * value, for which q is some power of two times its square root, x0.)
13702b90beaSjason *
13802b90beaSjason * If we insert a loop invariant y = 2q, we can then rewrite this using
13902b90beaSjason * C notation as:
14002b90beaSjason *
14102b90beaSjason * q = y = 0; x = x0;
14202b90beaSjason * for (k = NBITS; --k >= 0;) {
14302b90beaSjason * #if (NBITS is even)
14402b90beaSjason * x *= 2;
14502b90beaSjason * #endif
14602b90beaSjason * t = y + (1 << k);
14702b90beaSjason * if (x >= t) {
14802b90beaSjason * x -= t;
14902b90beaSjason * q += 1 << k;
15002b90beaSjason * y += 1 << (k + 1);
15102b90beaSjason * }
15202b90beaSjason * #if (NBITS is odd)
15302b90beaSjason * x *= 2;
15402b90beaSjason * #endif
15502b90beaSjason * }
15602b90beaSjason *
15702b90beaSjason * If x0 is fixed point, rather than an integer, we can simply alter the
15802b90beaSjason * scale factor between q and sqrt(x0). As it happens, we can easily arrange
15902b90beaSjason * for the scale factor to be 2**0 or 1, so that sqrt(x0) == q.
16002b90beaSjason *
16102b90beaSjason * In our case, however, x0 (and therefore x, y, q, and t) are multiword
16202b90beaSjason * integers, which adds some complication. But note that q is built one
16302b90beaSjason * bit at a time, from the top down, and is not used itself in the loop
16402b90beaSjason * (we use 2q as held in y instead). This means we can build our answer
16502b90beaSjason * in an integer, one word at a time, which saves a bit of work. Also,
16602b90beaSjason * since 1 << k is always a `new' bit in q, 1 << k and 1 << (k+1) are
16702b90beaSjason * `new' bits in y and we can set them with an `or' operation rather than
16802b90beaSjason * a full-blown multiword add.
16902b90beaSjason *
17002b90beaSjason * We are almost done, except for one snag. We must prove that none of our
17102b90beaSjason * intermediate calculations can overflow. We know that x0 is in [1..4)
17202b90beaSjason * and therefore the square root in q will be in [1..2), but what about x,
17302b90beaSjason * y, and t?
17402b90beaSjason *
17502b90beaSjason * We know that y = 2q at the beginning of each loop. (The relation only
17602b90beaSjason * fails temporarily while y and q are being updated.) Since q < 2, y < 4.
17702b90beaSjason * The sum in t can, in our case, be as much as y+(1<<1) = y+2 < 6, and.
17802b90beaSjason * Furthermore, we can prove with a bit of work that x never exceeds y by
17902b90beaSjason * more than 2, so that even after doubling, 0 <= x < 8. (This is left as
18002b90beaSjason * an exercise to the reader, mostly because I have become tired of working
18102b90beaSjason * on this comment.)
18202b90beaSjason *
18302b90beaSjason * If our floating point mantissas (which are of the form 1.frac) occupy
18402b90beaSjason * B+1 bits, our largest intermediary needs at most B+3 bits, or two extra.
18502b90beaSjason * In fact, we want even one more bit (for a carry, to avoid compares), or
18602b90beaSjason * three extra. There is a comment in fpu_emu.h reminding maintainers of
18702b90beaSjason * this, so we have some justification in assuming it.
18802b90beaSjason */
18902b90beaSjason struct fpn *
__fpu_sqrt(fe)19002b90beaSjason __fpu_sqrt(fe)
19102b90beaSjason struct fpemu *fe;
19202b90beaSjason {
19302b90beaSjason struct fpn *x = &fe->fe_f1;
19402b90beaSjason u_int bit, q, tt;
19502b90beaSjason u_int x0, x1, x2, x3;
19602b90beaSjason u_int y0, y1, y2, y3;
19702b90beaSjason u_int d0, d1, d2, d3;
19802b90beaSjason int e;
19902b90beaSjason
20002b90beaSjason /*
20102b90beaSjason * Take care of special cases first. In order:
20202b90beaSjason *
20302b90beaSjason * sqrt(NaN) = NaN
20402b90beaSjason * sqrt(+0) = +0
20502b90beaSjason * sqrt(-0) = -0
20602b90beaSjason * sqrt(x < 0) = NaN (including sqrt(-Inf))
20702b90beaSjason * sqrt(+Inf) = +Inf
20802b90beaSjason *
20902b90beaSjason * Then all that remains are numbers with mantissas in [1..2).
21002b90beaSjason */
21102b90beaSjason if (ISNAN(x) || ISZERO(x))
21202b90beaSjason return (x);
21302b90beaSjason if (x->fp_sign)
21402b90beaSjason return (__fpu_newnan(fe));
21502b90beaSjason if (ISINF(x))
21602b90beaSjason return (x);
21702b90beaSjason
21802b90beaSjason /*
21902b90beaSjason * Calculate result exponent. As noted above, this may involve
22002b90beaSjason * doubling the mantissa. We will also need to double x each
22102b90beaSjason * time around the loop, so we define a macro for this here, and
22202b90beaSjason * we break out the multiword mantissa.
22302b90beaSjason */
22402b90beaSjason #ifdef FPU_SHL1_BY_ADD
22502b90beaSjason #define DOUBLE_X { \
22602b90beaSjason FPU_ADDS(x3, x3, x3); FPU_ADDCS(x2, x2, x2); \
22702b90beaSjason FPU_ADDCS(x1, x1, x1); FPU_ADDC(x0, x0, x0); \
22802b90beaSjason }
22902b90beaSjason #else
23002b90beaSjason #define DOUBLE_X { \
23102b90beaSjason x0 = (x0 << 1) | (x1 >> 31); x1 = (x1 << 1) | (x2 >> 31); \
23202b90beaSjason x2 = (x2 << 1) | (x3 >> 31); x3 <<= 1; \
23302b90beaSjason }
23402b90beaSjason #endif
23502b90beaSjason #if (FP_NMANT & 1) != 0
23602b90beaSjason # define ODD_DOUBLE DOUBLE_X
23702b90beaSjason # define EVEN_DOUBLE /* nothing */
23802b90beaSjason #else
23902b90beaSjason # define ODD_DOUBLE /* nothing */
24002b90beaSjason # define EVEN_DOUBLE DOUBLE_X
24102b90beaSjason #endif
24202b90beaSjason x0 = x->fp_mant[0];
24302b90beaSjason x1 = x->fp_mant[1];
24402b90beaSjason x2 = x->fp_mant[2];
24502b90beaSjason x3 = x->fp_mant[3];
24602b90beaSjason e = x->fp_exp;
24702b90beaSjason if (e & 1) /* exponent is odd; use sqrt(2mant) */
24802b90beaSjason DOUBLE_X;
24902b90beaSjason /* THE FOLLOWING ASSUMES THAT RIGHT SHIFT DOES SIGN EXTENSION */
25002b90beaSjason x->fp_exp = e >> 1; /* calculates (e&1 ? (e-1)/2 : e/2 */
25102b90beaSjason
25202b90beaSjason /*
25302b90beaSjason * Now calculate the mantissa root. Since x is now in [1..4),
25402b90beaSjason * we know that the first trip around the loop will definitely
25502b90beaSjason * set the top bit in q, so we can do that manually and start
25602b90beaSjason * the loop at the next bit down instead. We must be sure to
25702b90beaSjason * double x correctly while doing the `known q=1.0'.
25802b90beaSjason *
25902b90beaSjason * We do this one mantissa-word at a time, as noted above, to
26061e87b28Sderaadt * save work. To avoid `(1U << 31) << 1', we also do the top bit
26102b90beaSjason * outside of each per-word loop.
26202b90beaSjason *
26302b90beaSjason * The calculation `t = y + bit' breaks down into `t0 = y0, ...,
26402b90beaSjason * t3 = y3, t? |= bit' for the appropriate word. Since the bit
26502b90beaSjason * is always a `new' one, this means that three of the `t?'s are
26602b90beaSjason * just the corresponding `y?'; we use `#define's here for this.
26702b90beaSjason * The variable `tt' holds the actual `t?' variable.
26802b90beaSjason */
26902b90beaSjason
27002b90beaSjason /* calculate q0 */
27102b90beaSjason #define t0 tt
27202b90beaSjason bit = FP_1;
27302b90beaSjason EVEN_DOUBLE;
27402b90beaSjason /* if (x >= (t0 = y0 | bit)) { */ /* always true */
27502b90beaSjason q = bit;
27602b90beaSjason x0 -= bit;
27702b90beaSjason y0 = bit << 1;
27802b90beaSjason /* } */
27902b90beaSjason ODD_DOUBLE;
28002b90beaSjason while ((bit >>= 1) != 0) { /* for remaining bits in q0 */
28102b90beaSjason EVEN_DOUBLE;
28202b90beaSjason t0 = y0 | bit; /* t = y + bit */
28302b90beaSjason if (x0 >= t0) { /* if x >= t then */
28402b90beaSjason x0 -= t0; /* x -= t */
28502b90beaSjason q |= bit; /* q += bit */
28602b90beaSjason y0 |= bit << 1; /* y += bit << 1 */
28702b90beaSjason }
28802b90beaSjason ODD_DOUBLE;
28902b90beaSjason }
29002b90beaSjason x->fp_mant[0] = q;
29102b90beaSjason #undef t0
29202b90beaSjason
29302b90beaSjason /* calculate q1. note (y0&1)==0. */
29402b90beaSjason #define t0 y0
29502b90beaSjason #define t1 tt
29602b90beaSjason q = 0;
29702b90beaSjason y1 = 0;
29861e87b28Sderaadt bit = 1U << 31;
29902b90beaSjason EVEN_DOUBLE;
30002b90beaSjason t1 = bit;
30102b90beaSjason FPU_SUBS(d1, x1, t1);
30202b90beaSjason FPU_SUBC(d0, x0, t0); /* d = x - t */
30302b90beaSjason if ((int)d0 >= 0) { /* if d >= 0 (i.e., x >= t) then */
30402b90beaSjason x0 = d0, x1 = d1; /* x -= t */
30502b90beaSjason q = bit; /* q += bit */
30602b90beaSjason y0 |= 1; /* y += bit << 1 */
30702b90beaSjason }
30802b90beaSjason ODD_DOUBLE;
30902b90beaSjason while ((bit >>= 1) != 0) { /* for remaining bits in q1 */
31002b90beaSjason EVEN_DOUBLE; /* as before */
31102b90beaSjason t1 = y1 | bit;
31202b90beaSjason FPU_SUBS(d1, x1, t1);
31302b90beaSjason FPU_SUBC(d0, x0, t0);
31402b90beaSjason if ((int)d0 >= 0) {
31502b90beaSjason x0 = d0, x1 = d1;
31602b90beaSjason q |= bit;
31702b90beaSjason y1 |= bit << 1;
31802b90beaSjason }
31902b90beaSjason ODD_DOUBLE;
32002b90beaSjason }
32102b90beaSjason x->fp_mant[1] = q;
32202b90beaSjason #undef t1
32302b90beaSjason
32402b90beaSjason /* calculate q2. note (y1&1)==0; y0 (aka t0) is fixed. */
32502b90beaSjason #define t1 y1
32602b90beaSjason #define t2 tt
32702b90beaSjason q = 0;
32802b90beaSjason y2 = 0;
32961e87b28Sderaadt bit = 1U << 31;
33002b90beaSjason EVEN_DOUBLE;
33102b90beaSjason t2 = bit;
33202b90beaSjason FPU_SUBS(d2, x2, t2);
33302b90beaSjason FPU_SUBCS(d1, x1, t1);
33402b90beaSjason FPU_SUBC(d0, x0, t0);
33502b90beaSjason if ((int)d0 >= 0) {
33602b90beaSjason x0 = d0, x1 = d1, x2 = d2;
33702b90beaSjason q |= bit;
33802b90beaSjason y1 |= 1; /* now t1, y1 are set in concrete */
33902b90beaSjason }
34002b90beaSjason ODD_DOUBLE;
34102b90beaSjason while ((bit >>= 1) != 0) {
34202b90beaSjason EVEN_DOUBLE;
34302b90beaSjason t2 = y2 | bit;
34402b90beaSjason FPU_SUBS(d2, x2, t2);
34502b90beaSjason FPU_SUBCS(d1, x1, t1);
34602b90beaSjason FPU_SUBC(d0, x0, t0);
34702b90beaSjason if ((int)d0 >= 0) {
34802b90beaSjason x0 = d0, x1 = d1, x2 = d2;
34902b90beaSjason q |= bit;
35002b90beaSjason y2 |= bit << 1;
35102b90beaSjason }
35202b90beaSjason ODD_DOUBLE;
35302b90beaSjason }
35402b90beaSjason x->fp_mant[2] = q;
35502b90beaSjason #undef t2
35602b90beaSjason
35702b90beaSjason /* calculate q3. y0, t0, y1, t1 all fixed; y2, t2, almost done. */
35802b90beaSjason #define t2 y2
35902b90beaSjason #define t3 tt
36002b90beaSjason q = 0;
36102b90beaSjason y3 = 0;
36261e87b28Sderaadt bit = 1U << 31;
36302b90beaSjason EVEN_DOUBLE;
36402b90beaSjason t3 = bit;
36502b90beaSjason FPU_SUBS(d3, x3, t3);
36602b90beaSjason FPU_SUBCS(d2, x2, t2);
36702b90beaSjason FPU_SUBCS(d1, x1, t1);
36802b90beaSjason FPU_SUBC(d0, x0, t0);
36902b90beaSjason if ((int)d0 >= 0) {
370eb400352Skettenis x0 = d0, x1 = d1, x2 = d2, x3 = d3;
37102b90beaSjason q |= bit;
37202b90beaSjason y2 |= 1;
37302b90beaSjason }
374eb400352Skettenis ODD_DOUBLE;
37502b90beaSjason while ((bit >>= 1) != 0) {
37602b90beaSjason EVEN_DOUBLE;
37702b90beaSjason t3 = y3 | bit;
37802b90beaSjason FPU_SUBS(d3, x3, t3);
37902b90beaSjason FPU_SUBCS(d2, x2, t2);
38002b90beaSjason FPU_SUBCS(d1, x1, t1);
38102b90beaSjason FPU_SUBC(d0, x0, t0);
38202b90beaSjason if ((int)d0 >= 0) {
383eb400352Skettenis x0 = d0, x1 = d1, x2 = d2, x3 = d3;
38402b90beaSjason q |= bit;
38502b90beaSjason y3 |= bit << 1;
38602b90beaSjason }
38702b90beaSjason ODD_DOUBLE;
38802b90beaSjason }
38902b90beaSjason x->fp_mant[3] = q;
39002b90beaSjason
39102b90beaSjason /*
39202b90beaSjason * The result, which includes guard and round bits, is exact iff
39302b90beaSjason * x is now zero; any nonzero bits in x represent sticky bits.
39402b90beaSjason */
39502b90beaSjason x->fp_sticky = x0 | x1 | x2 | x3;
39602b90beaSjason return (x);
39702b90beaSjason }
398