1*6244ddccSmiod /* $OpenBSD: fpu_div.c,v 1.5 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_div.c 8.1 (Berkeley) 6/11/93
4502b90beaSjason * $NetBSD: fpu_div.c,v 1.2 1994/11/20 20:52:38 deraadt Exp $
4602b90beaSjason */
4702b90beaSjason
4802b90beaSjason /*
4902b90beaSjason * Perform an FPU divide (return x / y).
5002b90beaSjason */
5102b90beaSjason
5202b90beaSjason #include <sys/types.h>
5302b90beaSjason
5402b90beaSjason #include <machine/fsr.h>
5502b90beaSjason
5602b90beaSjason #include "fpu_arith.h"
5702b90beaSjason #include "fpu_emu.h"
5802b90beaSjason #include "fpu_extern.h"
5902b90beaSjason
6002b90beaSjason /*
6102b90beaSjason * Division of normal numbers is done as follows:
6202b90beaSjason *
6302b90beaSjason * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
6402b90beaSjason * If X and Y are the mantissas (1.bbbb's), the quotient is then:
6502b90beaSjason *
6602b90beaSjason * q = (X / Y) * 2^((x exponent) - (y exponent))
6702b90beaSjason *
6802b90beaSjason * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
6902b90beaSjason * will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only
7002b90beaSjason * if X < Y. In that case, it will have to be shifted left one bit to
7102b90beaSjason * become a normal number, and the exponent decremented. Thus, the
7202b90beaSjason * desired exponent is:
7302b90beaSjason *
7402b90beaSjason * left_shift = x->fp_mant < y->fp_mant;
7502b90beaSjason * result_exp = x->fp_exp - y->fp_exp - left_shift;
7602b90beaSjason *
7702b90beaSjason * The quotient mantissa X/Y can then be computed one bit at a time
7802b90beaSjason * using the following algorithm:
7902b90beaSjason *
8002b90beaSjason * Q = 0; -- Initial quotient.
8102b90beaSjason * R = X; -- Initial remainder,
8202b90beaSjason * if (left_shift) -- but fixed up in advance.
8302b90beaSjason * R *= 2;
8402b90beaSjason * for (bit = FP_NMANT; --bit >= 0; R *= 2) {
8502b90beaSjason * if (R >= Y) {
8602b90beaSjason * Q |= 1 << bit;
8702b90beaSjason * R -= Y;
8802b90beaSjason * }
8902b90beaSjason * }
9002b90beaSjason *
9102b90beaSjason * The subtraction R -= Y always removes the uppermost bit from R (and
9202b90beaSjason * can sometimes remove additional lower-order 1 bits); this proof is
9302b90beaSjason * left to the reader.
9402b90beaSjason *
9502b90beaSjason * This loop correctly calculates the guard and round bits since they are
9602b90beaSjason * included in the expanded internal representation. The sticky bit
9702b90beaSjason * is to be set if and only if any other bits beyond guard and round
9802b90beaSjason * would be set. From the above it is obvious that this is true if and
9902b90beaSjason * only if the remainder R is nonzero when the loop terminates.
10002b90beaSjason *
10102b90beaSjason * Examining the loop above, we can see that the quotient Q is built
10202b90beaSjason * one bit at a time ``from the top down''. This means that we can
10302b90beaSjason * dispense with the multi-word arithmetic and just build it one word
10402b90beaSjason * at a time, writing each result word when it is done.
10502b90beaSjason *
10602b90beaSjason * Furthermore, since X and Y are both in [1.0,2.0), we know that,
10702b90beaSjason * initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and
10802b90beaSjason * is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1
10902b90beaSjason * set, and R can be set initially to either X - Y (when X >= Y) or
11002b90beaSjason * 2X - Y (when X < Y). In addition, comparing R and Y is difficult,
11102b90beaSjason * so we will simply calculate R - Y and see if that underflows.
11202b90beaSjason * This leads to the following revised version of the algorithm:
11302b90beaSjason *
11402b90beaSjason * R = X;
11502b90beaSjason * bit = FP_1;
11602b90beaSjason * D = R - Y;
11702b90beaSjason * if (D >= 0) {
11802b90beaSjason * result_exp = x->fp_exp - y->fp_exp;
11902b90beaSjason * R = D;
12002b90beaSjason * q = bit;
12102b90beaSjason * bit >>= 1;
12202b90beaSjason * } else {
12302b90beaSjason * result_exp = x->fp_exp - y->fp_exp - 1;
12402b90beaSjason * q = 0;
12502b90beaSjason * }
12602b90beaSjason * R <<= 1;
12702b90beaSjason * do {
12802b90beaSjason * D = R - Y;
12902b90beaSjason * if (D >= 0) {
13002b90beaSjason * q |= bit;
13102b90beaSjason * R = D;
13202b90beaSjason * }
13302b90beaSjason * R <<= 1;
13402b90beaSjason * } while ((bit >>= 1) != 0);
13502b90beaSjason * Q[0] = q;
13602b90beaSjason * for (i = 1; i < 4; i++) {
13761e87b28Sderaadt * q = 0, bit = 1U << 31;
13802b90beaSjason * do {
13902b90beaSjason * D = R - Y;
14002b90beaSjason * if (D >= 0) {
14102b90beaSjason * q |= bit;
14202b90beaSjason * R = D;
14302b90beaSjason * }
14402b90beaSjason * R <<= 1;
14502b90beaSjason * } while ((bit >>= 1) != 0);
14602b90beaSjason * Q[i] = q;
14702b90beaSjason * }
14802b90beaSjason *
14902b90beaSjason * This can be refined just a bit further by moving the `R <<= 1'
15002b90beaSjason * calculations to the front of the do-loops and eliding the first one.
15102b90beaSjason * The process can be terminated immediately whenever R becomes 0, but
15202b90beaSjason * this is relatively rare, and we do not bother.
15302b90beaSjason */
15402b90beaSjason
15502b90beaSjason struct fpn *
__fpu_div(fe)15602b90beaSjason __fpu_div(fe)
15702b90beaSjason struct fpemu *fe;
15802b90beaSjason {
15902b90beaSjason struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
16002b90beaSjason u_int q, bit;
16102b90beaSjason u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
16202b90beaSjason FPU_DECL_CARRY
16302b90beaSjason
16402b90beaSjason /*
16502b90beaSjason * Since divide is not commutative, we cannot just use ORDER.
16602b90beaSjason * Check either operand for NaN first; if there is at least one,
16702b90beaSjason * order the signalling one (if only one) onto the right, then
16802b90beaSjason * return it. Otherwise we have the following cases:
16902b90beaSjason *
17002b90beaSjason * Inf / Inf = NaN, plus NV exception
17102b90beaSjason * Inf / num = Inf [i.e., return x]
17202b90beaSjason * Inf / 0 = Inf [i.e., return x]
17302b90beaSjason * 0 / Inf = 0 [i.e., return x]
17402b90beaSjason * 0 / num = 0 [i.e., return x]
17502b90beaSjason * 0 / 0 = NaN, plus NV exception
17602b90beaSjason * num / Inf = 0
17702b90beaSjason * num / num = num (do the divide)
17802b90beaSjason * num / 0 = Inf, plus DZ exception
17902b90beaSjason */
18002b90beaSjason if (ISNAN(x) || ISNAN(y)) {
18102b90beaSjason ORDER(x, y);
18202b90beaSjason return (y);
18302b90beaSjason }
18402b90beaSjason if (ISINF(x) || ISZERO(x)) {
18502b90beaSjason if (x->fp_class == y->fp_class)
18602b90beaSjason return (__fpu_newnan(fe));
18702b90beaSjason return (x);
18802b90beaSjason }
18902b90beaSjason
19002b90beaSjason /* all results at this point use XOR of operand signs */
19102b90beaSjason x->fp_sign ^= y->fp_sign;
19202b90beaSjason if (ISINF(y)) {
19302b90beaSjason x->fp_class = FPC_ZERO;
19402b90beaSjason return (x);
19502b90beaSjason }
19602b90beaSjason if (ISZERO(y)) {
19702b90beaSjason fe->fe_cx = FSR_DZ;
19802b90beaSjason x->fp_class = FPC_INF;
19902b90beaSjason return (x);
20002b90beaSjason }
20102b90beaSjason
20202b90beaSjason /*
20302b90beaSjason * Macros for the divide. See comments at top for algorithm.
20402b90beaSjason * Note that we expand R, D, and Y here.
20502b90beaSjason */
20602b90beaSjason
20702b90beaSjason #define SUBTRACT /* D = R - Y */ \
20802b90beaSjason FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
20902b90beaSjason FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
21002b90beaSjason
21102b90beaSjason #define NONNEGATIVE /* D >= 0 */ \
21202b90beaSjason ((int)d0 >= 0)
21302b90beaSjason
21402b90beaSjason #ifdef FPU_SHL1_BY_ADD
21502b90beaSjason #define SHL1 /* R <<= 1 */ \
21602b90beaSjason FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
21702b90beaSjason FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
21802b90beaSjason #else
21902b90beaSjason #define SHL1 \
22002b90beaSjason r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
22102b90beaSjason r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
22202b90beaSjason #endif
22302b90beaSjason
22402b90beaSjason #define LOOP /* do ... while (bit >>= 1) */ \
22502b90beaSjason do { \
22602b90beaSjason SHL1; \
22702b90beaSjason SUBTRACT; \
22802b90beaSjason if (NONNEGATIVE) { \
22902b90beaSjason q |= bit; \
23002b90beaSjason r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
23102b90beaSjason } \
23202b90beaSjason } while ((bit >>= 1) != 0)
23302b90beaSjason
23402b90beaSjason #define WORD(r, i) /* calculate r->fp_mant[i] */ \
23502b90beaSjason q = 0; \
23661e87b28Sderaadt bit = 1U << 31; \
23702b90beaSjason LOOP; \
23802b90beaSjason (x)->fp_mant[i] = q
23902b90beaSjason
24002b90beaSjason /* Setup. Note that we put our result in x. */
24102b90beaSjason r0 = x->fp_mant[0];
24202b90beaSjason r1 = x->fp_mant[1];
24302b90beaSjason r2 = x->fp_mant[2];
24402b90beaSjason r3 = x->fp_mant[3];
24502b90beaSjason y0 = y->fp_mant[0];
24602b90beaSjason y1 = y->fp_mant[1];
24702b90beaSjason y2 = y->fp_mant[2];
24802b90beaSjason y3 = y->fp_mant[3];
24902b90beaSjason
25002b90beaSjason bit = FP_1;
25102b90beaSjason SUBTRACT;
25202b90beaSjason if (NONNEGATIVE) {
25302b90beaSjason x->fp_exp -= y->fp_exp;
25402b90beaSjason r0 = d0, r1 = d1, r2 = d2, r3 = d3;
25502b90beaSjason q = bit;
25602b90beaSjason bit >>= 1;
25702b90beaSjason } else {
25802b90beaSjason x->fp_exp -= y->fp_exp + 1;
25902b90beaSjason q = 0;
26002b90beaSjason }
26102b90beaSjason LOOP;
26202b90beaSjason x->fp_mant[0] = q;
26302b90beaSjason WORD(x, 1);
26402b90beaSjason WORD(x, 2);
26502b90beaSjason WORD(x, 3);
26602b90beaSjason x->fp_sticky = r0 | r1 | r2 | r3;
26702b90beaSjason
26802b90beaSjason return (x);
26902b90beaSjason }
270