code wiki / _hdl_build / nx_pattern_emit6_erfc.nx

nx_pattern_emit6_erfc.nx source

↩ module page · 63 lines · 4227 B

1// nx_pattern_emit6_erfc.nx -- PATTERN EMITTER: MATH_KERNEL / ERFC_CF (DLMF ch7 rung 2, 2// ME2-ERF-002). The Builder authors f64 erfc from the SOVEREIGN spec (nx_mathspec_erfc). 3// The emitter contains STRUCTURE only -- the Laplace continued fraction (backward 4// recurrence, a_k=k/2) -- and every NUMBER written into the kernel comes from the spec 5// table c[] (c[0]=1/sqrt(pi) FIRST so the tamper's litskip=0 targets the load-bearing 6// scale). Domain 1.75..6; the f64 ULP bar vs the sovereign oracle is the Engineer's 7// separate gate (_f64_erfc_gate); the emitted test checks ALGORITHM INVARIANTS only. 8// license_tier: ORIGINAL 9import "nx_pattern_emit.nx" 10import "nx_syscalls.nx" 11 12func pe6c_wlit(fd: i64, v: i64) -> i64 { 13 if v < 0 { pe_w(fd, "0 - " as *u8); pe_wn(fd, 0 - v); return 0 } 14 pe_wn(fd, v) 15 return 0 16} 17 18func pe6_author_erfc(modfile: *u8, modpath: *u8, testpath: *u8, c: *i64) -> i64 { 19 let mf: i64 = sys_openat_wr(modpath, 0x1a4) 20 if mf < 0 { return 0 } 21 pe_w(mf, "// AUTHORED BY THE NISHI BUILDER (pattern: MATH_KERNEL / ERFC_CF) -- no Claude core logic.\n" as *u8) 22 pe_w(mf, "// All constants from the SOVEREIGN bigfloat spec (nx_mathspec_erfc; 1/sqrt(pi) rail held).\n" as *u8) 23 pe_w(mf, "// Rung-2 domain 1.75..6: Laplace continued fraction (a_k=k/2, backward recurrence).\n" as *u8) 24 pe_w(mf, "import \"nx_syscalls.nx\"\nimport \"nx_f64.nx\"\nimport \"nx_f64_div.nx\"\nimport \"nx_f64_exp.nx\"\n" as *u8) 25 pe_w(mf, "func nx_f64_erfc(x: i64) -> i64 {\n" as *u8) 26 pe_w(mf, " let x2: i64 = nx_f64_mul(x, x)\n" as *u8) 27 pe_w(mf, " let e: i64 = nx_f64_exp(nx_f64_neg(x2))\n" as *u8) 28 pe_w(mf, " let pref: i64 = nx_f64_mul(e, " as *u8); pe6c_wlit(mf, c[0]); pe_w(mf, ")\n" as *u8) // 1/sqrt(pi) 29 pe_w(mf, " let n: i64 = 100\n var nf: i64 = 0\n var ci: i64 = 0\n" as *u8) 30 pe_w(mf, " while ci < n { nf = nx_f64_add(nf, " as *u8); pe6c_wlit(mf, c[2]); pe_w(mf, "); ci = ci + 1 }\n" as *u8) // 1.0 31 pe_w(mf, " var ak: i64 = nx_f64_mul(nf, " as *u8); pe6c_wlit(mf, c[1]); pe_w(mf, ")\n" as *u8) // 0.5 32 pe_w(mf, " var t: i64 = 0\n var k: i64 = n\n" as *u8) 33 pe_w(mf, " while k >= 1 {\n" as *u8) 34 pe_w(mf, " let den: i64 = nx_f64_add(x, t)\n" as *u8) 35 pe_w(mf, " t = nx_f64_div(ak, den)\n" as *u8) 36 pe_w(mf, " ak = nx_f64_sub(ak, " as *u8); pe6c_wlit(mf, c[1]); pe_w(mf, ")\n" as *u8) // 0.5 37 pe_w(mf, " k = k - 1\n }\n" as *u8) 38 pe_w(mf, " let den1: i64 = nx_f64_add(x, t)\n" as *u8) 39 pe_w(mf, " let cf: i64 = nx_f64_div(" as *u8); pe6c_wlit(mf, c[2]); pe_w(mf, ", den1)\n" as *u8) // 1.0 40 pe_w(mf, " return nx_f64_mul(pref, cf)\n}\n" as *u8) 41 sys_close(mf) 42 // ---- emitted INVARIANT test (positivity / range / monotonicity; ULP = separate gate) ---- 43 let tf: i64 = sys_openat_wr(testpath, 0x1a4) 44 if tf < 0 { return 0 } 45 pe_w(tf, "// GENERATED invariant KATs for the Builder-authored erfc (ERFC_CF).\n" as *u8) 46 pe_w(tf, "import \"" as *u8); pe_w(tf, modfile); pe_w(tf, "\"\n" as *u8) 47 pe_w(tf, "func main() -> i64 {\n var bad: i64 = 0\n" as *u8) 48 // erfc(2.0)=0x4000000000000000 must be a small POSITIVE normal, and < 0.5 49 pe_w(tf, " let a: i64 = nx_f64_erfc(0x4000000000000000)\n" as *u8) 50 pe_w(tf, " if (a >> 63) != 0 { bad = bad + 1 }\n" as *u8) // positive 51 pe_w(tf, " if a >= " as *u8); pe6c_wlit(tf, c[1]); pe_w(tf, " { bad = bad + 1 }\n" as *u8) // < 0.5 52 pe_w(tf, " if a == 0 { bad = bad + 1 }\n" as *u8) // nonzero 53 // strict monotone decreasing: erfc(2) > erfc(3) > erfc(4) (positive normals -> bits order) 54 pe_w(tf, " let b: i64 = nx_f64_erfc(0x4008000000000000)\n" as *u8) // 3.0 55 pe_w(tf, " let d: i64 = nx_f64_erfc(0x4010000000000000)\n" as *u8) // 4.0 56 pe_w(tf, " if a <= b { bad = bad + 1 }\n" as *u8) 57 pe_w(tf, " if b <= d { bad = bad + 1 }\n" as *u8) 58 pe_w(tf, " if (b >> 63) != 0 { bad = bad + 1 }\n" as *u8) 59 pe_w(tf, " if (d >> 63) != 0 { bad = bad + 1 }\n" as *u8) 60 pe_w(tf, " sys_exit(bad)\n return bad\n}\n" as *u8) 61 sys_close(tf) 62 return 1 63}