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 * .urem name of function to generate
12 * rem rem=div => %o0 / %o1; rem=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
45 ! Ready to divide. Compute size of quotient; scale comparand.
50 ! Divide by zero trap. If it returns, return 0 (about as
51 ! wrong as possible, but that is what SunOS does...).
57 cmp %o3, %o5 ! if %o1 exceeds %o0, done
58 blu LOC(got_result) ! (and algorithm fails otherwise)
60 sethi %hi(1 << (32 - 4 - 1)), %g1
62 blu LOC(not_really_big)
65 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
66 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
67 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
68 ! Compute ITER in an unorthodox manner: know we need to shift V into
69 ! the top decade: so do not even bother to compare to R.
79 2: addcc %o5, %o5, %o5
83 ! We get here if the %o1 overflowed while shifting.
84 ! This means that %o3 has the high-order bit set.
85 ! Restore %o5 and subtract from %o3.
86 sll %g1, 4, %g1 ! high order bit
87 srl %o5, 1, %o5 ! rest of %o5
98 /* NB: these are commented out in the V8-Sparc manual as well */
99 /* (I do not understand this) */
100 ! %o5 > %o3: went too far: back up 1 step
103 ! do single-bit divide steps
105 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
106 ! first divide step without thinking. BUT, the others are conditional,
107 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
108 ! order bit set in the first step, just falling into the regular
109 ! division loop will mess up the first time around.
110 ! So we unroll slightly...
113 bl LOC(end_regular_divide)
117 b LOC(end_single_divloop)
131 LOC(end_single_divloop):
133 bge LOC(single_divloop)
135 b,a LOC(end_regular_divide)
146 tst %o3 ! set up for initial iteration
149 ! depth 1, accumulated bits 0
152 ! remainder is positive
154 ! depth 2, accumulated bits 1
157 ! remainder is positive
159 ! depth 3, accumulated bits 3
162 ! remainder is positive
164 ! depth 4, accumulated bits 7
167 ! remainder is positive
170 add %o2, (7*2+1), %o2
173 ! remainder is negative
176 add %o2, (7*2-1), %o2
180 ! remainder is negative
182 ! depth 4, accumulated bits 5
185 ! remainder is positive
188 add %o2, (5*2+1), %o2
191 ! remainder is negative
194 add %o2, (5*2-1), %o2
199 ! remainder is negative
201 ! depth 3, accumulated bits 1
204 ! remainder is positive
206 ! depth 4, accumulated bits 3
209 ! remainder is positive
212 add %o2, (3*2+1), %o2
215 ! remainder is negative
218 add %o2, (3*2-1), %o2
222 ! remainder is negative
224 ! depth 4, accumulated bits 1
227 ! remainder is positive
230 add %o2, (1*2+1), %o2
233 ! remainder is negative
236 add %o2, (1*2-1), %o2
242 ! remainder is negative
244 ! depth 2, accumulated bits -1
247 ! remainder is positive
249 ! depth 3, accumulated bits -1
252 ! remainder is positive
254 ! depth 4, accumulated bits -1
257 ! remainder is positive
260 add %o2, (-1*2+1), %o2
263 ! remainder is negative
266 add %o2, (-1*2-1), %o2
270 ! remainder is negative
272 ! depth 4, accumulated bits -3
275 ! remainder is positive
278 add %o2, (-3*2+1), %o2
281 ! remainder is negative
284 add %o2, (-3*2-1), %o2
289 ! remainder is negative
291 ! depth 3, accumulated bits -3
294 ! remainder is positive
296 ! depth 4, accumulated bits -5
299 ! remainder is positive
302 add %o2, (-5*2+1), %o2
305 ! remainder is negative
308 add %o2, (-5*2-1), %o2
312 ! remainder is negative
314 ! depth 4, accumulated bits -7
317 ! remainder is positive
320 add %o2, (-7*2+1), %o2
323 ! remainder is negative
326 add %o2, (-7*2-1), %o2
332 LOC(end_regular_divide):
337 ! non-restoring fixup here (one instruction only!)