omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / checker2 / test_c2_blast_native.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* test_c2_blast_native: checker2's bit-blaster against the producer's compiled code (omega#446). |
| 18 | * |
| 19 | * Independent cross-check of the blaster's semantics. The other side is NOT checker2 code and NOT |
| 20 | * a hand-written label: it is the AArch64 machine code oscc emits (`oscc file.osc OUT` writes |
| 21 | * OUT.ir, OUT.code, OUT.entries), mapped executable and called as a black box per the documented |
| 22 | * native ABI (docs/osc/OSC-1-DESIGN.md section 7: params in x0..x5, return in x0, runtime pointer |
| 23 | * in x7). Every runtime entry the code reaches is a trap here (the blastable subset has no calls, |
| 24 | * memory or handles), and the trap code is read from the second argument register. Nothing of |
| 25 | * src/compiler is linked or read; only oscc's output bytes are used. |
| 26 | * |
| 27 | * The CNF side is the formula checker2 builds from oscc's own IR bytes, evaluated with unit |
| 28 | * propagation after fixing the argument bits as units (full Tseitin encoding: fixing the inputs |
| 29 | * fixes every gate, so the answer is exact; an undetermined gate fails the test). |
| 30 | * |
| 31 | * Two kinds of program, by file name: |
| 32 | * op_* fn f(<operands>, c: T) -> T ensures result != c { return <one operation>; } |
| 33 | * The ensures clause exposes the operation's value to the CNF. For each operand vector: |
| 34 | * run the code with c = 0. If it returns r (or traps ENSURES with c = 0, then r = 0, and a |
| 35 | * rerun with c = 1 must return 0), the CNF must be satisfiable at c = r (the ensures trap) |
| 36 | * and unsatisfiable at c = r ^ 1. If the code traps with any other code, the operation |
| 37 | * itself traps and the CNF must be satisfiable at c = 0 and at c = 1. |
| 38 | * other (the LRAT gate programs): the code traps with a code other than REQUIRES (9) exactly on |
| 39 | * the inputs at which the CNF is satisfiable. |
| 40 | * Inputs: exhaustive up to 16 operand bits; otherwise edge values of each operand type (0, 1, 2, |
| 41 | * -1, -2, MIN, MIN+1, MAX, MAX-1, the sign bit, every shift amount 0..w+1) in all combinations, |
| 42 | * plus 3000 pseudo-random vectors. |
| 43 | * |
| 44 | * Negative controls (file "controls" in the work directory, lines "<ir-name> <code-name>"): the |
| 45 | * CNF of one program checked against the code of a different operation of the same signature must |
| 46 | * disagree on at least one input, so the comparison can tell the operations apart. |
| 47 | * |
| 48 | * AArch64 Linux only (the documented native ABI); the harness refuses to run elsewhere. |
| 49 | * Usage: test_c2_blast_native <work-dir> (written by tests/checker2/blast_native.sh) */ |
| 50 | #include <dirent.h> |
| 51 | #include <setjmp.h> |
| 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
| 55 | #include <sys/mman.h> |
| 56 | |
| 57 | #include "c2_blast.h" |
| 58 | |
| 59 | #if !defined(__aarch64__) || !defined(__linux__) |
| 60 | int main(void) |
| 61 | { |
| 62 | printf("test_c2_blast_native: FAIL not an AArch64 Linux host; the native cross-check cannot run\n"); |
| 63 | return 1; |
| 64 | } |
| 65 | #else |
| 66 | |
| 67 | #define TRAP_REQUIRES 9 |
| 68 | #define TRAP_ENSURES 10 |
| 69 | |
| 70 | static int fails; |
| 71 | |
| 72 | static uint8_t *slurp(const char *path, size_t *n) |
| 73 | { |
| 74 | FILE *f = fopen(path, "rb"); |
| 75 | if (!f) return NULL; |
| 76 | fseek(f, 0, SEEK_END); |
| 77 | long len = ftell(f); |
| 78 | fseek(f, 0, SEEK_SET); |
| 79 | uint8_t *b = len >= 0 ? malloc((size_t)len + 1) : NULL; |
| 80 | if (b && fread(b, 1, (size_t)len, f) != (size_t)len) { free(b); b = NULL; } |
| 81 | fclose(f); |
| 82 | if (b) b[len] = 0; |
| 83 | *n = (size_t)len; |
| 84 | return b; |
| 85 | } |
| 86 | |
| 87 | /* ------------------------------------------------------------------ CNF side */ |
| 88 | |
| 89 | typedef struct { |
| 90 | const C2Cnf *c; |
| 91 | int **occ, *nocc; /* clauses containing literal l at occ[l + nvars] */ |
| 92 | signed char *val; |
| 93 | int *trail, ntrail; |
| 94 | } Ev; |
| 95 | |
| 96 | static void ev_init(Ev *e, const C2Cnf *c) |
| 97 | { |
| 98 | int nv = c->nvars; |
| 99 | e->c = c; |
| 100 | e->occ = calloc((size_t)nv * 2 + 1, sizeof *e->occ); |
| 101 | e->nocc = calloc((size_t)nv * 2 + 1, sizeof *e->nocc); |
| 102 | int *cap = calloc((size_t)nv * 2 + 1, sizeof *cap); |
| 103 | e->val = calloc((size_t)nv + 1, 1); |
| 104 | e->trail = malloc(((size_t)nv + 1) * sizeof *e->trail); |
| 105 | e->ntrail = 0; |
| 106 | for (int i = 0; i < c->nclauses; i++) |
| 107 | for (const int32_t *l = c->lits + c->start[i]; *l; l++) { |
| 108 | int s = *l + nv; |
| 109 | if (e->nocc[s] == cap[s]) { cap[s] = cap[s] ? cap[s] * 2 : 4; e->occ[s] = realloc(e->occ[s], (size_t)cap[s] * sizeof(int)); } |
| 110 | e->occ[s][e->nocc[s]++] = i; |
| 111 | } |
| 112 | free(cap); |
| 113 | } |
| 114 | |
| 115 | static void ev_free(Ev *e) |
| 116 | { |
| 117 | for (int i = 0; i < e->c->nvars * 2 + 1; i++) free(e->occ[i]); |
| 118 | free(e->occ); |
| 119 | free(e->nocc); |
| 120 | free(e->val); |
| 121 | free(e->trail); |
| 122 | } |
| 123 | |
| 124 | static int lval(const Ev *e, int l) { int v = e->val[abs(l)]; return l > 0 ? v : -v; } |
| 125 | static void set(Ev *e, int l) { e->val[abs(l)] = l > 0 ? 1 : -1; e->trail[e->ntrail++] = l; } |
| 126 | |
| 127 | /* bits[k] is argument bit k (CNF variable 2 + k). 1 satisfiable (a trap is reachable), 0 conflict, |
| 128 | * -1 propagation left gates undetermined. */ |
| 129 | static int ev_sat(Ev *e, const uint8_t *bits, int inputs) |
| 130 | { |
| 131 | const C2Cnf *c = e->c; |
| 132 | while (e->ntrail) e->val[abs(e->trail[--e->ntrail])] = 0; |
| 133 | for (int i = 0; i < c->nclauses; i++) |
| 134 | if (c->lits[c->start[i]] == 0) return 0; |
| 135 | for (int i = 0; i < c->nclauses; i++) { |
| 136 | const int32_t *l = c->lits + c->start[i]; |
| 137 | if (l[1] == 0) { |
| 138 | if (lval(e, l[0]) < 0) return 0; |
| 139 | if (!lval(e, l[0])) set(e, l[0]); |
| 140 | } |
| 141 | } |
| 142 | for (int k = 0; k < inputs; k++) { |
| 143 | int l = bits[k] ? 2 + k : -(2 + k); |
| 144 | if (lval(e, l) < 0) return 0; |
| 145 | if (!lval(e, l)) set(e, l); |
| 146 | } |
| 147 | for (int q = 0; q < e->ntrail; q++) { |
| 148 | int f = -e->trail[q]; |
| 149 | for (int k = 0; k < e->nocc[f + c->nvars]; k++) { |
| 150 | const int32_t *l = c->lits + c->start[e->occ[f + c->nvars][k]]; |
| 151 | int open = 0, last = 0, sat = 0; |
| 152 | for (; *l; l++) { |
| 153 | int s = lval(e, *l); |
| 154 | if (s > 0) { sat = 1; break; } |
| 155 | if (!s) { open++; last = *l; } |
| 156 | } |
| 157 | if (sat) continue; |
| 158 | if (!open) return 0; |
| 159 | if (open == 1) set(e, last); |
| 160 | } |
| 161 | } |
| 162 | return e->ntrail == c->nvars ? 1 : -1; |
| 163 | } |
| 164 | |
| 165 | /* ------------------------------------------------------------------ native side */ |
| 166 | |
| 167 | static sigjmp_buf jb; |
| 168 | static volatile uint64_t trap_code; |
| 169 | static void *rt_table[512]; |
| 170 | |
| 171 | static void rt_entry(uint64_t x0, uint64_t x1) |
| 172 | { |
| 173 | (void)x0; |
| 174 | trap_code = x1; |
| 175 | siglongjmp(jb, 1); |
| 176 | } |
| 177 | |
| 178 | typedef uint64_t (*NativeFn)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, void *); |
| 179 | |
| 180 | typedef struct { |
| 181 | uint8_t *map; |
| 182 | size_t maplen; |
| 183 | NativeFn fn; |
| 184 | } Native; |
| 185 | |
| 186 | static int native_load(const char *code_path, const char *entries_path, const char *fname, Native *nt) |
| 187 | { |
| 188 | size_t n, en; |
| 189 | uint8_t *code = slurp(code_path, &n); |
| 190 | uint8_t *ent = slurp(entries_path, &en); |
| 191 | long off = -1; |
| 192 | if (code && ent) { |
| 193 | for (char *line = strtok((char *)ent, "\n"); line; line = strtok(NULL, "\n")) { |
| 194 | char *name = strrchr(line, ' '); |
| 195 | if (name && strcmp(name + 1, fname) == 0) off = strtol(line, NULL, 10); |
| 196 | } |
| 197 | } |
| 198 | free(ent); |
| 199 | if (!code || off < 0 || (size_t)off >= n) { free(code); return -1; } |
| 200 | nt->maplen = (n + 4095) & ~(size_t)4095; |
| 201 | nt->map = mmap(NULL, nt->maplen, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 202 | if (nt->map == MAP_FAILED) { free(code); return -1; } |
| 203 | memcpy(nt->map, code, n); |
| 204 | free(code); |
| 205 | if (mprotect(nt->map, nt->maplen, PROT_READ | PROT_EXEC)) { munmap(nt->map, nt->maplen); return -1; } |
| 206 | __builtin___clear_cache((char *)nt->map, (char *)nt->map + n); |
| 207 | nt->fn = (NativeFn)(void *)(nt->map + off); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | /* 0 returned (*ret), 1 trapped (*code) */ |
| 212 | static int native_run(const Native *nt, const uint64_t *a, int na, uint64_t *ret, uint64_t *code) |
| 213 | { |
| 214 | uint64_t x[6] = {0}; |
| 215 | for (int i = 0; i < na && i < 6; i++) x[i] = a[i]; |
| 216 | if (sigsetjmp(jb, 0) == 0) { |
| 217 | *ret = nt->fn(x[0], x[1], x[2], x[3], x[4], x[5], 0, rt_table); |
| 218 | return 0; |
| 219 | } |
| 220 | *code = trap_code; |
| 221 | return 1; |
| 222 | } |
| 223 | |
| 224 | /* ------------------------------------------------------------------ values */ |
| 225 | |
| 226 | static int width(uint8_t s) |
| 227 | { |
| 228 | static const int w[] = {0, 1, 8, 16, 32, 64, 8, 16, 32, 64}; |
| 229 | return s <= S_I64 ? w[s] : 0; |
| 230 | } |
| 231 | static int is_signed(uint8_t s) { return s >= S_I8 && s <= S_I64; } |
| 232 | static uint64_t mask(int w) { return w == 64 ? ~0ULL : (1ULL << w) - 1; } |
| 233 | |
| 234 | /* canonical 64-bit register value of the low w bits of v */ |
| 235 | static uint64_t canon(uint64_t v, uint8_t s) |
| 236 | { |
| 237 | int w = width(s); |
| 238 | v &= mask(w); |
| 239 | if (is_signed(s) && w < 64 && (v >> (w - 1)) & 1) v |= ~mask(w); |
| 240 | return v; |
| 241 | } |
| 242 | |
| 243 | /* edge values of a type, as low-w-bit patterns */ |
| 244 | static int edges(uint8_t s, uint64_t *out) |
| 245 | { |
| 246 | int w = width(s), n = 0; |
| 247 | if (w == 1) { out[0] = 0; out[1] = 1; return 2; } |
| 248 | uint64_t m = mask(w), msb = 1ULL << (w - 1); |
| 249 | uint64_t base[] = {0, 1, 2, 3, m, m - 1, msb, msb + 1, msb - 1, msb - 2}; |
| 250 | for (unsigned i = 0; i < sizeof base / sizeof *base; i++) out[n++] = base[i] & m; |
| 251 | for (int k = 4; k <= w + 1; k++) out[n++] = (uint64_t)k & m; /* every shift amount */ |
| 252 | return n; |
| 253 | } |
| 254 | |
| 255 | static uint64_t rng = 88172645463325252ULL; |
| 256 | static uint64_t rnd(void) |
| 257 | { |
| 258 | rng ^= rng << 13; |
| 259 | rng ^= rng >> 7; |
| 260 | rng ^= rng << 17; |
| 261 | return rng; |
| 262 | } |
| 263 | |
| 264 | /* operand vectors for params 0..np-1: exhaustive up to 16 bits, else edges x edges + random */ |
| 265 | typedef struct { |
| 266 | uint64_t (*v)[6]; |
| 267 | long n, cap; |
| 268 | } Vecs; |
| 269 | |
| 270 | static void push(Vecs *vs, const uint64_t *x, int np) |
| 271 | { |
| 272 | if (vs->n == vs->cap) { vs->cap = vs->cap ? vs->cap * 2 : 1024; vs->v = realloc(vs->v, (size_t)vs->cap * sizeof *vs->v); } |
| 273 | for (int i = 0; i < 6; i++) vs->v[vs->n][i] = i < np ? x[i] : 0; |
| 274 | vs->n++; |
| 275 | } |
| 276 | |
| 277 | static void vectors(const C2Func *f, int np, Vecs *vs) |
| 278 | { |
| 279 | int bits = 0; |
| 280 | for (int i = 0; i < np; i++) bits += width(f->vt[i].s); |
| 281 | uint64_t x[6] = {0}; |
| 282 | if (bits <= 16) { |
| 283 | for (uint64_t a = 0; a < (1ULL << bits); a++) { |
| 284 | int sh = 0; |
| 285 | for (int i = 0; i < np; i++) { int w = width(f->vt[i].s); x[i] = (a >> sh) & mask(w); sh += w; } |
| 286 | push(vs, x, np); |
| 287 | } |
| 288 | return; |
| 289 | } |
| 290 | uint64_t e[6][80]; |
| 291 | int ne[6], idx[6] = {0}; |
| 292 | long combos = 1; |
| 293 | for (int i = 0; i < np; i++) { ne[i] = edges(f->vt[i].s, e[i]); combos *= ne[i]; } |
| 294 | if (combos <= 20000) |
| 295 | for (long c = 0; c < combos; c++) { |
| 296 | long r = c; |
| 297 | for (int i = 0; i < np; i++) { idx[i] = (int)(r % ne[i]); r /= ne[i]; x[i] = e[i][idx[i]]; } |
| 298 | push(vs, x, np); |
| 299 | } |
| 300 | for (int k = 0; k < 3000; k++) { |
| 301 | for (int i = 0; i < np; i++) { |
| 302 | /* mix: sometimes an edge value, sometimes a uniform pattern */ |
| 303 | uint64_t r = rnd(); |
| 304 | x[i] = (r & 3) == 0 ? e[i][(r >> 8) % (uint64_t)ne[i]] : rnd() & mask(width(f->vt[i].s)); |
| 305 | } |
| 306 | push(vs, x, np); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | static void put_bits(uint8_t *bits, int *at, uint64_t v, int w) |
| 311 | { |
| 312 | for (int k = 0; k < w; k++) bits[(*at)++] = (uint8_t)((v >> k) & 1); |
| 313 | } |
| 314 | |
| 315 | /* ------------------------------------------------------------------ one program */ |
| 316 | |
| 317 | typedef struct { |
| 318 | long vectors, native_traps, mismatches, undetermined; |
| 319 | char first[200]; |
| 320 | } Stat; |
| 321 | |
| 322 | static void note(Stat *st, const char *what, const uint64_t *x, int np, uint64_t extra) |
| 323 | { |
| 324 | st->mismatches++; |
| 325 | if (st->first[0]) return; |
| 326 | int k = snprintf(st->first, sizeof st->first, "%s at", what); |
| 327 | for (int i = 0; i < np && k < (int)sizeof st->first; i++) |
| 328 | k += snprintf(st->first + k, sizeof st->first - (size_t)k, " %#llx", (unsigned long long)x[i]); |
| 329 | if (k < (int)sizeof st->first) snprintf(st->first + k, sizeof st->first - (size_t)k, " (%#llx)", (unsigned long long)extra); |
| 330 | } |
| 331 | |
| 332 | /* compare the CNF of irfile with the code of codebase; value mode for op_* programs */ |
| 333 | static int compare(const char *dir, const char *irname, const char *codename, Stat *st) |
| 334 | { |
| 335 | char p[1024], q[1024]; |
| 336 | memset(st, 0, sizeof *st); |
| 337 | snprintf(p, sizeof p, "%s/%s.ir", dir, irname); |
| 338 | size_t n; |
| 339 | uint8_t *b = slurp(p, &n); |
| 340 | C2Unit *u = calloc(1, sizeof *u); |
| 341 | C2Verdict v; |
| 342 | if (!b || !u || c2_decode(b, n, u, &v) || c2_check_unit(u, &v) || u->nfuncs != 1) { |
| 343 | printf("FAIL %s: IR not accepted by checker2\n", irname); |
| 344 | free(b); |
| 345 | free(u); |
| 346 | return -1; |
| 347 | } |
| 348 | free(b); |
| 349 | C2Cnf cnf; |
| 350 | C2BlastInfo bi; |
| 351 | memset(&cnf, 0, sizeof cnf); |
| 352 | if (c2_blast_notrap(u, 0, &cnf, &bi)) { |
| 353 | printf("FAIL %s: not blasted (%s %s)\n", irname, bi.rule, bi.detail); |
| 354 | c2_unit_free(u); |
| 355 | free(u); |
| 356 | return -1; |
| 357 | } |
| 358 | Native nt; |
| 359 | snprintf(p, sizeof p, "%s/%s.code", dir, codename); |
| 360 | snprintf(q, sizeof q, "%s/%s.entries", dir, codename); |
| 361 | const C2Func *f = &u->fn[0]; |
| 362 | int rc = -1; |
| 363 | if (native_load(p, q, f->name, &nt)) { |
| 364 | printf("FAIL %s: cannot load native code %s\n", irname, codename); |
| 365 | goto out; |
| 366 | } |
| 367 | Ev e; |
| 368 | ev_init(&e, &cnf); |
| 369 | int value_mode = strncmp(irname, "op_", 3) == 0; |
| 370 | int np = f->nparams, nops = value_mode ? np - 1 : np; |
| 371 | uint8_t cs = f->vt[np - 1].s; |
| 372 | int cw = width(cs); |
| 373 | Vecs vs = {0}; |
| 374 | vectors(f, nops, &vs); |
| 375 | uint8_t *bits = malloc((size_t)bi.inputs + 1); |
| 376 | for (long i = 0; i < vs.n; i++) { |
| 377 | uint64_t a[6], ret = 0, code = 0; |
| 378 | int at = 0; |
| 379 | for (int k = 0; k < nops; k++) { a[k] = canon(vs.v[i][k], f->vt[k].s); put_bits(bits, &at, vs.v[i][k], width(f->vt[k].s)); } |
| 380 | st->vectors++; |
| 381 | if (!value_mode) { |
| 382 | int t = native_run(&nt, a, np, &ret, &code); |
| 383 | int native_trap = t && code != TRAP_REQUIRES; |
| 384 | st->native_traps += native_trap; |
| 385 | int s = ev_sat(&e, bits, bi.inputs); |
| 386 | if (s < 0) { st->undetermined++; continue; } |
| 387 | if (s != native_trap) note(st, native_trap ? "code traps, CNF holds" : "CNF traps, code holds", vs.v[i], nops, code); |
| 388 | continue; |
| 389 | } |
| 390 | /* value mode: last parameter c of the result type */ |
| 391 | a[np - 1] = 0; |
| 392 | int t = native_run(&nt, a, np, &ret, &code); |
| 393 | int optrap = 0; |
| 394 | uint64_t r = 0; |
| 395 | if (!t) r = ret & mask(cw); |
| 396 | else if (code == TRAP_ENSURES) { |
| 397 | a[np - 1] = canon(1, cs); |
| 398 | if (native_run(&nt, a, np, &ret, &code) || (ret & mask(cw)) != 0) { note(st, "ENSURES trap at c=0 not confirmed at c=1", vs.v[i], nops, code); continue; } |
| 399 | r = 0; |
| 400 | } else optrap = 1; |
| 401 | st->native_traps += optrap; |
| 402 | uint64_t cvals[2] = {optrap ? 0 : r, optrap ? 1 : (r ^ 1) & mask(cw)}; |
| 403 | int want[2] = {1, optrap}; |
| 404 | for (int j = 0; j < 2; j++) { |
| 405 | int at2 = at; |
| 406 | put_bits(bits, &at2, cvals[j], cw); |
| 407 | int s = ev_sat(&e, bits, bi.inputs); |
| 408 | if (s < 0) { st->undetermined++; continue; } |
| 409 | if (s != want[j]) |
| 410 | note(st, optrap ? "code traps, CNF does not" : (j ? "CNF disagrees: traps at c = r^1" : "CNF disagrees: no trap at c = r"), |
| 411 | vs.v[i], nops, optrap ? code : r); |
| 412 | } |
| 413 | } |
| 414 | free(bits); |
| 415 | free(vs.v); |
| 416 | ev_free(&e); |
| 417 | munmap(nt.map, nt.maplen); |
| 418 | rc = 0; |
| 419 | out: |
| 420 | c2_cnf_free(&cnf); |
| 421 | c2_unit_free(u); |
| 422 | free(u); |
| 423 | return rc; |
| 424 | } |
| 425 | |
| 426 | static int by_name(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } |
| 427 | |
| 428 | int main(int argc, char **argv) |
| 429 | { |
| 430 | if (argc != 2) { fprintf(stderr, "usage: test_c2_blast_native <work-dir>\n"); return 2; } |
| 431 | const char *dir = argv[1]; |
| 432 | for (unsigned i = 0; i < sizeof rt_table / sizeof *rt_table; i++) rt_table[i] = (void *)rt_entry; |
| 433 | DIR *d = opendir(dir); |
| 434 | if (!d) { printf("FAIL cannot open %s\n", dir); return 1; } |
| 435 | char **names = NULL; |
| 436 | int nn = 0, cap = 0; |
| 437 | struct dirent *de; |
| 438 | while ((de = readdir(d))) { |
| 439 | size_t l = strlen(de->d_name); |
| 440 | if (l <= 3 || strcmp(de->d_name + l - 3, ".ir")) continue; |
| 441 | if (nn == cap) { cap = cap ? cap * 2 : 64; names = realloc(names, (size_t)cap * sizeof *names); } |
| 442 | names[nn] = strndup(de->d_name, l - 3); |
| 443 | nn++; |
| 444 | } |
| 445 | closedir(d); |
| 446 | qsort(names, (size_t)nn, sizeof *names, by_name); |
| 447 | long vec = 0, ntraps = 0; |
| 448 | int ops = 0, gates = 0; |
| 449 | for (int i = 0; i < nn; i++) { |
| 450 | Stat st; |
| 451 | if (compare(dir, names[i], names[i], &st)) { fails++; free(names[i]); continue; } |
| 452 | vec += st.vectors; |
| 453 | ntraps += st.native_traps; |
| 454 | if (st.vectors <= 0) { |
| 455 | /* omega#454: a program that ran no vectors proves nothing; it must not count as agreeing */ |
| 456 | printf("FAIL %s: no vectors were run\n", names[i]); |
| 457 | fails++; |
| 458 | } else if (st.mismatches || st.undetermined) { |
| 459 | printf("FAIL %s: %ld of %ld vectors disagree, %ld undetermined; first: %s\n", names[i], st.mismatches, st.vectors, |
| 460 | st.undetermined, st.first); |
| 461 | fails++; |
| 462 | } else if (strncmp(names[i], "op_", 3) == 0) ops++; |
| 463 | else gates++; |
| 464 | free(names[i]); |
| 465 | } |
| 466 | free(names); |
| 467 | /* negative controls: the CNF of one operation against the code of another must disagree */ |
| 468 | char cp[1024]; |
| 469 | snprintf(cp, sizeof cp, "%s/controls", dir); |
| 470 | FILE *cf = fopen(cp, "r"); |
| 471 | int controls = 0; |
| 472 | char a[256], b[256]; |
| 473 | while (cf && fscanf(cf, "%255s %255s", a, b) == 2) { |
| 474 | Stat st; |
| 475 | if (compare(dir, a, b, &st) == 0 && st.mismatches > 0) controls++; |
| 476 | else { printf("FAIL control %s vs code of %s: no disagreement found\n", a, b); fails++; } |
| 477 | } |
| 478 | if (cf) fclose(cf); |
| 479 | if (ops < 180 || gates < 29 || controls < 8) { printf("FAIL too little coverage: %d operations, %d gate programs, %d controls\n", ops, gates, controls); fails++; } |
| 480 | printf("test_c2_blast_native: %d operation programs and %d gate programs agree with oscc's code on %ld vectors " |
| 481 | "(%ld native traps); %d controls disagree as required; %d failures\n", ops, gates, vec, ntraps, controls, fails); |
| 482 | return fails ? 1 : 0; |
| 483 | } |
| 484 | #endif |