nx_native_array_sum_bench.nx source
↩ module page · 42 lines · 1184 B
1// nx_native_array_sum_bench.nx -- memory-bound native micro-bench.
2//
3// Where nx_native_tightloop_bench tests register-only arithmetic
4// (compiler best case), this bench streams through a 1M-element i64
5// array and sums it. Memory-bound: 8MB doesn't fit L1; streams
6// through L2/L3.
7//
8// Returns (sum & 0xFF) as exit code so shell time can measure
9// without printing. Native target requires nx_syscalls.nx for mmap.
10
11import "nx_syscalls.nx"
12const K_MAGIC_1000000: i64 = 1000000
13
14func main() -> i64 {
15 let N: i64 = K_MAGIC_1000000 // 1M elements = 8MB i64 array
16 let ITERS: i64 = 50 // 50 sweeps = 400 MB streamed
17
18 let buf: *u8 = sys_mmap(N * 8)
19 let arr: *i64 = buf as *i64
20
21 // Initialize array with i * 7 mod 256 (a pattern that ensures the
22 // compiler can't const-fold the sum).
23 var k: i64 = 0
24 while k < N {
25 arr[k] = (k * 7) & 255
26 k = k + 1
27 }
28
29 // Sweep 'ITERS' times, accumulating.
30 var sum: i64 = 0
31 var iter: i64 = 0
32 while iter < ITERS {
33 var i: i64 = 0
34 while i < N {
35 sum = sum + arr[i]
36 i = i + 1
37 }
38 iter = iter + 1
39 }
40
41 return sum
42}