Files
R8Forge/asm/test.r8asm
T
dsh 6e67eb4678 The multiplier: shift and add, one bit at a time, 3977 characters
clb_mul was repeated addition -- add MC into the product, MUL times.  That is
the shortest thing to write, but the runtime is proportional to MUL's value:
255 * 255 took 733428 steps, and 2047 * 2047 ran into the 5000000 step ceiling
and hung outright (tape_run: HANG after 5000000 steps).  So the multiplier was
a ceiling on what any program could compute.

The new body is the plain binary multiplication from school: hold the
multiplicand, look at the multiplier's low bit, add the multiplicand into the
high half of the product when that bit is 1, then shift the whole product one
place right -- the multiplier lives in the low half of that register, so its
bits fall out one at a time.  The loop runs _CELL_SIZE rounds whatever the
operands are, and the tape got shorter as well:

    82 * 141     358586 -> 8374 steps
    255 * 255    733428 -> 17785 steps
    2047 * 2047      HANG -> 26123 steps
    tape           5007 -> 3977 characters

(Step counts are the tape's own, with no trailing newline -- tape_run counts
one if the file has one, which is where the first draft of this message got
numbers two out.)

Counter, carry and cross cell shift are the three pieces that needed care:

  * the round counter is _CELL_SIZE ones in NR, one eaten per round by
    set_0 + ror -- the same trick clb_inc_ip counts rotations with, so "how
    many rounds" costs no tape length, only runtime;
  * the carry out of the W bit add is not scratch: it is the top bit of the
    2W+1 bit shift register, and the next shift moves it into PH's top bit;
  * (F:PH:PL) >>= 1 is a cross cell shift.  ror all three, bring the bit to
    inject down to bit 0 with rol, move it with clb_bit_mov, ror back.  The
    low pair (PH -> PL) moves first, otherwise PH's bit 0 has already been
    overwritten by the carry.

And biz/rnz is a loop pair, not an if/else: a one shot conditional has to eat
the bit it tested (a trailing flb) or rnz jumps straight back.  The first
version of this hung exactly there -- no output at all, counter still 0x7ff.
The bit eaten is PL's low bit, which the shift discards anyway.

r8forge_byte_op_test now sweeps 0..255 x 0..255 exhaustively (65536/65536 ok,
6.7s instead of 1.5s) instead of sampling 64 combinations: the cost no longer
grows with the multiplier, so sampling was the only reason not to.  asm/
test.r8asm still prints 1322 1786 1600 5 94 127, now as 28729 characters and
57330 steps (was 31901 and about two million), and derive and tape_run agree.

AGENTS.md and README record all of it, plus one correction: the old note said
shift and add had been dismissed because "only tape length is the target".  It
is in fact 1000 characters shorter, so the reason given was wrong -- the
tradeoff was not.  What changed is the target function: the 5000000 step
ceiling is part of it, and a loop whose length does not depend on its operands
can still have a runtime that does.
2026-11-07 12:09:45 +08:00

111 lines
2.6 KiB
Plaintext

; 一个 hash:把消息当成多项式滚一遍,每步 h = (h + c) * 141 mod 2048。
;
; 141 是奇数,所以乘它在 mod 2^11 上可逆 —— 每个输入位都能影响到结果,
; 这就是 hash 要的那种"雪崩"的弱化版(11 位也别指望真雪崩)。
; 为什么不用 FNV 那个 403:那是老乘法器的遗留 —— 老的 clb_mul 是重复加法,
; 轮数 = MUL 的值,403 放 MUL 会顶到 500 万步上限(141 那版三轮约 207 万步)。
; 现在的 clb_mul 是移位-加法,轮数只跟位宽有关(MC=100、MUL=403 实测 10210 步),
; 乘数多大都不再影响能不能跑完;这里留着 141 只是为了跟以前的结果对得上。
; **hash 的强度花在运行时间上,纸带长度完全不变** —— 这就是本仓库的取舍。
;
; 只用两样东西:clb_add_nc(加)和 clb_mul(乘)。没有位运算,
; "会加减乘就能算 hash"的最小证明。
;
; 消息是 "R8F" = 82 56 70(ASCII 高位置 0,11 位 cell 正好装得下)。
; 手算:
; h1 = (0 + 82) * 141 mod 2048 = 1322
; h2 = (1322 + 56) * 141 mod 2048 = 1786
; h3 = (1786 + 70) * 141 mod 2048 = 1600
%include "asm/raw_add_nc.r8asm"
%include "asm/mul.r8asm"
; h 的初值就是 cell 的初值 0,不用写
.var H 0
.var C1 2
.var C2 4
.var C3 6
.var S1 8
.var S2 10
.var S3 12
; 进位输入:clb_add_nc 不动它的初值,给 0 就是普通加法
.var Ci1 14
.var Ci2 16
.var Ci3 18
; 三轮乘法各用一组 cell,互不打扰
.var MC1 20
.var MU1 21
.var PL1 22
.var PH1 23
.var MCA1 24
.var MS1 25
.var MCi1 26
.var MT1 27
.var MF1 28
.var MNR1 29
.var MC2 30
.var MU2 31
.var PL2 32
.var PH2 33
.var MCA2 34
.var MS2 35
.var MCi2 36
.var MT2 37
.var MF2 38
.var MNR2 39
.var MC3 40
.var MU3 41
.var PL3 42
.var PH3 43
.var MCA3 44
.var MS3 45
.var MCi3 46
.var MT3 47
.var MF3 48
.var MNR3 49
; 消息和乘数放进 cell
.tp C1
.xor 82
.tp C2
.xor 56
.tp C3
.xor 70
.tp MU1
.xor 141
.tp MU2
.xor 141
.tp MU3
.xor 141
; 第一轮:h = (0 + 82) * 141
clb_add_nc H C1 S1 Ci1
clb_mul S1 MU1 PL1 PH1 MCA1 MS1 MCi1 MT1 MF1 MNR1
output PL1
; 第二轮:h = (1322 + 56) * 141
clb_add_nc PL1 C2 S2 Ci2
clb_mul S2 MU2 PL2 PH2 MCA2 MS2 MCi2 MT2 MF2 MNR2
output PL2
; 第三轮:h = (1786 + 70) * 141
clb_add_nc PL2 C3 S3 Ci3
clb_mul S3 MU3 PL3 PH3 MCA3 MS3 MCi3 MT3 MF3 MNR3
output PL3
; 顺便把三轮的 PH 打出来:乘法器给的是 (PH:PL) 两格的积,
; PH 就是 11 位装不下、进到第二个 cell 的那一半,等于 (h+c)*141 >> 11。
; 这一版三轮分别是 5 94 127 —— 也就是说中间结果其实都是超出 2047 的,
; 只是取模之后落在了 PL 里。
output PH1
output PH2
output PH3