From 1f3f453e885f9d83af48c2eb384637702eec8be2 Mon Sep 17 00:00:00 2001 From: dsh Date: Wed, 18 Nov 2026 21:13:52 +0800 Subject: [PATCH] signed: the two two's complement wrappers, with exhaustive sweeps 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). --- asm/digits.r8asm | 68 ++++++++++++++++++++++++ asm/divmod.r8asm | 105 +++++++++++++++++++++++++++++++++++++ drv/digits_signed.r8asm | 22 ++++++++ drv/divmod_signed.r8asm | 23 ++++++++ examples/signed_demo.r8asm | 59 +++++++++++++++++++++ tests/byte_op_test.cc | 103 ++++++++++++++++++++++++++++++++++++ 6 files changed, 380 insertions(+) create mode 100644 drv/digits_signed.r8asm create mode 100644 drv/divmod_signed.r8asm create mode 100644 examples/signed_demo.r8asm diff --git a/asm/digits.r8asm b/asm/digits.r8asm index 7013eb5..7a69912 100644 --- a/asm/digits.r8asm +++ b/asm/digits.r8asm @@ -3,6 +3,7 @@ ; %include "asm/divmod.r8asm" %include "asm/add_ip.r8asm" +%include "asm/inc_ip.r8asm" ; clb_neg_ip(带符号显示要用) ; ; Macro clb_digits ; %1: Cell N @@ -103,3 +104,70 @@ endloop %3+12 .tp before_clb_digits %endmacro + +; Macro clb_digits_signed +; %1: Cell N +; %2: Cell REG +; %3: Cell S +; %4: Cell T +; %5: CellBit F +; +; 带符号的 clb_digits:按二补码解释 N(第 _CELL_SIZE-1 位是符号位),负数先打一个 +; '-' (45),再打 **|N|** 的四位十进制,和 clb_digits 一样定宽补零、高位在前: +; +; 7 -> 48 48 48 55 "0007" +; 2047 -> 45 48 48 48 49 "-0001" (2047 = -1) +; 1024 -> 45 49 48 50 52 "-1024" (1024 = -1024,|N| 仍放得进无符号范围) +; 0 -> 48 48 48 48 "0000" +; +; N 被吃干净(最后交给 clb_digits);REG 起 4 格、S 起 13 格**必须干净**、T 进来 +; 必须干净,契约与 clb_digits 完全一样。F 是符号标志格,结束时是 0。 +; +; ---- 符号位怎么读:转一格,让它落到 bit0 ---- +; +; `biz` 只判 bit0,而符号位在 bit W-1。`rol`(= W-1 次 `ror`)正好把 bit W-1 转到 +; bit0,再用 **bit_copy**(保留源)把它抄进 F,最后 `rol` 的逆操作 `ror` 把 N 原样 +; 还回去。三段一共 10 + 38 + 1 个字符。 +; +; 为什么不用"clb_sub_nc T HALF 看有没有借位"那种比较:clb_sub_nc 展开一次就是 +; 765 字符,还要再花 ~60 字符造 HALF 常数格。转位法一整段才 49 字符。 +; +; **不能用 clb_bit_mov 抄符号位**:它会顺手把源位清掉,于是 N 少了符号位、转回 +; 原样之后变成 N-HALF —— 而正是负数那一支要拿 N 取负,少了这个位 |N| 就错 +; (1024 会打成 0)。bit_copy 走缓冲格,源位原样保留,这正是它比 clb_bit_mov +; 多出来的那 27 个字符买的东西。 +; +; ---- 负数那一支 ---- +; +; '-' 放在 S+10(clb_divmod 的商格):**第一轮 divmod 进来第一件事就是 erase 它** +; (asm/divmod.r8asm 开头的 `erase %3`),所以这个常数格不用调用者事后擦,也不破坏 +; "S 起 13 格干净"这个入口契约(我们只在 S+10 留下 45,后面自然被清)。 +; +; 取负走 clb_neg_ip(2 个 cell、110 字符);T 在 bit_copy 之后是干净的,正好当它的 +; 草稿格,取负结束又回到 0。 +%macro clb_digits_signed 5 +.anchor before_clb_digits_signed + +; ---- 1) F = 符号位,N 里那一位不动 +.tp %1 +rol ; bit W-1 -> bit0 +.tp %1 +bit_copy %5 %4 ; F = 符号,N 保留(缓冲 T 结束是干净的) +.tp %1 +ror ; N 转回原样 + +; ---- 2) 负数:先打 '-' 再把 N 取负 +.tp %5 +biz + load %3+10 45 ; '-' = 45,放商格(divmod 第一轮会 erase 它) + output %3+10 + clb_neg_ip %1 %4 ; N = -N = |N| + .tp %5 + flb ; 吃掉条件(biz/rnz 是循环对) +rnz + +; ---- 3) |N| 走原来的宏 +clb_digits %1 %2 %3 + +.tp before_clb_digits_signed +%endmacro diff --git a/asm/divmod.r8asm b/asm/divmod.r8asm index 4fb969b..100022c 100644 --- a/asm/divmod.r8asm +++ b/asm/divmod.r8asm @@ -3,6 +3,7 @@ ; %include "asm/raw_sub.r8asm" %include "asm/raw_or.r8asm" +%include "asm/inc_ip.r8asm" ; clb_neg_ip(带符号除法要用) ; ; Macro clb_divmod ; %1: Cell N @@ -84,3 +85,107 @@ rnz .tp before_clb_divmod %endmacro + +; Macro clb_divmod_signed +; %1: Cell N +; %2: Cell D +; %3: Cell Q +; %4: Cell R +; %5: Cell S +; %6: Cell T1 +; %7: Cell T2 +; %8: CellBit F +; +; 带符号的 clb_divmod:把 N、D 按二补码解释,给出 **C 语义的截断除法** +; (q = trunc(n/d)、r = n - q*d,余数与被除数同号,商向零取整): +; +; 7 2 -> Q= 3 R= 1 -7 2 -> Q=-3 R=-1 +; 7 -2 -> Q=-3 R= 1 -7 -2 -> Q= 3 R=-1 +; -7 2 的两位十进制就是 "-0003" / "-0001"(配 clb_digits_signed) +; +; N、D 被吃成各自的**绝对值**(|N|、|D|);Q 是商、R 是余数(都带符号,宏自己覆盖)。 +; S 起的 7 格草稿只要 S+6(buf)干净(和 clb_divmod 一样);T1、T2 进来必须干净、 +; 结束时都是 0;F 只用最低位,进来最低位必须是 0,结束时也是 0。 +; +; ---- 做法:取两个符号位 -> 各取绝对值 -> 无符号长除 -> 回填符号 ---- +; +; 截断语义不需要额外判断:对绝对值做 clb_divmod 得到的 |q|、|r|,余数的符号就是 +; 被除数的符号,商的符号是两个符号位的异或 —— 这正好是 C 的 trunc 语义。 +; +; 符号位怎么读和 clb_digits_signed 一样:`rol` 把 bit W-1 转到 bit0,`bit_copy` +; (保留源)抄进标志格,再 `ror` 转回去。**不能**用 clb_bit_mov 抄,它会吃掉源位, +; 于是 N 少了符号位、取绝对值时会差一个 HALF。 +; +; ---- 四个条件块和"标志位怎么活过 clb_divmod" ---- +; +; 符号位一共产出两次用途:除法**前**(决定要不要取负)和除法**后**(决定 Q、R 的 +; 符号)。而 `biz`/`rnz` 是循环对,块的身体必须把它那个条件位清掉才能退出 —— +; 所以标志位用完就没了。于是"用完还要留"的那一位要去另一格存着: +; +; T2 = sign(N) (第 2 步 clb_bit_mov 搬进去) +; F = sign(D) (第 3 步搬进去) +; S+6 = 取负用的草稿格 +; +; **为什么草稿格用 S+6**:clb_neg_ip 要求草稿格进来是干净的,而 S 里只有 S+6 +; (buf)有"干净"这个契约;clb_neg_ip 用完它就归零,所以 clb_divmod 进场时它照样干净。 +; +; 第 5 步顺便把 F 从 sign(D) 翻成 sign(N)^sign(D):那一支进来就说明 sign(N)=1, +; `flb` 一下正好;sign(N)=0 时整支跳过,F 保持 sign(D) = 0^sign(D),还是异或。 +; +; ---- 代价 ---- +; +; 宏本体(最小驱动)≈ 700 字符,大头是四次 clb_neg_ip(每次 110)。没有再省: +; Q、R 的符号各要一次条件取负,N、D 各要一次,四次里没有一次是多余的。 +%macro clb_divmod_signed 8 +.anchor before_clb_divmod_signed + +; ---- 1) 两个符号位:F = sign(N)、T1 = sign(D);N、D 原样还回去 +.tp %1 +rol +.tp %1 +bit_copy %8 %5+6 ; F = sign(N),N 保留(S+6 当缓冲,结束是干净的) +.tp %1 +ror +.tp %2 +rol +.tp %2 +bit_copy %6 %5+6 ; T1 = sign(D),D 保留 +.tp %2 +ror + +; ---- 2) |N|:负数取负;sign(N) 搬进 T2 存着(余数还要用) +.tp %8 +biz + clb_neg_ip %1 %5+6 ; N = |N| + clb_bit_mov %7 ; T2 = sign(N),F 归零(身体结束在条件格 F 上) +rnz + +; ---- 3) |D|:同上,sign(D) 搬进 F 存着 +.tp %6 +biz + clb_neg_ip %2 %5+6 ; D = |D| + clb_bit_mov %8 ; F = sign(D),T1 归零 +rnz + +; ---- 4) 绝对值上的无符号长除法 +clb_divmod %1 %2 %3 %4 %5 + +; ---- 5) 余数取被除数的符号;顺手把 F 变成商的符号 +.tp %7 +biz + clb_neg_ip %4 %6 ; R = -R(T1 这时是干净的) + flb ; 吃掉条件 T2 + .tp %8 + flb ; F = sign(D) ^ sign(N) + .tp %7 ; 身体回到条件格 T2 +rnz + +; ---- 6) 商取异或符号 +.tp %8 +biz + clb_neg_ip %3 %6 ; Q = -Q + flb ; 吃掉条件 F +rnz + +.tp before_clb_divmod_signed +%endmacro diff --git a/drv/digits_signed.r8asm b/drv/digits_signed.r8asm new file mode 100644 index 0000000..5bf919e --- /dev/null +++ b/drv/digits_signed.r8asm @@ -0,0 +1,22 @@ +%include "asm/digits.r8asm" + +; clb_digits_signed 的驱动:0 号 cell 放值,1..4 是数字区,5 起是 clb_digits 的 +; 13 格草稿,18 是 T,19 是符号标志 F。 +; +; r8forge_derive drv/digits_signed.r8asm -1 0=7 # 48 48 48 55 "0007" +; r8forge_derive drv/digits_signed.r8asm -1 0=2047 # 45 48 48 48 49 "-0001" +; r8forge_derive drv/digits_signed.r8asm -1 0=1024 # 45 49 48 50 52 "-1024" +; +; out 先可能是 45('-'),再是区域四格。全扫在 r8forge_byte_op_test 里。 +.var N 0 +.var REG 1 +.var S 5 +.var T 18 +.var F 19 + +clb_digits_signed N REG S T F + +output REG+0 +output REG+1 +output REG+2 +output REG+3 diff --git a/drv/divmod_signed.r8asm b/drv/divmod_signed.r8asm new file mode 100644 index 0000000..75e4a98 --- /dev/null +++ b/drv/divmod_signed.r8asm @@ -0,0 +1,23 @@ +%include "asm/divmod.r8asm" + +; clb_divmod_signed 的驱动:0 号 cell 放被除数、1 号放除数,输出商和余数。 +; +; r8forge_derive drv/divmod_signed.r8asm -2 0=7 1=2 # 3 1 +; r8forge_derive drv/divmod_signed.r8asm -2 0=2040 1=2 # 2044 2040? 不, +; r8forge_derive drv/divmod_signed.r8asm -2 0=(2048-7) 1=2 # -3 -1 = 2045 2047 +; +; out 依次是商、余数(二补码,所以 2045 就是 -3)。N、D 被吃成绝对值(加 -2 能看到), +; T1、T2 和 F 结束时都是 0。全扫在 r8forge_byte_op_test 里。 +.var N 0 +.var D 1 +.var Q 2 +.var R 3 +.var S 5 +.var T1 12 +.var T2 13 +.var F 14 + +clb_divmod_signed N D Q R S T1 T2 F + +output Q +output R diff --git a/examples/signed_demo.r8asm b/examples/signed_demo.r8asm new file mode 100644 index 0000000..581b86b --- /dev/null +++ b/examples/signed_demo.r8asm @@ -0,0 +1,59 @@ +%include "asm/digits.r8asm" + +; out 45 48 48 48 51 45 48 48 48 49 45 49 48 50 52 +; +; signed_demo —— 有符号数包装宏(SPEC §7.10)的一组用例,三段各打一行: +; +; -0003 clb_divmod_signed:-7 / 2 = -3(截断),商按二补码写成 2045 +; -0001 同一个除法的余数:余数跟被除数同号 +; -1024 clb_digits_signed:1024 的位型就是 -1024 +; +; 第三段是最容易写错的那个:**|N| 可能放得进无符号范围、但符号位是 1** +; (-1024 的绝对值就是 1024)。所以取绝对值只能走 clb_neg_ip,不能靠 +; "清掉第 W-1 位" —— 那种写法在 1024 上会打出 0。 +; +; r8forge examples/signed_demo.r8asm > t.rot8 +; r8forge_tape_run t.rot8 # out 45 48 48 48 51 45 48 48 48 49 45 49 48 50 52 +; +; 布局:用例自己带草稿区,所以宏之间互不干扰。 +.var N 0 ; clb_divmod_signed 的被除数 +.var D 1 ; 除数 +.var Q 2 ; 商 +.var R 3 ; 余数 +.var S 5 ; S..S+11:clb_divmod_signed 的 7 格草稿 + T1、T2、F +.var T1 12 +.var T2 13 +.var F 14 +.var REG 15 ; REG..REG+3:四位数字输出 +.var S2 20 ; S2..S2+12:clb_digits_signed 的 13 格草稿 +.var T 33 ; 取负用的草稿格 +.var F2 34 ; 符号标志格 + +; Macro show +; %1: Cell value +; +; 把一个值按有符号定宽四位打出来(负数先打 '-')。clb_digits_signed 进场要求 +; REG、S2、T 干净、F2 的最低位数是 0,所以每次都把 REG 和 S2 整块擦一遍。 +%macro show 1 +%for k 4 + erase REG + k +%endfor +%for k 13 + erase S2 + k +%endfor +clb_digits_signed %1 REG S2 T F2 +%for k 4 + output REG + k +%endfor +%endmacro + +; ---- 1) -7 / 2:商 -3、余数 -1 ---- +load N 2041 ; -7 = 2048 - 7 +load D 2 +clb_divmod_signed N D Q R S T1 T2 F +show Q ; -0003 +show R ; -0001 + +; ---- 2) 1024 这个位型 = -1024 ---- +load N 1024 +show N ; -1024 diff --git a/tests/byte_op_test.cc b/tests/byte_op_test.cc index e81af58..989c5eb 100644 --- a/tests/byte_op_test.cc +++ b/tests/byte_op_test.cc @@ -538,6 +538,107 @@ int test_digits(const std::string &file) { 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 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 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 @@ -660,6 +761,8 @@ int main(int argc, char **argv) { 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") +