code wiki / _hdl_build / _bf_erf_gate_authored.nx
_bf_erf_gate_authored.nx source
↩ module page · 151 lines · 6149 B
1// _bf_erf_gate_authored.nx -- the ENGINEER's INDEPENDENT semantic gate for the
2// Builder-authored erf (ME2-ERF-001 component 4/5). Judge independence: this gate
3// RE-DERIVES 2/sqrt(pi) itself (bf_pi + Newton sqrt -- not read from the spec) and
4// evaluates erf at every grid point with a 44-term 120-bit series, then compares the
5// kernel's f64 BITS against the bf-rounded oracle. RUNG-1 BAR: max ULP <= 4 over
6// x = k/64 for k=1..112 (0.015625 .. 1.75 inclusive) + k/4096 for k=1..8 (small-x).
7// MAGNITUDE-ONLY LANDMINE (caught live 2026-06-12): alternating series is summed as
8// POS(even n) and NEG(odd n) separately, subtracted bf_cmp-ordered.
9// Positive grid suffices: antisymmetry is BIT-EXACT by construction (invariant test).
10// Durable: BFERF rows -> knowledge/status/math_engine.log. Exit 0 iff bar held.
11// license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_bigfloat120.nx"
14import "nx_bigfloat120_div.nx"
15import "nx_bigfloat120_trig.nx"
16import "_pe_f64erf.nx"
17func bg_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func bg_f(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
19func bg_n(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
20// Newton sqrt (independent re-derivation; same algorithm, different author than the spec)
21func bg_sqrt(out: *i64, a: *i64, seed_raw: i64) -> i64 {
22 let s: *i64 = bf_new()
23 bf_set_f64(s, seed_raw)
24 let q: *i64 = bf_new()
25 let sum: *i64 = bf_new()
26 var i: i64 = 0
27 while i < 8 {
28 bf_div(q, a, s)
29 bf_add(sum, s, q)
30 bf_div_small(s, sum, 2)
31 i = i + 1
32 }
33 return bf_copy(out, s)
34}
35// oracle: erf(x) bits for POSITIVE finite x (|x|<=1.75), 44-term series at 120 bits
36func bg_erf_oracle(xbits: i64, tosp: *i64) -> i64 {
37 let x: *i64 = bf_new()
38 bf_set_f64(x, xbits)
39 let x2: *i64 = bf_new()
40 bf_mul(x2, x, x)
41 // u_n = x^(2n+1)/n! incrementally: u_0 = x; u_n = u_{n-1} * x2 / n
42 let u: *i64 = bf_new()
43 bf_copy(u, x)
44 let pos: *i64 = bf_new()
45 bf_copy(pos, x) // n=0 term: x/(0!*1) = x (even -> POS)
46 let neg: *i64 = bf_new()
47 bf_set_int(neg, 0)
48 let t: *i64 = bf_new()
49 let term: *i64 = bf_new()
50 let acc: *i64 = bf_new()
51 var n: i64 = 1
52 while n <= 44 {
53 bf_mul(t, u, x2)
54 bf_div_small(u, t, n)
55 bf_div_small(term, u, 2 * n + 1)
56 if (n & 1) == 1 {
57 bf_add(acc, neg, term)
58 bf_copy(neg, acc)
59 } else {
60 bf_add(acc, pos, term)
61 bf_copy(pos, acc)
62 }
63 n = n + 1
64 }
65 let dif: *i64 = bf_new()
66 if bf_cmp(pos, neg) >= 0 { bf_sub(dif, pos, neg) } else { bf_sub(dif, neg, pos) }
67 let r: *i64 = bf_new()
68 bf_mul(r, dif, tosp)
69 return bf_to_f64(r, 0, 0)
70}
71func bg_ulp(a: i64, b: i64) -> i64 {
72 if a >= b { return a - b }
73 return b - a
74}
75func main() -> i64 {
76 bg_p("=== BF-ERF GATE: independent 120-bit oracle vs the Builder-authored kernel ===\n" as *u8)
77 let pi: *i64 = bf_new()
78 bf_pi(pi)
79 let spi: *i64 = bf_new()
80 bg_sqrt(spi, pi, 0x3FFC5BF891B4EF6A)
81 let two: *i64 = bf_new()
82 bf_set_int(two, 2)
83 let tosp: *i64 = bf_new()
84 bf_div(tosp, two, spi)
85 let kb: *i64 = bf_new()
86 let xb: *i64 = bf_new()
87 var maxulp: i64 = 0
88 var worst: i64 = 0
89 var npts: i64 = 0
90 // main grid: k/64, k=1..112 (0.015625 .. 1.75)
91 var k: i64 = 1
92 while k <= 112 {
93 bf_set_int(kb, k)
94 bf_div_small(xb, kb, 64)
95 let xbits: i64 = bf_to_f64(xb, 0, 0)
96 let want: i64 = bg_erf_oracle(xbits, tosp)
97 let got: i64 = nx_f64_erf(xbits)
98 let u: i64 = bg_ulp(got, want)
99 if u > maxulp { maxulp = u; worst = xbits }
100 npts = npts + 1
101 k = k + 1
102 }
103 // small-x grid: k/4096, k=1..8
104 k = 1
105 while k <= 8 {
106 bf_set_int(kb, k)
107 bf_div_small(xb, kb, 4096)
108 let xbits2: i64 = bf_to_f64(xb, 0, 0)
109 let want2: i64 = bg_erf_oracle(xbits2, tosp)
110 let got2: i64 = nx_f64_erf(xbits2)
111 let u2: i64 = bg_ulp(got2, want2)
112 if u2 > maxulp { maxulp = u2; worst = xbits2 }
113 npts = npts + 1
114 k = k + 1
115 }
116 let lfd: i64 = sys_openat_append("knowledge/status/math_engine.log" as *u8, 0x1a4)
117 if lfd >= 0 {
118 bg_f(lfd, "BFERF-GATE epoch=" as *u8); bg_n(lfd, sys_now_realtime_sec())
119 bg_f(lfd, " points=" as *u8); bg_n(lfd, npts)
120 bg_f(lfd, " max_ulp=" as *u8); bg_n(lfd, maxulp)
121 bg_f(lfd, " worst_x_bits=" as *u8); bg_n(lfd, worst)
122 if maxulp <= 4 { bg_f(lfd, " verdict=GREEN\n" as *u8) } else { bg_f(lfd, " verdict=RED\n" as *u8) }
123 // the scorecard's canonical anchor row (slot 12, class DLMF-ch7)
124 bg_f(lfd, "ERFGATE epoch=" as *u8); bg_n(lfd, sys_now_realtime_sec())
125 bg_f(lfd, " max_ulp=" as *u8); bg_n(lfd, maxulp)
126 if maxulp <= 4 { bg_f(lfd, " verdict=GREEN\n" as *u8) } else { bg_f(lfd, " verdict=RED\n" as *u8) }
127 sys_close(lfd)
128 }
129 bg_p(" points=" as *u8)
130 let ob: *u8 = sys_mmap(28)
131 var m: i64 = npts
132 var kk: i64 = 0
133 if m == 0 { ob[0] = 48 as u8; kk = 1 }
134 while m > 0 { ob[kk] = (48 + (m % 10)) as u8; m = m / 10; kk = kk + 1 }
135 let rb: *u8 = sys_mmap(28)
136 var z: i64 = 0
137 while z < kk { rb[z] = ob[kk-1-z]; z = z + 1 }
138 sys_write(1, rb, kk)
139 bg_p(" max_ulp=" as *u8)
140 m = maxulp
141 kk = 0
142 if m == 0 { ob[0] = 48 as u8; kk = 1 }
143 while m > 0 { ob[kk] = (48 + (m % 10)) as u8; m = m / 10; kk = kk + 1 }
144 z = 0
145 while z < kk { rb[z] = ob[kk-1-z]; z = z + 1 }
146 sys_write(1, rb, kk)
147 if maxulp <= 4 { bg_p(" BFERF-GATE: GREEN (rung-1 bar ulp<=4 held vs independent oracle)\n" as *u8); sys_exit(0); return 0 }
148 bg_p(" BFERF-GATE: RED (bar ulp<=4 broken -- kernel or oracle named for the Doctor)\n" as *u8)
149 sys_exit(1)
150 return 1
151}