1
/* $Id: rem.S,v 1.1 2003/04/14 03:24:43 bencollins 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
44
.register %g2,#scratch
48
! compute sign of result; if neither is negative, no problem
49
orcc %o1, %o0, %g0 ! either negative?
50
bge 2f ! no, go do the divide
51
mov %o0, %g2 ! compute sign in any case
56
! %o1 is definitely negative; %o0 might also be negative
57
bge 2f ! if %o0 not negative...
58
sub %g0, %o1, %o1 ! in any case, make %o1 nonneg
59
1: ! %o0 is negative, %o1 is nonnegative
60
sub %g0, %o0, %o0 ! make %o0 nonnegative
63
! Ready to divide. Compute size of quotient; scale comparand.
68
! Divide by zero trap. If it returns, return 0 (about as
69
! wrong as possible, but that is what SunOS does...).
70
! ta ST_DIV0 -- Not for SILO
75
cmp %o3, %o5 ! if %o1 exceeds %o0, done
76
blu Lgot_result ! (and algorithm fails otherwise)
79
sethi %hi(1 << (32 - 4 - 1)), %g1
85
! Here the dividend is >= 2**(31-N) or so. We must be careful here,
86
! as our usual N-at-a-shot divide step will cause overflow and havoc.
87
! The number of bits in the result here is N*ITER+SC, where SC <= N.
88
! Compute ITER in an unorthodox manner: know we need to shift V into
89
! 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
178
tst %o3 ! set up for initial iteration
181
! depth 1, accumulated bits 0
184
! remainder is positive
186
! depth 2, accumulated bits 1
189
! remainder is positive
191
! depth 3, accumulated bits 3
194
! remainder is positive
196
! depth 4, accumulated bits 7
199
! 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
251
! remainder is negative
253
! depth 4, accumulated bits 1
256
! remainder is positive
259
add %o2, (1*2+1), %o2
262
! remainder is negative
265
add %o2, (1*2-1), %o2
268
! remainder is negative
270
! depth 2, accumulated bits -1
273
! remainder is positive
275
! depth 3, accumulated bits -1
278
! remainder is positive
280
! depth 4, accumulated bits -1
283
! remainder is positive
286
add %o2, (-1*2+1), %o2
289
! remainder is negative
292
add %o2, (-1*2-1), %o2
295
! remainder is negative
297
! depth 4, accumulated bits -3
300
! remainder is positive
303
add %o2, (-3*2+1), %o2
306
! remainder is negative
309
add %o2, (-3*2-1), %o2
312
! remainder is negative
314
! depth 3, accumulated bits -3
317
! remainder is positive
319
! depth 4, accumulated bits -5
322
! remainder is positive
325
add %o2, (-5*2+1), %o2
328
! remainder is negative
331
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
380
1: smul %o2, %o1, %o2