interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / one_layer / generalization / corpus / oracle.c

118 lines · 6738 bytes Raw Blame History
1/* tests/one_layer/generalization/corpus/oracle.c -- independent C oracle for the GEN-3 public corpus
2 * (aien-spark/omega#478). `oracle <task_id> < inputs > expected`: one input line (decimal arguments, as the
3 * harness reads them) gives one expected line, the decimal result or `TRAP 2` (division by zero, the only
4 * trap these tasks have). Written from each task's prompt, separately from the Omegese solutions and the OSC
5 * references, so a shared mistake has to be made three times. Unsigned 32-bit arithmetic wraps modulo 2^32, as
6 * in OSC-1 and Omegese (docs/osc/OSC-2-DESIGN.md: wrap for unsigned types); it is computed in 64 bits and masked. */
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#define M32 0xffffffffull
13typedef uint64_t u;
14static int trap; /* 0, or the trap code of the current case */
15static u chk(u v) { return v & M32; } /* unsigned 32-bit wrap */
16
17static u clampv(u x, u lo, u hi) { return x < lo ? lo : x > hi ? hi : x; }
18static u adiff(u a, u b) { return a > b ? a - b : b - a; }
19static u sat8(u x) { return x > 255 ? 255 : x; }
20static u dsum(u n) { u s = 0; do { s += n % 10; n /= 10; } while (n); return s; }
21static u dcount(u n) { u c = 1; while (n >= 10) { c++; n /= 10; } return c; }
22static u pcount(u x) { u c = 0; for (; x; x >>= 1) c += x & 1; return c; }
23static u gcdv(u a, u b) { while (b) { u t = a % b; a = b; b = t; } return a; }
24static u max4(const u *v) { u m = v[0]; for (int i = 1; i < 4; i++) if (v[i] > m) m = v[i]; return m; }
25static u sum4(const u *v) { return v[0] + v[1] + v[2] + v[3]; }
26static u avg2(u a, u b) { return (a + b) / 2; } /* exact in 64 bits */
27
28static u task(const char *t, const u *a)
29{
30#define T(name) if (!strcmp(t, name))
31 T("clamp_sum") return clampv(chk(a[0] + a[1]), 10, 100);
32 T("clamp_triple") return clampv(chk(3 * a[0]), 50, 1000);
33 T("gap_inc") return chk(adiff(a[0], a[1]) + 1);
34 T("gap_half") return adiff(a[0], a[1]) / 2;
35 T("sat_sum") return sat8(chk(a[0] + a[1]));
36 T("sat_prod") return sat8(chk(a[0] * a[1]));
37 T("digsum_mod9") return dsum(a[0]) % 9;
38 T("digsum_scaled") return chk(dsum(a[0]) * a[1]);
39 T("digcount_nibbles") return 4 * dcount(a[0]);
40 T("popcnt_zero") return 32 - pcount(a[0]);
41 T("popcnt_odd") return pcount(a[0]) & 1;
42 T("lcm32") return (a[0] == 0 || a[1] == 0) ? 0 : chk(a[0] / gcdv(a[0], a[1]) * a[1]);
43 T("ds_plus_dc") return dsum(a[0]) + dcount(a[0]);
44 T("pc_times_dc") return pcount(a[0]) * dcount(a[0]);
45 T("gcd_plus_ds") return chk(gcdv(a[0], a[1]) + dsum(a[0]));
46 T("gcd_times_pc") return chk(gcdv(a[0], a[1]) * pcount(a[1]));
47 T("sum4_avg") return sum4(a) / 4;
48 T("sum4_mod") { if (a[4] == 0) { trap = 2; return 0; } return sum4(a) % a[4]; }
49 T("sum4_low") return sum4(a) & 65535;
50 T("max4_inc") return chk(max4(a) + 1);
51 T("max4_half") return max4(a) / 2;
52 T("sum_clamped") { u s = 0; for (u i = 0; i < a[0]; i++) s = chk(s + clampv(i, a[1], a[2])); return s; }
53 T("sum_dists") { u s = 0; for (u i = 0; i < a[0]; i++) s = chk(s + adiff(i, a[1])); return s; }
54 T("sum_sat") { u s = 0; for (u i = 0; i < a[0]; i++) s = chk(s + sat8(chk(i * a[1]))); return s; }
55 T("even_ds_odd_dc") return a[0] % 2 == 0 ? dsum(a[0]) : dcount(a[0]);
56 T("big_pc_small_ds") return a[0] >= 1000 ? pcount(a[0]) : dsum(a[0]);
57 T("gcd_or_dc") return a[0] > a[1] ? gcdv(a[0], a[1]) : dcount(a[1]);
58 T("avg_inc") return chk(avg2(a[0], a[1]) + 1);
59 T("avg_double") return chk(2 * avg2(a[0], a[1]));
60 T("cube_inc") return chk(chk(chk(a[0] * a[0]) * a[0]) + 1);
61 T("gcd_only") return gcdv(a[0], a[1]);
62 T("ds2_inc") return dsum(dsum(a[0])) + 1;
63 T("ds2_double") return 2 * dsum(dsum(a[0]));
64 /* transfer probes (withheld from extraction) */
65 T("clamp_gap") return clampv(adiff(a[0], a[1]), a[2], a[3]);
66 T("sat_affine") return sat8(chk(chk(a[0] * a[1]) + a[2]));
67 T("even_digsums") { u c = 0; for (u i = 0; i < a[0]; i++) c += dsum(i) % 2 == 0; return c; }
68 T("hamming") return pcount(a[0] ^ a[1]);
69 T("spread4") return 4 * max4(a) - sum4(a);
70 T("avg4") return avg2(avg2(a[0], a[1]), avg2(a[2], a[3]));
71 T("digits_both") return dcount(a[0]) + dcount(a[1]);
72 T("wrap_range") { u d = chk(a[2] - a[1]); if (d == 0) { trap = 2; return 0; } return chk(a[1] + a[0] % d); }
73 T("wrap_diff") return chk(a[0] - a[1]);
74 T("wrap8") return a[0] & 255;
75 T("digit_product") { if (a[0] == 0) return 0; u p = 1, n = a[0]; while (n) { p *= n % 10; n /= 10; } return p; }
76 T("hex_digits") { u c = 1, n = a[0]; while (n >= 16) { c++; n /= 16; } return c; }
77 T("zero_bits8") return 8 - pcount(a[0] & 255);
78 T("weighted4") return a[0] + 2 * a[1] + 3 * a[2] + 4 * a[3];
79 T("second_max") { u v[4]; memcpy(v, a, sizeof v);
80 for (int i = 0; i < 4; i++) for (int j = i + 1; j < 4; j++) if (v[j] > v[i]) { u x = v[i]; v[i] = v[j]; v[j] = x; }
81 return v[1]; }
82 T("ceil_avg") return (a[0] + a[1] + 1) / 2;
83#undef T
84 fprintf(stderr, "oracle: unknown task '%s'\n", t);
85 exit(2);
86}
87
88int main(int argc, char **argv)
89{
90 /* oracle --bound <task_id>: the gabs inputs --max option for generated inputs, `<arg>:<max>`, for the tasks
91 * that loop a[0] times (their 32-bit n would run for hours); empty for every other task */
92 if (argc == 3 && !strcmp(argv[1], "--bound")) {
93 static const char *const loops[] = {"even_digsums", "sum_clamped", "sum_dists", "sum_sat"};
94 u z[8] = {0};
95 (void)task(argv[2], z); /* exits 2 on an unknown task */
96 for (size_t i = 0; i < sizeof loops / sizeof loops[0]; i++)
97 if (!strcmp(argv[2], loops[i])) printf("0:4096\n");
98 return 0;
99 }
100 if (argc != 2) { fprintf(stderr, "usage: oracle <task_id> < inputs > expected | oracle --bound <task_id>\n"); return 2; }
101 char line[512];
102 while (fgets(line, sizeof line, stdin)) {
103 u a[8] = {0};
104 int n = 0;
105 char *p = line, *e;
106 while (n < 8) {
107 while (*p == ' ') p++;
108 if (*p == '\n' || !*p) break;
109 a[n++] = strtoull(p, &e, 10);
110 if (e == p || a[n - 1] > M32) { fprintf(stderr, "oracle: bad input line\n"); return 2; }
111 p = e;
112 }
113 trap = 0;
114 u r = task(argv[1], a);
115 if (trap) printf("TRAP %d\n", trap); else printf("%llu\n", (unsigned long long)r);
116 }
117 return 0;
118}