1 /* $NetBSD: divrem.m4,v 1.9 2002/01/21 23:40:41 ross Exp $ */
4 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
7 * Author: Chris G. Demetriou
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
31 * Division and remainder.
33 * The use of m4 is modeled after the sparc code, but the algorithm is
34 * simple binary long division.
36 * Note that the loops could probably benefit from unrolling.
41 * NAME name of function to generate
42 * OP OP=div: t10 / t11 -> t12; OP=rem: t10 % t11 -> t12
43 * S S=true: signed; S=false: unsigned
44 * WORDSIZE total number of bits
55 ifelse(S, `true', `define(NEG, `t4')')
57 #include <machine/asm.h>
59 LEAF(NAME, 0) /* XXX */
69 mov zero, RESULT /* Initialize result to zero */
73 /* Compute sign of result. If either is negative, this is easy. */
74 or A, B, NEG /* not the sign, but... */
75 srl NEG, WORDSIZE - 1, NEG /* rather, or of high bits */
76 blbc NEG, Ldoit /* neither negative? do it! */
79 ` xor A, B, NEG /* THIS is the sign! */
80 ', ` mov A, NEG /* sign follows A. */
82 srl NEG, WORDSIZE - 1, NEG /* make negation the low bit. */
84 srl A, WORDSIZE - 1, I /* is A negative? */
85 blbc I, LnegB /* no. */
86 /* A is negative; flip it. */
87 ifelse(WORDSIZE, `32', `
88 /* top 32 bits may be random junk */
92 srl B, WORDSIZE - 1, I /* is B negative? */
93 blbc I, Ldoit /* no. */
95 /* B is definitely negative, no matter how we got here. */
96 ifelse(WORDSIZE, `32', `
97 /* top 32 bits may be random junk */
103 ifelse(WORDSIZE, `32', `
105 * Clear the top 32 bits of each operand, as they may
106 * sign extension (if negated above), or random junk.
112 /* kill the special cases. */
113 beq B, Ldotrap /* division by zero! */
115 cmpult A, B, CC /* A < B? */
116 /* RESULT is already zero, from above. A is untouched. */
119 cmpeq A, B, CC /* A == B? */
125 * Find out how many bits of zeros are at the beginning of the divisor.
128 ldiq T_0, 1 /* I = 0; BIT = 1<<WORDSIZE-1 */
130 sll T_0, WORDSIZE-1, BIT
132 and B, BIT, CC /* if bit in B is set, done. */
134 addq I, 1, I /* increment I, shift bit */
136 cmplt I, WORDSIZE-1, CC /* if I leaves one bit, done. */
140 beq I, Ldodiv /* If I = 0, divide now. */
141 ldiq T_0, 1 /* BIT = 1<<WORDSIZE-1 */
142 sll T_0, WORDSIZE-1, BIT
145 and A, BIT, CC /* if bit in A is set, done. */
147 subq I, 1, I /* decrement I, shift bit */
149 bne I, LAloop /* If I != 0, loop again */
152 sll B, I, B /* B <<= i */
159 cmoveq CC, T_0, RESULT
173 /* Check to see if we should negate it. */
174 subq zero, RESULT, T_0
175 cmovlbs NEG, T_0, RESULT
190 ldiq a0, -2 /* This is the signal to SIGFPE! */
193 `', ` mov zero, A /* so that zero will be returned */