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_prd2.c

321 lines · 9566 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#include "turing/ty_prd2.h"
18#include "turing/ty_math.h"
19
20#include <fenv.h>
21#include <math.h>
22#include <stdlib.h>
23#include <string.h>
24
25#define P2_HDR 16u
26#define P2_REC 16u /* index, family, K, reserved */
27#define P2_CMP 24u /* pi, loc, scale */
28
29const char *tyq_prd2_reason_name(int r)
30{
31 switch (r) {
32 case PRD2_R_NONE: return "NONE";
33 case PRD2_R_MAGIC: return "MAGIC";
34 case PRD2_R_VERSION: return "VERSION";
35 case PRD2_R_SIZE: return "SIZE";
36 case PRD2_R_COUNT: return "COUNT";
37 case PRD2_R_INDEX: return "INDEX";
38 case PRD2_R_FAMILY: return "FAMILY";
39 case PRD2_R_K: return "K";
40 case PRD2_R_RESERVED: return "RESERVED";
41 case PRD2_R_WEIGHT_NONFINITE: return "WEIGHT_NONFINITE";
42 case PRD2_R_WEIGHT_RANGE: return "WEIGHT_RANGE";
43 case PRD2_R_WEIGHT_SUM: return "WEIGHT_SUM";
44 case PRD2_R_GAUSS_WEIGHT: return "GAUSS_WEIGHT";
45 case PRD2_R_LOC: return "LOC";
46 case PRD2_R_SCALE: return "SCALE";
47 default: return "?";
48 }
49}
50
51static uint32_t rd32(const uint8_t *b)
52{
53 return (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
54}
55static double rdf(const uint8_t *b)
56{
57 uint64_t u = 0;
58 double d;
59 for (int i = 7; i >= 0; i--)
60 u = (u << 8) | b[i];
61 memcpy(&d, &u, 8);
62 return d;
63}
64static void wr32(uint8_t *b, uint32_t v)
65{
66 for (int i = 0; i < 4; i++)
67 b[i] = (uint8_t)(v >> (8 * i));
68}
69static void wrf(uint8_t *b, double d)
70{
71 uint64_t u;
72 memcpy(&u, &d, 8);
73 for (int i = 0; i < 8; i++)
74 b[i] = (uint8_t)(u >> (8 * i));
75}
76
77static int refuse(int *reason, int r)
78{
79 if (reason)
80 *reason = r;
81 return TYQ_FAIL_PROTOCOL;
82}
83
84int ty_prd2_validate(const tyq_pred *p, int *reason)
85{
86 if (reason)
87 *reason = PRD2_R_NONE;
88 if (!p)
89 return TYQ_E_ARG;
90 if (p->family != TYQ_FAM_GAUSS && p->family != TYQ_FAM_MIX)
91 return refuse(reason, PRD2_R_FAMILY);
92 if (p->family == TYQ_FAM_GAUSS && p->K != 1u)
93 return refuse(reason, PRD2_R_K);
94 if (p->family == TYQ_FAM_MIX && (p->K < 1u || p->K > TYQ_KMAX))
95 return refuse(reason, PRD2_R_K);
96 double sum = 0.0;
97 for (uint32_t j = 0; j < p->K; j++) {
98 const tyq_comp *c = &p->comp[j];
99 if (!isfinite(c->pi))
100 return refuse(reason, PRD2_R_WEIGHT_NONFINITE);
101 if (!(c->pi > 0.0) || c->pi > 1.0)
102 return refuse(reason, PRD2_R_WEIGHT_RANGE);
103 if (!isfinite(c->loc))
104 return refuse(reason, PRD2_R_LOC);
105 if (!isfinite(c->scale) || !(c->scale > 0.0))
106 return refuse(reason, PRD2_R_SCALE);
107 sum += c->pi;
108 }
109 if (p->family == TYQ_FAM_GAUSS) {
110 if (p->comp[0].pi != 1.0)
111 return refuse(reason, PRD2_R_GAUSS_WEIGHT);
112 if (p->loc != p->comp[0].loc || p->scale != p->comp[0].scale)
113 return refuse(reason, PRD2_R_LOC);
114 } else if (fabs(sum - 1.0) > TYQ_NORM_TOL) {
115 return refuse(reason, PRD2_R_WEIGHT_SUM);
116 }
117 return TYQ_OK;
118}
119
120void ty_prd2_free(ty_prd2 *p)
121{
122 if (!p)
123 return;
124 free(p->rec);
125 p->rec = NULL;
126 p->n = 0;
127}
128
129int ty_prd2_parse(const uint8_t *buf, size_t len, uint32_t first_index, uint32_t count,
130 ty_prd2 *out, int *reason)
131{
132 if (reason)
133 *reason = PRD2_R_NONE;
134 if (!buf || !out || count == 0)
135 return TYQ_E_ARG;
136 out->n = 0;
137 out->first_index = first_index;
138 out->rec = NULL;
139 if ((uint64_t)first_index + count > UINT32_MAX)
140 return TYQ_E_ARG;
141 if (len < P2_HDR)
142 return refuse(reason, PRD2_R_SIZE);
143 if (memcmp(buf, "PRD2", 4) != 0)
144 return refuse(reason, PRD2_R_MAGIC);
145 if (rd32(buf + 4) != 2u)
146 return refuse(reason, PRD2_R_VERSION);
147 if (rd32(buf + 8) != count || rd32(buf + 12) != first_index)
148 return refuse(reason, PRD2_R_COUNT);
149 tyq_pred *rec = calloc(count, sizeof *rec);
150 if (!rec)
151 return TYQ_E_IO;
152 size_t off = P2_HDR;
153 for (uint32_t i = 0; i < count; i++) {
154 if (len - off < P2_REC) {
155 free(rec);
156 return refuse(reason, PRD2_R_SIZE);
157 }
158 tyq_pred *r = &rec[i];
159 const uint8_t *q = buf + off;
160 r->index = rd32(q);
161 r->family = rd32(q + 4);
162 r->K = rd32(q + 8);
163 uint32_t reserved = rd32(q + 12);
164 off += P2_REC;
165 if (r->index != first_index + i) {
166 free(rec);
167 return refuse(reason, PRD2_R_INDEX);
168 }
169 if (reserved != 0u) {
170 free(rec);
171 return refuse(reason, PRD2_R_RESERVED);
172 }
173 if (r->K < 1u || r->K > TYQ_KMAX) {
174 free(rec);
175 return refuse(reason, PRD2_R_K);
176 }
177 if (len - off < (size_t)r->K * P2_CMP) {
178 free(rec);
179 return refuse(reason, PRD2_R_SIZE);
180 }
181 for (uint32_t j = 0; j < r->K; j++, off += P2_CMP) {
182 r->comp[j].pi = rdf(buf + off);
183 r->comp[j].loc = rdf(buf + off + 8);
184 r->comp[j].scale = rdf(buf + off + 16);
185 }
186 if (r->family == TYQ_FAM_GAUSS) {
187 r->loc = r->comp[0].loc;
188 r->scale = r->comp[0].scale;
189 }
190 int rr;
191 if (ty_prd2_validate(r, &rr) != TYQ_OK) {
192 free(rec);
193 return refuse(reason, rr);
194 }
195 }
196 if (off != len) {
197 free(rec);
198 return refuse(reason, PRD2_R_SIZE);
199 }
200 out->n = count;
201 out->rec = rec;
202 return TYQ_OK;
203}
204
205int ty_prd2_encode(const ty_prd2 *p, uint8_t **buf, size_t *len)
206{
207 if (!p || !buf || !len || (p->n && !p->rec))
208 return TYQ_E_ARG;
209 *buf = NULL;
210 *len = 0;
211 size_t total = P2_HDR;
212 for (uint32_t i = 0; i < p->n; i++) {
213 if (ty_prd2_validate(&p->rec[i], NULL) != TYQ_OK || p->rec[i].index != p->first_index + i)
214 return TYQ_FAIL_PROTOCOL;
215 total += P2_REC + (size_t)p->rec[i].K * P2_CMP;
216 }
217 uint8_t *b = malloc(total);
218 if (!b)
219 return TYQ_E_IO;
220 memcpy(b, "PRD2", 4);
221 wr32(b + 4, 2u);
222 wr32(b + 8, p->n);
223 wr32(b + 12, p->first_index);
224 size_t off = P2_HDR;
225 for (uint32_t i = 0; i < p->n; i++) {
226 const tyq_pred *r = &p->rec[i];
227 wr32(b + off, r->index);
228 wr32(b + off + 4, r->family);
229 wr32(b + off + 8, r->K);
230 wr32(b + off + 12, 0u);
231 off += P2_REC;
232 for (uint32_t j = 0; j < r->K; j++, off += P2_CMP) {
233 wrf(b + off, r->comp[j].pi);
234 wrf(b + off + 8, r->comp[j].loc);
235 wrf(b + off + 16, r->comp[j].scale);
236 }
237 }
238 *buf = b;
239 *len = total;
240 return TYQ_OK;
241}
242
243int ty_qcont2_bits(const tyq_pred *p, int64_t k, double *bits, int *floor_hit)
244{
245 if (!p || !bits)
246 return TYQ_E_ARG;
247 if (ty_prd2_validate(p, NULL) != TYQ_OK)
248 return TYQ_FAIL_PROTOCOL;
249 if (p->family == TYQ_FAM_GAUSS)
250 return ty_qcont_bits(p, k, bits, floor_hit);
251 if (fegetround() != FE_TONEAREST)
252 return TYQ_FAIL_PROTOCOL;
253 double b[TYQ_KMAX];
254 int anyhit = 0;
255 double m = INFINITY;
256 for (uint32_t j = 0; j < p->K; j++) {
257 tyq_pred g;
258 memset(&g, 0, sizeof g);
259 g.index = p->index;
260 g.family = TYQ_FAM_GAUSS;
261 g.loc = p->comp[j].loc;
262 g.scale = p->comp[j].scale;
263 int hit = 0;
264 int rc = ty_qcont_bits(&g, k, &b[j], &hit);
265 if (rc != TYQ_OK)
266 return rc;
267 anyhit |= hit;
268 if (b[j] < m)
269 m = b[j];
270 }
271 double acc = 0.0;
272 for (uint32_t j = 0; j < p->K; j++)
273 acc += p->comp[j].pi * exp2(-(b[j] - m));
274 double v = m - log2(acc);
275 if (!isfinite(v))
276 return TYQ_FAIL_PROTOCOL;
277 *bits = v;
278 if (floor_hit)
279 *floor_hit = anyhit;
280 return TYQ_OK;
281}
282
283int ty_qcont2_point_ub(const tyq_pred *p, int64_t k, int64_t *ub, int *floor_hit)
284{
285 if (!ub)
286 return TYQ_E_ARG;
287 if (p && p->family == TYQ_FAM_GAUSS && ty_prd2_validate(p, NULL) == TYQ_OK)
288 return ty_qcont_point_ub(p, k, ub, floor_hit);
289 double bits;
290 int rc = ty_qcont2_bits(p, k, &bits, floor_hit);
291 if (rc != TYQ_OK)
292 return rc;
293 double s = 1e6 * bits;
294 if (!isfinite(s) || s > 9.0e18 || s < -9.0e18)
295 return TYQ_FAIL_PROTOCOL;
296 *ub = llrint(s);
297 return TYQ_OK;
298}
299
300int ty_qcont2_sum_ub(const tyq_pred *p, const int64_t *k, size_t n,
301 int64_t *ld_ub, uint64_t *floor_hits)
302{
303 if (!p || !k || !ld_ub || n == 0)
304 return TYQ_E_ARG;
305 int64_t sum = 0;
306 uint64_t hits = 0;
307 for (size_t i = 0; i < n; i++) {
308 int64_t ub;
309 int hit = 0;
310 int rc = ty_qcont2_point_ub(&p[i], k[i], &ub, &hit);
311 if (rc != TYQ_OK)
312 return rc;
313 if (ty_add(sum, ub, &sum) != TY_OK)
314 return TYQ_FAIL_PROTOCOL;
315 hits += (uint64_t)hit;
316 }
317 *ld_ub = sum;
318 if (floor_hits)
319 *floor_hits = hits;
320 return TYQ_OK;
321}