xref: /openbsd/lib/libc/arch/sparc64/fpu/fpu_div.c (revision 61e87b28)
1*61e87b28Sderaadt /*	$OpenBSD: fpu_div.c,v 1.3 2013/11/26 20:33:07 deraadt Exp $	*/
202b90beaSjason 
302b90beaSjason /*
402b90beaSjason  * Copyright (c) 1992, 1993
502b90beaSjason  *	The Regents of the University of California.  All rights reserved.
602b90beaSjason  *
702b90beaSjason  * This software was developed by the Computer Systems Engineering group
802b90beaSjason  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
902b90beaSjason  * contributed to Berkeley.
1002b90beaSjason  *
1102b90beaSjason  * All advertising materials mentioning features or use of this software
1202b90beaSjason  * must display the following acknowledgement:
1302b90beaSjason  *	This product includes software developed by the University of
1402b90beaSjason  *	California, Lawrence Berkeley Laboratory.
1502b90beaSjason  *
1602b90beaSjason  * Redistribution and use in source and binary forms, with or without
1702b90beaSjason  * modification, are permitted provided that the following conditions
1802b90beaSjason  * are met:
1902b90beaSjason  * 1. Redistributions of source code must retain the above copyright
2002b90beaSjason  *    notice, this list of conditions and the following disclaimer.
2102b90beaSjason  * 2. Redistributions in binary form must reproduce the above copyright
2202b90beaSjason  *    notice, this list of conditions and the following disclaimer in the
2302b90beaSjason  *    documentation and/or other materials provided with the distribution.
2402b90beaSjason  * 3. All advertising materials mentioning features or use of this software
2502b90beaSjason  *    must display the following acknowledgement:
2602b90beaSjason  *	This product includes software developed by the University of
2702b90beaSjason  *	California, Berkeley and its contributors.
2802b90beaSjason  * 4. Neither the name of the University nor the names of its contributors
2902b90beaSjason  *    may be used to endorse or promote products derived from this software
3002b90beaSjason  *    without specific prior written permission.
3102b90beaSjason  *
3202b90beaSjason  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3302b90beaSjason  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3402b90beaSjason  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3502b90beaSjason  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3602b90beaSjason  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3702b90beaSjason  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3802b90beaSjason  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3902b90beaSjason  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4002b90beaSjason  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4102b90beaSjason  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4202b90beaSjason  * SUCH DAMAGE.
4302b90beaSjason  *
4402b90beaSjason  *	@(#)fpu_div.c	8.1 (Berkeley) 6/11/93
4502b90beaSjason  *	$NetBSD: fpu_div.c,v 1.2 1994/11/20 20:52:38 deraadt Exp $
4602b90beaSjason  */
4702b90beaSjason 
4802b90beaSjason #if 0
4902b90beaSjason __FBSDID("$FreeBSD: src/lib/libc/sparc64/fpu/fpu_div.c,v 1.3 2002/03/22 21:52:58 obrien Exp $");
5002b90beaSjason #endif
5102b90beaSjason 
5202b90beaSjason /*
5302b90beaSjason  * Perform an FPU divide (return x / y).
5402b90beaSjason  */
5502b90beaSjason 
5602b90beaSjason #include <sys/types.h>
5702b90beaSjason 
5802b90beaSjason #include <machine/frame.h>
5902b90beaSjason #include <machine/fsr.h>
6002b90beaSjason 
6102b90beaSjason #include "fpu_arith.h"
6202b90beaSjason #include "fpu_emu.h"
6302b90beaSjason #include "fpu_extern.h"
6402b90beaSjason 
6502b90beaSjason /*
6602b90beaSjason  * Division of normal numbers is done as follows:
6702b90beaSjason  *
6802b90beaSjason  * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
6902b90beaSjason  * If X and Y are the mantissas (1.bbbb's), the quotient is then:
7002b90beaSjason  *
7102b90beaSjason  *	q = (X / Y) * 2^((x exponent) - (y exponent))
7202b90beaSjason  *
7302b90beaSjason  * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
7402b90beaSjason  * will be in [0.5,2.0).  Moreover, it will be less than 1.0 if and only
7502b90beaSjason  * if X < Y.  In that case, it will have to be shifted left one bit to
7602b90beaSjason  * become a normal number, and the exponent decremented.  Thus, the
7702b90beaSjason  * desired exponent is:
7802b90beaSjason  *
7902b90beaSjason  *	left_shift = x->fp_mant < y->fp_mant;
8002b90beaSjason  *	result_exp = x->fp_exp - y->fp_exp - left_shift;
8102b90beaSjason  *
8202b90beaSjason  * The quotient mantissa X/Y can then be computed one bit at a time
8302b90beaSjason  * using the following algorithm:
8402b90beaSjason  *
8502b90beaSjason  *	Q = 0;			-- Initial quotient.
8602b90beaSjason  *	R = X;			-- Initial remainder,
8702b90beaSjason  *	if (left_shift)		--   but fixed up in advance.
8802b90beaSjason  *		R *= 2;
8902b90beaSjason  *	for (bit = FP_NMANT; --bit >= 0; R *= 2) {
9002b90beaSjason  *		if (R >= Y) {
9102b90beaSjason  *			Q |= 1 << bit;
9202b90beaSjason  *			R -= Y;
9302b90beaSjason  *		}
9402b90beaSjason  *	}
9502b90beaSjason  *
9602b90beaSjason  * The subtraction R -= Y always removes the uppermost bit from R (and
9702b90beaSjason  * can sometimes remove additional lower-order 1 bits); this proof is
9802b90beaSjason  * left to the reader.
9902b90beaSjason  *
10002b90beaSjason  * This loop correctly calculates the guard and round bits since they are
10102b90beaSjason  * included in the expanded internal representation.  The sticky bit
10202b90beaSjason  * is to be set if and only if any other bits beyond guard and round
10302b90beaSjason  * would be set.  From the above it is obvious that this is true if and
10402b90beaSjason  * only if the remainder R is nonzero when the loop terminates.
10502b90beaSjason  *
10602b90beaSjason  * Examining the loop above, we can see that the quotient Q is built
10702b90beaSjason  * one bit at a time ``from the top down''.  This means that we can
10802b90beaSjason  * dispense with the multi-word arithmetic and just build it one word
10902b90beaSjason  * at a time, writing each result word when it is done.
11002b90beaSjason  *
11102b90beaSjason  * Furthermore, since X and Y are both in [1.0,2.0), we know that,
11202b90beaSjason  * initially, R >= Y.  (Recall that, if X < Y, R is set to X * 2 and
11302b90beaSjason  * is therefore at in [2.0,4.0).)  Thus Q is sure to have bit FP_NMANT-1
11402b90beaSjason  * set, and R can be set initially to either X - Y (when X >= Y) or
11502b90beaSjason  * 2X - Y (when X < Y).  In addition, comparing R and Y is difficult,
11602b90beaSjason  * so we will simply calculate R - Y and see if that underflows.
11702b90beaSjason  * This leads to the following revised version of the algorithm:
11802b90beaSjason  *
11902b90beaSjason  *	R = X;
12002b90beaSjason  *	bit = FP_1;
12102b90beaSjason  *	D = R - Y;
12202b90beaSjason  *	if (D >= 0) {
12302b90beaSjason  *		result_exp = x->fp_exp - y->fp_exp;
12402b90beaSjason  *		R = D;
12502b90beaSjason  *		q = bit;
12602b90beaSjason  *		bit >>= 1;
12702b90beaSjason  *	} else {
12802b90beaSjason  *		result_exp = x->fp_exp - y->fp_exp - 1;
12902b90beaSjason  *		q = 0;
13002b90beaSjason  *	}
13102b90beaSjason  *	R <<= 1;
13202b90beaSjason  *	do  {
13302b90beaSjason  *		D = R - Y;
13402b90beaSjason  *		if (D >= 0) {
13502b90beaSjason  *			q |= bit;
13602b90beaSjason  *			R = D;
13702b90beaSjason  *		}
13802b90beaSjason  *		R <<= 1;
13902b90beaSjason  *	} while ((bit >>= 1) != 0);
14002b90beaSjason  *	Q[0] = q;
14102b90beaSjason  *	for (i = 1; i < 4; i++) {
142*61e87b28Sderaadt  *		q = 0, bit = 1U << 31;
14302b90beaSjason  *		do {
14402b90beaSjason  *			D = R - Y;
14502b90beaSjason  *			if (D >= 0) {
14602b90beaSjason  *				q |= bit;
14702b90beaSjason  *				R = D;
14802b90beaSjason  *			}
14902b90beaSjason  *			R <<= 1;
15002b90beaSjason  *		} while ((bit >>= 1) != 0);
15102b90beaSjason  *		Q[i] = q;
15202b90beaSjason  *	}
15302b90beaSjason  *
15402b90beaSjason  * This can be refined just a bit further by moving the `R <<= 1'
15502b90beaSjason  * calculations to the front of the do-loops and eliding the first one.
15602b90beaSjason  * The process can be terminated immediately whenever R becomes 0, but
15702b90beaSjason  * this is relatively rare, and we do not bother.
15802b90beaSjason  */
15902b90beaSjason 
16002b90beaSjason struct fpn *
16102b90beaSjason __fpu_div(fe)
16202b90beaSjason 	struct fpemu *fe;
16302b90beaSjason {
16402b90beaSjason 	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
16502b90beaSjason 	u_int q, bit;
16602b90beaSjason 	u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
16702b90beaSjason 	FPU_DECL_CARRY
16802b90beaSjason 
16902b90beaSjason 	/*
17002b90beaSjason 	 * Since divide is not commutative, we cannot just use ORDER.
17102b90beaSjason 	 * Check either operand for NaN first; if there is at least one,
17202b90beaSjason 	 * order the signalling one (if only one) onto the right, then
17302b90beaSjason 	 * return it.  Otherwise we have the following cases:
17402b90beaSjason 	 *
17502b90beaSjason 	 *	Inf / Inf = NaN, plus NV exception
17602b90beaSjason 	 *	Inf / num = Inf [i.e., return x]
17702b90beaSjason 	 *	Inf / 0   = Inf [i.e., return x]
17802b90beaSjason 	 *	0 / Inf = 0 [i.e., return x]
17902b90beaSjason 	 *	0 / num = 0 [i.e., return x]
18002b90beaSjason 	 *	0 / 0   = NaN, plus NV exception
18102b90beaSjason 	 *	num / Inf = 0
18202b90beaSjason 	 *	num / num = num (do the divide)
18302b90beaSjason 	 *	num / 0   = Inf, plus DZ exception
18402b90beaSjason 	 */
18502b90beaSjason 	if (ISNAN(x) || ISNAN(y)) {
18602b90beaSjason 		ORDER(x, y);
18702b90beaSjason 		return (y);
18802b90beaSjason 	}
18902b90beaSjason 	if (ISINF(x) || ISZERO(x)) {
19002b90beaSjason 		if (x->fp_class == y->fp_class)
19102b90beaSjason 			return (__fpu_newnan(fe));
19202b90beaSjason 		return (x);
19302b90beaSjason 	}
19402b90beaSjason 
19502b90beaSjason 	/* all results at this point use XOR of operand signs */
19602b90beaSjason 	x->fp_sign ^= y->fp_sign;
19702b90beaSjason 	if (ISINF(y)) {
19802b90beaSjason 		x->fp_class = FPC_ZERO;
19902b90beaSjason 		return (x);
20002b90beaSjason 	}
20102b90beaSjason 	if (ISZERO(y)) {
20202b90beaSjason 		fe->fe_cx = FSR_DZ;
20302b90beaSjason 		x->fp_class = FPC_INF;
20402b90beaSjason 		return (x);
20502b90beaSjason 	}
20602b90beaSjason 
20702b90beaSjason 	/*
20802b90beaSjason 	 * Macros for the divide.  See comments at top for algorithm.
20902b90beaSjason 	 * Note that we expand R, D, and Y here.
21002b90beaSjason 	 */
21102b90beaSjason 
21202b90beaSjason #define	SUBTRACT		/* D = R - Y */ \
21302b90beaSjason 	FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
21402b90beaSjason 	FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
21502b90beaSjason 
21602b90beaSjason #define	NONNEGATIVE		/* D >= 0 */ \
21702b90beaSjason 	((int)d0 >= 0)
21802b90beaSjason 
21902b90beaSjason #ifdef FPU_SHL1_BY_ADD
22002b90beaSjason #define	SHL1			/* R <<= 1 */ \
22102b90beaSjason 	FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
22202b90beaSjason 	FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
22302b90beaSjason #else
22402b90beaSjason #define	SHL1 \
22502b90beaSjason 	r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
22602b90beaSjason 	r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
22702b90beaSjason #endif
22802b90beaSjason 
22902b90beaSjason #define	LOOP			/* do ... while (bit >>= 1) */ \
23002b90beaSjason 	do { \
23102b90beaSjason 		SHL1; \
23202b90beaSjason 		SUBTRACT; \
23302b90beaSjason 		if (NONNEGATIVE) { \
23402b90beaSjason 			q |= bit; \
23502b90beaSjason 			r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
23602b90beaSjason 		} \
23702b90beaSjason 	} while ((bit >>= 1) != 0)
23802b90beaSjason 
23902b90beaSjason #define	WORD(r, i)			/* calculate r->fp_mant[i] */ \
24002b90beaSjason 	q = 0; \
241*61e87b28Sderaadt 	bit = 1U << 31; \
24202b90beaSjason 	LOOP; \
24302b90beaSjason 	(x)->fp_mant[i] = q
24402b90beaSjason 
24502b90beaSjason 	/* Setup.  Note that we put our result in x. */
24602b90beaSjason 	r0 = x->fp_mant[0];
24702b90beaSjason 	r1 = x->fp_mant[1];
24802b90beaSjason 	r2 = x->fp_mant[2];
24902b90beaSjason 	r3 = x->fp_mant[3];
25002b90beaSjason 	y0 = y->fp_mant[0];
25102b90beaSjason 	y1 = y->fp_mant[1];
25202b90beaSjason 	y2 = y->fp_mant[2];
25302b90beaSjason 	y3 = y->fp_mant[3];
25402b90beaSjason 
25502b90beaSjason 	bit = FP_1;
25602b90beaSjason 	SUBTRACT;
25702b90beaSjason 	if (NONNEGATIVE) {
25802b90beaSjason 		x->fp_exp -= y->fp_exp;
25902b90beaSjason 		r0 = d0, r1 = d1, r2 = d2, r3 = d3;
26002b90beaSjason 		q = bit;
26102b90beaSjason 		bit >>= 1;
26202b90beaSjason 	} else {
26302b90beaSjason 		x->fp_exp -= y->fp_exp + 1;
26402b90beaSjason 		q = 0;
26502b90beaSjason 	}
26602b90beaSjason 	LOOP;
26702b90beaSjason 	x->fp_mant[0] = q;
26802b90beaSjason 	WORD(x, 1);
26902b90beaSjason 	WORD(x, 2);
27002b90beaSjason 	WORD(x, 3);
27102b90beaSjason 	x->fp_sticky = r0 | r1 | r2 | r3;
27202b90beaSjason 
27302b90beaSjason 	return (x);
27402b90beaSjason }
275