2.9
[glibc/nacl-glibc.git] / sysdeps / sparc / sparc32 / divrem.m4
blob30d532ad779dfbebdb4416fe81ee4dc1e70764e1
1 /*
2  * Division and remainder, from Appendix E of the Sparc Version 8
3  * Architecture Manual, with fixes from Gordon Irlam.
4  */
6 /*
7  * Input: dividend and divisor in %o0 and %o1 respectively.
8  *
9  * m4 parameters:
10  *  NAME        name of function to generate
11  *  OP          OP=div => %o0 / %o1; OP=rem => %o0 % %o1
12  *  S           S=true => signed; S=false => unsigned
13  *
14  * Algorithm parameters:
15  *  N           how many bits per iteration we try to get (4)
16  *  WORDSIZE    total number of bits (32)
17  *
18  * Derived constants:
19  *  TOPBITS     number of bits in the top `decade' of a number
20  *
21  * Important variables:
22  *  Q           the partial quotient under development (initially 0)
23  *  R           the remainder so far, initially the dividend
24  *  ITER        number of main division loop iterations required;
25  *              equal to ceil(log2(quotient) / N).  Note that this
26  *              is the log base (2^N) of the quotient.
27  *  V           the current comparand, initially divisor*2^(ITER*N-1)
28  *
29  * Cost:
30  *  Current estimate for non-large dividend is
31  *      ceil(log2(quotient) / N) * (10 + 7N/2) + C
32  *  A large dividend is one greater than 2^(31-TOPBITS) and takes a
33  *  different path, as the upper bits of the quotient must be developed
34  *  one bit at a time.
35  */
37 define(N, `4')dnl
38 define(WORDSIZE, `32')dnl
39 define(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N)))dnl
40 dnl
41 define(dividend, `%o0')dnl
42 define(divisor, `%o1')dnl
43 define(Q, `%o2')dnl
44 define(R, `%o3')dnl
45 define(ITER, `%o4')dnl
46 define(V, `%o5')dnl
47 dnl
48 dnl m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d
49 define(T, `%g1')dnl
50 define(SC, `%g2')dnl
51 ifelse(S, `true', `define(SIGN, `%g3')')dnl
53 dnl
54 dnl This is the recursive definition for developing quotient digits.
55 dnl
56 dnl Parameters:
57 dnl  $1 the current depth, 1 <= $1 <= N
58 dnl  $2 the current accumulation of quotient bits
59 dnl  N  max depth
60 dnl
61 dnl We add a new bit to $2 and either recurse or insert the bits in
62 dnl the quotient.  R, Q, and V are inputs and outputs as defined above;
63 dnl the condition codes are expected to reflect the input R, and are
64 dnl modified to reflect the output R.
65 dnl
66 define(DEVELOP_QUOTIENT_BITS,
67 `       ! depth $1, accumulated bits $2
68         bl      LOC($1.eval(2**N+$2))
69         srl     V,1,V
70         ! remainder is positive
71         subcc   R,V,R
72         ifelse($1, N,
73         `       b       9f
74                 add     Q, ($2*2+1), Q
75         ', `    DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')')
76 LOC($1.eval(2**N+$2)):
77         ! remainder is negative
78         addcc   R,V,R
79         ifelse($1, N,
80         `       b       9f
81                 add     Q, ($2*2-1), Q
82         ', `    DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')')
83         ifelse($1, 1, `9:')')dnl
85 #include <sysdep.h>
86 #include <sys/trap.h>
88 ENTRY(NAME)
89 ifelse(S, `true',
90 `       ! compute sign of result; if neither is negative, no problem
91         orcc    divisor, dividend, %g0  ! either negative?
92         bge     2f                      ! no, go do the divide
93 ifelse(OP, `div',
94 `       xor     divisor, dividend, SIGN ! compute sign in any case',
95 `       mov     dividend, SIGN          ! sign of remainder matches dividend')
96         tst     divisor
97         bge     1f
98         tst     dividend
99         ! divisor is definitely negative; dividend might also be negative
100         bge     2f                      ! if dividend not negative...
101         sub     %g0, divisor, divisor   ! in any case, make divisor nonneg
102 1:      ! dividend is negative, divisor is nonnegative
103         sub     %g0, dividend, dividend ! make dividend nonnegative
106         ! Ready to divide.  Compute size of quotient; scale comparand.
107         orcc    divisor, %g0, V
108         bne     1f
109         mov     dividend, R
111                 ! Divide by zero trap.  If it returns, return 0 (about as
112                 ! wrong as possible, but that is what SunOS does...).
113                 ta      ST_DIV0
114                 retl
115                 clr     %o0
118         cmp     R, V                    ! if divisor exceeds dividend, done
119         blu     LOC(got_result)         ! (and algorithm fails otherwise)
120         clr     Q
121         sethi   %hi(1 << (WORDSIZE - TOPBITS - 1)), T
122         cmp     R, T
123         blu     LOC(not_really_big)
124         clr     ITER
126         ! `Here the dividend is >= 2**(31-N) or so.  We must be careful here,
127         ! as our usual N-at-a-shot divide step will cause overflow and havoc.
128         ! The number of bits in the result here is N*ITER+SC, where SC <= N.
129         ! Compute ITER in an unorthodox manner: know we need to shift V into
130         ! the top decade: so do not even bother to compare to R.'
131         1:
132                 cmp     V, T
133                 bgeu    3f
134                 mov     1, SC
135                 sll     V, N, V
136                 b       1b
137                 add     ITER, 1, ITER
139         ! Now compute SC.
140         2:      addcc   V, V, V
141                 bcc     LOC(not_too_big)
142                 add     SC, 1, SC
144                 ! We get here if the divisor overflowed while shifting.
145                 ! This means that R has the high-order bit set.
146                 ! Restore V and subtract from R.
147                 sll     T, TOPBITS, T   ! high order bit
148                 srl     V, 1, V         ! rest of V
149                 add     V, T, V
150                 b       LOC(do_single_div)
151                 sub     SC, 1, SC
153         LOC(not_too_big):
154         3:      cmp     V, R
155                 blu     2b
156                 nop
157                 be      LOC(do_single_div)
158                 nop
159         /* NB: these are commented out in the V8-Sparc manual as well */
160         /* (I do not understand this) */
161         ! V > R: went too far: back up 1 step
162         !       srl     V, 1, V
163         !       dec     SC
164         ! do single-bit divide steps
165         !
166         ! We have to be careful here.  We know that R >= V, so we can do the
167         ! first divide step without thinking.  BUT, the others are conditional,
168         ! and are only done if R >= 0.  Because both R and V may have the high-
169         ! order bit set in the first step, just falling into the regular
170         ! division loop will mess up the first time around.
171         ! So we unroll slightly...
172         LOC(do_single_div):
173                 subcc   SC, 1, SC
174                 bl      LOC(end_regular_divide)
175                 nop
176                 sub     R, V, R
177                 mov     1, Q
178                 b       LOC(end_single_divloop)
179                 nop
180         LOC(single_divloop):
181                 sll     Q, 1, Q
182                 bl      1f
183                 srl     V, 1, V
184                 ! R >= 0
185                 sub     R, V, R
186                 b       2f
187                 add     Q, 1, Q
188         1:      ! R < 0
189                 add     R, V, R
190                 sub     Q, 1, Q
191         2:
192         LOC(end_single_divloop):
193                 subcc   SC, 1, SC
194                 bge     LOC(single_divloop)
195                 tst     R
196                 b,a     LOC(end_regular_divide)
198 LOC(not_really_big):
200         sll     V, N, V
201         cmp     V, R
202         bleu    1b
203         addcc   ITER, 1, ITER
204         be      LOC(got_result)
205         sub     ITER, 1, ITER
207         tst     R       ! set up for initial iteration
208 LOC(divloop):
209         sll     Q, N, Q
210         DEVELOP_QUOTIENT_BITS(1, 0)
211 LOC(end_regular_divide):
212         subcc   ITER, 1, ITER
213         bge     LOC(divloop)
214         tst     R
215         bl,a    LOC(got_result)
216         ! non-restoring fixup here (one instruction only!)
217 ifelse(OP, `div',
218 `       sub     Q, 1, Q
219 ', `    add     R, divisor, R
222 LOC(got_result):
223 ifelse(S, `true',
224 `       ! check to see if answer should be < 0
225         tst     SIGN
226         bl,a    1f
227         ifelse(OP, `div', `sub %g0, Q, Q', `sub %g0, R, R')
228 1:')
229         retl
230         ifelse(OP, `div', `mov Q, %o0', `mov R, %o0')
232 END(NAME)