32 bit cells: widen the VMs to uint32_t, fix the mask at 32, make width a build option

All five R8Forge machines stored a cell in uint16_t, so anything above 16 bits
was silently truncated even though geometry.hh (and the viz) allow 1..32; and
`(1 << cell_size) - 1` for _CELL_MAX was undefined at 32.

  * cells and out/input buffers are uint32_t in derive, tape_run, byte_op_test,
    examples_test and synth_test (r8veritas*.hh keep their uint16_t search
    state -- that is node ids, unrelated)
  * kDefaultCellMask and the injected _CELL_MAX special-case 32 instead of
    shifting by 32
  * width is now a build option: -DR8FORGE_CELL_SIZE=<1..32> (CMake cache var,
    added as a compile definition), so a whole toolchain of another width is one
    configure away; build*/ is ignored so those trees do not leak into git

Verified: 11 bits unchanged (tape 64513, byte_op ok); a 16 bit build passes
synth_test 8/8 and byte_op_test (65536 exhaustive pairs); a 32 bit build passes
synth_test, verify_clb_bit_add_nc and assembles full_add to the same 95 chars.
Exhaustive 32 bit is 2^64 pairs -- that stays sampled, not verified.

SPEC 13 checklist now says five VMs / uint32_t / the 32 bit mask case.
This commit is contained in:
dsh committed 2026-11-10 18:23:26 +08:00
1 parent 0d08fdc838
commit 90feedb37b
10 files changed
+81 -70

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
build/
build*/
.cache/
.continue/
.vscode/
+5
View File
@@ -6,6 +6,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(include)
# The width of a cell. 11 is the project default; the tests and the VMs follow
# it, so `-DR8FORGE_CELL_SIZE=16` builds an entirely 16 bit toolchain.
set(R8FORGE_CELL_SIZE 11 CACHE STRING "bits per cell (1..32)")
add_compile_definitions(R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE})
find_package(Threads REQUIRED)
add_library(r8forge_core src/r8asm.cc src/synth.cc)
+1 -1
View File
@@ -37,7 +37,7 @@ inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
```
汇编器把三个名字注入 `.r8asm` 程序的作用域,宏库靠它们与位宽解耦:
位宽可以用构建选项换:`cmake -S . -B build16 -DR8FORGE_CELL_SIZE=16` 会造出一整套 16 位的工具链(汇编器、库、五份 VM 都跟着走);实测 11 / 16 / 32 位下 `r8forge_synth_test` 都是 8/8 通过、`byte_op_test` 在 16 位下也全过(32 位的穷举是 2^64 组,跑不了)。\n\n汇编器把三个名字注入 `.r8asm` 程序的作用域,宏库靠它们与位宽解耦:
| 名字 | 值(11 位) | 用途 |
|---|---|---|
+4 -1
View File
@@ -9,4 +9,7 @@
// these are the only places the old 8 bit / 256 cell assumptions live.
inline constexpr size_t kDefaultCellSize = 11;
inline constexpr size_t kDefaultMemoryCells = 259;
inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
// The VMs store a cell in uint32_t, so 32 bits is the ceiling -- and `1u << 32`
// would be undefined, hence the special case instead of a plain shift.
inline constexpr uint32_t kDefaultCellMask =
kDefaultCellSize >= 32 ? 0xFFFFFFFFu : (1u << kDefaultCellSize) - 1u;
+12 -12
View File
@@ -19,15 +19,15 @@ namespace {
// Characters handed to INP (','); -i/--input fills it once per process, which is
// what a scripted run of an interactive program needs. The live terminal case
// lives in ROT-8-viz, not here.
std::deque<uint16_t> g_input;
std::deque<uint32_t> g_input;
struct machine {
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
std::vector<uint32_t> cells = std::vector<uint32_t>(kDefaultMemoryCells, 0);
std::vector<uint32_t> out;
size_t dp = 0;
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
static uint32_t ror(uint32_t v) {
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit = 5'000'000) {
@@ -121,7 +121,7 @@ int main(int argc, char **argv) {
return 2;
}
for (const char *p = argv[++i]; *p; p++)
g_input.push_back(uint16_t((unsigned char)*p & 0x7f));
g_input.push_back(uint32_t((unsigned char)*p & 0x7f));
continue;
}
argv[++keep] = argv[i];
@@ -141,7 +141,7 @@ int main(int argc, char **argv) {
machine m;
m.run(tape);
std::cout << "out";
for (uint16_t v : m.out)
for (uint32_t v : m.out)
std::cout << " " << int(v);
std::cout << "\n";
return 0;
@@ -159,11 +159,11 @@ int main(int argc, char **argv) {
continue;
}
const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str());
m.cells[size_t(at)] = uint16_t(std::atoi(eq + 1) & kDefaultCellMask);
m.cells[size_t(at)] = uint32_t(std::atoi(eq + 1) & kDefaultCellMask);
}
m.run(tape);
std::cout << "out";
for (uint16_t v : m.out) {
for (uint32_t v : m.out) {
std::cout << " " << int(v);
}
std::cout << "\ncells";
@@ -188,11 +188,11 @@ int main(int argc, char **argv) {
continue;
}
const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str());
m.cells[size_t(at)] = uint16_t(std::atoi(eq + 1) & kDefaultCellMask);
m.cells[size_t(at)] = uint32_t(std::atoi(eq + 1) & kDefaultCellMask);
}
m.run(tape);
std::cout << "out";
for (uint16_t v : m.out) {
for (uint32_t v : m.out) {
std::cout << " " << int(v);
}
std::cout << "\n";
@@ -216,7 +216,7 @@ int main(int argc, char **argv) {
for (int combo = 0; combo < (1 << params); combo++) {
machine m;
for (int b = 0; b < params; b++)
m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] = uint16_t((combo >> b) & 1);
m.cells[static_cast<size_t>(addr[static_cast<size_t>(b)])] = uint32_t((combo >> b) & 1);
m.run(tape);
unsigned val = 0;
for (int b = 0; b < params; b++)
+4 -1
View File
@@ -69,7 +69,10 @@ void r8asm::ast::set_cell_size(size_t cell_size) {
// the names a .r8asm program sees stay as they were: _CELL_SIZE is the
// 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)},
// 32-bit cells are the ceiling (the VMs store a cell in uint32_t), and
// `1 << 32` is undefined -- spell the largest width out.
{"_CELL_MAX", static_cast<general_data>(cell_size >= 32 ? 0xFFFFFFFFu
: (1u << cell_size) - 1u)},
{"_ROL_STEP", static_cast<general_data>(cell_size - 1)}}};
}
+33 -33
View File
@@ -26,14 +26,14 @@ constexpr int kCellMod = 1 << kCellSize;
constexpr int kCellMask = int(kDefaultCellMask);
struct machine {
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
std::vector<uint32_t> cells = std::vector<uint32_t>(kDefaultMemoryCells, 0);
std::vector<uint32_t> out;
size_t dp = 0;
size_t steps = 0;
bool finished = true; // false: the step limit ran out
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
static uint32_t ror(uint32_t v) {
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit) {
@@ -112,7 +112,7 @@ void report(const std::string &name, int a, int b, const machine &m,
const std::string &want) {
if (failures++ < 5) {
std::cerr << " WRONG " << name << " a=" << a << " b=" << b << " out";
for (uint16_t v : m.out)
for (uint32_t v : m.out)
std::cerr << " " << int(v);
std::cerr << " want " << want << " finished=" << m.finished
<< " steps=" << m.steps << "\n";
@@ -127,8 +127,8 @@ int test_bitop(const std::string &file, const char *name, int op) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[2] = uint16_t(b); // B
m.cells[0] = uint32_t(a); // A
m.cells[2] = uint32_t(b); // B
m.run(tape, 100'000);
const int want = op == 0 ? (a & b) : op == 1 ? (a | b) : (a ^ b);
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == a &&
@@ -154,8 +154,8 @@ int test_add(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint16_t(a);
m.cells[2] = uint16_t(b);
m.cells[0] = uint32_t(a);
m.cells[2] = uint32_t(b);
m.run(tape, 100'000);
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 5 && m.out[0] == a &&
@@ -183,8 +183,8 @@ int test_inc(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int ci = 0; ci < 2; ci++) {
machine m;
m.cells[0] = uint16_t(a);
m.cells[2] = uint16_t(ci);
m.cells[0] = uint32_t(a);
m.cells[2] = uint32_t(ci);
m.run(tape, 100'000);
const int sum = a + ci;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == a &&
@@ -213,8 +213,8 @@ int test_add_nc(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[2] = uint16_t(b); // B
m.cells[0] = uint32_t(a); // A
m.cells[2] = uint32_t(b); // B
m.run(tape, 100'000);
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 &&
@@ -240,7 +240,7 @@ int test_inc_nc(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[0] = uint32_t(a); // A
m.run(tape, 100'000);
const int sum = a + 1;
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
@@ -265,7 +265,7 @@ int test_neg_nc(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[0] = uint32_t(a); // A
m.run(tape, 100'000);
const int neg = (kCellMod - a) & kCellMask;
const int carry = a == 0 ? 1 : 0; // -0 is 0 but it does carry out
@@ -291,8 +291,8 @@ int test_sub_nc(const std::string &file) {
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[2] = uint16_t(b); // B
m.cells[0] = uint32_t(a); // A
m.cells[2] = uint32_t(b); // B
m.run(tape, 100'000);
const int diff = (a - b) & kDefaultCellMask;
const int no_borrow = a >= b ? 1 : 0;
@@ -318,7 +318,7 @@ int test_not_nc(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[0] = uint32_t(a); // A
m.run(tape, 100'000);
const int inv = (~a) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
@@ -341,7 +341,7 @@ int test_not_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[0] = uint32_t(a); // A
m.run(tape, 100'000);
const int inv = (~a) & kDefaultCellMask;
const bool ok =
@@ -364,7 +364,7 @@ int test_inc_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[0] = uint32_t(a); // A
m.run(tape, 100'000);
const int want = (a + 1) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want &&
@@ -387,7 +387,7 @@ int test_neg_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[0] = uint32_t(a); // A
m.run(tape, 100'000);
const int want = (kCellMod - a) & kCellMask;
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want;
@@ -409,7 +409,7 @@ int test_dec_ip(const std::string &file) {
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
m.cells[0] = uint16_t(a);
m.cells[0] = uint32_t(a);
m.run(tape, 100'000);
const int want = (a - 1) & kDefaultCellMask;
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == want;
@@ -431,7 +431,7 @@ int test_nonzero(const std::string &file) {
int bad = 0;
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint16_t(b); // B
m.cells[0] = uint32_t(b); // B
m.run(tape, 100'000);
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == b &&
m.out[1] == (b != 0 ? 1 : 0);
@@ -455,7 +455,7 @@ int test_ones(const std::string &file) {
int bad = 0, n = 0;
for (int a = 0; a <= kCellSize; a++) {
machine m;
m.cells[0] = uint16_t(a); // A, the count
m.cells[0] = uint32_t(a); // A, the count
m.run(tape, 5'000'000);
n++;
const int want = (1 << a) - 1;
@@ -487,8 +487,8 @@ int test_divmod(const std::string &file) {
for (int d : divs)
for (int a = 0; a < kCellMod; a++) {
machine m;
m.cells[0] = uint16_t(a); // N
m.cells[1] = uint16_t(d); // D
m.cells[0] = uint32_t(a); // N
m.cells[1] = uint32_t(d); // D
m.run(tape, 5'000'000);
n++;
if (m.steps > worst) worst = m.steps;
@@ -517,7 +517,7 @@ int test_digits(const std::string &file) {
size_t worst = 0;
for (int v = 0; v < kCellMod; v++) {
machine m;
m.cells[0] = uint16_t(v);
m.cells[0] = uint32_t(v);
m.run(tape, 5'000'000);
n++;
if (m.steps > worst) worst = m.steps;
@@ -525,7 +525,7 @@ int test_digits(const std::string &file) {
while (want.size() < 4) want.insert(want.begin(), '0');
bool ok = m.finished && m.out.size() == 4;
for (int i = 0; ok && i < 4; i++) {
ok = m.out[size_t(i)] == (uint16_t)(unsigned char)want[size_t(i)];
ok = m.out[size_t(i)] == (uint32_t)(unsigned char)want[size_t(i)];
}
if (!ok) {
bad++;
@@ -558,7 +558,7 @@ int test_undigits(const std::string &file) {
rest /= 10;
if (garbage)
digit |= 5 << 7; // bits the macro must ignore
m.cells[size_t(i)] = uint16_t('0' + digit);
m.cells[size_t(i)] = uint32_t('0' + digit);
}
m.run(tape, 500'000);
n++;
@@ -587,8 +587,8 @@ int test_add_ip(const std::string &file) {
for (int a : accs)
for (int b = 0; b < 256; b++) {
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[1] = uint16_t(b); // B
m.cells[0] = uint32_t(a); // A
m.cells[1] = uint32_t(b); // B
m.run(tape, 5'000'000);
n++;
const int want = (a + b) & kDefaultCellMask;
@@ -617,8 +617,8 @@ int test_mul(const std::string &file) {
for (int mc = 0; mc < 256; mc++)
for (int mu = 0; mu < 256; mu++) {
machine m;
m.cells[0] = uint16_t(mc); // MC
m.cells[1] = uint16_t(mu); // MUL
m.cells[0] = uint32_t(mc); // MC
m.cells[1] = uint32_t(mu); // MUL
m.run(tape, 5'000'000);
n++;
const int p = mc * mu;
+7 -7
View File
@@ -28,15 +28,15 @@
namespace {
struct machine {
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
std::deque<uint16_t> in; // characters for INP, from the header's `; in`
std::vector<uint32_t> cells = std::vector<uint32_t>(kDefaultMemoryCells, 0);
std::vector<uint32_t> out;
std::deque<uint32_t> in; // characters for INP, from the header's `; in`
size_t dp = 0;
size_t steps = 0;
bool finished = true; // false: the step limit ran out
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
static uint32_t ror(uint32_t v) {
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape, size_t limit = 5'000'000) {
@@ -213,10 +213,10 @@ int main(int argc, char **argv) {
expected_in(file, in_text);
machine m;
for (char ch : in_text)
m.in.push_back(uint16_t((unsigned char)ch & 0x7f));
m.in.push_back(uint32_t((unsigned char)ch & 0x7f));
m.run(tape);
std::vector<int> got;
for (uint16_t v : m.out)
for (uint32_t v : m.out)
got.push_back(int(v));
const bool ok = m.finished && got == want;
std::cout << name << ": " << tape.size() << " characters, " << m.steps
+7 -7
View File
@@ -15,12 +15,12 @@ const char *const INSTRUCTIONS = "><-+[].r"; // r = our ror alias
// note "-" is ror, and there is no separate 'r'
struct machine {
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
std::vector<uint16_t> out;
std::vector<uint32_t> cells = std::vector<uint32_t>(kDefaultMemoryCells, 0);
std::vector<uint32_t> out;
size_t dp = 0;
static uint16_t ror(uint16_t v) {
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
static uint32_t ror(uint32_t v) {
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
}
void run(const std::string &tape) {
@@ -97,9 +97,9 @@ int main(int argc, char **argv) {
for (int combo = 0; combo < 8; combo++) {
const int a = combo & 1, b = (combo >> 1) & 1, c = (combo >> 2) & 1;
machine m;
m.cells[0] = uint16_t(a); // A
m.cells[1] = uint16_t(b); // B
m.cells[2] = uint16_t(c); // Ci
m.cells[0] = uint32_t(a); // A
m.cells[1] = uint32_t(b); // B
m.cells[2] = uint32_t(c); // Ci
m.run(tape);
const int sum = a ^ b ^ c;
const int carry = (a & b) | (a & c) | (b & c);
+7 -7
View File
@@ -34,8 +34,8 @@ int main(int argc, char **argv) {
const std::string tape((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
std::vector<uint16_t> cells(kDefaultMemoryCells, 0);
std::deque<uint16_t> input;
std::vector<uint32_t> cells(kDefaultMemoryCells, 0);
std::deque<uint32_t> input;
bool trace = false;
for (int i = 2; i < argc; i++) {
const std::string a = argv[i];
@@ -49,17 +49,17 @@ int main(int argc, char **argv) {
return 2;
}
for (const char *p = argv[++i]; *p; p++)
input.push_back(uint16_t((unsigned char)*p & 0x7f));
input.push_back(uint32_t((unsigned char)*p & 0x7f));
continue;
}
const size_t eq = a.find('=');
if (eq == std::string::npos)
continue;
const size_t at = size_t(std::stoul(a.substr(0, eq)));
cells.at(at) = uint16_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
cells.at(at) = uint32_t(std::stoul(a.substr(eq + 1)) & kDefaultCellMask);
}
std::vector<uint16_t> out;
std::vector<uint32_t> out;
size_t dp = 0;
const size_t limit = 5'000'000; // same as r8forge_derive
size_t pc = 0;
@@ -89,7 +89,7 @@ int main(int argc, char **argv) {
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = uint16_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1)));
cells[dp] = uint32_t((cells[dp] >> 1) | ((cells[dp] & 1u) << (kDefaultCellSize - 1)));
break;
// biz / rnz test the LOW BIT, not "is the cell zero" -- see ROT-8-viz // src/rot8.c. Reading them as a Brainfuck style zero test makes every
// byte level program in the library look like an endless loop.
@@ -137,7 +137,7 @@ int main(int argc, char **argv) {
}
std::cout << "out";
for (uint16_t v : out)
for (uint32_t v : out)
std::cout << " " << int(v);
std::cout << " (" << steps << " steps, cell0 ended 0x" << std::hex
<< int(cells[0]) << std::dec << ")\n";