nx_h264_total_zeros_gate.nx source
↩ module page · 90 lines · 3936 B
1// nx_h264_total_zeros_gate.nx -- REFEREE for total_zeros VLC (rung 4c-iii).
2// (a) ANCHOR (TotalCoeff=1) bit-exact: tz=0 -> "1"(0x80,1); tz=1 -> "011"(0x60,3);
3// tz=15 -> "000000001"(byte0=0,byte1=0x80,9 bits).
4// (b) ROUND-TRIP all 16 values (tz 0..15) at TotalCoeff=1: encode->decode->recover.
5// Every value PRINTED. stdout + knowledge/status/h264_total_zeros_gate.log. Exit 0/1.
6// Sovereign: nx_syscalls + nx_h264_bits + nx_h264_bitwriter + nx_h264_total_zeros.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_h264_bits.nx"
10import "nx_h264_bitwriter.nx"
11import "nx_h264_total_zeros.nx"
12
13func gp(logfd: i64, s: *u8) -> i64 {
14 var n: i64 = 0
15 while s[n] != (0 as u8) { n = n + 1 }
16 sys_write(1, s, n)
17 if logfd > 0 { sys_write(logfd, s, n) }
18 return 0
19}
20func gn(logfd: i64, v: i64) -> i64 {
21 let bb: *u8 = sys_mmap(28)
22 var m: i64 = v
23 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m }
24 let t: *u8 = sys_mmap(28)
25 var k: i64 = 0
26 if m == 0 { t[0] = 48 as u8; k = 1 }
27 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
28 var i: i64 = 0
29 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
30 sys_write(1, bb, k)
31 if logfd > 0 { sys_write(logfd, bb, k) }
32 return 0
33}
34
35func main() -> i64 {
36 let logfd: i64 = sys_openat_append("knowledge/status/h264_total_zeros_gate.log\x00" as *u8, 0x1a4)
37 gp(logfd, "H264-TOTAL-ZEROS-GATE (rung 4c-iii, TotalCoeff=1)\n\x00" as *u8)
38 var ok: i64 = 1
39
40 // anchors
41 let b0: *u8 = sys_mmap(8)
42 let w0: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
43 bw_init(w0, b0, 8); nx_total_zeros_encode(w0, 1, 0)
44 gp(logfd, " anchor tz0: byte0=\x00" as *u8); gn(logfd, b0[0] as i64); gp(logfd, " bits=\x00" as *u8); gn(logfd, bw_total_bits(w0)); gp(logfd, " (expect 128,1)\n\x00" as *u8)
45 if (b0[0] as i64) != 128 { ok = 0 }
46 if bw_total_bits(w0) != 1 { ok = 0 }
47
48 let b1: *u8 = sys_mmap(8)
49 let w1: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
50 bw_init(w1, b1, 8); nx_total_zeros_encode(w1, 1, 1)
51 gp(logfd, " anchor tz1: byte0=\x00" as *u8); gn(logfd, b1[0] as i64); gp(logfd, " bits=\x00" as *u8); gn(logfd, bw_total_bits(w1)); gp(logfd, " (expect 96,3)\n\x00" as *u8)
52 if (b1[0] as i64) != 96 { ok = 0 }
53 if bw_total_bits(w1) != 3 { ok = 0 }
54
55 let b15: *u8 = sys_mmap(8)
56 let w15: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
57 bw_init(w15, b15, 8); nx_total_zeros_encode(w15, 1, 15)
58 gp(logfd, " anchor tz15: byte0=\x00" as *u8); gn(logfd, b15[0] as i64); gp(logfd, " byte1=\x00" as *u8); gn(logfd, b15[1] as i64); gp(logfd, " bits=\x00" as *u8); gn(logfd, bw_total_bits(w15)); gp(logfd, " (expect 0,128,9)\n\x00" as *u8)
59 if (b15[0] as i64) != 0 { ok = 0 }
60 if (b15[1] as i64) != 128 { ok = 0 }
61 if bw_total_bits(w15) != 9 { ok = 0 }
62
63 // round-trip tz 0..15
64 gp(logfd, " round-trip tz0..15:\n\x00" as *u8)
65 var tz: i64 = 0
66 while tz < 16 {
67 let buf: *u8 = sys_mmap(8)
68 let bw: *BitWriter = sys_mmap(NX_BW_BYTES) as *BitWriter
69 bw_init(bw, buf, 8)
70 let e: i64 = nx_total_zeros_encode(bw, 1, tz)
71 let br: *BitReader = sys_mmap(NX_BR_BYTES) as *BitReader
72 br_init(br, buf, 8)
73 let got: i64 = nx_total_zeros_decode(br, 1)
74 if got != tz { ok = 0; gp(logfd, " tz=\x00" as *u8); gn(logfd, tz); gp(logfd, " -> \x00" as *u8); gn(logfd, got); gp(logfd, " MISMATCH\n\x00" as *u8) }
75 if e != 1 { ok = 0 }
76 tz = tz + 1
77 }
78 gp(logfd, " (all 16 round-tripped)\n\x00" as *u8)
79
80 if ok == 1 {
81 gp(logfd, "H264-TOTAL-ZEROS-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8)
82 if logfd > 0 { sys_close(logfd) }
83 sys_exit(0)
84 return 0
85 }
86 gp(logfd, "H264-TOTAL-ZEROS-GATE result=FAIL verdict=RED\n\x00" as *u8)
87 if logfd > 0 { sys_close(logfd) }
88 sys_exit(1)
89 return 1
90}