-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiv.fj
241 lines (216 loc) · 6.89 KB
/
div.fj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
ns bit {
// Complexity: n(5@+11)
// dst[:n], src[:n] = src[:n] / 10, src[:n] % 10.
def div10 n, dst, src @ zero, end {
.zero n, dst
.div10.cmp_sub_10 zero, src+(n-4)*dw, dst+(n-4)*dw
rep(n-4, i) .div10.cmp_sub_10 src+(n-1-i)*dw, src+(n-5-i)*dw, dst+(n-5-i)*dw
;end
zero: .bit 0 // this bit is never modified
end:
}
ns div10 {
// Complexity: 4@+12
// if (val > 10) {
// val -= 10;
// res = !res;
// }
// for val4:val[3,2,1,0] of length 5, and Assumes val <= 19.
// This macro is getting called on consecutive 5-bits, shifting the 5-bit-frame 1 bit right at a time.
def cmp_sub_10 val4, val, res @ yes, no, _1xxxx, _1xx0x, _1xx1x, _01x1x, _0110x {
..if1 val4, _1xxxx
..if0 val+3*dw, no
..if1 val+ dw, _01x1x
..if0 val+2*dw, no
_0110x:
..not val+2*dw
val+3*dw+dbit; yes
_01x1x:
val+3*dw+dbit; yes
_1xxxx:
..not val4
..if val+dw, _1xx0x, _1xx1x
_1xx0x:
val+2*dw+dbit; yes
_1xx1x:
val+3*dw+dbit; yes
yes:
..not val+dw
..not res
no:
}
}
// Time Complexity: n^2(10@+20)
// Space Complexity: n^2(11@+22)
// if b==0: goto end (do nothing)
// q = a/b (signed division)
// r = a%b (signed modulo - sign(r)==sign(a))
// @NOTE: a,b are SIGNED numbers. If you want a division with unsigned ints, use the div macro.
// @NOTE: this division implementation is WASTEFUL in space, yet saves running time, compared to div_loop.
//
// @NOTE: There is a better version: This one is slow, big, and doesn't error on b==0.
// Also it supports only one convention for the sign of the reminder.
// For a faster & better division see hex.div, hex.idiv.
// q,a,b,r are bit[:n].
def idiv n, a, b, q, r @ negative_a, negative_b, one_negative, neg_b_1, do_div, neg_b_2, neg_ans, end {
.mov negative_a, a+dw*(n-1)
.mov negative_b, b+dw*(n-1)
.zero one_negative
.if0 negative_a, neg_b_1
.not one_negative
.neg n, a
neg_b_1:
.if0 negative_b, do_div
.not one_negative
.neg n, b
do_div:
.div n, a, b, q, r
.if0 negative_a, neg_b_2
.neg n, a
.neg n, r
neg_b_2:
.if0 negative_b, neg_ans
.neg n, b
neg_ans:
.if0 one_negative, end
.neg n, q
;end
negative_a:
.bit
negative_b:
.bit
one_negative:
.bit
end:
}
// Time Complexity: n^2(10@+20)
// Space Complexity: n^2(11@+22)
// if b==0: goto end (do nothing)
// q = a/b (unsigned division)
// r = a%b (unsigned modulo)
// @NOTE: a,b are UNSIGNED numbers. If you want a division with signed ints, use the idiv macro.
// @NOTE: this division implementation is WASTEFUL in space, yet saves running time, compared to div_loop.
//
// @NOTE: There is a better version: This one is slow, big, and doesn't error on b==0.
// For a faster & better division see hex.div, hex.idiv.
// q,a,b,r are bit[:n].
def div n, a, b, q, r @ Q, R, end {
.if0 n, b, end
.zero 2*n, R
.zero n, Q
rep(n, i) .div.div_step n, a+(n-1-i)*dw, b, R+(n-1-i)*dw, Q+(n-1-i)*dw
.mov n, r, R
.mov n, q, Q
;end
R:
.vec 2*n
Q:
.vec n
end:
}
ns div {
// Time Complexity: n(10@+20)
// Space Complexity: n(11@+22)
// R[0] ^= N
// if R[:n] >= D[:n]:
// R -= D
// not Q[0]
//
// R,D are bit[:n], while R,N are bits.
def div_step n, N, D, R, Q @ do_sub, end {
..xor R, N
..cmp n, R, D, end, do_sub, do_sub
do_sub:
..sub n, R, D
..not Q
end:
}
}
// Time Complexity: n^2(18@+18)
// Space Complexity: n(37@+58)
// if b==0: goto end (do nothing)
// q = a/b (signed division)
// r = a%b (signed modulo - sign(r)==sign(a))
// @NOTE: a,b are SIGNED numbers. If you want a division with unsigned ints, use the div_loop macro.
// @NOTE: this division implementation saves space, yet is slower, compared to div.
//
// @NOTE: There is a better version: This one is slow, big, and doesn't error on b==0.
// Also it supports only one convention for the sign of the reminder.
// For a faster & better division see hex.div, hex.idiv.
// q,a,b,r are bit[:n].
def idiv_loop n, a, b, q, r @ negative_a, negative_b, one_negative, neg_b_1, do_div, neg_b_2, neg_ans, end {
.mov negative_a, a+dw*(n-1)
.mov negative_b, b+dw*(n-1)
.zero one_negative
.if0 negative_a, neg_b_1
.not one_negative
.neg n, a
neg_b_1:
.if0 negative_b, do_div
.not one_negative
.neg n, b
do_div:
.div_loop n, a, b, q, r
.if0 negative_a, neg_b_2
.neg n, a
.neg n, r
neg_b_2:
.if0 negative_b, neg_ans
.neg n, b
neg_ans:
.if0 one_negative, end
.neg n, q
;end
negative_a:
.bit
negative_b:
.bit
one_negative:
.bit
end:
}
// Time Complexity: n^2(18@+18)
// Space Complexity: n(25@+22)
// if b==0: goto end (do nothing)
// q = a/b (unsigned division)
// r = a%b (unsigned modulo)
// @NOTE: a,b are UNSIGNED numbers. If you want a division with signed ints, use the idiv_loop macro.
// @NOTE: this division implementation saves space, yet is slower, compared to div.
//
// @NOTE: There is a better version: This one is slow, big, and doesn't error on b==0.
// For a faster division see hex.div, hex.idiv.
// q,a,b,r are bit[:n].
def div_loop n, a, b, q, r @ loop, do_sub, loop_end, after_loop, A, Q, R, i, end {
.if0 n, b, end
.zero n, R
.zero n, Q
.mov n, A, a
.zero n, i
.not i+(n-1)*dw
loop:
.if0 n, i, after_loop //Comp: n(@+2)
.shl n, R //Comp: n(2@-1)
.xor R, A+(n-1)*dw
.cmp n, R, b, loop_end, do_sub, do_sub //Comp: n(2@+4) (Space=n(3@+6))
do_sub:
.sub n, R, b //Comp: n(8@+16)
.xor n, Q, i //Comp: n(@-1)
loop_end:
.shr n, i //Comp: n(2@-1)
.shl n, A //Comp: n(2@-1)
;loop
after_loop:
.mov n, r, R
.mov n, q, Q
;end
A:
.vec n
R:
.vec n
Q:
.vec n
i:
.vec n
end:
}
}