Files
R8Forge/tests/byte_add_test.cc
T
dsh 43390efe8a Byte level add, one bit per cell: 65536 combinations ok
The rotation based byte layer never ran.  Measured with r8forge_derive -2:

    raw_add   A=0,B=0 / 1+1 / 3+5 / 1+0   out empty (step limit hit)
    raw_add   A=127,B=130                 out 0 0 0 0 0, everything destroyed
    raw_and   0xF0 and 0xCC, 1 and 1, 0 and 5   out empty
    copy      A=5                         out empty (A=0 returns fine)

Not "only operand preservation is missing": set_0 (= [-]) cannot clear a cell
that holds more than one bit, and biz only answers "is this whole cell zero",
because ror does not change zero-ness.  So the bits of a packed byte are
unreadable, and the whole ror + set_0 layer has no future.  The docs now say
that instead of blaming operand preservation.

What replaces it is one bit per cell:

  asm/byte_add.r8asm   8 bit adder.  Bit i owns four adjacent cells at base+4i:
                       A, B, carry (in -> out), scratch.  The synthesised four
                       cell slice bit_raw_add_nc does the bit, clb_bit_mov moves
                       the carry into the next quartet.  A doubles as the
                       accumulator, so calling byte_add again continues the sum.
  asm/pack_byte.r8asm  eight flags -> one byte, the only direction that works.
                       bit_raw_flip_if consumes a flag into flipping bit 0 of an
                       accumulator that may already hold bits (no set_0 there).
  examples/add8.r8asm  a+b, 1308 characters.  A shared carry cell layout needs
                       only 25 cells but was measured at 2517: paying .tp travel
                       eight times beats saving seven carry cells.
  examples/add8_inc.r8asm   add 1 three times, proving the block composes.
  tests/byte_add_test.cc    r8forge_byte_add_test: runs the tapes on the ROT-8
                       machine over every input pair, 0.3 s.

Also: AGENTS.md gains the measurement table, the third instruction set
constraint (packed bytes are unreadable), the new files, and the note that
build/ has to be rebuilt with the VS bundled cmake -- scoop's 4.4 reconfigures
it and dies with "No preprocessor test for PellesC".
2026-11-06 23:40:14 +08:00

168 lines
4.8 KiB
C++

// Assembles the byte-level adders (examples/add8*.r8asm) and runs them on a tiny
// ROT-8 machine for every input combination. This is the runtime check the
// library never had: pass/fail comes from executing the tape, not from looking
// at the character count.
//
// r8forge_byte_add_test [repo root]
//
// The paths in the sources are relative to the repo root, so run it from there.
#include "r8forge/r8asm.hh"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
namespace {
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
size_t dp = 0;
size_t steps = 0;
bool finished = true; // false: the step limit ran out (a loop that never ends)
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
void run(const std::string &tape, size_t limit) {
for (size_t pc = 0; pc < tape.size(); pc++) {
if (++steps > limit) {
finished = false;
return;
}
switch (tape[pc]) {
case '>':
dp++;
break;
case '<':
dp--;
break;
case '-': // flb
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = ror(cells[dp]);
break;
case '[':
if (cells[dp] == 0) {
int depth = 1;
while (depth) {
pc++;
if (tape[pc] == '[')
depth++;
else if (tape[pc] == ']')
depth--;
}
}
break;
case ']':
if (cells[dp] != 0) {
int depth = 1;
while (depth) {
pc--;
if (tape[pc] == ']')
depth++;
else if (tape[pc] == '[')
depth--;
}
}
break;
case '.':
out.push_back(cells[dp]);
break;
default: // ',' (input) is not modelled
break;
}
}
}
};
// The examples lay bit i out in cell 4i + off: off 0 is A, 1 is B, 3 is scratch.
void put_bits(machine &m, int off, int value) {
for (int b = 0; b < 8; b++)
if (value & (1 << b))
m.cells[size_t(4 * b + off)] ^= 1;
}
std::string assemble(const std::string &file) {
r8asm assembler;
if (!assembler.import_file(file, 8) || assembler.failed()) {
std::cerr << "could not assemble " << file << "\n";
std::exit(1);
}
return assembler.export_rot8();
}
int failures = 0;
void report(const std::string &what, int a, int b, const machine &m, int sum,
int carry) {
if (failures++ < 5)
std::cerr << " WRONG " << what << " a=" << a << " b=" << b << " out";
if (failures <= 5) {
for (uint8_t v : m.out)
std::cerr << " " << int(v);
std::cerr << " (expect low " << (sum & 0xff) << ", carry " << carry
<< "), finished=" << m.finished << " steps=" << m.steps << "\n";
}
}
// Every (a, b) pair through examples/add8.r8asm.
int test_add(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "add8: " << tape.size() << " characters\n";
int bad = 0;
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
put_bits(m, 0, a);
put_bits(m, 1, b);
m.run(tape, 200'000);
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 2 &&
m.out[0] == (sum & 0xff) && m.out[1] == (sum >> 8);
if (!ok) {
bad++;
if (bad <= 3)
report("add8", a, b, m, sum, sum >> 8);
}
}
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
return bad;
}
// examples/add8_inc.r8asm adds 1 three times, so the carry is only the carry of
// that last add: earlier carries are dropped (the accumulator is 8 bits).
int test_inc(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "add8_inc: " << tape.size() << " characters\n";
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
put_bits(m, 0, a);
m.run(tape, 400'000);
const int low = (a + 3) & 0xff;
const int carry = ((a + 2) & 0xff) == 0xff ? 1 : 0;
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == low &&
m.out[1] == carry;
if (!ok) {
bad++;
if (bad <= 3)
report("add8_inc", a, 3, m, a + 3, carry);
}
}
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
return bad;
}
} // namespace
int main(int argc, char **argv) {
const std::string root = argc > 1 ? argv[1] : ".";
const int bad = test_add(root + "/examples/add8.r8asm") +
test_inc(root + "/examples/add8_inc.r8asm");
std::cout << (bad ? "FAILED\n" : "byte level adders ok\n");
return bad ? 1 : 0;
}