code wiki / _hdl_build / nx_author_test.nx
nx_author_test.nx source
↩ module page · 49 lines · 2277 B
1// nx_author_test.nx -- the team AUTHORS a new primitive from its own diagnosis, by EXPERIMENT.
2// It is given ONLY the mnemonic it diagnosed as missing ("imulq") -- not what it does. It
3// probes the instruction on 5 (K,x) examples, observes the outputs by RUNNING them, and
4// INFERS the semantics by testing hypotheses. It learns, with no one telling it, that imul
5// computes MULTIPLY -- the team teaches itself a new op. Known answer: learned == MULTIPLY.
6
7import "nx_author.nx"
8
9func at_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
10func at_num(v: i64) -> i64 {
11 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }
12 let t: *u8 = sys_mmap(28); var k: i64 = 0
13 if m == 0 { t[0] = 48; k = 1 }
14 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 }
15 if v < 0 { at_puts("-" as *u8) }
16 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
17 sys_write(1, bb, k); return 0
18}
19
20func at_main(r: *i64) -> i64 {
21 at_puts("=== TEAM AUTHORS a new primitive by EXPERIMENT (diagnosed mnemonic 'imul', not told what it does) ===\n" as *u8)
22 let Ks: *i64 = sys_mmap(8 * 8) as *i64
23 let Xs: *i64 = sys_mmap(8 * 8) as *i64
24 let obs: *i64 = sys_mmap(8 * 8) as *i64
25 Ks[0]=3; Ks[1]=7; Ks[2]=5; Ks[3]=11; Ks[4]=6
26 Xs[0]=5; Xs[1]=4; Xs[2]=9; Xs[3]=3; Xs[4]=7
27 let learned: i64 = au_learn("imulq" as *u8, Ks, Xs, 5, obs)
28 var i: i64 = 0
29 while i < 5 {
30 at_puts(" probe: imulq $" as *u8); at_num(Ks[i]); at_puts(", x=" as *u8); at_num(Xs[i])
31 at_puts(" -> observed " as *u8); at_num(obs[i]); at_puts("\n" as *u8)
32 i = i + 1
33 }
34 at_puts(" the team tested 7 hypotheses against the observations and LEARNED: imul computes " as *u8)
35 at_puts(au_name(learned)); at_puts("\n" as *u8)
36 at_puts("----------------------------------------------------------------\n" as *u8)
37 at_puts(" no one told the team what imul does -- it ran the instruction and inferred the semantics ITSELF.\n" as *u8)
38 r[0] = 0; if learned == AU_MUL { r[0] = 1 }
39 return 1
40}
41
42func main() -> i64 {
43 let r: *i64 = sys_mmap(8 * 4) as *i64
44 let n: i64 = at_main(r)
45 var ec: i64 = 0
46 if r[0] != 1 { ec = 1 }
47 sys_exit(ec)
48 return ec
49}