Blame sysdeps/sparc/sparc32/udiv.S

Packit 6c4009
   /* This file is generated from divrem.m4; DO NOT EDIT! */
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
 *  .udiv	name of function to generate
Packit 6c4009
 *  div		div=div => %o0 / %o1; div=rem => %o0 % %o1
Packit 6c4009
 *  false		false=true => signed; false=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
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <sys/trap.h>
Packit 6c4009
Packit 6c4009
ENTRY(.udiv)
Packit 6c4009
Packit 6c4009
	! Ready to divide.  Compute size of quotient; scale comparand.
Packit 6c4009
	orcc	%o1, %g0, %o5
Packit 6c4009
	bne	1f
Packit 6c4009
	mov	%o0, %o3
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	%o3, %o5			! if %o1 exceeds %o0, done
Packit 6c4009
	blu	LOC(got_result)		! (and algorithm fails otherwise)
Packit 6c4009
	clr	%o2
Packit 6c4009
	sethi	%hi(1 << (32 - 4 - 1)), %g1
Packit 6c4009
	cmp	%o3, %g1
Packit 6c4009
	blu	LOC(not_really_big)
Packit 6c4009
	clr	%o4
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	%o5, %g1
Packit 6c4009
		bgeu	3f
Packit 6c4009
		mov	1, %g2
Packit 6c4009
		sll	%o5, 4, %o5
Packit 6c4009
		b	1b
Packit 6c4009
		add	%o4, 1, %o4
Packit 6c4009
Packit 6c4009
	! Now compute %g2.
Packit 6c4009
	2:	addcc	%o5, %o5, %o5
Packit 6c4009
		bcc	LOC(not_too_big)
Packit 6c4009
		add	%g2, 1, %g2
Packit 6c4009
Packit 6c4009
		! We get here if the %o1 overflowed while shifting.
Packit 6c4009
		! This means that %o3 has the high-order bit set.
Packit 6c4009
		! Restore %o5 and subtract from %o3.
Packit 6c4009
		sll	%g1, 4, %g1	! high order bit
Packit 6c4009
		srl	%o5, 1, %o5		! rest of %o5
Packit 6c4009
		add	%o5, %g1, %o5
Packit 6c4009
		b	LOC(do_single_div)
Packit 6c4009
		sub	%g2, 1, %g2
Packit 6c4009
Packit 6c4009
	LOC(not_too_big):
Packit 6c4009
	3:	cmp	%o5, %o3
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
	! %o5 > %o3: went too far: back up 1 step
Packit 6c4009
	!	srl	%o5, 1, %o5
Packit 6c4009
	!	dec	%g2
Packit 6c4009
	! do single-bit divide steps
Packit 6c4009
	!
Packit 6c4009
	! We have to be careful here.  We know that %o3 >= %o5, so we can do the
Packit 6c4009
	! first divide step without thinking.  BUT, the others are conditional,
Packit 6c4009
	! and are only done if %o3 >= 0.  Because both %o3 and %o5 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	%g2, 1, %g2
Packit 6c4009
		bl	LOC(end_regular_divide)
Packit 6c4009
		nop
Packit 6c4009
		sub	%o3, %o5, %o3
Packit 6c4009
		mov	1, %o2
Packit 6c4009
		b	LOC(end_single_divloop)
Packit 6c4009
		nop
Packit 6c4009
	LOC(single_divloop):
Packit 6c4009
		sll	%o2, 1, %o2
Packit 6c4009
		bl	1f
Packit 6c4009
		srl	%o5, 1, %o5
Packit 6c4009
		! %o3 >= 0
Packit 6c4009
		sub	%o3, %o5, %o3
Packit 6c4009
		b	2f
Packit 6c4009
		add	%o2, 1, %o2
Packit 6c4009
	1:	! %o3 < 0
Packit 6c4009
		add	%o3, %o5, %o3
Packit 6c4009
		sub	%o2, 1, %o2
Packit 6c4009
	2:
Packit 6c4009
	LOC(end_single_divloop):
Packit 6c4009
		subcc	%g2, 1, %g2
Packit 6c4009
		bge	LOC(single_divloop)
Packit 6c4009
		tst	%o3
Packit 6c4009
		b,a	LOC(end_regular_divide)
Packit 6c4009
Packit 6c4009
LOC(not_really_big):
Packit 6c4009
1:
Packit 6c4009
	sll	%o5, 4, %o5
Packit 6c4009
	cmp	%o5, %o3
Packit 6c4009
	bleu	1b
Packit 6c4009
	addcc	%o4, 1, %o4
Packit 6c4009
	be	LOC(got_result)
Packit 6c4009
	sub	%o4, 1, %o4
Packit 6c4009
Packit 6c4009
	tst	%o3	! set up for initial iteration
Packit 6c4009
LOC(divloop):
Packit 6c4009
	sll	%o2, 4, %o2
Packit 6c4009
		! depth 1, accumulated bits 0
Packit 6c4009
	bl	LOC(1.16)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 2, accumulated bits 1
Packit 6c4009
	bl	LOC(2.17)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 3, accumulated bits 3
Packit 6c4009
	bl	LOC(3.19)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits 7
Packit 6c4009
	bl	LOC(4.23)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (7*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.23):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (7*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(3.19):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits 5
Packit 6c4009
	bl	LOC(4.21)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (5*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.21):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (5*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(2.17):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 3, accumulated bits 1
Packit 6c4009
	bl	LOC(3.17)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits 3
Packit 6c4009
	bl	LOC(4.19)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (3*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.19):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (3*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(3.17):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits 1
Packit 6c4009
	bl	LOC(4.17)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (1*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.17):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (1*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(1.16):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 2, accumulated bits -1
Packit 6c4009
	bl	LOC(2.15)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 3, accumulated bits -1
Packit 6c4009
	bl	LOC(3.15)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits -1
Packit 6c4009
	bl	LOC(4.15)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-1*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.15):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-1*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(3.15):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits -3
Packit 6c4009
	bl	LOC(4.13)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-3*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.13):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-3*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(2.15):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 3, accumulated bits -3
Packit 6c4009
	bl	LOC(3.13)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits -5
Packit 6c4009
	bl	LOC(4.11)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-5*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.11):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-5*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(3.13):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
			! depth 4, accumulated bits -7
Packit 6c4009
	bl	LOC(4.9)
Packit 6c4009
	srl	%o5,1,%o5
Packit 6c4009
	! remainder is positive
Packit 6c4009
	subcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-7*2+1), %o2
Packit 6c4009
Packit 6c4009
LOC(4.9):
Packit 6c4009
	! remainder is negative
Packit 6c4009
	addcc	%o3,%o5,%o3
Packit 6c4009
		b	9f
Packit 6c4009
		add	%o2, (-7*2-1), %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
9:
Packit 6c4009
LOC(end_regular_divide):
Packit 6c4009
	subcc	%o4, 1, %o4
Packit 6c4009
	bge	LOC(divloop)
Packit 6c4009
	tst	%o3
Packit 6c4009
	bl,a	LOC(got_result)
Packit 6c4009
	! non-restoring fixup here (one instruction only!)
Packit 6c4009
	sub	%o2, 1, %o2
Packit 6c4009
Packit 6c4009
Packit 6c4009
LOC(got_result):
Packit 6c4009
Packit 6c4009
	retl
Packit 6c4009
	mov %o2, %o0
Packit 6c4009
Packit 6c4009
END(.udiv)
Packit 6c4009
strong_alias (.udiv, __wrap_.udiv)