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:
dsh committed 2026-11-06 21:19:19 +08:00
1 parent b1a8ca5617
commit 18bf78273e
3 files changed
+81

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
# params 5 mode 0 metric 1
1 17 0
2 18 0
0 6 0
4 4 1
1 12 0
+50
View File
@@ -0,0 +1,50 @@
%include "asm/lib.r8asm"
; 忽略进位的加法器切片:只有两个操作数,进位在 ci 这个 cell 里就地复用(进->出),
; 最高位产生的进位直接丢掉(前提:和不会溢出,即 A+B <= _CELL_MAX).
;
; p1..p5 = A, B, 进位(进->出), S, tmp
;
; stat 是从定义直接算出来的:S = A^B^Ci,进位 = AB|AC|BC;A、B、tmp 结束为 0,
; 所以每个输入组合后的值只落在「进位」和 S 这两位上。
%synth bit_add_nc_synth
params 5
stat 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12 0 8 8 4 8 4 4 12
metric flat
%endsynth
; 合成块假定不参与运算的参数从 0 开始(compress_stat_space 的语义),而 S 和 tmp
; 在循环里带着上一轮的值,所以外面包一层先清掉它们。
;
; 已知限制:合成出来的程序只碰每个 cell 的低 K 位(K = 参与运算的输入位数),
; 高位是"不追踪"的。所以调用者必须保证喂进来的 cell 高位是 0 —— 本轮实测:
; 单片对所有输入组合都对(包括全零),但用 ror 把上一片的进位/和挪到下一位之后,
; cell 里会留下第 7 位,下一片里那些"循环直到 cell 归零"的块就再也退不出来了。
; 也就是说要把多片串起来,还得先把每个 bit 挪到一个干净的 cell 里。
%macro bit_add_nc 5
.tp %4
set_0
.tp %5
set_0
bit_add_nc_synth %1 %2 %3 %4 %5
%endmacro
; 8 位: A、B、S 逐位右移,进位一直留在 ci 里;最后一轮产生的进位不再参与运算,
; 也就是被丢掉了。
; 注意:在补上"清高位"这一步之前,这个 8 位版本会卡住(见上面的已知限制)。
%macro raw_add_nc 5
.anchor before_raw_add_nc
.tp %3
set_0
%rep _CELL_SIZE
bit_add_nc %1 %2 %3 %4 %5
.tp %1
ror
.tp %2
ror
.tp %4
ror
%endrep
.tp before_raw_add_nc
%endmacro
+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;