code wiki / _hdl_build / nx_vmvres_gate.nx
nx_vmvres_gate.nx source
↩ module page · 90 lines · 4107 B
1// nx_vmvres_gate.nx -- proves MV prediction + efficient residual coding TOGETHER slash the MV bitrate, and round-trips
2// exactly. Same realistic pan field as nx_vmvpred_gate; codes the predicted residuals with mvr_put (1-bit zero-flag),
3// reads them back (exact), and compares the total against absolute varint coding -- the 65% magnitude win now lands in
4// the total because the common 0-residual costs 1 bit not 5.
5import "nx_syscalls.nx"
6import "nx_gate_emit_lib.nx"
7import "nx_vmvpred.nx"
8import "nx_vmvres.nx"
9import "nx_gate_verdict.nx"
10
11func vbits(v: i64) -> i64 { return 5 + ve_blen(ve_zze(v)) }
12
13func main() -> i64 {
14 g_puts("nx_vmvres gate (MVP + efficient residual coding: total MV bitrate, round-trip)\n" as *u8)
15 var pass: i64 = 0; var total: i64 = 0
16 let BW: i64 = 6; let BH: i64 = 4; let NB: i64 = BW*BH
17
18 let mvx: *i64 = sys_mmap(NB*8) as *i64
19 let mvy: *i64 = sys_mmap(NB*8) as *i64
20 var by: i64 = 0
21 while by < BH { var bx: i64 = 0
22 while bx < BW {
23 let i: i64 = by*BW + bx
24 var dx: i64 = 3; var dy: i64 = 1
25 if bx >= 2 { if bx <= 3 { if by >= 1 { if by <= 2 { dx = 3; dy = 5 } } } }
26 if bx == 5 { if by == 0 { dx = 8; dy = 0 - 2 } }
27 mvx[i] = dx; mvy[i] = dy
28 bx = bx + 1
29 }
30 by = by + 1
31 }
32
33 let L: *i64 = sys_mmap(2*8) as *i64
34 let T: *i64 = sys_mmap(2*8) as *i64
35 let TR: *i64 = sys_mmap(2*8) as *i64
36 let pred: *i64 = sys_mmap(2*8) as *i64
37 let rx: *i64 = sys_mmap(NB*8) as *i64 // residual dx per block
38 let ry: *i64 = sys_mmap(NB*8) as *i64 // residual dy per block
39 let buf: *u8 = sys_mmap(1024) as *u8
40
41 var abs_bits: i64 = 0
42 var bp: i64 = 0
43 by = 0
44 while by < BH { var bx: i64 = 0
45 while bx < BW {
46 let i: i64 = by*BW + bx
47 L[0]=0; L[1]=0; T[0]=0; T[1]=0; TR[0]=0; TR[1]=0
48 if bx > 0 { L[0]=mvx[i-1]; L[1]=mvy[i-1] }
49 if by > 0 { T[0]=mvx[i-BW]; T[1]=mvy[i-BW] }
50 if by > 0 { if bx < BW-1 { TR[0]=mvx[i-BW+1]; TR[1]=mvy[i-BW+1] } }
51 mvp_predict(L, T, TR, pred)
52 rx[i] = mvx[i] - pred[0]
53 ry[i] = mvy[i] - pred[1]
54 abs_bits = abs_bits + vbits(mvx[i]) + vbits(mvy[i]) // baseline: absolute varint
55 bp = mvr_put(buf, bp, rx[i]) // MVP + efficient residual coding
56 bp = mvr_put(buf, bp, ry[i])
57 bx = bx + 1
58 }
59 by = by + 1
60 }
61 let mvp_bits: i64 = bp
62
63 // round-trip: read every component back and check it matches the residual we wrote
64 var rp: i64 = 0
65 var ok: i64 = 1
66 var k: i64 = 0
67 while k < NB {
68 let gx: i64 = mvr_get(buf, rp); rp = rp + mvr_len(buf, rp)
69 let gy: i64 = mvr_get(buf, rp); rp = rp + mvr_len(buf, rp)
70 if gx != rx[k] { ok = 0 }
71 if gy != ry[k] { ok = 0 }
72 k = k + 1
73 }
74
75 g_puts(" [measure] MV bits over " as *u8); g_pn(NB); g_puts(" blocks: absolute-varint=" as *u8); g_pn(abs_bits); g_puts(" MVP+zero-flag=" as *u8); g_pn(mvp_bits); g_puts(" (saving " as *u8); g_pn((abs_bits - mvp_bits) * 100 / abs_bits); g_puts("%)\n" as *u8)
76
77 pass = pass + g_check("MVP+efficient residual round-trips exactly" as *u8, ok); total=total+1
78 pass = pass + g_check("total MV bitrate now cut >= 50% (the magnitude win lands in the total)" as *u8, mvp_bits * 2 <= abs_bits); total=total+1
79
80 g_puts("---- vmvres gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
81 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
82 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
83 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
84 let ctr__dry: *i64 = gv_ctr()
85 ctr__dry[0] = pass
86 ctr__dry[1] = total
87 let rc__dry: i64 = gv_verdict("VMVRES-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
88 sys_exit(rc__dry)
89 return rc__dry
90}