code wiki / _hdl_build / nx_intra_gate.nx

nx_intra_gate.nx source

↩ module page · 55 lines · 3294 B

1// nx_intra_gate.nx -- proves + MEASURES sovereign intra prediction (nx_intra): the right mode predicts structure 2// with ~0 residual, and beats naive pixels-128 coding (the q/bit gain). Native, fast. 3import "nx_syscalls.nx" 4import "nx_gate_emit_lib.nx" 5import "nx_intra.nx" 6import "nx_gate_verdict.nx" 7 8func naive_sad(block: *i64) -> i64 { var s: i64=0; var i: i64=0; while i<16 { let d: i64=block[i]-128; if d<0 {s=s-d} else {s=s+d} i=i+1 } return s } 9 10func main() -> i64 { 11 g_puts("nx_intra gate (intra prediction modes, MEASURED)\n" as *u8) 12 var pass: i64 = 0; var total: i64 = 0 13 let block: *i64 = sys_mmap(16*8) as *i64 14 let pred: *i64 = sys_mmap(16*8) as *i64 15 let top: *i64 = sys_mmap(4*8) as *i64 16 let left: *i64 = sys_mmap(4*8) as *i64 17 top[0]=100; top[1]=110; top[2]=120; top[3]=130 18 left[0]=90; left[1]=95; left[2]=100; left[3]=105 19 20 // 1) VERTICAL structure: block columns = top row -> VERTICAL predicts it exactly 21 var y: i64=0; while y<4 { var x: i64=0; while x<4 { block[y*4+x] = top[x]; x=x+1 } y=y+1 } 22 let mv: i64 = in_best(block, top, left, 1, 1, pred) 23 let sv: i64 = in_sad(block, pred) 24 pass = pass + g_check("vertical-structured block -> picks VERTICAL, residual 0" as *u8, (mv == IN_VERT) & (sv == 0)); total=total+1 25 26 // 2) HORIZONTAL structure: block rows = left col -> HORIZONTAL predicts it exactly 27 y=0; while y<4 { var x: i64=0; while x<4 { block[y*4+x] = left[y]; x=x+1 } y=y+1 } 28 let mh: i64 = in_best(block, top, left, 1, 1, pred) 29 let sh: i64 = in_sad(block, pred) 30 pass = pass + g_check("horizontal-structured block -> picks HORIZONTAL, residual 0" as *u8, (mh == IN_HORIZ) & (sh == 0)); total=total+1 31 32 // 3) flat block at the neighbour mean -> DC, residual 0 33 var i: i64=0; while i<16 { block[i] = 105; i=i+1 } // 105 ~ mean of top(115)+left(97.5)=106 34 let md: i64 = in_best(block, top, left, 1, 1, pred) 35 pass = pass + g_check("flat block -> DC mode" as *u8, md == IN_DC); total=total+1 36 37 // 4) q/bit gain: a structured block -> best-mode residual << naive pixels-128 residual 38 y=0; while y<4 { var x: i64=0; while x<4 { block[y*4+x] = top[x]; x=x+1 } y=y+1 } // vertical structure again 39 in_best(block, top, left, 1, 1, pred) 40 let best_r: i64 = in_sad(block, pred) 41 let naive_r: i64 = naive_sad(block) 42 g_puts(" [measure] structured block residual: intra-pred=" as *u8); g_pn(best_r); g_puts(" vs naive pixels-128=" as *u8); g_pn(naive_r); g_puts("\n" as *u8) 43 pass = pass + g_check("intra prediction slashes residual vs naive (q/bit gain)" as *u8, best_r * 4 < naive_r); total=total+1 44 45 g_puts("---- intra gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 46 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 47 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 48 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 49 let ctr__dry: *i64 = gv_ctr() 50 ctr__dry[0] = pass 51 ctr__dry[1] = total 52 let rc__dry: i64 = gv_verdict("INTRA-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8) 53 sys_exit(rc__dry) 54 return rc__dry 55}