code wiki / _hdl_build / nx_v128_share_probe.nx
nx_v128_share_probe.nx source
↩ module page · 115 lines · 5203 B
1// nx_v128_share_probe.nx -- F618 rung-1 HOT-LOOP RE-MEASURE (native): what share of the shipped-config
2// P-frame encode (416x320, emode 673, qp 22 -- the nx_vcodec_layout_gate recipe) is the 16x16 motion
3// search (vm_search_q, the SAD-dominated kernel the design names first)? Times (a) the full vv_enc_rct9
4// P encode and (b) an isolated whole-frame vm_search_q sweep with the encoder's own args, same frames.
5// The sweep is a LOWER BOUND on total SAD-family work (encode also does subpel refine, me8 partition
6// searches when armed, zero-SAD skip checks, intra SAD). license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_video_codec_wasm.nx"
9const K_MAGIC_2026: i64 = 2026
10const K_MAGIC_524288: i64 = 524288
11const K_MAGIC_4096: i64 = 4096
12const K_MAGIC_1048576: i64 = 1048576
13const K_MAGIC_8192: i64 = 8192
14const K_MAGIC_1000000: i64 = 1000000
15
16const SPW: i64 = 416
17const SPH: i64 = 320
18const SPQP: i64 = 22
19const SPEMODE: i64 = 673
20const SPITER: i64 = 30
21
22func spw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
23func spn(v: i64) -> i64 {
24 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
25 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
26 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
27
28func sp_fill(f0: *u8, f1: *u8, sz: i64) -> i64 {
29 let N: i64 = SPW*SPH
30 var i: i64 = 0
31 while i < N {
32 let x: i64 = i % SPW
33 let y: i64 = i / SPW
34 var v: i64 = (x + y) * 2 + ((x * y) % 17)
35 if x > (SPW/2) { v = v + 40 }
36 v = v % 256
37 f0[i] = v as u8
38 i = i + 1 }
39 i = N
40 while i < sz { f0[i] = 128 as u8; i = i + 1 }
41 var yy: i64 = 0
42 while yy < SPH {
43 var xx: i64 = 0
44 while xx < SPW {
45 var sx: i64 = xx - 2
46 if sx < 0 { sx = 0 }
47 f1[yy*SPW + xx] = f0[yy*SPW + sx]
48 xx = xx + 1 }
49 yy = yy + 1 }
50 i = N
51 while i < sz { f1[i] = f0[i]; i = i + 1 }
52 return 0 }
53
54func main() -> i64 {
55 let vmscr3: *i64 = sys_mmap(32) as *i64 // vm_search scratch (K_MAGIC_2026-07-29: hoisted ONE alloc; the per-call sys_mmap inside vm_search was the wasm 0-stub + native map-leak class)
56 spw("=== nx_v128_share_probe: motion-search share of shipped-config P encode (416x320 emode673 qp22) ===\n" as *u8)
57 let N: i64 = SPW*SPH
58 let C: i64 = (SPW/2)*(SPH/2)
59 let sz: i64 = N + 2*C
60 let f0: *u8 = sys_mmap(sz)
61 let f1: *u8 = sys_mmap(sz)
62 let zeros: *u8 = sys_mmap(sz)
63 let rEK: *u8 = sys_mmap(sz)
64 let rEP: *u8 = sys_mmap(sz)
65 let sK: *u8 = sys_mmap(K_MAGIC_524288)
66 let sP: *u8 = sys_mmap(K_MAGIC_524288)
67 let blk: *i64 = sys_mmap(K_MAGIC_4096) as *i64
68 let mv: *i64 = sys_mmap(128) as *i64
69 let rctx: *i64 = sys_mmap(K_MAGIC_4096) as *i64
70 let est: *i64 = sys_mmap(K_MAGIC_4096) as *i64
71 let probs: *i64 = sys_mmap(K_MAGIC_4096) as *i64
72 let rcbuf: *u8 = sys_mmap(K_MAGIC_1048576)
73 let t8c: *i64 = sys_mmap(K_MAGIC_8192) as *i64
74 let mvplane: *i64 = sys_mmap(K_MAGIC_8192) as *i64
75 vc_t8_init(t8c)
76 var i: i64 = 0
77 while i < sz { zeros[i] = 0 as u8; i = i + 1 }
78 sp_fill(f0, f1, sz)
79 // key(rct8) once -> rEK is the P-frame's prev (the shipped chain's reference)
80 rctx[0]=SPEMODE; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
81 rctx[5]=0; rctx[6]=0; rctx[7]=0; rctx[8]=0; rctx[9]=mvplane as i64
82 let kb: i64 = vv_enc_rct8(f0, zeros, rEK, SPW, SPH, SPQP, 1, SPQP*188, sK, K_MAGIC_524288, blk, mv, rctx)
83 spw(" key bytes=" as *u8); spn(kb); spw("\n" as *u8)
84 // (a) FULL P encode timing (vv_enc_rct9, the vcv-10 P path)
85 let t0: i64 = sys_now_us()
86 var it: i64 = 0
87 while it < SPITER {
88 rctx[0]=SPEMODE; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
89 rctx[5]=0; rctx[6]=0; rctx[7]=0; rctx[8]=0; rctx[9]=mvplane as i64
90 vv_enc_rct9(f1, rEK, rEP, SPW, SPH, SPQP, SPQP*94, sP, K_MAGIC_524288, blk, mv, rctx)
91 it = it + 1 }
92 let t1: i64 = sys_now_us()
93 let encus: i64 = (t1 - t0) / SPITER
94 // (b) isolated whole-frame 16x16 vm_search_q sweep (the encoder's own first-search args)
95 let BW: i64 = SPW / 16
96 let BH: i64 = SPH / 16
97 let t2: i64 = sys_now_us()
98 it = 0
99 var sadsum: i64 = 0
100 while it < SPITER {
101 var by: i64 = 0
102 while by < BH {
103 var bx: i64 = 0
104 while bx < BW {
105 let s: i64 = vm_search_q(f1, rEK, SPW, SPH, bx, by, 16, 16, mv, SPQP, vmscr3)
106 sadsum = sadsum + s
107 bx = bx + 1 }
108 by = by + 1 }
109 it = it + 1 }
110 let t3: i64 = sys_now_us()
111 let srchus: i64 = (t3 - t2) / SPITER
112 spw(" P-enc(vv_enc_rct9) = " as *u8); spn(encus); spw(" us/frame (" as *u8); spn(K_MAGIC_1000000/(encus+1)); spw(" fps)\n" as *u8)
113 spw(" 16x16 search sweep = " as *u8); spn(srchus); spw(" us/frame (sadsum=" as *u8); spn(sadsum); spw(")\n" as *u8)
114 spw(" search share (LOWER BOUND on SAD-family) = " as *u8); spn((srchus*1000)/(encus+1)); spw(" permil\n" as *u8)
115 return 0 }