Repeated subtraction ran N/D rounds and hit the 5M step ceiling for D = 1
(2047/1 hangs outright); it also cost 2.46M steps for 2047/2. Binary long
division is fixed at _CELL_SIZE rounds whatever the operands are:
R = 0 ; Q = 0
repeat _CELL_SIZE: R = (R << 1) | next bit (rol N hands the bits out
MSB first and restores N
after W rounds)
if R >= D: R -= D ; quotient bit = 1
The one non obvious part is the comparison. R needs W+1 bits after the shift,
but the high bit (H, the bit rol wraps out of Rlo) only has to decide the
commit: H = 1 means R >= 2^W > D, so commit = H | Bo, where Bo is raw_sub's "no
borrow" (Rlo >= D). SPEC 14's Shi = H & Bo is always 0 under the invariant "R
< D at the start of every round" (H = 1 and Bo = 1 together would need R >= 2D),
so that clb_bit_and and its copy do not exist in the built version.
Signature is now clb_divmod N D Q R S: N and D survive, Q and R each get their
own cell (the old version ate N into the remainder), and S is a 7 cell scratch
block (Rhi, Sd, Bo, Cm, Ci, buf, NR). Only S+5 (the copy buffer) has to be
clean on entry; the rest is rewritten or used as a single bit, which is what
lets clb_digits pass the same dirty block back in every round.
The order that cost clb_mul two reworks is the delicate bit: rol Rlo wraps its
top bit into bit 0, so clb_bit_mov has to move that bit into Rhi (which also
clears it) before bit_copy injects N's bit. bit_copy (not clb_bit_mov) is what
keeps N intact.
Measured on drv/divmod.r8asm (11 bit):
repeated subtraction shift-and-restore
2047/10 495,986 28,593
2047/2 2,461,650 31,997
999/7 340,086 28,661
2047/1 HANG (>5M) 32,383
Small N/D get slower (fixed W rounds) -- the same trade clb_mul made. Worst
case over the sweep is 32,383 steps; the tape grows 4189 -> 4499 characters.
About the numbers in SPEC 14 / AGENTS: the 1.48M steps claimed there do not
reproduce on this tree. Measured before the change, 2047/10 was 495,986 and
clb_digits(2047) was 740,695, so the docs now carry the measured values and say
where the old figure came from.
clb_digits keeps its shape (one divmod in a 4 round loop, written digits shift
up by one cell), with S grown to 13 cells: TEN, quotient, remainder, the 7
divmod scratch cells, the loop counter and add_ip's T/F. 2047 goes from
740,695 to 145,138 steps (worst 164,867) and 10829 -> 13717 characters. It
needs its own %include "asm/add_ip.r8asm" now: that used to arrive through the
old divmod.
Tests: test_divmod sweeps 8 divisors x all 2048 dividends (16384 pairs; the old
76 sample table is a subset) and also asserts N and D come back unchanged;
test_digits sweeps all 2048 values. Both print the worst step count.
Two traps found on the way:
* r8forge_derive (and byte_op_test/synth_test) checked the assembler error flag
before export_rot8(), but an unknown macro or a bad .tp target is found while
the tree is *expanded*: a missing %include left a truncated tape that still
ran and printed a plausible wrong answer (all four digits came out '0'). All
three now check after the export.
* raw_sub's comment claimed Bo = 1 means A < B; the code (and clb_sub_nc) mean
A >= B. Comment fixed, behaviour untouched.
Also adds drv/div_shift.r8asm, the single step check for the cross cell shift
plus injection, and records the measurements in SPEC 7.6/7.7/14 and AGENTS.
116 lines
3.4 KiB
C++
116 lines
3.4 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/geometry.hh"
|
|
#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<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
|
|
std::vector<uint16_t> out;
|
|
size_t dp = 0;
|
|
|
|
static uint16_t ror(uint16_t v) {
|
|
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
|
|
}
|
|
|
|
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;
|
|
// biz / rnz test the LOW BIT of the cell, not "is the cell zero":
|
|
// rot8_biz: if ((*mem & 1) == 0) jump past the matching ']'
|
|
// rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['
|
|
// (reference: ROT-8-viz src/rot8.c). With "cell == 0" instead, every
|
|
// byte level program in the library looks like an endless loop.
|
|
case '[':
|
|
if ((cells[dp] & 1) == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
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:
|
|
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, kDefaultCellSize)) {
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
return 1;
|
|
}
|
|
const std::string tape = assembler.export_rot8();
|
|
if (assembler.failed()) { // unknown macro / bad '.tp', found while expanding
|
|
std::cerr << "could not assemble " << file << "\n";
|
|
return 1;
|
|
}
|
|
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] = uint16_t(a); // A
|
|
m.cells[1] = uint16_t(b); // B
|
|
m.cells[2] = uint16_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;
|
|
}
|