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 / src / turing / ty_ctr1.c

177 lines · 5815 bytes Raw Blame History
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/* Turing Yield CTR1 reader. See ty_ctr1.h. */
18#include "turing/ty_ctr1.h"
19
20#include "turing/ty_math.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/stat.h>
26
27void ty_stream_init(ty_stream *s) { memset(s, 0, sizeof *s); }
28
29void ty_stream_free(ty_stream *s) {
30 free(s->ev);
31 memset(s, 0, sizeof *s);
32}
33
34int ty_stream_push(ty_stream *s, const ty_ev *e) {
35 if (s->n == s->cap) {
36 size_t nc = s->cap ? s->cap * 2 : 4096;
37 ty_ev *p = realloc(s->ev, nc * sizeof *p);
38 if (!p) return TY_E_IO;
39 s->ev = p;
40 s->cap = nc;
41 }
42 s->ev[s->n++] = *e;
43 if (e->first) s->ncrumb++;
44 return TY_OK;
45}
46
47static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); }
48static uint32_t rd32(const uint8_t *p) {
49 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
50}
51
52#define WHY(...) \
53 do { \
54 if (why && whylen) snprintf(why, whylen, __VA_ARGS__); \
55 } while (0)
56
57int ty_ctr1_decode(const uint8_t r[TY_CTR1_BYTES], int64_t prev_idx, ty_ev *out, char *why, size_t whylen) {
58 if (memcmp(r, "CTR1", 4) != 0) {
59 WHY("bad magic");
60 return TY_E_FORMAT;
61 }
62 if (rd16(r + 4) != 1) {
63 WHY("version %u != 1", rd16(r + 4));
64 return TY_E_FORMAT;
65 }
66 uint8_t kind = r[6], rc = r[7], origin = r[22], prune = r[23], verify = r[24], fit = r[25];
67 uint32_t idx = rd32(r + 8);
68 uint16_t op = rd16(r + 20), depth = rd16(r + 29);
69 memset(out, 0, sizeof *out);
70 if (origin != 0) {
71 WHY("op_origin %u (control condition has library disabled: all ops are base)", origin);
72 return TY_E_FORMAT;
73 }
74 if (verify > 4 || fit > 2) {
75 WHY("verify %u / fit %u out of range", verify, fit);
76 return TY_E_FORMAT;
77 }
78 if (kind == 1) { /* EXPAND */
79 if (op >= TY_CTR1_NOPS) {
80 WHY("op_index %u >= %u", op, TY_CTR1_NOPS);
81 return TY_E_FORMAT;
82 }
83 if (prune > 4) {
84 WHY("prune %u out of range", prune);
85 return TY_E_FORMAT;
86 }
87 if (prune != 0) {
88 if (rc != 1) {
89 WHY("pruned event with result_class %u (must be Pruned=1)", rc);
90 return TY_E_FORMAT;
91 }
92 out->sym = (uint8_t)(TY_O_P_EQUIV + (prune - 1));
93 } else {
94 if (rc < 2 || rc > 4) {
95 WHY("unpruned EXPAND with result_class %u", rc);
96 return TY_E_FORMAT;
97 }
98 out->sym = (uint8_t)(TY_O_FAILED + (rc - 2));
99 }
100 out->op = (uint8_t)op;
101 } else if (kind == 2) { /* SUBMIT */
102 if (prune != 0 || rc < 5 || rc > 7) {
103 WHY("SUBMIT with prune %u result_class %u", prune, rc);
104 return TY_E_FORMAT;
105 }
106 out->sym = rc == 5 ? TY_O_ACCEPT : TY_O_REJECT;
107 out->op = TY_OP_SUBMIT;
108 } else {
109 WHY("kind %u", kind);
110 return TY_E_FORMAT;
111 }
112 out->depth = (uint8_t)(depth > TY_DEPTH_CLIP ? TY_DEPTH_CLIP : depth);
113 out->idx = idx;
114 if (idx == 0) {
115 out->first = 1;
116 } else if (prev_idx < 0 || (int64_t)idx <= prev_idx) {
117 WHY("event_index %u after %lld without a crumb start", idx, (long long)prev_idx);
118 return TY_E_FORMAT;
119 }
120 return TY_OK;
121}
122
123int64_t ty_ctr1_read(const char *path, ty_stream *s, char *why, size_t whylen) {
124 struct stat st;
125 if (stat(path, &st) != 0) {
126 WHY("cannot stat %s", path);
127 return TY_E_IO;
128 }
129 if (st.st_size % TY_CTR1_BYTES != 0) {
130 WHY("size %lld is not a multiple of %d", (long long)st.st_size, TY_CTR1_BYTES);
131 return TY_E_FORMAT;
132 }
133 FILE *f = fopen(path, "rb");
134 if (!f) {
135 WHY("cannot open %s", path);
136 return TY_E_IO;
137 }
138 enum { BATCH = 4096 };
139 uint8_t *buf = malloc((size_t)BATCH * TY_CTR1_BYTES);
140 if (!buf) {
141 fclose(f);
142 return TY_E_IO;
143 }
144 int64_t n = 0, prev = -1;
145 uint32_t crumb = 0;
146 int rc = TY_OK;
147 size_t k;
148 while (rc == TY_OK && (k = fread(buf, TY_CTR1_BYTES, BATCH, f)) > 0) {
149 for (size_t i = 0; i < k; ++i) {
150 ty_ev e;
151 char w[160];
152 rc = ty_ctr1_decode(buf + i * TY_CTR1_BYTES, prev, &e, w, sizeof w);
153 if (rc != TY_OK) {
154 WHY("%s record %lld: %s", path, (long long)n, w);
155 break;
156 }
157 if (e.first) {
158 if (n > 0) ++crumb;
159 } else if ((int64_t)e.idx != prev + 1) {
160 s->gaps++;
161 }
162 e.crumb = (uint16_t)(crumb > 65535 ? 65535 : crumb);
163 prev = e.idx;
164 if ((rc = ty_stream_push(s, &e)) != TY_OK) break;
165 ++n;
166 }
167 }
168 if (rc == TY_OK && ferror(f)) rc = TY_E_IO;
169 fclose(f);
170 free(buf);
171 if (rc != TY_OK) return rc;
172 if (n * TY_CTR1_BYTES != (int64_t)st.st_size) {
173 WHY("read %lld records, expected %lld", (long long)n, (long long)(st.st_size / TY_CTR1_BYTES));
174 return TY_E_IO;
175 }
176 return n;
177}