divrem.m4 revision 1.1 1 /* $NetBSD: divrem.m4,v 1.1 1995/02/10 17:50:20 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
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.
14 *
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.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 /*
31 * Division and remainder.
32 *
33 * The use of m4 is modeled after the sparc code, but the algorithm is
34 * simple binary long division.
35 *
36 * Note that the loops could probably benefit from unrolling.
37 */
38
39 /*
40 * M4 Parameters
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 [XXX NOT YET]
44 * WORDSIZE total number of bits
45 */
46
47 define(A, `t10')
48 define(B, `t11')
49 define(RESULT, `t12')
50
51 define(BIT, `t0')
52 define(I, `t1')
53 define(CC, `t2')
54 define(T_0, `t3')
55 ifelse(S, `true', `define(SIGN, `t4')')
56
57 #include "DEFS.h"
58
59 LEAF(NAME, 0) /* XXX */
60 lda sp, -48(sp)
61 stq BIT, 0(sp)
62 stq I, 8(sp)
63 stq CC, 16(sp)
64 stq T_0, 24(sp)
65 ifelse(S, `true',
66 ` stq SIGN, 32(sp)')
67 mov zero, RESULT /* Initialize result to zero */
68
69 ifelse(S, `true',
70 `
71 /* Compute sign of result. If either is negative, this is easy. */
72 or A, B, SIGN /* not the sign, but... */
73 bgt SIGN, Ldoit /* neither negative? do it! */
74
75 ifelse(OP, `div',
76 ` xor A, B, SIGN /* THIS is the sign! */
77 ', ` mov A, SIGN /* sign follows A. */
78 ')
79 bge A, LnegB /* see if A is negative */
80 /* A is negative; flip it. */
81 subq zero, A, A
82 bge B, Ldoit /* see if B is negative */
83 LnegB:
84 /* B is definitely negative, no matter how we got here. */
85 subq zero, B, B
86 Ldoit:
87 ')
88
89 /* kill the special cases. */
90 beq B, Ldotrap /* division by zero! XXX */
91
92 1: cmpult A, B, CC /* A < B? */
93 /* RESULT is already zero, from above. A is untouched. */
94 bne CC, Lret_result
95
96 cmpeq A, B, CC /* A == B? */
97 cmovne CC, 1, RESULT
98 cmovne CC, zero, A
99 bne CC, Lret_result
100
101 /*
102 * Find out how many bits of zeros are at the beginning of the divisor.
103 */
104 LBbits:
105 CONST(1, T_0) /* I = 0; BIT = 1<<WORDSIZE-1 */
106 mov zero, I
107 sll T_0, WORDSIZE-1, BIT
108 LBloop:
109 and B, BIT, CC /* if bit in B is set, done. */
110 bne CC, LAbits
111 addq I, 1, I /* increment I, shift bit */
112 srl BIT, 1, BIT
113 cmplt I, WORDSIZE-1, CC /* if I leaves one bit, done. */
114 bne CC, LBloop
115
116 LAbits:
117 beq I, Ldodiv /* If I = 0, divide now. */
118 CONST(1, T_0) /* BIT = 1<<WORDSIZE-1 */
119 sll T_0, WORDSIZE-1, BIT
120
121 LAloop:
122 and A, BIT, CC /* if bit in A is set, done. */
123 bne CC, Ldodiv
124 subq I, 1, I /* decrement I, shift bit */
125 srl BIT, 1, BIT
126 bne I, LAloop /* If I != 0, loop again */
127
128 Ldodiv:
129 sll B, I, B /* B <<= i */
130 CONST(1, T_0)
131 sll T_0, I, BIT
132
133 Ldivloop:
134 cmpult A, B, CC
135 or RESULT, BIT, T_0
136 cmoveq CC, T_0, RESULT
137 subq A, B, T_0
138 cmoveq CC, T_0, A
139 srl BIT, 1, BIT
140 srl B, 1, B
141 beq A, Lret_result
142 bne BIT, Ldivloop
143
144 Lret_result:
145 ifelse(OP, `div',
146 `', ` mov A, RESULT
147 ')
148 ifelse(S, `true',
149 `
150 /* Check to see if we should negate it. */
151 subqv zero, RESULT, T_0
152 cmovlt SIGN, T_0, RESULT
153 ')
154
155 ldq BIT, 0(sp)
156 ldq I, 8(sp)
157 ldq CC, 16(sp)
158 ldq T_0, 24(sp)
159 ifelse(S, `true',
160 ` ldq SIGN, 32(sp)')
161 lda sp, 48(sp)
162 ret zero, (t9), 1
163
164 Ldotrap:
165 CONST(-2, a0) /* This is the signal to SIGFPE! */
166 call_pal PAL_gentrap
167 br zero, Lret_result
168
169 END(NAME)
170