-
Notifications
You must be signed in to change notification settings - Fork 12
/
alu.sol
389 lines (378 loc) · 10.5 KB
/
alu.sol
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
pragma solidity ^0.4.15;
import "./vmmemory.sol";
/**
* @title the ALU for the solidity interpreter.
* @author Sami Mäkelä
*/
contract ALU is VMMemory {
/**
* @dev handles the ALU operations of the WASM machine i.e. the WASM instructions are implemented and run here
*
* @param hint the actual opcode
* @param r1 register one
* @param r2 register two
* @param r3 register three
* @param ireg the register holding the immediate value
*
* @return returns the result of the operation
*/
function handleALU(uint hint, uint r1, uint r2, uint r3, uint ireg) internal pure returns (uint, bool) {
uint res = r1;
if (hint == 0) return (r1, false);
else if (hint == 1 || hint == 6) {
return (0, true);
// revert(); // Trap
}
// Loading from memory
else if (hint & 0xc0 == 0xc0) {
uint8[] memory arr = toMemory(r2, r3);
res = loadX(arr, (r1+ireg)%8, hint);
}
else if (hint == 2) {
if (r1 < r2) res = r1;
else res = r2;
}
// Calculate conditional jump
else if (hint == 3) {
if (r2 != 0) res = r1;
else res = r3;
}
// Calculate jump to jump table
else if (hint == 4) {
res = r2 + (r1 >= ireg ? ireg : r1);
}
// Check dynamic call
else if (hint == 7) {
if (ireg != r2) revert();
res = 0;
}
else if (hint == 0x45 || hint == 0x50) {
if (r1 == 0) res = 1;
else res = 0;
}
else if (hint == 0x46 || hint == 0x51) {
if (r1 == r2) res = 1;
else res = 0;
}
else if (hint == 0x47 || hint == 0x52) {
if (r1 != r2) res = 1;
else res = 0;
}
else if (hint == 0x48) {
if (int32(r1) < int32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x49) {
if (uint32(r1) < uint32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x4a) {
if (int32(r1) > int32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x4b) {
if (uint32(r1) > uint32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x4c) {
if (int32(r1) <= int32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x4d) {
if (uint32(r1) <= uint32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x4e) {
if (int32(r1) >= int32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x4f) {
if (uint32(r1) >= uint32(r2)) res = 1;
else res = 0;
}
else if (hint == 0x53) {
if (int64(r1) < int64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x54) {
if (uint64(r1) < uint64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x55) {
if (int64(r1) > int64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x56) {
if (uint64(r1) > uint64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x57) {
if (int64(r1) <= int64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x58) {
if (uint64(r1) <= uint64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x59) {
if (int64(r1) >= int64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x5a) {
if (uint64(r1) >= uint64(r2)) res = 1;
else res = 0;
}
else if (hint == 0x67) {
res = clz32(uint32(r1));
}
else if (hint == 0x68) {
res = ctz32(uint32(r1));
}
else if (hint == 0x69) {
res = popcnt32(uint32(r1));
}
else if (hint == 0x79) {
res = clz64(uint64(r1));
}
else if (hint == 0x7a) {
res = ctz64(uint64(r1));
}
else if (hint == 0x7b) {
res = popcnt64(uint64(r1));
}
else if (hint == 0x6a || hint == 0x7c) {
res = r1+r2;
}
else if (hint == 0x6b || hint == 0x7d) {
res = r1-r2;
}
else if (hint == 0x6c || hint == 0x7e) {
res = r1*r2;
}
else if (hint == 0x6d) {
res = uint(int32(r1)/int32(r2));
}
else if (hint == 0x7f) {
res = uint(int64(r1)/int64(r2));
}
else if (hint == 0x6e || hint == 0x80) {
res = r1/r2;
}
else if (hint == 0x6f) {
res = uint(int32(r1)%int32(r2));
}
else if (hint == 0x81) {
res = uint(int64(r1)%int64(r2));
}
else if (hint == 0x70 || hint == 0x82) {
res = r1%r2;
}
else if (hint == 0x71 || hint == 0x83) {
res = r1&r2;
}
else if (hint == 0x72 || hint == 0x84) {
res = r1|r2;
}
else if (hint == 0x73 || hint == 0x85) {
res = r1^r2;
}
else if (hint == 0x74 || hint == 0x86) {
res = r1*2**r2; // shift
}
else if (hint == 0x75 || hint == 0x87) {
res = r1/2**r2;
}
else if (hint == 0x76 || hint == 0x88) {
res = r1/2**r2;
}
// rol, ror -- fix
else if (hint == 0x77) {
res = (r1*2**r2) | (r1/2**32);
}
else if (hint == 0x78) {
res = (r1/2**r2) | (r1*2**32);
}
else if (hint == 0x89) {
res = (r1*2**r2) | (r1/2**64);
}
else if (hint == 0x8a) {
res = (r1/2**r2) | (r1*2**64);
}
if (hint >= 0x62 && hint <= 0x78) {
res = res % (2**32);
}
else if (hint >= 0x7c && hint <= 0x8a) {
res = res % (2**64);
}
return (res, false);
}
/**
* @dev counts the number of set bits for a 32 bit value
*
* @param r1 the input value
*
* @return number of sit bits in r1
*/
function popcnt32(uint32 r1) internal pure returns (uint8) {
uint32 temp = r1;
temp = (temp & 0x55555555) + ((temp >> 1) & 0x55555555);
temp = (temp & 0x33333333) + ((temp >> 2) & 0x33333333);
temp = (temp & 0x0f0f0f0f) + ((temp >> 4) & 0x0f0f0f0f);
temp = (temp & 0x00ff00ff) + ((temp >> 8) & 0x00ff00ff);
temp = (temp & 0x0000ffff) + ((temp >> 16) & 0x0000ffff);
return uint8(temp);
}
/**
* @dev counts the number of set bits for a 64 bit value
*
* @param r1 the input value
*
* @return returns the number of set bits for r1
*/
function popcnt64(uint64 r1) internal pure returns (uint8) {
uint64 temp = r1;
temp = (temp & 0x5555555555555555) + ((temp >> 1) & 0x5555555555555555);
temp = (temp & 0x3333333333333333) + ((temp >> 2) & 0x3333333333333333);
temp = (temp & 0x0f0f0f0f0f0f0f0f) + ((temp >> 4) & 0x0f0f0f0f0f0f0f0f);
temp = (temp & 0x00ff00ff00ff00ff) + ((temp >> 8) & 0x00ff00ff00ff00ff);
temp = (temp & 0x0000ffff0000ffff) + ((temp >> 16) & 0x0000ffff0000ffff);
temp = (temp & 0x00000000ffffffff) + ((temp >> 32) & 0x00000000ffffffff);
return uint8(temp);
}
/**
* @dev counts the number of leading zeroes for a 32-bit value using binary search
*
* @param r1 the input
*
* @return returns the number of leading zeroes for r1
*/
function clz32(uint32 r1) internal pure returns (uint8) {
if (r1 == 0) return 32;
uint32 temp_r1 = r1;
uint8 n = 0;
if (temp_r1 & 0xffff0000 == 0) {
n += 16;
temp_r1 = temp_r1 << 16;
}
if (temp_r1 & 0xff000000 == 0) {
n += 8;
temp_r1 = temp_r1 << 8;
}
if (temp_r1 & 0xf0000000 == 0) {
n += 4;
temp_r1 = temp_r1 << 4;
}
if (temp_r1 & 0xc0000000 == 0) {
n += 2;
temp_r1 = temp_r1 << 2;
}
if (temp_r1 & 0x8000000 == 0) {
n++;
}
return n;
}
/**
* @dev counts the number of leading zeroes for a 64-bit value using binary search
*
* @param r1 the input value
*
* @return returns the number of leading zeroes for the input vlaue
*/
function clz64(uint64 r1) internal pure returns (uint8) {
if (r1 == 0) return 64;
uint64 temp_r1 = r1;
uint8 n = 0;
if (temp_r1 & 0xffffffff00000000 == 0) {
n += 32;
temp_r1 = temp_r1 << 32;
}
if (temp_r1 & 0xffff000000000000 == 0) {
n += 16;
temp_r1 == temp_r1 << 16;
}
if (temp_r1 & 0xff00000000000000 == 0) {
n+= 8;
temp_r1 = temp_r1 << 8;
}
if (temp_r1 & 0xf000000000000000 == 0) {
n += 4;
temp_r1 = temp_r1 << 4;
}
if (temp_r1 & 0xc000000000000000 == 0) {
n += 2;
temp_r1 = temp_r1 << 2;
}
if (temp_r1 & 0x8000000000000000 == 0) {
n += 1;
}
return n;
}
/**
* @dev counts the number of trailing zeroes for a 32-bit value using binary search
*
* @param r1 the input value
*
* @return returns the number of trailing zeroes for the input value
*/
function ctz32(uint32 r1) internal pure returns (uint8) {
if (r1 == 0) return 32;
uint32 temp_r1 = r1;
uint8 n = 0;
if (temp_r1 & 0x0000ffff == 0) {
n += 16;
temp_r1 = temp_r1 >> 16;
}
if (temp_r1 & 0x000000ff == 0) {
n += 8;
temp_r1 = temp_r1 >> 8;
}
if (temp_r1 & 0x0000000f == 0) {
n += 4;
temp_r1 = temp_r1 >> 4;
}
if (temp_r1 & 0x00000003 == 0) {
n += 2;
temp_r1 = temp_r1 >> 2;
}
if (temp_r1 & 0x00000001 == 0) {
n += 1;
}
return n;
}
/**
* @dev returns the number of trailing zeroes for a 64-bit input value using binary search
*
* @param r1 the input value
*
* @return returns the trailing zeroes count for the input value
*/
function ctz64(uint64 r1) internal pure returns (uint8) {
if (r1 == 0) return 64;
uint64 temp_r1 = r1;
uint8 n = 0;
if (temp_r1 & 0x00000000ffffffff == 0) {
n += 32;
temp_r1 = temp_r1 >> 32;
}
if (temp_r1 & 0x000000000000ffff == 0) {
n += 16;
temp_r1 = temp_r1 >> 16;
}
if (temp_r1 & 0x00000000000000ff == 0) {
n += 8;
temp_r1 = temp_r1 >> 8;
}
if (temp_r1 & 0x000000000000000f == 0) {
n += 4;
temp_r1 = temp_r1 >> 4;
}
if (temp_r1 & 0x0000000000000003 == 0) {
n += 2;
temp_r1 = temp_r1 >> 2;
}
if (temp_r1 & 0x0000000000000001 == 0) {
n += 1;
}
return n;
}
}