1 /* This file is generated from divrem.m4; DO NOT EDIT! */
3 * Division and remainder, from Appendix E of the Sparc Version 8
4 * Architecture Manual, with fixes from Gordon Irlam.
8 * Input: dividend and divisor in %o0 and %o1 respectively.
11 * .udiv name of function to generate
12 * div div=div => %o0 / %o1; div=rem => %o0 % %o1
13 * false false=true => signed; false=false => unsigned
15 * Algorithm parameters:
16 * N how many bits per iteration we try to get (4)
17 * WORDSIZE total number of bits (32)
20 * TOPBITS number of bits in the top decade of a number
22 * Important variables:
23 * Q the partial quotient under development (initially 0)
24 * R the remainder so far, initially the dividend
25 * ITER number of main division loop iterations required;
26 * equal to ceil(log2(quotient) / N). Note that this
27 * is the log base (2^N) of the quotient.
28 * V the current comparand, initially divisor*2^(ITER*N-1)
31 * Current estimate for non-large dividend is
32 * ceil(log2(quotient) / N) * (10 + 7N/2) + C
33 * A large dividend is one greater than 2^(31-TOPBITS) and takes a
34 * different path, as the upper bits of the quotient must be developed
42 #include <asm/traps.h>
47 #include <machine/trap.h>
53 ! Ready to divide. Compute size of quotient; scale comparand.
58 ! Divide by zero trap. If it returns, return 0 (about as
59 ! wrong as possible, but that is what SunOS does...).
65 cmp %o3, %o5 ! if %o1 exceeds %o0, done
66 blu Lgot_result ! (and algorithm fails otherwise)
68 sethi %hi(1 << (32 - 4 - 1)), %g1
73 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
74 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
75 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
76 ! Compute ITER in an unorthodox manner: know we need to shift V into
77 ! the top decade: so do not even bother to compare to R.
87 2: addcc %o5, %o5, %o5
91 ! We get here if the %o1 overflowed while shifting.
92 ! This means that %o3 has the high-order bit set.
93 ! Restore %o5 and subtract from %o3.
94 sll %g1, 4, %g1 ! high order bit
95 srl %o5, 1, %o5 ! rest of %o5
106 /* NB: these are commented out in the V8-Sparc manual as well */
107 /* (I do not understand this) */
108 ! %o5 > %o3: went too far: back up 1 step
111 ! do single-bit divide steps
113 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
114 ! first divide step without thinking. BUT, the others are conditional,
115 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
116 ! order bit set in the first step, just falling into the regular
117 ! division loop will mess up the first time around.
118 ! So we unroll slightly...
121 bl Lend_regular_divide
125 b Lend_single_divloop
143 b,a Lend_regular_divide
154 tst %o3 ! set up for initial iteration
157 ! depth 1, accumulated bits 0
160 ! remainder is positive
162 ! depth 2, accumulated bits 1
165 ! remainder is positive
167 ! depth 3, accumulated bits 3
170 ! remainder is positive
172 ! depth 4, accumulated bits 7
175 ! remainder is positive
178 add %o2, (7*2+1), %o2
181 ! remainder is negative
184 add %o2, (7*2-1), %o2
188 ! remainder is negative
190 ! depth 4, accumulated bits 5
193 ! remainder is positive
196 add %o2, (5*2+1), %o2
199 ! remainder is negative
202 add %o2, (5*2-1), %o2
207 ! remainder is negative
209 ! depth 3, accumulated bits 1
212 ! remainder is positive
214 ! depth 4, accumulated bits 3
217 ! remainder is positive
220 add %o2, (3*2+1), %o2
223 ! remainder is negative
226 add %o2, (3*2-1), %o2
230 ! remainder is negative
232 ! depth 4, accumulated bits 1
235 ! remainder is positive
238 add %o2, (1*2+1), %o2
241 ! remainder is negative
244 add %o2, (1*2-1), %o2
250 ! remainder is negative
252 ! depth 2, accumulated bits -1
255 ! remainder is positive
257 ! depth 3, accumulated bits -1
260 ! remainder is positive
262 ! depth 4, accumulated bits -1
265 ! remainder is positive
268 add %o2, (-1*2+1), %o2
271 ! remainder is negative
274 add %o2, (-1*2-1), %o2
278 ! remainder is negative
280 ! depth 4, accumulated bits -3
283 ! remainder is positive
286 add %o2, (-3*2+1), %o2
289 ! remainder is negative
292 add %o2, (-3*2-1), %o2
297 ! remainder is negative
299 ! depth 3, accumulated bits -3
302 ! remainder is positive
304 ! depth 4, accumulated bits -5
307 ! remainder is positive
310 add %o2, (-5*2+1), %o2
313 ! remainder is negative
316 add %o2, (-5*2-1), %o2
320 ! remainder is negative
322 ! depth 4, accumulated bits -7
325 ! remainder is positive
328 add %o2, (-7*2+1), %o2
331 ! remainder is negative
334 add %o2, (-7*2-1), %o2
345 ! non-restoring fixup here (one instruction only!)