155112Storek /*
2*63319Sbostic * Copyright (c) 1992, 1993
3*63319Sbostic * The Regents of the University of California. All rights reserved.
455112Storek *
555112Storek * This software was developed by the Computer Systems Engineering group
655112Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
755112Storek * contributed to Berkeley.
855112Storek *
955500Sbostic * All advertising materials mentioning features or use of this software
1055500Sbostic * must display the following acknowledgement:
1155500Sbostic * This product includes software developed by the University of
1259195Storek * California, Lawrence Berkeley Laboratory.
1355500Sbostic *
1455112Storek * %sccs.include.redist.c%
1555112Storek *
16*63319Sbostic * @(#)fpu_div.c 8.1 (Berkeley) 06/11/93
1755112Storek *
1859195Storek * from: $Header: fpu_div.c,v 1.3 92/11/26 01:39:47 torek Exp $
1955112Storek */
2055112Storek
2155112Storek /*
2255112Storek * Perform an FPU divide (return x / y).
2355112Storek */
2455112Storek
2556537Sbostic #include <sys/types.h>
2655112Storek
2756537Sbostic #include <machine/reg.h>
2855112Storek
2956537Sbostic #include <sparc/fpu/fpu_arith.h>
3056537Sbostic #include <sparc/fpu/fpu_emu.h>
3155112Storek
3255112Storek /*
3355112Storek * Division of normal numbers is done as follows:
3455112Storek *
3555112Storek * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
3655112Storek * If X and Y are the mantissas (1.bbbb's), the quotient is then:
3755112Storek *
3855112Storek * q = (X / Y) * 2^((x exponent) - (y exponent))
3955112Storek *
4055112Storek * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
4155112Storek * will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only
4255112Storek * if X < Y. In that case, it will have to be shifted left one bit to
4355112Storek * become a normal number, and the exponent decremented. Thus, the
4455112Storek * desired exponent is:
4555112Storek *
4655112Storek * left_shift = x->fp_mant < y->fp_mant;
4755112Storek * result_exp = x->fp_exp - y->fp_exp - left_shift;
4855112Storek *
4955112Storek * The quotient mantissa X/Y can then be computed one bit at a time
5055112Storek * using the following algorithm:
5155112Storek *
5255112Storek * Q = 0; -- Initial quotient.
5355112Storek * R = X; -- Initial remainder,
5455112Storek * if (left_shift) -- but fixed up in advance.
5555112Storek * R *= 2;
5655112Storek * for (bit = FP_NMANT; --bit >= 0; R *= 2) {
5755112Storek * if (R >= Y) {
5855112Storek * Q |= 1 << bit;
5955112Storek * R -= Y;
6055112Storek * }
6155112Storek * }
6255112Storek *
6355112Storek * The subtraction R -= Y always removes the uppermost bit from R (and
6455112Storek * can sometimes remove additional lower-order 1 bits); this proof is
6555112Storek * left to the reader.
6655112Storek *
6755112Storek * This loop correctly calculates the guard and round bits since they are
6855112Storek * included in the expanded internal representation. The sticky bit
6955112Storek * is to be set if and only if any other bits beyond guard and round
7055112Storek * would be set. From the above it is obvious that this is true if and
7155112Storek * only if the remainder R is nonzero when the loop terminates.
7255112Storek *
7355112Storek * Examining the loop above, we can see that the quotient Q is built
7455112Storek * one bit at a time ``from the top down''. This means that we can
7555112Storek * dispense with the multi-word arithmetic and just build it one word
7655112Storek * at a time, writing each result word when it is done.
7755112Storek *
7855112Storek * Furthermore, since X and Y are both in [1.0,2.0), we know that,
7955112Storek * initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and
8055112Storek * is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1
8155112Storek * set, and R can be set initially to either X - Y (when X >= Y) or
8255112Storek * 2X - Y (when X < Y). In addition, comparing R and Y is difficult,
8355112Storek * so we will simply calculate R - Y and see if that underflows.
8455112Storek * This leads to the following revised version of the algorithm:
8555112Storek *
8655112Storek * R = X;
8755112Storek * bit = FP_1;
8855112Storek * D = R - Y;
8955112Storek * if (D >= 0) {
9055112Storek * result_exp = x->fp_exp - y->fp_exp;
9155112Storek * R = D;
9255112Storek * q = bit;
9355112Storek * bit >>= 1;
9455112Storek * } else {
9555112Storek * result_exp = x->fp_exp - y->fp_exp - 1;
9655112Storek * q = 0;
9755112Storek * }
9855112Storek * R <<= 1;
9955112Storek * do {
10055112Storek * D = R - Y;
10155112Storek * if (D >= 0) {
10255112Storek * q |= bit;
10355112Storek * R = D;
10455112Storek * }
10555112Storek * R <<= 1;
10655112Storek * } while ((bit >>= 1) != 0);
10755112Storek * Q[0] = q;
10855112Storek * for (i = 1; i < 4; i++) {
10955112Storek * q = 0, bit = 1 << 31;
11055112Storek * do {
11155112Storek * D = R - Y;
11255112Storek * if (D >= 0) {
11355112Storek * q |= bit;
11455112Storek * R = D;
11555112Storek * }
11655112Storek * R <<= 1;
11755112Storek * } while ((bit >>= 1) != 0);
11855112Storek * Q[i] = q;
11955112Storek * }
12055112Storek *
12155112Storek * This can be refined just a bit further by moving the `R <<= 1'
12255112Storek * calculations to the front of the do-loops and eliding the first one.
12355112Storek * The process can be terminated immediately whenever R becomes 0, but
12455112Storek * this is relatively rare, and we do not bother.
12555112Storek */
12655112Storek
12755112Storek struct fpn *
fpu_div(fe)12855112Storek fpu_div(fe)
12955112Storek register struct fpemu *fe;
13055112Storek {
13155112Storek register struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
13255112Storek register u_int q, bit;
13355112Storek register u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
13455112Storek FPU_DECL_CARRY
13555112Storek
13655112Storek /*
13755112Storek * Since divide is not commutative, we cannot just use ORDER.
13855112Storek * Check either operand for NaN first; if there is at least one,
13955112Storek * order the signalling one (if only one) onto the right, then
14055112Storek * return it. Otherwise we have the following cases:
14155112Storek *
14255112Storek * Inf / Inf = NaN, plus NV exception
14355112Storek * Inf / num = Inf [i.e., return x]
14455112Storek * Inf / 0 = Inf [i.e., return x]
14555112Storek * 0 / Inf = 0 [i.e., return x]
14655112Storek * 0 / num = 0 [i.e., return x]
14755112Storek * 0 / 0 = NaN, plus NV exception
14855112Storek * num / Inf = 0
14955112Storek * num / num = num (do the divide)
15055112Storek * num / 0 = Inf, plus DZ exception
15155112Storek */
15255112Storek if (ISNAN(x) || ISNAN(y)) {
15355112Storek ORDER(x, y);
15455112Storek return (y);
15555112Storek }
15655112Storek if (ISINF(x) || ISZERO(x)) {
15755112Storek if (x->fp_class == y->fp_class)
15855112Storek return (fpu_newnan(fe));
15955112Storek return (x);
16055112Storek }
16155112Storek
16255112Storek /* all results at this point use XOR of operand signs */
16355112Storek x->fp_sign ^= y->fp_sign;
16455112Storek if (ISINF(y)) {
16555112Storek x->fp_class = FPC_ZERO;
16655112Storek return (x);
16755112Storek }
16855112Storek if (ISZERO(y)) {
16955112Storek fe->fe_cx = FSR_DZ;
17055112Storek x->fp_class = FPC_INF;
17155112Storek return (x);
17255112Storek }
17355112Storek
17455112Storek /*
17555112Storek * Macros for the divide. See comments at top for algorithm.
17655112Storek * Note that we expand R, D, and Y here.
17755112Storek */
17855112Storek
17955112Storek #define SUBTRACT /* D = R - Y */ \
18055112Storek FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
18155112Storek FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
18255112Storek
18355112Storek #define NONNEGATIVE /* D >= 0 */ \
18455112Storek ((int)d0 >= 0)
18555112Storek
18655112Storek #ifdef FPU_SHL1_BY_ADD
18755112Storek #define SHL1 /* R <<= 1 */ \
18855112Storek FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
18955112Storek FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
19055112Storek #else
19155112Storek #define SHL1 \
19255112Storek r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
19355112Storek r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
19455112Storek #endif
19555112Storek
19655112Storek #define LOOP /* do ... while (bit >>= 1) */ \
19755112Storek do { \
19855112Storek SHL1; \
19955112Storek SUBTRACT; \
20055112Storek if (NONNEGATIVE) { \
20155112Storek q |= bit; \
20255112Storek r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
20355112Storek } \
20455112Storek } while ((bit >>= 1) != 0)
20555112Storek
20655112Storek #define WORD(r, i) /* calculate r->fp_mant[i] */ \
20755112Storek q = 0; \
20855112Storek bit = 1 << 31; \
20955112Storek LOOP; \
21055112Storek (x)->fp_mant[i] = q
21155112Storek
21255112Storek /* Setup. Note that we put our result in x. */
21355112Storek r0 = x->fp_mant[0];
21455112Storek r1 = x->fp_mant[1];
21555112Storek r2 = x->fp_mant[2];
21655112Storek r3 = x->fp_mant[3];
21755112Storek y0 = y->fp_mant[0];
21855112Storek y1 = y->fp_mant[1];
21955112Storek y2 = y->fp_mant[2];
22055112Storek y3 = y->fp_mant[3];
22155112Storek
22255112Storek bit = FP_1;
22355112Storek SUBTRACT;
22455112Storek if (NONNEGATIVE) {
22555112Storek x->fp_exp -= y->fp_exp;
22655112Storek r0 = d0, r1 = d1, r2 = d2, r3 = d3;
22755112Storek q = bit;
22855112Storek bit >>= 1;
22955112Storek } else {
23055112Storek x->fp_exp -= y->fp_exp + 1;
23155112Storek q = 0;
23255112Storek }
23355112Storek LOOP;
23455112Storek x->fp_mant[0] = q;
23555112Storek WORD(x, 1);
23655112Storek WORD(x, 2);
23755112Storek WORD(x, 3);
23855112Storek x->fp_sticky = r0 | r1 | r2 | r3;
23955112Storek
24055112Storek return (x);
24155112Storek }
242