code wiki / _hdl_build / nx_pattern_emit6_log2.nx
nx_pattern_emit6_log2.nx source
↩ module page · 68 lines · 5475 B
1// nx_pattern_emit6_log2.nx -- PATTERN EMITTER: MATH_KERNEL / LOG2_REDUCE (math-engine
2// repertoire-growth rep -- a NEW emitter-kid past the 9 existing pe6 templates).
3// PROVENANCE: template tutor-SUBMITTED (Claude) for COUNCIL approval; author=TUTOR pending
4// EoE back-fill. The KERNEL it begets is BUILDER/emitter-authored + RECONSTRUCTABLE
5// (re-beget => byte-identical). It REUSES the SOVEREIGN _pm_log_spec (oracle-generated,
6// mpmath@60dps) verbatim -- ZERO new/hand-typed constants. Clones pe6_author_logf64's
7// LOG_REDUCE reduction+poly and changes ONLY the final recombine:
8// log2(x) = log2(2^k * m) = k + ln(m)/ln2 (ln2 reconstructed from spec c[2]+c[3]).
9// LAWS: flat ifs, no &&/||, <=6 args per func, one transcendental per emitted file.
10// license_tier: ORIGINAL
11import "nx_pattern_emit.nx"
12import "nx_syscalls.nx"
13
14func pe6_author_log2(name: *u8, modfile: *u8, modpath: *u8, testpath: *u8, c: *i64, ncoef: i64) -> i64 {
15 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 }
16 pe_w(mf, "// AUTHORED BY THE NISHI BUILDER (pattern: MATH_KERNEL / LOG2_REDUCE).\n" as *u8)
17 pe_w(mf, "// Base-2 log, binary64 bits-up. Structure from LOG_REDUCE; constants from the\n" as *u8)
18 pe_w(mf, "// sovereign _pm_log_spec (oracle-generated). log2(x)=k+ln(m)/ln2.\n" as *u8)
19 pe_w(mf, "import \"nx_syscalls.nx\"\nimport \"nx_tier.nx\"\nimport \"nx_f64.nx\"\nimport \"nx_f64_div.nx\"\nimport \"nx_f64_cvt.nx\"\n" as *u8)
20 pe_w(mf, "func " as *u8); pe_w(mf, name); pe_w(mf, "(x: i64) -> i64 {\n" as *u8)
21 pe_w(mf, " let cls: nx_int = nx_f64_classify(x)\n" as *u8)
22 pe_w(mf, " if cls == NX_F64_CLS_NAN { return NX_F64_NAN_RAW }\n" as *u8)
23 pe_w(mf, " if cls == NX_F64_CLS_ZERO { return 0xFFF0000000000000 }\n" as *u8)
24 pe_w(mf, " if nx_f64_sign(x) == 1 { return NX_F64_NAN_RAW }\n" as *u8)
25 pe_w(mf, " if cls == NX_F64_CLS_INF { return x }\n" as *u8)
26 pe_w(mf, " var sig: i64 = nx_f64_mant_field(x)\n" as *u8)
27 pe_w(mf, " var ef: i64 = nx_f64_exp_field(x)\n" as *u8)
28 pe_w(mf, " if ef == 0 {\n ef = 1\n while sig < NX_F64_IMPLICIT_1 { sig = sig << 1; ef = ef - 1 }\n } else {\n sig = sig | NX_F64_IMPLICIT_1\n }\n" as *u8)
29 pe_w(mf, " var k: i64 = ef - 1023\n var mexp: i64 = 1023\n" as *u8)
30 pe_w(mf, " if sig >= " as *u8); pe_wn(mf, c[1]); pe_w(mf, " { k = k + 1; mexp = 1022 }\n" as *u8)
31 pe_w(mf, " let mfv: i64 = (mexp << 52) | (sig & NX_F64_MANT_MASK)\n" as *u8)
32 pe_w(mf, " let f: i64 = nx_f64_sub(mfv, " as *u8); pe_wn(mf, c[0]); pe_w(mf, ")\n" as *u8)
33 pe_w(mf, " let two: i64 = nx_f64_add(" as *u8); pe_wn(mf, c[0]); pe_w(mf, ", " as *u8); pe_wn(mf, c[0]); pe_w(mf, ")\n" as *u8)
34 pe_w(mf, " let s: i64 = nx_f64_div(f, nx_f64_add(two, f))\n" as *u8)
35 pe_w(mf, " let z: i64 = nx_f64_mul(s, s)\n" as *u8)
36 pe_w(mf, " var p: i64 = " as *u8); pe_wn(mf, c[4]); pe_w(mf, "\n" as *u8)
37 var i: i64 = 1
38 while i < ncoef {
39 pe_w(mf, " p = nx_f64_add(nx_f64_mul(p, z), " as *u8); pe_wn(mf, c[4 + i]); pe_w(mf, ")\n" as *u8)
40 i = i + 1
41 }
42 pe_w(mf, " let r: i64 = nx_f64_mul(z, p)\n" as *u8)
43 pe_w(mf, " let lnm: i64 = nx_f64_sub(f, nx_f64_mul(s, nx_f64_sub(f, r)))\n" as *u8)
44 pe_w(mf, " let kf: i64 = nx_i64_to_f64(k)\n" as *u8)
45 pe_w(mf, " let ln2: i64 = nx_f64_add(" as *u8); pe_wn(mf, c[2]); pe_w(mf, ", " as *u8); pe_wn(mf, c[3]); pe_w(mf, ")\n" as *u8)
46 pe_w(mf, " return nx_f64_add(kf, nx_f64_div(lnm, ln2))\n}\n" as *u8)
47 sys_close(mf)
48
49 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 }
50 pe_w(tf, "// AUTHORED BY THE NISHI BUILDER (pattern: MATH_KERNEL / LOG2 test) -- KATs from algorithm invariants.\n" as *u8)
51 pe_w(tf, "import \"" as *u8); pe_w(tf, modfile); pe_w(tf, "\"\nimport \"nx_syscalls.nx\"\n" as *u8)
52 pe_w(tf, "func main() -> i64 {\n var bad: i64 = 0\n" as *u8)
53 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(" as *u8); pe_wn(tf, c[0]); pe_w(tf, ") != 0 { bad = bad + 1 }\n" as *u8)
54 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0) != 0xFFF0000000000000 { bad = bad + 1 }\n" as *u8)
55 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0x8000000000000000) != 0xFFF0000000000000 { bad = bad + 1 }\n" as *u8)
56 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0xBFF0000000000000) != 0x7FF8000000000000 { bad = bad + 1 }\n" as *u8)
57 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0x7FF0000000000000) != 0x7FF0000000000000 { bad = bad + 1 }\n" as *u8)
58 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0x7FF8000000000000) != 0x7FF8000000000000 { bad = bad + 1 }\n" as *u8)
59 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0x4000000000000000) != 0x3FF0000000000000 { bad = bad + 1 }\n" as *u8)
60 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0x4010000000000000) != 0x4000000000000000 { bad = bad + 1 }\n" as *u8)
61 pe_w(tf, " if " as *u8); pe_w(tf, name); pe_w(tf, "(0x3FE0000000000000) != 0xBFF0000000000000 { bad = bad + 1 }\n" as *u8)
62 pe_w(tf, " if bad == 0 { sys_write(1, \"MATHKERNEL-LOG2-TEST GREEN\" as *u8, 26) } else { sys_write(1, \"MATHKERNEL-LOG2-TEST RED\" as *u8, 24) }\n" as *u8)
63 pe_w(tf, " let nl: *u8 = sys_mmap(4)\n nl[0] = 10\n" as *u8)
64 pe_w(tf, " sys_write(1, nl, 1)\n" as *u8)
65 pe_w(tf, " sys_exit(bad)\n return bad\n}\n" as *u8)
66 sys_close(tf)
67 return 1
68}