Blame sysdeps/sparc/sparc32/divrem.m4

Packit 6c4009
/*
Packit 6c4009
 * Division and remainder, from Appendix E of the Sparc Version 8
Packit 6c4009
 * Architecture Manual, with fixes from Gordon Irlam.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Input: dividend and divisor in %o0 and %o1 respectively.
Packit 6c4009
 *
Packit 6c4009
 * m4 parameters:
Packit 6c4009
 *  NAME	name of function to generate
Packit 6c4009
 *  OP		OP=div => %o0 / %o1; OP=rem => %o0 % %o1
Packit 6c4009
 *  S		S=true => signed; S=false => unsigned
Packit 6c4009
 *
Packit 6c4009
 * Algorithm parameters:
Packit 6c4009
 *  N		how many bits per iteration we try to get (4)
Packit 6c4009
 *  WORDSIZE	total number of bits (32)
Packit 6c4009
 *
Packit 6c4009
 * Derived constants:
Packit 6c4009
 *  TOPBITS	number of bits in the top `decade' of a number
Packit 6c4009
 *
Packit 6c4009
 * Important variables:
Packit 6c4009
 *  Q		the partial quotient under development (initially 0)
Packit 6c4009
 *  R		the remainder so far, initially the dividend
Packit 6c4009
 *  ITER	number of main division loop iterations required;
Packit 6c4009
 *		equal to ceil(log2(quotient) / N).  Note that this
Packit 6c4009
 *		is the log base (2^N) of the quotient.
Packit 6c4009
 *  V		the current comparand, initially divisor*2^(ITER*N-1)
Packit 6c4009
 *
Packit 6c4009
 * Cost:
Packit 6c4009
 *  Current estimate for non-large dividend is
Packit 6c4009
 *	ceil(log2(quotient) / N) * (10 + 7N/2) + C
Packit 6c4009
 *  A large dividend is one greater than 2^(31-TOPBITS) and takes a
Packit 6c4009
 *  different path, as the upper bits of the quotient must be developed
Packit 6c4009
 *  one bit at a time.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
define(N, `4')dnl
Packit 6c4009
define(WORDSIZE, `32')dnl
Packit 6c4009
define(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N)))dnl
Packit 6c4009
dnl
Packit 6c4009
define(dividend, `%o0')dnl
Packit 6c4009
define(divisor, `%o1')dnl
Packit 6c4009
define(Q, `%o2')dnl
Packit 6c4009
define(R, `%o3')dnl
Packit 6c4009
define(ITER, `%o4')dnl
Packit 6c4009
define(V, `%o5')dnl
Packit 6c4009
dnl
Packit 6c4009
dnl m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d
Packit 6c4009
define(T, `%g1')dnl
Packit 6c4009
define(SC, `%g2')dnl
Packit 6c4009
ifelse(S, `true', `define(SIGN, `%g3')')dnl
Packit 6c4009
Packit 6c4009
dnl
Packit 6c4009
dnl This is the recursive definition for developing quotient digits.
Packit 6c4009
dnl
Packit 6c4009
dnl Parameters:
Packit 6c4009
dnl  $1	the current depth, 1 <= $1 <= N
Packit 6c4009
dnl  $2	the current accumulation of quotient bits
Packit 6c4009
dnl  N	max depth
Packit 6c4009
dnl
Packit 6c4009
dnl We add a new bit to $2 and either recurse or insert the bits in
Packit 6c4009
dnl the quotient.  R, Q, and V are inputs and outputs as defined above;
Packit 6c4009
dnl the condition codes are expected to reflect the input R, and are
Packit 6c4009
dnl modified to reflect the output R.
Packit 6c4009
dnl
Packit 6c4009
define(DEVELOP_QUOTIENT_BITS,
Packit 6c4009
`	! depth $1, accumulated bits $2
Packit 6c4009
	bl	LOC($1.eval(2**N+$2))
Packit 6c4009
	srl	V,1,V
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	R,V,R
Packit 6c4009
	ifelse($1, N,
Packit 6c4009
	`	b	9f
Packit 6c4009
		add	Q, ($2*2+1), Q
Packit 6c4009
', `	DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')')
Packit 6c4009
LOC($1.eval(2**N+$2)):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	R,V,R
Packit 6c4009
	ifelse($1, N,
Packit 6c4009
	`	b	9f
Packit 6c4009
		add	Q, ($2*2-1), Q
Packit 6c4009
', `	DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')')
Packit 6c4009
ifelse($1, 1, `9:')')dnl
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <sys/trap.h>
Packit 6c4009
Packit 6c4009
ENTRY(NAME)
Packit 6c4009
ifelse(S, `true',
Packit 6c4009
`	! compute sign of result; if neither is negative, no problem
Packit 6c4009
	orcc	divisor, dividend, %g0	! either negative?
Packit 6c4009
	bge	2f			! no, go do the divide
Packit 6c4009
ifelse(OP, `div',
Packit 6c4009
`	xor	divisor, dividend, SIGN	! compute sign in any case',
Packit 6c4009
`	mov	dividend, SIGN		! sign of remainder matches dividend')
Packit 6c4009
	tst	divisor
Packit 6c4009
	bge	1f
Packit 6c4009
	tst	dividend
Packit 6c4009
	! divisor is definitely negative; dividend might also be negative
Packit 6c4009
	bge	2f			! if dividend not negative...
Packit 6c4009
	sub	%g0, divisor, divisor	! in any case, make divisor nonneg
Packit 6c4009
1:	! dividend is negative, divisor is nonnegative
Packit 6c4009
	sub	%g0, dividend, dividend	! make dividend nonnegative
Packit 6c4009
2:
Packit 6c4009
')
Packit 6c4009
	! Ready to divide.  Compute size of quotient; scale comparand.
Packit 6c4009
	orcc	divisor, %g0, V
Packit 6c4009
	bne	1f
Packit 6c4009
	mov	dividend, R
Packit 6c4009
Packit 6c4009
		! Divide by zero trap.  If it returns, return 0 (about as
Packit 6c4009
		! wrong as possible, but that is what SunOS does...).
Packit 6c4009
		ta	ST_DIV0
Packit 6c4009
		retl
Packit 6c4009
		clr	%o0
Packit 6c4009
Packit 6c4009
1:
Packit 6c4009
	cmp	R, V			! if divisor exceeds dividend, done
Packit 6c4009
	blu	LOC(got_result)		! (and algorithm fails otherwise)
Packit 6c4009
	clr	Q
Packit 6c4009
	sethi	%hi(1 << (WORDSIZE - TOPBITS - 1)), T
Packit 6c4009
	cmp	R, T
Packit 6c4009
	blu	LOC(not_really_big)
Packit 6c4009
	clr	ITER
Packit 6c4009
Packit 6c4009
	! `Here the dividend is >= 2**(31-N) or so.  We must be careful here,
Packit 6c4009
	! as our usual N-at-a-shot divide step will cause overflow and havoc.
Packit 6c4009
	! The number of bits in the result here is N*ITER+SC, where SC <= N.
Packit 6c4009
	! Compute ITER in an unorthodox manner: know we need to shift V into
Packit 6c4009
	! the top decade: so do not even bother to compare to R.'
Packit 6c4009
	1:
Packit 6c4009
		cmp	V, T
Packit 6c4009
		bgeu	3f
Packit 6c4009
		mov	1, SC
Packit 6c4009
		sll	V, N, V
Packit 6c4009
		b	1b
Packit 6c4009
		add	ITER, 1, ITER
Packit 6c4009
Packit 6c4009
	! Now compute SC.
Packit 6c4009
	2:	addcc	V, V, V
Packit 6c4009
		bcc	LOC(not_too_big)
Packit 6c4009
		add	SC, 1, SC
Packit 6c4009
Packit 6c4009
		! We get here if the divisor overflowed while shifting.
Packit 6c4009
		! This means that R has the high-order bit set.
Packit 6c4009
		! Restore V and subtract from R.
Packit 6c4009
		sll	T, TOPBITS, T	! high order bit
Packit 6c4009
		srl	V, 1, V		! rest of V
Packit 6c4009
		add	V, T, V
Packit 6c4009
		b	LOC(do_single_div)
Packit 6c4009
		sub	SC, 1, SC
Packit 6c4009
Packit 6c4009
	LOC(not_too_big):
Packit 6c4009
	3:	cmp	V, R
Packit 6c4009
		blu	2b
Packit 6c4009
		nop
Packit 6c4009
		be	LOC(do_single_div)
Packit 6c4009
		nop
Packit 6c4009
	/* NB: these are commented out in the V8-Sparc manual as well */
Packit 6c4009
	/* (I do not understand this) */
Packit 6c4009
	! V > R: went too far: back up 1 step
Packit 6c4009
	!	srl	V, 1, V
Packit 6c4009
	!	dec	SC
Packit 6c4009
	! do single-bit divide steps
Packit 6c4009
	!
Packit 6c4009
	! We have to be careful here.  We know that R >= V, so we can do the
Packit 6c4009
	! first divide step without thinking.  BUT, the others are conditional,
Packit 6c4009
	! and are only done if R >= 0.  Because both R and V may have the high-
Packit 6c4009
	! order bit set in the first step, just falling into the regular
Packit 6c4009
	! division loop will mess up the first time around.
Packit 6c4009
	! So we unroll slightly...
Packit 6c4009
	LOC(do_single_div):
Packit 6c4009
		subcc	SC, 1, SC
Packit 6c4009
		bl	LOC(end_regular_divide)
Packit 6c4009
		nop
Packit 6c4009
		sub	R, V, R
Packit 6c4009
		mov	1, Q
Packit 6c4009
		b	LOC(end_single_divloop)
Packit 6c4009
		nop
Packit 6c4009
	LOC(single_divloop):
Packit 6c4009
		sll	Q, 1, Q
Packit 6c4009
		bl	1f
Packit 6c4009
		srl	V, 1, V
Packit 6c4009
		! R >= 0
Packit 6c4009
		sub	R, V, R
Packit 6c4009
		b	2f
Packit 6c4009
		add	Q, 1, Q
Packit 6c4009
	1:	! R < 0
Packit 6c4009
		add	R, V, R
Packit 6c4009
		sub	Q, 1, Q
Packit 6c4009
	2:
Packit 6c4009
	LOC(end_single_divloop):
Packit 6c4009
		subcc	SC, 1, SC
Packit 6c4009
		bge	LOC(single_divloop)
Packit 6c4009
		tst	R
Packit 6c4009
		b,a	LOC(end_regular_divide)
Packit 6c4009
Packit 6c4009
LOC(not_really_big):
Packit 6c4009
1:
Packit 6c4009
	sll	V, N, V
Packit 6c4009
	cmp	V, R
Packit 6c4009
	bleu	1b
Packit 6c4009
	addcc	ITER, 1, ITER
Packit 6c4009
	be	LOC(got_result)
Packit 6c4009
	sub	ITER, 1, ITER
Packit 6c4009
Packit 6c4009
	tst	R	! set up for initial iteration
Packit 6c4009
LOC(divloop):
Packit 6c4009
	sll	Q, N, Q
Packit 6c4009
	DEVELOP_QUOTIENT_BITS(1, 0)
Packit 6c4009
LOC(end_regular_divide):
Packit 6c4009
	subcc	ITER, 1, ITER
Packit 6c4009
	bge	LOC(divloop)
Packit 6c4009
	tst	R
Packit 6c4009
	bl,a	LOC(got_result)
Packit 6c4009
	! non-restoring fixup here (one instruction only!)
Packit 6c4009
ifelse(OP, `div',
Packit 6c4009
`	sub	Q, 1, Q
Packit 6c4009
', `	add	R, divisor, R
Packit 6c4009
')
Packit 6c4009
Packit 6c4009
LOC(got_result):
Packit 6c4009
ifelse(S, `true',
Packit 6c4009
`	! check to see if answer should be < 0
Packit 6c4009
	tst	SIGN
Packit 6c4009
	bl,a	1f
Packit 6c4009
	ifelse(OP, `div', `sub %g0, Q, Q', `sub %g0, R, R')
Packit 6c4009
1:')
Packit 6c4009
	retl
Packit 6c4009
	ifelse(OP, `div', `mov Q, %o0', `mov R, %o0')
Packit 6c4009
Packit 6c4009
END(NAME)
Packit 6c4009
ifelse(OP, `div', ifelse(S, `false', `strong_alias (.udiv, __wrap_.udiv)
Packit 6c4009
'))dnl