Files
R8Forge/tests/synth_test.cc
T
dsh 548c239a85 Tape-length search beats the layer search on the full adder
Five layers cost 95 characters; six layers cost 90.  Both are in examples/ and
both pass the eight-combination check on the ROT-8 machine in
tests/synth_test.cc (which now takes a file argument).

    sacrifice B,  FLIP {S,T}
    sacrifice A,  FLIP {B}
    sacrifice B,  FLIP {A,Ci,S}
    sacrifice Ci, FLIP {B,Co,T}
    sacrifice S,  SET1 {Ci,Co}
    sacrifice T,  FLIP {B,Ci,S,Co}

Cost 75 in the model, 90 on the tape once the inter-block hops and the two
output lines are counted.  The goal was popped at 75 only after everything
below 75 had been expanded, so 75 is the minimum of this cost model.

Trading a layer for a shorter tape is not a contradiction: the layer search
cannot see that a small, cheap block can be worth more than one wide, expensive
one.
2026-11-06 09:12:15 +08:00

104 lines
2.8 KiB
C++

// Assembles examples/full_add.r8asm (whose body is synthesised at assembly
// time) and runs the resulting tape on a tiny ROT-8 machine for all eight
// input combinations.
#include "r8forge/r8asm.hh"
#include <cstdlib>
#include <iostream>
#include <vector>
namespace {
// the eight ROT-8 instructions
const char *const INSTRUCTIONS = "><-+[].r"; // r = our ror alias
// real ROT-8: '>' '<' '-' '+' '[' ']' '.' ','
// note "-" is ror, and there is no separate 'r'
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
size_t dp = 0;
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
void run(const std::string &tape) {
for (size_t pc = 0; pc < tape.size(); pc++) {
switch (tape[pc]) {
case '>':
dp++;
break;
case '<':
dp--;
break;
case '-': // flb: flip last bit
cells[dp] ^= 1;
break;
case '+': // ror: rotate right
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:
break;
}
}
}
};
} // namespace
int main(int argc, char **argv) {
const char *file = argc > 1 ? argv[1] : "examples/full_add.r8asm";
r8asm assembler;
if (!assembler.import_file(file, 8)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
const std::string tape = assembler.export_rot8();
std::cout << "tape: " << tape.size() << " characters\n";
int failures = 0;
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] = uint8_t(a); // A
m.cells[1] = uint8_t(b); // B
m.cells[2] = uint8_t(c); // Ci
m.run(tape);
const int sum = a ^ b ^ c;
const int carry = (a & b) | (a & c) | (b & c);
const bool ok = m.out.size() == 2 && m.out[0] == sum && m.out[1] == carry;
std::cout << " " << a << b << c << " -> " << (m.out.size() > 0 ? int(m.out[0]) : -1)
<< (m.out.size() > 1 ? int(m.out[1]) : -1) << " expect " << sum
<< carry << (ok ? " ok" : " WRONG") << "\n";
if (!ok)
failures++;
}
std::cout << (failures ? "FAILED\n" : "all eight combinations ok\n");
return failures ? 1 : 0;
}