code wiki / _hdl_build / nx_mulchain_deep_test.nx
nx_mulchain_deep_test.nx source
↩ module page · 61 lines · 3432 B
1// nx_mulchain_deep_test.nx -- GATE for the deepened, cost-bounded chain search (the escalation
2// resolution). Proves three things, exit 0 only if ALL hold:
3// (A) COST SELECTION is correct under a LATENCY model (cap=5, tie_thresh=3): constants with a
4// <=3-op chain pick the chain; constants needing >3 ops (incl. the escalated 466/683/691)
5// and primes with no short chain pick IMUL. This is the per-constant cost decision that
6// replaces the blunt depth-4 "expensive -> escalate".
7// (B) every chain the selector keeps is VERIFIED to compute c*x on a fresh held-out input
8// (independent of the search's own check) -- verify, don't trust.
9// (C) TRACTABLE: the run completes (the primes that hung at depth 6/12 fail-fast at the cap).
10// Memory-backed verdict tally in small helpers (mitigates the non-deterministic compiler's
11// fat-main register clobber). license_tier: ORIGINAL
12
13import "nx_mulchain_deep.nx"
14import "nx_syscalls.nx"
15
16func dt_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func dt_num(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=48+(m%10); m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(1, bb, k); return 0 }
18
19// one cost-selection check: returns 1 if mc_cost_select(c) == expect AND (if a chain was kept)
20// it actually computes c*x on a fresh input; else 0.
21func dt_check(c: i64, expect: i64) -> i64 {
22 let op: *i64 = sys_mmap(8*20) as *i64; let a: *i64 = sys_mmap(8*20) as *i64; let b: *i64 = sys_mmap(8*20) as *i64
23 let got: i64 = mc_cost_select(c, 5, 3, op, a, b)
24 var ok: i64 = 1
25 if got != expect { ok = 0 }
26 if got > 0 { // a chain was kept -> independently verify it
27 let x: i64 = 20577
28 if mc_eval(op, a, b, got, x) != c * x { ok = 0 }
29 }
30 dt_puts(" c=" as *u8); dt_num(c); dt_puts(" select=" as *u8); dt_num(got)
31 dt_puts(" expect=" as *u8); dt_num(expect)
32 if ok == 1 { dt_puts(" OK\n" as *u8) } else { dt_puts(" FAIL\n" as *u8) }
33 return ok
34}
35
36func main() -> i64 {
37 dt_puts("=== deepened + cost-bounded chain search (LATENCY model: cap=5, tie<=3) ===\n" as *u8)
38 let cs: *i64 = sys_mmap(8*16) as *i64
39 let ex: *i64 = sys_mmap(8*16) as *i64
40 // chain-wins (<=3 ops, beat/tie imul):
41 cs[0]=45; ex[0]=2
42 cs[1]=100; ex[1]=3
43 cs[2]=255; ex[2]=3
44 cs[3]=9; ex[3]=1
45 // imul-wins: escalated 5-op constants (chain found but >3 cyc), and primes w/ no short chain:
46 cs[4]=466; ex[4]=MCD_USE_IMUL
47 cs[5]=683; ex[5]=MCD_USE_IMUL
48 cs[6]=691; ex[6]=MCD_USE_IMUL
49 cs[7]=10007; ex[7]=MCD_USE_IMUL
50 cs[8]=1000003; ex[8]=MCD_USE_IMUL
51 cs[9]=715827883; ex[9]=MCD_USE_IMUL
52 let N: i64 = 10
53 let res: *i64 = sys_mmap(8 * 16) as *i64 // memory-backed verdicts (compiler mitigation)
54 var i: i64 = 0
55 while i < N { res[i] = dt_check(cs[i], ex[i]); i = i + 1 }
56 var pass: i64 = 0; var j: i64 = 0
57 while j < N { pass = pass + res[j]; j = j + 1 }
58 dt_puts("----\n passed " as *u8); dt_num(pass); dt_puts("/" as *u8); dt_num(N); dt_puts("\n" as *u8)
59 if pass == N { dt_puts(" GATE PASS: cost-selection correct, chains verified, tractable.\n" as *u8); sys_exit(0); return 0 }
60 dt_puts(" GATE FAIL\n" as *u8); sys_exit(1); return 1
61}