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
48 _Div: /* 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 xor %o1, %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.
107 ! We get here if the %o1 overflowed while shifting.
108 ! This means that %o3 has the high-order bit set.
109 ! Restore %o5 and subtract from %o3.
110 sll %g1, 4, %g1 ! high order bit
111 srl %o5, 1, %o5 ! rest of %o5
125 /* NB: these are commented out in the V8-Sparc manual as well */
126 /* (I do not understand this) */
127 ! %o5 > %o3: went too far: back up 1 step
130 ! do single-bit divide steps
132 ! We have to be careful here. We know that %o3 >= %o5, so we can do the
133 ! first divide step without thinking. BUT, the others are conditional,
134 ! and are only done if %o3 >= 0. Because both %o3 and %o5 may have the high-
135 ! order bit set in the first step, just falling into the regular
136 ! division loop will mess up the first time around.
137 ! So we unroll slightly...
140 bl Lend_regular_divide
146 b Lend_single_divloop
167 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
203 add %o2, (7*2+1), %o2
206 ! remainder is negative
209 add %o2, (7*2-1), %o2
212 ! remainder is negative
214 ! depth 4, accumulated bits 5
217 ! remainder is positive
220 add %o2, (5*2+1), %o2
223 ! remainder is negative
226 add %o2, (5*2-1), %o2
229 ! remainder is negative
231 ! depth 3, accumulated bits 1
234 ! remainder is positive
236 ! depth 4, accumulated bits 3
239 ! remainder is positive
242 add %o2, (3*2+1), %o2
245 ! remainder is negative
248 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
335 ! remainder is negative
337 ! depth 4, accumulated bits -7
340 ! remainder is positive
343 add %o2, (-7*2+1), %o2
346 ! remainder is negative
349 add %o2, (-7*2-1), %o2
358 ! non-restoring fixup here (one instruction only!)
362 ! check to see if answer should be < 0