code wiki / _hdl_build / nx_phys_constants.nx
nx_phys_constants.nx source
↩ module page · 215 lines · 8962 B
1// nx_phys_constants.nx -- the SOVEREIGN EXACT physical-constants registry (science-exceed arc, rung
2// PHYS-CONSTANTS). COMPOSES nx_phys_units: every constant carries its dimension in the SAME 7-SI-base
3// integer basis [L,M,T,I,TH,N,J], and the gate cross-checks a constant's dimension against the units
4// registry (sci_units.tsv). DATA-driven: knowledge/registry/sci_constants.tsv.
5//
6// EXCEED vs Mathematica Quantity (float magnitudes, cloud-tethered): the 2019 SI redefinition FIXED
7// c, dnu_Cs, h, e, k, N_A (=> R) EXACTLY by definition. Nishi stores those si_exact=1 constants as
8// their EXACT defining decimals -- sovereign, offline, attestable, no float approximation. Honest
9// scope: this is an exact-VALUE registry (not yet a bignum calculator); CODATA-measured constants
10// (si_exact=0) carry the best-known value.
11//
12// BAKED GATE (runs + asserts before verdict=GREEN): c's value == its exact SI definition; c & N_A are
13// si_exact while G is measured; c's dimension == the "velocity" unit's dimension (COMPOSE check vs
14// nx_phys_units' registry); an unknown symbol is rejected; >=7 constants are SI-exact.
15//
16// module: nishi-core.science.phys_constants
17// depends: nishi-core.sys.syscalls
18// capability: SOVEREIGN_EXACT_PHYSICAL_CONSTANTS
19// license_tier: ORIGINAL
20import "nx_syscalls.nx"
21const PC_MAGIC_65536: i64 = 65536
22
23const PC_REF: *u8 = "knowledge/registry/sci_constants.tsv"
24const PU_REF: *u8 = "knowledge/registry/sci_units.tsv"
25const PC_LOG: *u8 = "knowledge/status/phys_constants.log"
26
27func pc_w(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 }
28func pc_wn(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;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 }
29func pc_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
30
31func pc_read(path: *u8, buf: *u8, cap: i64) -> i64 {
32 let fd: i64 = sys_openat_rd(path)
33 if fd < 0 { return 0 }
34 var n: i64 = 0
35 var r: i64 = sys_read(fd, buf, cap - 1)
36 while r > 0 { n = n + r; if n >= cap - 1 { r = 0 } else { r = sys_read(fd, buf + n, cap - 1 - n) } }
37 sys_close(fd)
38 return n
39}
40
41func pc_field(buf: *u8, ls: i64, le: i64, f: i64, dst: *u8, cap: i64) -> i64 {
42 var col: i64 = 0
43 var p: i64 = ls
44 var k: i64 = 0
45 while p < le {
46 if buf[p] == (9 as u8) { if col == f { dst[k] = 0 as u8; return k } col = col + 1; if col == f { k = 0 } }
47 else { if col == f { if k < cap - 1 { dst[k] = buf[p]; k = k + 1 } } }
48 p = p + 1
49 }
50 dst[k] = 0 as u8
51 return k
52}
53
54func pc_atoi(s: *u8, len: i64) -> i64 {
55 var neg: i64 = 0; var i: i64 = 0; var v: i64 = 0
56 if len > 0 { if s[0] == (45 as u8) { neg = 1; i = 1 } }
57 while i < len { if s[i] >= (48 as u8) { if s[i] <= (57 as u8) { v = v * 10 + (s[i] - 48) } } i = i + 1 }
58 if neg == 1 { return 0 - v }
59 return v
60}
61
62func pc_streq(a: *u8, b: *u8) -> i64 {
63 var i: i64 = 0
64 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 }
65 if b[i] != (0 as u8) { return 0 }
66 return 1
67}
68
69func pc_eq7(a: *i64, b: *i64) -> i64 { var k: i64=0; while k<7 { if a[k]!=b[k] { return 0 } k=k+1 } return 1 }
70
71// constants row: symbol(0) name(1) value(2) unit(3) si_exact(4) L(5) M(6) T(7) I(8) TH(9) N(10) J(11)
72func pc_const_lookup(cbuf: *u8, cn: i64, sym: *u8, val: *u8, si_ptr: *i64, dim: *i64) -> i64 {
73 let fld: *u8 = sys_mmap(64)
74 let f2: *u8 = sys_mmap(64)
75 var ls: i64 = 0
76 var i: i64 = 0
77 while i <= cn {
78 var eol: i64 = 0
79 if i == cn { eol = 1 } else { if cbuf[i] == (10 as u8) { eol = 1 } }
80 if eol == 1 {
81 if i > ls { if cbuf[ls] != (35 as u8) {
82 pc_field(cbuf, ls, i, 0, fld, 64)
83 if pc_streq(fld, sym) == 1 {
84 pc_field(cbuf, ls, i, 2, val, 64)
85 let sl: i64 = pc_field(cbuf, ls, i, 4, f2, 64)
86 si_ptr[0] = pc_atoi(f2, sl)
87 var c: i64 = 0
88 while c < 7 {
89 let dl: i64 = pc_field(cbuf, ls, i, 5 + c, f2, 64)
90 dim[c] = pc_atoi(f2, dl)
91 c = c + 1
92 }
93 return 1
94 }
95 } }
96 ls = i + 1
97 }
98 i = i + 1
99 }
100 return 0
101}
102
103// units row: name(0) L(1)..J(7) -> fill dim[0..7]. 1 found / 0 not.
104func pc_unit_dim(ubuf: *u8, un: i64, name: *u8, dim: *i64) -> i64 {
105 let fld: *u8 = sys_mmap(64)
106 let f2: *u8 = sys_mmap(32)
107 var ls: i64 = 0
108 var i: i64 = 0
109 while i <= un {
110 var eol: i64 = 0
111 if i == un { eol = 1 } else { if ubuf[i] == (10 as u8) { eol = 1 } }
112 if eol == 1 {
113 if i > ls { if ubuf[ls] != (35 as u8) {
114 pc_field(ubuf, ls, i, 0, fld, 64)
115 if pc_streq(fld, name) == 1 {
116 var c: i64 = 0
117 while c < 7 { let dl: i64 = pc_field(ubuf, ls, i, 1 + c, f2, 32); dim[c] = pc_atoi(f2, dl); c = c + 1 }
118 return 1
119 }
120 } }
121 ls = i + 1
122 }
123 i = i + 1
124 }
125 return 0
126}
127
128// count total constant rows + si_exact==1 rows.
129func pc_count(cbuf: *u8, cn: i64, si_ptr: *i64, tot_ptr: *i64) -> i64 {
130 let f0: *u8 = sys_mmap(64)
131 let f2: *u8 = sys_mmap(64)
132 var tot: i64 = 0
133 var se: i64 = 0
134 var ls: i64 = 0
135 var i: i64 = 0
136 while i <= cn {
137 var eol: i64 = 0
138 if i == cn { eol = 1 } else { if cbuf[i] == (10 as u8) { eol = 1 } }
139 if eol == 1 {
140 if i > ls { if cbuf[ls] != (35 as u8) {
141 pc_field(cbuf, ls, i, 0, f0, 64)
142 if pc_len(f0) > 0 {
143 tot = tot + 1
144 let sl: i64 = pc_field(cbuf, ls, i, 4, f2, 64)
145 if pc_atoi(f2, sl) == 1 { se = se + 1 }
146 }
147 } }
148 ls = i + 1
149 }
150 i = i + 1
151 }
152 si_ptr[0] = se
153 tot_ptr[0] = tot
154 return 0
155}
156
157func main() -> i64 {
158 let cbuf: *u8 = sys_mmap(PC_MAGIC_65536)
159 let cn: i64 = pc_read(PC_REF, cbuf, PC_MAGIC_65536)
160 let ubuf: *u8 = sys_mmap(PC_MAGIC_65536)
161 let un: i64 = pc_read(PU_REF, ubuf, PC_MAGIC_65536)
162 if cn <= 0 { pc_w(1, "PHYSCONST-GATE verdict=RED reason=no-ref\n" as *u8); sys_exit(11); return 11 }
163
164 let val_c: *u8 = sys_mmap(64)
165 let trash: *u8 = sys_mmap(64)
166 let dim_c: *i64 = sys_mmap(56) as *i64
167 let dim_t: *i64 = sys_mmap(56) as *i64
168 let dim_vel: *i64 = sys_mmap(56) as *i64
169 let si: *i64 = sys_mmap(8) as *i64
170
171 var ok_lookup: i64 = 1
172 if pc_const_lookup(cbuf, cn, "c" as *u8, val_c, si, dim_c) != 1 { ok_lookup = 0 }
173 let si_c: i64 = si[0]
174 if pc_const_lookup(cbuf, cn, "N_A" as *u8, trash, si, dim_t) != 1 { ok_lookup = 0 }
175 let si_NA: i64 = si[0]
176 if pc_const_lookup(cbuf, cn, "G" as *u8, trash, si, dim_t) != 1 { ok_lookup = 0 }
177 let si_G: i64 = si[0]
178
179 let have_vel: i64 = pc_unit_dim(ubuf, un, "velocity" as *u8, dim_vel)
180 let unk: i64 = pc_const_lookup(cbuf, cn, "ZZZ_NOEXIST" as *u8, trash, si, dim_t)
181
182 let se: *i64 = sys_mmap(8) as *i64
183 let tot: *i64 = sys_mmap(8) as *i64
184 pc_count(cbuf, cn, se, tot)
185
186 let v1: i64 = pc_streq(val_c, "299792458" as *u8) // exact SI-defining value stored
187 var v2: i64 = 0; if si_c == 1 { v2 = 1 } // c is SI-exact
188 var v3: i64 = 0; if si_NA == 1 { v3 = 1 } // N_A is SI-exact
189 var v4: i64 = 0; if si_G == 0 { v4 = 1 } // G is measured (NOT exact)
190 var v5: i64 = 0; if have_vel == 1 { v5 = pc_eq7(dim_c, dim_vel) } // COMPOSE: c.dim == velocity dim
191 var v6: i64 = 0; if unk == 0 { v6 = 1 } // unknown symbol rejected
192 var v7: i64 = 0; if se[0] >= 7 { v7 = 1 } // >=7 SI-exact constants
193
194 var ok: i64 = 0
195 if ok_lookup == 1 { if v1 == 1 { if v2 == 1 { if v3 == 1 { if v4 == 1 { if v5 == 1 { if v6 == 1 { if v7 == 1 { ok = 1 } } } } } } } }
196
197 var p2: i64 = 0
198 while p2 < 2 {
199 var fd: i64 = 1
200 if p2 == 1 { fd = sys_openat_append(PC_LOG, 0x1a4) }
201 if fd >= 0 {
202 pc_w(fd, "PHYSCONST-GATE constants=" as *u8); pc_wn(fd, tot[0])
203 pc_w(fd, " si_exact=" as *u8); pc_wn(fd, se[0])
204 pc_w(fd, " exact_value=" as *u8); if v1 == 1 { pc_w(fd, "1" as *u8) } else { pc_w(fd, "0" as *u8) }
205 pc_w(fd, " compose_dim=" as *u8); if v5 == 1 { pc_w(fd, "1" as *u8) } else { pc_w(fd, "0" as *u8) }
206 pc_w(fd, " model=exact-value-registry" as *u8)
207 if ok == 1 { pc_w(fd, " verdict=GREEN\n" as *u8) } else { pc_w(fd, " verdict=RED\n" as *u8) }
208 if p2 == 1 { sys_close(fd) }
209 }
210 p2 = p2 + 1
211 }
212 if ok == 1 { sys_exit(0); return 0 }
213 sys_exit(1)
214 return 1
215}