-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
97 lines (89 loc) · 2.28 KB
/
main.cpp
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
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include "compile_random.h"
#include "nand_nor.h"
#include "nn_integer.h"
enum ECommand : uint64_t {
nand_ = 1,
nor_ = 2,
and_ = 3,
or_ = 4,
xor_ = 5,
};
void add_commands(std::vector<uint64_t>& code) {
const std::vector<uint64_t> ops = {nand_, nor_, and_, or_, xor_};
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<uint64_t> uid(0, 4);
std::uniform_int_distribution<uint64_t> uid_32(1, UINT32_MAX);
code.emplace_back(ops[uid(rng)]);
code.emplace_back(uid_32(rng));
code.emplace_back(uid_32(rng));
code.emplace_back(ops[uid(rng)]);
code.emplace_back(uid_32(rng));
code.emplace_back(uid_32(rng));
}
uint64_t diff_me(uint64_t x) {
using u64 = uint64_t;
CNNInt<u64> r(std::chrono::system_clock::now().time_since_epoch().count());
CNNInt<u64> test(x);
test++;
test--;
test|=r;
test^=r;
test&=r;
test^=r;
test = ~test;
return test.value();
}
uint64_t execute_code(std::vector<uint64_t>& code) {
using u64 = uint64_t;
CNNInt<u64> ip(0);
auto extract_two_operands = [&]() {
auto a = code[ip.value()];
auto b = code[(++ip).value()];
ip++;
return std::make_pair(a, b);
};
u64 result = 0;
while (ip.value() <= code.size() - 1) {
auto command = code[ip.value()];
ip++;
auto ops = extract_two_operands();
switch (command) {
case nand_: {
result = XorR<u64, RND>(NandR<u64, RND>(ops.first, ops.second), result);
break;
}
case nor_: {
result = XorR<u64, RND>(NorR<u64, RND>(ops.first, ops.second), result);
break;
}
case and_: {
result = XorR<u64, RND>(AndR<u64, RND>(ops.first, ops.second), result);
break;
}
case or_: {
result = XorR<u64, RND>(OrR<u64, RND>(ops.first, ops.second), result);
break;
}
case xor_: {
result = XorR<u64, RND>(XorR<u64, RND>(ops.first, ops.second), result);
break;
}
default:
break;
}
}
return result;
}
int main() {
std::vector<uint64_t> code;
add_commands(code);
auto now = std::chrono::system_clock::now().time_since_epoch().count();
std::cout << execute_code(code) << std::endl;
std::cout << diff_me(now) << std::endl;
return 0;
}