2 * Division and remainder, from Appendix E of the Sparc Version 8
3 * Architecture Manual, with fixes from Gordon Irlam.
7 * Input: dividend and divisor in %o0 and %o1 respectively.
10 * NAME name of function to generate
11 * OP OP=div => %o0 / %o1; OP=rem => %o0 % %o1
12 * S S=true => signed; S=false => unsigned
14 * Algorithm parameters:
15 * N how many bits per iteration we try to get (4)
16 * WORDSIZE total number of bits (32)
19 * TOPBITS number of bits in the top `decade' of a number
21 * Important variables:
22 * Q the partial quotient under development (initially 0)
23 * R the remainder so far, initially the dividend
24 * ITER number of main division loop iterations required;
25 * equal to ceil(log2(quotient) / N). Note that this
26 * is the log base (2^N) of the quotient.
27 * V the current comparand, initially divisor*2^(ITER*N-1)
30 * Current estimate for non-large dividend is
31 * ceil(log2(quotient) / N) * (10 + 7N/2) + C
32 * A large dividend is one greater than 2^(31-TOPBITS) and takes a
33 * different path, as the upper bits of the quotient must be developed
38 define(WORDSIZE, `32')
39 define(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N)))
41 define(dividend, `%o0')
42 define(divisor, `%o1')
48 /* m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d */
51 ifelse(S, `true', `define(SIGN, `%g6')')
54 * This is the recursive definition for developing quotient digits.
57 * $1 the current depth, 1 <= $1 <= N
58 * $2 the current accumulation of quotient bits
61 * We add a new bit to $2 and either recurse or insert the bits in
62 * the quotient. R, Q, and V are inputs and outputs as defined above;
63 * the condition codes are expected to reflect the input R, and are
64 * modified to reflect the output R.
66 define(DEVELOP_QUOTIENT_BITS,
67 ` ! depth $1, accumulated bits $2
70 ! remainder is positive
75 ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')')
77 ! remainder is negative
82 ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')')
86 #include <machine/trap.h>
90 ` ! compute sign of result; if neither is negative, no problem
91 orcc divisor, dividend, %g0 ! either negative?
92 bge 2f ! no, go do the divide
93 xor divisor, dividend, SIGN ! compute sign in any case
97 ! divisor is definitely negative; dividend might also be negative
98 bge 2f ! if dividend not negative...
99 sub %g0, divisor, divisor ! in any case, make divisor nonneg
100 1: ! dividend is negative, divisor is nonnegative
101 sub %g0, dividend, dividend ! make dividend nonnegative
104 ! Ready to divide. Compute size of quotient; scale comparand.
109 ! Divide by zero trap. If it returns, return 0 (about as
110 ! wrong as possible, but that is what SunOS does...).
116 cmp R, V ! if divisor exceeds dividend, done
117 blu Lgot_result ! (and algorithm fails otherwise)
119 sethi %hi(1 << (WORDSIZE - TOPBITS - 1)), T
124 ! `Here the dividend is >= 2^(31-N) or so. We must be careful here,
125 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
126 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
127 ! Compute ITER in an unorthodox manner: know we need to shift V into
128 ! the top decade: so do not even bother to compare to R.'
142 ! We get here if the divisor overflowed while shifting.
143 ! This means that R has the high-order bit set.
144 ! Restore V and subtract from R.
145 sll T, TOPBITS, T ! high order bit
146 srl V, 1, V ! rest of V
157 /* NB: these are commented out in the V8-Sparc manual as well */
158 /* (I do not understand this) */
159 ! V > R: went too far: back up 1 step
162 ! do single-bit divide steps
164 ! We have to be careful here. We know that R >= V, so we can do the
165 ! first divide step without thinking. BUT, the others are conditional,
166 ! and are only done if R >= 0. Because both R and V may have the high-
167 ! order bit set in the first step, just falling into the regular
168 ! division loop will mess up the first time around.
169 ! So we unroll slightly...
172 bl Lend_regular_divide
176 b Lend_single_divloop
194 b,a Lend_regular_divide
205 tst R ! set up for initial iteration
208 DEVELOP_QUOTIENT_BITS(1, 0)
214 ! non-restoring fixup here (one instruction only!)
217 ', ` add R, divisor, R
222 ` ! check to see if answer should be < 0
225 ifelse(OP, `div', `sub %g0, Q, Q', `sub %g0, R, R')
228 ifelse(OP, `div', `mov Q, %o0', `mov R, %o0')