From 33b22276fbfc845280b97687ac235c5ea728a2c3 Mon Sep 17 00:00:00 2001 From: starcells Date: Sat, 8 Aug 2026 15:23:14 +0800 Subject: [PATCH] 202608081522 --- include/r8veritas.hh | 11 +++++------ src/r8veritas.cc | 20 ++++++++++---------- tests/r8_test.cc | 22 +++++++++++++++++++--- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 3c96cd1..7db5881 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -18,9 +18,9 @@ #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) #define INS_SIZE (CELL_SIZE + 1) -// mask and bytecode +// mask and opcode #define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH) -// mask,bytecode and sacrifice +// mask,opcode and sacrifice namespace r8veritas { using cell = uint8_t; @@ -42,7 +42,7 @@ enum ins_opcode { FLIP = 1, }; -using ins = struct ins { +struct ins { r8veritas::ins_body bc; r8veritas::index sacrifice; operator const ins_bytecode() const { @@ -112,9 +112,8 @@ template <> struct hash> { #define OPCODE_FLIP (static_cast(r8veritas::ins_opcode::FLIP)) r8veritas::truthtable generate_truthtable(const r8veritas::op &operation); -std::pair -operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins); -// true means that {stat,ins} has been operated. +r8veritas::stat_ref operate_ins(const r8veritas::stat_ref &last_stat, + const r8veritas::ins &ins); inline std::unordered_map< std::pair, diff --git a/src/r8veritas.cc b/src/r8veritas.cc index 60fbeda..52fc17d 100644 --- a/src/r8veritas.cc +++ b/src/r8veritas.cc @@ -49,13 +49,13 @@ r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) { save_stat(std::move(out)))); } -std::pair // true -> the stat has been visited. -operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) { +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; auto it = visited_stat.find(key); if (it != visited_stat.end()) { // found in visited_stat - return {it->second, true}; + return it->second; } int opcode = ins.bc & 1; @@ -74,7 +74,7 @@ operate_ins(const r8veritas::stat_ref &last_stat, const r8veritas::ins &ins) { 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))); - return {tmp_next_stat_ref, false}; + return tmp_next_stat_ref; } std::ostream &operator<<(std::ostream &os, const r8veritas::stat_ref &s) { @@ -176,7 +176,7 @@ 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}); } } return (new_truthtable); @@ -198,21 +198,21 @@ bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer) { if (r8veritas::debug_output) std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl; r8veritas::stat_ref tmp_stat_ref; - std::pair tmp_ans; + 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.first == truthtable.second) { + if (tmp_ans == truthtable.second) { while (!stats.empty()) stats.pop(); remembered_stats.clear(); return (true); } - if (!remembered_stats.contains(tmp_ans.first)) { - stats.push(tmp_ans.first); - remembered_stats.insert(tmp_ans.first); + if (!remembered_stats.contains(tmp_ans)) { + stats.push(tmp_ans); + remembered_stats.insert(tmp_ans); } } stats.pop(); diff --git a/tests/r8_test.cc b/tests/r8_test.cc index e9a7d01..bf2dd50 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -7,6 +7,13 @@ r8veritas::cell func_copy(r8veritas::cell inp) { return (A << 1) | A; } +r8veritas::cell func_clb_and(r8veritas::cell inp) { + bool A = inp & 0b1; + bool B = inp & 0b10; + bool S = A & B; + return S << 2; +} + r8veritas::cell func_full_add_Co(r8veritas::cell inp) { bool A = inp & 0b1; bool B = inp & 0b10; @@ -25,7 +32,7 @@ r8veritas::cell func_full_add_S(r8veritas::cell inp) { return (S << 3) | (inp & 0b111); } -r8veritas::cell func_half_add(r8veritas::cell inp) { +r8veritas::cell func_clb_half_add(r8veritas::cell inp) { bool A = inp & 0b1; bool B = inp & 0b10; bool S = A ^ B; @@ -33,11 +40,19 @@ r8veritas::cell func_half_add(r8veritas::cell inp) { return (S << 1) | Co; } +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 << 3) | (Co << 2) | (inp & 0b11); +} + int main() { initialize_nicknames(); initialize_tp_paths(); r8veritas::debug_output = true; - auto tab1 = generate_truthtable(func_full_add_Co); + auto tab1 = generate_truthtable(func_full_add_S); auto new_tab1 = compress_stat_space(tab1); if (bfs(new_tab1)) { r8veritas::path p = track_path(tab1); @@ -45,6 +60,7 @@ int main() { std::cout << r8veritas::to_ins(p.top().second) << std::endl; p.pop(); } - } + } else + std::cout << "PATH NOT FOUND!"; return (0); }