code wiki / _hdl_build / nx_vtransform_gate.nx

nx_vtransform_gate.nx source

↩ module page · 77 lines · 4809 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" 12import "nx_gate_verdict.nx" 13 14func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 15func bcopy(d: *i64, s: *i64) -> i64 { var i: i64=0; while i<16 { d[i]=s[i]; i=i+1 } return 0 } 16func 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 } 17func 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 } 18 19func main() -> i64 { 20 g_puts("nx_vtransform gate (sovereign 4x4 transform+quantize of the residual, MEASURED)\n" as *u8) 21 var pass: i64 = 0; var total: i64 = 0 22 23 let orig: *i64 = sys_mmap(16*8) as *i64 24 let blk: *i64 = sys_mmap(16*8) as *i64 25 26 // 1) energy compaction -- flat residual (all 8) -> only DC nonzero 27 var i: i64 = 0; while i < 16 { orig[i] = 8; i = i + 1 } 28 bcopy(blk, orig); vt_fwd(blk) 29 var r1: i64 = 1 30 if blk[0] != 128 { r1 = 0 } // DC = 8 * 16 31 var nzf: i64 = vt_nonzero(blk) 32 if nzf != 1 { r1 = 0 } // every other coeff zero 33 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) 34 pass = pass + g_check("energy compaction: flat residual -> all energy in DC" as *u8, r1); total=total+1 35 36 // 2) lossless round-trip at q=1 37 i = 0; while i < 16 { orig[i] = (i*i - 7*i + 3); i = i + 1 } // an arbitrary residual 38 bcopy(blk, orig) 39 vt_fwd(blk); vt_quant(blk, 1); vt_dequant(blk, 1); vt_inv(blk) 40 pass = pass + g_check("lossless round-trip at q=1 (fwd->quant->dequant->inv == orig)" as *u8, beq(blk, orig)); total=total+1 41 42 // 3) quantization compaction on a smooth (motion-comp-like) residual 43 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 44 bcopy(blk, orig) 45 vt_fwd(blk) 46 let nz_before: i64 = vt_nonzero(blk) 47 let nz_after: i64 = vt_quant(blk, 16) 48 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) 49 pass = pass + g_check("quantization compaction: nonzero coeffs drop sharply" as *u8, nz_after < nz_before); total=total+1 50 51 // 4) bounded lossy round-trip at q=8 52 bcopy(blk, orig) 53 vt_fwd(blk); vt_quant(blk, 8); vt_dequant(blk, 8); vt_inv(blk) 54 let me: i64 = maxerr(blk, orig) 55 g_puts(" [measure] lossy round-trip q=8: max pixel error=" as *u8); g_pn(me); g_puts("\n" as *u8) 56 pass = pass + g_check("bounded lossy round-trip (max error small)" as *u8, me <= 8); total=total+1 57 58 // 5) anti-tautology: a checkerboard (high-freq) residual does NOT collapse to DC 59 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 } 60 bcopy(blk, orig); vt_fwd(blk) 61 var r5: i64 = 1 62 if blk[0] != 0 { r5 = 0 } // checkerboard has ZERO DC... 63 if vt_nonzero(blk) == 0 { r5 = 0 } // ...but real energy in a high-freq coeff (not faked away) 64 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) 65 pass = pass + g_check("anti-tautology: high-freq residual keeps high-freq energy" as *u8, r5); total=total+1 66 67 g_puts("---- vtransform gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 68 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 69 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 70 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 71 let ctr__dry: *i64 = gv_ctr() 72 ctr__dry[0] = pass 73 ctr__dry[1] = total 74 let rc__dry: i64 = gv_verdict("VTRANSFORM-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 75 sys_exit(rc__dry) 76 return rc__dry 77}