code wiki / (root) / nx_bench_fnv.nx

nx_bench_fnv.nx source

↩ module page · 52 lines · 2073 B

1// nx_bench_fnv.nx -- NishiLang twin of bench/xlang/fnv.c. FNV-1a over an LCG byte 2// stream (multiply+xor). Prints hash then elapsed_us. (x>>33)&0xff is sign-shift 3// agnostic (result low byte = x bits 33..40, identical for sar/shr) so the 4// checksum is bit-exact vs the gcc AND clang uint64 builds (3rd-party oracles). 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_200000000: i64 = 200000000 12const K_MAGIC_1469598103934665603: i64 = 1469598103934665603 13const K_MAGIC_6364136223846793005: i64 = 6364136223846793005 14const K_MAGIC_1442695040888963407: i64 = 1442695040888963407 15const K_MAGIC_1099511628211: i64 = 1099511628211 16 17func bf_emit_i64(fd: i64, n: i64) -> i64 { 18 let scratch: *u8 = sys_mmap(32) 19 var v: i64 = n 20 var neg: i64 = 0 21 if v < 0 { neg = 1; v = 0 - v } 22 var k: i64 = 0 23 if v == 0 { scratch[0] = 0x30 as u8; k = 1 } 24 while v > 0 { scratch[k] = (0x30 + (v - (v / 10) * 10)) as u8; v = v / 10; k = k + 1 } 25 let rev: *u8 = sys_mmap(48) 26 var ro: i64 = 0 27 if neg == 1 { rev[0] = 0x2D as u8; ro = 1 } 28 var j: i64 = 0 29 while j < k { rev[ro + j] = scratch[k - 1 - j]; j = j + 1 } 30 rev[ro + k] = 0x0A as u8 31 sys_write(fd, rev, ro + k + 1) 32 return 0 33} 34 35func main() -> i64 { 36 let N: i64 = K_MAGIC_200000000 37 var x: i64 = 1 38 var h: i64 = K_MAGIC_1469598103934665603 39 let start: i64 = nx_clock_monotonic_ns() 40 var i: i64 = 0 41 while i < N { 42 x = x * K_MAGIC_6364136223846793005 + K_MAGIC_1442695040888963407 43 let b: i64 = (x >> 33) & 0xff 44 h = (h ^ b) * K_MAGIC_1099511628211 45 i = i + 1 46 } 47 let end: i64 = nx_clock_monotonic_ns() 48 let us: i64 = (end - start) / 1000 49 bf_emit_i64(1, h) 50 bf_emit_i64(1, us) 51 return 0 52}