code wiki / _hdl_build / nx_geo_s2_gate.nx
nx_geo_s2_gate.nx source
↩ module page · 100 lines · 4262 B
1// nx_geo_s2_gate.nx -- GATE for GEO-005 Hilbert (S2-class) cell index, on an 8x8 grid (k=3, 64 cells).
2// Proves correctness AND the measured locality exceed over geohash's Z-order:
3// bijection : Hilbert xy2d hits all 64 distinct indices [0,63] exactly once
4// roundtrip : d2xy(xy2d(x,y)) == (x,y) for all cells
5// continuity : max Manhattan step between CONSECUTIVE Hilbert indices == 1 (the defining property)
6// exceed : Z-order's max consecutive step == 8 (= grid width N) -- it TEARS; Hilbert (1) wins
7//
8// Evidence -> knowledge/status/geo_s2.log (GEOS2GATE authored=organ ... verdict=GREEN).
9// license_tier: ORIGINAL
10import "nx_geo_s2.nx"
11import "nx_syscalls.nx"
12import "nx_gate_verdict.nx"
13
14const GS2_LOG: *u8 = "knowledge/status/geo_s2.log"
15
16func g2_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 }
17func g2_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 }
18func g2_abs(x: i64) -> i64 { if x < 0 { return 0 - x } return x }
19
20func g2_emit(fd: i64, cnt: i64, rt: i64, hmax: i64, zmax: i64, ok: i64) -> i64 {
21 g2_w(fd, "GEOS2GATE authored=organ method=hilbert-curve grid=8x8 bijection_count=" as *u8); g2_wn(fd, cnt)
22 g2_w(fd, " roundtrip_ok=" as *u8); g2_wn(fd, rt)
23 g2_w(fd, " hilbert_consec_max=" as *u8); g2_wn(fd, hmax)
24 g2_w(fd, " zorder_consec_max=" as *u8); g2_wn(fd, zmax)
25 if ok == 1 { g2_w(fd, " verdict=GREEN\n" as *u8) } else { g2_w(fd, " verdict=RED\n" as *u8) }
26 return 0
27}
28
29func main() -> i64 {
30 let hx: *i64 = sys_mmap(8 * 64) as *i64
31 let hy: *i64 = sys_mmap(8 * 64) as *i64
32 let zx: *i64 = sys_mmap(8 * 64) as *i64
33 let zy: *i64 = sys_mmap(8 * 64) as *i64
34 let seen: *i64 = sys_mmap(8 * 64) as *i64
35 var i: i64 = 0
36 while i < 64 { seen[i] = 0; i = i + 1 }
37
38 var bij_ok: i64 = 1
39 var rt_ok: i64 = 1
40 let o2: *i64 = sys_mmap(16) as *i64
41 var x: i64 = 0
42 while x < 8 {
43 var y: i64 = 0
44 while y < 8 {
45 let hd: i64 = geo_hilbert_xy2d(3, x, y)
46 if hd < 0 { bij_ok = 0 } else {
47 if hd >= 64 { bij_ok = 0 } else {
48 if seen[hd] == 1 { bij_ok = 0 }
49 seen[hd] = 1
50 hx[hd] = x; hy[hd] = y
51 }
52 }
53 let zd: i64 = geo_zorder_xy2d(3, x, y)
54 zx[zd] = x; zy[zd] = y
55 // round-trip
56 geo_hilbert_d2xy(3, hd, o2)
57 if o2[0] != x { rt_ok = 0 }
58 if o2[1] != y { rt_ok = 0 }
59 y = y + 1
60 }
61 x = x + 1
62 }
63
64 var cnt: i64 = 0
65 i = 0
66 while i < 64 { cnt = cnt + seen[i]; i = i + 1 }
67
68 // consecutive-index Manhattan step: max over d -> d+1
69 var hmax: i64 = 0
70 var zmax: i64 = 0
71 var d: i64 = 0
72 while d < 63 {
73 let hm: i64 = g2_abs(hx[d] - hx[d + 1]) + g2_abs(hy[d] - hy[d + 1])
74 if hm > hmax { hmax = hm }
75 let zm: i64 = g2_abs(zx[d] - zx[d + 1]) + g2_abs(zy[d] - zy[d + 1])
76 if zm > zmax { zmax = zm }
77 d = d + 1
78 }
79
80 var ok: i64 = 1
81 if bij_ok != 1 { ok = 0 }
82 if cnt != 64 { ok = 0 }
83 if rt_ok != 1 { ok = 0 }
84 if hmax != 1 { ok = 0 } // Hilbert is continuous
85 if zmax <= hmax { ok = 0 } // Z-order tears -> measured exceed
86
87 g2_emit(1, cnt, rt_ok, hmax, zmax, ok)
88 let lf: i64 = sys_openat_append(GS2_LOG, 420)
89 if lf >= 0 { g2_emit(lf, cnt, rt_ok, hmax, zmax, ok); sys_close(lf) }
90
91 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
92 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
93 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
94 let ctr__dry: *i64 = gv_ctr()
95 ctr__dry[0] = ok
96 ctr__dry[1] = 1
97 let rc__dry: i64 = gv_verdict("GEO-S2-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
98 sys_exit(rc__dry)
99 return rc__dry
100}