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
48 _Rem: /* needed for export */
49 ! compute sign of result; if neither is negative, no problem
50 orcc %o1, %o0, %g0 ! either negative?
51 bge 2f ! no, go do the divide
52 mov %o0, %g2 ! compute sign in any case
57 ! %o1 is definitely negative; %o0 might also be negative
58 bge 2f ! if %o0 not negative...
59 sub %g0, %o1, %o1 ! in any case, make %o1 nonneg
60 1: ! %o0 is negative, %o1 is nonnegative
61 sub %g0, %o0, %o0 ! make %o0 nonnegative
64 ! Ready to divide. Compute size of quotient; scale comparand.
69 ! Divide by zero trap. If it returns, return 0 (about as
70 ! wrong as possible, but that is what SunOS does...).
76 cmp %o3, %o5 ! if %o1 exceeds %o0, done
77 blu Lgot_result ! (and algorithm fails otherwise)
80 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.
108 ! We get here if the %o1 overflowed while shifting.
109 ! This means that %o3 has the high-order bit set.
110 ! Restore %o5 and subtract from %o3.
111 sll %g1, 4, %g1 ! high order bit
112 srl %o5, 1, %o5 ! rest of %o5
126 /* NB: these are commented out in the V8-Sparc manual as well */
127 /* (I do not understand this) */
128 ! %o5 > %o3: went too far: back up 1 step
131 ! do single-bit divide steps
133 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
134 ! first divide step without thinking. BUT, the others are conditional,
135 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
136 ! order bit set in the first step, just falling into the regular
137 ! division loop will mess up the first time around.
138 ! So we unroll slightly...
141 bl Lend_regular_divide
147 b Lend_single_divloop
168 b,a Lend_regular_divide
179 tst %o3 ! set up for initial iteration
182 ! depth 1, accumulated bits 0
185 ! remainder is positive
187 ! depth 2, accumulated bits 1
190 ! remainder is positive
192 ! depth 3, accumulated bits 3
195 ! remainder is positive
197 ! depth 4, accumulated bits 7
200 ! remainder is positive
204 add %o2, (7*2+1), %o2
207 ! remainder is negative
210 add %o2, (7*2-1), %o2
213 ! remainder is negative
215 ! depth 4, accumulated bits 5
218 ! remainder is positive
221 add %o2, (5*2+1), %o2
224 ! remainder is negative
227 add %o2, (5*2-1), %o2
230 ! remainder is negative
232 ! depth 3, accumulated bits 1
235 ! remainder is positive
237 ! depth 4, accumulated bits 3
240 ! remainder is positive
243 add %o2, (3*2+1), %o2
246 ! remainder is negative
249 add %o2, (3*2-1), %o2
252 ! remainder is negative
254 ! depth 4, accumulated bits 1
257 ! remainder is positive
260 add %o2, (1*2+1), %o2
263 ! remainder is negative
266 add %o2, (1*2-1), %o2
269 ! remainder is negative
271 ! depth 2, accumulated bits -1
274 ! remainder is positive
276 ! depth 3, accumulated bits -1
279 ! remainder is positive
281 ! depth 4, accumulated bits -1
284 ! remainder is positive
287 add %o2, (-1*2+1), %o2
290 ! remainder is negative
293 add %o2, (-1*2-1), %o2
296 ! remainder is negative
298 ! depth 4, accumulated bits -3
301 ! remainder is positive
304 add %o2, (-3*2+1), %o2
307 ! remainder is negative
310 add %o2, (-3*2-1), %o2
313 ! remainder is negative
315 ! depth 3, accumulated bits -3
318 ! remainder is positive
320 ! depth 4, accumulated bits -5
323 ! remainder is positive
326 add %o2, (-5*2+1), %o2
329 ! remainder is negative
332 add %o2, (-5*2-1), %o2
336 ! remainder is negative
338 ! depth 4, accumulated bits -7
341 ! remainder is positive
344 add %o2, (-7*2+1), %o2
347 ! remainder is negative
350 add %o2, (-7*2-1), %o2
359 ! non-restoring fixup here (one instruction only!)
363 ! check to see if answer should be < 0
381 1: smul %o2, %o1, %o2