-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmemory.fj
122 lines (104 loc) · 2.8 KB
/
memory.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
// ---------- Memory Variables:
ns hex {
// Size Complexity: 1
//
// This is another basic variable in the FlipJump standard library - a hexadecimal (can contain only 0-15).
// you can't place it as any other standard library macro, as "running" this line is undefined behavior.
// The hex (and hex.vec) should be placed in a place that won't run.
def hex val {
;(val > 0xf ? 0xf : (val < 0 ? 0 : val)) * dw
}
def hex {
.hex 0
}
// Size Complexity: n
// read the comment for the hex macro.
def vec n, value {
rep(n, i) .hex (value>>(4*i))&0xf
}
def vec n {
rep(n, i) .hex
}
}
// ---------- Memory Manipulation:
ns hex {
// Time Complexity: @
// Space Complexity: @+12
// hex = 0
def zero hex {
.xor hex, hex
}
// Time Complexity: n@
// Space Complexity: n(@+12)
// x[:n] = 0
def zero n, x {
rep (n, i) .zero x+i*dw
}
// Time Complexity: 2@+1
// Space Complexity: 2@+25
// dst = src
//
// both dst,src are hexes.
def mov dst, src @ end {
stl.comp_if1 dst==src, end
.zero dst
.xor dst, src
end:
}
// Time Complexity: n(2@)
// Space Complexity: n(2@+24)
// Unsafe if dst and src overlap! but safe if they are the exact same address.
// dst[:n] = src[:n]
def mov n, dst, src @ end {
stl.comp_if1 dst==src, end
.zero n, dst
.xor n, dst, src
end:
}
// Complexity: 4 (avg. 2: #on-bits)
// hex ^= val (constant)
def xor_by hex, val {
wflip hex+w, (val > 0xf ? 0xf : (val < 0 ? 0 : val)) * dw
}
// Complexity: 4n (avg. 2n: #on-bits)
// hex[:n] ^= val (constant)
def xor_by n, hex, val {
rep(n, i) .xor_by hex+i*dw, (val>>(4*i))&0xf
}
// Time Complexity: @+4
// Space Complexity: @+16
// hex = val (constant)
def set hex, val {
.zero hex
.xor_by hex, val
}
// Time Complexity: n(@+4)
// Space Complexity: n(@+16)
// hex[:n] = val (constant)
def set n, hex, val {
rep(n, i) .set hex+i*dw, (val>>(4*i))&0xf
}
// Time Complexity: 3@+1
// Space Complexity: 3@+37
// hex1, hex2 = hex2, hex1
//
// both hex1,hex2 are hexes.
def swap hex1, hex2 @ end {
stl.comp_if1 hex1==hex2, end
.xor hex1, hex2
.xor hex2, hex1
.xor hex1, hex2
end:
}
// Time Complexity: n(3@)
// Space Complexity: n(3@+36)
// Unsafe if dst and src overlap! but safe if they are the exact same address.
// hex1[:n], hex2[:n] = hex2[:n], hex1[:n]
def swap n, hex1, hex2 @ end {
stl.comp_if1 hex1==hex2, end
.xor n, hex1, hex2
.xor n, hex2, hex1
.xor n, hex1, hex2
end:
}
}