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
44 #include <machine/trap.h>
49 ! Ready to divide. Compute size of quotient; scale comparand.
54 ! Divide by zero trap. If it returns, return 0 (about as
55 ! wrong as possible, but that is what SunOS does...).
61 cmp %o3, %o5 ! if %o1 exceeds %o0, done
62 blu Lgot_result ! (and algorithm fails otherwise)
64 sethi %hi(1 << (32 - 4 - 1)), %g1
69 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
70 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
71 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
72 ! Compute ITER in an unorthodox manner: know we need to shift V into
73 ! the top decade: so do not even bother to compare to R.
83 2: addcc %o5, %o5, %o5
87 ! We get here if the %o1 overflowed while shifting.
88 ! This means that %o3 has the high-order bit set.
89 ! Restore %o5 and subtract from %o3.
90 sll %g1, 4, %g1 ! high order bit
91 srl %o5, 1, %o5 ! rest of %o5
102 /* NB: these are commented out in the V8-Sparc manual as well */
103 /* (I do not understand this) */
104 ! %o5 > %o3: went too far: back up 1 step
107 ! do single-bit divide steps
109 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
110 ! first divide step without thinking. BUT, the others are conditional,
111 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
112 ! order bit set in the first step, just falling into the regular
113 ! division loop will mess up the first time around.
114 ! So we unroll slightly...
117 bl Lend_regular_divide
121 b Lend_single_divloop
139 b,a Lend_regular_divide
150 tst %o3 ! set up for initial iteration
153 ! depth 1, accumulated bits 0
156 ! remainder is positive
158 ! depth 2, accumulated bits 1
161 ! remainder is positive
163 ! depth 3, accumulated bits 3
166 ! remainder is positive
168 ! depth 4, accumulated bits 7
171 ! remainder is positive
174 add %o2, (7*2+1), %o2
177 ! remainder is negative
180 add %o2, (7*2-1), %o2
184 ! remainder is negative
186 ! depth 4, accumulated bits 5
189 ! remainder is positive
192 add %o2, (5*2+1), %o2
195 ! remainder is negative
198 add %o2, (5*2-1), %o2
203 ! remainder is negative
205 ! depth 3, accumulated bits 1
208 ! remainder is positive
210 ! depth 4, accumulated bits 3
213 ! remainder is positive
216 add %o2, (3*2+1), %o2
219 ! remainder is negative
222 add %o2, (3*2-1), %o2
226 ! remainder is negative
228 ! depth 4, accumulated bits 1
231 ! remainder is positive
234 add %o2, (1*2+1), %o2
237 ! remainder is negative
240 add %o2, (1*2-1), %o2
246 ! remainder is negative
248 ! depth 2, accumulated bits -1
251 ! remainder is positive
253 ! depth 3, accumulated bits -1
256 ! remainder is positive
258 ! depth 4, accumulated bits -1
261 ! remainder is positive
264 add %o2, (-1*2+1), %o2
267 ! remainder is negative
270 add %o2, (-1*2-1), %o2
274 ! remainder is negative
276 ! depth 4, accumulated bits -3
279 ! remainder is positive
282 add %o2, (-3*2+1), %o2
285 ! remainder is negative
288 add %o2, (-3*2-1), %o2
293 ! remainder is negative
295 ! depth 3, accumulated bits -3
298 ! remainder is positive
300 ! depth 4, accumulated bits -5
303 ! remainder is positive
306 add %o2, (-5*2+1), %o2
309 ! remainder is negative
312 add %o2, (-5*2-1), %o2
316 ! remainder is negative
318 ! depth 4, accumulated bits -7
321 ! remainder is positive
324 add %o2, (-7*2+1), %o2
327 ! remainder is negative
330 add %o2, (-7*2-1), %o2
341 ! non-restoring fixup here (one instruction only!)