diff --git a/include/r8veritas.hh b/include/r8veritas.hh index 8fb3177..31cfdbd 100644 --- a/include/r8veritas.hh +++ b/include/r8veritas.hh @@ -19,19 +19,14 @@ #include #define CELL_SIZE_BITWIDTH std::bit_width(unsigned(CELL_SIZE)) - #define INS_SIZE (CELL_SIZE + 1) // mask and opcode #define INS_BYTECODE_SIZE (INS_SIZE + CELL_SIZE_BITWIDTH) // mask,opcode and sacrifice -#define OPCODE_SET1 (static_cast(r8veritas::ins_opcode::SET1)) -#define OPCODE_FLIP (static_cast(r8veritas::ins_opcode::FLIP)) - template class r8veritas { public: using cell = uint8_t; - using stat = std::array; using stat_ref = const stat *; using truthtable = std::pair; @@ -39,6 +34,11 @@ public: using index = size_t; using influence = size_t; using ins_body = size_t; + /* + e.g. cell_size=3: + 0b aaa b + ^--mask ^opcode + */ using ins_bytecode = size_t; enum ins_opcode { SET1 = 0, @@ -60,8 +60,20 @@ public: private: std::array all_influences; - const std::set all_ins = []() -> std::set { - std::set s; + struct compare_ins { + bool operator()(const ins &a, const ins &b) const { + auto pop_a = std::popcount(a.bc >> 1); + auto pop_b = std::popcount(a.bc >> 1); + if (pop_a != pop_b) + return pop_a < pop_b; + if (a.bc != b.bc) + return a.bc < b.bc; + return a.sacrifice < b.sacrifice; + } + }; + 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++) { @@ -153,9 +165,9 @@ public: 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) { + if (opcode == ins_opcode::SET1) { next_stat[i] |= cell_mask; - } else if (opcode == OPCODE_FLIP) { + } else if (opcode == ins_opcode::FLIP) { next_stat[i] ^= cell_mask; } } @@ -314,4 +326,8 @@ public: } }; +#undef CELL_SIZE_BITWIDTH +#undef INS_SIZE +#undef INS_BYTECODE_SIZE + #endif \ No newline at end of file