-
Notifications
You must be signed in to change notification settings - Fork 4
/
logics.fj
360 lines (328 loc) · 11.5 KB
/
logics.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
// ---------- Logical Macros:
ns hex {
// Time Complexity: @
// Space Complexity: @+12
// dst ^= src
//
// both dst,src are hexes
def xor dst, src {
.exact_xor dst+dbit+3, dst+dbit+2, dst+dbit+1, dst+dbit+0, src
}
// Time Complexity: n@
// Space Complexity: n(@+12)
// dst[:n] ^= src[:n]
def xor n, dst, src {
rep (n, i) .xor dst+i*dw, src+i*dw
}
// Time Complexity: @
// Space Complexity: @+12
// {d3,d2,d1,d0} ^= src
//
// d3,d2,d1,d0 are bit-addresses; src is hex.
def exact_xor d3, d2, d1, d0, src @ switch, end {
wflip src+w, switch, src
pad 16
switch:
;end // 0
d0;end // 1
d1;end // 2
d1;switch+1*dw // 3
d2;end // 4
d2;switch+1*dw // 5
d2;switch+2*dw // 6
d2;switch+3*dw // 7
d3;end // 8
d3;switch+1*dw // 9
d3;switch+2*dw // 10
d3;switch+3*dw // 11
d3;switch+4*dw // 12
d3;switch+5*dw // 13
d3;switch+6*dw // 14
d3;switch+7*dw // 15
end:
wflip src+w, switch
}
// Time Complexity: @+4
// Space Complexity: @+28
// dst ^= src
// src = 0
//
// both dst,src are hexes
def xor_zero dst, src {
.double_xor dst, src, src
}
// Time Complexity: n(@+4)
// Space Complexity: n(@+28)
// dst[:n] ^= src[:n]
// src[:n] = 0
def xor_zero n, dst, src {
rep (n, i) .xor_zero dst+i*dw, src+i*dw
}
// Time Complexity: @+4
// Space Complexity: @+28
// dst1 ^= src
// dst2 ^= src
//
// dst1,dst2,src are hexes
def double_xor dst1, dst2, src {
.double_exact_xor \
dst1+dbit+3, dst1+dbit+2, dst1+dbit+1, dst1+dbit+0, \
dst2+dbit+3, dst2+dbit+2, dst2+dbit+1, dst2+dbit+0, \
src
}
// Time Complexity: n(@+4)
// Space Complexity: n(@+28)
// address(bit_address) ^= src
// var ^= src
// var,src are hex[:n], address is an address.
def address_and_variable_xor n, address, var, src {
rep(n, i) .double_exact_xor \
address+4*i+3, address+4*i+2, address+4*i+1, address+4*i+0, \
var+dbit+i*dw+3, var+dbit+i*dw+2, var+dbit+i*dw+1, var+dbit+i*dw+0, \
src+i*dw
}
// Time Complexity: @+4
// Space Complexity: @+28
// {t3,t2,t1,t0} ^= src
// {d3,d2,d1,d0} ^= src
//
// t3,t2,t1,t0,d3,d2,d1,d0 are bit-addresses; src is hex.
def double_exact_xor t3, t2, t1, t0, d3, d2, d1, d0, src @ first_flip, second_flip, end {
wflip src+w, first_flip, src
pad 16
first_flip:
;end // 0
d0;second_flip+ 0*dw // 1
d1;second_flip+ 1*dw // 2
d1;second_flip+ 2*dw // 3
d2;second_flip+ 3*dw // 4
d2;second_flip+ 4*dw // 5
d2;second_flip+ 5*dw // 6
d2;second_flip+ 6*dw // 7
d3;second_flip+ 7*dw // 8
d3;second_flip+ 8*dw // 9
d3;second_flip+ 9*dw // 10
d3;second_flip+10*dw // 11
d3;second_flip+11*dw // 12
d3;second_flip+12*dw // 13
d3;second_flip+13*dw // 14
d3;second_flip+14*dw // 15
second_flip:
t0;end // 1
t1;end // 2
t1;first_flip+1*dw // 3
t2;end // 4
t2;first_flip+1*dw // 5
t2;first_flip+2*dw // 6
t2;first_flip+3*dw // 7
t3;end // 8
t3;first_flip+1*dw // 9
t3;first_flip+2*dw // 10
t3;first_flip+3*dw // 11
t3;first_flip+4*dw // 12
t3;first_flip+5*dw // 13
t3;first_flip+6*dw // 14
t3;first_flip+7*dw // 15
end:
wflip src+w, first_flip
}
// Time Complexity: n(@+12)
// Space Complexity: n(@+60)
// address1(bit_address) ^= src
// var1 ^= src
// address2(bit_address) ^= src
// var2 ^= src
// var1,var2,src are hex[:n], address1,address2 are addresses.
def address_and_variable_double_xor n, address1, var1, address2, var2, src {
rep(n, i) .quadrupled_exact_xor \
address1+4*i+3, address1+4*i+2, address1+4*i+1, address1+4*i+0, \
var1+dbit+i*dw+3, var1+dbit+i*dw+2, var1+dbit+i*dw+1, var1+dbit+i*dw+0, \
address2+4*i+3, address2+4*i+2, address2+4*i+1, address2+4*i+0, \
var2+dbit+i*dw+3, var2+dbit+i*dw+2, var2+dbit+i*dw+1, var2+dbit+i*dw+0, \
src+i*dw
}
// Time Complexity: @+12
// Space Complexity: @+60
// {q3,q2,q1,q0} ^= src
// {r3,r2,r1,r0} ^= src
// {t3,t2,t1,t0} ^= src
// {d3,d2,d1,d0} ^= src
//
// r3,r2,r1,r0,q3,q2,q1,q0,t3,t2,t1,t0,d3,d2,d1,d0 are bit-addresses; src is hex.
def quadrupled_exact_xor r3, r2, r1, r0, q3, q2, q1, q0, \
t3, t2, t1, t0, d3, d2, d1, d0, src @ first_flip, second_flip, third_flip, fourth_flip, end {
wflip src+w, first_flip, src
pad 16
first_flip:
;end // 0
r0;second_flip+ 1*dw // 1
r1;second_flip+ 2*dw // 2
r1;second_flip+ 3*dw // 3
r2;second_flip+ 4*dw // 4
r2;second_flip+ 5*dw // 5
r2;second_flip+ 6*dw // 6
r2;second_flip+ 7*dw // 7
r3;second_flip+ 8*dw // 8
r3;second_flip+ 9*dw // 9
r3;second_flip+10*dw // 10
r3;second_flip+11*dw // 11
r3;second_flip+12*dw // 12
r3;second_flip+13*dw // 13
r3;second_flip+14*dw // 14
r3;second_flip+15*dw // 15
second_flip:
;end // 0
q0;third_flip+ 1*dw // 1
q1;third_flip+ 2*dw // 2
q1;third_flip+ 3*dw // 3
q2;third_flip+ 4*dw // 4
q2;third_flip+ 5*dw // 5
q2;third_flip+ 6*dw // 6
q2;third_flip+ 7*dw // 7
q3;third_flip+ 8*dw // 8
q3;third_flip+ 9*dw // 9
q3;third_flip+10*dw // 10
q3;third_flip+11*dw // 11
q3;third_flip+12*dw // 12
q3;third_flip+13*dw // 13
q3;third_flip+14*dw // 14
q3;third_flip+15*dw // 15
third_flip:
;end // 0
t0;fourth_flip+ 1*dw // 1
t1;fourth_flip+ 2*dw // 2
t1;fourth_flip+ 3*dw // 3
t2;fourth_flip+ 4*dw // 4
t2;fourth_flip+ 5*dw // 5
t2;fourth_flip+ 6*dw // 6
t2;fourth_flip+ 7*dw // 7
t3;fourth_flip+ 8*dw // 8
t3;fourth_flip+ 9*dw // 9
t3;fourth_flip+10*dw // 10
t3;fourth_flip+11*dw // 11
t3;fourth_flip+12*dw // 12
t3;fourth_flip+13*dw // 13
t3;fourth_flip+14*dw // 14
t3;fourth_flip+15*dw // 15
fourth_flip:
;end // 0
d0;end // 1
d1;end // 2
d1;first_flip+1*dw // 3
d2;end // 4
d2;first_flip+1*dw // 5
d2;first_flip+2*dw // 6
d2;first_flip+3*dw // 7
d3;end // 8
d3;first_flip+1*dw // 9
d3;first_flip+2*dw // 10
d3;first_flip+3*dw // 11
d3;first_flip+4*dw // 12
d3;first_flip+5*dw // 13
d3;first_flip+6*dw // 14
d3;first_flip+7*dw // 15
end:
wflip src+w, first_flip
}
// Complexity: 4
// hex = !hex (15-hex)
def not hex {
hex+dbit+0;
hex+dbit+1;
hex+dbit+2;
hex+dbit+3;
}
// Complexity: 4n
// x[:n] = !x[:n]
def not n, x {
rep(n, i) .not x+i*dw
}
// Time Complexity: 4@+10
// Space Complexity: 4@+52
// dst |= src
// @requires hex.or.init (or hex.init)
//
// both dst,src are hexes.
def or dst, src < .or.dst {
.tables.jump_to_table_entry dst, src, .or.dst
}
// Time Complexity: n(4@+10)
// Space Complexity: n(4@+52)
// dst[:n] |= src[:n]
// @requires hex.or.init (or hex.init)
def or n, dst, src {
rep(n, i) .or dst+i*dw, src+i*dw
}
ns or {
// Time Complexity: 6 (when jumping to dst, until finished)
// Space Complexity: 595
// This is where the or "truth" tables are.
// @output-param dst: This variable is an 8-bit variable (in a single op, [dbit,dbit+8)).
// Its 8-bits are expected to be {src<<4 | dst} at the jump to it (for the src,dst hexes of the or operation).
// Its 8-bits are expected to be 0 after the jump to it.
// @requires hex.tables.init_shared (or hex.init)
def init @ switch, clean_table_entry, end < ..tables.res > dst {
;end
dst: ;.switch
pad 256
switch:
// The next line is the bitwise-or flipping-table.
// The [src<<4 | dst] entry sets hex.tables.res (assumed to be 0) to (dst | src) ^ dst,
// so that xoring it with dst will update it to the or-result.
// Upon entering here, .dst was xored with the correct table-entry, and was jumped into.
// Space Complexity / total table ops: 337.
rep(256, d) stl.wflip_macro \
..tables.res+w, \
(((d&0xf)|(d>>4))^(d&0xf))*dw, \
clean_table_entry+d*dw
clean_table_entry:
// xors back the table-entry from .dst
..tables.clean_table_entry__table .dst
end:
}
}
// Time Complexity: 4@+10
// Space Complexity: 4@+52
// dst &= src
//
// both dst,src are hexes.
// @requires hex.and.init (or hex.init)
def and dst, src < .and.dst {
.tables.jump_to_table_entry dst, src, .and.dst
}
// Time Complexity: n(4@+10)
// Space Complexity: n(4@+52)
// dst[:n] &= src[:n]
// @requires hex.and.init (or hex.init)
def and n, dst, src {
rep(n, i) .and dst+i*dw, src+i*dw
}
ns and {
// Time Complexity: 6 (when jumping to dst, until finished)
// Space Complexity: 595
// This is where the and "truth" tables are.
// @output-param dst: This variable is an 8-bit variable (in a single op, [dbit,dbit+8)).
// Its 8-bits are expected to be {src<<4 | dst} at the jump to it (for the src,dst hexes of the and operation).
// Its 8-bits are expected to be 0 after the jump to it.
// @requires hex.tables.init_shared (or hex.init)
def init @ switch, clean_table_entry, end < ..tables.res > dst {
;end
dst: ;.switch
pad 256
switch:
// The next line is the bitwise-and flipping-table.
// The [src<<4 | dst] entry sets hex.tables.res (assumed to be 0) to (dst & src) ^ dst,
// so that xoring it with dst will update it to the and-result.
// Upon entering here, .dst was xored with the correct table-entry, and was jumped into.
// Space Complexity / total table ops: 337.
rep(256, d) stl.wflip_macro \
..tables.res+w, \
(((d&0xf)&(d>>4))^(d&0xf))*dw, \
clean_table_entry+d*dw
clean_table_entry:
// xors back the table-entry from .dst
..tables.clean_table_entry__table .dst
end:
}
}
}