-
Notifications
You must be signed in to change notification settings - Fork 0
/
comprakt_bench_load_store.INLINEUTILS.java
215 lines (191 loc) · 6.13 KB
/
comprakt_bench_load_store.INLINEUTILS.java
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
/* This Benchmark focusses on the load store optimization in our compiler */
class CompraktBenchLoadStore {
public static void main(String[] args) {
BooleanUtils bool = new BooleanUtils();
CompraktBenchLoadStore bench = new CompraktBenchLoadStore();
bench.part1(bool);
bench.part2(bool);
bench.part3(bool);
}
/* test the implementation*/
public void part1(BooleanUtils bool) {
new Str().init20(84, 69, 83, 84, 32, 84, 72, 69, 32, 83, 72, 73, 70, 84, 32, 82, 69, 71, 73, 83)
.concat(new Str().init3(84, 69, 82)).println(); /* TEST THE SHIFT REGISTER */
MemberBasedShiftRegister shr = new MemberBasedShiftRegister();
int val = 0;
while (val < 256) {
shr.setFromUnsignedInt(bool, val);
shr.dump(bool);
int res = shr.asUnsignedInt(bool);
if (res != val) {
new Str().init20(115, 104, 105, 102, 116, 32, 114, 101, 103, 105, 115, 116, 101, 114, 32, 105, 115, 32, 98, 114)
.concat(new Str().init4(111, 107, 101, 110)).println(); /* shift register is broken */
System.out.println(res);
System.out.println(val);
int[] panic = new int[-1];
}
System.out.println(res);
val = val + 1;
}
}
/* Allocate shift register inside the loop, doesn't escape
=> should eliminate allocation, loads and stores
ALLOCATES: 60000000 * sizeof(MemberBasedShiftRegister) + churn
*/
public void part2(BooleanUtils bool) {
int sum = 0;
int i = 0;
while (i < 6000000) {
MemberBasedShiftRegister shr = new MemberBasedShiftRegister();
shr.init(false);
if ((i % 2) == 0) {
shr.shiftLeft(false);
} else {
shr.shiftLeft(true);
}
sum = sum + shr.asUnsignedInt(bool);
i = i + 1;
}
System.out.println(sum);
}
/* Allocate shift register outside of the loop. Allocation doesn't escape the function.
=> should eliminate allocation, loads and stores
ALLOCATES: sizeof(MemberBasedShiftRegister) + churn
*/
public void part3(BooleanUtils bool) {
new Str().init20(69, 88, 69, 82, 67, 73, 83, 69, 32, 84, 72, 69, 32, 83, 72, 73, 70, 84, 32, 82)
.concat(new Str().init16(69, 71, 73, 83, 84, 69, 82, 32, 65, 32, 76, 73, 84, 84, 76, 69)).println(); /* EXERCISE THE SHIFT REGISTER A LITTLE */
MemberBasedShiftRegister shr = new MemberBasedShiftRegister();
shr.init(false);
int x = 0;
int sum = 0;
while (x < 250000000) {
if ((x % 2) == 0) {
shr.shiftLeft(false);
} else {
shr.shiftLeft(true);
}
sum = sum + shr.asUnsignedInt(bool);
x = x+1;
}
shr.dump(bool);
System.out.println(sum);
}
}
class MemberBasedShiftRegister {
/* LSB */
public boolean bit0;
public boolean bit1;
public boolean bit2;
public boolean bit3;
public boolean bit4;
public boolean bit5;
public boolean bit6;
/* MSB */
public boolean bit7;
public void init(boolean init) {
bit0 = init;
bit1 = init;
bit2 = init;
bit3 = init;
bit4 = init;
bit5 = init;
bit6 = init;
bit7 = init;
}
public void shiftLeft(boolean bit) {
bit7 = bit6;
bit6 = bit5;
bit5 = bit4;
bit4 = bit3;
bit3 = bit2;
bit2 = bit1;
bit0 = bit;
}
public void shiftRight(boolean bit) {
bit0 = bit1;
bit1 = bit2;
bit2 = bit3;
bit3 = bit4;
bit4 = bit5;
bit5 = bit6;
bit6 = bit7;
bit7 = bit;
}
public int asUnsignedInt(BooleanUtils bool) {
int sum = 0;
sum = sum + 1 * bool.toInt(bit0);
sum = sum + 2 * bool.toInt(bit1);
sum = sum + 4 * bool.toInt(bit2);
sum = sum + 8 * bool.toInt(bit3);
sum = sum + 16 * bool.toInt(bit4);
sum = sum + 32 * bool.toInt(bit5);
sum = sum + 64 * bool.toInt(bit6);
sum = sum + 128 * bool.toInt(bit7);
return sum;
}
public void setFromUnsignedInt(BooleanUtils bool, int x) {
if (x < 0 || x > 255) {
int[] y = new int[-1];
}
int left = x;
left = left - 128;
bit7 = left >= 0;
if (!bit7) {
left = left + 128;
}
left = left - 64;
bit6 = left >= 0;
if (!bit6) {
left = left + 64;
}
left = left - 32;
bit5 = left >= 0;
if (!bit5) {
left = left + 32;
}
left = left - 16;
bit4 = left >= 0;
if (!bit4) {
left = left + 16;
}
left = left - 8;
bit3 = left >= 0;
if (!bit3) {
left = left + 8;
}
left = left - 4;
bit2 = left >= 0;
if (!bit2) {
left = left + 4;
}
left = left - 2;
bit1 = left >= 0;
if (!bit1) {
left = left + 2;
}
left = left - 1;
bit0 = left >= 0;
if (!bit0) {
left = left + 1;
}
}
public void dump(BooleanUtils bool) {
new Str().init6(98, 105, 116, 48, 58, 32).print(); /* bit0: */
bool.println(bit0);
new Str().init6(98, 105, 116, 49, 58, 32).print(); /* bit1: */
bool.println(bit1);
new Str().init6(98, 105, 116, 50, 58, 32).print(); /* bit2: */
bool.println(bit2);
new Str().init6(98, 105, 116, 51, 58, 32).print(); /* bit3: */
bool.println(bit3);
new Str().init6(98, 105, 116, 52, 58, 32).print(); /* bit4: */
bool.println(bit4);
new Str().init6(98, 105, 116, 53, 58, 32).print(); /* bit5: */
bool.println(bit5);
new Str().init6(98, 105, 116, 54, 58, 32).print(); /* bit6: */
bool.println(bit6);
new Str().init6(98, 105, 116, 55, 58, 32).print(); /* bit7: */
bool.println(bit7);
}
}