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 / research / atemporal / at1 / model / at1_bn.c

463 lines · 17157 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/* Exact arithmetic for the AT-1 engine: arbitrary-precision integers (32-bit limbs, Knuth
18 * algorithm D for division), reduced rationals and Gaussian rationals. Used for validation
19 * (rules 3 and 4 of AT1_CASE_V1 section 4 are decided exactly for every in-limit case, as
20 * section 2 requires), for the exact nullspace of the literal H_total, and for the exact
21 * verdict comparisons of AT0_RESULT_V2 section 4. Nothing here ever wraps around. */
22#include "at1_model.h"
23
24#include <math.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29void *at1_xmalloc(size_t n)
30{
31 void *p = malloc(n ? n : 1);
32 if (!p) at1_fatal("RESOURCE_LIMIT");
33 return p;
34}
35void *at1_xcalloc(size_t n, size_t sz)
36{
37 void *p = calloc(n ? n : 1, sz ? sz : 1);
38 if (!p) at1_fatal("RESOURCE_LIMIT");
39 return p;
40}
41void *at1_xrealloc(void *p, size_t n)
42{
43 void *q = realloc(p, n ? n : 1);
44 if (!q) at1_fatal("RESOURCE_LIMIT");
45 return q;
46}
47
48/* ---- integers ------------------------------------------------------------------------------ */
49void bn_init(bn *a) { a->neg = 0; a->n = 0; a->cap = 0; a->d = NULL; }
50void bn_free(bn *a) { free(a->d); bn_init(a); }
51
52static void bn_reserve(bn *a, int n)
53{
54 if (a->cap >= n) return;
55 int cap = a->cap ? a->cap : 4;
56 while (cap < n) cap *= 2;
57 a->d = at1_xrealloc(a->d, (size_t)cap * sizeof(uint32_t));
58 a->cap = cap;
59}
60static void bn_trim(bn *a)
61{
62 while (a->n > 0 && a->d[a->n - 1] == 0) a->n--;
63 if (a->n == 0) a->neg = 0;
64}
65static void bn_swap(bn *a, bn *b) { bn t = *a; *a = *b; *b = t; }
66
67void bn_copy(bn *r, const bn *a)
68{
69 if (r == a) return;
70 bn_reserve(r, a->n);
71 if (a->n) memcpy(r->d, a->d, (size_t)a->n * sizeof(uint32_t));
72 r->n = a->n; r->neg = a->neg;
73}
74void bn_set_u64(bn *r, uint64_t v)
75{
76 bn_reserve(r, 2);
77 r->d[0] = (uint32_t)v; r->d[1] = (uint32_t)(v >> 32); r->n = 2; r->neg = 0;
78 bn_trim(r);
79}
80void bn_set_i64(bn *r, int64_t v)
81{
82 uint64_t m = v < 0 ? (uint64_t)0 - (uint64_t)v : (uint64_t)v;
83 bn_set_u64(r, m);
84 r->neg = v < 0 && m != 0;
85}
86int bn_is_zero(const bn *a) { return a->n == 0; }
87int bn_sign(const bn *a) { return a->n == 0 ? 0 : a->neg ? -1 : 1; }
88
89static int mag_cmp(const uint32_t *a, int an, const uint32_t *b, int bn_)
90{
91 if (an != bn_) return an < bn_ ? -1 : 1;
92 for (int i = an - 1; i >= 0; i--) if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
93 return 0;
94}
95int bn_cmp_abs(const bn *a, const bn *b) { return mag_cmp(a->d, a->n, b->d, b->n); }
96int bn_cmp(const bn *a, const bn *b)
97{
98 int sa = bn_sign(a), sb = bn_sign(b);
99 if (sa != sb) return sa < sb ? -1 : 1;
100 int c = bn_cmp_abs(a, b);
101 return sa < 0 ? -c : c;
102}
103
104/* r = |a| + |b| (magnitudes), sign set by caller */
105static void mag_add(bn *r, const bn *a, const bn *b)
106{
107 bn t; bn_init(&t);
108 int n = (a->n > b->n ? a->n : b->n) + 1;
109 bn_reserve(&t, n);
110 uint64_t c = 0;
111 for (int i = 0; i < n; i++) {
112 c += (i < a->n ? a->d[i] : 0u);
113 c += (i < b->n ? b->d[i] : 0u);
114 t.d[i] = (uint32_t)c; c >>= 32;
115 }
116 t.n = n; t.neg = 0; bn_trim(&t);
117 bn_swap(r, &t); bn_free(&t);
118}
119/* r = |a| - |b|, requires |a| >= |b| */
120static void mag_sub(bn *r, const bn *a, const bn *b)
121{
122 bn t; bn_init(&t);
123 bn_reserve(&t, a->n);
124 int64_t br = 0;
125 for (int i = 0; i < a->n; i++) {
126 int64_t x = (int64_t)a->d[i] - (i < b->n ? (int64_t)b->d[i] : 0) - br;
127 br = x < 0;
128 if (x < 0) x += (int64_t)1 << 32;
129 t.d[i] = (uint32_t)x;
130 }
131 t.n = a->n; t.neg = 0; bn_trim(&t);
132 bn_swap(r, &t); bn_free(&t);
133}
134void bn_add(bn *r, const bn *a, const bn *b)
135{
136 int an = a->neg, bneg = b->neg;
137 if (an == bneg) { mag_add(r, a, b); r->neg = an && r->n; return; }
138 int c = bn_cmp_abs(a, b);
139 if (c == 0) { r->n = 0; r->neg = 0; return; }
140 if (c > 0) { mag_sub(r, a, b); r->neg = an && r->n; }
141 else { mag_sub(r, b, a); r->neg = bneg && r->n; }
142}
143void bn_neg(bn *r, const bn *a) { bn_copy(r, a); if (r->n) r->neg = !r->neg; }
144void bn_abs(bn *r, const bn *a) { bn_copy(r, a); r->neg = 0; }
145void bn_sub(bn *r, const bn *a, const bn *b)
146{
147 bn nb; bn_init(&nb); bn_neg(&nb, b);
148 bn_add(r, a, &nb);
149 bn_free(&nb);
150}
151void bn_mul(bn *r, const bn *a, const bn *b)
152{
153 if (a->n == 0 || b->n == 0) { r->n = 0; r->neg = 0; return; }
154 bn t; bn_init(&t);
155 int n = a->n + b->n;
156 bn_reserve(&t, n);
157 memset(t.d, 0, (size_t)n * sizeof(uint32_t));
158 for (int i = 0; i < a->n; i++) {
159 uint64_t c = 0, ai = a->d[i];
160 for (int j = 0; j < b->n; j++) {
161 c += ai * b->d[j] + t.d[i + j];
162 t.d[i + j] = (uint32_t)c; c >>= 32;
163 }
164 t.d[i + b->n] = (uint32_t)c;
165 }
166 t.n = n; t.neg = a->neg != b->neg; bn_trim(&t);
167 bn_swap(r, &t); bn_free(&t);
168}
169
170static int clz32(uint32_t x) { int n = 0; if (!x) return 32; while (!(x & 0x80000000u)) { x <<= 1; n++; } return n; }
171
172/* Knuth TAOCP vol. 2, 4.3.1 algorithm D, on magnitudes; truncated signs applied after */
173void bn_divmod(bn *q, bn *rem, const bn *a, const bn *b)
174{
175 if (b->n == 0) at1_fatal("INTERNAL_ERROR division by zero");
176 int qneg = a->neg != b->neg, rneg = a->neg;
177 if (bn_cmp_abs(a, b) < 0) {
178 if (rem) { bn_copy(rem, a); }
179 if (q) { q->n = 0; q->neg = 0; }
180 return;
181 }
182 bn Q, R; bn_init(&Q); bn_init(&R);
183 if (b->n == 1) {
184 uint64_t d = b->d[0], r = 0;
185 bn_reserve(&Q, a->n);
186 for (int i = a->n - 1; i >= 0; i--) {
187 uint64_t cur = (r << 32) | a->d[i];
188 Q.d[i] = (uint32_t)(cur / d); r = cur % d;
189 }
190 Q.n = a->n; bn_trim(&Q);
191 bn_set_u64(&R, r);
192 } else {
193 int s = clz32(b->d[b->n - 1]);
194 int n = b->n, m = a->n - b->n;
195 uint32_t *v = at1_xmalloc((size_t)n * sizeof(uint32_t));
196 uint32_t *u = at1_xmalloc((size_t)(a->n + 1) * sizeof(uint32_t));
197 for (int i = n - 1; i > 0; i--) v[i] = (b->d[i] << s) | (s ? (uint32_t)((uint64_t)b->d[i - 1] >> (32 - s)) : 0);
198 v[0] = b->d[0] << s;
199 u[a->n] = s ? (uint32_t)((uint64_t)a->d[a->n - 1] >> (32 - s)) : 0;
200 for (int i = a->n - 1; i > 0; i--) u[i] = (a->d[i] << s) | (s ? (uint32_t)((uint64_t)a->d[i - 1] >> (32 - s)) : 0);
201 u[0] = a->d[0] << s;
202 bn_reserve(&Q, m + 1);
203 for (int j = m; j >= 0; j--) {
204 uint64_t num = ((uint64_t)u[j + n] << 32) | u[j + n - 1];
205 uint64_t qhat = num / v[n - 1], rhat = num % v[n - 1];
206 while (qhat >= ((uint64_t)1 << 32) || qhat * v[n - 2] > ((rhat << 32) | u[j + n - 2])) {
207 qhat--; rhat += v[n - 1];
208 if (rhat >= ((uint64_t)1 << 32)) break;
209 }
210 int64_t borrow = 0; uint64_t carry = 0;
211 for (int i = 0; i < n; i++) {
212 uint64_t p = qhat * v[i] + carry;
213 carry = p >> 32;
214 int64_t t = (int64_t)u[i + j] - (int64_t)(uint32_t)p - borrow;
215 borrow = t < 0;
216 u[i + j] = (uint32_t)(t + (borrow ? ((int64_t)1 << 32) : 0));
217 }
218 int64_t t = (int64_t)u[j + n] - (int64_t)carry - borrow;
219 borrow = t < 0;
220 u[j + n] = (uint32_t)(t + (borrow ? ((int64_t)1 << 32) : 0));
221 if (borrow) {
222 qhat--;
223 uint64_t c = 0;
224 for (int i = 0; i < n; i++) {
225 c += (uint64_t)u[i + j] + v[i];
226 u[i + j] = (uint32_t)c; c >>= 32;
227 }
228 u[j + n] = (uint32_t)((uint64_t)u[j + n] + c);
229 }
230 Q.d[j] = (uint32_t)qhat;
231 }
232 Q.n = m + 1; bn_trim(&Q);
233 bn_reserve(&R, n);
234 for (int i = 0; i < n; i++) R.d[i] = (u[i] >> s) | (s ? (uint32_t)((uint64_t)u[i + 1] << (32 - s)) : 0);
235 R.n = n; bn_trim(&R);
236 free(u); free(v);
237 }
238 Q.neg = qneg && Q.n; R.neg = rneg && R.n;
239 if (q) bn_swap(q, &Q);
240 if (rem) bn_swap(rem, &R);
241 bn_free(&Q); bn_free(&R);
242}
243
244void bn_gcd(bn *r, const bn *a, const bn *b)
245{
246 bn x, y, t; bn_init(&x); bn_init(&y); bn_init(&t);
247 bn_abs(&x, a); bn_abs(&y, b);
248 while (y.n) { bn_divmod(NULL, &t, &x, &y); bn_swap(&x, &y); bn_swap(&y, &t); }
249 bn_swap(r, &x);
250 bn_free(&x); bn_free(&y); bn_free(&t);
251}
252
253void bn_shl(bn *r, const bn *a, unsigned bits)
254{
255 if (a->n == 0) { r->n = 0; r->neg = 0; return; }
256 unsigned ws = bits / 32, bs = bits % 32;
257 bn t; bn_init(&t);
258 int n = a->n + (int)ws + 1;
259 bn_reserve(&t, n);
260 memset(t.d, 0, (size_t)n * sizeof(uint32_t));
261 for (int i = 0; i < a->n; i++) {
262 uint64_t x = (uint64_t)a->d[i] << bs;
263 t.d[i + (int)ws] |= (uint32_t)x;
264 t.d[i + (int)ws + 1] |= (uint32_t)(x >> 32);
265 }
266 t.n = n; t.neg = a->neg; bn_trim(&t);
267 bn_swap(r, &t); bn_free(&t);
268}
269
270void bn_pow10(bn *r, unsigned k)
271{
272 bn ten; bn_init(&ten); bn_set_u64(&ten, 10);
273 bn_set_u64(r, 1);
274 for (unsigned i = 0; i < k; i++) bn_mul(r, r, &ten);
275 bn_free(&ten);
276}
277
278int bn_bitlen(const bn *a)
279{
280 if (a->n == 0) return 0;
281 return 32 * (a->n - 1) + (32 - clz32(a->d[a->n - 1]));
282}
283
284int bn_isqrt_exact(const bn *a, bn *root)
285{
286 if (a->neg) return 0;
287 if (a->n == 0) { root->n = 0; root->neg = 0; return 1; }
288 /* Newton from above: x0 = 2^ceil(bits/2) >= sqrt(a) */
289 bn x, y, t, one, two; bn_init(&x); bn_init(&y); bn_init(&t); bn_init(&one); bn_init(&two);
290 bn_set_u64(&one, 1); bn_set_u64(&two, 2);
291 bn_shl(&x, &one, (unsigned)(bn_bitlen(a) + 1) / 2 + 1);
292 for (;;) {
293 bn_divmod(&t, NULL, a, &x);
294 bn_add(&y, &x, &t);
295 bn_divmod(&y, NULL, &y, &two);
296 if (bn_cmp(&y, &x) >= 0) break;
297 bn_swap(&x, &y);
298 }
299 bn_mul(&t, &x, &x);
300 int sq = bn_cmp(&t, a) == 0;
301 if (sq) bn_copy(root, &x);
302 bn_free(&x); bn_free(&y); bn_free(&t); bn_free(&one); bn_free(&two);
303 return sq;
304}
305
306int bn_fits_i64(const bn *a, int64_t *out)
307{
308 if (a->n > 2) return 0;
309 uint64_t m = a->n == 0 ? 0 : a->n == 1 ? a->d[0] : ((uint64_t)a->d[1] << 32) | a->d[0];
310 if (m > (uint64_t)INT64_MAX) return 0;
311 *out = a->neg ? -(int64_t)m : (int64_t)m;
312 return 1;
313}
314
315int bn_from_digits(bn *r, const char *s, size_t len)
316{
317 if (len > AT1_TOKEN_DIGITS_MAX) return 0;
318 bn t, chunk, scale; bn_init(&t); bn_init(&chunk); bn_init(&scale);
319 size_t i = 0;
320 while (i < len) {
321 size_t take = len - i < 9 ? len - i : 9;
322 uint64_t v = 0, sc = 1;
323 for (size_t j = 0; j < take; j++) { v = v * 10 + (uint64_t)(s[i + j] - '0'); sc *= 10; }
324 bn_set_u64(&scale, sc); bn_set_u64(&chunk, v);
325 bn_mul(&t, &t, &scale); bn_add(&t, &t, &chunk);
326 i += take;
327 }
328 bn_swap(r, &t);
329 bn_free(&t); bn_free(&chunk); bn_free(&scale);
330 return 1;
331}
332
333/* ---- rationals ------------------------------------------------------------------------------ */
334void bq_init(bq *a) { bn_init(&a->num); bn_init(&a->den); bn_set_u64(&a->den, 1); }
335void bq_free(bq *a) { bn_free(&a->num); bn_free(&a->den); }
336void bq_copy(bq *r, const bq *a) { bn_copy(&r->num, &a->num); bn_copy(&r->den, &a->den); }
337
338static void bq_reduce(bq *r)
339{
340 if (r->den.neg) { bn_neg(&r->den, &r->den); bn_neg(&r->num, &r->num); }
341 if (r->num.n == 0) { bn_set_u64(&r->den, 1); return; }
342 bn g; bn_init(&g);
343 bn_gcd(&g, &r->num, &r->den);
344 if (!(g.n == 1 && g.d[0] == 1)) {
345 bn_divmod(&r->num, NULL, &r->num, &g);
346 bn_divmod(&r->den, NULL, &r->den, &g);
347 }
348 bn_free(&g);
349}
350void bq_set_bn(bq *r, const bn *n, const bn *d) { bn_copy(&r->num, n); bn_copy(&r->den, d); bq_reduce(r); }
351void bq_set_i64(bq *r, int64_t n, int64_t d) { bn_set_i64(&r->num, n); bn_set_i64(&r->den, d); bq_reduce(r); }
352int bq_is_zero(const bq *a) { return a->num.n == 0; }
353int bq_sign(const bq *a) { return bn_sign(&a->num); }
354int bq_cmp(const bq *a, const bq *b)
355{
356 bn x, y; bn_init(&x); bn_init(&y);
357 bn_mul(&x, &a->num, &b->den); bn_mul(&y, &b->num, &a->den);
358 int c = bn_cmp(&x, &y);
359 bn_free(&x); bn_free(&y);
360 return c;
361}
362void bq_add(bq *r, const bq *a, const bq *b)
363{
364 bn x, y, d; bn_init(&x); bn_init(&y); bn_init(&d);
365 bn_mul(&x, &a->num, &b->den); bn_mul(&y, &b->num, &a->den); bn_mul(&d, &a->den, &b->den);
366 bn_add(&r->num, &x, &y); bn_swap(&r->den, &d);
367 bq_reduce(r);
368 bn_free(&x); bn_free(&y); bn_free(&d);
369}
370void bq_neg(bq *r, const bq *a) { bq_copy(r, a); bn_neg(&r->num, &r->num); }
371void bq_abs(bq *r, const bq *a) { bq_copy(r, a); r->num.neg = 0; }
372void bq_sub(bq *r, const bq *a, const bq *b)
373{
374 bq nb; bq_init(&nb); bq_neg(&nb, b); bq_add(r, a, &nb); bq_free(&nb);
375}
376void bq_mul(bq *r, const bq *a, const bq *b)
377{
378 bn x, d; bn_init(&x); bn_init(&d);
379 bn_mul(&x, &a->num, &b->num); bn_mul(&d, &a->den, &b->den);
380 bn_swap(&r->num, &x); bn_swap(&r->den, &d);
381 bq_reduce(r);
382 bn_free(&x); bn_free(&d);
383}
384void bq_div(bq *r, const bq *a, const bq *b)
385{
386 bn x, d; bn_init(&x); bn_init(&d);
387 bn_mul(&x, &a->num, &b->den); bn_mul(&d, &a->den, &b->num);
388 bn_swap(&r->num, &x); bn_swap(&r->den, &d);
389 bq_reduce(r);
390 bn_free(&x); bn_free(&d);
391}
392
393double bq_to_double(const bq *a)
394{
395 if (a->num.n == 0) return 0.0;
396 /* q = floor(|num| 2^s / den) with 65..66 significant bits, sticky bit for the remainder,
397 * then one correctly rounded uint64 -> double conversion and an exact scaling */
398 bn n, d, q, rem; bn_init(&n); bn_init(&d); bn_init(&q); bn_init(&rem);
399 bn_abs(&n, &a->num); bn_copy(&d, &a->den);
400 int s = 66 + bn_bitlen(&d) - bn_bitlen(&n);
401 if (s >= 0) bn_shl(&n, &n, (unsigned)s); else bn_shl(&d, &d, (unsigned)(-s));
402 bn_divmod(&q, &rem, &n, &d);
403 int qb = bn_bitlen(&q), drop = qb - 63; /* keep 63 bits, fold the rest into sticky */
404 uint64_t top = 0; int sticky = rem.n != 0;
405 for (int i = qb - 1; i >= 0; i--) {
406 int bit = (int)((q.d[i / 32] >> (i % 32)) & 1u);
407 if (i >= drop) top = (top << 1) | (uint64_t)bit; else if (bit) sticky = 1;
408 }
409 top = (top << 1) | (uint64_t)sticky; /* 64 bits: 63 significant + sticky */
410 double r = ldexp((double)top, drop - 1 - s);
411 bn_free(&n); bn_free(&d); bn_free(&q); bn_free(&rem);
412 return a->num.neg ? -r : r;
413}
414
415void bq_from_double(bq *r, double x)
416{
417 uint64_t bits; memcpy(&bits, &x, sizeof bits);
418 int neg = (int)(bits >> 63);
419 int e = (int)((bits >> 52) & 0x7ff);
420 uint64_t m = bits & (((uint64_t)1 << 52) - 1);
421 if (e == 0) e = 1; else m |= (uint64_t)1 << 52;
422 int p = e - 1075; /* x = m * 2^p */
423 bn one; bn_init(&one);
424 bn_set_u64(&r->num, m); bn_set_u64(&r->den, 1); bn_set_u64(&one, 1);
425 if (p >= 0) bn_shl(&r->num, &r->num, (unsigned)p); else bn_shl(&r->den, &one, (unsigned)(-p));
426 if (neg && r->num.n) r->num.neg = 1;
427 bq_reduce(r);
428 bn_free(&one);
429}
430
431void bq_from_scaled(bq *r, const bn *n, unsigned k)
432{
433 bn d; bn_init(&d); bn_pow10(&d, k);
434 bq_set_bn(r, n, &d);
435 bn_free(&d);
436}
437
438/* ---- Gaussian rationals -------------------------------------------------------------------- */
439void gq_init(gq *a) { bq_init(&a->re); bq_init(&a->im); }
440void gq_free(gq *a) { bq_free(&a->re); bq_free(&a->im); }
441void gq_copy(gq *r, const gq *a) { bq_copy(&r->re, &a->re); bq_copy(&r->im, &a->im); }
442int gq_is_zero(const gq *a) { return bq_is_zero(&a->re) && bq_is_zero(&a->im); }
443void gq_add(gq *r, const gq *a, const gq *b) { bq_add(&r->re, &a->re, &b->re); bq_add(&r->im, &a->im, &b->im); }
444void gq_sub(gq *r, const gq *a, const gq *b) { bq_sub(&r->re, &a->re, &b->re); bq_sub(&r->im, &a->im, &b->im); }
445void gq_conj(gq *r, const gq *a) { bq_copy(&r->re, &a->re); bq_neg(&r->im, &a->im); }
446void gq_neg(gq *r, const gq *a) { bq_neg(&r->re, &a->re); bq_neg(&r->im, &a->im); }
447void gq_mul(gq *r, const gq *a, const gq *b)
448{
449 bq t1, t2, re, im; bq_init(&t1); bq_init(&t2); bq_init(&re); bq_init(&im);
450 bq_mul(&t1, &a->re, &b->re); bq_mul(&t2, &a->im, &b->im); bq_sub(&re, &t1, &t2);
451 bq_mul(&t1, &a->re, &b->im); bq_mul(&t2, &a->im, &b->re); bq_add(&im, &t1, &t2);
452 bq_copy(&r->re, &re); bq_copy(&r->im, &im);
453 bq_free(&t1); bq_free(&t2); bq_free(&re); bq_free(&im);
454}
455void gq_div(gq *r, const gq *a, const gq *b)
456{
457 gq cb, num; bq n2, t; gq_init(&cb); gq_init(&num); bq_init(&n2); bq_init(&t);
458 gq_conj(&cb, b);
459 gq_mul(&num, a, &cb);
460 bq_mul(&n2, &b->re, &b->re); bq_mul(&t, &b->im, &b->im); bq_add(&n2, &n2, &t);
461 bq_div(&r->re, &num.re, &n2); bq_div(&r->im, &num.im, &n2);
462 gq_free(&cb); gq_free(&num); bq_free(&n2); bq_free(&t);
463}