Files
r8asm-rev-tests/tools/stdmac_clear_alt.py
T
starcells 75376b5927 清零必要性拆解:干净入口下清零是死代码,现役 3 块 LSB 清既贵又不完整;变体 D 最优
实测(HEAD 4014a8f):
- 干净入口:now/none/only3/D 四变体全表 22/22 全过 -> 每比特清零在正常路径上是死代码
- 脏入口:清 %3 是真必需(Sum 随 A 旋转,残留位转到 LSB 被 biz 当条件读 -> A_out=A_in^S_in);
  清 %4 的 LSB 挡不住高位残留(Co 不旋转,bit1..7 无人碰,入口 85 -> 输出 Co=84);
  清 %5 无实测作用(glue 里 bit_copy 已把它当 dst 清过)
- tape 长度:now 1570 / none 1414 / only3 1442 / D 1506
- 变体 D(体里全删 + glue 进循环前 erase S + erase Co):全表 x 脏 S/Co 组合 0 失败,且比现役短 64 字节
2026-09-13 04:43:16 +00:00

103 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""stdmac_clear_alt.py — 清零的另一种做法对拍。
对比四种入口清理策略:
now = 现役 HEAD 4014a8f(每个比特清 %3/%4/%5 的 LSB,共 3 块)
none = 全删(靠调用契约)
only3 = 只留清 %3(Sum 跟着 A 旋,残留位会逐个转到 LSB 当条件读)
D = 体里全删,改为调用点进循环前 `erase S` + `erase Co`(整 cell 清一次)
测:干净全表 22 + 脏入口扫描 + 实际 tape 长度。
"""
import itertools
import os
import re
import sys
HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(HERE, "tools"))
from stdmac_matrix import PRELUDE, REV, build_tools, run_case, set_cell, PROBE # noqa: E402
raw = open(os.path.join(REV, "asm", "raw_inc.r8asm")).read()
body = re.search(r"%macro bit_raw_inc 5\n(.*?)\n%endmacro", raw, re.S).group(1)
_, rest = body.split(".tp %3\nbiz\n flb\nrnz\n", 1)
CLEARS = {"3": ".tp %3\nbiz\n flb\nrnz\n\n"}
for n in ("4", "5"):
blk = ".tp %" + n + "\nbiz\n flb\nrnz\n\n"
rest = rest.replace(blk, "", 1)
CLEARS[n] = blk
core = rest.strip("\n") + "\n"
GLUE = """
%macro {glue} 5
.anchor before_{glue}
{pre}
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
{bit} %1 %2 %3 %4 %5
.tp %4
clb_bit_mov %2
.tp %1
ror
.tp %3
ror
%endrep
.tp %2
clb_bit_mov %4
.tp %5
rol
clb_bit_mov %2
.tp before_{glue}
%endmacro
"""
SPECS = (("now", "345", ""),
("none", "", ""),
("only3", "3", ""),
("D", "", "erase %3\nerase %4\n"))
FRAG = ""
for name, keep, pre in SPECS:
bit = "bit_raw_inc_" + name
FRAG += ("%macro " + bit + " 5\n.anchor before_" + bit + "\n"
+ "".join(CLEARS[x] for x in keep) + core.replace("bit_raw_inc", bit)
+ "\n%endmacro\n" + GLUE.format(glue="raw_inc_" + name, bit=bit, pre=pre))
open(os.path.join(HERE, "tests", "inc_clear_alt.r8asm.frag"), "w").write(FRAG)
DIRTY = ((0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (170, 85, 85), (255, 255, 255))
VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255]
def case(name, a, cin, s_in=0, co_in=0, tmp_in=0, tape_path=None):
src = PRELUDE + FRAG + set_cell("A", a) + set_cell("Ci", cin)
src += ".tp S\n.xor %d\n.tp Co\n.xor %d\n.tp tmp\n.xor %d\n" % (s_in, co_in, tmp_in)
src += "raw_inc_%s A Ci S Co tmp\noutput A\noutput Ci\noutput S\noutput Co\n" % name
return run_case(src)
build_tools()
print("%-6s | %-12s | %-34s | tape 字节" % ("变体", "干净全表", "脏入口失败(期望 126,1,127,0)"))
print("-" * 78)
for name, _, _ in SPECS:
bad = [c for c in itertools.product(VALS, [0, 1])
if case(name, c[0], c[1]) != [c[0], c[1], (c[0] + c[1]) & 0xFF, (c[0] + c[1]) >> 8]]
dirty_bad = []
for (s_in, co_in, tmp_in) in DIRTY:
got = case(name, 126, 1, s_in, co_in, tmp_in)
if got != [126, 1, 127, 0]:
dirty_bad.append(((s_in, co_in, tmp_in), got))
# tape 长度:同一段程序的汇编产物
src = PRELUDE + FRAG + "raw_inc_%s A Ci S Co tmp\n" % name
import subprocess, tempfile
with tempfile.NamedTemporaryFile("w", suffix=".r8asm", delete=False, dir=REV) as f:
f.write(src)
p = f.name
out = subprocess.run([PROBE, p], cwd=REV, capture_output=True, text=True).stdout
os.unlink(p)
detail = "; ".join("%s->%s" % (d, g) for d, g in dirty_bad[:2]) or "全过"
print("%-6s | %-12s | %-34s | %d" % (name, "全过" if not bad else "错 %d" % len(bad), detail, len(out)))