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