code wiki / (root) / nx_sum_bench.nx

nx_sum_bench.nx source

↩ module page · 59 lines · 1905 B

1// nx_sum_bench.nx -- NishiLang scalar sum bench for cross-language 2// comparison against sum_c.c. Same N + reps + checksum. 3// 4// Run: compile to RV-V, run under qemu-riscv64-static. Reports 5// nanoseconds. See nxc2/docs/PERF_NISHI_VS_C.md for the comparison. 6 7// nx_safety_envelope: 8// intended_use: AUTO_APPLIED -- primitive-specific tuning queued 9// sil_target: SIL1 10// evidence: [bulk_applied_2026-05-16, see-file-comment-for-detail] 11// verdict: NOT_YET_EVALUATED 12 13import "nx_kernel_v2.nx" 14import "nx_log.nx" 15import "nx_clock.nx" 16 17const N: i64 = 1000000 18const REPS: i64 = 100 19 20func sum_scalar(arr: *i64) -> i64 { 21 var s: i64 = 0 22 var i: i64 = 0 23 while i < N { 24 s = s + arr[i] 25 i = i + 1 26 } 27 return s 28} 29 30func main() -> nx_exit { 31 let raw: *u8 = sys_mmap(N * 8) 32 let arr: *i64 = raw as *i64 33 var i: i64 = 0 34 while i < N { arr[i] = i; i = i + 1 } 35 36 println("=== nx_sum_bench (NishiLang scalar, qemu RV-V) ===" as *u8) 37 println("N:" as *u8); print_i64(N); println("" as *u8) 38 println("REPS:" as *u8); print_i64(REPS); println("" as *u8) 39 40 let t0: i64 = nx_clock_monotonic_ns() 41 var total: i64 = 0 42 var rep: i64 = 0 43 while rep < REPS { 44 total = total + sum_scalar(arr) 45 rep = rep + 1 46 } 47 let t1: i64 = nx_clock_monotonic_ns() 48 let elapsed: i64 = t1 - t0 49 let per_rep: i64 = elapsed / REPS 50 51 println("Elapsed ns:" as *u8); print_i64(elapsed); println("" as *u8) 52 println("Per rep ns:" as *u8); print_i64(per_rep); println("" as *u8) 53 println("Checksum:" as *u8); print_i64(total); println("" as *u8) 54 let expected: i64 = REPS * (N - 1) * N / 2 55 println("Expected:" as *u8); print_i64(expected); println("" as *u8) 56 if total != expected { println("FAIL: checksum mismatch" as *u8); return 1 } 57 println("PASS: scalar sum correct" as *u8) 58 return 0 59}