1 /* $Id: rem.S,v 1.7 1996/09/30 02:22:34 davem Exp $
2 * rem.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 * .rem name of function to generate
18 * rem rem=div => %o0 / %o1; rem=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 mov %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.
106 ! We get here if the %o1 overflowed while shifting.
107 ! This means that %o3 has the high-order bit set.
108 ! Restore %o5 and subtract from %o3.
109 sll %g1, 4, %g1 ! high order bit
110 srl %o5, 1, %o5 ! rest of %o5
124 /* NB: these are commented out in the V8-Sparc manual as well */
125 /* (I do not understand this) */
126 ! %o5 > %o3: went too far: back up 1 step
129 ! do single-bit divide steps
131 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
132 ! first divide step without thinking. BUT, the others are conditional,
133 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
134 ! order bit set in the first step, just falling into the regular
135 ! division loop will mess up the first time around.
136 ! So we unroll slightly...
139 bl Lend_regular_divide
145 b Lend_single_divloop
166 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
202 add %o2, (7*2+1), %o2
205 ! remainder is negative
208 add %o2, (7*2-1), %o2
211 ! remainder is negative
213 ! depth 4, accumulated bits 5
216 ! remainder is positive
219 add %o2, (5*2+1), %o2
222 ! remainder is negative
225 add %o2, (5*2-1), %o2
228 ! remainder is negative
230 ! depth 3, accumulated bits 1
233 ! remainder is positive
235 ! depth 4, accumulated bits 3
238 ! remainder is positive
241 add %o2, (3*2+1), %o2
244 ! remainder is negative
247 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
334 ! remainder is negative
336 ! depth 4, accumulated bits -7
339 ! remainder is positive
342 add %o2, (-7*2+1), %o2
345 ! remainder is negative
348 add %o2, (-7*2-1), %o2
357 ! non-restoring fixup here (one instruction only!)
361 ! check to see if answer should be < 0
379 1: smul %o2, %o1, %o2