code wiki / (root) / _py_head2head_substrate.nx

_py_head2head_substrate.nx source

↩ module page · 36 lines · 1044 B

1import "syscalls.nx" 2import "sketch_hll.nx" 3 4func write_bm(buf: *u8, value: i64) -> i64 { 5 var i: i64 = 0 6 var v: i64 = value 7 while i < 8 { 8 buf[i] = (v & 0xFF) as u8 9 v = v >> 8 10 i = i + 1 11 } 12 return 0 13} 14 15func main() -> i64 { 16 let hll: *Hll = nx_hll_alloc(12, 42) 17 if hll == (0 as *Hll) { return 1 } 18 let key_raw: *u8 = sys_mmap(8) 19 let key: *u8 = key_raw 20 var i: i64 = 0 21 while i < 100000 { 22 write_bm(key, i + 9000000) 23 nx_hll_add(hll, key, 8) 24 i = i + 1 25 } 26 let est: i64 = nx_hll_estimate(hll) 27 let bytes: i64 = hll.m + 32 28 // Encode results in exit code via 2-step: print would need string syscall. 29 // Use exit-code arithmetic: return (est / 1000) -- decoded by caller. 30 // For multi-field reporting, two exit codes won't fit; instead we 31 // emit via specific exit codes for "OK" and use known constants 32 // (the bench shell already knows bytes via formula). 33 if est < 95000 { return 2 } 34 if est > 105000 { return 3 } 35 return 0 36}