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.h

107 lines · 5286 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: description-length arithmetic (docs/turing/TURING_YIELD_PROFILE_V0.md).
18 *
19 * Unit: 1 T = 1 bit of net held-out description-length gain versus a declared
20 * baseline under a versioned measurement profile. Everything that lands in a
21 * digested record is an integer: code lengths are int64 micro-bits (1 bit =
22 * 1,000,000 ub), model lengths are exact bit counts. Bits, never nats.
23 *
24 * The scoring path is integer only (no libm): a probability is a 16-bit
25 * quantized table entry q in 1..65536 (P = q / 65536), and -log2 P comes from
26 * an integer log2 (ty_log2_q32). Per-symbol error of ty_ubits_q16 against the
27 * exact real value is at most 0.5 ub (rounding) + 0.001 ub (log2 truncation),
28 * so a sum over N symbols is within 0.501 * N ub of exact, in any build.
29 *
30 * The double-precision entry points (ty_validate_f64, ty_ubits_f64) exist so
31 * that an outside predictor's distribution is checked before it is scored.
32 */
33#ifndef TURING_TY_MATH_H
34#define TURING_TY_MATH_H
35
36#include <stddef.h>
37#include <stdint.h>
38
39#define TY_QBITS 16
40#define TY_QONE 65536u /* quantized probability of 1 */
41#define TY_UB_PER_BIT 1000000LL /* micro-bits per bit */
42#define TY_KMAX 16 /* largest alphabet a model may declare */
43
44__extension__ typedef unsigned __int128 ty_u128;
45/* Worst-case |computed - exact| per scored symbol, in units of 1/1000 ub. */
46#define TY_QERR_MILLI_UB_PER_SYMBOL 501
47
48enum {
49 TY_OK = 0,
50 TY_E_NAN = -1, /* a probability is NaN */
51 TY_E_INF = -2, /* a probability is infinite */
52 TY_E_NEG = -3, /* a probability is negative */
53 TY_E_NORM = -4, /* the vector does not sum to 1 within tolerance */
54 TY_E_ZERO_REALIZED = -5, /* the outcome that happened was given probability 0 */
55 TY_E_UNDERFLOW = -6, /* realized probability below 2^-62 (or subnormal) */
56 TY_E_RANGE = -7, /* argument outside its declared range */
57 TY_E_OVERFLOW = -8, /* int64 micro-bit arithmetic would overflow */
58 TY_E_ARG = -9, /* NULL or malformed argument */
59 TY_E_FORMAT = -10, /* malformed input bytes (CTR1, model code) */
60 TY_E_PROFILE = -11, /* a model or record disagrees with the measurement profile */
61 TY_E_BASELINE = -12, /* baseline is not the one the profile declares */
62 TY_E_LEAK = -13, /* a model was fit on held-out (split-reserved) data */
63 TY_E_PROVENANCE = -14, /* fit provenance does not match the split manifest */
64 TY_E_DIGEST = -15, /* a recomputed digest differs from the recorded one */
65 TY_E_IO = -16
66};
67const char *ty_err_name(int e);
68
69/* log2(x) for x >= 1 as unsigned Q32.32 (integer part in the high 32 bits),
70 * truncated toward zero; error < 2^-31. Returns 0 for x == 0 (callers refuse
71 * zero before calling). Integer only. */
72uint64_t ty_log2_q32(uint64_t x);
73
74/* Micro-bits of -log2(q / 65536), q in 1..65536 (q = 65536 -> 0). Rounded to
75 * nearest. Negative return = TY_E_RANGE. Table-backed after first use. */
76int64_t ty_ubits_q16(uint32_t q);
77
78/* Micro-bits of -log2(num / den), 1 <= num <= den. */
79int ty_ubits_ratio(uint64_t num, uint64_t den, int64_t *out);
80
81/* Validate a probability vector: no NaN/Inf/negative, |sum - 1| <= tol
82 * (summed in index order), n >= 1. */
83int ty_validate_f64(const double *p, size_t n, double tol);
84
85/* Validate p, then micro-bits of -log2 p[realized]. The realized value is
86 * converted exactly to a Q62 integer (ldexp + truncation), so the result is
87 * build-independent: ZERO_REALIZED if p[realized] == 0, UNDERFLOW if it is
88 * subnormal or below 2^-62. Normalization is not re-imposed; a vector off by
89 * less than tol is scored as given. */
90int ty_ubits_f64(const double *p, size_t n, size_t realized, double tol, int64_t *out);
91
92/* Checked int64 arithmetic for description lengths. */
93int ty_add(int64_t a, int64_t b, int64_t *out);
94int ty_sub(int64_t a, int64_t b, int64_t *out);
95int ty_bits_to_ub(uint64_t bits, int64_t *out);
96/* DL(M, D) = L(M) + L(D|M): model length in whole bits, data length in ub. */
97int ty_dl(uint64_t model_bits, int64_t data_ub, int64_t *out);
98/* T(M; B, D) = DL(B, D) - DL(M, D), in ub. */
99int ty_gain(int64_t dl_baseline_ub, int64_t dl_candidate_ub, int64_t *out);
100
101/* Profile estimator: KT (add-1/2) smoothing of counts, then 16-bit
102 * quantization by largest remainder with every entry >= 1 (the floor) and the
103 * K entries summing to exactly 65536. Ties in remainder go to the lower
104 * symbol index. 2 <= K <= TY_KMAX. */
105int ty_quantize_kt(const uint64_t *counts, unsigned K, uint32_t *q);
106
107#endif /* TURING_TY_MATH_H */