code wiki / _hdl_build / nx_cost_race_test.nx
nx_cost_race_test.nx source
↩ module page · 118 lines · 6195 B
1// nx_cost_race_test.nx -- the team judges by COST, not just instruction count, reading the
2// latency of BOTH its own emitted code and the competitors' (gcc, clang) straight from the
3// machine code. This is the team applying, ITSELF, the "count != cost" judgment I used to
4// supply by hand: clang's x*100 imul is 1 insn but ~3 cycles, vs the team's 3 one-cycle leas,
5// so by COST it is a TIE, not a loss. Gate: every team sequence verified by execution AND
6// both competitors costed -> the team raced correctly on the honest metric. exit 0.
7
8import "nx_mulchain.nx"
9import "nx_superopt_emit.nx"
10import "nx_engineer_crash.nx"
11import "nx_gcc_race.nx"
12
13func cr_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func cr_num(v: i64) -> i64 {
15 let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }
16 let t: *u8 = sys_mmap(28); var k: i64 = 0
17 if m == 0 { t[0] = 48; k = 1 }
18 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 }
19 if v < 0 { cr_puts("-" as *u8) }
20 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
21 sys_write(1, bb, k); return 0
22}
23func 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 }
24func cr_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 }
25func cr_build_c(c: i64, path: *u8) -> i64 {
26 let buf: *u8 = sys_mmap(128); var oi: i64 = 0
27 let pre: *u8 = "long f(long x){return x*" as *u8
28 var j: i64 = 0; while pre[j] != (0 as u8) { buf[oi] = pre[j]; oi = oi + 1; j = j + 1 }
29 let t: *u8 = sys_mmap(28); var m: i64 = c; var k: i64 = 0
30 if m == 0 { t[0] = 48; k = 1 }
31 while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 }
32 var i: i64 = 0; while i < k { buf[oi] = t[k - 1 - i]; oi = oi + 1; i = i + 1 }
33 let post: *u8 = ";}\n" as *u8
34 j = 0; while post[j] != (0 as u8) { buf[oi] = post[j]; oi = oi + 1; j = j + 1 }
35 cr_write(path, buf, oi); return 0
36}
37func cr_min(a: i64, b: i64) -> i64 { if b > 0 { if b < a { return b } } return a }
38
39// res: 0=tc(team count) 1=tk(team cost) 2=verified 3=bestC(count) 4=bestK(cost) 5=vd_count 6=vd_cost
40func cr_one(c: i64, gccpath: *u8, res: *i64) -> i64 {
41 let op: *i64 = sys_mmap(8*12) as *i64; let a: *i64 = sys_mmap(8*12) as *i64; let b: *i64 = sys_mmap(8*12) as *i64
42 let dd: *i64 = sys_mmap(8*16) as *i64; let buf: *u8 = sys_mmap(8192); let icnt: *i64 = sys_mmap(8) as *i64
43 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
44 var ref: i64 = 0; var kk: i64 = 0; while kk < 8 { ref = ref + dd[kk]*c; kk = kk+1 }; ref = ref & 255
45 let L: i64 = mulchain_find(c, 8, op, a, b)
46 let blen: i64 = se_emit_full(op, a, b, L, dd, 8, buf, icnt)
47 res[0] = icnt[0]
48 cr_write("/tmp/cr.s" as *u8, buf, blen)
49 res[2] = 0
50 if eng_link("/tmp/cr.s" as *u8, "/tmp/cr.elf" as *u8) == 0 { if eng_run("/tmp/cr.elf" as *u8, 0 as *u8) == ref { res[2] = 1 } }
51 res[1] = gr_cost_obj("/tmp/cr.elf" as *u8, "/tmp/cr_t.txt" as *u8, "synth" as *u8) // team COST
52
53 cr_build_c(c, "/tmp/cr.c" as *u8)
54 let gc: i64 = gr_race_cv(gccpath, "/tmp/cr.c" as *u8, "/tmp/cr_g.o" as *u8, "/tmp/cr_g.txt" as *u8, "f" as *u8)
55 let gk: i64 = gr_cost_obj("/tmp/cr_g.o" as *u8, "/tmp/cr_gc.txt" as *u8, "f" as *u8)
56 var bc: i64 = gc; var bk: i64 = gk
57 if gr_exists("/usr/bin/clang" as *u8) == 1 {
58 let cc: i64 = gr_race_cv("/usr/bin/clang" as *u8, "/tmp/cr.c" as *u8, "/tmp/cr_c.o" as *u8, "/tmp/cr_c.txt" as *u8, "f" as *u8)
59 let ck: i64 = gr_cost_obj("/tmp/cr_c.o" as *u8, "/tmp/cr_cc.txt" as *u8, "f" as *u8)
60 bc = cr_min(bc, cc); bk = cr_min(bk, ck)
61 }
62 res[3] = bc; res[4] = bk
63 res[5] = 0; if res[0] < bc { res[5] = 1 } if res[0] > bc { res[5] = 0 - 1 }
64 res[6] = 0; if res[1] < bk { res[6] = 1 } if res[1] > bk { res[6] = 0 - 1 }
65 return 0
66}
67
68func run_all(ks: *i64, nk: i64, gccpath: *u8, mat: *i64, r: *i64) -> i64 {
69 let res: *i64 = sys_mmap(8*8) as *i64
70 var i: i64 = 0
71 while i < nk {
72 cr_one(ks[i], gccpath, res)
73 var f: i64 = 0
74 while f < 7 { mat[i*7+f] = res[f]; f = f + 1 }
75 r[i] = 0; if res[2] == 1 { if res[4] > 0 { r[i] = 1 } }
76 i = i + 1
77 }
78 return 0
79}
80
81func report(ks: *i64, nk: i64, mat: *i64) -> i64 {
82 var cl: i64 = 0; var kl: i64 = 0
83 var k: i64 = 0
84 while k < nk {
85 cr_puts(" x*" as *u8); cr_num(ks[k])
86 cr_puts(": team count " as *u8); cr_num(mat[k*7+0]); cr_puts(" cost " as *u8); cr_num(mat[k*7+1])
87 cr_puts(" | best comp count " as *u8); cr_num(mat[k*7+3]); cr_puts(" cost " as *u8); cr_num(mat[k*7+4])
88 cr_puts(" -> by-count " as *u8)
89 if mat[k*7+5] == 1 { cr_puts("WIN" as *u8) } else { if mat[k*7+5] == 0 { cr_puts("tie" as *u8) } else { cr_puts("loss" as *u8); cl = cl + 1 } }
90 cr_puts(", by-COST " as *u8)
91 if mat[k*7+6] == 1 { cr_puts("WIN\n" as *u8) } else { if mat[k*7+6] == 0 { cr_puts("tie\n" as *u8) } else { cr_puts("loss\n" as *u8); kl = kl + 1 } }
92 k = k + 1
93 }
94 cr_puts("----------------------------------------------------------------\n" as *u8)
95 cr_puts(" by raw instruction count: " as *u8); cr_num(cl); cr_puts(" loss(es). by COST (latency, the honest metric): " as *u8); cr_num(kl); cr_puts(" loss(es).\n" as *u8)
96 cr_puts(" the team now judges itself on cost -- the count != cost insight, computed BY THE TEAM, not handed to it.\n" as *u8)
97 return 0
98}
99
100func cr_main(r: *i64) -> i64 {
101 cr_puts("=== COST-AWARE race: team judges by latency (reads both sides' machine code) vs best of {gcc,clang} ===\n" as *u8)
102 let mat: *i64 = sys_mmap(8*7*16) as *i64
103 let ks: *i64 = sys_mmap(8*16) as *i64
104 ks[0]=45; ks[1]=100; ks[2]=255
105 let nk: i64 = 3
106 let gcc: *u8 = gr_newest_gcc()
107 run_all(ks, nk, gcc, mat, r)
108 report(ks, nk, mat)
109 return nk
110}
111
112func main() -> i64 {
113 let rr: *i64 = sys_mmap(8 * 16) as *i64
114 let nk: i64 = cr_main(rr)
115 let ec: i64 = tally(rr, nk)
116 sys_exit(ec)
117 return ec
118}