code wiki / _hdl_build / nx_lpc_speed_bench.nx

nx_lpc_speed_bench.nx source

↩ module page · 118 lines · 6460 B

1// nx_lpc_speed_bench.nx -- HARDWARE-RUNG measurement: how fast does the sovereign LPC audio codec 2// (the EXACT vc_lpc_encode/decode the live room ships in nx_video_client.wasm) run on THIS CPU? 3// (operator 2026-07-02: "hardware foundational rung each rung up"). Times REAL encode+decode of 4// 21ms/1024-sample frames (the live framing after today's latency cut) with the monotonic clock, 5// verifies BIT-EXACT roundtrip on every frame (lossless = the codec's contract), and reports the 6// REALTIME FACTOR: how many concurrent 48kHz peers this CPU can encode+decode (the 8-peer room's 7// compute headroom, measured not assumed). NEG-control: a tampered stream must NOT roundtrip. 8// Log -> knowledge/status/lpc_speed.log (LPCBENCH ... verdict=). expect_exit: 0 license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_video_client_wasm.nx" 11const LPB_MAGIC_1024: i64 = 1024 12const LPB_MAGIC_2026: i64 = 2026 13const LPB_MAGIC_2000: i64 = 2000 14const LPB_MAGIC_262144: i64 = 262144 15const LPB_MAGIC_20260702: i64 = 20260702 16const LPB_MAGIC_1103515245: i64 = 1103515245 17const LPB_MAGIC_12345: i64 = 12345 18 19const LPB_LOG: *u8 = "knowledge/status/lpc_speed.log" 20 21func bw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 22func bwn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} 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} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(fd,bb,k); return 0 } 23 24func main() -> i64 { 25 bw(1, "=== nx_lpc_speed_bench: the LIVE room's audio codec, timed on THIS hardware ===\n") 26 let FRAME: i64 = LPB_MAGIC_1024 // the live capture framing (21ms @48k, shipped LPB_MAGIC_2026-07-02) 27 let NFR: i64 = LPB_MAGIC_2000 // LPB_MAGIC_2000 frames = ~42.6s of speech-band signal 28 let pcm: *u8 = sys_mmap(FRAME * 2 + 64) 29 let enc: *u8 = sys_mmap(FRAME * 2 + 128) 30 let dec: *u8 = sys_mmap(FRAME * 2 + 128) 31 let scr: *u8 = sys_mmap(LPB_MAGIC_262144) 32 33 // deterministic speech-like test signal: sum of two integer "tones" + LCG noise floor 34 // (pure silence would flatter the coder; pure noise would sandbag it) 35 let seed: *i64 = sys_mmap(8) as *i64 36 seed[0] = LPB_MAGIC_20260702 37 var enc_us: i64 = 0 38 var dec_us: i64 = 0 39 var enc_bytes: i64 = 0 40 var exact: i64 = 1 41 var f: i64 = 0 42 while f < NFR { 43 var i: i64 = 0 44 while i < FRAME { 45 let t: i64 = f * FRAME + i 46 seed[0] = (seed[0] * LPB_MAGIC_1103515245 + LPB_MAGIC_12345) & 0x7fffffff 47 let noise: i64 = ((seed[0] >> 13) % 129) - 64 48 var s: i64 = 0 49 let ph1: i64 = (t * 37) % 200 50 if ph1 < 100 { s = s + (ph1 * 40 - LPB_MAGIC_2000) } else { s = s + (LPB_MAGIC_2000 - (ph1 - 100) * 40) } 51 let ph2: i64 = (t * 111) % 64 52 if ph2 < 32 { s = s + (ph2 * 60 - 960) } else { s = s + (960 - (ph2 - 32) * 60) } 53 s = s + noise 54 pcm[i*2] = (s & 0xFF) as u8 55 pcm[i*2+1] = ((s >> 8) & 0xFF) as u8 56 i = i + 1 57 } 58 let t0: i64 = sys_now_us() 59 let el: i64 = vc_lpc_encode(pcm, FRAME, enc, FRAME * 2 + 64, scr) 60 let t1: i64 = sys_now_us() 61 if el <= 0 { bw(1, "encoder refused a frame -> RED\n"); return 1 } 62 enc_bytes = enc_bytes + el 63 let nd: i64 = vc_lpc_decode(enc, el, dec, FRAME * 2 + 64, scr) 64 let t2: i64 = sys_now_us() 65 enc_us = enc_us + (t1 - t0) 66 dec_us = dec_us + (t2 - t1) 67 if nd != FRAME { exact = 0 } 68 i = 0 69 while i < FRAME * 2 { if dec[i] != pcm[i] { exact = 0; i = FRAME * 2 } else { i = i + 1 } } 70 f = f + 1 71 } 72 let total_samples: i64 = NFR * FRAME 73 if enc_us < 1 { enc_us = 1 } 74 if dec_us < 1 { dec_us = 1 } 75 let enc_ksps: i64 = total_samples * 1000 / enc_us // kilo-samples per second 76 let dec_ksps: i64 = total_samples * 1000 / dec_us 77 // realtime factor: one 48kHz stream needs 48 ksps; a full duplex peer costs 1 encode of self 78 // + N-1 decodes. rt = how many 48k streams this CPU sustains on ONE core for each direction. 79 let enc_rt: i64 = enc_ksps / 48 80 let dec_rt: i64 = dec_ksps / 48 81 let ratio_permille: i64 = enc_bytes * 1000 / (total_samples * 2) 82 83 // NEG-control: tamper one byte mid-stream -> the roundtrip must NOT be byte-exact anymore 84 let el2: i64 = vc_lpc_encode(pcm, FRAME, enc, FRAME * 2 + 64, scr) 85 var neg: i64 = 0 86 if el2 > 10 { 87 enc[el2/2] = ((enc[el2/2] as i64) ^ 0x55) as u8 88 let nd2: i64 = vc_lpc_decode(enc, el2, dec, FRAME * 2 + 64, scr) 89 if nd2 != FRAME { neg = 1 } else { 90 var j: i64 = 0 91 while j < FRAME * 2 { if dec[j] != pcm[j] { neg = 1; j = FRAME * 2 } else { j = j + 1 } } 92 } 93 } 94 95 bw(1, " frames="); bwn(1, NFR); bw(1, " x "); bwn(1, FRAME); bw(1, " samples (21ms live framing), signal=speech-like\n") 96 bw(1, " encode: "); bwn(1, enc_ksps); bw(1, " ksamples/s = "); bwn(1, enc_rt); bw(1, "x realtime-48k (one core)\n") 97 bw(1, " decode: "); bwn(1, dec_ksps); bw(1, " ksamples/s = "); bwn(1, dec_rt); bw(1, "x realtime-48k (one core)\n") 98 bw(1, " compression: "); bwn(1, ratio_permille); bw(1, " permille of raw PCM (lossless, bit-exact roundtrip="); bwn(1, exact); bw(1, ")\n") 99 bw(1, " 8-peer room budget: 1 encode + 7 decodes needs "); bwn(1, 48 + 7 * 48); bw(1, " ksps total -> headroom is measured above\n") 100 101 var green: i64 = 1 102 if exact != 1 { green = 0 } 103 if neg != 1 { green = 0 } 104 if enc_rt < 8 { green = 0 } // floor: must at least sustain the 8-peer room in realtime 105 if dec_rt < 8 { green = 0 } 106 107 let lfd: i64 = sys_openat_append(LPB_LOG, 420) 108 if lfd >= 0 { 109 bw(lfd, "LPCBENCH frame=1024 enc_ksps="); bwn(lfd, enc_ksps); bw(lfd, " dec_ksps="); bwn(lfd, dec_ksps) 110 bw(lfd, " enc_rt48="); bwn(lfd, enc_rt); bw(lfd, " dec_rt48="); bwn(lfd, dec_rt) 111 bw(lfd, " ratio_pm="); bwn(lfd, ratio_permille); bw(lfd, " exact="); bwn(lfd, exact); bw(lfd, " negctl="); bwn(lfd, neg) 112 if green==1 { bw(lfd, " verdict=GREEN\n") } else { bw(lfd, " verdict=RED\n") } 113 sys_close(lfd) 114 } 115 if green==1 { bw(1, "LPC-SPEED-BENCH verdict=GREEN -- codec compute is measured, lossless, neg-control fired\n"); return 0 } 116 bw(1, "LPC-SPEED-BENCH verdict=RED\n") 117 return 1 118}