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 * .div name of function to generate
12 * div div=div => %o0 / %o1; div=rem => %o0 % %o1
13 * true true=true => signed; true=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>
52 ! compute sign of result; if neither is negative, no problem
53 orcc %o1, %o0, %g0 ! either negative?
54 bge 2f ! no, go do the divide
55 xor %o1, %o0, %g6 ! compute sign in any case
59 ! %o1 is definitely negative; %o0 might also be negative
60 bge 2f ! if %o0 not negative...
61 sub %g0, %o1, %o1 ! in any case, make %o1 nonneg
62 1: ! %o0 is negative, %o1 is nonnegative
63 sub %g0, %o0, %o0 ! make %o0 nonnegative
66 ! Ready to divide. Compute size of quotient; scale comparand.
71 ! Divide by zero trap. If it returns, return 0 (about as
72 ! wrong as possible, but that is what SunOS does...).
78 cmp %o3, %o5 ! if %o1 exceeds %o0, done
79 blu Lgot_result ! (and algorithm fails otherwise)
81 sethi %hi(1 << (32 - 4 - 1)), %g1
86 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
87 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
88 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
89 ! Compute ITER in an unorthodox manner: know we need to shift V into
90 ! the top decade: so do not even bother to compare to R.
100 2: addcc %o5, %o5, %o5
104 ! We get here if the %o1 overflowed while shifting.
105 ! This means that %o3 has the high-order bit set.
106 ! Restore %o5 and subtract from %o3.
107 sll %g1, 4, %g1 ! high order bit
108 srl %o5, 1, %o5 ! rest of %o5
119 /* NB: these are commented out in the V8-Sparc manual as well */
120 /* (I do not understand this) */
121 ! %o5 > %o3: went too far: back up 1 step
124 ! do single-bit divide steps
126 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
127 ! first divide step without thinking. BUT, the others are conditional,
128 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
129 ! order bit set in the first step, just falling into the regular
130 ! division loop will mess up the first time around.
131 ! So we unroll slightly...
134 bl Lend_regular_divide
138 b Lend_single_divloop
156 b,a Lend_regular_divide
167 tst %o3 ! set up for initial iteration
170 ! depth 1, accumulated bits 0
173 ! remainder is positive
175 ! depth 2, accumulated bits 1
178 ! remainder is positive
180 ! depth 3, accumulated bits 3
183 ! remainder is positive
185 ! depth 4, accumulated bits 7
188 ! remainder is positive
191 add %o2, (7*2+1), %o2
194 ! remainder is negative
197 add %o2, (7*2-1), %o2
201 ! remainder is negative
203 ! depth 4, accumulated bits 5
206 ! remainder is positive
209 add %o2, (5*2+1), %o2
212 ! remainder is negative
215 add %o2, (5*2-1), %o2
220 ! remainder is negative
222 ! depth 3, accumulated bits 1
225 ! remainder is positive
227 ! depth 4, accumulated bits 3
230 ! remainder is positive
233 add %o2, (3*2+1), %o2
236 ! remainder is negative
239 add %o2, (3*2-1), %o2
243 ! remainder is negative
245 ! depth 4, accumulated bits 1
248 ! remainder is positive
251 add %o2, (1*2+1), %o2
254 ! remainder is negative
257 add %o2, (1*2-1), %o2
263 ! remainder is negative
265 ! depth 2, accumulated bits -1
268 ! remainder is positive
270 ! depth 3, accumulated bits -1
273 ! remainder is positive
275 ! depth 4, accumulated bits -1
278 ! remainder is positive
281 add %o2, (-1*2+1), %o2
284 ! remainder is negative
287 add %o2, (-1*2-1), %o2
291 ! remainder is negative
293 ! depth 4, accumulated bits -3
296 ! remainder is positive
299 add %o2, (-3*2+1), %o2
302 ! remainder is negative
305 add %o2, (-3*2-1), %o2
310 ! remainder is negative
312 ! depth 3, accumulated bits -3
315 ! remainder is positive
317 ! depth 4, accumulated bits -5
320 ! remainder is positive
323 add %o2, (-5*2+1), %o2
326 ! remainder is negative
329 add %o2, (-5*2-1), %o2
333 ! remainder is negative
335 ! depth 4, accumulated bits -7
338 ! remainder is positive
341 add %o2, (-7*2+1), %o2
344 ! remainder is negative
347 add %o2, (-7*2-1), %o2
358 ! non-restoring fixup here (one instruction only!)
363 ! check to see if answer should be < 0