Home | History | Annotate | Line # | Download | only in fpe
fpu_div.c revision 1.2.36.1
      1  1.2.36.1   skrll /*	$NetBSD: fpu_div.c,v 1.2.36.1 2004/08/03 10:36:40 skrll Exp $ */
      2       1.1  briggs 
      3       1.1  briggs /*
      4       1.1  briggs  * Copyright (c) 1992, 1993
      5       1.1  briggs  *	The Regents of the University of California.  All rights reserved.
      6       1.1  briggs  *
      7       1.1  briggs  * This software was developed by the Computer Systems Engineering group
      8       1.1  briggs  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9       1.1  briggs  * contributed to Berkeley.
     10       1.1  briggs  *
     11       1.1  briggs  * All advertising materials mentioning features or use of this software
     12       1.1  briggs  * must display the following acknowledgement:
     13       1.1  briggs  *	This product includes software developed by the University of
     14       1.1  briggs  *	California, Lawrence Berkeley Laboratory.
     15       1.1  briggs  *
     16       1.1  briggs  * Redistribution and use in source and binary forms, with or without
     17       1.1  briggs  * modification, are permitted provided that the following conditions
     18       1.1  briggs  * are met:
     19       1.1  briggs  * 1. Redistributions of source code must retain the above copyright
     20       1.1  briggs  *    notice, this list of conditions and the following disclaimer.
     21       1.1  briggs  * 2. Redistributions in binary form must reproduce the above copyright
     22       1.1  briggs  *    notice, this list of conditions and the following disclaimer in the
     23       1.1  briggs  *    documentation and/or other materials provided with the distribution.
     24  1.2.36.1   skrll  * 3. Neither the name of the University nor the names of its contributors
     25       1.1  briggs  *    may be used to endorse or promote products derived from this software
     26       1.1  briggs  *    without specific prior written permission.
     27       1.1  briggs  *
     28       1.1  briggs  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29       1.1  briggs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30       1.1  briggs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31       1.1  briggs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32       1.1  briggs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33       1.1  briggs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34       1.1  briggs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35       1.1  briggs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36       1.1  briggs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37       1.1  briggs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38       1.1  briggs  * SUCH DAMAGE.
     39       1.1  briggs  *
     40       1.1  briggs  *	@(#)fpu_div.c	8.1 (Berkeley) 6/11/93
     41       1.1  briggs  */
     42       1.1  briggs 
     43       1.1  briggs /*
     44       1.1  briggs  * Perform an FPU divide (return x / y).
     45       1.1  briggs  */
     46       1.1  briggs 
     47  1.2.36.1   skrll #include <sys/cdefs.h>
     48  1.2.36.1   skrll __KERNEL_RCSID(0, "$NetBSD: fpu_div.c,v 1.2.36.1 2004/08/03 10:36:40 skrll Exp $");
     49  1.2.36.1   skrll 
     50       1.1  briggs #include <sys/types.h>
     51       1.1  briggs 
     52       1.1  briggs #include <machine/reg.h>
     53       1.1  briggs 
     54       1.1  briggs #include "fpu_arith.h"
     55       1.1  briggs #include "fpu_emulate.h"
     56       1.1  briggs 
     57       1.1  briggs /*
     58       1.1  briggs  * Division of normal numbers is done as follows:
     59       1.1  briggs  *
     60       1.1  briggs  * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
     61       1.1  briggs  * If X and Y are the mantissas (1.bbbb's), the quotient is then:
     62       1.1  briggs  *
     63       1.1  briggs  *	q = (X / Y) * 2^((x exponent) - (y exponent))
     64       1.1  briggs  *
     65       1.1  briggs  * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
     66       1.1  briggs  * will be in [0.5,2.0).  Moreover, it will be less than 1.0 if and only
     67       1.1  briggs  * if X < Y.  In that case, it will have to be shifted left one bit to
     68       1.1  briggs  * become a normal number, and the exponent decremented.  Thus, the
     69       1.1  briggs  * desired exponent is:
     70       1.1  briggs  *
     71       1.1  briggs  *	left_shift = x->fp_mant < y->fp_mant;
     72       1.1  briggs  *	result_exp = x->fp_exp - y->fp_exp - left_shift;
     73       1.1  briggs  *
     74       1.1  briggs  * The quotient mantissa X/Y can then be computed one bit at a time
     75       1.1  briggs  * using the following algorithm:
     76       1.1  briggs  *
     77       1.1  briggs  *	Q = 0;			-- Initial quotient.
     78       1.1  briggs  *	R = X;			-- Initial remainder,
     79       1.1  briggs  *	if (left_shift)		--   but fixed up in advance.
     80       1.1  briggs  *		R *= 2;
     81       1.1  briggs  *	for (bit = FP_NMANT; --bit >= 0; R *= 2) {
     82       1.1  briggs  *		if (R >= Y) {
     83       1.1  briggs  *			Q |= 1 << bit;
     84       1.1  briggs  *			R -= Y;
     85       1.1  briggs  *		}
     86       1.1  briggs  *	}
     87       1.1  briggs  *
     88       1.1  briggs  * The subtraction R -= Y always removes the uppermost bit from R (and
     89       1.1  briggs  * can sometimes remove additional lower-order 1 bits); this proof is
     90       1.1  briggs  * left to the reader.
     91       1.1  briggs  *
     92       1.1  briggs  * This loop correctly calculates the guard and round bits since they are
     93       1.1  briggs  * included in the expanded internal representation.  The sticky bit
     94       1.1  briggs  * is to be set if and only if any other bits beyond guard and round
     95       1.1  briggs  * would be set.  From the above it is obvious that this is true if and
     96       1.1  briggs  * only if the remainder R is nonzero when the loop terminates.
     97       1.1  briggs  *
     98       1.1  briggs  * Examining the loop above, we can see that the quotient Q is built
     99       1.1  briggs  * one bit at a time ``from the top down''.  This means that we can
    100       1.1  briggs  * dispense with the multi-word arithmetic and just build it one word
    101       1.1  briggs  * at a time, writing each result word when it is done.
    102       1.1  briggs  *
    103       1.1  briggs  * Furthermore, since X and Y are both in [1.0,2.0), we know that,
    104       1.1  briggs  * initially, R >= Y.  (Recall that, if X < Y, R is set to X * 2 and
    105       1.1  briggs  * is therefore at in [2.0,4.0).)  Thus Q is sure to have bit FP_NMANT-1
    106       1.1  briggs  * set, and R can be set initially to either X - Y (when X >= Y) or
    107       1.1  briggs  * 2X - Y (when X < Y).  In addition, comparing R and Y is difficult,
    108       1.1  briggs  * so we will simply calculate R - Y and see if that underflows.
    109       1.1  briggs  * This leads to the following revised version of the algorithm:
    110       1.1  briggs  *
    111       1.1  briggs  *	R = X;
    112       1.1  briggs  *	bit = FP_1;
    113       1.1  briggs  *	D = R - Y;
    114       1.1  briggs  *	if (D >= 0) {
    115       1.1  briggs  *		result_exp = x->fp_exp - y->fp_exp;
    116       1.1  briggs  *		R = D;
    117       1.1  briggs  *		q = bit;
    118       1.1  briggs  *		bit >>= 1;
    119       1.1  briggs  *	} else {
    120       1.1  briggs  *		result_exp = x->fp_exp - y->fp_exp - 1;
    121       1.1  briggs  *		q = 0;
    122       1.1  briggs  *	}
    123       1.1  briggs  *	R <<= 1;
    124       1.1  briggs  *	do  {
    125       1.1  briggs  *		D = R - Y;
    126       1.1  briggs  *		if (D >= 0) {
    127       1.1  briggs  *			q |= bit;
    128       1.1  briggs  *			R = D;
    129       1.1  briggs  *		}
    130       1.1  briggs  *		R <<= 1;
    131       1.1  briggs  *	} while ((bit >>= 1) != 0);
    132       1.1  briggs  *	Q[0] = q;
    133       1.1  briggs  *	for (i = 1; i < 4; i++) {
    134       1.1  briggs  *		q = 0, bit = 1 << 31;
    135       1.1  briggs  *		do {
    136       1.1  briggs  *			D = R - Y;
    137       1.1  briggs  *			if (D >= 0) {
    138       1.1  briggs  *				q |= bit;
    139       1.1  briggs  *				R = D;
    140       1.1  briggs  *			}
    141       1.1  briggs  *			R <<= 1;
    142       1.1  briggs  *		} while ((bit >>= 1) != 0);
    143       1.1  briggs  *		Q[i] = q;
    144       1.1  briggs  *	}
    145       1.1  briggs  *
    146       1.1  briggs  * This can be refined just a bit further by moving the `R <<= 1'
    147       1.1  briggs  * calculations to the front of the do-loops and eliding the first one.
    148       1.1  briggs  * The process can be terminated immediately whenever R becomes 0, but
    149       1.1  briggs  * this is relatively rare, and we do not bother.
    150       1.1  briggs  */
    151       1.1  briggs 
    152       1.1  briggs struct fpn *
    153       1.1  briggs fpu_div(fe)
    154       1.1  briggs 	register struct fpemu *fe;
    155       1.1  briggs {
    156       1.1  briggs 	register struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
    157       1.1  briggs 	register u_int q, bit;
    158       1.2  briggs 	register u_int r0, r1, r2, d0, d1, d2, y0, y1, y2;
    159       1.1  briggs 	FPU_DECL_CARRY
    160       1.1  briggs 
    161       1.1  briggs 	fe->fe_fpsr &= ~FPSR_EXCP; /* clear all exceptions */
    162       1.1  briggs 
    163       1.1  briggs 	/*
    164       1.1  briggs 	 * Since divide is not commutative, we cannot just use ORDER.
    165       1.1  briggs 	 * Check either operand for NaN first; if there is at least one,
    166       1.1  briggs 	 * order the signalling one (if only one) onto the right, then
    167       1.1  briggs 	 * return it.  Otherwise we have the following cases:
    168       1.1  briggs 	 *
    169       1.1  briggs 	 *	Inf / Inf = NaN, plus NV exception
    170       1.1  briggs 	 *	Inf / num = Inf [i.e., return x]
    171       1.1  briggs 	 *	Inf / 0   = Inf [i.e., return x]
    172       1.1  briggs 	 *	0 / Inf = 0 [i.e., return x]
    173       1.1  briggs 	 *	0 / num = 0 [i.e., return x]
    174       1.1  briggs 	 *	0 / 0   = NaN, plus NV exception
    175       1.1  briggs 	 *	num / Inf = 0
    176       1.1  briggs 	 *	num / num = num (do the divide)
    177       1.1  briggs 	 *	num / 0   = Inf, plus DZ exception
    178       1.1  briggs 	 */
    179       1.1  briggs 	if (ISNAN(x) || ISNAN(y)) {
    180       1.1  briggs 		ORDER(x, y);
    181       1.1  briggs 		return (y);
    182       1.1  briggs 	}
    183       1.1  briggs 	if (ISINF(x) || ISZERO(x)) {
    184       1.1  briggs 		if (x->fp_class == y->fp_class)
    185       1.1  briggs 			return (fpu_newnan(fe));
    186       1.1  briggs 		return (x);
    187       1.1  briggs 	}
    188       1.1  briggs 
    189       1.1  briggs 	/* all results at this point use XOR of operand signs */
    190       1.1  briggs 	x->fp_sign ^= y->fp_sign;
    191       1.1  briggs 	if (ISINF(y)) {
    192       1.1  briggs 		x->fp_class = FPC_ZERO;
    193       1.1  briggs 		return (x);
    194       1.1  briggs 	}
    195       1.1  briggs 	if (ISZERO(y)) {
    196       1.1  briggs 		fe->fe_fpsr |= FPSR_DZ;
    197       1.1  briggs 		x->fp_class = FPC_INF;
    198       1.1  briggs 		return (x);
    199       1.1  briggs 	}
    200       1.1  briggs 
    201       1.1  briggs 	/*
    202       1.1  briggs 	 * Macros for the divide.  See comments at top for algorithm.
    203       1.1  briggs 	 * Note that we expand R, D, and Y here.
    204       1.1  briggs 	 */
    205       1.1  briggs 
    206       1.1  briggs #define	SUBTRACT		/* D = R - Y */ \
    207       1.2  briggs 	FPU_SUBS(d2, r2, y2); \
    208       1.1  briggs 	FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
    209       1.1  briggs 
    210       1.1  briggs #define	NONNEGATIVE		/* D >= 0 */ \
    211       1.1  briggs 	((int)d0 >= 0)
    212       1.1  briggs 
    213       1.1  briggs #ifdef FPU_SHL1_BY_ADD
    214       1.1  briggs #define	SHL1			/* R <<= 1 */ \
    215       1.2  briggs 	FPU_ADDS(r2, r2, r2); \
    216       1.1  briggs 	FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
    217       1.1  briggs #else
    218       1.1  briggs #define	SHL1 \
    219       1.1  briggs 	r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
    220       1.2  briggs 	r2 <<= 1
    221       1.1  briggs #endif
    222       1.1  briggs 
    223       1.1  briggs #define	LOOP			/* do ... while (bit >>= 1) */ \
    224       1.1  briggs 	do { \
    225       1.1  briggs 		SHL1; \
    226       1.1  briggs 		SUBTRACT; \
    227       1.1  briggs 		if (NONNEGATIVE) { \
    228       1.1  briggs 			q |= bit; \
    229       1.2  briggs 			r0 = d0, r1 = d1, r2 = d2; \
    230       1.1  briggs 		} \
    231       1.1  briggs 	} while ((bit >>= 1) != 0)
    232       1.1  briggs 
    233       1.1  briggs #define	WORD(r, i)			/* calculate r->fp_mant[i] */ \
    234       1.1  briggs 	q = 0; \
    235       1.1  briggs 	bit = 1 << 31; \
    236       1.1  briggs 	LOOP; \
    237       1.1  briggs 	(x)->fp_mant[i] = q
    238       1.1  briggs 
    239       1.1  briggs 	/* Setup.  Note that we put our result in x. */
    240       1.1  briggs 	r0 = x->fp_mant[0];
    241       1.1  briggs 	r1 = x->fp_mant[1];
    242       1.1  briggs 	r2 = x->fp_mant[2];
    243       1.1  briggs 	y0 = y->fp_mant[0];
    244       1.1  briggs 	y1 = y->fp_mant[1];
    245       1.1  briggs 	y2 = y->fp_mant[2];
    246       1.1  briggs 
    247       1.1  briggs 	bit = FP_1;
    248       1.1  briggs 	SUBTRACT;
    249       1.1  briggs 	if (NONNEGATIVE) {
    250       1.1  briggs 		x->fp_exp -= y->fp_exp;
    251       1.2  briggs 		r0 = d0, r1 = d1, r2 = d2;
    252       1.1  briggs 		q = bit;
    253       1.1  briggs 		bit >>= 1;
    254       1.1  briggs 	} else {
    255       1.1  briggs 		x->fp_exp -= y->fp_exp + 1;
    256       1.1  briggs 		q = 0;
    257       1.1  briggs 	}
    258       1.1  briggs 	LOOP;
    259       1.1  briggs 	x->fp_mant[0] = q;
    260       1.1  briggs 	WORD(x, 1);
    261       1.1  briggs 	WORD(x, 2);
    262       1.2  briggs 	x->fp_sticky = r0 | r1 | r2;
    263       1.1  briggs 
    264       1.1  briggs 	return (x);
    265       1.1  briggs }
    266