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
omega-mv / src / turing / ty_qcont.c
| 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 | /* qint.v1 closed-form scorer. See ty_qcont.h. binary64, log space, no |
| 18 | * probability floor, no renormalization, no caps. */ |
| 19 | #include "turing/ty_qcont.h" |
| 20 | #include "turing/ty_math.h" |
| 21 | |
| 22 | #include <fenv.h> |
| 23 | #include <math.h> |
| 24 | |
| 25 | #define TYQ_LN2 0.693147180559945309417232121458 |
| 26 | #define TYQ_HLN2PI 0.918938533204672741780329736406 /* 0.5*ln(2*pi) */ |
| 27 | |
| 28 | const char *tyq_status_name(int s) |
| 29 | { |
| 30 | switch (s) { |
| 31 | case TYQ_OK: return "OK"; |
| 32 | case TYQ_FAIL_PROTOCOL: return "FAIL_PROTOCOL"; |
| 33 | case TYQ_FAIL_FLOOR: return "FAIL_FLOOR"; |
| 34 | case TYQ_FAIL_LEAK: return "FAIL_LEAK"; |
| 35 | case TYQ_FAIL_DIGEST: return "FAIL_DIGEST"; |
| 36 | case TYQ_FAIL_PROFILE: return "FAIL_PROFILE"; |
| 37 | case TYQ_FAIL_BASELINE: return "FAIL_BASELINE"; |
| 38 | case TYQ_E_ARG: return "E_ARG"; |
| 39 | case TYQ_E_IO: return "E_IO"; |
| 40 | default: return "UNKNOWN"; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | double tyq_lnsinhc(double a) |
| 45 | { |
| 46 | if (a < 1e-4) |
| 47 | return a * a / 6.0; |
| 48 | if (a < 1.0) |
| 49 | return log(sinh(a) / a); |
| 50 | return a - log(2.0 * a) + log1p(-exp(-2.0 * a)); |
| 51 | } |
| 52 | |
| 53 | int ty_qcont_bits(const tyq_pred *p, int64_t k, double *bits, int *floor_hit) |
| 54 | { |
| 55 | if (!p || !bits) |
| 56 | return TYQ_E_ARG; |
| 57 | if (fegetround() != FE_TONEAREST) |
| 58 | return TYQ_FAIL_PROTOCOL; |
| 59 | if (p->family != TYQ_FAM_GAUSS) |
| 60 | return TYQ_FAIL_PROTOCOL; |
| 61 | if (!isfinite(p->loc) || !isfinite(p->scale) || !(p->scale > 0.0)) |
| 62 | return TYQ_FAIL_PROTOCOL; |
| 63 | if (k >= TYQ_K_LIMIT || k <= -TYQ_K_LIMIT) |
| 64 | return TYQ_FAIL_PROTOCOL; |
| 65 | |
| 66 | double s = p->scale; |
| 67 | int hit = 0; |
| 68 | if (s < TYQ_SD_MIN) { |
| 69 | s = TYQ_SD_MIN; |
| 70 | hit = 1; |
| 71 | } |
| 72 | double zc = (((double)k + 0.5) * TYQ_DELTA - p->loc) / s; |
| 73 | double b = TYQ_DELTA / s; |
| 74 | if (!isfinite(zc) || !isfinite(b) || !(b > 0.0)) |
| 75 | return TYQ_FAIL_PROTOCOL; |
| 76 | double a = fabs(zc) * b / 2.0; |
| 77 | double nll = zc * zc / 2.0 + TYQ_HLN2PI - log(b) - tyq_lnsinhc(a); |
| 78 | double v = nll / TYQ_LN2; |
| 79 | if (!isfinite(v)) |
| 80 | return TYQ_FAIL_PROTOCOL; |
| 81 | *bits = v; |
| 82 | if (floor_hit) |
| 83 | *floor_hit = hit; |
| 84 | return TYQ_OK; |
| 85 | } |
| 86 | |
| 87 | int ty_qcont_point_ub(const tyq_pred *p, int64_t k, int64_t *ub, int *floor_hit) |
| 88 | { |
| 89 | double bits; |
| 90 | int hit = 0; |
| 91 | if (!ub) |
| 92 | return TYQ_E_ARG; |
| 93 | int rc = ty_qcont_bits(p, k, &bits, &hit); |
| 94 | if (rc != TYQ_OK) |
| 95 | return rc; |
| 96 | double scaled = 1e6 * bits; |
| 97 | if (!isfinite(scaled) || scaled > 9.0e18 || scaled < -9.0e18) |
| 98 | return TYQ_FAIL_PROTOCOL; |
| 99 | *ub = (int64_t)llrint(scaled); |
| 100 | if (floor_hit) |
| 101 | *floor_hit = hit; |
| 102 | return TYQ_OK; |
| 103 | } |
| 104 | |
| 105 | int ty_qcont_sum_ub(const tyq_pred *p, const int64_t *k, size_t n, |
| 106 | int64_t *ld_ub, uint64_t *floor_hits) |
| 107 | { |
| 108 | if (!ld_ub || !floor_hits || (n && (!p || !k))) |
| 109 | return TYQ_E_ARG; |
| 110 | int64_t sum = 0; |
| 111 | uint64_t hits = 0; |
| 112 | for (size_t i = 0; i < n; i++) { |
| 113 | int64_t ub; |
| 114 | int hit = 0; |
| 115 | int rc = ty_qcont_point_ub(&p[i], k[i], &ub, &hit); |
| 116 | if (rc != TYQ_OK) |
| 117 | return rc; |
| 118 | if (ty_add(sum, ub, &sum) != TY_OK) |
| 119 | return TYQ_FAIL_PROTOCOL; |
| 120 | hits += (uint64_t)hit; |
| 121 | } |
| 122 | *ld_ub = sum; |
| 123 | *floor_hits = hits; |
| 124 | return TYQ_OK; |
| 125 | } |
| 126 | |
| 127 | int ty_qcont_bytes_ub(uint32_t program_bytes, int64_t *bytes_ub) |
| 128 | { |
| 129 | if (!bytes_ub) |
| 130 | return TYQ_E_ARG; |
| 131 | *bytes_ub = INT64_C(8000000) * (int64_t)program_bytes; |
| 132 | return TYQ_OK; |
| 133 | } |
| 134 | |
| 135 | int ty_qcont_rider_ub(uint32_t k_params, uint32_t n_prefix_incr, int64_t *rider_ub) |
| 136 | { |
| 137 | if (!rider_ub) |
| 138 | return TYQ_E_ARG; |
| 139 | if (fegetround() != FE_TONEAREST) |
| 140 | return TYQ_FAIL_PROTOCOL; |
| 141 | if (k_params == 0) { |
| 142 | *rider_ub = 0; |
| 143 | return TYQ_OK; |
| 144 | } |
| 145 | if (n_prefix_incr == 0) |
| 146 | return TYQ_FAIL_PROTOCOL; |
| 147 | double v = 1e6 * ((double)k_params * 0.5 * log2((double)n_prefix_incr)); |
| 148 | if (!isfinite(v) || v > 9.0e18) |
| 149 | return TYQ_FAIL_PROTOCOL; |
| 150 | *rider_ub = (int64_t)llrint(v); |
| 151 | return TYQ_OK; |
| 152 | } |
| 153 | |
| 154 | int ty_qcont_floor_check(uint64_t floor_hits, uint64_t scored_points) |
| 155 | { |
| 156 | if (floor_hits > UINT64_MAX / 1000u) |
| 157 | return TYQ_FAIL_FLOOR; |
| 158 | return floor_hits * 1000u > scored_points ? TYQ_FAIL_FLOOR : TYQ_OK; |
| 159 | } |