clb_digits_signed N REG S T F prints '-' (45) and then |N| as the same four zero padded digits; clb_divmod_signed N D Q R S T1 T2 F is the C style truncated division (q = trunc(n/d), r = n - q*d, remainder follows the dividend). Both are wrappers: clb_digits / clb_divmod still do the work, so the tape only grows by the sign handling -- 572 characters for the display (12151 vs 11579 in the same driver layout) and 1191 for the division (6018 vs 4827). The sign bit is read with rol + bit_copy + ror, 49 characters. clb_bit_mov is deliberately NOT used: it eats the source bit, so N comes back as N - HALF and 1024 -- whose magnitude 1024 only fits the unsigned range -- prints as 0000. A clb_sub_nc against a HALF constant was the other candidate: 765 characters for the call plus ~60 for the constant cell. A flag has to outlive the block that consumes it, because biz/rnz only exit once the body clears the condition bit. So sign(N) is moved into T2, sign(D) into F, and the remainder block flips F into sign(N)^sign(D) on its way out; a single conditional negation of the quotient then carries the xor. The negation scratch is S+6: the only cell in S with a "clean on entry" contract, and clb_neg_ip leaves it zero again for clb_divmod. Verified by r8forge_byte_op_test: all 2048 values for the display (worst 117606 steps) and 4202 division pairs -- every |a|,|b| <= 32 plus the 0/+-1/+-1023/+-1024 boundary combinations -- compared against C's / and %. examples/signed_demo.r8asm is the single point regression that runs as a program (the flagship cannot carry them: +12.1k / +5.6k on a 62351 tape).
773 lines
28 KiB
C++
773 lines
28 KiB
C++
// Runs the byte level library macros over every input pair on a ROT-8 machine.
|
|
//
|
|
// r8forge_byte_op_test [repo root]
|
|
//
|
|
// The machine below follows the reference implementation in ROT-8-viz
|
|
// (src/rot8.c): `biz` jumps when the LOW BIT of the cell is 0 and `rnz` jumps
|
|
// back when it is 1, so `set_0` (= [-]) clears the low bit of whatever is in
|
|
// the cell. Reading `[`/`]` as a Brainfuck style "is the cell zero" test makes
|
|
// every one of these programs look like an endless loop -- which is exactly the
|
|
// mistake three interpreters in this repo used to make.
|
|
#include "r8forge/r8asm.hh"
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
// The cell width is a knob (geometry.hh), so every expectation below is
|
|
// written in terms of it instead of in terms of a byte: kCellMod is one past
|
|
// the largest value a cell holds. The sweeps walk the first 256 values, which
|
|
// is a sample of the cell range rather than all of it.
|
|
constexpr int kCellSize = int(kDefaultCellSize);
|
|
constexpr int kCellMod = 1 << kCellSize;
|
|
constexpr int kCellMask = int(kDefaultCellMask);
|
|
|
|
struct machine {
|
|
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 uint32_t ror(uint32_t v) {
|
|
return uint32_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
|
|
}
|
|
|
|
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 = (dp + 1) % cells.size();
|
|
break;
|
|
case '<':
|
|
dp = (dp + cells.size() - 1) % cells.size();
|
|
break;
|
|
case '-': // flb
|
|
cells[dp] ^= 1;
|
|
break;
|
|
case '+': // ror
|
|
cells[dp] = ror(cells[dp]);
|
|
break;
|
|
case '[': // biz: branch when the low bit is zero
|
|
if ((cells[dp] & 1) == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']': // rnz: return when the low bit is one
|
|
if ((cells[dp] & 1) == 1) {
|
|
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 expansion (export_rot8) is where an unknown macro or a bad '.tp' target
|
|
// is found, so the error flag has to be checked after it: a truncated tape
|
|
// still runs, and produces a plausible looking wrong answer.
|
|
std::string assemble(const std::string &file) {
|
|
r8asm assembler;
|
|
if (!assembler.import_file(file, kDefaultCellSize)) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
std::exit(1);
|
|
}
|
|
std::string tape = assembler.export_rot8();
|
|
if (assembler.failed()) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
std::exit(1);
|
|
}
|
|
return tape;
|
|
}
|
|
|
|
int failures = 0;
|
|
|
|
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 (uint32_t v : m.out)
|
|
std::cerr << " " << int(v);
|
|
std::cerr << " want " << want << " finished=" << m.finished
|
|
<< " steps=" << m.steps << "\n";
|
|
}
|
|
}
|
|
|
|
// raw_and / raw_or / raw_xor: both operands are preserved, the result is third.
|
|
int test_bitop(const std::string &file, const char *name, int op) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << name << ": " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
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 &&
|
|
m.out[1] == b && m.out[2] == want;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report(name, a, b, m,
|
|
std::to_string(a) + " " + std::to_string(b) + " " +
|
|
std::to_string(want));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// raw_add: out is A, B, Ci, S, Co.
|
|
int test_add(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "raw_add: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
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 &&
|
|
m.out[1] == b && m.out[2] == 0 &&
|
|
m.out[3] == (sum & kCellMask) && m.out[4] == (sum >> kCellSize);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("raw_add", a, b, m,
|
|
std::to_string(a) + " " + std::to_string(b) + " 0 " +
|
|
std::to_string(sum & kCellMask) + " " +
|
|
std::to_string(sum >> kCellSize));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// raw_inc: out is A, Ci, S, Co; S = A + Ci.
|
|
int test_inc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "raw_inc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int ci = 0; ci < 2; ci++) {
|
|
machine m;
|
|
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 &&
|
|
m.out[1] == ci && m.out[2] == (sum & kCellMask) &&
|
|
m.out[3] == (sum >> kCellSize);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("raw_inc", a, ci, m,
|
|
std::to_string(a) + " " + std::to_string(ci) + " " +
|
|
std::to_string(sum & kCellMask) + " " +
|
|
std::to_string(sum >> kCellSize));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 512 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_add_nc: out is A, B, S, Ci. This one eats both operands, so A and B have
|
|
// to come back zero -- that is what lets it use the 4 cell slice.
|
|
int test_add_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_add_nc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
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 &&
|
|
m.out[1] == 0 && m.out[2] == (sum & kCellMask) &&
|
|
m.out[3] == (sum >> kCellSize);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_add_nc", a, b, m,
|
|
"0 0 " + std::to_string(sum & kCellMask) + " " +
|
|
std::to_string(sum >> kCellSize));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_inc_nc: out is A, S, Ci.
|
|
int test_inc_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_inc_nc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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 &&
|
|
m.out[1] == (sum & kCellMask) && m.out[2] == (sum >> kCellSize);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_inc_nc", a, 1, m,
|
|
"0 " + std::to_string(sum & kCellMask) + " " +
|
|
std::to_string(sum >> kCellSize));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_neg_nc: out is A, S, Ci; S = -A, A is eaten.
|
|
int test_neg_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_neg_nc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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
|
|
const bool ok = m.finished && m.out.size() == 3 && m.out[0] == 0 &&
|
|
m.out[1] == neg && m.out[2] == carry;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_neg_nc", a, 0, m,
|
|
"0 " + std::to_string(neg) + " " + std::to_string(carry));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_sub_nc: out is A, B, S, Ci; S = A - B and Ci = 1 means no borrow.
|
|
int test_sub_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_sub_nc: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
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;
|
|
const bool ok = m.finished && m.out.size() == 4 && m.out[0] == 0 &&
|
|
m.out[1] == 0 && m.out[2] == diff && m.out[3] == no_borrow;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_sub_nc", a, b, m,
|
|
"0 0 " + std::to_string(diff) + " " +
|
|
std::to_string(no_borrow));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// Same macro with its carry in at 0 is the ones' complement: S = ~A.
|
|
int test_not_nc(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_neg_nc (carry 0): " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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 &&
|
|
m.out[1] == inv && m.out[2] == 0;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_neg_nc/0", a, 0, m, "0 " + std::to_string(inv) + " 0");
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_not_ip: the ones' complement done in place, one cell, no carry.
|
|
int test_not_ip(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_not_ip: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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() == 1 && m.out[0] == inv;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_not_ip", a, 0, m, std::to_string(inv));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_inc_ip: A = A + 1, both in place and in two cells.
|
|
int test_inc_ip(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_inc_ip: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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 &&
|
|
m.cells[1] == 0; // the counter cell is spent
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_inc_ip", a, 0, m, std::to_string(want));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_neg_ip: A = -A, in place, two cells.
|
|
int test_neg_ip(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_neg_ip: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_neg_ip", a, 0, m, std::to_string(want));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_dec_ip: A = A - 1, in place.
|
|
int test_dec_ip(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_dec_ip: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int a = 0; a < 256; a++) {
|
|
machine m;
|
|
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;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_dec_ip", a, 0, m, std::to_string(want));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_nonzero: F = (B != 0), B untouched.
|
|
int test_nonzero(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_nonzero: " << tape.size() << " characters\n";
|
|
int bad = 0;
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
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);
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_nonzero", b, 0, m,
|
|
std::to_string(b) + " " + std::to_string(b != 0 ? 1 : 0));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_ones: B = 2^A - 1, i.e. A consecutive ones from the low bit, A is eaten.
|
|
// Only A <= cell width is in the contract, so that is the whole domain.
|
|
int test_ones(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_ones: " << tape.size() << " characters\n";
|
|
int bad = 0, n = 0;
|
|
for (int a = 0; a <= kCellSize; a++) {
|
|
machine m;
|
|
m.cells[0] = uint32_t(a); // A, the count
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
const int want = (1 << a) - 1;
|
|
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == 0 &&
|
|
m.out[1] == want;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_ones", a, 0, m, std::to_string(want));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " counts ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_divmod: Q = N div D, R = N mod D, and N and D come back unchanged. Shift
|
|
// and restore, so the loop runs _CELL_SIZE times whatever N and D are -- the
|
|
// repeated subtraction this replaced ran N/D times and crossed the 5M step
|
|
// budget outright for D = 1, so it could only be sampled. Now every dividend
|
|
// can be swept for a spread of divisors, and the divisor set is a superset of
|
|
// the old sampled table's.
|
|
int test_divmod(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_divmod: " << tape.size() << " characters\n";
|
|
const int divs[] = {1, 2, 3, 7, 10, 100, 1000, 2047};
|
|
int bad = 0, n = 0;
|
|
size_t worst = 0;
|
|
for (int d : divs)
|
|
for (int a = 0; a < kCellMod; a++) {
|
|
machine m;
|
|
m.cells[0] = uint32_t(a); // N
|
|
m.cells[1] = uint32_t(d); // D
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
if (m.steps > worst) worst = m.steps;
|
|
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == a / d &&
|
|
m.out[1] == a % d && m.cells[0] == a && m.cells[1] == d;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_divmod", a, d, m,
|
|
std::to_string(a / d) + " " + std::to_string(a % d));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " pairs ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "")
|
|
<< " (worst " << worst << " steps)\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_digits: the value becomes four ASCII digits, most significant first. The
|
|
// divmod underneath is now O(_CELL_SIZE) instead of O(N/10), so the whole cell
|
|
// range fits in the budget: every value is swept.
|
|
int test_digits(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_digits: " << tape.size() << " characters\n";
|
|
int bad = 0, n = 0;
|
|
size_t worst = 0;
|
|
for (int v = 0; v < kCellMod; v++) {
|
|
machine m;
|
|
m.cells[0] = uint32_t(v);
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
if (m.steps > worst) worst = m.steps;
|
|
std::string want = std::to_string(v);
|
|
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)] == (uint32_t)(unsigned char)want[size_t(i)];
|
|
}
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3) report("clb_digits", v, 0, m, want);
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " values ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "")
|
|
<< " (worst " << worst << " steps)\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_digits_signed: the two's complement reading of the same values, with a
|
|
// leading '-' (45) in front of the digits when the value is negative. The
|
|
// interesting corner is 1024, which IS negative (-1024) but whose magnitude is
|
|
// out of the signed range -- it still has to print "-1024", so the wrapper must
|
|
// not compute |N| as "clear the sign bit".
|
|
int test_digits_signed(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_digits_signed: " << tape.size() << " characters\n";
|
|
const int half = kCellMod / 2;
|
|
int bad = 0, n = 0;
|
|
size_t worst = 0;
|
|
for (int v = 0; v < kCellMod; v++) {
|
|
machine m;
|
|
m.cells[0] = uint32_t(v);
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
if (m.steps > worst) worst = m.steps;
|
|
const int signed_v = v >= half ? v - kCellMod : v;
|
|
std::string digits = std::to_string(signed_v < 0 ? -signed_v : signed_v);
|
|
while (digits.size() < 4) digits.insert(digits.begin(), '0');
|
|
std::vector<uint32_t> want;
|
|
std::string text;
|
|
if (signed_v < 0) {
|
|
want.push_back(45);
|
|
text = "-";
|
|
}
|
|
for (char c : digits)
|
|
want.push_back(uint32_t((unsigned char)c));
|
|
text += digits;
|
|
const bool ok = m.finished && m.out == want &&
|
|
m.cells[18] == 0 && m.cells[19] == 0;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_digits_signed", v, 0, m, text);
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " values ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "")
|
|
<< " (worst " << worst << " steps)\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_divmod_signed: C semantics on two's complement values -- the quotient is
|
|
// trunc(a/b) and the remainder takes the sign of the dividend, so q*b + r == a
|
|
// holds with |r| < |b|. Every combination with |a|,|b| <= 32 is swept, plus the
|
|
// boundary values (0, +-1, +-1023, +-1024) against each other.
|
|
int test_divmod_signed(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_divmod_signed: " << tape.size() << " characters\n";
|
|
const int half = kCellMod / 2;
|
|
auto bits = [](int v) { return uint32_t(v & kCellMask); };
|
|
auto signed_of = [&](int v) { return v >= half ? v - kCellMod : v; };
|
|
|
|
std::vector<int> pairs; // the (a, b) bit patterns to try
|
|
for (int a = -32; a <= 32; a++)
|
|
for (int b = -32; b <= 32; b++) {
|
|
if (b == 0)
|
|
continue;
|
|
pairs.push_back(bits(a));
|
|
pairs.push_back(bits(b));
|
|
}
|
|
const int boundary[] = {0, 1, -1, 1023, -1023, 1024, -1024};
|
|
for (int a : boundary)
|
|
for (int b : boundary) {
|
|
if (b == 0)
|
|
continue;
|
|
pairs.push_back(bits(a));
|
|
pairs.push_back(bits(b));
|
|
}
|
|
|
|
int bad = 0, n = 0;
|
|
size_t worst = 0;
|
|
for (size_t i = 0; i + 1 < pairs.size(); i += 2) {
|
|
const int a = signed_of(int(pairs[i]));
|
|
const int b = signed_of(int(pairs[i + 1]));
|
|
machine m;
|
|
m.cells[0] = pairs[i]; // N
|
|
m.cells[1] = pairs[i + 1]; // D
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
if (m.steps > worst) worst = m.steps;
|
|
const int q = a / b; // C++ truncates toward zero: exactly the signed contract
|
|
const int r = a - q * b;
|
|
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == bits(q) &&
|
|
m.out[1] == bits(r) && m.cells[0] == bits(a < 0 ? -a : a) &&
|
|
m.cells[1] == bits(b < 0 ? -b : b) && m.cells[12] == 0 &&
|
|
m.cells[13] == 0 && m.cells[14] == 0;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_divmod_signed", int(pairs[i]), int(pairs[i + 1]), m,
|
|
std::to_string(q) + " " + std::to_string(r));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " pairs ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "")
|
|
<< " (worst " << worst << " steps)\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_undigits: the inverse of clb_digits. Every value is fed in as its decimal
|
|
// ASCII digits (most significant first, the same cells clb_digits writes) and
|
|
// has to come back unchanged. Every second case also sets garbage in bits 7 and
|
|
// above of each digit cell: the macro only looks at the low 7 bits, so the
|
|
// answer must not move.
|
|
int test_undigits(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_undigits: " << tape.size() << " characters\n";
|
|
const int digs = int(kCellSize) * 30103 / 100000 + 1;
|
|
int bad = 0, n = 0;
|
|
size_t worst = 0;
|
|
for (int v = 0; v < kCellMod; v++) {
|
|
for (int garbage = 0; garbage < 2; garbage++) {
|
|
machine m;
|
|
int rest = v;
|
|
for (int i = digs - 1; i >= 0; i--) {
|
|
int digit = rest % 10;
|
|
rest /= 10;
|
|
if (garbage)
|
|
digit |= 5 << 7; // bits the macro must ignore
|
|
m.cells[size_t(i)] = uint32_t('0' + digit);
|
|
}
|
|
m.run(tape, 500'000);
|
|
n++;
|
|
if (m.steps > worst) worst = m.steps;
|
|
const bool ok = m.finished && m.out.size() == 1 && m.out[0] == v;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_undigits", v, garbage, m, std::to_string(v));
|
|
}
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " digits ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "")
|
|
<< " (worst " << worst << " steps)\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_add_ip: A = A + B in place, B is eaten. The loop runs B times, so this
|
|
// one is sampled rather than exhaustive -- 256 addends x 8 accumulators.
|
|
int test_add_ip(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_add_ip: " << tape.size() << " characters (sampled)\n";
|
|
const int accs[] = {0, 1, 5, 127, 128, 200, 254, 255};
|
|
int bad = 0, n = 0;
|
|
for (int a : accs)
|
|
for (int b = 0; b < 256; b++) {
|
|
machine m;
|
|
m.cells[0] = uint32_t(a); // A
|
|
m.cells[1] = uint32_t(b); // B
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
const int want = (a + b) & kDefaultCellMask;
|
|
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == want &&
|
|
m.out[1] == 0;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_add_ip", a, b, m, std::to_string(want) + " 0");
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " sampled combinations ok")
|
|
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
|
|
return bad;
|
|
}
|
|
|
|
// clb_mul: (PH:PL) = MC * MUL, MC preserved. Shift and add: the loop runs
|
|
// once per bit of MUL whatever MUL's value is, so the classic 8 bit domain can
|
|
// be swept exhaustively. (The repeated addition version this replaced looped
|
|
// MUL times, so it could only be sampled -- and it hit the step limit outright
|
|
// for large multipliers.)
|
|
int test_mul(const std::string &file) {
|
|
const std::string tape = assemble(file);
|
|
std::cout << "clb_mul: " << tape.size() << " characters\n";
|
|
int bad = 0, n = 0;
|
|
for (int mc = 0; mc < 256; mc++)
|
|
for (int mu = 0; mu < 256; mu++) {
|
|
machine m;
|
|
m.cells[0] = uint32_t(mc); // MC
|
|
m.cells[1] = uint32_t(mu); // MUL
|
|
m.run(tape, 20'000'000);
|
|
n++;
|
|
const int p = mc * mu;
|
|
const bool ok = m.finished && m.out.size() == 3 &&
|
|
m.out[0] == (p & kCellMask) && m.out[1] == ((p >> kCellSize) & kCellMask) &&
|
|
m.out[2] == mc;
|
|
if (!ok) {
|
|
bad++;
|
|
if (bad <= 3)
|
|
report("clb_mul", mc, mu, m,
|
|
std::to_string(p & kCellMask) + " " + std::to_string((p >> kCellSize) & kCellMask) +
|
|
" " + std::to_string(mc));
|
|
}
|
|
}
|
|
std::cout << " " << (bad ? "FAILED" : "all " + std::to_string(n) + " 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 std::string drv = root + "/drv/";
|
|
const int bad = test_add(drv + "raw_add_byte.r8asm") +
|
|
test_bitop(drv + "raw_and_byte.r8asm", "raw_and", 0) +
|
|
test_bitop(drv + "raw_or_byte.r8asm", "raw_or", 1) +
|
|
test_bitop(drv + "raw_xor_byte.r8asm", "raw_xor", 2) +
|
|
test_inc(drv + "raw_inc_byte.r8asm") +
|
|
test_add_nc(drv + "raw_add_nc_byte.r8asm") +
|
|
test_inc_nc(drv + "raw_inc_nc_byte.r8asm") +
|
|
test_neg_nc(drv + "raw_neg_nc_byte.r8asm") +
|
|
test_not_nc(drv + "raw_not_nc_byte.r8asm") +
|
|
test_not_ip(drv + "not_ip_byte.r8asm") +
|
|
test_inc_ip(drv + "inc_ip_byte.r8asm") +
|
|
test_neg_ip(drv + "neg_ip_byte.r8asm") +
|
|
test_dec_ip(drv + "dec_ip_byte.r8asm") +
|
|
test_nonzero(drv + "nonzero_byte.r8asm") +
|
|
test_ones(drv + "ones.r8asm") +
|
|
test_divmod(drv + "divmod.r8asm") +
|
|
test_digits(drv + "digits.r8asm") +
|
|
test_digits_signed(drv + "digits_signed.r8asm") +
|
|
test_divmod_signed(drv + "divmod_signed.r8asm") +
|
|
test_undigits(drv + "undigits_byte.r8asm") +
|
|
test_add_ip(drv + "add_ip_byte.r8asm") +
|
|
test_mul(drv + "mul_byte.r8asm") +
|
|
test_sub_nc(drv + "raw_sub_nc_byte.r8asm");
|
|
std::cout << (bad ? "FAILED\n" : "byte level library ok\n");
|
|
return bad ? 1 : 0;
|
|
}
|