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

200 lines · 6930 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 TY-1 math. See ty_math.h. Plain C11, integer scoring path. */
18#include "turing/ty_math.h"
19
20#include <math.h>
21#include <string.h>
22
23const char *ty_err_name(int e) {
24 switch (e) {
25 case TY_OK: return "OK";
26 case TY_E_NAN: return "NAN";
27 case TY_E_INF: return "INF";
28 case TY_E_NEG: return "NEGATIVE";
29 case TY_E_NORM: return "NOT_NORMALIZED";
30 case TY_E_ZERO_REALIZED: return "ZERO_PROB_ON_REALIZED";
31 case TY_E_UNDERFLOW: return "UNDERFLOW";
32 case TY_E_RANGE: return "RANGE";
33 case TY_E_OVERFLOW: return "OVERFLOW";
34 case TY_E_ARG: return "ARG";
35 case TY_E_FORMAT: return "FORMAT";
36 case TY_E_PROFILE: return "PROFILE_MISMATCH";
37 case TY_E_BASELINE: return "BASELINE_MISMATCH";
38 case TY_E_LEAK: return "LEAK_HELDOUT_IN_FIT";
39 case TY_E_PROVENANCE: return "PROVENANCE_MISMATCH";
40 case TY_E_DIGEST: return "DIGEST_MISMATCH";
41 case TY_E_IO: return "IO";
42 default: return "?";
43 }
44}
45
46/* Bit-by-bit binary logarithm. x = 2^e * m with m in [1, 2) held as Q62.
47 * Each step squares m (exact 124-bit product, truncated back to Q62); when
48 * m^2 >= 2 the next fractional bit is 1 and m is halved. Truncating the product
49 * loses < 2^-62 relative per step; over 32 steps that perturbs m by < 2^-29
50 * relative at the last step, which can move the result by < 2^-31. */
51uint64_t ty_log2_q32(uint64_t x) {
52 if (x == 0) return 0;
53 unsigned e = 63u - (unsigned)__builtin_clzll(x);
54 ty_u128 m;
55 if (e <= 62)
56 m = (ty_u128)x << (62 - e);
57 else
58 m = (ty_u128)(x >> 1); /* e == 63: drops one low bit, < 2^-62 relative */
59 uint64_t frac = 0;
60 const ty_u128 two = (ty_u128)1 << 63;
61 for (int i = 0; i < 32; ++i) {
62 m = (m * m) >> 62;
63 frac <<= 1;
64 if (m >= two) {
65 m >>= 1;
66 frac |= 1;
67 }
68 }
69 return ((uint64_t)e << 32) | frac;
70}
71
72/* Round a nonnegative Q32 bit count to micro-bits. */
73static int64_t q32_to_ub(uint64_t v) {
74 ty_u128 t = (ty_u128)v * (uint64_t)TY_UB_PER_BIT + ((uint64_t)1 << 31);
75 return (int64_t)(t >> 32);
76}
77
78static int64_t ub_table[TY_QONE + 1];
79static int ub_table_ready;
80
81static void ub_table_init(void) {
82 ub_table[0] = TY_E_RANGE;
83 for (uint32_t q = 1; q <= TY_QONE; ++q) {
84 uint64_t v = ((uint64_t)TY_QBITS << 32) - ty_log2_q32(q); /* q <= 2^16, so v >= 0 */
85 ub_table[q] = q32_to_ub(v);
86 }
87 ub_table_ready = 1;
88}
89
90int64_t ty_ubits_q16(uint32_t q) {
91 if (q == 0 || q > TY_QONE) return TY_E_RANGE;
92 if (!ub_table_ready) ub_table_init();
93 return ub_table[q];
94}
95
96int ty_ubits_ratio(uint64_t num, uint64_t den, int64_t *out) {
97 if (!out) return TY_E_ARG;
98 if (num == 0) return TY_E_ZERO_REALIZED;
99 if (num > den) return TY_E_RANGE;
100 uint64_t v = ty_log2_q32(den) - ty_log2_q32(num);
101 /* Both logs truncate by < 2^-31; the difference is off by < 2^-31. Guard the
102 * num == den case against a spurious negative. */
103 if (num == den) v = 0;
104 *out = q32_to_ub(v);
105 return TY_OK;
106}
107
108int ty_validate_f64(const double *p, size_t n, double tol) {
109 if (!p || n == 0 || !(tol >= 0.0) || isinf(tol)) return TY_E_ARG;
110 double s = 0.0;
111 for (size_t i = 0; i < n; ++i) {
112 if (isnan(p[i])) return TY_E_NAN;
113 if (isinf(p[i])) return TY_E_INF;
114 if (p[i] < 0.0 || signbit(p[i])) {
115 if (p[i] == 0.0) continue; /* -0.0 counts as zero */
116 return TY_E_NEG;
117 }
118 s += p[i];
119 }
120 if (isinf(s) || fabs(s - 1.0) > tol) return TY_E_NORM;
121 return TY_OK;
122}
123
124int ty_ubits_f64(const double *p, size_t n, size_t realized, double tol, int64_t *out) {
125 if (!out || realized >= n) return TY_E_ARG;
126 int rc = ty_validate_f64(p, n, tol);
127 if (rc != TY_OK) return rc;
128 double v = p[realized];
129 if (v == 0.0) return TY_E_ZERO_REALIZED;
130 if (fpclassify(v) == FP_SUBNORMAL) return TY_E_UNDERFLOW;
131 if (v > 1.0) return TY_E_RANGE; /* only possible inside tol; refuse, never clamp */
132 double scaled = ldexp(v, 62); /* exact: power-of-two scaling */
133 if (scaled < 1.0) return TY_E_UNDERFLOW;
134 uint64_t num = (uint64_t)scaled; /* truncation, exact integer part */
135 return ty_ubits_ratio(num, (uint64_t)1 << 62, out);
136}
137
138int ty_add(int64_t a, int64_t b, int64_t *out) {
139 if (!out) return TY_E_ARG;
140 if (__builtin_add_overflow(a, b, out)) return TY_E_OVERFLOW;
141 return TY_OK;
142}
143
144int ty_sub(int64_t a, int64_t b, int64_t *out) {
145 if (!out) return TY_E_ARG;
146 if (__builtin_sub_overflow(a, b, out)) return TY_E_OVERFLOW;
147 return TY_OK;
148}
149
150int ty_bits_to_ub(uint64_t bits, int64_t *out) {
151 if (!out) return TY_E_ARG;
152 if (bits > (uint64_t)(INT64_MAX / TY_UB_PER_BIT)) return TY_E_OVERFLOW;
153 *out = (int64_t)bits * TY_UB_PER_BIT;
154 return TY_OK;
155}
156
157int ty_dl(uint64_t model_bits, int64_t data_ub, int64_t *out) {
158 int64_t m;
159 if (data_ub < 0) return TY_E_RANGE;
160 int rc = ty_bits_to_ub(model_bits, &m);
161 if (rc != TY_OK) return rc;
162 return ty_add(m, data_ub, out);
163}
164
165int ty_gain(int64_t dl_baseline_ub, int64_t dl_candidate_ub, int64_t *out) {
166 if (dl_baseline_ub < 0 || dl_candidate_ub < 0) return TY_E_RANGE;
167 return ty_sub(dl_baseline_ub, dl_candidate_ub, out);
168}
169
170int ty_quantize_kt(const uint64_t *counts, unsigned K, uint32_t *q) {
171 if (!counts || !q || K < 2 || K > TY_KMAX) return TY_E_ARG;
172 ty_u128 w[TY_KMAX], W = 0;
173 for (unsigned s = 0; s < K; ++s) {
174 if (counts[s] > ((uint64_t)1 << 62)) return TY_E_RANGE;
175 w[s] = (ty_u128)counts[s] * 2 + 1; /* KT: n + 1/2, doubled */
176 W += w[s];
177 }
178 const uint64_t R = TY_QONE - K; /* one unit per symbol is the floor */
179 uint64_t base_sum = 0;
180 ty_u128 rem[TY_KMAX];
181 for (unsigned s = 0; s < K; ++s) {
182 ty_u128 t = (ty_u128)R * w[s];
183 uint64_t b = (uint64_t)(t / W);
184 rem[s] = t % W;
185 q[s] = 1u + (uint32_t)b;
186 base_sum += b;
187 }
188 uint64_t left = R - base_sum; /* < K */
189 unsigned char given[TY_KMAX];
190 memset(given, 0, sizeof given);
191 while (left > 0) {
192 int best = -1;
193 for (unsigned s = 0; s < K; ++s)
194 if (!given[s] && (best < 0 || rem[s] > rem[best])) best = (int)s;
195 given[best] = 1;
196 q[best] += 1;
197 --left;
198 }
199 return TY_OK;
200}