From eb8051206e7f267125e6d222777102eb359436c8 Mon Sep 17 00:00:00 2001 From: StarCells Date: Wed, 29 Jul 2026 17:51:18 +0800 Subject: [PATCH] Added debug output format. --- include/r8veritas.hh | 15 +++++--- src/r8veritas.cc | 89 ++++++++++++++++++++++++++++++++------------ tests/r8_test.cc | 24 +++++------- 3 files changed, 86 insertions(+), 42 deletions(-) diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 1b77b1f..555df32 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -13,7 +13,7 @@ #include #include -#define CELL_SIZE 5 +#define CELL_SIZE 3 #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define INS_SIZE (CELL_SIZE + 1) @@ -55,9 +55,9 @@ inline r8veritas::ins to_ins(const r8veritas::ins_bytecode &bc) { using path = std::stack>; -static std::array all_influences; +inline std::array all_influences; -static const std::set all_ins = +inline const std::set all_ins = []() -> std::set { std::set s; for (r8veritas::index sacrifice_it = 0; sacrifice_it < CELL_SIZE; @@ -76,25 +76,30 @@ static const std::set all_ins = 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; } }; -// Overload hash to make some unordered_maps work. template <> struct hash> { - // Yep,magic again. 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; diff --git a/src/r8veritas.cc b/src/r8veritas.cc index a57a9e1..297570d 100644 --- a/src/r8veritas.cc +++ b/src/r8veritas.cc @@ -8,6 +8,12 @@ #include #include #include + + + + + + void initialize_nicknames() { for (r8veritas::index i = 0; i < CELL_SIZE; i++) { nicknames[i] = "%" + std::to_string(i + 1); @@ -86,34 +92,70 @@ std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) { std::ostream &operator<<(std::ostream &os, const r8veritas::ins &s) { auto str = std::bitset(s.bc).to_string(); - os << tp_paths[s.sacrifice] << std::endl - << "biz" << std::endl - << "\tflb" << std::endl; - /* - .tp xxx - biz - flb - */ + std::queue arg_list; + bool just_sacrifice = ((s.bc == 0b0) || (s.bc == 0b1)); + std::reverse(&str[0], &str[CELL_SIZE]); - if ((s.bc != 0b0) && (s.bc != 0b1)) { + if (!just_sacrifice) { for (r8veritas::index i = 0; i < str.length() - 1; i++) { if (str[i] == '1') { - os << "\t" << tp_paths[i] << std::endl; - /* - .tp yyy - */ - if (str[CELL_SIZE] == '1') - os << "\tflb" << std::endl; - else - os << "\tset_1" << std::endl; + arg_list.push(i); } } - os << "\t" << tp_paths[s.sacrifice] << std::endl; } - os << "rnz" << std::endl; - /* - rnz - */ + + 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); } @@ -152,7 +194,8 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { } r8veritas::index i = stats.size(); - // std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; + if (r8veritas::debug_output) + std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; r8veritas::stat_ref tmp_stat_ref; std::pair tmp_ans; auto operated = false; diff --git a/tests/r8_test.cc b/tests/r8_test.cc index d4c7c8b..7224dd1 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -29,11 +29,19 @@ r8veritas::cell func_full_add_S(r8veritas::cell inp) { return (S << 3) | (inp & 0b111); } +r8veritas::cell func_half_add(r8veritas::cell inp) { + bool A = inp & 0b1; + bool B = inp & 0b10; + bool S = A ^ B; + bool Co = A & B; + return (S << 1) | Co; +} + int main() { initialize_nicknames(); initialize_tp_paths(); - - auto tab1 = generate_truthtable(func_full_add_Co); + r8veritas::debug_output = true; + auto tab1 = generate_truthtable(func_half_add); auto new_tab1 = compress_stat_space(tab1); if (bfs(new_tab1)) { r8veritas::path p = track_path(tab1); @@ -42,17 +50,5 @@ int main() { p.pop(); } } - - nicknames[4] = "bit_add_buf_1"; - initialize_tp_paths(); - auto tab2 = generate_truthtable(func_full_add_S); - auto new_tab2 = compress_stat_space(tab2); - if (bfs(new_tab2)) { - r8veritas::path p = track_path(tab2); - while (!p.empty()) { - std::cout << r8veritas::to_ins(p.top().second) << std::endl; - p.pop(); - } - } return (0); }