raw_sub: a subtraction that keeps its operands
clb_sub_nc eats A and B, so the repeated subtraction divider has to copy both every round (two 495 character copies, about five hundred steps each) -- that is where clb_divmod's 1.48 million steps for 2047 comes from. Binary long division compares against the same divisor every round and cannot afford that, so it needs a non destructive subtract first. raw_sub A B S Bo Bc Ci buf is three useful lines: copy ~B, set Ci = 1, then raw_add (which already preserves its operands). S = A + ~B + 1 = A - B, and A and B are untouched. About 2900 characters, 7 cells, 9 sampled cases correct. One trap measured: do not flip Bo at the end to turn a carry into a borrow -- clb_not_ip complements the whole cell, so 0 becomes 2047 and 1 becomes 2046. Bo keeps the library's existing meaning instead: 1 means A >= B (no borrow), same as clb_sub_nc's carry. This is step one of replacing clb_divmod's O(N/D) loop with shift-and-restore division at O(cell_size) rounds (11 instead of 205 for 2047, so roughly forty times fewer steps for maybe a few hundred extra characters of tape).
This commit is contained in:
1 parent
91e3fed537
commit
9a9ca48d9a
4 files changed
+83
-1
No files matched your search
@@ -214,6 +214,7 @@ inline constexpr uint32_t kDefaultCellMask = (1u << kDefaultCellSize) - 1u;
|
||||
| **`clb_ones`(数量 → 从末位起 N 个 1,4 cell)** | `drv/ones.r8asm` | **742 字符**(带 output)/ **828**(最小驱动) | **12 个计数全对**(N=0..11) |
|
||||
| **`clb_divmod`(N div D / N mod D,7 cell)** | `drv/divmod.r8asm` | **4189 字符**(最小驱动)/ 4195(带 output) | **76 组抽样全对**(O(N/D) 运行,`2047/1` 会撞步数上限) |
|
||||
| **`clb_digits`(整数 → 4 位 ASCII 十进制,16 cell)** | `drv/digits.r8asm` | **10829 字符**(最小驱动)/ 13743(带 output) | **11 个值全对**(0/7/10/99/100/512/1000/1234/2047 等) |
|
||||
| **`raw_sub`(A-B,**保留 A、B**,Bo = 没借位,7 cell)** | `drv/raw_sub.r8asm` | **≈2900 字符** | **9 组抽样全对**;长除法要用它(`clb_sub_nc` 会把操作数吃掉) |
|
||||
| `asm/fibonacci.r8asm` | — | 11959 字符 | 跑出 13 项 `1 1 2 3 5 8 13 21 34 55 89 144 233` |
|
||||
|
||||
`raw_*` 保留操作数(A、B 原样还回来),`clb_*_nc` 把 A、B 吃干净 —— 所以后者能省掉
|
||||
|
||||
@@ -440,7 +440,6 @@ A 的 12 个取值全对(`r8forge_byte_op_test` 的 `test_ones`)。
|
||||
除零没有定义,调用者保证 `D >= 1`。
|
||||
|
||||
### 7.7 `clb_digits`:整数转十进制字符串
|
||||
|
||||
`clb_digits N REG S`:把 N 展开成 **`REG..REG+3` 四个连续 cell,每格一个 ASCII 数字
|
||||
(`'0'..'9'`),高位在前、定长 4 位、左边补 `'0'`**(11 位 cell 最大 2047,正好 4 位)。
|
||||
N 被吃干净;`REG` 起 4 格、`S` 起 8 格必须连续且干净。纸带 **10829 字符**(最小驱动),
|
||||
@@ -470,6 +469,24 @@ N 被吃干净;`REG` 起 4 格、`S` 起 8 格必须连续且干净。纸带 *
|
||||
* **造常数不能分两次"置位再转"**:第二次旋转会把第一次放好的位一起带走。正确做法是
|
||||
**绕整圈走一次**(`W` 次 `ror`,转到目标位就 `set_1`,最后转回原位)。
|
||||
|
||||
### 7.8 `raw_sub`:保留操作数的减法(长除法的地基)
|
||||
|
||||
`raw_sub A B S Bo Bc Ci buf`:**`S = A - B`,而且 A、B 都原样还回来**,
|
||||
`Bo` 是"没有借位"(1 表示 `A >= B`,和 `clb_sub_nc` 的进位一致)。
|
||||
7 个 cell,纸带 **2900 字符**左右,9 组抽样全对。
|
||||
|
||||
做法就是把 `raw_add`(它本来就保留操作数)套上补码:`S = A + ~B + 1`,
|
||||
进位输出就是"没借位"。所以整个宏只有三行有用的话:copy 出 `~B`、`Ci = 1`、
|
||||
`raw_add`。
|
||||
|
||||
**踩到的坑**:别在最后 `clb_not_ip Bo` 想"取反得到借位" —— `clb_not_ip` 是
|
||||
整格取反,0 会变成 2047、1 会变成 2046(实测),不是 0/1。要用"没借位"这个
|
||||
现成语义,和 `clb_sub_nc` 保持一致。
|
||||
|
||||
存在的理由:`clb_sub_nc` 会吃掉 A 和 B,所以"重复减法"的除法器每轮都得 copy 两个
|
||||
副本;二进制长除法每轮都要拿同一个除数去比/减,必须有个不破坏操作数的减法。
|
||||
这是把 `clb_divmod` 从 O(N/D) 换成 O(位宽) 的第一步。
|
||||
|
||||
---
|
||||
|
||||
## 8. 原地 `_ip` 家族(结果回到原来的 cell)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
%include "asm/raw_add.r8asm"
|
||||
%include "asm/not_ip.r8asm"
|
||||
|
||||
; 保留操作数的减法:S = A - B,而且 **A、B 都不动**,另外给出借位 Bo。
|
||||
;
|
||||
; 为什么需要它:`clb_sub_nc` 会把 A 和 B 都吃干净,所以"重复减法"的除法器
|
||||
; 每一轮都得 copy 出两个副本(每个 495 字符、每次约五百步),这也是
|
||||
; `clb_divmod` 跑到 2047 要 148 万步的原因。二进制长除法每一轮都要拿同一个
|
||||
; 除数去比/减,必须有个"不破坏操作数"的减法。
|
||||
;
|
||||
; 做法就是把 `raw_add`(它本来就保留 A、B)套上补码:
|
||||
;
|
||||
; S = A + ~B + 1 = A - B
|
||||
;
|
||||
; 于是进位输出 Co 就是"没有借位"——`clb_sub_nc` 也是这样,但那个是破坏性的。
|
||||
; 本宏把 Co 取反留在 Bo 里,读起来更顺:**Bo = 1 表示 A < B**。
|
||||
;
|
||||
; 前提:A、B 的值 < 2^_CELL_SIZE(B 取反后和 A 相加不会溢出到第 W+1 位以外的解释)。
|
||||
|
||||
; Macro raw_sub
|
||||
; %1: Cell A
|
||||
; %2: Cell B
|
||||
; %3: Cell S
|
||||
; %4: Cell Bo
|
||||
; %5: Cell Bc
|
||||
; %6: Cell Ci
|
||||
; %7: Cell buf
|
||||
;
|
||||
; S = A - B(mod 2^W),A、B 保持原样,Bo = (A < B ? 1 : 0)。
|
||||
; Bc、Ci、buf 是草稿(进来要干净),结束时 Ci 和 buf 是垃圾、Bc 是 ~B。
|
||||
%macro raw_sub 7
|
||||
.anchor before_raw_sub
|
||||
|
||||
; Bc = ~B
|
||||
.tp %2
|
||||
copy %5 %7
|
||||
clb_not_ip %5
|
||||
|
||||
; Ci = 1,做的是"加补码"
|
||||
.tp %6
|
||||
set_1
|
||||
|
||||
; S = A + Bc + 1,进位出到 Bo
|
||||
raw_add %1 %5 %6 %3 %4 %7
|
||||
|
||||
; 注意:这里**不能**再 `clb_not_ip %4`。`clb_not_ip` 是整格取反(~carry),
|
||||
; 得到的是 2046/2047 而不是 0/1(实测)。所以 Bo 和 `clb_sub_nc` 的进位一样,
|
||||
; 语义是"**没有借位**":Bo = 1 表示 A >= B。
|
||||
|
||||
.tp before_raw_sub
|
||||
%endmacro
|
||||
@@ -0,0 +1,13 @@
|
||||
%include "asm/raw_sub.r8asm"
|
||||
.var A 0
|
||||
.var Bc 1
|
||||
.var B 2
|
||||
.var Ci 3
|
||||
.var S 4
|
||||
.var Bo 5
|
||||
.var buf 6
|
||||
raw_sub A B S Bo Bc Ci buf
|
||||
output A
|
||||
output B
|
||||
output S
|
||||
output Bo
|
||||
Reference in new issue
Block a user