code wiki / _hdl_build / nx_div_pick_test.nx

nx_div_pick_test.nx source

↩ module page · 44 lines · 2167 B

1// nx_div_pick_test.nx -- proves the hardware-adaptive divider picker selects the 2// OPTIMAL divider per target, reproducing the honest crossover BY MEASUREMENT 3// (not hardcoded). The Seed's "grow to the hardware" mechanism, gated. 4// 5// From the proven honest latency race (nx_latency_metric_test): 6// W=16: r2=181 r4=139 Newton-shallow=166 Newton-deep=363 -> radix-4 optimal 7// W=64: r2=967 r4=743 Newton-shallow=320 Newton-deep=709 -> Newton optimal 8// So the picker must choose: W16 -> R4 (1) for BOTH multiplier models; W64 -> 9// NEWTON (2) for BOTH (shallow 320 and deep 709 both beat r4's 743). The sign-flip 10// R4->NEWTON across width IS the multiplicative-divider frontier, chosen optimally. 11// 12// Known answer (FAIL LOUD): "1 1 2 2" rc=0 (p16_shallow p16_deep p64_shallow p64_deep). 13 14import "nx_div_pick.nx" 15 16func _emit_num(v: i64) -> i64 { 17 let b: *u8 = sys_mmap(28); var n: i64 = v; if n < 0 { n = 0 - n } 18 let t2: *u8 = sys_mmap(28); var t: i64 = 0 19 if n == 0 { t2[0] = 48; t = 1 } 20 while n > 0 { t2[t] = 48 + (n % 10); n = n / 10; t = t + 1 } 21 var i: i64 = 0; while i < t { b[i] = t2[t - 1 - i]; i = i + 1 } 22 b[t] = 32; sys_write(1, b, t + 1); return 0 23} 24func _nl() -> i64 { let z: *u8 = sys_mmap(2); z[0] = 10; sys_write(1, z, 1); return 0 } 25 26func main() -> i64 { 27 let depth: *i64 = sys_mmap(8192 * 8) as *i64 28 29 let p16s: i64 = nx_div_pick(16, 0, depth) // W=16, shallow (Wallace) multiplier 30 let p16d: i64 = nx_div_pick(16, 1, depth) // W=16, deep (limb-schoolbook) multiplier 31 let p64s: i64 = nx_div_pick(64, 0, depth) // W=64, shallow multiplier 32 let p64d: i64 = nx_div_pick(64, 1, depth) // W=64, deep multiplier 33 34 _emit_num(p16s); _emit_num(p16d); _emit_num(p64s); _emit_num(p64d); _nl() 35 36 // Small width: radix-4 is optimal regardless of multiplier model (log W 37 // doesn't pay off). Large width: Newton/Goldschmidt is optimal for both. 38 if p16s != NXDP_R4 { sys_exit(1); return 1 } 39 if p16d != NXDP_R4 { sys_exit(2); return 2 } 40 if p64s != NXDP_NEWTON { sys_exit(3); return 3 } 41 if p64d != NXDP_NEWTON { sys_exit(4); return 4 } 42 sys_exit(0) 43 return 0 44}