code wiki / _hdl_build / nx_karatsuba_test.nx
nx_karatsuba_test.nx
buildroot/runtime/_hdl_build/nx_karatsuba_test.nx
about
nx_karatsuba_test.nx -- ALGORITHM-LEVEL search space #2: Karatsuba multiplication. To
multiply x*y, split each into halves (x=x1*B+x0): the schoolbook way needs 4 sub-multiplies
(x0y0, x0y1, x1y0, x1y1); Karatsuba needs only 3 (z0=x0y0, z2=x1y1, z1=(x0+x1)(y0+y1)-z0-z2),
trading a multiply for cheap adds. NO compiler turns schoolbook into Karatsuba -- the win is
ALGORITHMIC -- so the team's Karatsuba beats whatever gcc/clang/rust emit for the naive
bignum multiply, on the right operand sizes. Verified here BY EXECUTION: karatsuba == native
over many inputs, and it uses 3 multiplies not 4. (16-bit operands, 8-bit halves, fits i64.)
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 11 | const KB: i64 = 256 // half-base 2^8 |
functions
| 14 | func karatsuba(x: i64, y: i64, mc: *i64) -> i64 called by 1: ka_main |
| 23 | func ka_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } |
| 24 | func ka_num(v: i64) -> i64 |
| 33 | func ka_main(r: *i64) -> i64 |
| 56 | func main() -> i64 |