code wiki / _hdl_build / nx_ciaaw_gate.nx

nx_ciaaw_gate.nx source

↩ module page · 107 lines · 6144 B

1// nx_ciaaw_gate.nx -- VERIFY + LIAR-KILL gate for the CIAAW (international, upstream-of-NIST) external- 2// reference loop. Standard atomic weights are UNCERTAIN quantities (intervals/uncertainties), NOT exact 3// values like isotope masses -- so the correct test is agreement WITHIN CIAAW's stated +/- uncertainty, not 4// exact equality. Compares OUR Element.mass_q3 against the IUPAC/CIAAW abridged value+uncertainty in the 5// SOVEREIGN seg_store (key "atw:<Z>", fields mass_q3 + unc_q3, populated by nx_ciaaw_ingest via nx_html_table): 6// AGREE iff |ours - ciaaw| <= ciaaw_uncertainty. disagree = our value is OUTSIDE the international uncertainty 7// (a real drift). missing = no CIAAW standard weight (radioactive/synthetic). GREEN iff negctrl OK AND 8// disagree==0. license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_seg_store.nx" 11import "nx_uxf_decode.nx" 12import "nx_chem_periodic.nx" 13 14const CG_STORE: *u8 = "knowledge/store/ciaaw-atw-" 15const CG_LOG: *u8 = "knowledge/status/ciaaw_gate.log" 16 17func cg_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 } 18func cg_wn(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(16); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} let t: *u8=sys_mmap(16); 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{b[i]=t[k-1-i];i=i+1}; sys_write(fd,b,k); return 0 } 19func cg_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 } 20func cg_itoa(v: i64, dst: *u8) -> i64 { var m: i64=v; let t: *u8=sys_mmap(24); 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{dst[i]=t[k-1-i];i=i+1} dst[k]=0 as u8; return k } 21func cg_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8){ if s[i]>=(48 as u8){ if s[i]<=(57 as u8){ v=v*10+(s[i]-48) } } i=i+1 } return v } 22 23// fetch CIAAW value+uncertainty for Z. fills val_out (-1 if absent), unc_out. returns 1 found / 0 absent. 24func cg_ref(z: i64, val_out: *i64, unc_out: *i64) -> i64 { 25 let kb: *u8 = sys_mmap(64); var kn: i64 = 0 26 let kp: *u8 = "atw:" as *u8; var ki: i64 = 0 27 while kp[ki]!=(0 as u8){ kb[kn]=kp[ki]; kn=kn+1; ki=ki+1 } 28 kn = kn + cg_itoa(z, ((kb as i64)+kn) as *u8) 29 let gp: *i64 = sys_mmap(8) as *i64; let gl: *i64 = sys_mmap(8) as *i64 30 val_out[0] = 0 - 1; unc_out[0] = 0 31 if ss_get(CG_STORE, kb, gp, gl) != 1 { return 0 } 32 let dk: *i64 = sys_mmap(64) as *i64; let dv: *i64 = sys_mmap(64) as *i64 33 let nf: i64 = canon_decode(gp[0] as *u8, gl[0], dk, dv, 8) 34 if nf < 0 { return 0 } 35 var f: i64 = 0 36 while f < nf { 37 if cg_streq(dk[f] as *u8, "mass_q3" as *u8)==1 { val_out[0] = cg_atoi(dv[f] as *u8) } 38 if cg_streq(dk[f] as *u8, "unc_q3" as *u8)==1 { unc_out[0] = cg_atoi(dv[f] as *u8) } 39 f = f + 1 40 } 41 if val_out[0] < 0 { return 0 } 42 return 1 43} 44 45func main() -> i64 { 46 let arr: *Element = nx_chem_periodic_table_118() 47 let tv: *i64 = sys_mmap(8) as *i64; let tu: *i64 = sys_mmap(8) as *i64 48 49 var nc: i64 = 1 50 if cg_ref(1, tv, tu) != 1 { nc = 0 } else { if tv[0] != 1008 { nc = 0 } } 51 if cg_ref(6, tv, tu) != 1 { nc = 0 } else { if tv[0] != 12011 { nc = 0 } } 52 if cg_ref(999, tv, tu) != 0 { nc = 0 } 53 54 var agree: i64 = 0; var disagree: i64 = 0; var missing: i64 = 0 55 let rep: *u8 = sys_mmap(65536); var rw: i64 = 0 56 let v: *i64 = sys_mmap(8) as *i64; let u: *i64 = sys_mmap(8) as *i64 57 58 var idx: i64 = 0 59 while idx < 118 { 60 let e: *Element = ((arr as i64) + idx * NX_ELEM_BYTES) as *Element 61 let z: i64 = e.z 62 let ourq3: i64 = e.mass_q3 63 if cg_ref(z, v, u) == 0 { missing = missing + 1 } 64 else { 65 var diff: i64 = ourq3 - v[0] 66 if diff < 0 { diff = 0 - diff } 67 if diff <= u[0] { agree = agree + 1 } 68 else { 69 disagree = disagree + 1 70 let m1: *u8=" DISAGREE Z="; var b: i64=0; while m1[b]!=(0 as u8){rep[rw]=m1[b];rw=rw+1;b=b+1} 71 let zb: *u8=sys_mmap(24); cg_itoa(z, zb); b=0; while zb[b]!=(0 as u8){rep[rw]=zb[b];rw=rw+1;b=b+1} 72 let m3: *u8=" ours="; b=0; while m3[b]!=(0 as u8){rep[rw]=m3[b];rw=rw+1;b=b+1} 73 let ob: *u8=sys_mmap(24); cg_itoa(ourq3, ob); b=0; while ob[b]!=(0 as u8){rep[rw]=ob[b];rw=rw+1;b=b+1} 74 let m4: *u8=" ciaaw="; b=0; while m4[b]!=(0 as u8){rep[rw]=m4[b];rw=rw+1;b=b+1} 75 let nbb: *u8=sys_mmap(24); cg_itoa(v[0], nbb); b=0; while nbb[b]!=(0 as u8){rep[rw]=nbb[b];rw=rw+1;b=b+1} 76 let m5: *u8=" +/-"; b=0; while m5[b]!=(0 as u8){rep[rw]=m5[b];rw=rw+1;b=b+1} 77 let ub: *u8=sys_mmap(24); cg_itoa(u[0], ub); b=0; while ub[b]!=(0 as u8){rep[rw]=ub[b];rw=rw+1;b=b+1} 78 let m6: *u8=" diff="; b=0; while m6[b]!=(0 as u8){rep[rw]=m6[b];rw=rw+1;b=b+1} 79 let db: *u8=sys_mmap(24); cg_itoa(diff, db); b=0; while db[b]!=(0 as u8){rep[rw]=db[b];rw=rw+1;b=b+1} 80 rep[rw]=10 as u8; rw=rw+1 81 } 82 } 83 idx = idx + 1 84 } 85 86 var ok: i64 = 0 87 if nc==1 { if disagree==0 { ok = 1 } } 88 89 var p2: i64 = 0 90 while p2 < 2 { 91 var fd: i64 = 1 92 if p2==1 { fd = sys_openat_append(CG_LOG, 0x1a4) } 93 if fd >= 0 { 94 cg_w(fd, "CIAAW-GATE elements=118 agree(within-uncertainty)=" as *u8); cg_wn(fd, agree) 95 cg_w(fd, " disagree(outside-uncertainty)=" as *u8); cg_wn(fd, disagree) 96 cg_w(fd, " missing(no-std-weight)=" as *u8); cg_wn(fd, missing) 97 cg_w(fd, " negctrl=" as *u8); if nc==1 { cg_w(fd, "OK" as *u8) } else { cg_w(fd, "FAIL" as *u8) } 98 cg_w(fd, " ref=IUPAC-CIAAW-abridged(sovereign-seg_store) model=within-stated-uncertainty" as *u8) 99 if ok==1 { cg_w(fd, " verdict=GREEN\n" as *u8) } else { cg_w(fd, " verdict=RED\n" as *u8) } 100 sys_write(fd, rep, rw) 101 if p2==1 { sys_close(fd) } 102 } 103 p2 = p2 + 1 104 } 105 if ok==1 { sys_exit(0); return 0 } 106 sys_exit(1); return 1 107}