diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 73467a5..1c38855 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -12,7 +12,7 @@ #include #include -#define CELL_SIZE 5 +#define CELL_SIZE 8 #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define INS_SIZE (CELL_SIZE + 1) @@ -23,13 +23,7 @@ namespace r8veritas { // using cell = std::bitset; using cell = size_t; -using stat = struct stat { - std::array datas; - r8veritas::cell unvisited_cell = 0; - bool operator==(const r8veritas::stat &s) const { - return (this->datas == s.datas); - } -}; +using stat = std::array; using stat_ref = const stat *; using truthtable = std::pair; using op = std::function; @@ -87,7 +81,7 @@ namespace std { template <> struct hash { size_t operator()(const r8veritas::stat &s) const { size_t h = 0; - for (auto i : s.datas) { + for (auto i : s) { h ^= hash{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2); } return h; @@ -113,7 +107,7 @@ template <> struct hash> { r8veritas::truthtable generate_truthtable(const r8veritas::op &operation); std::pair -operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,bool is_initialization = false); +operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins); // true means that {stat,ins} has been operated. static std::unordered_map< @@ -139,5 +133,4 @@ std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s); std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s); static std::unordered_set stat_pool; - #endif \ No newline at end of file diff --git a/src/r8veritas.cc b/src/r8veritas.cc index c9c596b..511de43 100644 --- a/src/r8veritas.cc +++ b/src/r8veritas.cc @@ -5,12 +5,13 @@ #include #include #include +#include #include r8veritas::stat_ref generate_empty_stat() { r8veritas::stat arr; for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { - arr.datas[i] = 0; + arr[i] = 0; } auto tmp_ptr = stat_pool.insert(std::move(arr)).first; return &(*tmp_ptr); @@ -23,20 +24,17 @@ r8veritas::stat_ref save_stat(r8veritas::stat &&st) { r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { r8veritas::stat inp = *generate_empty_stat(); - inp.unvisited_cell = (1 << CELL_SIZE) - 1; r8veritas::stat out = *generate_empty_stat(); - out.unvisited_cell = 0; for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { - inp.datas[i] = i; - out.datas[i] = operation(r8veritas::cell(i)); + inp[i] = i; + out[i] = operation(r8veritas::cell(i)); } return (r8veritas::truthtable(save_stat(std::move(inp)), save_stat(std::move(out)))); } std::pair -operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins, - bool is_initialization) { +operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) { auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins)); r8veritas::stat next_stat = *last_stat; auto it = visited_stat.find(key); @@ -48,22 +46,15 @@ operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins, r8veritas::cell cell_mask = (ins.bc >> 1); for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { - if ((next_stat.datas[i] & (1 << ins.sacrifice)) != 0) { - next_stat.datas[i] ^= (1 << ins.sacrifice); + if ((next_stat[i] & (1 << ins.sacrifice)) != 0) { + next_stat[i] ^= (1 << ins.sacrifice); if (opcode == OPCODE_SET1) { - next_stat.datas[i] |= cell_mask; + next_stat[i] |= cell_mask; } else if (opcode == OPCODE_FLIP) { - next_stat.datas[i] ^= cell_mask; + next_stat[i] ^= cell_mask; } } } - next_stat.unvisited_cell = last_stat->unvisited_cell; - if (!is_initialization) { - next_stat.unvisited_cell &= ~(1 << ins.sacrifice); - next_stat.unvisited_cell &= ~(ins.bc >> 1); - } else { - next_stat.unvisited_cell ^= ~(1 << ins.sacrifice); - } r8veritas::stat_ref tmp_next_stat_ref = save_stat(std::move(next_stat)); visited_stat.insert(std::pair(key, tmp_next_stat_ref)); visited_op.insert(std::pair(tmp_next_stat_ref, std::move(key))); @@ -71,7 +62,7 @@ operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins, } std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) { - for (auto i : (*s).datas) { + for (auto i : *s) { os << std::bitset(i).to_string() << " "; } return (os); @@ -105,8 +96,8 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable) { for (r8veritas::index i = 0; i < CELL_SIZE; i++) { influences[i] = 0; for (r8veritas::index j = 0; j < (1 << CELL_SIZE); j++) { - if ((*(raw_truthtable.second)).datas[j] != - (*(raw_truthtable.second)).datas[j ^ (1 << i)]) { + if ((*(raw_truthtable.second))[j] != + (*(raw_truthtable.second))[j ^ (1 << i)]) { influences[i]++; } } @@ -114,14 +105,14 @@ compress_stat_space(const r8veritas::truthtable &raw_truthtable) { } for (r8veritas::index i = 0; i < CELL_SIZE; i++) { if (influences[i] == 0) { - new_truthtable.first = - operate_ins(new_truthtable.first, {0, i}, true).first; + new_truthtable.first = operate_ins(new_truthtable.first, {0, i}).first; } } return (new_truthtable); } static std::queue stats; +static std::unordered_set remembered_stats; bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { if (layer == 1) { @@ -140,15 +131,13 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { for (; i > 0; i--) { tmp_stat_ref = stats.front(); for (auto j : r8veritas::all_ins) { - if (((1 << (j.sacrifice)) & (tmp_stat_ref->unvisited_cell)) == 0) { - tmp_ans = operate_ins(tmp_stat_ref, j); - } else - continue; + tmp_ans = operate_ins(tmp_stat_ref, j); if (tmp_ans.first == truthtable.second) { return (true); } - if (!(tmp_ans.second)) { + if (!((tmp_ans.second) || remembered_stats.contains(tmp_ans.first))) { stats.push(tmp_ans.first); + remembered_stats.insert(tmp_ans.first); } } stats.pop(); diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 99a73d3..9a3d7ef 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -21,7 +21,7 @@ r8veritas::cell func_full_add(r8veritas::cell inp) { } int main() { - auto tab = generate_truthtable(func_and); + auto tab = generate_truthtable(func_full_add); auto new_tab = compress_stat_space(tab); if (bfs(new_tab)) { r8veritas::path p = track_path(tab);