From 03889e7874d6c21a904f6d4a76eb6ca9a8babf39 Mon Sep 17 00:00:00 2001 From: StarCells Date: Fri, 14 Aug 2026 18:01:06 +0800 Subject: [PATCH] [err]202608141801 --- include/r8veritas.hh | 397 +++++++++++++++++++++++++++++++------------ src/r8veritas.cc | 215 ----------------------- tests/r8_test.cc | 19 ++- 3 files changed, 300 insertions(+), 331 deletions(-) diff --git a/include/r8veritas.hh b/include/r8veritas.hh index ce058f8..ea102f8 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -1,12 +1,16 @@ #ifndef R8VERITAS #define R8VERITAS +#include #include #include +#include #include #include #include +#include #include +#include #include #include #include @@ -14,7 +18,6 @@ #include #include -#define CELL_SIZE 5 #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define INS_SIZE (CELL_SIZE + 1) @@ -22,119 +25,297 @@ #define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH) // mask,opcode and sacrifice -namespace r8veritas { -using cell = uint8_t; -using stat = std::array; -using stat_ref = const stat *; -using truthtable = std::pair; -using op = std::function; -using index = size_t; -using influence = size_t; -using ins_body = size_t; -using ins_bytecode = size_t; -/* -using ins_body = std::bitset; -using ins_bytecode = std::bitset; -*/ - -enum ins_opcode { - SET1 = 0, - FLIP = 1, -}; - -struct ins { - r8veritas::ins_body bc; - r8veritas::index sacrifice; - operator const ins_bytecode() const { - return (bc << CELL_SIZE_BITWIDTH) | sacrifice; - } -}; -inline r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) { - return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)}; -} -using path = - std::stack>; - -inline std::array all_influences; - -inline const std::set all_ins = - []() -> std::set { - std::set s; - for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE; - sacrifice_it++) { - for (r8veritas::index bc_it = 0; bc_it < (1 << INS_SIZE); bc_it++) { - auto tmp = bc_it & (~(1 << (sacrifice_it + 1))); - // sacrifice shouldn't be operated, - // which means that bit in bc's mask can't be 1. - // so here I forcefully change it to 0, - // and use set to ensure it won't duplicate. - // for here's the initialization part,it's acceptable - s.insert({((tmp == 0) ? 1 : tmp), sacrifice_it}); - // both bc == 0b0 and bc == 0b1 mean erasing sacrifice,so here I skip 0b0 - } - } - return s; -}(); - -inline bool debug_output = false; - -} // namespace r8veritas - -namespace std { -// Overload hash to make some unordered_maps work. - -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); - // Magic,from Boost's hash_combine. - // An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421 - } - return h; - } -}; - -template <> struct hash> { - size_t operator()( - const pair &p) const { - size_t h = hash{}(p.first) + 0x9e3779b9; - // Yep,magic again. - h ^= hash{}(p.second) + 0x9e3779b9 + (h << 6) + - (h >> 2); - return h; - } -}; - -} // namespace std - #define OPCODE_SET1 (static_cast(r8veritas::ins_opcode::SET1)) #define OPCODE_FLIP (static_cast(r8veritas::ins_opcode::FLIP)) -r8veritas::truthtable generate_truthtable(const r8veritas::op &operation); -r8veritas::stat_ref operate_ins(const r8veritas::stat_ref &last_stat, - const r8veritas::ins &ins); +template class r8veritas { +public: + using cell = uint8_t; -inline std::unordered_map< - r8veritas::stat_ref, - std::pair> - visited_op; + using stat = std::array; + using stat_ref = const stat *; + using truthtable = std::pair; + using op = std::function; + using index = size_t; + using influence = size_t; + using ins_body = size_t; + using ins_bytecode = size_t; + enum ins_opcode { + SET1 = 0, + FLIP = 1, + }; -inline std::unordered_set stat_pool; -inline std::array nicknames; -inline std::array tp_paths; + struct ins { + r8veritas::ins_body bc; + r8veritas::index sacrifice; + operator const ins_bytecode() const { + return (bc << CELL_SIZE_BITWIDTH) | sacrifice; + } + }; + using path = + std::stack>; -std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s); +private: + std::array all_influences; -r8veritas::truthtable -compress_stat_space(const r8veritas::truthtable &raw_truthtable); -// raw means input must be 0 ~ (1< all_ins = []() -> std::set { + std::set s; + for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE; + sacrifice_it++) { + for (r8veritas::index bc_it = 0; bc_it < (1 << INS_SIZE); bc_it++) { + auto tmp = bc_it & (~(1 << (sacrifice_it + 1))); + // sacrifice shouldn't be operated, + // which means that bit in bc's mask can't be 1. + // so here I forcefully change it to 0, + // and use set to ensure it won't duplicate. + // for here's the initialization part,it's acceptable + s.insert({((tmp == 0) ? 1 : tmp), sacrifice_it}); + // both bc == 0b0 and bc == 0b1 mean erasing sacrifice,so here I skip + // 0b0 + } + } + return s; + }(); + + bool debug_output = false; + std::unordered_map> + visited_op; + struct stat_hash { + size_t operator()(const stat &s) const { + size_t h = 0; + for (auto i : s) { + h ^= std::hash{}(i) + 0x9e3779b9 + (h << 6) + (h >> 2); + // Magic,from Boost's hash_combine. + // An explanation from MCMC here: https://zhuanlan.zhihu.com/p/574573421 + } + return h; + } + }; + std::unordered_set stat_pool; + std::array nicknames; + std::array tp_paths; + std::queue stats; + std::unordered_set remembered_stats; + +public: + r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) { + return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)}; + } + void initialize_nicknames() { + for (r8veritas::index i = 0; i < CELL_SIZE; i++) { + nicknames[i] = "%" + std::to_string(i + 1); + } + return; + } + + void initialize_tp_paths() { + for (r8veritas::index i = 0; i < CELL_SIZE; i++) { + tp_paths[i] = ".tp " + nicknames[i]; + } + + return; + } + + r8veritas::stat_ref generate_empty_stat() { + r8veritas::stat arr; + for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { + arr[i] = 0; + } + auto tmp_ptr = stat_pool.insert(std::move(arr)).first; + return &(*tmp_ptr); + } + + // WARNING:ST WILL BE DESTROYED + r8veritas::stat_ref save_stat(r8veritas::stat &&st) { + return &(*stat_pool.insert(std::move(st)).first); + } + + r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { + r8veritas::stat inp = *generate_empty_stat(); + r8veritas::stat out = *generate_empty_stat(); + for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { + inp[i] = i; + out[i] = operation(r8veritas::cell(i)); + } + return (r8veritas::truthtable(save_stat(std::move(inp)), + save_stat(std::move(out)))); + } + + r8veritas::stat_ref 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; + int opcode = ins.bc & 1; + + 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 (opcode == OPCODE_SET1) { + next_stat[i] |= cell_mask; + } else if (opcode == OPCODE_FLIP) { + next_stat[i] ^= cell_mask; + } + } + } + r8veritas::stat_ref tmp_next_stat_ref = save_stat(std::move(next_stat)); + visited_op.insert(std::pair(tmp_next_stat_ref, std::move(key))); + return tmp_next_stat_ref; + } + + void output_stat(std::ostream &os, const r8veritas::stat_ref &s) { + for (auto i : *s) { + os << std::bitset(i).to_string() << " "; + } + } + + void output_ins(std::ostream &os, const r8veritas::ins &s) { + + auto str = std::bitset(s.bc).to_string(); + std::queue arg_list; + bool just_sacrifice = ((s.bc == 0b0) || (s.bc == 0b1)); + + std::reverse(&str[0], &str[CELL_SIZE]); + /* e.g. + CELL_SIZE = 4 + (0b)00101 -> 01001 + ^ ^str[CELL_SIZE] + */ + if (!just_sacrifice) { + for (r8veritas::index i = 0; i < str.length() - 1; i++) { + if (str[i] == '1') { + arg_list.push(i); + } + } + } + + bool is_set1 = (str[CELL_SIZE] == '1'); + + if (r8veritas::debug_output) { + os << "SACRIFICE: " << s.sacrifice; + if (!just_sacrifice) { + if (is_set1) + os << ", SET1: {"; + else + os << ", FLIP: {"; + while (!arg_list.empty()) { + os << arg_list.front() << ','; + arg_list.pop(); + } + os << "\b}"; + } + os << ';'; + /* e.g. + + SACRIFICE: a, SET1: {b,c}; + + */ + } else { + os << tp_paths[s.sacrifice] << std::endl; + os << "biz" << std::endl; + if (!just_sacrifice) { + while (!arg_list.empty()) { + os << '\t' << tp_paths[arg_list.front()] << std::endl; + if (is_set1) + os << "\tset_1" << std::endl; + else + os << "\tflb" << std::endl; + arg_list.pop(); + } + os << '\t' << tp_paths[s.sacrifice] << std::endl; + } + os << "\tflb" << std::endl; + os << "rnz" << std::endl; + /* e.g. + + .tp a + biz + .tp b + set_1 + .tp c + set_1 + .tp a + flb + rnz + + */ + } + } + + r8veritas::truthtable + compress_stat_space(const r8veritas::truthtable &raw_truthtable) { + // raw means input must be 0 ~ (1< influences; + r8veritas::truthtable new_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)]) { + // when [j] can influence the ans + influences[i]++; + } + } + influences[i] >>= 1; + } + for (r8veritas::index i = 0; i < CELL_SIZE; i++) { + if (influences[i] == 0) { + new_truthtable.first = operate_ins(new_truthtable.first, {0, i}); + } + } + return (new_truthtable); + } + + bool bfs(const r8veritas::truthtable &truthtable, + r8veritas::index layer = 1) { + + if (layer == 1) { + stats.push(truthtable.first); + } + + if (stats.empty()) { + return (false); + } + + r8veritas::index i = stats.size(); + if (r8veritas::debug_output) + std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; + r8veritas::stat_ref tmp_stat_ref; + r8veritas::stat_ref tmp_ans; + auto operated = false; + for (; i > 0; i--) { + tmp_stat_ref = stats.front(); + for (auto j : r8veritas::all_ins) { + tmp_ans = operate_ins(tmp_stat_ref, j); + if (tmp_ans == truthtable.second) { + while (!stats.empty()) + stats.pop(); + remembered_stats.clear(); + return (true); + } + if (!remembered_stats.contains(tmp_ans)) { + stats.push(tmp_ans); + remembered_stats.insert(tmp_ans); + } + } + stats.pop(); + } + return (bfs(truthtable, layer + 1)); + } + + r8veritas::path track_path(const r8veritas::truthtable &truthtable) { + r8veritas::path p; + for (auto tmp_stat = truthtable.second; tmp_stat != truthtable.first;) { + p.push(visited_op[tmp_stat]); + tmp_stat = p.top().first; + } + return p; + } + void enable_debug_output() { debug_output = true; } + void disable_debug_output() { debug_output = false; } +}; -bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer = 1); -r8veritas::path track_path(const r8veritas::truthtable &truthtable); -std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s); -std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s); -void initialize_nicknames(); -void initialize_tp_paths(); #endif \ No newline at end of file diff --git a/src/r8veritas.cc b/src/r8veritas.cc index c54132e..10b9ed9 100644 --- a/src/r8veritas.cc +++ b/src/r8veritas.cc @@ -8,218 +8,3 @@ #include #include #include - -void initialize_nicknames() { - for (r8veritas::index i = 0; i < CELL_SIZE; i++) { - nicknames[i] = "%" + std::to_string(i + 1); - } - return; -} - -void initialize_tp_paths() { - for (r8veritas::index i = 0; i < CELL_SIZE; i++) { - tp_paths[i] = ".tp " + nicknames[i]; - } - - return; -} - -r8veritas::stat_ref generate_empty_stat() { - r8veritas::stat arr; - for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { - arr[i] = 0; - } - auto tmp_ptr = stat_pool.insert(std::move(arr)).first; - return &(*tmp_ptr); -} - -// WARNING:ST WILL BE DESTROYED -r8veritas::stat_ref save_stat(r8veritas::stat &&st) { - return &(*stat_pool.insert(std::move(st)).first); -} - -r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { - r8veritas::stat inp = *generate_empty_stat(); - r8veritas::stat out = *generate_empty_stat(); - for (r8veritas::index i = 0; i < (1 << CELL_SIZE); i++) { - inp[i] = i; - out[i] = operation(r8veritas::cell(i)); - } - return (r8veritas::truthtable(save_stat(std::move(inp)), - save_stat(std::move(out)))); -} - -r8veritas::stat_ref 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; - int opcode = ins.bc & 1; - - 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 (opcode == OPCODE_SET1) { - next_stat[i] |= cell_mask; - } else if (opcode == OPCODE_FLIP) { - next_stat[i] ^= cell_mask; - } - } - } - r8veritas::stat_ref tmp_next_stat_ref = save_stat(std::move(next_stat)); - visited_op.insert(std::pair(tmp_next_stat_ref, std::move(key))); - return tmp_next_stat_ref; -} - -std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) { - for (auto i : *s) { - os << std::bitset(i).to_string() << " "; - } - return (os); -} - -std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s) { - - auto str = std::bitset(s.bc).to_string(); - std::queue arg_list; - bool just_sacrifice = ((s.bc == 0b0) || (s.bc == 0b1)); - - std::reverse(&str[0], &str[CELL_SIZE]); - /* e.g. - CELL_SIZE = 4 - (0b)00101 -> 01001 - ^ ^str[CELL_SIZE] - */ - if (!just_sacrifice) { - for (r8veritas::index i = 0; i < str.length() - 1; i++) { - if (str[i] == '1') { - arg_list.push(i); - } - } - } - - bool is_set1 = (str[CELL_SIZE] == '1'); - - if (r8veritas::debug_output) { - os << "SACRIFICE: " << s.sacrifice; - if (!just_sacrifice) { - if (is_set1) - os << ", SET1: {"; - else - os << ", FLIP: {"; - while (!arg_list.empty()) { - os << arg_list.front() << ','; - arg_list.pop(); - } - os << "\b}"; - } - os << ';'; - /* e.g. - - SACRIFICE: a, SET1: {b,c}; - - */ - } else { - os << tp_paths[s.sacrifice] << std::endl; - os << "biz" << std::endl; - if (!just_sacrifice) { - while (!arg_list.empty()) { - os << '\t' << tp_paths[arg_list.front()] << std::endl; - if (is_set1) - os << "\tset_1" << std::endl; - else - os << "\tflb" << std::endl; - arg_list.pop(); - } - os << '\t' << tp_paths[s.sacrifice] << std::endl; - } - os << "\tflb" << std::endl; - os << "rnz" << std::endl; - /* e.g. - - .tp a - biz - .tp b - set_1 - .tp c - set_1 - .tp a - flb - rnz - - */ - } - - return (os); -} - -r8veritas::truthtable -compress_stat_space(const r8veritas::truthtable &raw_truthtable) { - std::array influences; - r8veritas::truthtable new_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)]) { - // when [j] can influence the ans - influences[i]++; - } - } - influences[i] >>= 1; - } - for (r8veritas::index i = 0; i < CELL_SIZE; i++) { - if (influences[i] == 0) { - new_truthtable.first = operate_ins(new_truthtable.first, {0, i}); - } - } - 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) { - stats.push(truthtable.first); - } - - if (stats.empty()) { - return (false); - } - - r8veritas::index i = stats.size(); - if (r8veritas::debug_output) - std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; - r8veritas::stat_ref tmp_stat_ref; - r8veritas::stat_ref tmp_ans; - auto operated = false; - for (; i > 0; i--) { - tmp_stat_ref = stats.front(); - for (auto j : r8veritas::all_ins) { - tmp_ans = operate_ins(tmp_stat_ref, j); - if (tmp_ans == truthtable.second) { - while (!stats.empty()) - stats.pop(); - remembered_stats.clear(); - return (true); - } - if (!remembered_stats.contains(tmp_ans)) { - stats.push(tmp_ans); - remembered_stats.insert(tmp_ans); - } - } - stats.pop(); - } - return (bfs(truthtable, layer + 1)); -} - -r8veritas::path track_path(const r8veritas::truthtable &truthtable) { - r8veritas::path p; - for (auto tmp_stat = truthtable.second; tmp_stat != truthtable.first;) { - p.push(visited_op[tmp_stat]); - tmp_stat = p.top().first; - } - return p; -} \ No newline at end of file diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 9cb39e0..42f467b 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -2,6 +2,8 @@ #include #include +#define r8veritas r8veritas<6> + r8veritas::cell func_copy(r8veritas::cell inp) { bool A = inp & 0b1; return (A << 1) | A; @@ -49,15 +51,16 @@ r8veritas::cell func_half_add(r8veritas::cell inp) { } int main() { - initialize_nicknames(); - initialize_tp_paths(); - r8veritas::debug_output = true; - auto tab1 = generate_truthtable(func_full_add_Co); - auto new_tab1 = compress_stat_space(tab1); - if (bfs(new_tab1)) { - r8veritas::path p = track_path(tab1); + static r8veritas problem; + problem.initialize_nicknames(); + problem.initialize_tp_paths(); + problem.disable_debug_output(); + auto tab1 = problem.generate_truthtable(func_full_add_Co); + auto new_tab1 = problem.compress_stat_space(tab1); + if (problem.bfs(new_tab1)) { + auto p = problem.track_path(tab1); while (!p.empty()) { - std::cout << r8veritas::to_ins(p.top().second) << std::endl; + std::cout << problem.to_ins(p.top().second) << std::endl; p.pop(); } } else