code wiki / _hdl_build / _codec_bench.nx
_codec_bench.nx source
↩ module page · 57 lines · 3013 B
1// _codec_bench.nx -- SOVEREIGN SAME-CORPUS codec head-to-head (no-wave, no external tool, no
2// fabricated numbers): our lossless frame codec (fc = MED spatial predictor + Rice) vs sovereign
3// DEFLATE, on IDENTICAL frames. Two deflate baselines: raw (general compressor) and Sub-prefiltered
4// (PNG-class = PNG applies per-row filters then deflate). Verdict per frame = fc size < PNG-class size.
5// All three measured on the SAME bytes -> a real measured exceed, honest by construction.
6import "nx_frame_codec.nx"
7import "nx_deflate_fixed.nx"
8import "nx_syscalls.nx"
9func bp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func bn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; var k: i64=0; let t: *u8=sys_mmap(28); if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 }
11// PNG "Sub" filter (per-row left predictor) -> residual buffer
12func sub_filter(buf: *u8, w: i64, h: i64, res: *u8) -> i64 {
13 var y: i64 = 0
14 while y < h {
15 var x: i64 = 0
16 while x < w {
17 if x == 0 { res[y * w] = buf[y * w] }
18 if x > 0 { res[y * w + x] = (((buf[y * w + x] & 255) - (buf[y * w + x - 1] & 255)) & 255) as u8 }
19 x = x + 1
20 }
21 y = y + 1
22 }
23 return 0
24}
25// measure fc vs deflate-raw vs deflate-Sub(PNG-class) for the frame in buf; return 1 if fc wins vs PNG-class
26func bench_buf(label: *u8, buf: *u8, w: i64, h: i64, scratch: *u8, cap: i64) -> i64 {
27 let n: i64 = w * h
28 let fco: *u8 = scratch
29 let dfo: *u8 = (scratch as i64 + cap) as *u8
30 let sub: *u8 = (scratch as i64 + 2 * cap) as *u8
31 let fc: i64 = fc_encode(buf, w, h, fco, cap)
32 let dr: i64 = dfl_encode(buf, n, dfo, cap)
33 sub_filter(buf, w, h, sub)
34 let dp: i64 = dfl_encode(sub, n, dfo, cap)
35 bp(label); bp(" raw="); bn(n); bp(" fc="); bn(fc); bp(" deflate_raw="); bn(dr); bp(" deflate_PNGsub="); bn(dp)
36 var win: i64 = 0
37 if fc < dp { win = 1 }
38 bp(" fc<PNGclass="); if win == 1 { bp("WIN") } else { bp("lose") }; bp("\n")
39 return win
40}
41func main() -> i64 {
42 let w: i64 = 32
43 let h: i64 = 32
44 let n: i64 = w * h
45 let cap: i64 = n * 4 + 64
46 let buf: *u8 = sys_mmap(n + 64)
47 let scratch: *u8 = sys_mmap(cap * 3 + 64)
48 var wins: i64 = 0
49 fc_gen_gradient(buf, w, h); wins = wins + bench_buf("gradient" as *u8, buf, w, h, scratch, cap)
50 fc_gen_checker(buf, w, h); wins = wins + bench_buf("checker " as *u8, buf, w, h, scratch, cap)
51 fc_gen_edge(buf, w, h); wins = wins + bench_buf("edge " as *u8, buf, w, h, scratch, cap)
52 fc_gen_extremes(buf, w, h); wins = wins + bench_buf("extremes" as *u8, buf, w, h, scratch, cap)
53 fc_gen_noise(buf, w, h); wins = wins + bench_buf("noise " as *u8, buf, w, h, scratch, cap)
54 bp("=== fc beats PNG-class(deflate+Sub) on "); bn(wins); bp("/5 frames (sovereign same-corpus head-to-head, no-wave) ===\n")
55 sys_exit(0)
56 return 0
57}