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