清零必要性拆解:干净入口下清零是死代码,现役 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 字节
This commit is contained in:
starcells committed 2026-09-13 04:43:16 +00:00
1 parent 4c027a8082
commit 75376b5927
4 files changed
+1157

No files matched your search

+314
View File
@@ -0,0 +1,314 @@
%macro bit_raw_inc_now 5
.anchor before_bit_raw_inc_now
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %5
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_now
%endmacro
%macro raw_inc_now 5
.anchor before_raw_inc_now
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_now %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_raw_inc_now
%endmacro
%macro bit_raw_inc_none 5
.anchor before_bit_raw_inc_none
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_none
%endmacro
%macro raw_inc_none 5
.anchor before_raw_inc_none
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_none %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_raw_inc_none
%endmacro
%macro bit_raw_inc_only3 5
.anchor before_bit_raw_inc_only3
.tp %3
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_only3
%endmacro
%macro raw_inc_only3 5
.anchor before_raw_inc_only3
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_only3 %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_raw_inc_only3
%endmacro
%macro bit_raw_inc_D 5
.anchor before_bit_raw_inc_D
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_D
%endmacro
%macro raw_inc_D 5
.anchor before_raw_inc_D
erase %3
erase %4
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_D %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_raw_inc_D
%endmacro
+636
View File
@@ -0,0 +1,636 @@
%macro bit_raw_inc_none 5
.anchor before_bit_raw_inc_none
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_none
%endmacro
%macro raw_inc_none 5
.anchor before_raw_inc_none
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_none %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_raw_inc_none
%endmacro
%macro bit_raw_inc_3 5
.anchor before_bit_raw_inc_3
.tp %3
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_3
%endmacro
%macro raw_inc_3 5
.anchor before_raw_inc_3
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_3 %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_raw_inc_3
%endmacro
%macro bit_raw_inc_4 5
.anchor before_bit_raw_inc_4
.tp %4
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_4
%endmacro
%macro raw_inc_4 5
.anchor before_raw_inc_4
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_4 %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_raw_inc_4
%endmacro
%macro bit_raw_inc_5 5
.anchor before_bit_raw_inc_5
.tp %5
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_5
%endmacro
%macro raw_inc_5 5
.anchor before_raw_inc_5
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_5 %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_raw_inc_5
%endmacro
%macro bit_raw_inc_34 5
.anchor before_bit_raw_inc_34
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_34
%endmacro
%macro raw_inc_34 5
.anchor before_raw_inc_34
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_34 %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_raw_inc_34
%endmacro
%macro bit_raw_inc_35 5
.anchor before_bit_raw_inc_35
.tp %3
biz
flb
rnz
.tp %5
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_35
%endmacro
%macro raw_inc_35 5
.anchor before_raw_inc_35
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_35 %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_raw_inc_35
%endmacro
%macro bit_raw_inc_45 5
.anchor before_bit_raw_inc_45
.tp %4
biz
flb
rnz
.tp %5
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_45
%endmacro
%macro raw_inc_45 5
.anchor before_raw_inc_45
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_45 %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_raw_inc_45
%endmacro
%macro bit_raw_inc_345 5
.anchor before_bit_raw_inc_345
.tp %3
biz
flb
rnz
.tp %4
biz
flb
rnz
.tp %5
biz
flb
rnz
.tp %2
biz
.tp %1
flb
.tp %3
flb
.tp %2
flb
rnz
.tp %1
biz
.tp %4
set_1
.tp %5
set_1
.tp %1
flb
rnz
.tp %3
biz
.tp %1
set_1
.tp %2
set_1
.tp %4
set_1
.tp %3
flb
rnz
.tp %5
biz
.tp %1
flb
.tp %3
flb
.tp %4
flb
.tp %5
flb
rnz
.tp before_bit_raw_inc_345
%endmacro
%macro raw_inc_345 5
.anchor before_raw_inc_345
.tp %2
bit_copy %5 %4
.tp %5
ror
%rep _CELL_SIZE
bit_raw_inc_345 %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_raw_inc_345
%endmacro
+102
View File
@@ -0,0 +1,102 @@
#!/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)))
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env python3
"""stdmac_clear_necessity.py — bit_raw_inc 三个入口清零块的必要性拆解。
做法:从核心仓现役 bit_raw_inc 体内抽出三个清零块(清 %3=Sum / %4=Co / %5=tmp),
按 2^3 组合生成变体,分别跑:
(a) 干净入口的全表(22 例)—— 体检清零是否影响正常路径
(b) 脏入口扫描(一次弄脏一个 cell)—— 看 A/Ci 会不会被带偏
"""
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 # 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 = {}
for n in ("4", "5"):
blk = ".tp %" + n + "\nbiz\n flb\nrnz\n\n"
assert blk in rest, "找不到清 %" + n + " 的块"
rest = rest.replace(blk, "", 1)
CLEARS[n] = blk
CLEARS["3"] = ".tp %3\nbiz\n flb\nrnz\n\n"
core = rest.strip("\n") + "\n"
GLUE = """
%macro {glue} 5
.anchor before_{glue}
.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
"""
VARIANTS = {}
for k in range(4):
for keep in itertools.combinations("345", k):
name = "".join(keep) or "none"
bit = "bit_raw_inc_" + name
glue = "raw_inc_" + name
macro_body = core.replace("bit_raw_inc", bit)
VARIANTS[name] = (
"%macro " + bit + " 5\n"
".anchor before_" + bit + "\n"
+ "".join(CLEARS[x] for x in keep)
+ macro_body
+ "\n%endmacro\n"
+ GLUE.format(glue=glue, bit=bit)
)
FRAG = "".join(VARIANTS.values())
open(os.path.join(HERE, "tests", "inc_clear_variants.r8asm.frag"), "w").write(FRAG)
DIRTY = ((0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (170, 85, 85))
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):
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("清零块组合:3=清 Sum、4=清 Co、5=清 tmp(不写=该块已删)\n")
print("%-6s | %-14s | 脏入口(A=126 Ci=1,期望 126,1,127,0)" % ("保留", "干净入口全表"))
print("-" * 88)
summary = {}
for name in sorted(VARIANTS, key=lambda n: (len(n), n)):
bad = []
for a, cin in itertools.product(VALS, [0, 1]):
if case(name, a, cin) != [a, cin, (a + cin) & 0xFF, (a + cin) >> 8]:
bad.append((a, cin))
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))
summary[name] = (len(bad), len(dirty_bad))
detail = "; ".join("脏%s->%s" % (d, g) for d, g in dirty_bad[:2]) or "全过"
print("%-6s | %-14s | %s" % (name if name != "none" else "无", "全过" if not bad else "错 %d" % len(bad), detail))
print("\n干净+脏入口都过的组合:", ", ".join(sorted(n for n, (b, d) in summary.items() if b == 0 and d == 0)) or "(无)")