实测(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 字节
106 lines
3.5 KiB
Python
106 lines
3.5 KiB
Python
#!/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 "(无)")
|