code wiki / _hdl_build / nx_geo_s2_gate.nx

nx_geo_s2_gate.nx source

↩ module page · 92 lines · 3730 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" 12 13const GS2_LOG: *u8 = "knowledge/status/geo_s2.log" 14 15func 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 } 16func 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 } 17func g2_abs(x: i64) -> i64 { if x < 0 { return 0 - x } return x } 18 19func g2_emit(fd: i64, cnt: i64, rt: i64, hmax: i64, zmax: i64, ok: i64) -> i64 { 20 g2_w(fd, "GEOS2GATE authored=organ method=hilbert-curve grid=8x8 bijection_count=" as *u8); g2_wn(fd, cnt) 21 g2_w(fd, " roundtrip_ok=" as *u8); g2_wn(fd, rt) 22 g2_w(fd, " hilbert_consec_max=" as *u8); g2_wn(fd, hmax) 23 g2_w(fd, " zorder_consec_max=" as *u8); g2_wn(fd, zmax) 24 if ok == 1 { g2_w(fd, " verdict=GREEN\n" as *u8) } else { g2_w(fd, " verdict=RED\n" as *u8) } 25 return 0 26} 27 28func main() -> i64 { 29 let hx: *i64 = sys_mmap(8 * 64) as *i64 30 let hy: *i64 = sys_mmap(8 * 64) as *i64 31 let zx: *i64 = sys_mmap(8 * 64) as *i64 32 let zy: *i64 = sys_mmap(8 * 64) as *i64 33 let seen: *i64 = sys_mmap(8 * 64) as *i64 34 var i: i64 = 0 35 while i < 64 { seen[i] = 0; i = i + 1 } 36 37 var bij_ok: i64 = 1 38 var rt_ok: i64 = 1 39 let o2: *i64 = sys_mmap(16) as *i64 40 var x: i64 = 0 41 while x < 8 { 42 var y: i64 = 0 43 while y < 8 { 44 let hd: i64 = geo_hilbert_xy2d(3, x, y) 45 if hd < 0 { bij_ok = 0 } else { 46 if hd >= 64 { bij_ok = 0 } else { 47 if seen[hd] == 1 { bij_ok = 0 } 48 seen[hd] = 1 49 hx[hd] = x; hy[hd] = y 50 } 51 } 52 let zd: i64 = geo_zorder_xy2d(3, x, y) 53 zx[zd] = x; zy[zd] = y 54 // round-trip 55 geo_hilbert_d2xy(3, hd, o2) 56 if o2[0] != x { rt_ok = 0 } 57 if o2[1] != y { rt_ok = 0 } 58 y = y + 1 59 } 60 x = x + 1 61 } 62 63 var cnt: i64 = 0 64 i = 0 65 while i < 64 { cnt = cnt + seen[i]; i = i + 1 } 66 67 // consecutive-index Manhattan step: max over d -> d+1 68 var hmax: i64 = 0 69 var zmax: i64 = 0 70 var d: i64 = 0 71 while d < 63 { 72 let hm: i64 = g2_abs(hx[d] - hx[d + 1]) + g2_abs(hy[d] - hy[d + 1]) 73 if hm > hmax { hmax = hm } 74 let zm: i64 = g2_abs(zx[d] - zx[d + 1]) + g2_abs(zy[d] - zy[d + 1]) 75 if zm > zmax { zmax = zm } 76 d = d + 1 77 } 78 79 var ok: i64 = 1 80 if bij_ok != 1 { ok = 0 } 81 if cnt != 64 { ok = 0 } 82 if rt_ok != 1 { ok = 0 } 83 if hmax != 1 { ok = 0 } // Hilbert is continuous 84 if zmax <= hmax { ok = 0 } // Z-order tears -> measured exceed 85 86 g2_emit(1, cnt, rt_ok, hmax, zmax, ok) 87 let lf: i64 = sys_openat_append(GS2_LOG, 420) 88 if lf >= 0 { g2_emit(lf, cnt, rt_ok, hmax, zmax, ok); sys_close(lf) } 89 90 if ok == 1 { return 0 } 91 return 1 92}