code wiki / _hdl_build / nx_selfix_test.nx

nx_selfix_test.nx source

↩ module page · 109 lines · 5652 B

1// nx_selfix_test.nx -- the team SELF-FIXES across EVERY constant. It self-selects its 2// strategy (shift-add chain for cheap constants, the imul it diagnosed it lacked for the 3// ones the chain can't reach), emits the choice, VERIFIES it 1:1 by execution, costs it, and 4// races the best of {gcc, clang} by COST. The point: the chain generator's GAPS are now 5// closed by the team itself -- it diagnosed the missing op last loop and wields it this loop. 6// Gate: every constant handled + verified by execution -> exit 0. 7 8import "nx_selfix.nx" 9import "nx_engineer_crash.nx" 10import "nx_gcc_race.nx" 11 12func sf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 13func sf_num(v: i64) -> i64 { 14 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m } 15 let t: *u8 = sys_mmap(28); var k: i64 = 0 16 if m == 0 { t[0] = 48; k = 1 } 17 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 18 if v < 0 { sf_puts("-" as *u8) } 19 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 20 sys_write(1, bb, k); return 0 21} 22func tally(r: *i64, n: i64) -> i64 { var ec: i64 = 0; var i: i64 = 0; while i < n { if r[i] != 1 { if ec == 0 { ec = i + 1 } } i = i + 1 } return ec } 23func sf_write(path: *u8, buf: *u8, len: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 - 1 } sys_write(fd, buf, len); sys_close(fd); return 0 } 24func sf_build_c(c: i64, path: *u8) -> i64 { 25 let buf: *u8 = sys_mmap(128); var oi: i64 = 0 26 let pre: *u8 = "long f(long x){return x*" as *u8 27 var j: i64 = 0; while pre[j] != (0 as u8) { buf[oi] = pre[j]; oi = oi + 1; j = j + 1 } 28 let t: *u8 = sys_mmap(28); var m: i64 = c; var k: i64 = 0 29 if m == 0 { t[0] = 48; k = 1 } 30 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 } 31 var i: i64 = 0; while i < k { buf[oi] = t[k - 1 - i]; oi = oi + 1; i = i + 1 } 32 let post: *u8 = ";}\n" as *u8 33 j = 0; while post[j] != (0 as u8) { buf[oi] = post[j]; oi = oi + 1; j = j + 1 } 34 sf_write(path, buf, oi); return 0 35} 36func sf_min(a: i64, b: i64) -> i64 { if b > 0 { if b < a { return b } } return a } 37 38// res: 0=strat 1=team_count 2=verified 3=team_cost 4=best_comp_cost 5=vd_cost 39func sf_one(c: i64, gccpath: *u8, res: *i64) -> i64 { 40 let dd: *i64 = sys_mmap(8 * 16) as *i64 41 let buf: *u8 = sys_mmap(8192); let icnt: *i64 = sys_mmap(8) as *i64; let strat: *i64 = sys_mmap(8) as *i64 42 dd[0]=5; dd[1]=0-8; dd[2]=123; dd[3]=0-456; dd[4]=77; dd[5]=0-1; dd[6]=1000; dd[7]=0-1000 43 var ref: i64 = 0; var kk: i64 = 0; while kk < 8 { ref = ref + dd[kk] * c; kk = kk + 1 }; ref = ref & 255 44 let blen: i64 = selfix_emit(c, dd, 8, buf, icnt, strat) 45 res[0] = strat[0]; res[1] = icnt[0] 46 sf_write("/tmp/sf.s" as *u8, buf, blen) 47 res[2] = 0 48 if eng_link("/tmp/sf.s" as *u8, "/tmp/sf.elf" as *u8) == 0 { if eng_run("/tmp/sf.elf" as *u8, 0 as *u8) == ref { res[2] = 1 } } 49 res[3] = gr_cost_obj("/tmp/sf.elf" as *u8, "/tmp/sf_t.txt" as *u8, "synth" as *u8) 50 sf_build_c(c, "/tmp/sf.c" as *u8) 51 gr_race_cv(gccpath, "/tmp/sf.c" as *u8, "/tmp/sf_g.o" as *u8, "/tmp/sf_g.txt" as *u8, "f" as *u8) 52 var bk: i64 = gr_cost_obj("/tmp/sf_g.o" as *u8, "/tmp/sf_gc.txt" as *u8, "f" as *u8) 53 if gr_exists("/usr/bin/clang" as *u8) == 1 { 54 gr_race_cv("/usr/bin/clang" as *u8, "/tmp/sf.c" as *u8, "/tmp/sf_c.o" as *u8, "/tmp/sf_c.txt" as *u8, "f" as *u8) 55 bk = sf_min(bk, gr_cost_obj("/tmp/sf_c.o" as *u8, "/tmp/sf_cc.txt" as *u8, "f" as *u8)) 56 } 57 res[4] = bk 58 res[5] = 0; if res[3] < bk { res[5] = 1 } if res[3] > bk { res[5] = 0 - 1 } 59 return 0 60} 61 62func sf_run(ks: *i64, nk: i64, gccpath: *u8, mat: *i64, r: *i64) -> i64 { 63 let res: *i64 = sys_mmap(8 * 8) as *i64 64 var i: i64 = 0 65 while i < nk { 66 sf_one(ks[i], gccpath, res) 67 var f: i64 = 0; while f < 6 { mat[i * 6 + f] = res[f]; f = f + 1 } 68 r[i] = 0; if res[2] == 1 { r[i] = 1 } 69 i = i + 1 70 } 71 return 0 72} 73 74func sf_report(ks: *i64, nk: i64, mat: *i64) -> i64 { 75 var k: i64 = 0 76 while k < nk { 77 sf_puts(" x*" as *u8); sf_num(ks[k]) 78 sf_puts(": team chose " as *u8); sf_puts(sfx_name(mat[k * 6 + 0])) 79 sf_puts(" (" as *u8); sf_num(mat[k * 6 + 1]); sf_puts(" insn, cost " as *u8); sf_num(mat[k * 6 + 3]); sf_puts(") vs best-comp cost " as *u8); sf_num(mat[k * 6 + 4]) 80 sf_puts(" ver=" as *u8); if mat[k * 6 + 2] == 1 { sf_puts("Y" as *u8) } else { sf_puts("N" as *u8) } 81 sf_puts(" cost " as *u8) 82 if mat[k * 6 + 5] == 1 { sf_puts("WIN\n" as *u8) } else { if mat[k * 6 + 5] == 0 { sf_puts("tie\n" as *u8) } else { sf_puts("behind\n" as *u8) } } 83 k = k + 1 84 } 85 sf_puts("----------------------------------------------------------------\n" as *u8) 86 sf_puts(" the team self-selected its strategy for EVERY constant (chain where cheap, imul where the chain\n" as *u8) 87 sf_puts(" couldn't reach) -- closing its own diagnosed gap, no me. it now handles constants its chain alone could not.\n" as *u8) 88 return 0 89} 90 91func sf_main(r: *i64) -> i64 { 92 sf_puts("=== TEAM SELF-FIX: strategy toolkit {chain, imul} with cost self-selection -- closes its own gaps ===\n" as *u8) 93 let mat: *i64 = sys_mmap(8 * 6 * 16) as *i64 94 let ks: *i64 = sys_mmap(8 * 16) as *i64 95 ks[0]=45; ks[1]=100; ks[2]=1000003; ks[3]=715827883 96 let nk: i64 = 4 97 let gcc: *u8 = gr_newest_gcc() 98 sf_run(ks, nk, gcc, mat, r) 99 sf_report(ks, nk, mat) 100 return nk 101} 102 103func main() -> i64 { 104 let rr: *i64 = sys_mmap(8 * 16) as *i64 105 let nk: i64 = sf_main(rr) 106 let ec: i64 = tally(rr, nk) 107 sys_exit(ec) 108 return ec 109}