1*4e951a73Srin /* $NetBSD: fpu_div.c,v 1.9 2022/09/06 23:04:08 rin Exp $ */
218b2f7e6Ssimonb
318b2f7e6Ssimonb /*
418b2f7e6Ssimonb * Copyright (c) 1992, 1993
518b2f7e6Ssimonb * The Regents of the University of California. All rights reserved.
618b2f7e6Ssimonb *
718b2f7e6Ssimonb * This software was developed by the Computer Systems Engineering group
818b2f7e6Ssimonb * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
918b2f7e6Ssimonb * contributed to Berkeley.
1018b2f7e6Ssimonb *
1118b2f7e6Ssimonb * All advertising materials mentioning features or use of this software
1218b2f7e6Ssimonb * must display the following acknowledgement:
1318b2f7e6Ssimonb * This product includes software developed by the University of
1418b2f7e6Ssimonb * California, Lawrence Berkeley Laboratory.
1518b2f7e6Ssimonb *
1618b2f7e6Ssimonb * Redistribution and use in source and binary forms, with or without
1718b2f7e6Ssimonb * modification, are permitted provided that the following conditions
1818b2f7e6Ssimonb * are met:
1918b2f7e6Ssimonb * 1. Redistributions of source code must retain the above copyright
2018b2f7e6Ssimonb * notice, this list of conditions and the following disclaimer.
2118b2f7e6Ssimonb * 2. Redistributions in binary form must reproduce the above copyright
2218b2f7e6Ssimonb * notice, this list of conditions and the following disclaimer in the
2318b2f7e6Ssimonb * documentation and/or other materials provided with the distribution.
24aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
2518b2f7e6Ssimonb * may be used to endorse or promote products derived from this software
2618b2f7e6Ssimonb * without specific prior written permission.
2718b2f7e6Ssimonb *
2818b2f7e6Ssimonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2918b2f7e6Ssimonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3018b2f7e6Ssimonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3118b2f7e6Ssimonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3218b2f7e6Ssimonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3318b2f7e6Ssimonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3418b2f7e6Ssimonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3518b2f7e6Ssimonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3618b2f7e6Ssimonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3718b2f7e6Ssimonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3818b2f7e6Ssimonb * SUCH DAMAGE.
3918b2f7e6Ssimonb *
4018b2f7e6Ssimonb * @(#)fpu_div.c 8.1 (Berkeley) 6/11/93
4118b2f7e6Ssimonb */
4218b2f7e6Ssimonb
4318b2f7e6Ssimonb /*
4418b2f7e6Ssimonb * Perform an FPU divide (return x / y).
4518b2f7e6Ssimonb */
4618b2f7e6Ssimonb
47ed517291Slukem #include <sys/cdefs.h>
48*4e951a73Srin __KERNEL_RCSID(0, "$NetBSD: fpu_div.c,v 1.9 2022/09/06 23:04:08 rin Exp $");
49ed517291Slukem
5018b2f7e6Ssimonb #include <sys/types.h>
5118b2f7e6Ssimonb #if defined(DIAGNOSTIC)||defined(DEBUG)
5218b2f7e6Ssimonb #include <sys/systm.h>
5318b2f7e6Ssimonb #endif
5418b2f7e6Ssimonb
5518b2f7e6Ssimonb #include <machine/fpu.h>
5606f65540Srin #include <machine/reg.h>
5718b2f7e6Ssimonb
5818b2f7e6Ssimonb #include <powerpc/fpu/fpu_arith.h>
5918b2f7e6Ssimonb #include <powerpc/fpu/fpu_emu.h>
6018b2f7e6Ssimonb
6118b2f7e6Ssimonb /*
6218b2f7e6Ssimonb * Division of normal numbers is done as follows:
6318b2f7e6Ssimonb *
6418b2f7e6Ssimonb * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
6518b2f7e6Ssimonb * If X and Y are the mantissas (1.bbbb's), the quotient is then:
6618b2f7e6Ssimonb *
6718b2f7e6Ssimonb * q = (X / Y) * 2^((x exponent) - (y exponent))
6818b2f7e6Ssimonb *
6918b2f7e6Ssimonb * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
7018b2f7e6Ssimonb * will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only
7118b2f7e6Ssimonb * if X < Y. In that case, it will have to be shifted left one bit to
7218b2f7e6Ssimonb * become a normal number, and the exponent decremented. Thus, the
7318b2f7e6Ssimonb * desired exponent is:
7418b2f7e6Ssimonb *
7518b2f7e6Ssimonb * left_shift = x->fp_mant < y->fp_mant;
7618b2f7e6Ssimonb * result_exp = x->fp_exp - y->fp_exp - left_shift;
7718b2f7e6Ssimonb *
7818b2f7e6Ssimonb * The quotient mantissa X/Y can then be computed one bit at a time
7918b2f7e6Ssimonb * using the following algorithm:
8018b2f7e6Ssimonb *
8118b2f7e6Ssimonb * Q = 0; -- Initial quotient.
8218b2f7e6Ssimonb * R = X; -- Initial remainder,
8318b2f7e6Ssimonb * if (left_shift) -- but fixed up in advance.
8418b2f7e6Ssimonb * R *= 2;
8518b2f7e6Ssimonb * for (bit = FP_NMANT; --bit >= 0; R *= 2) {
8618b2f7e6Ssimonb * if (R >= Y) {
8718b2f7e6Ssimonb * Q |= 1 << bit;
8818b2f7e6Ssimonb * R -= Y;
8918b2f7e6Ssimonb * }
9018b2f7e6Ssimonb * }
9118b2f7e6Ssimonb *
9218b2f7e6Ssimonb * The subtraction R -= Y always removes the uppermost bit from R (and
9318b2f7e6Ssimonb * can sometimes remove additional lower-order 1 bits); this proof is
9418b2f7e6Ssimonb * left to the reader.
9518b2f7e6Ssimonb *
9618b2f7e6Ssimonb * This loop correctly calculates the guard and round bits since they are
9718b2f7e6Ssimonb * included in the expanded internal representation. The sticky bit
9818b2f7e6Ssimonb * is to be set if and only if any other bits beyond guard and round
9918b2f7e6Ssimonb * would be set. From the above it is obvious that this is true if and
10018b2f7e6Ssimonb * only if the remainder R is nonzero when the loop terminates.
10118b2f7e6Ssimonb *
10218b2f7e6Ssimonb * Examining the loop above, we can see that the quotient Q is built
10318b2f7e6Ssimonb * one bit at a time ``from the top down''. This means that we can
10418b2f7e6Ssimonb * dispense with the multi-word arithmetic and just build it one word
10518b2f7e6Ssimonb * at a time, writing each result word when it is done.
10618b2f7e6Ssimonb *
10718b2f7e6Ssimonb * Furthermore, since X and Y are both in [1.0,2.0), we know that,
10818b2f7e6Ssimonb * initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and
10918b2f7e6Ssimonb * is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1
11018b2f7e6Ssimonb * set, and R can be set initially to either X - Y (when X >= Y) or
11118b2f7e6Ssimonb * 2X - Y (when X < Y). In addition, comparing R and Y is difficult,
11218b2f7e6Ssimonb * so we will simply calculate R - Y and see if that underflows.
11318b2f7e6Ssimonb * This leads to the following revised version of the algorithm:
11418b2f7e6Ssimonb *
11518b2f7e6Ssimonb * R = X;
11618b2f7e6Ssimonb * bit = FP_1;
11718b2f7e6Ssimonb * D = R - Y;
11818b2f7e6Ssimonb * if (D >= 0) {
11918b2f7e6Ssimonb * result_exp = x->fp_exp - y->fp_exp;
12018b2f7e6Ssimonb * R = D;
12118b2f7e6Ssimonb * q = bit;
12218b2f7e6Ssimonb * bit >>= 1;
12318b2f7e6Ssimonb * } else {
12418b2f7e6Ssimonb * result_exp = x->fp_exp - y->fp_exp - 1;
12518b2f7e6Ssimonb * q = 0;
12618b2f7e6Ssimonb * }
12718b2f7e6Ssimonb * R <<= 1;
12818b2f7e6Ssimonb * do {
12918b2f7e6Ssimonb * D = R - Y;
13018b2f7e6Ssimonb * if (D >= 0) {
13118b2f7e6Ssimonb * q |= bit;
13218b2f7e6Ssimonb * R = D;
13318b2f7e6Ssimonb * }
13418b2f7e6Ssimonb * R <<= 1;
13518b2f7e6Ssimonb * } while ((bit >>= 1) != 0);
13618b2f7e6Ssimonb * Q[0] = q;
13718b2f7e6Ssimonb * for (i = 1; i < 4; i++) {
13818b2f7e6Ssimonb * q = 0, bit = 1 << 31;
13918b2f7e6Ssimonb * do {
14018b2f7e6Ssimonb * D = R - Y;
14118b2f7e6Ssimonb * if (D >= 0) {
14218b2f7e6Ssimonb * q |= bit;
14318b2f7e6Ssimonb * R = D;
14418b2f7e6Ssimonb * }
14518b2f7e6Ssimonb * R <<= 1;
14618b2f7e6Ssimonb * } while ((bit >>= 1) != 0);
14718b2f7e6Ssimonb * Q[i] = q;
14818b2f7e6Ssimonb * }
14918b2f7e6Ssimonb *
15018b2f7e6Ssimonb * This can be refined just a bit further by moving the `R <<= 1'
15118b2f7e6Ssimonb * calculations to the front of the do-loops and eliding the first one.
15218b2f7e6Ssimonb * The process can be terminated immediately whenever R becomes 0, but
15318b2f7e6Ssimonb * this is relatively rare, and we do not bother.
15418b2f7e6Ssimonb */
15518b2f7e6Ssimonb
15618b2f7e6Ssimonb struct fpn *
fpu_div(struct fpemu * fe)15718b2f7e6Ssimonb fpu_div(struct fpemu *fe)
15818b2f7e6Ssimonb {
15918b2f7e6Ssimonb struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
16018b2f7e6Ssimonb u_int q, bit;
16118b2f7e6Ssimonb u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
16218b2f7e6Ssimonb FPU_DECL_CARRY
16318b2f7e6Ssimonb
16418b2f7e6Ssimonb /*
16518b2f7e6Ssimonb * Since divide is not commutative, we cannot just use ORDER.
16618b2f7e6Ssimonb * Check either operand for NaN first; if there is at least one,
16718b2f7e6Ssimonb * order the signalling one (if only one) onto the right, then
16818b2f7e6Ssimonb * return it. Otherwise we have the following cases:
16918b2f7e6Ssimonb *
17018b2f7e6Ssimonb * Inf / Inf = NaN, plus NV exception
17118b2f7e6Ssimonb * Inf / num = Inf [i.e., return x]
17218b2f7e6Ssimonb * Inf / 0 = Inf [i.e., return x]
17318b2f7e6Ssimonb * 0 / Inf = 0 [i.e., return x]
17418b2f7e6Ssimonb * 0 / num = 0 [i.e., return x]
17518b2f7e6Ssimonb * 0 / 0 = NaN, plus NV exception
17618b2f7e6Ssimonb * num / Inf = 0
17718b2f7e6Ssimonb * num / num = num (do the divide)
17818b2f7e6Ssimonb * num / 0 = Inf, plus DZ exception
17918b2f7e6Ssimonb */
18018b2f7e6Ssimonb DPRINTF(FPE_REG, ("fpu_div:\n"));
18118b2f7e6Ssimonb DUMPFPN(FPE_REG, x);
18218b2f7e6Ssimonb DUMPFPN(FPE_REG, y);
18318b2f7e6Ssimonb DPRINTF(FPE_REG, ("=>\n"));
18418b2f7e6Ssimonb if (ISNAN(x) || ISNAN(y)) {
185e7be9b6fSrin if (ISSNAN(x) || ISSNAN(y))
18618b2f7e6Ssimonb fe->fe_cx |= FPSCR_VXSNAN;
187*4e951a73Srin if (ISNAN(x))
188*4e951a73Srin y = x;
18918b2f7e6Ssimonb DUMPFPN(FPE_REG, y);
19018b2f7e6Ssimonb return (y);
19118b2f7e6Ssimonb }
19218b2f7e6Ssimonb /*
19318b2f7e6Ssimonb * Need to split the following out cause they generate different
19418b2f7e6Ssimonb * exceptions.
19518b2f7e6Ssimonb */
19618b2f7e6Ssimonb if (ISINF(x)) {
19718b2f7e6Ssimonb if (x->fp_class == y->fp_class) {
19818b2f7e6Ssimonb fe->fe_cx |= FPSCR_VXIDI;
19918b2f7e6Ssimonb return (fpu_newnan(fe));
20018b2f7e6Ssimonb }
20118b2f7e6Ssimonb DUMPFPN(FPE_REG, x);
20218b2f7e6Ssimonb return (x);
20318b2f7e6Ssimonb }
20418b2f7e6Ssimonb if (ISZERO(x)) {
20518b2f7e6Ssimonb if (x->fp_class == y->fp_class) {
20618b2f7e6Ssimonb fe->fe_cx |= FPSCR_VXZDZ;
20718b2f7e6Ssimonb return (fpu_newnan(fe));
20818b2f7e6Ssimonb }
20918b2f7e6Ssimonb DUMPFPN(FPE_REG, x);
21018b2f7e6Ssimonb return (x);
21118b2f7e6Ssimonb }
21218b2f7e6Ssimonb
21318b2f7e6Ssimonb /* all results at this point use XOR of operand signs */
21418b2f7e6Ssimonb x->fp_sign ^= y->fp_sign;
21518b2f7e6Ssimonb if (ISINF(y)) {
21618b2f7e6Ssimonb x->fp_class = FPC_ZERO;
21718b2f7e6Ssimonb DUMPFPN(FPE_REG, x);
21818b2f7e6Ssimonb return (x);
21918b2f7e6Ssimonb }
22018b2f7e6Ssimonb if (ISZERO(y)) {
22118b2f7e6Ssimonb fe->fe_cx = FPSCR_ZX;
22218b2f7e6Ssimonb x->fp_class = FPC_INF;
22318b2f7e6Ssimonb DUMPFPN(FPE_REG, x);
22418b2f7e6Ssimonb return (x);
22518b2f7e6Ssimonb }
22618b2f7e6Ssimonb
22718b2f7e6Ssimonb /*
22818b2f7e6Ssimonb * Macros for the divide. See comments at top for algorithm.
22918b2f7e6Ssimonb * Note that we expand R, D, and Y here.
23018b2f7e6Ssimonb */
23118b2f7e6Ssimonb
23218b2f7e6Ssimonb #define SUBTRACT /* D = R - Y */ \
23318b2f7e6Ssimonb FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
23418b2f7e6Ssimonb FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
23518b2f7e6Ssimonb
23618b2f7e6Ssimonb #define NONNEGATIVE /* D >= 0 */ \
23718b2f7e6Ssimonb ((int)d0 >= 0)
23818b2f7e6Ssimonb
23918b2f7e6Ssimonb #ifdef FPU_SHL1_BY_ADD
24018b2f7e6Ssimonb #define SHL1 /* R <<= 1 */ \
24118b2f7e6Ssimonb FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
24218b2f7e6Ssimonb FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
24318b2f7e6Ssimonb #else
24418b2f7e6Ssimonb #define SHL1 \
24518b2f7e6Ssimonb r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
24618b2f7e6Ssimonb r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
24718b2f7e6Ssimonb #endif
24818b2f7e6Ssimonb
24918b2f7e6Ssimonb #define LOOP /* do ... while (bit >>= 1) */ \
25018b2f7e6Ssimonb do { \
25118b2f7e6Ssimonb SHL1; \
25218b2f7e6Ssimonb SUBTRACT; \
25318b2f7e6Ssimonb if (NONNEGATIVE) { \
25418b2f7e6Ssimonb q |= bit; \
25518b2f7e6Ssimonb r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
25618b2f7e6Ssimonb } \
25718b2f7e6Ssimonb } while ((bit >>= 1) != 0)
25818b2f7e6Ssimonb
25918b2f7e6Ssimonb #define WORD(r, i) /* calculate r->fp_mant[i] */ \
26018b2f7e6Ssimonb q = 0; \
26118b2f7e6Ssimonb bit = 1 << 31; \
26218b2f7e6Ssimonb LOOP; \
26318b2f7e6Ssimonb (x)->fp_mant[i] = q
26418b2f7e6Ssimonb
26518b2f7e6Ssimonb /* Setup. Note that we put our result in x. */
26618b2f7e6Ssimonb r0 = x->fp_mant[0];
26718b2f7e6Ssimonb r1 = x->fp_mant[1];
26818b2f7e6Ssimonb r2 = x->fp_mant[2];
26918b2f7e6Ssimonb r3 = x->fp_mant[3];
27018b2f7e6Ssimonb y0 = y->fp_mant[0];
27118b2f7e6Ssimonb y1 = y->fp_mant[1];
27218b2f7e6Ssimonb y2 = y->fp_mant[2];
27318b2f7e6Ssimonb y3 = y->fp_mant[3];
27418b2f7e6Ssimonb
27518b2f7e6Ssimonb bit = FP_1;
27618b2f7e6Ssimonb SUBTRACT;
27718b2f7e6Ssimonb if (NONNEGATIVE) {
27818b2f7e6Ssimonb x->fp_exp -= y->fp_exp;
27918b2f7e6Ssimonb r0 = d0, r1 = d1, r2 = d2, r3 = d3;
28018b2f7e6Ssimonb q = bit;
28118b2f7e6Ssimonb bit >>= 1;
28218b2f7e6Ssimonb } else {
28318b2f7e6Ssimonb x->fp_exp -= y->fp_exp + 1;
28418b2f7e6Ssimonb q = 0;
28518b2f7e6Ssimonb }
28618b2f7e6Ssimonb LOOP;
28718b2f7e6Ssimonb x->fp_mant[0] = q;
28818b2f7e6Ssimonb WORD(x, 1);
28918b2f7e6Ssimonb WORD(x, 2);
29018b2f7e6Ssimonb WORD(x, 3);
29118b2f7e6Ssimonb x->fp_sticky = r0 | r1 | r2 | r3;
29218b2f7e6Ssimonb
29318b2f7e6Ssimonb DUMPFPN(FPE_REG, x);
29418b2f7e6Ssimonb return (x);
29518b2f7e6Ssimonb }
296