191174Stmm/*
291174Stmm * Copyright (c) 1992, 1993
391174Stmm *	The Regents of the University of California.  All rights reserved.
491174Stmm *
591174Stmm * This software was developed by the Computer Systems Engineering group
691174Stmm * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
791174Stmm * contributed to Berkeley.
891174Stmm *
991174Stmm * All advertising materials mentioning features or use of this software
1091174Stmm * must display the following acknowledgement:
1191174Stmm *	This product includes software developed by the University of
1291174Stmm *	California, Lawrence Berkeley Laboratory.
1391174Stmm *
1491174Stmm * Redistribution and use in source and binary forms, with or without
1591174Stmm * modification, are permitted provided that the following conditions
1691174Stmm * are met:
1791174Stmm * 1. Redistributions of source code must retain the above copyright
1891174Stmm *    notice, this list of conditions and the following disclaimer.
1991174Stmm * 2. Redistributions in binary form must reproduce the above copyright
2091174Stmm *    notice, this list of conditions and the following disclaimer in the
2191174Stmm *    documentation and/or other materials provided with the distribution.
2291174Stmm * 4. Neither the name of the University nor the names of its contributors
2391174Stmm *    may be used to endorse or promote products derived from this software
2491174Stmm *    without specific prior written permission.
2591174Stmm *
2691174Stmm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2791174Stmm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2891174Stmm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2991174Stmm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3091174Stmm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3191174Stmm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3291174Stmm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3391174Stmm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3491174Stmm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3591174Stmm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3691174Stmm * SUCH DAMAGE.
3791174Stmm *
3891174Stmm *	@(#)fpu_div.c	8.1 (Berkeley) 6/11/93
3992986Sobrien *	$NetBSD: fpu_div.c,v 1.2 1994/11/20 20:52:38 deraadt Exp $
4091174Stmm */
4191174Stmm
4292986Sobrien#include <sys/cdefs.h>
4392986Sobrien__FBSDID("$FreeBSD$");
4492986Sobrien
4591174Stmm/*
4691174Stmm * Perform an FPU divide (return x / y).
4791174Stmm */
4891174Stmm
4991174Stmm#include <sys/types.h>
5091174Stmm
5191174Stmm#include <machine/frame.h>
5291174Stmm#include <machine/fp.h>
5391174Stmm#include <machine/fsr.h>
5491174Stmm
5591174Stmm#include "fpu_arith.h"
5691174Stmm#include "fpu_emu.h"
5791174Stmm#include "fpu_extern.h"
5891174Stmm
5991174Stmm/*
6091174Stmm * Division of normal numbers is done as follows:
6191174Stmm *
6291174Stmm * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
6391174Stmm * If X and Y are the mantissas (1.bbbb's), the quotient is then:
6491174Stmm *
6591174Stmm *	q = (X / Y) * 2^((x exponent) - (y exponent))
6691174Stmm *
6791174Stmm * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
6891174Stmm * will be in [0.5,2.0).  Moreover, it will be less than 1.0 if and only
6991174Stmm * if X < Y.  In that case, it will have to be shifted left one bit to
7091174Stmm * become a normal number, and the exponent decremented.  Thus, the
7191174Stmm * desired exponent is:
7291174Stmm *
7391174Stmm *	left_shift = x->fp_mant < y->fp_mant;
7491174Stmm *	result_exp = x->fp_exp - y->fp_exp - left_shift;
7591174Stmm *
7691174Stmm * The quotient mantissa X/Y can then be computed one bit at a time
7791174Stmm * using the following algorithm:
7891174Stmm *
7991174Stmm *	Q = 0;			-- Initial quotient.
8091174Stmm *	R = X;			-- Initial remainder,
8191174Stmm *	if (left_shift)		--   but fixed up in advance.
8291174Stmm *		R *= 2;
8391174Stmm *	for (bit = FP_NMANT; --bit >= 0; R *= 2) {
8491174Stmm *		if (R >= Y) {
8591174Stmm *			Q |= 1 << bit;
8691174Stmm *			R -= Y;
8791174Stmm *		}
8891174Stmm *	}
8991174Stmm *
9091174Stmm * The subtraction R -= Y always removes the uppermost bit from R (and
9191174Stmm * can sometimes remove additional lower-order 1 bits); this proof is
9291174Stmm * left to the reader.
9391174Stmm *
9491174Stmm * This loop correctly calculates the guard and round bits since they are
9591174Stmm * included in the expanded internal representation.  The sticky bit
9691174Stmm * is to be set if and only if any other bits beyond guard and round
9791174Stmm * would be set.  From the above it is obvious that this is true if and
9891174Stmm * only if the remainder R is nonzero when the loop terminates.
9991174Stmm *
10091174Stmm * Examining the loop above, we can see that the quotient Q is built
10191174Stmm * one bit at a time ``from the top down''.  This means that we can
10291174Stmm * dispense with the multi-word arithmetic and just build it one word
10391174Stmm * at a time, writing each result word when it is done.
10491174Stmm *
10591174Stmm * Furthermore, since X and Y are both in [1.0,2.0), we know that,
10691174Stmm * initially, R >= Y.  (Recall that, if X < Y, R is set to X * 2 and
10791174Stmm * is therefore at in [2.0,4.0).)  Thus Q is sure to have bit FP_NMANT-1
10891174Stmm * set, and R can be set initially to either X - Y (when X >= Y) or
10991174Stmm * 2X - Y (when X < Y).  In addition, comparing R and Y is difficult,
11091174Stmm * so we will simply calculate R - Y and see if that underflows.
11191174Stmm * This leads to the following revised version of the algorithm:
11291174Stmm *
11391174Stmm *	R = X;
11491174Stmm *	bit = FP_1;
11591174Stmm *	D = R - Y;
11691174Stmm *	if (D >= 0) {
11791174Stmm *		result_exp = x->fp_exp - y->fp_exp;
11891174Stmm *		R = D;
11991174Stmm *		q = bit;
12091174Stmm *		bit >>= 1;
12191174Stmm *	} else {
12291174Stmm *		result_exp = x->fp_exp - y->fp_exp - 1;
12391174Stmm *		q = 0;
12491174Stmm *	}
12591174Stmm *	R <<= 1;
12691174Stmm *	do  {
12791174Stmm *		D = R - Y;
12891174Stmm *		if (D >= 0) {
12991174Stmm *			q |= bit;
13091174Stmm *			R = D;
13191174Stmm *		}
13291174Stmm *		R <<= 1;
13391174Stmm *	} while ((bit >>= 1) != 0);
13491174Stmm *	Q[0] = q;
13591174Stmm *	for (i = 1; i < 4; i++) {
13691174Stmm *		q = 0, bit = 1 << 31;
13791174Stmm *		do {
13891174Stmm *			D = R - Y;
13991174Stmm *			if (D >= 0) {
14091174Stmm *				q |= bit;
14191174Stmm *				R = D;
14291174Stmm *			}
14391174Stmm *			R <<= 1;
14491174Stmm *		} while ((bit >>= 1) != 0);
14591174Stmm *		Q[i] = q;
14691174Stmm *	}
14791174Stmm *
14891174Stmm * This can be refined just a bit further by moving the `R <<= 1'
14991174Stmm * calculations to the front of the do-loops and eliding the first one.
15091174Stmm * The process can be terminated immediately whenever R becomes 0, but
15191174Stmm * this is relatively rare, and we do not bother.
15291174Stmm */
15391174Stmm
15491174Stmmstruct fpn *
15591174Stmm__fpu_div(fe)
15692889Sobrien	struct fpemu *fe;
15791174Stmm{
15892889Sobrien	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
15992889Sobrien	u_int q, bit;
16092889Sobrien	u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
16191174Stmm	FPU_DECL_CARRY
16291174Stmm
16391174Stmm	/*
16491174Stmm	 * Since divide is not commutative, we cannot just use ORDER.
16591174Stmm	 * Check either operand for NaN first; if there is at least one,
16691174Stmm	 * order the signalling one (if only one) onto the right, then
16791174Stmm	 * return it.  Otherwise we have the following cases:
16891174Stmm	 *
16991174Stmm	 *	Inf / Inf = NaN, plus NV exception
170205396Smarius	 *	Inf / num = Inf [i.e., return x #]
171205396Smarius	 *	Inf / 0   = Inf [i.e., return x #]
172205396Smarius	 *	0 / Inf = 0 [i.e., return x #]
173205396Smarius	 *	0 / num = 0 [i.e., return x #]
17491174Stmm	 *	0 / 0   = NaN, plus NV exception
175205396Smarius	 *	num / Inf = 0 #
17691174Stmm	 *	num / num = num (do the divide)
177205396Smarius	 *	num / 0   = Inf #, plus DZ exception
178205396Smarius	 *
179205396Smarius	 * # Sign of result is XOR of operand signs.
18091174Stmm	 */
18191174Stmm	if (ISNAN(x) || ISNAN(y)) {
18291174Stmm		ORDER(x, y);
18391174Stmm		return (y);
18491174Stmm	}
18591174Stmm	if (ISINF(x) || ISZERO(x)) {
18691174Stmm		if (x->fp_class == y->fp_class)
18791174Stmm			return (__fpu_newnan(fe));
188205396Smarius		x->fp_sign ^= y->fp_sign;
18991174Stmm		return (x);
19091174Stmm	}
19191174Stmm
19291174Stmm	x->fp_sign ^= y->fp_sign;
19391174Stmm	if (ISINF(y)) {
19491174Stmm		x->fp_class = FPC_ZERO;
19591174Stmm		return (x);
19691174Stmm	}
19791174Stmm	if (ISZERO(y)) {
19891174Stmm		fe->fe_cx = FSR_DZ;
19991174Stmm		x->fp_class = FPC_INF;
20091174Stmm		return (x);
20191174Stmm	}
20291174Stmm
20391174Stmm	/*
20491174Stmm	 * Macros for the divide.  See comments at top for algorithm.
20591174Stmm	 * Note that we expand R, D, and Y here.
20691174Stmm	 */
20791174Stmm
20891174Stmm#define	SUBTRACT		/* D = R - Y */ \
20991174Stmm	FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
21091174Stmm	FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
21191174Stmm
21291174Stmm#define	NONNEGATIVE		/* D >= 0 */ \
21391174Stmm	((int)d0 >= 0)
21491174Stmm
21591174Stmm#ifdef FPU_SHL1_BY_ADD
21691174Stmm#define	SHL1			/* R <<= 1 */ \
21791174Stmm	FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
21891174Stmm	FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
21991174Stmm#else
22091174Stmm#define	SHL1 \
22191174Stmm	r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
22291174Stmm	r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
22391174Stmm#endif
22491174Stmm
22591174Stmm#define	LOOP			/* do ... while (bit >>= 1) */ \
22691174Stmm	do { \
22791174Stmm		SHL1; \
22891174Stmm		SUBTRACT; \
22991174Stmm		if (NONNEGATIVE) { \
23091174Stmm			q |= bit; \
23191174Stmm			r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
23291174Stmm		} \
23391174Stmm	} while ((bit >>= 1) != 0)
23491174Stmm
23591174Stmm#define	WORD(r, i)			/* calculate r->fp_mant[i] */ \
23691174Stmm	q = 0; \
23791174Stmm	bit = 1 << 31; \
23891174Stmm	LOOP; \
23991174Stmm	(x)->fp_mant[i] = q
24091174Stmm
24191174Stmm	/* Setup.  Note that we put our result in x. */
24291174Stmm	r0 = x->fp_mant[0];
24391174Stmm	r1 = x->fp_mant[1];
24491174Stmm	r2 = x->fp_mant[2];
24591174Stmm	r3 = x->fp_mant[3];
24691174Stmm	y0 = y->fp_mant[0];
24791174Stmm	y1 = y->fp_mant[1];
24891174Stmm	y2 = y->fp_mant[2];
24991174Stmm	y3 = y->fp_mant[3];
25091174Stmm
25191174Stmm	bit = FP_1;
25291174Stmm	SUBTRACT;
25391174Stmm	if (NONNEGATIVE) {
25491174Stmm		x->fp_exp -= y->fp_exp;
25591174Stmm		r0 = d0, r1 = d1, r2 = d2, r3 = d3;
25691174Stmm		q = bit;
25791174Stmm		bit >>= 1;
25891174Stmm	} else {
25991174Stmm		x->fp_exp -= y->fp_exp + 1;
26091174Stmm		q = 0;
26191174Stmm	}
26291174Stmm	LOOP;
26391174Stmm	x->fp_mant[0] = q;
26491174Stmm	WORD(x, 1);
26591174Stmm	WORD(x, 2);
26691174Stmm	WORD(x, 3);
26791174Stmm	x->fp_sticky = r0 | r1 | r2 | r3;
26891174Stmm
26991174Stmm	return (x);
27091174Stmm}
271