diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 669c526..73467a5 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -12,7 +12,7 @@ #include #include -#define CELL_SIZE 8 +#define CELL_SIZE 5 #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define INS_SIZE (CELL_SIZE + 1) @@ -23,7 +23,13 @@ namespace r8veritas { // using cell = std::bitset; using cell = size_t; -using stat = std::array; +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_ref = const stat *; using truthtable = std::pair; using op = std::function; @@ -80,9 +86,9 @@ namespace std { template <> struct hash { size_t operator()(const r8veritas::stat &s) const { - size_t h=0; - for(auto i:s){ - h ^= hash{}(i) + 0x9e3779b9 + (h<<6) + (h>>2); + size_t h = 0; + for (auto i : s.datas) { + h ^= hash{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2); } return h; } @@ -107,7 +113,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); +operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins,bool is_initialization = false); // true means that {stat,ins} has been operated. static std::unordered_map< @@ -133,4 +139,5 @@ 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 0fab780..c9c596b 100644 --- a/src/r8veritas.cc +++ b/src/r8veritas.cc @@ -10,7 +10,7 @@ r8veritas::stat_ref generate_empty_stat() { r8veritas::stat arr; for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { - arr[i] = 0; + arr.datas[i] = 0; } auto tmp_ptr = stat_pool.insert(std::move(arr)).first; return &(*tmp_ptr); @@ -23,17 +23,20 @@ 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[i] = i; - out[i] = operation(r8veritas::cell(i)); + inp.datas[i] = i; + out.datas[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) { +operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins, + bool is_initialization) { auto key = std::pair(last_stat, r8veritas::ins_bytecode(ins)); r8veritas::stat next_stat = *last_stat; auto it = visited_stat.find(key); @@ -45,15 +48,22 @@ 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[i] & (1 << ins.sacrifice)) != 0) { - next_stat[i] ^= (1 << ins.sacrifice); + if ((next_stat.datas[i] & (1 << ins.sacrifice)) != 0) { + next_stat.datas[i] ^= (1 << ins.sacrifice); if (opcode == OPCODE_SET1) { - next_stat[i] |= cell_mask; + next_stat.datas[i] |= cell_mask; } else if (opcode == OPCODE_FLIP) { - next_stat[i] ^= cell_mask; + next_stat.datas[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))); @@ -61,7 +71,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) { + for (auto i : (*s).datas) { os << std::bitset(i).to_string() << " "; } return (os); @@ -95,8 +105,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))[j] != - (*(raw_truthtable.second))[j ^ (1 << i)]) { + if ((*(raw_truthtable.second)).datas[j] != + (*(raw_truthtable.second)).datas[j ^ (1 << i)]) { influences[i]++; } } @@ -104,7 +114,8 @@ 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}).first; + new_truthtable.first = + operate_ins(new_truthtable.first, {0, i}, true).first; } } return (new_truthtable); @@ -129,7 +140,10 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { for (; i > 0; i--) { tmp_stat_ref = stats.front(); for (auto j : r8veritas::all_ins) { - tmp_ans = operate_ins(tmp_stat_ref, j); + if (((1 << (j.sacrifice)) & (tmp_stat_ref->unvisited_cell)) == 0) { + tmp_ans = operate_ins(tmp_stat_ref, j); + } else + continue; if (tmp_ans.first == truthtable.second) { return (true); } diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 9a3d7ef..99a73d3 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_full_add); + auto tab = generate_truthtable(func_and); auto new_tab = compress_stat_space(tab); if (bfs(new_tab)) { r8veritas::path p = track_path(tab);