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 / at0 / evaluator / src / at0e_big.c

271 lines · 11045 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-0 evaluator: int128 rationals, scaled decimals,
18 * 4096-bit integers, and the two tri-state comparison forms of AT0_RESULT_V1
19 * section 4. Nothing here touches floating point except the bit-pattern
20 * decoder sbig_from_f64_bits, which only reads integer fields of the pattern. */
21#include "at0e.h"
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26static void die(const char *m) { fprintf(stderr, "at0e: internal: %s\n", m); abort(); }
27
28/* ---------------- rationals ---------------- */
29static i128 iabs(i128 a) { return a < 0 ? -a : a; }
30static i128 igcd(i128 a, i128 b) { a = iabs(a); b = iabs(b); while (b) { i128 t = a % b; a = b; b = t; } return a; }
31
32rat rat_make(i128 n, i128 d) {
33 rat r;
34 if (d == 0) die("rational with zero denominator");
35 if (d < 0) { n = -n; d = -d; }
36 i128 g = igcd(n, d);
37 if (g == 0) g = 1;
38 r.n = n / g; r.d = d / g;
39 return r;
40}
41rat rat_add(rat a, rat b) { return rat_make(a.n * b.d + b.n * a.d, a.d * b.d); }
42rat rat_sub(rat a, rat b) { return rat_make(a.n * b.d - b.n * a.d, a.d * b.d); }
43rat rat_mul(rat a, rat b) { return rat_make(a.n * b.n, a.d * b.d); }
44rat rat_neg(rat a) { return rat_make(-a.n, a.d); }
45int rat_cmp(rat a, rat b) { i128 l = a.n * b.d, r = b.n * a.d; return (l > r) - (l < r); }
46int rat_is_zero(rat a) { return a.n == 0; }
47int rat_is_int(rat a) { return a.d == 1; }
48rat rat_mod1(rat a) {
49 i128 q = a.n / a.d; /* truncates toward zero */
50 i128 rem = a.n - q * a.d;
51 if (rem < 0) rem += a.d;
52 return rat_make(rem, a.d);
53}
54static u128 isqrt_u128(u128 v) {
55 if (v < 2) return v;
56 u128 lo = 1, hi = (u128)1 << 64; /* v < 2^128 so sqrt < 2^64 */
57 while (lo < hi) {
58 u128 mid = lo + (hi - lo + 1) / 2;
59 if (mid <= v / mid) lo = mid; else hi = mid - 1;
60 }
61 return lo;
62}
63int rat_sqrt(rat a, rat *out) {
64 if (a.n < 0) return 0;
65 u128 n = (u128)a.n, d = (u128)a.d;
66 u128 sn = isqrt_u128(n), sd = isqrt_u128(d);
67 if (sn * sn != n || sd * sd != d) return 0;
68 *out = rat_make((i128)sn, (i128)sd);
69 return 1;
70}
71
72/* Canonical integer text: optional '-', no leading zeros, no '+', "-0" invalid. */
73static int parse_int_text(const char *s, size_t len, i128 *out, int *canonical) {
74 size_t i = 0; int neg = 0;
75 if (len == 0) return 0;
76 if (s[0] == '-') { neg = 1; i = 1; if (len == 1) return 0; }
77 if (s[i] == '+') return 0;
78 i128 v = 0;
79 for (size_t j = i; j < len; j++) {
80 if (s[j] < '0' || s[j] > '9') return 0;
81 if (j - i >= 20) { v = (i128)1 << 100; if (*canonical == 1) *canonical = -1; continue; } /* beyond any contract limit: saturate (no overflow) and flag; rat_parse refuses it as CASE_INVALID_PARAMETER */
82 v = v * 10 + (s[j] - '0');
83 }
84 if (len - i > 1 && s[i] == '0') *canonical = 0; /* leading zero */
85 if (neg && v == 0) *canonical = 0; /* -0 */
86 *out = neg ? -v : v;
87 return 1;
88}
89int parse_int_canonical(const char *s, long *out) {
90 i128 v; int canon = 1;
91 if (!parse_int_text(s, strlen(s), &v, &canon) || !canon) return 0;
92 if (v > 1000000000000L || v < -1000000000000L) return 0;
93 *out = (long)v;
94 return 1;
95}
96int rat_parse(const char *tok, rat *out, int *canonical) {
97 const char *slash = strchr(tok, '/');
98 i128 n, d; int canon = 1;
99 if (!slash) return 0;
100 if (!parse_int_text(tok, (size_t)(slash - tok), &n, &canon)) return 0;
101 if (!parse_int_text(slash + 1, strlen(slash + 1), &d, &canon)) return 0;
102 if (d < 1) return 0; /* "d >= 1" is shape */
103 int huge = canon == -1; /* a part saturated in parse_int_text: beyond any limit, reduction unknowable */
104 if (!huge) {
105 if (igcd(n, d) != 1) canon = 0;
106 if (n == 0 && d != 1) canon = 0;
107 }
108 if (canon != 0 && (huge || iabs(n) > AT0E_RAT_LIMIT || d > AT0E_RAT_LIMIT)) canon = -1; /* beyond the section 2 limits: INVALID_PARAMETER (ambiguity A2); encoding errors still win */
109 *canonical = canon;
110 *out = rat_make(n, d);
111 return 1;
112}
113void rat_format(rat a, char *buf, size_t cap) {
114 long long n = (long long)a.n, d = (long long)a.d;
115 snprintf(buf, cap, "%lld/%lld", n, d);
116}
117int crat_parse(const char *tok, crat *out, int *canonical) {
118 size_t len = strlen(tok);
119 if (len < 5 || tok[0] != '(' || tok[len - 1] != ')') return 0;
120 const char *semi = strchr(tok, ';');
121 if (!semi) return 0;
122 char re[64], im[64];
123 size_t rl = (size_t)(semi - tok - 1), il = len - 2 - rl - 1;
124 if (rl == 0 || rl >= sizeof re || il == 0 || il >= sizeof im) return 0;
125 memcpy(re, tok + 1, rl); re[rl] = 0;
126 memcpy(im, semi + 1, il); im[il] = 0;
127 int c1 = 1, c2 = 1;
128 if (!rat_parse(re, &out->re, &c1) || !rat_parse(im, &out->im, &c2)) return 0;
129 *canonical = (c1 < 0 || c2 < 0) ? -1 : (c1 && c2);
130 return 1;
131}
132int crat_is_zero(crat z) { return rat_is_zero(z.re) && rat_is_zero(z.im); }
133
134/* ---------------- scaled decimals ---------------- */
135int scaled_parse(const char *tok, scaled *out, int *canonical) {
136 const char *at = strchr(tok, '@');
137 if (!at) return 0;
138 size_t nl = (size_t)(at - tok);
139 if (nl == 0 || nl > 38) return 0;
140 u128 n = 0;
141 for (size_t i = 0; i < nl; i++) { if (tok[i] < '0' || tok[i] > '9') return 0; n = n * 10 + (u128)(tok[i] - '0'); }
142 int canon = 1;
143 if (nl > 1 && tok[0] == '0') canon = 0;
144 const char *ks = at + 1; size_t kl = strlen(ks);
145 if (kl == 0 || kl > 2) return 0;
146 int k = 0;
147 for (size_t i = 0; i < kl; i++) { if (ks[i] < '0' || ks[i] > '9') return 0; k = k * 10 + (ks[i] - '0'); }
148 if (kl == 2 && ks[0] == '0') canon = 0;
149 if (k > 40) return 0;
150 if (n == 0 && k != 0) canon = 0;
151 if (n != 0 && k != 0 && n % 10 == 0) canon = 0;
152 *canonical = canon;
153 out->n = n; out->k = k;
154 return 1;
155}
156
157/* ---------------- big integers ---------------- */
158void big_zero(big *a) { memset(a, 0, sizeof *a); }
159void big_from_u128(big *a, u128 v) { big_zero(a); for (int i = 0; i < 4; i++) { a->l[i] = (uint32_t)v; v >>= 32; } }
160int big_is_zero(const big *a) { for (int i = 0; i < BIG_LIMBS; i++) if (a->l[i]) return 0; return 1; }
161int big_cmp(const big *a, const big *b) {
162 for (int i = BIG_LIMBS - 1; i >= 0; i--) { if (a->l[i] != b->l[i]) return a->l[i] > b->l[i] ? 1 : -1; }
163 return 0;
164}
165void big_add(big *r, const big *a, const big *b) {
166 uint64_t carry = 0;
167 for (int i = 0; i < BIG_LIMBS; i++) { uint64_t s = (uint64_t)a->l[i] + b->l[i] + carry; r->l[i] = (uint32_t)s; carry = s >> 32; }
168 if (carry) die("big_add overflow");
169}
170void big_sub(big *r, const big *a, const big *b) {
171 int64_t borrow = 0;
172 for (int i = 0; i < BIG_LIMBS; i++) {
173 int64_t s = (int64_t)a->l[i] - b->l[i] - borrow;
174 if (s < 0) { s += (int64_t)1 << 32; borrow = 1; } else borrow = 0;
175 r->l[i] = (uint32_t)s;
176 }
177 if (borrow) die("big_sub underflow");
178}
179void big_shl(big *a, unsigned bits) {
180 unsigned limbs = bits / 32, rem = bits % 32;
181 if (limbs >= BIG_LIMBS) { if (!big_is_zero(a)) die("big_shl overflow"); return; }
182 for (unsigned i = BIG_LIMBS - limbs; i < BIG_LIMBS; i++) if (a->l[i]) die("big_shl overflow");
183 if (limbs) {
184 for (int i = BIG_LIMBS - 1; i >= (int)limbs; i--) a->l[i] = a->l[i - limbs];
185 for (unsigned i = 0; i < limbs; i++) a->l[i] = 0;
186 }
187 if (rem) {
188 uint32_t carry = 0;
189 for (int i = 0; i < BIG_LIMBS; i++) { uint64_t v = ((uint64_t)a->l[i] << rem) | carry; a->l[i] = (uint32_t)v; carry = (uint32_t)(v >> 32); }
190 if (carry) die("big_shl overflow");
191 }
192}
193void big_mul_u32(big *a, uint32_t m) {
194 uint64_t carry = 0;
195 for (int i = 0; i < BIG_LIMBS; i++) { uint64_t v = (uint64_t)a->l[i] * m + carry; a->l[i] = (uint32_t)v; carry = v >> 32; }
196 if (carry) die("big_mul overflow");
197}
198void big_mul_pow10(big *a, unsigned k) { while (k >= 9) { big_mul_u32(a, 1000000000u); k -= 9; } while (k--) big_mul_u32(a, 10u); }
199
200void sbig_zero(sbig *a) { a->neg = 0; big_zero(&a->m); }
201void sbig_neg(sbig *a) { if (!big_is_zero(&a->m)) a->neg = !a->neg; }
202void sbig_abs(sbig *a) { a->neg = 0; }
203void sbig_add(sbig *r, const sbig *a, const sbig *b) {
204 sbig t;
205 if (a->neg == b->neg) { big_add(&t.m, &a->m, &b->m); t.neg = a->neg; }
206 else {
207 int c = big_cmp(&a->m, &b->m);
208 if (c == 0) { sbig_zero(&t); }
209 else if (c > 0) { big_sub(&t.m, &a->m, &b->m); t.neg = a->neg; }
210 else { big_sub(&t.m, &b->m, &a->m); t.neg = b->neg; }
211 }
212 if (big_is_zero(&t.m)) t.neg = 0;
213 *r = t;
214}
215int sbig_cmp(const sbig *a, const sbig *b) {
216 if (a->neg != b->neg) return a->neg ? -1 : 1;
217 int c = big_cmp(&a->m, &b->m);
218 return a->neg ? -c : c;
219}
220
221/* Q = v * 2^1100 * 10^80 for a finite binary64 v = (-1)^s * m * 2^e. */
222void sbig_from_f64_bits(sbig *q, uint64_t bits) {
223 int sign = (int)(bits >> 63);
224 int expo = (int)((bits >> 52) & 0x7ff);
225 uint64_t frac = bits & 0xfffffffffffffULL;
226 uint64_t m; int e;
227 if (expo == 0x7ff) die("nonfinite bit pattern reached exact arithmetic");
228 if (expo == 0) { m = frac; e = -1074; }
229 else { m = frac | (1ULL << 52); e = expo - 1075; }
230 sbig_zero(q);
231 if (m == 0) return;
232 big_from_u128(&q->m, m);
233 big_shl(&q->m, (unsigned)(e + (int)AT0E_DEN_P2));
234 big_mul_pow10(&q->m, AT0E_DEN_P10);
235 q->neg = sign;
236}
237void sbig_from_scaled(sbig *q, scaled s) {
238 sbig_zero(q);
239 if (s.n == 0) return;
240 big_from_u128(&q->m, s.n);
241 big_mul_pow10(&q->m, AT0E_DEN_P10 - (unsigned)s.k);
242 big_shl(&q->m, AT0E_DEN_P2);
243}
244void sbig_from_int(sbig *q, long v) {
245 sbig_zero(q);
246 if (v == 0) return;
247 big_from_u128(&q->m, (u128)(v < 0 ? -v : v));
248 big_mul_pow10(&q->m, AT0E_DEN_P10);
249 big_shl(&q->m, AT0E_DEN_P2);
250 q->neg = v < 0;
251}
252
253tri tri_absolute(const sbig *v, const sbig *b, const sbig *tol) {
254 sbig av = *v, hi, lo, nb = *b;
255 sbig_abs(&av);
256 sbig_add(&hi, &av, b);
257 if (sbig_cmp(&hi, tol) <= 0) return CMP_PASS;
258 sbig_neg(&nb);
259 sbig_add(&lo, &av, &nb);
260 if (sbig_cmp(&lo, tol) > 0) return CMP_FAIL;
261 return CMP_INDET;
262}
263tri tri_signed(const sbig *v, const sbig *b, const sbig *tol) {
264 sbig hi, lo, nb = *b;
265 sbig_add(&hi, v, b);
266 if (sbig_cmp(&hi, tol) <= 0) return CMP_PASS;
267 sbig_neg(&nb);
268 sbig_add(&lo, v, &nb);
269 if (sbig_cmp(&lo, tol) > 0) return CMP_FAIL;
270 return CMP_INDET;
271}