docs: record the signed number plan in AGENTS, SPEC and TODO
The four decisions are settled and the work is not done, so each fact goes where it
belongs:
SPEC 5.4 two complement, the sign bit is _CELL_SIZE-1 (-1024..1023 at 11 bits,
-128..127 at 8 bits, symmetric, so no |MIN| trap), display -0042,
truncating division (the remainder takes the sign of the dividend),
silent wraparound. Addition, subtraction and multiplication need no
change: the arithmetic is already modulo 2^W.
TODO three independently verifiable steps -- clb_digits_signed (sign test
via clb_sub_nc against HALF = 2^(_CELL_SIZE-1), negate a copy, then the
existing digits), clb_divmod_signed (magnitudes, unsigned divide, signs
reapplied), and the calculator unary minus when HAVE is clear. Each
step lists its acceptance sweep.
AGENTS a short pointer so the next session sees the big queued thing.
The width trap is called out in all three: HALF must be %rep _CELL_SIZE; a literal
11 would misread the sign bit on an 8 bit machine.
This commit is contained in:
1 parent
3ac4ede73d
commit
723b8285dd
3 files changed
+42
No files matched your search
@@ -118,8 +118,15 @@ powershell -File tests\widths.ps1 # 位宽矩阵:每个位宽一
|
||||
* **字节级程序"跑不动"先怀疑虚拟机**:仓库里曾有三份解释器把 `biz` 写成整格零判断,于是
|
||||
所有字节级程序看起来都在死循环 —— 别再把库判成坏的。
|
||||
|
||||
## 下一件大事:有符号数
|
||||
|
||||
约定已定(二补码、`-0042` 显示、截断除法、静默溢出),见 SPEC §5.4;三个包装宏的清单
|
||||
和每步验收在 [`TODO.md`](TODO.md) 的"有符号数(三步走)"。**加、减、乘不用改**,缺的是
|
||||
带符号显示、带符号除法、以及计算器里的一元负号。
|
||||
|
||||
## 下一步
|
||||
|
||||
|
||||
候选、想清楚但没做的优化、以及工具侧的小尾巴都收在 [`TODO.md`](TODO.md);
|
||||
`SPEC.md` §12 是"别试"的结论,不是待办。
|
||||
|
||||
|
||||
@@ -353,6 +353,22 @@ endloop C
|
||||
`ABCDEFGH`)。**它不在 `asm/test.r8asm` 里**:MAX=8 的版本要三万多字符,而全家桶离 65536
|
||||
只剩约一千。
|
||||
|
||||
|
||||
### 5.4 有符号数(约定已定,实现待做)
|
||||
|
||||
表示法用**二补码**:值 `v` 为负当且仅当第 `_CELL_SIZE-1` 位是 1(11 位时 1024..2047 =
|
||||
−1024..−1,8 位时 128..255 = −128..−1)。范围对称,所以不存在"最小负数取反溢出"。
|
||||
|
||||
四条已定的约定:
|
||||
|
||||
* **加、减、乘不需要改**:模 2^W 运算天然按二补码正确。
|
||||
* **显示**:`-0042`,即字符 45 加现有的定宽补零数字;正数不打印加号。
|
||||
* **除法**:截断(C 语义)—— `q = trunc(a/b)`,`r = a - q*b`,余数与被除数同号。
|
||||
* **溢出**:静默回绕,不显示错误。
|
||||
|
||||
待做的是三个包装宏(`clb_digits_signed`、`clb_divmod_signed`、计算器里的一元负号),
|
||||
清单和验收在 [`TODO.md`](TODO.md)。
|
||||
|
||||
## 6. 位级宏(一个 cell 装一位)
|
||||
|
||||
### 6.1 总表
|
||||
|
||||
@@ -7,8 +7,27 @@
|
||||
已经做完、从这份清单里移出去的:桌上计算器(`examples/calc.r8asm`,11 位 62585 字符、三方
|
||||
一致、位宽矩阵见 `tests/widths.ps1`)、`INP` 整格写入的语义修正、参考虚拟机支持 >64k 纸带。
|
||||
|
||||
## 有符号数(三步走,约定见 SPEC §5.4)
|
||||
|
||||
表示法、显示格式、除法语义、溢出行为都已经定死,剩下三个**可独立验收**的步骤:
|
||||
|
||||
1. **`clb_digits_signed N REG S T F`**:`T` = |N| 的副本,`F` = 符号位。判负用
|
||||
`clb_sub_nc T HALF`(`HALF = 2^(_CELL_SIZE-1)`,**必须 `%rep _CELL_SIZE`,写死 11 会
|
||||
在 8 位机器上把符号位判错**),负数先 `output 45` 再对副本 `clb_neg_ip`,最后走现有
|
||||
`clb_digits`。验收:2048 个值全扫,期望串在 C++ 里按 `x >= 2^(W-1) ? x - 2^W : x` 算,
|
||||
`2047 -> -0001`、`1024 -> -1024`、`0 -> 0000`。
|
||||
2. **`clb_divmod_signed N D Q R S T1 T2 F`**:取符号 → 各自绝对值 → 调 `clb_divmod` →
|
||||
商的符号按异或、余数取被除数的符号(截断语义自动成立)。验收:`|a|,|b| <= 32` 全组合
|
||||
+ 边界(0、正负 1、正负 1023、正负 1024),与 C 的除法和取余逐项比。
|
||||
3. **计算器的一元负号**:字符 45 在 `HAVE == 0` 时置 `NEG` 标志,flush 时对
|
||||
`clb_undigits` 的结果取反;`HAVE == 1` 时仍是减号。验收:`3 5 - .` -> `-0002`、
|
||||
`-5 2 * .` -> `-0010`、`-7 -2 / .` -> `0003`。
|
||||
|
||||
每步做完都能停在可用状态(第 1 步之后 `clb_digits_signed` 就能单独用)。
|
||||
|
||||
## 交互计算(下一步)
|
||||
|
||||
|
||||
* **RPN 栈**:现在的计算器是两寄存器(`A` 累加器 + `P` 待结算操作数),**没有栈**。
|
||||
`.tp` 是汇编期常量,所以只能做"固定 N 格 + 整体挪位"的假栈:每次 push ≈ N 个 `copy`
|
||||
(约 500 字符/格)。65536 的纸带预算下 **16 深**可行(~12k 字符),4 深最划算;理论上
|
||||
|
||||
Reference in new issue
Block a user