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