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 ! Ready to divide. Compute size of quotient; scale comparand.
47 ! Divide by zero trap. If it returns, return 0 (about as
48 ! wrong as possible, but that is what SunOS does...).
54 cmp %o3, %o5 ! if %o1 exceeds %o0, done
55 blu LOC(got_result) ! (and algorithm fails otherwise)
57 sethi %hi(1 << (32 - 4 - 1)), %g1
59 blu LOC(not_really_big)
62 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
63 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
64 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
65 ! Compute ITER in an unorthodox manner: know we need to shift V into
66 ! the top decade: so do not even bother to compare to R.
76 2: addcc %o5, %o5, %o5
80 ! We get here if the %o1 overflowed while shifting.
81 ! This means that %o3 has the high-order bit set.
82 ! Restore %o5 and subtract from %o3.
83 sll %g1, 4, %g1 ! high order bit
84 srl %o5, 1, %o5 ! rest of %o5
95 /* NB: these are commented out in the V8-Sparc manual as well */
96 /* (I do not understand this) */
97 ! %o5 > %o3: went too far: back up 1 step
100 ! do single-bit divide steps
102 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
103 ! first divide step without thinking. BUT, the others are conditional,
104 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
105 ! order bit set in the first step, just falling into the regular
106 ! division loop will mess up the first time around.
107 ! So we unroll slightly...
110 bl LOC(end_regular_divide)
114 b LOC(end_single_divloop)
128 LOC(end_single_divloop):
130 bge LOC(single_divloop)
132 b,a LOC(end_regular_divide)
143 tst %o3 ! set up for initial iteration
146 ! depth 1, accumulated bits 0
149 ! remainder is positive
151 ! depth 2, accumulated bits 1
154 ! remainder is positive
156 ! depth 3, accumulated bits 3
159 ! remainder is positive
161 ! depth 4, accumulated bits 7
164 ! remainder is positive
167 add %o2, (7*2+1), %o2
170 ! remainder is negative
173 add %o2, (7*2-1), %o2
177 ! remainder is negative
179 ! depth 4, accumulated bits 5
182 ! remainder is positive
185 add %o2, (5*2+1), %o2
188 ! remainder is negative
191 add %o2, (5*2-1), %o2
196 ! remainder is negative
198 ! depth 3, accumulated bits 1
201 ! remainder is positive
203 ! depth 4, accumulated bits 3
206 ! remainder is positive
209 add %o2, (3*2+1), %o2
212 ! remainder is negative
215 add %o2, (3*2-1), %o2
219 ! remainder is negative
221 ! depth 4, accumulated bits 1
224 ! remainder is positive
227 add %o2, (1*2+1), %o2
230 ! remainder is negative
233 add %o2, (1*2-1), %o2
239 ! remainder is negative
241 ! depth 2, accumulated bits -1
244 ! remainder is positive
246 ! depth 3, accumulated bits -1
249 ! remainder is positive
251 ! depth 4, accumulated bits -1
254 ! remainder is positive
257 add %o2, (-1*2+1), %o2
260 ! remainder is negative
263 add %o2, (-1*2-1), %o2
267 ! remainder is negative
269 ! depth 4, accumulated bits -3
272 ! remainder is positive
275 add %o2, (-3*2+1), %o2
278 ! remainder is negative
281 add %o2, (-3*2-1), %o2
286 ! remainder is negative
288 ! depth 3, accumulated bits -3
291 ! remainder is positive
293 ! depth 4, accumulated bits -5
296 ! remainder is positive
299 add %o2, (-5*2+1), %o2
302 ! remainder is negative
305 add %o2, (-5*2-1), %o2
309 ! remainder is negative
311 ! depth 4, accumulated bits -7
314 ! remainder is positive
317 add %o2, (-7*2+1), %o2
320 ! remainder is negative
323 add %o2, (-7*2-1), %o2
329 LOC(end_regular_divide):
334 ! non-restoring fixup here (one instruction only!)