1 /* $Id: sdiv.S,v 1.6 1996/10/02 17:37:00 davem Exp $
2 * sdiv.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 * .div name of function to generate
18 * div div=div => %o0 / %o1; div=rem => %o0 % %o1
19 * true true=true => signed; true=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
47 ! compute sign of result; if neither is negative, no problem
48 orcc %o1, %o0, %g0 ! either negative?
49 bge 2f ! no, go do the divide
50 xor %o1, %o0, %g2 ! compute sign in any case
55 ! %o1 is definitely negative; %o0 might also be negative
56 bge 2f ! if %o0 not negative...
57 sub %g0, %o1, %o1 ! in any case, make %o1 nonneg
58 1: ! %o0 is negative, %o1 is nonnegative
59 sub %g0, %o0, %o0 ! make %o0 nonnegative
62 ! Ready to divide. Compute size of quotient; scale comparand.
67 ! Divide by zero trap. If it returns, return 0 (about as
68 ! wrong as possible, but that is what SunOS does...).
74 cmp %o3, %o5 ! if %o1 exceeds %o0, done
75 blu Lgot_result ! (and algorithm fails otherwise)
78 sethi %hi(1 << (32 - 4 - 1)), %g1
84 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
85 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
86 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
87 ! Compute ITER in an unorthodox manner: know we need to shift V into
88 ! the top decade: so do not even bother to compare to R.
105 ! We get here if the %o1 overflowed while shifting.
106 ! This means that %o3 has the high-order bit set.
107 ! Restore %o5 and subtract from %o3.
108 sll %g1, 4, %g1 ! high order bit
109 srl %o5, 1, %o5 ! rest of %o5
123 /* NB: these are commented out in the V8-Sparc manual as well */
124 /* (I do not understand this) */
125 ! %o5 > %o3: went too far: back up 1 step
128 ! do single-bit divide steps
130 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
131 ! first divide step without thinking. BUT, the others are conditional,
132 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
133 ! order bit set in the first step, just falling into the regular
134 ! division loop will mess up the first time around.
135 ! So we unroll slightly...
138 bl Lend_regular_divide
144 b Lend_single_divloop
165 b,a Lend_regular_divide
177 tst %o3 ! set up for initial iteration
180 ! depth 1, accumulated bits 0
183 ! remainder is positive
185 ! depth 2, accumulated bits 1
188 ! remainder is positive
190 ! depth 3, accumulated bits 3
193 ! remainder is positive
195 ! depth 4, accumulated bits 7
198 ! remainder is positive
201 add %o2, (7*2+1), %o2
204 ! remainder is negative
207 add %o2, (7*2-1), %o2
210 ! remainder is negative
212 ! depth 4, accumulated bits 5
215 ! remainder is positive
218 add %o2, (5*2+1), %o2
221 ! remainder is negative
224 add %o2, (5*2-1), %o2
227 ! remainder is negative
229 ! depth 3, accumulated bits 1
232 ! remainder is positive
234 ! depth 4, accumulated bits 3
237 ! remainder is positive
240 add %o2, (3*2+1), %o2
243 ! remainder is negative
246 add %o2, (3*2-1), %o2
250 ! remainder is negative
252 ! depth 4, accumulated bits 1
255 ! remainder is positive
258 add %o2, (1*2+1), %o2
261 ! remainder is negative
264 add %o2, (1*2-1), %o2
267 ! remainder is negative
269 ! depth 2, accumulated bits -1
272 ! remainder is positive
274 ! depth 3, accumulated bits -1
277 ! remainder is positive
279 ! depth 4, accumulated bits -1
282 ! remainder is positive
285 add %o2, (-1*2+1), %o2
288 ! remainder is negative
291 add %o2, (-1*2-1), %o2
294 ! remainder is negative
296 ! depth 4, accumulated bits -3
299 ! remainder is positive
302 add %o2, (-3*2+1), %o2
305 ! remainder is negative
308 add %o2, (-3*2-1), %o2
311 ! remainder is negative
313 ! depth 3, accumulated bits -3
316 ! remainder is positive
318 ! depth 4, accumulated bits -5
321 ! remainder is positive
324 add %o2, (-5*2+1), %o2
327 ! remainder is negative
330 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
356 ! non-restoring fixup here (one instruction only!)
360 ! check to see if answer should be < 0