A 5 cell adder that ignores carry in and out

The idea: for an adder whose operand sum is known not to overflow, there is no
need for a separate carry in cell or a carry out cell -- the carry can live in
one cell and be overwritten each bit, and the final carry is simply dropped.
That takes the slice from 6 cells (A, B, Ci, S, Co, tmp) down to 5 (A, B,
carry, S, tmp).

The slice was not hand written: the table was derived from S = A^B^Ci and
carry = AB|AC|BC, and %synth searched it with metric flat:

    bit_add_nc_synth: tape cost 27 in 5 blocks   (1891406 states)
    bit_raw_add_synth (the 6 cell full adder):   tape cost 38 in 5 blocks

so 11 units of the metric cheaper, about 29%, plus one cell less per slice.

Verified: all eight input combinations of a single slice are correct through the
built in machine (A, B, Ci -> S), including the all zero one.

Known limitation, found rather than guessed: the synthesised program only
touches the low K bits of each cell, the bits the truth table knows about.  The
caller therefore has to hand it cells whose high bits are zero.  Chaining slices
with ror breaks that: the rotated cell keeps bit 7, and a block that loops until
its cell reaches zero can never get there.  So the 8 bit raw_add_nc hangs; the
slice itself is fine, the driver needs to move each bit into a clean cell first.

Also added: r8forge_derive <file> -1 addr=value ... runs a tape with chosen cells
and prints OUT, which is how the slice was checked.
This commit is contained in:
starcells committed 2026-11-06 21:19:19 +08:00
1 parent e7ef0b0392
commit 7feebca5ee
3 files changed
+81

No files matched your search

+25
View File
@@ -9,6 +9,7 @@
#include "r8forge/r8asm.hh"
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
@@ -93,6 +94,30 @@ int main(int argc, char **argv) {
std::cout << "\n";
return 0;
}
// params == -1: the rest of argv is addr=value; set those cells, run, dump OUT
if (params == -1) {
r8asm a;
if (!a.import_file(file, 8)) {
std::cerr << "could not assemble " << file << "\n";
return 1;
}
machine m;
for (int i = 3; i < argc; i++) {
const char *eq = std::strchr(argv[i], '=');
if (eq == nullptr) {
continue;
}
const int at = std::atoi(std::string(argv[i], size_t(eq - argv[i])).c_str());
m.cells[size_t(at)] = uint8_t(std::atoi(eq + 1) & 0xff);
}
m.run(a.export_rot8());
std::cout << "out";
for (uint8_t v : m.out) {
std::cout << " " << int(v);
}
std::cout << "\n";
return 0;
}
if (params < 1 || params > 8) {
std::cerr << "params must be 1..8\n";
return 2;