Rename per request: CELL_SIZE is the machine width, CELL_ARGC the parameter count

r8asm::ast::CELL_SIZE        machine word width in bits, run time value
                               (import_file's second argument, 8 in the CLI)
  r8veritas<CELL_ARGC>         how many parameter cells a synth block has,
                               compile time, selected by 'params N'

set_cell_bits went back to set_cell_size for the same reason, and the .r8asm
visible names (_CELL_SIZE, _CELL_MAX, _ROL_STEP) are unchanged, so lib.r8asm
keeps working as written.

This time the string literal used by %rep and by the .xor masking is the one
the C++ member is now called too, which removes the trap the previous rename
walked into.

Regressions after the rename:
  asm/test.r8asm                  exit 0, 1666 characters, 7/7 cached
  examples/full_add_layers.r8asm  eight combinations ok
  derive bit_raw_add              same table as the hand written version
  clang-cl build of all five targets, and the clang built binaries produce the
  same 1666 character tape and the same table.
This commit is contained in:
dsh committed 2026-11-06 20:27:49 +08:00
1 parent 7d259b1c6c
commit e5d87791e0
3 files changed
+49 -49

No files matched your search

+3 -3
View File
@@ -103,8 +103,8 @@ private:
class ast {
public:
ast_node root = {r8asm::ast_node_t::CODEBLOCK, "", {}};
size_t CELL_BITS;
// I know template class is the most elegant solution of CELL_BITS
size_t CELL_SIZE;
// I know template class is the most elegant solution of CELL_SIZE
// but I can't be bother to add that.
// The assembler has no exceptions: anything that cannot be assembled
@@ -199,7 +199,7 @@ private:
ast_node expand_node_to_ins(const ast_node &node);
std::string expand_root();
bool import_macro(const std::string &macro_name, const ast_node &node);
void set_cell_bits(size_t cell_bits);
void set_cell_size(size_t cell_size);
};
ast main_tree;
+34 -34
View File
@@ -23,7 +23,7 @@
// Search strategy:
// * only the input bits that influence the answer survive (the old
// compress_stat_space), so a plane is a 2^K bit truth table instead of
// 2^PARAMS, and the whole state is PARAMS * 2^K bits
// 2^CELL_ARGC, and the whole state is CELL_ARGC * 2^K bits
// * forward BFS keeps every reachable state of depth < d plus the block
// that produced it
// * the last block is not searched forward. A block clears its own
@@ -56,10 +56,10 @@
#include <utility>
#include <vector>
template <int PARAMS> class r8veritas {
template <int CELL_ARGC> class r8veritas {
public:
using cell = uint8_t;
using stat = std::array<cell, (size_t(1) << PARAMS)>;
using stat = std::array<cell, (size_t(1) << CELL_ARGC)>;
using stat_ref = const stat *;
using truthtable = std::pair<stat_ref, stat_ref>;
using op = std::function<cell(cell)>;
@@ -85,7 +85,7 @@ public:
// set_1. The inter-block hop from the previous sacrifice back to this one
// is not in the model -- the emitter charges it afterwards.
struct cost_options {
std::array<int, PARAMS> addr{}; // cell address of each plane
std::array<int, CELL_ARGC> addr{}; // cell address of each plane
int max_cost = 4096; // give up above this tape length
size_t max_states = 600'000'000;
unsigned threads = 0; // 0 = one worker per hardware thread
@@ -113,16 +113,16 @@ public:
std::string message;
};
std::array<std::string, PARAMS> nicknames;
std::array<std::string, PARAMS> tp_paths;
std::array<std::string, CELL_ARGC> nicknames;
std::array<std::string, CELL_ARGC> tp_paths;
void initialize_nicknames() {
for (int i = 0; i < PARAMS; i++)
for (int i = 0; i < CELL_ARGC; i++)
nicknames[i] = "%" + std::to_string(i + 1);
}
void initialize_tp_paths() {
for (int i = 0; i < PARAMS; i++)
for (int i = 0; i < CELL_ARGC; i++)
tp_paths[i] = ".tp " + nicknames[i];
}
@@ -135,7 +135,7 @@ public:
truthtable generate_truthtable(const op &operation) {
stat in{}, out{};
for (size_t i = 0; i < (size_t(1) << PARAMS); i++) {
for (size_t i = 0; i < (size_t(1) << CELL_ARGC); i++) {
in[i] = cell(i);
out[i] = operation(cell(i));
}
@@ -152,13 +152,13 @@ public:
// starts from a canonical state.
truthtable compress_stat_space(const truthtable &raw) {
stat start = *raw.first;
for (int i = 0; i < PARAMS; i++) {
for (int i = 0; i < CELL_ARGC; i++) {
bool influences = false;
for (size_t j = 0; j < (size_t(1) << PARAMS); j++)
for (size_t j = 0; j < (size_t(1) << CELL_ARGC); j++)
if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)])
influences = true;
if (!influences)
for (size_t j = 0; j < (size_t(1) << PARAMS); j++)
for (size_t j = 0; j < (size_t(1) << CELL_ARGC); j++)
start[j] = cell(start[j] & ~(1u << i));
}
return {keep(start), raw.second};
@@ -173,7 +173,7 @@ public:
for (size_t j = 0; j < cur.size(); j++)
if (cur[j] & (1u << i.sacrifice)) {
cur[j] = cell(cur[j] & ~(1u << i.sacrifice));
for (int b = 0; b < PARAMS; b++)
for (int b = 0; b < CELL_ARGC; b++)
if (i.mask & (1 << b))
cur[j] = i.or_mode ? cell(cur[j] | (1u << b))
: cell(cur[j] ^ (1u << b));
@@ -189,7 +189,7 @@ public:
// ---------------------------------------------------------------- output
void output_stat(const stat &s) const {
for (auto v : s)
std::cout << std::bitset<PARAMS>(v).to_string() << " ";
std::cout << std::bitset<CELL_ARGC>(v).to_string() << " ";
}
void output_ins(const instruction &ins, std::ostream &os,
@@ -199,7 +199,7 @@ public:
if (ins.mask) {
os << (ins.or_mode ? ", SET1: {" : ", FLIP: {");
bool first = true;
for (int b = 0; b < PARAMS; b++)
for (int b = 0; b < CELL_ARGC; b++)
if (ins.mask & (1 << b)) {
if (!first)
os << ',';
@@ -213,7 +213,7 @@ public:
}
os << tp_paths[ins.sacrifice] << "\n";
os << "biz\n";
for (int b = 0; b < PARAMS; b++)
for (int b = 0; b < CELL_ARGC; b++)
if (ins.mask & (1 << b)) {
os << '\t' << tp_paths[b] << "\n";
os << '\t' << (ins.or_mode ? "set_1" : "flb") << "\n";
@@ -242,10 +242,10 @@ public:
return r;
};
std::array<int, PARAMS> bit_of{};
std::array<int, CELL_ARGC> bit_of{};
int k = 0;
for (int i = 0; i < PARAMS; i++)
for (size_t j = 0; j < (size_t(1) << PARAMS); j++)
for (int i = 0; i < CELL_ARGC; i++)
for (size_t j = 0; j < (size_t(1) << CELL_ARGC); j++)
if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) {
bit_of[k++] = i;
break;
@@ -289,10 +289,10 @@ public:
};
// which input bits matter, in compression order
std::array<int, PARAMS> bit_of{};
std::array<int, CELL_ARGC> bit_of{};
int k = 0;
for (int i = 0; i < PARAMS; i++) {
for (size_t j = 0; j < (size_t(1) << PARAMS); j++)
for (int i = 0; i < CELL_ARGC; i++) {
for (size_t j = 0; j < (size_t(1) << CELL_ARGC); j++)
if ((*raw.second)[j] != (*raw.second)[j ^ (size_t(1) << i)]) {
bit_of[k++] = i;
break;
@@ -336,10 +336,10 @@ private:
// ---------------------------------------------------------------- engine
// K relevant inputs: every plane is a 2^K bit truth table, the whole state
// is PARAMS * 2^K bits packed into KEYW words.
// is CELL_ARGC * 2^K bits packed into KEYW words.
template <int K> struct engine {
static constexpr int M = 1 << K; // bits per plane
static constexpr int KEYW = (PARAMS * M + 63) / 64;
static constexpr int KEYW = (CELL_ARGC * M + 63) / 64;
static constexpr uint64_t PMASK =
(M >= 64) ? ~uint64_t(0) : ((uint64_t(1) << (M & 63)) - 1);
using key = std::array<uint64_t, KEYW>;
@@ -364,7 +364,7 @@ private:
key n = k;
const uint64_t c = get(k, i.sacrifice);
put(n, i.sacrifice, 0);
for (int b = 0; b < PARAMS; b++)
for (int b = 0; b < CELL_ARGC; b++)
if (i.mask & (1 << b))
put(n, b, i.or_mode ? (get(k, b) | c) : (get(k, b) ^ c));
return n;
@@ -456,7 +456,7 @@ private:
for (size_t i = 0; i < insns.size(); i++) {
const instruction &in = insns[i];
int lo = opt.addr[in.sacrifice], hi = lo, cnt = 0;
for (int b = 0; b < PARAMS; b++)
for (int b = 0; b < CELL_ARGC; b++)
if (in.mask & (1 << b)) {
cnt++;
lo = std::min(lo, opt.addr[b]);
@@ -470,7 +470,7 @@ private:
// start = identity over the compressed inputs, goal = the target
key start = E::zero(), goal = E::zero();
for (size_t j = 0; j < full.size(); j++)
for (int b = 0; b < PARAMS; b++) {
for (int b = 0; b < CELL_ARGC; b++) {
if (j & (size_t(1) << b))
E::put(start, b, E::get(start, b) | (uint64_t(1) << j));
if ((*raw.second)[full[j]] & (1u << b))
@@ -675,7 +675,7 @@ private:
// start = identity over the compressed inputs, goal = the target
key start = E::zero(), goal = E::zero();
for (size_t j = 0; j < full.size(); j++)
for (int b = 0; b < PARAMS; b++) {
for (int b = 0; b < CELL_ARGC; b++) {
if (j & (size_t(1) << b))
E::put(start, b, E::get(start, b) | (uint64_t(1) << j));
if ((*raw.second)[full[j]] & (1u << b))
@@ -794,10 +794,10 @@ private:
const instruction &ins, uint64_t c,
typename E::key pre, int b, F &f) {
using key = typename E::key;
for (; b < PARAMS; b++)
for (; b < CELL_ARGC; b++)
if (ins.mask & (1 << b))
break;
if (b == PARAMS)
if (b == CELL_ARGC)
return f(pre);
const uint64_t T = E::get(post, b);
@@ -821,13 +821,13 @@ private:
// ---------------------------------------------------------------- syntax
std::vector<instruction> build_instructions() const {
std::vector<instruction> out;
out.reserve(PARAMS * (2 * (1 << (PARAMS - 1)) + 1));
for (int s = 0; s < PARAMS; s++) {
out.reserve(CELL_ARGC * (2 * (1 << (CELL_ARGC - 1)) + 1));
for (int s = 0; s < CELL_ARGC; s++) {
out.push_back({uint8_t(s), 0, false}); // pure sacrifice: clear plane s
for (int sub = 1; sub < (1 << (PARAMS - 1)); sub++) {
for (int sub = 1; sub < (1 << (CELL_ARGC - 1)); sub++) {
uint8_t m = 0;
int bit = 0;
for (int b = 0; b < PARAMS; b++)
for (int b = 0; b < CELL_ARGC; b++)
if (b != s)
m |= uint8_t(((sub >> bit++) & 1) << b);
out.push_back({uint8_t(s), m, false});
+12 -12
View File
@@ -64,13 +64,13 @@ std::string peel(std::string &str) { // Peel the first word from string.
#undef EMPTY_CHARS
#undef OPERATORS
void r8asm::ast::set_cell_bits(size_t cell_bits) {
CELL_BITS = cell_bits;
void r8asm::ast::set_cell_size(size_t cell_size) {
CELL_SIZE = cell_size;
// the names a .r8asm program sees stay as they were: _CELL_SIZE is the
// machine word width in bits (CELL_BITS on the C++ side)
scopes = {{{"_CELL_SIZE", static_cast<general_data>(cell_bits)},
{"_CELL_MAX", static_cast<general_data>((1 << cell_bits) - 1)},
{"_ROL_STEP", static_cast<general_data>(cell_bits - 1)}}};
// machine word width in bits (CELL_SIZE on the C++ side)
scopes = {{{"_CELL_SIZE", static_cast<general_data>(cell_size)},
{"_CELL_MAX", static_cast<general_data>((1 << cell_size) - 1)},
{"_ROL_STEP", static_cast<general_data>(cell_size - 1)}}};
}
r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) {
@@ -259,10 +259,10 @@ r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
/*
XOR
children[0]: argument
WARNING: argument bigger than ((1<<CELL_BITS)-1) will be cut off
WARNING: argument bigger than ((1<<CELL_SIZE)-1) will be cut off
e.g.
children[0] -> 5(0b101)
when define CELL_BITS 2, only the last 2 bit will be noticed,
when define CELL_SIZE 2, only the last 2 bit will be noticed,
and 5 means 0b01(1) here.
then the node will be expanded like this:
@@ -438,7 +438,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
filename += tp->value;
// now tp points at RQUOTE
import_file(filename, main_tree.CELL_BITS);
import_file(filename, main_tree.CELL_SIZE);
// import_file has already said why if it failed; the error flag is
// set, so this file will not be reported as assembled.
} break;
@@ -552,7 +552,7 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) {
}
file << "%endmacro\n";
file.close();
import_file(path, main_tree.CELL_BITS);
import_file(path, main_tree.CELL_SIZE);
std::remove(path.c_str());
}
} break;
@@ -857,10 +857,10 @@ bool r8asm::import_file(const std::string &filename, size_t cell_size) {
return (false);
}
// Before parsing: %include asks main_tree.CELL_BITS for the width to hand
// Before parsing: %include asks main_tree.CELL_SIZE for the width to hand
// to the included file, and that used to answer 0 because the size was
// only set at the end of this function.
main_tree.set_cell_bits(cell_size);
main_tree.set_cell_size(cell_size);
for (const auto &token_it : parse(stream).children) {
main_tree.root.children.push_back(token_it);