202608081522

This commit is contained in:
starcells committed 2026-08-08 15:23:14 +08:00
1 parent a75704a4c0
commit 33b22276fb
3 files changed
+34 -19

No files matched your search

+5 -6
View File
@@ -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<pair<r8veritas::stat_ref, r8veritas::ins_bytecode>> {
#define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP))
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation);
std::pair<r8veritas::stat_ref, bool>
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<r8veritas::stat_ref, r8veritas::ins_bytecode>,
+10 -10
View File
@@ -49,13 +49,13 @@ r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) {
save_stat(std::move(out))));
}
std::pair<r8veritas::stat_ref, bool> // 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 <last_stat,ins> 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<r8veritas::stat_ref, bool> 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();
+19 -3
View File
@@ -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);
}