code wiki / _hdl_build / nx_math_gate_emit.nx

nx_math_gate_emit.nx source

↩ module page · 117 lines · 7053 B

1// nx_math_gate_emit.nx -- PATTERN EMITTER: MATH_GATE (GATE_AUTHORING shape). 2// The Builder authors a live-oracle ULP gate from DATA -- edge raws, band 3// descriptors, function/oracle names -- with the PROVEN helper templates baked 4// in. Motivation (measured, 2026-06-10 session): every defect found while 5// closing ch4 was in a HAND-WRITTEN gate, never in a team organ -- 6// (1) unmasked >>63 sign-extension, (2) flat identity bars at large args, 7// (3) raw-literal arithmetic slips. Template (1) is impossible here (the 8// helpers mask), (3) shrinks to the data rows, (2) stays oracle-gate domain. 9// Emits a SINGLE-FUNCTION gate: kernelfn vs oraclefn, banded xorshift points + 10// edge list, signed-ordered-map ULP, GREEN iff worse=0. 11// strs: [0]=gatepath [1]=MARKER [2]=kernelfn [3]=oraclefn [4..4+ni) imports 12// nums: [0]=ni [1]=seed [2]=npoints [3]=signrandom [4]=nedges 13// [5..5+nedges) edge raws, then 8 = 4 x (efbase, efmask) 14// license_tier: ORIGINAL 15 16import "nx_syscalls.nx" 17import "nx_pattern_emit.nx" 18 19func mge_wlit(fd: i64, v: i64) -> i64 { 20 if v < 0 { 21 pe_w(fd, "0 - " as *u8) 22 pe_wn(fd, 0 - v) 23 return 0 24 } 25 pe_wn(fd, v) 26 return 0 27} 28 29func mge_author(strs: *i64, nums: *i64) -> i64 { 30 let mf: i64 = sys_openat_wr(strs[0] as *u8, 0x1a4); if mf < 0 { return 0 } 31 let mark: *u8 = strs[1] as *u8 32 pe_w(mf, "// AUTHORED BY THE NISHI BUILDER (pattern: MATH_GATE) -- no Claude core logic.\n" as *u8) 33 pe_w(mf, "// Live-oracle ULP gate from the data spec; helper templates proven-and-baked.\n" as *u8) 34 pe_w(mf, "import \"nx_syscalls.nx\"\nimport \"nx_f64.nx\"\nimport \"nx_f64_div.nx\"\nimport \"nx_f64_cvt.nx\"\n" as *u8) 35 var i: i64 = 0 36 while i < nums[0] { 37 pe_w(mf, "import \"" as *u8) 38 pe_w(mf, strs[4 + i] as *u8) 39 pe_w(mf, "\"\n" as *u8) 40 i = i + 1 41 } 42 pe_w(mf, "func mg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }\n" as *u8) 43 pe_w(mf, "func mg_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, \"-\" as *u8, 1) }; let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48; k = 1 }; while m > 0 { t[k] = 48 + (m % 10); m = m / 10; k = k + 1 }; var i: i64 = 0; while i < k { bb[i] = t[k-1-i]; i = i + 1 }; sys_write(1, bb, k); return 0 }\n" as *u8) 44 pe_w(mf, "func mg_puthex(v: i64) -> i64 { let bb: *u8 = sys_mmap(20); var i: i64 = 0; while i < 16 { let nib: i64 = (v >> ((15 - i) * 4)) & 15; if nib < 10 { bb[i] = 48 + nib } else { bb[i] = 55 + nib } i = i + 1 } sys_write(1, bb, 16); return 0 }\n" as *u8) 45 pe_w(mf, "func mg_rng(state: *i64) -> i64 {\n" as *u8) 46 pe_w(mf, " var s: i64 = state[0]\n" as *u8) 47 pe_w(mf, " s = s ^ ((s >> 12) & 0x000FFFFFFFFFFFFF)\n" as *u8) 48 pe_w(mf, " s = s ^ (s << 25)\n" as *u8) 49 pe_w(mf, " s = s ^ ((s >> 27) & 0x0000001FFFFFFFFF)\n" as *u8) 50 pe_w(mf, " state[0] = s\n return s * 2685821657736338717\n}\n" as *u8) 51 pe_w(mf, "func mg_ord(v: i64) -> i64 {\n if v >= 0 { return v }\n return (1 << 63) - v\n}\n" as *u8) 52 pe_w(mf, "func mg_ulp(got: i64, want: i64) -> i64 {\n" as *u8) 53 pe_w(mf, " if got == want { return 0 }\n" as *u8) 54 pe_w(mf, " var u: i64 = mg_ord(got) - mg_ord(want)\n" as *u8) 55 pe_w(mf, " if u < 0 { u = 0 - u }\n return u\n}\n" as *u8) 56 pe_w(mf, "func main() -> i64 {\n" as *u8) 57 pe_w(mf, " let xs: *i64 = sys_mmap(8 * 1024) as *i64\n" as *u8) 58 pe_w(mf, " var n: i64 = 0\n" as *u8) 59 let ne: i64 = nums[4] 60 i = 0 61 while i < ne { 62 pe_w(mf, " xs[n] = " as *u8); mge_wlit(mf, nums[5 + i]); pe_w(mf, "; n = n + 1\n" as *u8) 63 i = i + 1 64 } 65 pe_w(mf, " let st: *i64 = sys_mmap(16) as *i64\n" as *u8) 66 pe_w(mf, " st[0] = " as *u8); mge_wlit(mf, nums[1]); pe_w(mf, "\n" as *u8) 67 pe_w(mf, " var j: i64 = 0\n" as *u8) 68 pe_w(mf, " while j < " as *u8); pe_wn(mf, nums[2]); pe_w(mf, " {\n" as *u8) 69 pe_w(mf, " var raw: i64 = mg_rng(st)\n" as *u8) 70 pe_w(mf, " var efr: i64 = 0\n" as *u8) 71 pe_w(mf, " let band: i64 = raw & 3\n" as *u8) 72 let bb: i64 = 5 + ne 73 i = 0 74 while i < 4 { 75 pe_w(mf, " if band == " as *u8); pe_wn(mf, i) 76 pe_w(mf, " { efr = " as *u8); pe_wn(mf, nums[bb + 2 * i]) 77 pe_w(mf, " + (raw & " as *u8); pe_wn(mf, nums[bb + 2 * i + 1]) 78 pe_w(mf, ") }\n" as *u8) 79 i = i + 1 80 } 81 pe_w(mf, " raw = (efr << 52) | (mg_rng(st) & 0x000FFFFFFFFFFFFF)\n" as *u8) 82 if nums[3] == 1 { 83 pe_w(mf, " if (mg_rng(st) & 1) == 1 { raw = raw | (1 << 63) }\n" as *u8) 84 } 85 pe_w(mf, " xs[n] = raw\n n = n + 1\n j = j + 1\n }\n" as *u8) 86 pe_w(mf, " var g_exact: i64 = 0\n var g_u1: i64 = 0\n var g_worse: i64 = 0\n var g_max: i64 = 0\n" as *u8) 87 pe_w(mf, " var i: i64 = 0\n" as *u8) 88 pe_w(mf, " while i < n {\n" as *u8) 89 pe_w(mf, " let x: i64 = xs[i]\n" as *u8) 90 pe_w(mf, " let want: i64 = " as *u8); pe_w(mf, strs[3] as *u8); pe_w(mf, "(x)\n" as *u8) 91 pe_w(mf, " let got: i64 = " as *u8); pe_w(mf, strs[2] as *u8); pe_w(mf, "(x)\n" as *u8) 92 pe_w(mf, " let u: i64 = mg_ulp(got, want)\n" as *u8) 93 pe_w(mf, " if u == 0 { g_exact = g_exact + 1 }\n" as *u8) 94 pe_w(mf, " if u == 1 { g_u1 = g_u1 + 1 }\n" as *u8) 95 pe_w(mf, " if u > 1 {\n g_worse = g_worse + 1\n" as *u8) 96 pe_w(mf, " if g_worse <= 20 {\n" as *u8) 97 pe_w(mf, " mg_puts(\"" as *u8); pe_w(mf, mark); pe_w(mf, "BAD x=\" as *u8); mg_puthex(x)\n" as *u8) 98 pe_w(mf, " mg_puts(\" exp=\" as *u8); mg_puthex(want)\n" as *u8) 99 pe_w(mf, " mg_puts(\" got=\" as *u8); mg_puthex(got)\n" as *u8) 100 pe_w(mf, " mg_puts(\" ulp=\" as *u8); mg_putn(u)\n" as *u8) 101 pe_w(mf, " mg_puts(\"\\n\" as *u8)\n }\n }\n" as *u8) 102 pe_w(mf, " if u > g_max { g_max = u }\n" as *u8) 103 pe_w(mf, " i = i + 1\n }\n" as *u8) 104 pe_w(mf, " mg_puts(\"" as *u8); pe_w(mf, mark); pe_w(mf, "-ULP total=\" as *u8); mg_putn(n)\n" as *u8) 105 pe_w(mf, " mg_puts(\" exact=\" as *u8); mg_putn(g_exact)\n" as *u8) 106 pe_w(mf, " mg_puts(\" ulp1=\" as *u8); mg_putn(g_u1)\n" as *u8) 107 pe_w(mf, " mg_puts(\" worse=\" as *u8); mg_putn(g_worse)\n" as *u8) 108 pe_w(mf, " mg_puts(\" max=\" as *u8); mg_putn(g_max)\n" as *u8) 109 pe_w(mf, " mg_puts(\"\\n\" as *u8)\n" as *u8) 110 pe_w(mf, " if g_worse == 0 {\n" as *u8) 111 pe_w(mf, " mg_puts(\"" as *u8); pe_w(mf, mark); pe_w(mf, "-GATE verdict=GREEN\\n\" as *u8)\n" as *u8) 112 pe_w(mf, " return 0\n }\n" as *u8) 113 pe_w(mf, " mg_puts(\"" as *u8); pe_w(mf, mark); pe_w(mf, "-GATE verdict=RED\\n\" as *u8)\n" as *u8) 114 pe_w(mf, " var rc: i64 = g_worse\n if rc > 100 { rc = 100 }\n return rc\n}\n" as *u8) 115 sys_close(mf) 116 return 1 117}