1 /* $Id: urem.S,v 1.4 1996/09/30 02:22:42 davem Exp $
2 * urem.S: This routine was taken from glibc-1.09 and is covered
3 * by the GNU Library General Public License Version 2.
6 /* This file is generated from divrem.m4; DO NOT EDIT! */
8 * Division and remainder, from Appendix E of the Sparc Version 8
9 * Architecture Manual, with fixes from Gordon Irlam.
13 * Input: dividend and divisor in %o0 and %o1 respectively.
16 * .urem name of function to generate
17 * rem rem=div => %o0 / %o1; rem=rem => %o0 % %o1
18 * false false=true => signed; false=false => unsigned
20 * Algorithm parameters:
21 * N how many bits per iteration we try to get (4)
22 * WORDSIZE total number of bits (32)
25 * TOPBITS number of bits in the top decade of a number
27 * Important variables:
28 * Q the partial quotient under development (initially 0)
29 * R the remainder so far, initially the dividend
30 * ITER number of main division loop iterations required;
31 * equal to ceil(log2(quotient) / N). Note that this
32 * is the log base (2^N) of the quotient.
33 * V the current comparand, initially divisor*2^(ITER*N-1)
36 * Current estimate for non-large dividend is
37 * ceil(log2(quotient) / N) * (10 + 7N/2) + C
38 * A large dividend is one greater than 2^(31-TOPBITS) and takes a
39 * different path, as the upper bits of the quotient must be developed
46 _Urem: /* needed for export */
48 ! Ready to divide. Compute size of quotient; scale comparand.
53 ! Divide by zero trap. If it returns, return 0 (about as
54 ! wrong as possible, but that is what SunOS does...).
60 cmp %o3, %o5 ! if %o1 exceeds %o0, done
61 blu Lgot_result ! (and algorithm fails otherwise)
64 sethi %hi(1 << (32 - 4 - 1)), %g1
70 ! Here the dividend is >= 2**(31-N) or so. We must be careful here,
71 ! as our usual N-at-a-shot divide step will cause overflow and havoc.
72 ! The number of bits in the result here is N*ITER+SC, where SC <= N.
73 ! Compute ITER in an unorthodox manner: know we need to shift V into
74 ! the top decade: so do not even bother to compare to R.
91 ! We get here if the %o1 overflowed while shifting.
92 ! This means that %o3 has the high-order bit set.
93 ! Restore %o5 and subtract from %o3.
94 sll %g1, 4, %g1 ! high order bit
95 srl %o5, 1, %o5 ! rest of %o5
109 /* NB: these are commented out in the V8-Sparc manual as well */
110 /* (I do not understand this) */
111 ! %o5 > %o3: went too far: back up 1 step
114 ! do single-bit divide steps
116 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
117 ! first divide step without thinking. BUT, the others are conditional,
118 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
119 ! order bit set in the first step, just falling into the regular
120 ! division loop will mess up the first time around.
121 ! So we unroll slightly...
124 bl Lend_regular_divide
130 b Lend_single_divloop
149 b,a Lend_regular_divide
162 tst %o3 ! set up for initial iteration
165 ! depth 1, accumulated bits 0
168 ! remainder is positive
170 ! depth 2, accumulated bits 1
173 ! remainder is positive
175 ! depth 3, accumulated bits 3
178 ! remainder is positive
180 ! depth 4, accumulated bits 7
183 ! remainder is positive
186 add %o2, (7*2+1), %o2
189 ! remainder is negative
192 add %o2, (7*2-1), %o2
195 ! remainder is negative
197 ! depth 4, accumulated bits 5
200 ! remainder is positive
203 add %o2, (5*2+1), %o2
206 ! remainder is negative
209 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
234 ! remainder is negative
236 ! depth 4, accumulated bits 1
239 ! remainder is positive
242 add %o2, (1*2+1), %o2
245 ! remainder is negative
248 add %o2, (1*2-1), %o2
251 ! remainder is negative
253 ! depth 2, accumulated bits -1
256 ! remainder is positive
258 ! depth 3, accumulated bits -1
261 ! remainder is positive
263 ! depth 4, accumulated bits -1
266 ! remainder is positive
269 add %o2, (-1*2+1), %o2
272 ! remainder is negative
275 add %o2, (-1*2-1), %o2
278 ! remainder is negative
280 ! depth 4, accumulated bits -3
283 ! remainder is positive
286 add %o2, (-3*2+1), %o2
289 ! remainder is negative
292 add %o2, (-3*2-1), %o2
295 ! remainder is negative
297 ! depth 3, accumulated bits -3
300 ! remainder is positive
302 ! depth 4, accumulated bits -5
305 ! remainder is positive
308 add %o2, (-5*2+1), %o2
311 ! remainder is negative
314 add %o2, (-5*2-1), %o2
317 ! remainder is negative
319 ! depth 4, accumulated bits -7
322 ! remainder is positive
325 add %o2, (-7*2+1), %o2
328 ! remainder is negative
331 add %o2, (-7*2-1), %o2
340 ! non-restoring fixup here (one instruction only!)