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 * .rem name of function to generate
12 * rem rem=div => %o0 / %o1; rem=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
44 ! compute sign of result; if neither is negative, no problem
45 orcc %o1, %o0, %g0 ! either negative?
46 bge 2f ! no, go do the divide
47 mov %o0, %g3 ! sign of remainder matches %o0
51 ! %o1 is definitely negative; %o0 might also be negative
52 bge 2f ! if %o0 not negative...
53 sub %g0, %o1, %o1 ! in any case, make %o1 nonneg
54 1: ! %o0 is negative, %o1 is nonnegative
55 sub %g0, %o0, %o0 ! make %o0 nonnegative
58 ! Ready to divide. Compute size of quotient; scale comparand.
63 ! Divide by zero trap. If it returns, return 0 (about as
64 ! wrong as possible, but that is what SunOS does...).
70 cmp %o3, %o5 ! if %o1 exceeds %o0, done
71 blu LOC(got_result) ! (and algorithm fails otherwise)
73 sethi %hi(1 << (32 - 4 - 1)), %g1
75 blu LOC(not_really_big)
78 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
79 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
80 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
81 ! Compute ITER in an unorthodox manner: know we need to shift V into
82 ! the top decade: so do not even bother to compare to R.
92 2: addcc %o5, %o5, %o5
96 ! We get here if the %o1 overflowed while shifting.
97 ! This means that %o3 has the high-order bit set.
98 ! Restore %o5 and subtract from %o3.
99 sll %g1, 4, %g1 ! high order bit
100 srl %o5, 1, %o5 ! rest of %o5
109 be LOC(do_single_div)
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 LOC(end_regular_divide)
130 b LOC(end_single_divloop)
144 LOC(end_single_divloop):
146 bge LOC(single_divloop)
148 b,a LOC(end_regular_divide)
159 tst %o3 ! set up for initial iteration
162 ! depth 1, accumulated bits 0
165 ! remainder is positive
167 ! depth 2, accumulated bits 1
170 ! remainder is positive
172 ! depth 3, accumulated bits 3
175 ! remainder is positive
177 ! depth 4, accumulated bits 7
180 ! remainder is positive
183 add %o2, (7*2+1), %o2
186 ! remainder is negative
189 add %o2, (7*2-1), %o2
193 ! remainder is negative
195 ! depth 4, accumulated bits 5
198 ! remainder is positive
201 add %o2, (5*2+1), %o2
204 ! remainder is negative
207 add %o2, (5*2-1), %o2
212 ! remainder is negative
214 ! depth 3, accumulated bits 1
217 ! remainder is positive
219 ! depth 4, accumulated bits 3
222 ! remainder is positive
225 add %o2, (3*2+1), %o2
228 ! remainder is negative
231 add %o2, (3*2-1), %o2
235 ! remainder is negative
237 ! depth 4, accumulated bits 1
240 ! remainder is positive
243 add %o2, (1*2+1), %o2
246 ! remainder is negative
249 add %o2, (1*2-1), %o2
255 ! remainder is negative
257 ! depth 2, accumulated bits -1
260 ! remainder is positive
262 ! depth 3, accumulated bits -1
265 ! remainder is positive
267 ! depth 4, accumulated bits -1
270 ! remainder is positive
273 add %o2, (-1*2+1), %o2
276 ! remainder is negative
279 add %o2, (-1*2-1), %o2
283 ! remainder is negative
285 ! depth 4, accumulated bits -3
288 ! remainder is positive
291 add %o2, (-3*2+1), %o2
294 ! remainder is negative
297 add %o2, (-3*2-1), %o2
302 ! remainder is negative
304 ! depth 3, accumulated bits -3
307 ! remainder is positive
309 ! depth 4, accumulated bits -5
312 ! remainder is positive
315 add %o2, (-5*2+1), %o2
318 ! remainder is negative
321 add %o2, (-5*2-1), %o2
325 ! remainder is negative
327 ! depth 4, accumulated bits -7
330 ! remainder is positive
333 add %o2, (-7*2+1), %o2
336 ! remainder is negative
339 add %o2, (-7*2-1), %o2
345 LOC(end_regular_divide):
350 ! non-restoring fixup here (one instruction only!)
355 ! check to see if answer should be < 0