code wiki / _hdl_build / nx_race_native_opt.nx
nx_race_native_opt.nx source
↩ module page · 38 lines · 1564 B
1// nx_race_native_opt.nx -- "OURS, OPTIMIZED": the SAME MINSTD LCG as nx_race_native, but with the
2// mod-by-(2^31-1) DIVIDE replaced by MERSENNE STRENGTH-REDUCTION (shifts/adds) -- exactly the optimization
3// gcc -O2 applies automatically. Since 2^31 == 1 (mod 2^31-1), p mod (2^31-1) = fold the 31-bit halves.
4// PURPOSE: prove the measured ~1.7x Nishi-vs-gcc-O2 gap is PURELY auto-strength-reduction (nx_cc doesn't do
5// it yet) -- when the optimization is expressed, Nishi's sovereign codegen reaches the optimizing-gcc frontier.
6// MUST print 668950819 (identical to the divide version) or the reduction is wrong. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8const K_MAGIC_50000000: i64 = 50000000
9const K_MAGIC_48271: i64 = 48271
10const K_MAGIC_2147483647: i64 = 2147483647
11func putn(v: i64) -> i64 {
12 let bb: *u8 = sys_mmap(28)
13 var m: i64 = v
14 if m < 0 { m = 0 - m }
15 let t: *u8 = sys_mmap(28)
16 var k: i64 = 0
17 if m == 0 { t[0] = (48 as u8); k = 1 }
18 while m > 0 { t[k] = ((48 + (m % 10)) as u8); m = m / 10; k = k + 1 }
19 var i: i64 = 0
20 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
21 sys_write(1, bb, k)
22 return 0
23}
24func main() -> i64 {
25 var h: i64 = 1
26 var i: i64 = 1
27 while i <= K_MAGIC_50000000 {
28 let p: i64 = h * K_MAGIC_48271
29 var s: i64 = (p & K_MAGIC_2147483647) + (p >> 31)
30 s = (s & K_MAGIC_2147483647) + (s >> 31)
31 if s >= K_MAGIC_2147483647 { s = s - K_MAGIC_2147483647 }
32 h = s
33 i = i + 1
34 }
35 putn(h)
36 sys_write(1, "\n" as *u8, 1)
37 return 0
38}