[err]202607242043
This commit is contained in:
1 parent
d87d69686b
commit
a46246810c
3 files changed
+125
-24
No files matched your search
+31
-8
@@ -1,10 +1,13 @@
|
||||
#ifndef R8VERITAS
|
||||
#define R8VERITAS
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <ostream>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
@@ -23,26 +26,34 @@ using stat = std::array<r8veritas::cell, (1 << CELL_SIZE)>;
|
||||
using truthtable = std::pair<r8veritas::stat, r8veritas::stat>;
|
||||
using op = std::function<r8veritas::cell(r8veritas::cell)>;
|
||||
using index = size_t;
|
||||
using influence = size_t;
|
||||
using ins_body = size_t;
|
||||
using ins_bytecode = size_t;
|
||||
/*
|
||||
using ins_body = std::bitset<INS_SIZE>;
|
||||
using ins_bytecode = std::bitset<INS_BYTECODE_SIZE>;
|
||||
*/
|
||||
|
||||
enum ins_opcode {
|
||||
SET1 = 0,
|
||||
FLIP = 1,
|
||||
};
|
||||
|
||||
/*
|
||||
using ins_body = std::bitset<INS_SIZE>;
|
||||
using ins_bytecode = std::bitset<INS_BYTECODE_SIZE>;
|
||||
*/
|
||||
using ins_body = size_t;
|
||||
using ins_bytecode = size_t;
|
||||
using ins = struct ins {
|
||||
r8veritas::ins_body bc;
|
||||
r8veritas::index sacrifice;
|
||||
operator const ins_bytecode() const {
|
||||
return (bc << CELL_SIZE_BITWIDTH) | sacrifice;
|
||||
}
|
||||
r8veritas::ins operator()(const r8veritas::ins_bytecode &bc) {
|
||||
return {bc >> CELL_SIZE_BITWIDTH, bc & ((1 << CELL_SIZE_BITWIDTH) - 1)};
|
||||
}
|
||||
};
|
||||
|
||||
using path = std::stack<std::pair<r8veritas::stat, r8veritas::ins_bytecode>>;
|
||||
|
||||
static std::array<r8veritas::influence, CELL_SIZE> all_influences;
|
||||
|
||||
static const std::set<r8veritas::ins> all_ins =
|
||||
[]() -> std::set<r8veritas::ins> {
|
||||
std::set<r8veritas::ins> s;
|
||||
@@ -73,6 +84,7 @@ inline r8veritas::stat generate_empty_stat() {
|
||||
}
|
||||
|
||||
namespace std {
|
||||
|
||||
// Overload hash to make some unordered_maps work.
|
||||
template <> struct hash<r8veritas::stat> {
|
||||
// Magic,from Boost's hash_combine.
|
||||
@@ -102,8 +114,9 @@ template <> struct hash<pair<r8veritas::stat, r8veritas::ins_bytecode>> {
|
||||
#define OPCODE_FLIP (static_cast<int>(r8veritas::ins_opcode::FLIP))
|
||||
|
||||
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation);
|
||||
r8veritas::stat operate_ins(const r8veritas::stat &last_stat,
|
||||
const r8veritas::ins &ins);
|
||||
std::pair<r8veritas::stat, bool> operate_ins(const r8veritas::stat &last_stat,
|
||||
const r8veritas::ins &ins);
|
||||
// true means that {stat,ins} has been operated.
|
||||
|
||||
static std::unordered_map<std::pair<r8veritas::stat, r8veritas::ins_bytecode>,
|
||||
r8veritas::stat>
|
||||
@@ -113,4 +126,14 @@ static std::unordered_map<r8veritas::stat,
|
||||
std::pair<r8veritas::stat, r8veritas::ins_bytecode>>
|
||||
visited_op;
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s);
|
||||
|
||||
r8veritas::truthtable
|
||||
compress_stat_space(const r8veritas::truthtable &raw_truthtable);
|
||||
// raw means input must be 0 ~ (1<<CELL_SIZE),
|
||||
// which makes reaching the output of after-xor input easier.
|
||||
|
||||
bool bfs(const r8veritas::truthtable &truthtable, r8veritas::index layer = 1);
|
||||
r8veritas::path track_path(const r8veritas::truthtable &truthtable);
|
||||
|
||||
#endif
|
||||
+77
-11
@@ -1,6 +1,8 @@
|
||||
#include "r8veritas.hh"
|
||||
#include <functional>
|
||||
#include <array>
|
||||
#include <bitset>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
|
||||
r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) {
|
||||
@@ -13,18 +15,13 @@ r8veritas::truthtable generate_truthtable(const r8veritas::op &operation) {
|
||||
return (r8veritas::truthtable(inp, out));
|
||||
}
|
||||
|
||||
r8veritas::stat operate_ins(const r8veritas::stat &last_stat,
|
||||
const r8veritas::ins &ins) {
|
||||
std::pair<r8veritas::stat, bool> operate_ins(const r8veritas::stat &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()) { // foud <last_stat,ins> in visited_stat
|
||||
// DEBUG
|
||||
std::cout
|
||||
<< "Visited:"
|
||||
<< std::hash<std::pair<r8veritas::stat, r8veritas::ins_bytecode>>{}(key)
|
||||
<< std::endl;
|
||||
return (it->second);
|
||||
return {it->second, true};
|
||||
}
|
||||
int opcode = ins.bc & 1;
|
||||
|
||||
@@ -42,6 +39,75 @@ r8veritas::stat operate_ins(const r8veritas::stat &last_stat,
|
||||
}
|
||||
visited_stat.insert_or_assign(key, next_stat);
|
||||
visited_op.insert_or_assign(next_stat, std::move(key));
|
||||
|
||||
return (next_stat);
|
||||
return {next_stat, false};
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const r8veritas::stat &s) {
|
||||
for (auto i : s) {
|
||||
os << std::bitset<CELL_SIZE>(i).to_string() << " ";
|
||||
}
|
||||
return (os);
|
||||
}
|
||||
|
||||
r8veritas::truthtable
|
||||
compress_stat_space(const r8veritas::truthtable &raw_truthtable) {
|
||||
std::array<r8veritas::influence, CELL_SIZE> 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)]) {
|
||||
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}).first;
|
||||
}
|
||||
}
|
||||
return (new_truthtable);
|
||||
}
|
||||
|
||||
static std::queue<r8veritas::stat> 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();
|
||||
std::cout << "LAYER=" << layer << ",stats_size=" << i << std::endl;
|
||||
r8veritas::stat tmp_stat;
|
||||
std::pair<r8veritas::stat, bool> tmp_ans;
|
||||
auto operated = false;
|
||||
for (; i > 0; i--) {
|
||||
tmp_stat = stats.front();
|
||||
for (auto j : r8veritas::all_ins) {
|
||||
tmp_ans = operate_ins(tmp_stat, j);
|
||||
if (tmp_ans.first == truthtable.second) {
|
||||
return (true);
|
||||
}
|
||||
if (!(tmp_ans.second)) {
|
||||
stats.push(tmp_ans.first);
|
||||
}
|
||||
}
|
||||
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;
|
||||
std::cout << tmp_stat << ";" << p.top().second << std::endl;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
+17
-5
@@ -1,17 +1,29 @@
|
||||
#include "r8veritas.hh"
|
||||
#include <bitset>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
r8veritas::cell func_and(r8veritas::cell inp) {
|
||||
return ((((inp >> 0) & (inp >> 1)) & r8veritas::cell(0b1)) << 2) |
|
||||
(inp & 0b11); // inp[0]->arg1, inp[1]->arg2, inp[2]->ans
|
||||
}
|
||||
|
||||
r8veritas::cell func_copy(r8veritas::cell inp) {
|
||||
return (((inp & 1) << 1) | (inp & 1)); // inp[0] -> from,inp[1]->to
|
||||
}
|
||||
|
||||
r8veritas::cell func_full_add(r8veritas::cell inp) {
|
||||
bool A = inp & 0b1;
|
||||
bool B = inp & 0b10;
|
||||
bool C = inp & 0b100;
|
||||
bool S = A ^ B ^ C;
|
||||
bool Ci = (A & B) | (A & C) | (B & C);
|
||||
return (Ci << 4 | S << 3) | (inp & 0b111);
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto tab = generate_truthtable(func_and);
|
||||
for (auto i : r8veritas::all_ins) {
|
||||
std::cout << std::bitset<INS_SIZE>(i.bc).to_string() << "," << i.sacrifice
|
||||
<< ";";
|
||||
}
|
||||
tab = compress_stat_space(tab);
|
||||
std::cout << bfs(tab) << std::endl;
|
||||
r8veritas::path p = track_path(tab);
|
||||
return (0);
|
||||
}
|
||||
Reference in new issue
Block a user