code wiki / _hdl_build / nx_race_kernel.nx

nx_race_kernel.nx source

↩ module page · 69 lines · 2547 B

1// nx_race_kernel.nx -- Nishi contestant in the ONGOING race vs C. A representative 2// CPU-bound integer kernel (LCG mix + xorshift + masked reduction = the shape of 3// hashing / PRNG / sensor signal work). Pure compute, NO I/O (fair: no syscall 4// confound in the timed region). Self-times with rdtsc (best-of-3 to cut noise) 5// and prints "<checksum> <cycles>" so the harness can compare 1:1 against the C 6// build of the SAME algorithm. The checksum is a bounded reduction (always small 7// + positive) so it is byte-identical across the two languages regardless of 8// signed-print quirks. 9// 10// Fairness contract (RACING_TEAM_BENCHMARK_DOCTRINE): identical algorithm, same K, 11// same shift semantics (arithmetic >>), same wrapping 64-bit multiply, same box. 12 13import "nx_spore_syscalls.nx" // minimal surface (write/exit/mmap) -- fair vs C, which links only what it uses 14const RK_MAGIC_1234567: i64 = 1234567 15const RK_MAGIC_6364136223846793005: i64 = 6364136223846793005 16const RK_MAGIC_1442695040888963407: i64 = 1442695040888963407 17const RK_MAGIC_65535: i64 = 65535 18 19const RK_K: i64 = 20000000 20 21// the kernel -- MUST be byte-identical in meaning to race_kernel_c.c 22func rk_kernel() -> i64 { 23 var c: i64 = RK_MAGIC_1234567 24 var acc: i64 = 0 25 var i: i64 = 0 26 while i < RK_K { 27 c = c * RK_MAGIC_6364136223846793005 + RK_MAGIC_1442695040888963407 // LCG, wraps mod 2^64 28 c = c ^ (c >> 31) // xorshift (arithmetic >>) 29 acc = acc + (c & RK_MAGIC_65535) // bounded reduction (stays positive) 30 i = i + 1 31 } 32 return acc 33} 34 35func rk_emit_dec(v: i64, term: i64) -> i64 { 36 let b: *u8 = sys_mmap(32) 37 var len: i64 = 0 38 if v == 0 { b[0] = 48; len = 1 } 39 else { 40 let t: *u8 = sys_mmap(32) 41 var m: i64 = v 42 var kk: i64 = 0 43 while m > 0 { t[kk] = 48 + (m % 10); m = m / 10; kk = kk + 1 } 44 var i: i64 = 0 45 while i < kk { b[i] = t[kk - 1 - i]; i = i + 1 } 46 len = kk 47 } 48 b[len] = term as u8 49 sys_write(1, b, len + 1) 50 return 0 51} 52 53func main() -> i64 { 54 var best: i64 = 0 55 var acc: i64 = 0 56 var r: i64 = 0 57 while r < 3 { 58 let t0: i64 = __rdtsc() 59 acc = rk_kernel() 60 let t1: i64 = __rdtsc() 61 let dt: i64 = t1 - t0 62 if r == 0 { best = dt } else { if dt < best { best = dt } } 63 r = r + 1 64 } 65 rk_emit_dec(acc, 32) // checksum, space 66 rk_emit_dec(best, 10) // cycles, newline 67 sys_exit(0) 68 return 0 69}