Files
r8asm-rev-tests/tools/stdmac_clear_criterion.py
starcells e29db3cce2 按'只对声明位负责'的契约重判清零必要性(用户口径)
- 判定改为:A/Ci 保持、S 输出正确、Co&1 == 正确进位;Co 高位不判
- now(3 块) 0 失败 / D 0 / none 36-of-48 失败(A 被脏 S 带偏) / only3(只清 Sum) 0 失败
- 结论:Co/tmp 的清零不必要(Co 的 LSB 在体内从不是 biz 判定对象,只被写/被 clb_bit_mov 消费);
  Sum 那块必要(旋转累加器:未写位置必须为 0,脏 1 会转到 LSB 被当条件读 -> 污染 A/Ci/Co-LSB)
- only3: 1442 字节,比现役 1570 省 128
2026-09-13 04:51:17 +00:00

61 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""stdmac_clear_criterion.py — 按"只对声明的位负责"的契约重新判定清零必要性。
契约(用户口径):
- 宏只负责参数声明的位:Co/tmp 是 CellBit -> 只对 LSB 负责,高位残留不算错,
算完保留脏是契约内行为。
- 判据 = 计算是否受影响:A(整 Cell 输入,Ci 也是)必须保持原值,S 的输出必须是正确和,
Co 的 LSB 必须是正确进位。Co 的高位不参与判定。
跑:四变体(now / none / only3 / D)× 脏 S/Co/tmp × 两组 (A,Ci)。
"""
import itertools
import os
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, build_tools, run_case, set_cell # noqa: E402
build_tools()
FRAG = open(os.path.join(HERE, "tests", "inc_clear_alt.r8asm.frag")).read()
VARIANTS = ("now", "none", "only3", "D")
INPUTS = ((126, 1), (0, 1))
S_DIRTY = (0, 1, 170, 255)
CO_DIRTY = (0, 85, 255)
TMP_DIRTY = (0, 85)
def case(name, a, cin, s_in, co_in, tmp_in):
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)
print("判定: A==a 且 Ci==cin 且 S==(a+cin)&255 且 (Co&1)==carry [Co 高位不算]")
print("%-6s | 计算受影响例数 / %d" % ("变体", len(INPUTS) * len(S_DIRTY) * len(CO_DIRTY) * len(TMP_DIRTY)))
print("-" * 70)
for name in VARIANTS:
bad = []
for (a, cin), s_in, co_in, tmp_in in itertools.product(INPUTS, S_DIRTY, CO_DIRTY, TMP_DIRTY):
got = case(name, a, cin, s_in, co_in, tmp_in)
tot = a + cin
ok = (got[0] == a and got[1] == cin and got[2] == (tot & 0xFF)
and (got[3] & 1) == (tot >> 8))
if not ok:
bad.append(((a, cin, s_in, co_in, tmp_in), got))
print("%-6s | %-3d %s" % (name, len(bad),
"" if not bad else "例:" + "; ".join(
"S脏%d,Co脏%d->%s" % (d[2], d[3], g) for d, g in bad[:2])))
print("\n=== only3 放宽判定下的全表(干净入口)复核 ===")
VALS = [0, 1, 2, 3, 5, 126, 127, 128, 170, 254, 255]
bad = []
for a, cin in itertools.product(VALS, [0, 1]):
got = case("only3", a, cin, 0, 0, 0)
if got != [a, cin, (a + cin) & 0xFF, (a + cin) >> 8]:
bad.append((a, cin, got))
print("only3 干净入口失败:", len(bad), bad[:3])