code wiki / _hdl_build / nx_vtransform_gate.nx
nx_vtransform_gate.nx source
↩ module page · 69 lines · 4365 B
1// nx_vtransform_gate.nx -- proves + MEASURES the sovereign 4x4 transform+quantize (nx_vtransform), V-R2.
2// Native nx_cc->nxasm, no node.
3// 1) energy compaction: a flat residual -> all energy in 1 coeff (DC); a smooth residual -> few coeffs
4// 2) lossless round-trip at q=1: fwd -> quant -> dequant -> inv == the original residual
5// 3) quantization compaction (MEASURED): a smooth residual at q=16 -> nonzero coeffs << 16 (the entropy cost)
6// 4) bounded lossy round-trip at q=8: reconstructed within a bounded error of the original
7// 5) anti-tautology: a high-frequency (checkerboard) residual does NOT collapse to DC (transform is real)
8// license_tier: ORIGINAL
9import "nx_syscalls.nx"
10import "nx_gate_emit_lib.nx"
11import "nx_vtransform.nx"
12
13func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
14func bcopy(d: *i64, s: *i64) -> i64 { var i: i64=0; while i<16 { d[i]=s[i]; i=i+1 } return 0 }
15func beq(a: *i64, b: *i64) -> i64 { var i: i64=0; while i<16 { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
16func maxerr(a: *i64, b: *i64) -> i64 { var m: i64=0; var i: i64=0; while i<16 { let e: i64=g_abs(a[i]-b[i]); if e>m { m=e } i=i+1 } return m }
17
18func main() -> i64 {
19 g_puts("nx_vtransform gate (sovereign 4x4 transform+quantize of the residual, MEASURED)\n" as *u8)
20 var pass: i64 = 0; var total: i64 = 0
21
22 let orig: *i64 = sys_mmap(16*8) as *i64
23 let blk: *i64 = sys_mmap(16*8) as *i64
24
25 // 1) energy compaction -- flat residual (all 8) -> only DC nonzero
26 var i: i64 = 0; while i < 16 { orig[i] = 8; i = i + 1 }
27 bcopy(blk, orig); vt_fwd(blk)
28 var r1: i64 = 1
29 if blk[0] != 128 { r1 = 0 } // DC = 8 * 16
30 var nzf: i64 = vt_nonzero(blk)
31 if nzf != 1 { r1 = 0 } // every other coeff zero
32 g_puts(" [measure] flat residual -> DC=" as *u8); g_pn(blk[0]); g_puts(", nonzero coeffs=" as *u8); g_pn(nzf); g_puts(" / 16\n" as *u8)
33 pass = pass + g_check("energy compaction: flat residual -> all energy in DC" as *u8, r1); total=total+1
34
35 // 2) lossless round-trip at q=1
36 i = 0; while i < 16 { orig[i] = (i*i - 7*i + 3); i = i + 1 } // an arbitrary residual
37 bcopy(blk, orig)
38 vt_fwd(blk); vt_quant(blk, 1); vt_dequant(blk, 1); vt_inv(blk)
39 pass = pass + g_check("lossless round-trip at q=1 (fwd->quant->dequant->inv == orig)" as *u8, beq(blk, orig)); total=total+1
40
41 // 3) quantization compaction on a smooth (motion-comp-like) residual
42 i = 0; while i < 16 { let rr: i64 = i / 4; let cc: i64 = i % 4; orig[i] = 3 + rr + cc; i = i + 1 } // gentle gradient
43 bcopy(blk, orig)
44 vt_fwd(blk)
45 let nz_before: i64 = vt_nonzero(blk)
46 let nz_after: i64 = vt_quant(blk, 16)
47 g_puts(" [measure] smooth residual: nonzero " as *u8); g_pn(nz_before); g_puts(" -> after quant(16) " as *u8); g_pn(nz_after); g_puts(" / 16\n" as *u8)
48 pass = pass + g_check("quantization compaction: nonzero coeffs drop sharply" as *u8, nz_after < nz_before); total=total+1
49
50 // 4) bounded lossy round-trip at q=8
51 bcopy(blk, orig)
52 vt_fwd(blk); vt_quant(blk, 8); vt_dequant(blk, 8); vt_inv(blk)
53 let me: i64 = maxerr(blk, orig)
54 g_puts(" [measure] lossy round-trip q=8: max pixel error=" as *u8); g_pn(me); g_puts("\n" as *u8)
55 pass = pass + g_check("bounded lossy round-trip (max error small)" as *u8, me <= 8); total=total+1
56
57 // 5) anti-tautology: a checkerboard (high-freq) residual does NOT collapse to DC
58 i = 0; while i < 16 { let rr: i64 = i/4; let cc: i64 = i%4; if ((rr+cc) & 1) == 0 { orig[i]=20 } else { orig[i]=0-20 } i=i+1 }
59 bcopy(blk, orig); vt_fwd(blk)
60 var r5: i64 = 1
61 if blk[0] != 0 { r5 = 0 } // checkerboard has ZERO DC...
62 if vt_nonzero(blk) == 0 { r5 = 0 } // ...but real energy in a high-freq coeff (not faked away)
63 g_puts(" [measure] checkerboard: DC=" as *u8); g_pn(blk[0]); g_puts(", high-freq coeff present (nonzero=" as *u8); g_pn(vt_nonzero(blk)); g_puts(")\n" as *u8)
64 pass = pass + g_check("anti-tautology: high-freq residual keeps high-freq energy" as *u8, r5); total=total+1
65
66 g_puts("---- vtransform gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
67 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
68 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
69}