code wiki / _hdl_build / _f64_gamma_refl_gate.nx
_f64_gamma_refl_gate.nx source
↩ module page · 55 lines · 2823 B
1// _f64_gamma_refl_gate.nx -- ULP gate for the gamma reflection kernel (0<x<0.5) vs the
2// full-domain sovereign oracle bf_gamma_f64 (which does Stirling+shift+REFLECTION at
3// 120 bits, reflection-product proven 40/40). x = k/64, k=1..31 (0.0156..0.484).
4// The reflection composes Gamma(1-x) (<=7 ulp rung) so the honest bar is <=16 ulp
5// (matches the gamma rung-1 relaxed ship); the MEASURED max is logged either way.
6// Durable: GAMMAREFL-GATE -> knowledge/status/math_engine.log.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_bigfloat120.nx"
10import "nx_bigfloat120_div.nx"
11import "nx_bigfloat120_gamma.nx"
12import "_pe_f64gamma_refl.nx"
13
14func gr_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
15func gr_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 }
16func gr_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 }
17
18func gr_ulp(a: i64, b: i64) -> i64 { if a >= b { return a - b } return b - a }
19
20func main() -> i64 {
21 gr_p("=== F64-GAMMA-REFL GATE: reflection kernel (0<x<0.5) vs full-domain bf_gamma oracle ===\n" as *u8)
22 let kb: *i64 = bf_new()
23 let xb: *i64 = bf_new()
24 var maxulp: i64 = 0
25 var worst: i64 = 0
26 var npts: i64 = 0
27 var k: i64 = 1
28 while k <= 31 {
29 bf_set_int(kb, k)
30 bf_div_small(xb, kb, 64)
31 let xbits: i64 = bf_to_f64(xb, 0, 0)
32 let want: i64 = bf_gamma_f64(xbits)
33 let got: i64 = nx_f64_gamma_refl(xbits)
34 let u: i64 = gr_ulp(got, want)
35 if u > maxulp { maxulp = u; worst = xbits }
36 npts = npts + 1
37 k = k + 1
38 }
39 let lfd: i64 = sys_openat_append("knowledge/status/math_engine.log" as *u8, 0x1a4)
40 if lfd >= 0 {
41 gr_f(lfd, "GAMMAREFL-GATE epoch=" as *u8); gr_n(lfd, sys_now_realtime_sec())
42 gr_f(lfd, " points=" as *u8); gr_n(lfd, npts)
43 gr_f(lfd, " max_ulp=" as *u8); gr_n(lfd, maxulp)
44 gr_f(lfd, " worst_x_bits=" as *u8); gr_n(lfd, worst)
45 gr_f(lfd, " domain=0..0.5 method=euler-reflection(sin*gamma) oracle=bf_gamma_f64(full-domain)" as *u8)
46 if maxulp <= 16 { gr_f(lfd, " verdict=GREEN\n" as *u8) } else { gr_f(lfd, " verdict=RED\n" as *u8) }
47 sys_close(lfd)
48 }
49 gr_p(" points=" as *u8); gr_n(1, npts)
50 gr_p(" max_ulp=" as *u8); gr_n(1, maxulp)
51 if maxulp <= 16 { gr_p(" GAMMAREFL-GATE: GREEN (closes the x<0.5 LOUD-NAN debt; bar ulp<=16 held)\n" as *u8); sys_exit(0); return 0 }
52 gr_p(" GAMMAREFL-GATE: RED (measured gap named)\n" as *u8)
53 sys_exit(1)
54 return 1
55}