code wiki / _hdl_build / nx_modtest.nx

nx_modtest.nx source

↩ module page · 19 lines · 1011 B

1// nx_modtest.nx -- MODOPT-001 gate witness: a hot loop with x % 2^k (% 1024). 2// Production compiler emits IDIV (slow); with the opt_strength_reduce % 2^k -> & (2^k-1) patch the 3// compiler should emit AND (fast). The RESULT must be identical either way (correctness); the .s 4// changes idivq -> andq (the win). i is a loop counter (>= 1) so VRA should prove non-negative. 5import "nx_syscalls.nx" 6import "nx_estr.nx" 7const K_MAGIC_50000000: i64 = 50000000 8const K_MAGIC_1024: i64 = 1024 9// L010 EXEMPLAR MIGRATION 2026-07-21: the local putn (a redundant reimplementation of the emit primitive) 10// was removed; this organ now inherits es_putn from the nx_estr shared base (es_putn is a strict superset -- 11// it also handles negatives; acc here is always positive so the output is byte-identical, verified). 12func main() -> i64 { 13 var acc: i64 = 0 14 var i: i64 = 1 15 while i <= K_MAGIC_50000000 { acc = acc + (i % K_MAGIC_1024); i = i + 1 } 16 es_putn(acc) 17 sys_write(1, "\n" as *u8, 1) 18 return 0 19}