divrem.m4 revision 1.3 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1992, 1993
3 1.1 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This software was developed by the Computer Systems Engineering group
6 1.1 cgd * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 1.1 cgd * contributed to Berkeley.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.1 cgd * 3. All advertising materials mentioning features or use of this software
18 1.1 cgd * must display the following acknowledgement:
19 1.1 cgd * This product includes software developed by the University of
20 1.1 cgd * California, Berkeley and its contributors.
21 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
22 1.1 cgd * may be used to endorse or promote products derived from this software
23 1.1 cgd * without specific prior written permission.
24 1.1 cgd *
25 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 cgd * SUCH DAMAGE.
36 1.1 cgd *
37 1.1 cgd * from: Header: divrem.m4,v 1.4 92/06/25 13:23:57 torek Exp
38 1.3 mrg * $Id: divrem.m4,v 1.3 1996/11/18 16:08:30 mrg Exp $
39 1.1 cgd */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Division and remainder, from Appendix E of the Sparc Version 8
43 1.1 cgd * Architecture Manual, with fixes from Gordon Irlam.
44 1.1 cgd */
45 1.1 cgd
46 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
47 1.1 cgd .asciz "@(#)divrem.m4 8.1 (Berkeley) 6/4/93"
48 1.1 cgd #endif /* LIBC_SCCS and not lint */
49 1.1 cgd
50 1.1 cgd /*
51 1.1 cgd * Input: dividend and divisor in %o0 and %o1 respectively.
52 1.1 cgd *
53 1.1 cgd * m4 parameters:
54 1.1 cgd * NAME name of function to generate
55 1.1 cgd * OP OP=div => %o0 / %o1; OP=rem => %o0 % %o1
56 1.1 cgd * S S=true => signed; S=false => unsigned
57 1.1 cgd *
58 1.1 cgd * Algorithm parameters:
59 1.1 cgd * N how many bits per iteration we try to get (4)
60 1.1 cgd * WORDSIZE total number of bits (32)
61 1.1 cgd *
62 1.1 cgd * Derived constants:
63 1.1 cgd * TWOSUPN 2^N, for label generation (m4 exponentiation currently broken)
64 1.1 cgd * TOPBITS number of bits in the top `decade' of a number
65 1.1 cgd *
66 1.1 cgd * Important variables:
67 1.1 cgd * Q the partial quotient under development (initially 0)
68 1.1 cgd * R the remainder so far, initially the dividend
69 1.1 cgd * ITER number of main division loop iterations required;
70 1.1 cgd * equal to ceil(log2(quotient) / N). Note that this
71 1.1 cgd * is the log base (2^N) of the quotient.
72 1.1 cgd * V the current comparand, initially divisor*2^(ITER*N-1)
73 1.1 cgd *
74 1.1 cgd * Cost:
75 1.1 cgd * Current estimate for non-large dividend is
76 1.1 cgd * ceil(log2(quotient) / N) * (10 + 7N/2) + C
77 1.1 cgd * A large dividend is one greater than 2^(31-TOPBITS) and takes a
78 1.1 cgd * different path, as the upper bits of the quotient must be developed
79 1.1 cgd * one bit at a time.
80 1.1 cgd */
81 1.1 cgd
82 1.1 cgd define(N, `4')
83 1.1 cgd define(TWOSUPN, `16')
84 1.1 cgd define(WORDSIZE, `32')
85 1.1 cgd define(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N)))
86 1.1 cgd
87 1.1 cgd define(dividend, `%o0')
88 1.1 cgd define(divisor, `%o1')
89 1.1 cgd define(Q, `%o2')
90 1.1 cgd define(R, `%o3')
91 1.1 cgd define(ITER, `%o4')
92 1.1 cgd define(V, `%o5')
93 1.1 cgd
94 1.1 cgd /* m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d */
95 1.1 cgd define(T, `%g1')
96 1.1 cgd define(SC, `%g7')
97 1.1 cgd ifelse(S, `true', `define(SIGN, `%g6')')
98 1.1 cgd
99 1.1 cgd /*
100 1.1 cgd * This is the recursive definition for developing quotient digits.
101 1.1 cgd *
102 1.1 cgd * Parameters:
103 1.1 cgd * $1 the current depth, 1 <= $1 <= N
104 1.1 cgd * $2 the current accumulation of quotient bits
105 1.1 cgd * N max depth
106 1.1 cgd *
107 1.1 cgd * We add a new bit to $2 and either recurse or insert the bits in
108 1.1 cgd * the quotient. R, Q, and V are inputs and outputs as defined above;
109 1.1 cgd * the condition codes are expected to reflect the input R, and are
110 1.1 cgd * modified to reflect the output R.
111 1.1 cgd */
112 1.1 cgd define(DEVELOP_QUOTIENT_BITS,
113 1.1 cgd ` ! depth $1, accumulated bits $2
114 1.1 cgd bl L.$1.eval(TWOSUPN+$2)
115 1.1 cgd srl V,1,V
116 1.1 cgd ! remainder is positive
117 1.1 cgd subcc R,V,R
118 1.1 cgd ifelse($1, N,
119 1.1 cgd ` b 9f
120 1.1 cgd add Q, ($2*2+1), Q
121 1.1 cgd ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')')
122 1.1 cgd L.$1.eval(TWOSUPN+$2):
123 1.1 cgd ! remainder is negative
124 1.1 cgd addcc R,V,R
125 1.1 cgd ifelse($1, N,
126 1.1 cgd ` b 9f
127 1.1 cgd add Q, ($2*2-1), Q
128 1.1 cgd ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')')
129 1.1 cgd ifelse($1, 1, `9:')')
130 1.1 cgd
131 1.3 mrg #include <machine/asm.h>
132 1.1 cgd #include <machine/trap.h>
133 1.1 cgd
134 1.1 cgd FUNC(NAME)
135 1.1 cgd ifelse(S, `true',
136 1.1 cgd ` ! compute sign of result; if neither is negative, no problem
137 1.1 cgd orcc divisor, dividend, %g0 ! either negative?
138 1.1 cgd bge 2f ! no, go do the divide
139 1.2 pk ifelse(OP, `div',
140 1.2 pk `xor divisor, dividend, SIGN',
141 1.2 pk `mov dividend, SIGN') ! compute sign in any case
142 1.1 cgd tst divisor
143 1.1 cgd bge 1f
144 1.1 cgd tst dividend
145 1.1 cgd ! divisor is definitely negative; dividend might also be negative
146 1.1 cgd bge 2f ! if dividend not negative...
147 1.1 cgd neg divisor ! in any case, make divisor nonneg
148 1.1 cgd 1: ! dividend is negative, divisor is nonnegative
149 1.1 cgd neg dividend ! make dividend nonnegative
150 1.1 cgd 2:
151 1.1 cgd ')
152 1.1 cgd ! Ready to divide. Compute size of quotient; scale comparand.
153 1.1 cgd orcc divisor, %g0, V
154 1.1 cgd bnz 1f
155 1.1 cgd mov dividend, R
156 1.1 cgd
157 1.1 cgd ! Divide by zero trap. If it returns, return 0 (about as
158 1.1 cgd ! wrong as possible, but that is what SunOS does...).
159 1.1 cgd t ST_DIV0
160 1.1 cgd retl
161 1.1 cgd clr %o0
162 1.1 cgd
163 1.1 cgd 1:
164 1.1 cgd cmp R, V ! if divisor exceeds dividend, done
165 1.1 cgd blu Lgot_result ! (and algorithm fails otherwise)
166 1.1 cgd clr Q
167 1.1 cgd sethi %hi(1 << (WORDSIZE - TOPBITS - 1)), T
168 1.1 cgd cmp R, T
169 1.1 cgd blu Lnot_really_big
170 1.1 cgd clr ITER
171 1.1 cgd
172 1.1 cgd ! `Here the dividend is >= 2^(31-N) or so. We must be careful here,
173 1.1 cgd ! as our usual N-at-a-shot divide step will cause overflow and havoc.
174 1.1 cgd ! The number of bits in the result here is N*ITER+SC, where SC <= N.
175 1.1 cgd ! Compute ITER in an unorthodox manner: know we need to shift V into
176 1.1 cgd ! the top decade: so do not even bother to compare to R.'
177 1.1 cgd 1:
178 1.1 cgd cmp V, T
179 1.1 cgd bgeu 3f
180 1.1 cgd mov 1, SC
181 1.1 cgd sll V, N, V
182 1.1 cgd b 1b
183 1.1 cgd inc ITER
184 1.1 cgd
185 1.1 cgd ! Now compute SC.
186 1.1 cgd 2: addcc V, V, V
187 1.1 cgd bcc Lnot_too_big
188 1.1 cgd inc SC
189 1.1 cgd
190 1.1 cgd ! We get here if the divisor overflowed while shifting.
191 1.1 cgd ! This means that R has the high-order bit set.
192 1.1 cgd ! Restore V and subtract from R.
193 1.1 cgd sll T, TOPBITS, T ! high order bit
194 1.1 cgd srl V, 1, V ! rest of V
195 1.1 cgd add V, T, V
196 1.1 cgd b Ldo_single_div
197 1.1 cgd dec SC
198 1.1 cgd
199 1.1 cgd Lnot_too_big:
200 1.1 cgd 3: cmp V, R
201 1.1 cgd blu 2b
202 1.1 cgd nop
203 1.1 cgd be Ldo_single_div
204 1.1 cgd nop
205 1.1 cgd /* NB: these are commented out in the V8-Sparc manual as well */
206 1.1 cgd /* (I do not understand this) */
207 1.1 cgd ! V > R: went too far: back up 1 step
208 1.1 cgd ! srl V, 1, V
209 1.1 cgd ! dec SC
210 1.1 cgd ! do single-bit divide steps
211 1.1 cgd !
212 1.1 cgd ! We have to be careful here. We know that R >= V, so we can do the
213 1.1 cgd ! first divide step without thinking. BUT, the others are conditional,
214 1.1 cgd ! and are only done if R >= 0. Because both R and V may have the high-
215 1.1 cgd ! order bit set in the first step, just falling into the regular
216 1.1 cgd ! division loop will mess up the first time around.
217 1.1 cgd ! So we unroll slightly...
218 1.1 cgd Ldo_single_div:
219 1.1 cgd deccc SC
220 1.1 cgd bl Lend_regular_divide
221 1.1 cgd nop
222 1.1 cgd sub R, V, R
223 1.1 cgd mov 1, Q
224 1.1 cgd b Lend_single_divloop
225 1.1 cgd nop
226 1.1 cgd Lsingle_divloop:
227 1.1 cgd sll Q, 1, Q
228 1.1 cgd bl 1f
229 1.1 cgd srl V, 1, V
230 1.1 cgd ! R >= 0
231 1.1 cgd sub R, V, R
232 1.1 cgd b 2f
233 1.1 cgd inc Q
234 1.1 cgd 1: ! R < 0
235 1.1 cgd add R, V, R
236 1.1 cgd dec Q
237 1.1 cgd 2:
238 1.1 cgd Lend_single_divloop:
239 1.1 cgd deccc SC
240 1.1 cgd bge Lsingle_divloop
241 1.1 cgd tst R
242 1.1 cgd b,a Lend_regular_divide
243 1.1 cgd
244 1.1 cgd Lnot_really_big:
245 1.1 cgd 1:
246 1.1 cgd sll V, N, V
247 1.1 cgd cmp V, R
248 1.1 cgd bleu 1b
249 1.1 cgd inccc ITER
250 1.1 cgd be Lgot_result
251 1.1 cgd dec ITER
252 1.1 cgd
253 1.1 cgd tst R ! set up for initial iteration
254 1.1 cgd Ldivloop:
255 1.1 cgd sll Q, N, Q
256 1.1 cgd DEVELOP_QUOTIENT_BITS(1, 0)
257 1.1 cgd Lend_regular_divide:
258 1.1 cgd deccc ITER
259 1.1 cgd bge Ldivloop
260 1.1 cgd tst R
261 1.1 cgd bl,a Lgot_result
262 1.1 cgd ! non-restoring fixup here (one instruction only!)
263 1.1 cgd ifelse(OP, `div',
264 1.1 cgd ` dec Q
265 1.1 cgd ', ` add R, divisor, R
266 1.1 cgd ')
267 1.1 cgd
268 1.1 cgd Lgot_result:
269 1.1 cgd ifelse(S, `true',
270 1.1 cgd ` ! check to see if answer should be < 0
271 1.1 cgd tst SIGN
272 1.1 cgd bl,a 1f
273 1.1 cgd ifelse(OP, `div', `neg Q', `neg R')
274 1.1 cgd 1:')
275 1.1 cgd retl
276 1.1 cgd ifelse(OP, `div', `mov Q, %o0', `mov R, %o0')
277