code wiki / (root) / nx_bench_divpow2.nx

nx_bench_divpow2.nx source

↩ module page · 57 lines · 2144 B

1// nx_bench_divpow2.nx -- NishiLang twin of bench/xlang/divpow2.c. Signed 2// divide-by-power-of-two over NEGATIVE + positive + INT_MIN dividends (the 3// round-toward-zero bias path collatz never hits). Checksum bit-exact vs gcc 4// AND clang = the correctness oracle for the G7 sign-bias sequence. 5// license_tier: ORIGINAL No hw writes (Rule 26). 6// (nx_syscalls_x86_64.nx import REMOVED 2026-07-31, debt 1785528831: this file already 7// gets the canonical syscall layer via nx_clock.nx -> syscalls.nx, so importing the raw-x86 8// module too put TWO syscall layers in one TU -- every wrapper twice, numbering picked by 9// definition ORDER, silently.) 10import "nx_clock.nx" 11const K_MAGIC_60000000: i64 = 60000000 12const K_MAGIC_1234567891011: i64 = 1234567891011 13const K_MAGIC_2654435761: i64 = 2654435761 14const K_MAGIC_1024: i64 = 1024 15const K_MAGIC_9223372036854775807: i64 = 9223372036854775807 16const K_MAGIC_4096: i64 = 4096 17 18func bd_emit_i64(fd: i64, n: i64) -> i64 { 19 let scratch: *u8 = sys_mmap(32) 20 var v: i64 = n 21 var neg: i64 = 0 22 if v < 0 { neg = 1; v = 0 - v } 23 var k: i64 = 0 24 if v == 0 { scratch[0] = 0x30 as u8; k = 1 } 25 while v > 0 { scratch[k] = (0x30 + (v - (v / 10) * 10)) as u8; v = v / 10; k = k + 1 } 26 let rev: *u8 = sys_mmap(48) 27 var ro: i64 = 0 28 if neg == 1 { rev[0] = 0x2D as u8; ro = 1 } 29 var j: i64 = 0 30 while j < k { rev[ro + j] = scratch[k - 1 - j]; j = j + 1 } 31 rev[ro + k] = 0x0A as u8 32 sys_write(fd, rev, ro + k + 1) 33 return 0 34} 35 36func main() -> i64 { 37 let N: i64 = K_MAGIC_60000000 38 var chk: i64 = 0 39 let start: i64 = nx_clock_monotonic_ns() 40 var v: i64 = 0 - K_MAGIC_1234567891011 41 var i: i64 = 0 42 while i < N { 43 v = v + K_MAGIC_2654435761 44 let a: i64 = v / 2 45 let b: i64 = v / 8 46 let cc: i64 = v / K_MAGIC_1024 47 chk = chk ^ (a + b * 3 + cc * 5) 48 i = i + 1 49 } 50 let im: i64 = 0 - K_MAGIC_9223372036854775807 - 1 51 chk = chk ^ (im / 2) ^ (im / K_MAGIC_4096) 52 let end: i64 = nx_clock_monotonic_ns() 53 let us: i64 = (end - start) / 1000 54 bd_emit_i64(1, chk) 55 bd_emit_i64(1, us) 56 return 0 57}