2 * udiv.S: This routine was taken from glibc-1.09 and is covered
3 * by the GNU Library General Public License Version 2.
7 /* This file is generated from divrem.m4; DO NOT EDIT! */
9 * Division and remainder, from Appendix E of the Sparc Version 8
10 * Architecture Manual, with fixes from Gordon Irlam.
14 * Input: dividend and divisor in %o0 and %o1 respectively.
17 * .udiv name of function to generate
18 * div div=div => %o0 / %o1; div=rem => %o0 % %o1
19 * false false=true => signed; false=false => unsigned
21 * Algorithm parameters:
22 * N how many bits per iteration we try to get (4)
23 * WORDSIZE total number of bits (32)
26 * TOPBITS number of bits in the top decade of a number
28 * Important variables:
29 * Q the partial quotient under development (initially 0)
30 * R the remainder so far, initially the dividend
31 * ITER number of main division loop iterations required;
32 * equal to ceil(log2(quotient) / N). Note that this
33 * is the log base (2^N) of the quotient.
34 * V the current comparand, initially divisor*2^(ITER*N-1)
37 * Current estimate for non-large dividend is
38 * ceil(log2(quotient) / N) * (10 + 7N/2) + C
39 * A large dividend is one greater than 2^(31-TOPBITS) and takes a
40 * different path, as the upper bits of the quotient must be developed
48 _Udiv: /* needed for export */
50 ! Ready to divide. Compute size of quotient; scale comparand.
55 ! Divide by zero trap. If it returns, return 0 (about as
56 ! wrong as possible, but that is what SunOS does...).
62 cmp %o3, %o5 ! if %o1 exceeds %o0, done
63 blu Lgot_result ! (and algorithm fails otherwise)
66 sethi %hi(1 << (32 - 4 - 1)), %g1
72 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
73 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
74 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
75 ! Compute ITER in an unorthodox manner: know we need to shift V into
76 ! the top decade: so do not even bother to compare to R.
93 ! We get here if the %o1 overflowed while shifting.
94 ! This means that %o3 has the high-order bit set.
95 ! Restore %o5 and subtract from %o3.
96 sll %g1, 4, %g1 ! high order bit
97 srl %o5, 1, %o5 ! rest of %o5
111 /* NB: these are commented out in the V8-Sparc manual as well */
112 /* (I do not understand this) */
113 ! %o5 > %o3: went too far: back up 1 step
116 ! do single-bit divide steps
118 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
119 ! first divide step without thinking. BUT, the others are conditional,
120 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
121 ! order bit set in the first step, just falling into the regular
122 ! division loop will mess up the first time around.
123 ! So we unroll slightly...
126 bl Lend_regular_divide
132 b Lend_single_divloop
151 b,a Lend_regular_divide
164 tst %o3 ! set up for initial iteration
167 ! depth 1, accumulated bits 0
170 ! remainder is positive
172 ! depth 2, accumulated bits 1
175 ! remainder is positive
177 ! depth 3, accumulated bits 3
180 ! remainder is positive
182 ! depth 4, accumulated bits 7
185 ! remainder is positive
188 add %o2, (7*2+1), %o2
191 ! remainder is negative
194 add %o2, (7*2-1), %o2
197 ! remainder is negative
199 ! depth 4, accumulated bits 5
202 ! remainder is positive
205 add %o2, (5*2+1), %o2
208 ! remainder is negative
211 add %o2, (5*2-1), %o2
214 ! remainder is negative
216 ! depth 3, accumulated bits 1
219 ! remainder is positive
221 ! depth 4, accumulated bits 3
224 ! remainder is positive
227 add %o2, (3*2+1), %o2
230 ! remainder is negative
233 add %o2, (3*2-1), %o2
236 ! remainder is negative
238 ! depth 4, accumulated bits 1
241 ! remainder is positive
244 add %o2, (1*2+1), %o2
247 ! remainder is negative
250 add %o2, (1*2-1), %o2
253 ! remainder is negative
255 ! depth 2, accumulated bits -1
258 ! remainder is positive
260 ! depth 3, accumulated bits -1
263 ! remainder is positive
265 ! depth 4, accumulated bits -1
268 ! remainder is positive
271 add %o2, (-1*2+1), %o2
274 ! remainder is negative
277 add %o2, (-1*2-1), %o2
280 ! remainder is negative
282 ! depth 4, accumulated bits -3
285 ! remainder is positive
288 add %o2, (-3*2+1), %o2
291 ! remainder is negative
294 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
319 ! remainder is negative
321 ! depth 4, accumulated bits -7
324 ! remainder is positive
327 add %o2, (-7*2+1), %o2
330 ! remainder is negative
333 add %o2, (-7*2-1), %o2
342 ! non-restoring fixup here (one instruction only!)