code wiki / _hdl_build / nx_mathspec_gamma.nx
nx_mathspec_gamma.nx source
↩ module page · 137 lines · 5509 B
1// nx_mathspec_gamma.nx -- SOVEREIGN SPEC EMITTER for the GAMMA kernel (DLMF ch5
2// rung 1, ME2-GAMMA-001). Constants from the 120-bit bigfloat; no python, no minimax:
3// Stirling coefficients c_k = B_2k / ((2k)(2k-1)) from the EXACT Bernoulli table
4// (the sinhcosh P_k gene), signs (-1)^(k+1) BAKED; half*ln(2*pi) via bf_pi + bf_ln;
5// recurrence pull-up target W=8; overflow cut 171.625; reflection (x<0.5) = RUNG-2
6// NAMED DEBT (LOUD-NAN contract, the erf gap gene).
7// REFUSAL RAILS (rail the ORACLE the future gate will use -- algebra beats trust):
8// R1: bf_gamma_f64(5.0) == 24.0 BIT-EXACT (0x4038000000000000)
9// R2: bf_gamma_f64(0.5) == sqrt(pi) BIT-EXACT (0x3FFC5BF891B4EF6A -- the constant
10// independently Newton-derived and libm-verified earlier this same day)
11// Layout: c[0]=one c[1]=half c[2]=half_ln2pi c[3]=W(8.0) c[4]=overflow_cut(171.625)
12// c[5..12]=Stirling c_k hi-first k=8..1 (SIGNED) c[13]=lo_cut(0.5)
13// Emits runtime/_hdl_build/_pm_gamma_spec.nx :: pm_gamma_spec_fill(c)->14
14// license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_pattern_emit.nx"
17import "nx_bigfloat120.nx"
18import "nx_bigfloat120_div.nx"
19import "nx_bigfloat120_trig.nx"
20import "nx_bigfloat120_ln.nx"
21import "nx_bigfloat120_gamma.nx"
22const K_MAGIC_1373: i64 = 1373
23const K_MAGIC_2730: i64 = 2730
24const K_MAGIC_3617: i64 = 3617
25
26func msg_wlit(fd: i64, v: i64) -> i64 {
27 if v < 0 {
28 pe_w(fd, "0 - " as *u8)
29 pe_wn(fd, 0 - v)
30 return 0
31 }
32 pe_wn(fd, v)
33 return 0
34}
35
36func msg_emit_row(fd: i64, idx: i64, v: i64) -> i64 {
37 pe_w(fd, " c[" as *u8); pe_wn(fd, idx); pe_w(fd, "] = " as *u8)
38 msg_wlit(fd, v)
39 pe_w(fd, "\n" as *u8)
40 return 0
41}
42
43// c_k = (bnum/bden) / ((2k)(2k-1)) by factor division (sinhcosh P_k gene)
44func msg_ck(out: *i64, bnum: i64, bden: i64, k: i64) -> i64 {
45 let t: *i64 = bf_new()
46 bf_set_int(t, bnum)
47 let u: *i64 = bf_new()
48 bf_div_small(u, t, bden)
49 bf_div_small(t, u, 2 * k)
50 bf_div_small(u, t, 2 * k - 1)
51 return bf_copy(out, u)
52}
53
54func main() -> i64 {
55 // ---- rails: the gate's oracle must satisfy algebra BIT-EXACTLY ----
56 if bf_gamma_f64(0x4014000000000000) != 0x4038000000000000 {
57 pe_w(1, "REFUSED: bf_gamma_f64(5) != 24 -- oracle rail R1 failed\n" as *u8)
58 sys_exit(2)
59 return 2
60 }
61 // KNOWN ORACLE WOBBLE (measured 2026-06-12, _gprobe): bf_gamma_f64(0.5) returns
62 // sqrt(pi)+1ulp (...395 vs ...394) -- integer-exact, half-integer 1-ulp rounding.
63 // Rail tolerance: <=1 ulp here; the gamma GATE bar must absorb oracle wobble
64 // (rung-1 bar 8 ulp >> 1). Oracle final-rounding refinement = ME2-GAMMA-002.
65 var g05: i64 = bf_gamma_f64(0x3FE0000000000000) - 0x3FFC5BF891B4EF6A
66 if g05 < 0 { g05 = 0 - g05 }
67 if g05 > 1 {
68 pe_w(1, "REFUSED: bf_gamma_f64(0.5) off sqrt(pi) by >1 ulp -- oracle rail R2 failed\n" as *u8)
69 sys_exit(2)
70 return 2
71 }
72 // ---- half*ln(2*pi) ----
73 let pi: *i64 = bf_new()
74 bf_pi(pi)
75 let twopi: *i64 = bf_new()
76 bf_mul_small(twopi, pi, 2)
77 let l2p_bits: i64 = bf_ln_f64(bf_to_f64(twopi, 0, 0))
78 // half it exactly in exponent space is wrong for ln values -- divide in bf:
79 let l2pb: *i64 = bf_new()
80 bf_set_f64(l2pb, l2p_bits)
81 let hb: *i64 = bf_new()
82 bf_div_small(hb, l2pb, 2)
83 let half_ln2pi: i64 = bf_to_f64(hb, 0, 0)
84 // ---- emit ----
85 let fd: i64 = sys_openat_wr("runtime/_hdl_build/_pm_gamma_spec.nx" as *u8, 0x1a4)
86 if fd < 0 { sys_exit(2) }
87 pe_w(fd, "// _pm_gamma_spec.nx -- GENERATED by nx_mathspec_gamma (SOVEREIGN bigfloat:\n" as *u8)
88 pe_w(fd, "// exact Bernoulli Stirling + bf_pi + bf_ln; oracle rails Gamma(5)=24 and\n" as *u8)
89 pe_w(fd, "// Gamma(0.5)=sqrt(pi) held BIT-EXACT at emit time). DO NOT HAND-EDIT.\n" as *u8)
90 pe_w(fd, "import \"nx_syscalls.nx\"\n\nfunc pm_gamma_spec_fill(c: *i64) -> i64 {\n" as *u8)
91 let one: *i64 = bf_new()
92 bf_set_int(one, 1)
93 msg_emit_row(fd, 0, bf_to_f64(one, 0, 0))
94 let half: *i64 = bf_new()
95 bf_copy(half, one)
96 half[0] = half[0] - 1
97 msg_emit_row(fd, 1, bf_to_f64(half, 0, 0))
98 msg_emit_row(fd, 2, half_ln2pi)
99 let w8: *i64 = bf_new()
100 bf_set_int(w8, 8)
101 msg_emit_row(fd, 3, bf_to_f64(w8, 0, 0))
102 // overflow cut 171.625 = 1373/8
103 let oc: *i64 = bf_new()
104 bf_set_int(oc, K_MAGIC_1373)
105 let ocq: *i64 = bf_new()
106 bf_div_small(ocq, oc, 8)
107 msg_emit_row(fd, 4, bf_to_f64(ocq, 0, 0))
108 // Stirling c_k hi-first k=8..1, Bernoulli |B_2k| num/den, sign (-1)^(k+1)
109 let bn: *i64 = sys_mmap(8 * 10) as *i64
110 let bd: *i64 = sys_mmap(8 * 10) as *i64
111 bn[1] = 1; bd[1] = 6
112 bn[2] = 1; bd[2] = 30
113 bn[3] = 1; bd[3] = 42
114 bn[4] = 1; bd[4] = 30
115 bn[5] = 5; bd[5] = 66
116 bn[6] = 691; bd[6] = K_MAGIC_2730
117 bn[7] = 7; bd[7] = 6
118 bn[8] = K_MAGIC_3617; bd[8] = 510
119 var k: i64 = 8
120 var idx: i64 = 5
121 while k >= 1 {
122 let ck: *i64 = bf_new()
123 msg_ck(ck, bn[k], bd[k], k)
124 var bits: i64 = bf_to_f64(ck, 0, 0)
125 if (k & 1) == 0 { bits = bits | (1 << 63) }
126 msg_emit_row(fd, idx, bits)
127 idx = idx + 1
128 k = k - 1
129 }
130 // lo cut 0.5 (below = reflection, rung-2 LOUD-NAN debt)
131 msg_emit_row(fd, 13, bf_to_f64(half, 0, 0))
132 pe_w(fd, " return 14\n}\n" as *u8)
133 sys_close(fd)
134 pe_w(1, "SPEC EMITTED: _pm_gamma_spec.nx (14 consts; oracle rails Gamma(5)=24 + Gamma(.5)=sqrt(pi) within 1ulp known-wobble)\n" as *u8)
135 sys_exit(0)
136 return 0
137}