1
/* $Id: udiv.S,v 1.2 2001/06/06 17:04:50 bencollins Exp $
2
* udiv.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
* .udiv name of function to generate
18
* div div=div => %o0 / %o1; div=rem => %o0 % %o1
19
* false false=true => signed; false=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
! 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...).
55
! ta ST_DIV0 -- Not for SILO
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!)