code wiki / _hdl_build / nx_intra_gate.nx
nx_intra_gate.nx source
↩ module page · 47 lines · 2855 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"
6
7func 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 }
8
9func main() -> i64 {
10 g_puts("nx_intra gate (intra prediction modes, MEASURED)\n" as *u8)
11 var pass: i64 = 0; var total: i64 = 0
12 let block: *i64 = sys_mmap(16*8) as *i64
13 let pred: *i64 = sys_mmap(16*8) as *i64
14 let top: *i64 = sys_mmap(4*8) as *i64
15 let left: *i64 = sys_mmap(4*8) as *i64
16 top[0]=100; top[1]=110; top[2]=120; top[3]=130
17 left[0]=90; left[1]=95; left[2]=100; left[3]=105
18
19 // 1) VERTICAL structure: block columns = top row -> VERTICAL predicts it exactly
20 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 }
21 let mv: i64 = in_best(block, top, left, 1, 1, pred)
22 let sv: i64 = in_sad(block, pred)
23 pass = pass + g_check("vertical-structured block -> picks VERTICAL, residual 0" as *u8, (mv == IN_VERT) & (sv == 0)); total=total+1
24
25 // 2) HORIZONTAL structure: block rows = left col -> HORIZONTAL predicts it exactly
26 y=0; while y<4 { var x: i64=0; while x<4 { block[y*4+x] = left[y]; x=x+1 } y=y+1 }
27 let mh: i64 = in_best(block, top, left, 1, 1, pred)
28 let sh: i64 = in_sad(block, pred)
29 pass = pass + g_check("horizontal-structured block -> picks HORIZONTAL, residual 0" as *u8, (mh == IN_HORIZ) & (sh == 0)); total=total+1
30
31 // 3) flat block at the neighbour mean -> DC, residual 0
32 var i: i64=0; while i<16 { block[i] = 105; i=i+1 } // 105 ~ mean of top(115)+left(97.5)=106
33 let md: i64 = in_best(block, top, left, 1, 1, pred)
34 pass = pass + g_check("flat block -> DC mode" as *u8, md == IN_DC); total=total+1
35
36 // 4) q/bit gain: a structured block -> best-mode residual << naive pixels-128 residual
37 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
38 in_best(block, top, left, 1, 1, pred)
39 let best_r: i64 = in_sad(block, pred)
40 let naive_r: i64 = naive_sad(block)
41 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)
42 pass = pass + g_check("intra prediction slashes residual vs naive (q/bit gain)" as *u8, best_r * 4 < naive_r); total=total+1
43
44 g_puts("---- intra gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
45 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
46 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
47}