-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxor_to_pointer.fj
181 lines (160 loc) · 7.45 KB
/
xor_to_pointer.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
ns hex {
// Time Complexity: w(0.5@+2) + @
// Space Complexity: w(0.5@+14) + @
// like: *ptr;
// Flip the address the pointer points to.
// ptr is a hex[:w/4] that holds an address.
def ptr_flip ptr @ cleanup < hex.pointers.to_flip {
wflip hex.pointers.to_flip+w, cleanup
.pointers.set_flip_pointer ptr
;hex.pointers.to_flip
pad 4
cleanup:
wflip hex.pointers.to_flip+w, cleanup
}
// Time Complexity: w(0.5@+2) + @+6
// Space Complexity: w(0.5@+14) + @+6
// The stl.comp_flip_if executes in ~##w, which should be much less than @/2 operations.
// like: (*ptr)+dbit;
// Flip the address dbit-ahead of what the pointer points to.
// ptr is a hex[:w/4] that holds an address, which we assume is dw-aligned.
def ptr_flip_dbit ptr < .pointers.to_flip {
wflip .pointers.to_flip, dbit
.ptr_flip ptr
wflip .pointers.to_flip, dbit
}
// Time Complexity: w(0.5@+2) + 5@+12
// Space Complexity: w(0.5@+14) + 5@+76
// like: hex.xor *ptr, hex
// ptr is a hex[:w/4] that holds an address, which we assume is an hex-variable, which is dw-aligned.
def xor_hex_to_ptr ptr, hex {
.pointers.set_flip_pointer ptr
.pointers.xor_hex_to_flip_ptr hex
}
// Time Complexity: w(0.5@+2) + 10@+24
// Space Complexity: w(0.5@+14) + 10@+152
// like: hex.xor *ptr, hex[:2]
// ptr is a hex[:w/4] that holds an address, which we assume is an hex-variable, which is dw-aligned.
def xor_byte_to_ptr ptr, hex {
.pointers.set_flip_pointer ptr
.pointers.xor_byte_to_flip_ptr hex
}
// Time Complexity: n(w(0.5@+2) + 14@+26)
// Space Complexity: n(w(0.9@+17) + 10@+131)
// like: hex.xor *ptr[:n], hex[:n]
// ptr is a hex[:w/4] that holds an address, which we assume is an hex-variable, which is dw-aligned.
def xor_hex_to_ptr n, ptr, hex {
rep(n, i) .pointers.xor_hex_to_ptr_and_inc ptr, hex + i*dw
.ptr_sub ptr, n
}
// Time Complexity: n(w(0.5@+2) + 19@+38)
// Space Complexity: n(w(0.9@+17) + 15@+207)
// like: hex.xor *ptr[:n], hex[:2n]
// ptr is a hex[:w/4] that holds an address, which we assume is an hex-variable, which is dw-aligned.
def xor_byte_to_ptr n, ptr, hex {
rep(n, i) .pointers.xor_byte_to_ptr_and_inc ptr, hex + i*2*dw
.ptr_sub ptr, n
}
ns pointers {
// Time Complexity: w(0.5@+2) + 14@+26
// Space Complexity: w(0.9@+17) + 10@+131
// like: hex.xor *ptr, hex
// ptr += dw
// ptr is a hex[:w/4] that holds an address, which we assume is an hex-variable, which is dw-aligned.
def xor_hex_to_ptr_and_inc ptr, hex {
..xor_hex_to_ptr ptr, hex
..ptr_inc ptr
}
// Time Complexity: w(0.5@+2) + 19@+38
// Space Complexity: w(0.9@+17) + 15@+207
// like: hex.xor *ptr, hex[:2]
// ptr += dw
// ptr is a hex[:w/4] that holds an address, which we assume is an hex-variable, which is dw-aligned.
def xor_byte_to_ptr_and_inc ptr, hex {
..xor_byte_to_ptr ptr, hex
..ptr_inc ptr
}
// Time Complexity: 5@+12
// Space Complexity: 5@+76
// xors (the parameter hex) to the hex pointed by the memory-word hex.pointers.to_flip.
// use after: .pointers.set_flip_pointer ptr
// does: .xor *ptr, hex (as it uses the address in to_flip)
// It assumes that the value in the memory-word to_flip is a dw-aligned address to an hex-variable.
def xor_hex_to_flip_ptr hex {
.xor_hex_to_flip_ptr hex, 0
}
// Time Complexity: 10@+24
// Space Complexity: 10@+152
// xors (the byte hex[2:]) to the byte pointed by the memory-word hex.pointers.to_flip.
// use after: .pointers.set_flip_pointer ptr
// does: .xor *ptr, hex[:2] (as it uses the address in to_flip)
// It assumes that the value in the memory-word to_flip is a dw-aligned address to an hex-variable.
def xor_byte_to_flip_ptr hex {
rep(2, i) .xor_hex_to_flip_ptr hex+i*dw, 4*i
}
// Time Complexity: 5@+12
// Space Complexity: 5@+76
// xors (the parameter hex, shifted left by bit_shift) to the hex/byte pointed by the memory-word hex.pointers.to_flip.
// use after: .pointers.set_flip_pointer ptr
// does: .xor *ptr, hex<<bit_shift (as it uses the address in to_flip)
// It assumes that the value in the memory-word to_flip is a dw-aligned address to an hex-variable.
// bit_shift is a constant that's assumed to be divisible by 4.
def xor_hex_to_flip_ptr hex, bit_shift @ prepare_flip_bit0, prepare_flip_bit1, prepare_flip_bit2, prepare_flip_bit3, \
after_flip_bit0, after_flip_bit1, after_flip_bit2, after_flip_bit3, cleanup < hex.pointers.to_flip {
wflip hex.pointers.to_flip+w, after_flip_bit0, prepare_flip_bit0
prepare_flip_bit0:
wflip hex.pointers.to_flip, dbit+0+bit_shift
hex.if_flags hex, 0xAAAA, after_flip_bit0, hex.pointers.to_flip
prepare_flip_bit1:
wflip hex.pointers.to_flip, (dbit+0+bit_shift)^(dbit+1+bit_shift)
hex.if_flags hex, 0xCCCC, after_flip_bit1, hex.pointers.to_flip
prepare_flip_bit3:
wflip hex.pointers.to_flip, (dbit+1+bit_shift)^(dbit+3+bit_shift)
hex.if_flags hex, 0xFF00, after_flip_bit3, hex.pointers.to_flip
prepare_flip_bit2:
wflip hex.pointers.to_flip, (dbit+3+bit_shift)^(dbit+2+bit_shift)
hex.if_flags hex, 0xF0F0, after_flip_bit2, hex.pointers.to_flip
pad 4
after_flip_bit0:
hex.pointers.to_flip+dbit+0;prepare_flip_bit1
after_flip_bit1:
hex.pointers.to_flip+dbit+1;prepare_flip_bit3
after_flip_bit2:
wflip hex.pointers.to_flip, dbit+2+bit_shift, cleanup
after_flip_bit3:
hex.pointers.to_flip+dbit+0;prepare_flip_bit2
cleanup:
wflip hex.pointers.to_flip+w, after_flip_bit2
}
}
// Time Complexity: w(1.5@+5)
// Space Complexity: w(1.5@+17)
// like: wflip *ptr, value
// ptr is a hex[:w/4] that holds an address, which we assume is w-aligned.
def ptr_wflip ptr, value {
.pointers.set_flip_pointer ptr
rep(w, i) .pointers.advance_by_one_and_flip__ptr_wflip (#(i^((i+1)%w))), (value>>i)&1
}
ns pointers {
// Advances *to_flip by 1 (which takes n flips, from bit0 to bit1, bit2,...).
// If do_flip (value) isn't 0 - than make a flip, like: to_flip;advance.
def advance_by_one_and_flip__ptr_wflip n, do_flip @ cleanup, advance < hex.pointers.to_flip {
stl.comp_if0 do_flip, advance
wflip hex.pointers.to_flip+w, cleanup, hex.pointers.to_flip
cleanup:
wflip hex.pointers.to_flip+w, cleanup, advance
pad 4
advance:
rep(n, i) bit.exact_not hex.pointers.to_flip+i
}
}
// Time Complexity: w(1.5@+5)
// Space Complexity: w(1.5@+17)
// like: wflip (*ptr)+w, value
// ptr is a hex[:w/4] that holds an address, which we assume is dw-aligned.
def ptr_wflip_2nd_word ptr, value {
ptr+dbit + ((#w-1)/4)*dw + (#w-1)%4;
hex.ptr_wflip ptr, value
ptr+dbit + ((#w-1)/4)*dw + (#w-1)%4;
}
}