forked from tomhea/flip-jump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitlib.fj
402 lines (314 loc) · 7.21 KB
/
bitlib.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Every line is (a bit) bananas!
// Implementation of binary-variables operations, and vectors of it
// should be assembled with the runlib.fj file
// This file is independent of the bit-width, and uses the consts defined at runlib.fj
// Everything after // is ignored, and every whitespace is ignored (besides new line)
// An opcode is: F;J F; ;J or just ; (no F => F=0; no J => J=next)
// Complexity note: phi is log2 of the total number of fj operations, counting wflips as one op.
// so 2 wflips with value=label (while label is padded with 2^n) causes phi-n space/time complexity
// The complexities are not updated in this file (should be lower/faster).
// ---------- Memory Variables:
// Size Complexity: 1
def bit value {
; value ? dw : 0
}
def bit {
bit 0
}
// Size Complexity: n
ns bit {
def vec n, value {
rep(n, i) bit (value>>i)&1
}
def vec n {
rep(n, i) bit
}
}
// ---------- Memory Manipulation:
ns bit {
// Complexity: phi+2
def zero x {
.xor x, x
}
// Complexity: n(phi+2)
def zero n, x {
rep(n, i) .zero x+i*dw
}
// Complexity: phi+3
def one x {
.zero x
.not x
}
// Complexity: n(phi+3)
def one n, x {
rep(n, i) .one x+i*dw
}
////Complexity: 2phi + 9
//__if_mov dst src @ l0 l1 flip end {
// .if src l0 l1
// l0: .if dst end flip
// l1: .if dst flip end
// flip: .not dst
// end:
//}
// safe even for dst==src !
// Complexity: 2phi+5
def mov dst, src @ do_mov, end {
comp_if1 dst==src, end
do_mov:
.zero dst
.xor dst, src
end:
}
// Complexity: n(2phi+5)
def mov n, dst, src {
rep(n, i) .mov dst+i*dw, src+i*dw
}
//Complexity: 2phi+10
def swap a, b @ a0, a1, notnot, end {
.if a, a0, a1
a0:
.if b, end, notnot
a1:
.if b, notnot, end
notnot:
.not a
.not b
end:
}
// Complexity: n(2phi+10)
def swap n, a, b {
rep(n, i) .swap a+i*dw, b+i*dw
}
}
// ---------- Conditional Jump
ns bit {
// Complexity: phi+4
def if x, l0, l1 @ label_ptr, base_jump_label {
.xor label_ptr, x
label_ptr:
;base_jump_label
pad 2
base_jump_label:
;l0
label_ptr + dbit;l1
}
// Complexity: n(phi+4)
def if n, x, l0, l1 {
rep(n-1, i) ._.if_next_l0 x+i*dw, l1
.if x+(n-1)*dw, l0, l1
}
ns _ {
def if_next_l0 x, l1 @ l0 {
..if x, l0, l1
l0:
}
}
def if1 x, l1 @ end {
.if x, end, l1
end:
}
def if1 n, x, l1 @ end {
.if n, x, end, l1
end:
}
def if0 x, l0 @ end {
.if x, l0, end
end:
}
def if0 n, x, l0 @ end {
.if n, x, l0, end
end:
}
// Complexity: 2phi+8
def cmp a, b, lt, eq, gt @ a_is1_label {
.if1 a, a_is1_label
.if b, eq, lt
a_is1_label:
.if b, gt, eq
}
// Complexity: n(2phi+8)
def cmp n, a, b, lt, eq, gt {
rep(n-1, i) ._.cmp_next_eq a+(n-1-i)*dw, b+(n-1-i)*dw, lt, gt
.cmp a, b, lt, eq, gt
}
ns _ {
def cmp_next_eq a, b, lt, gt @ eq {
..cmp a, b, lt, eq, gt
eq:
}
}
}
// ---------- Logical Macros:
ns bit {
// Complexity: phi+2
def xor dst, src {
.exact_xor dst + dbit, src
}
// Complexity: n(phi+2)
def xor n, dst, src {
rep(n, i) .xor dst+dw*i, src+dw*i
}
def exact_xor dst, src @ base_jump_label, cleanup {
wflip src+w, base_jump_label, src
pad 2
base_jump_label:
;cleanup
dst;
cleanup:
wflip src+w, base_jump_label
}
// Complexity: phi+3
def double_exact_xor dst1, dst2, src @ base_jump_label, cleanup {
wflip src+w, base_jump_label, src
pad 2
base_jump_label:
;cleanup
dst1;
dst2;
cleanup:
wflip src+w, base_jump_label
}
// Complexity: n(phi+3)
def bit_var_xor n, bit, var, src {
rep(n, i) .double_exact_xor bit+i, var+dbit+i*dw, src+i*dw
}
def xor_zero dst, src {
.double_exact_xor dst+dbit, src+dbit, src
}
// Complexity: n(phi+3)
def xor_zero n, dst, src {
rep(n, i) .xor_zero dst+dw*i, src+dw*i
}
// Complexity: 2phi+7
def or dst, src @ end {
.if0 src, end
.one dst
end:
}
// Complexity: n(2phi+7)
def or n, dst, src {
rep(n, i) .or dst+dw*i, src+dw*i
}
// Complexity: 2phi+6
def and dst, src @ end {
.if1 src, end
.zero dst
end:
}
// Complexity: n(2phi+6)
def and n, dst, src {
rep(n, i) .and dst+dw*i, src+dw*i
}
// Complexity: 1
def not dst {
dst + dbit;
}
// Complexity: n
def not n, dst {
rep(n, i) .not dst+dw*i
}
def exact_not dst {
dst;
}
}
// ---------- Logical Shifts
ns bit {
// Complexity: n(2phi+5)
def shr n, x {
.shr n, 1, x
}
// Complexity: n(2phi+5)
def shr n, times, x {
rep(n-times, i) .mov x+i*dw, x+(i+times)*dw
.zero times, x+(n-times)*dw
}
// Complexity: n(2phi+5)
def shl n, x {
.shl n, 1, x
}
// Complexity: n(2phi+5)
def shl n, times, x {
rep(n-times, i) .mov x+(n-1-i)*dw, x+(n-1-i-times)*dw
.zero times, x
}
// Complexity: n(2phi+5)
def ror n, x @ temp_bit {
.mov temp_bit, x
rep(n-1, i) .mov x+i*dw, x+(i+1)*dw
.mov x+(n-1)*dw, temp_bit
skip
temp_bit:
bit
}
// Complexity: n(2phi+5)
def rol n, x @ temp_bit {
.mov temp_bit, x+(n-1)*dw
rep(n-1, i) .mov x+(n-1-i)*dw, x+(n-1-i-1)*dw
.mov x, temp_bit
skip
temp_bit:
bit
}
}
// ---------- Arithmetical Macros
// carry is both input and output
ns bit {
// Unsafe for dst==carry (but there is no reason in calling it that way)
// Complexity: 2phi+10
def inc1 dst, carry @ end {
.if0 carry, end
.not dst
.if0 dst, end
.not carry
end:
}
// Complexity: n(2phi+10)
def inc n, x @ carry {
.one carry
rep(n, i) .inc1 x+i*dw, carry
skip
carry:
bit
}
// Complexity: n(2phi+12)
def dec n, x {
.not n, x
.inc n, x
.not n, x
}
// Complexity: n(2phi+11)
def neg n, x {
.not n, x
.inc n, x
}
// Unsafe for dst==carry (but there is no reason in calling it that way)
// Complexity: 8phi+33
def add1 dst, src, carry @ _src {
.mov _src, src
.inc1 dst, _src
.inc1 dst, carry
.or carry, _src
skip
_src:
bit
}
// Complexity: n(8phi+33)
def add n, dst, src @ carry {
.zero carry
rep(n, i) .add1 dst+i*dw, src+i*dw, carry
skip
carry:
bit
}
// Complexity: n(8phi+35)
def sub n, dst, src @ carry {
.not n, src
.one carry
rep(n, i) .add1 dst+i*dw, src+i*dw, carry
.not n, src
skip
carry:
bit
}
}