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 / algebra / realize_common.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 | /* Omega mixed algebra MA-2: shared realization helpers and registry. */ |
| 18 | #include "algebra/realize_common.h" |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | static const oma_rz_impl *const k_impls[] = { |
| 24 | &oma_rz_r1_plain, &oma_rz_r1_sdot, &oma_rz_r1_sdot_il, &oma_rz_r1_smmla, |
| 25 | &oma_rz_r2_bitplane, &oma_rz_r2b_lut, &oma_rz_r2c_crumb, |
| 26 | &oma_rz_r3_sparse, |
| 27 | &oma_rz_r4_rns, |
| 28 | &oma_rz_r5_dense5, |
| 29 | }; |
| 30 | |
| 31 | size_t oma_rz_count(void) { return sizeof k_impls / sizeof k_impls[0]; } |
| 32 | |
| 33 | const oma_rz_impl *oma_rz_get(size_t i) { return i < oma_rz_count() ? k_impls[i] : NULL; } |
| 34 | |
| 35 | const oma_rz_impl *oma_rz_find(const char *id) { |
| 36 | if (!id) return NULL; |
| 37 | for (size_t i = 0; i < oma_rz_count(); i++) |
| 38 | if (strcmp(k_impls[i]->id, id) == 0) return k_impls[i]; |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | void oma_rz_free(oma_rz_plan *p) { |
| 43 | if (!p) return; |
| 44 | free(p->mem); |
| 45 | free(p->aux); |
| 46 | free(p->scratch); |
| 47 | memset(p, 0, sizeof *p); |
| 48 | } |
| 49 | |
| 50 | const char *oma_rz_strerror(int rc) { |
| 51 | switch (rc) { |
| 52 | case OMA_RZ_OK: return "ok"; |
| 53 | case OMA_RZ_E_ARG: return "bad argument"; |
| 54 | case OMA_RZ_E_TRIT: return "weight outside {-1,0,+1}"; |
| 55 | case OMA_RZ_E_NOMEM: return "out of memory"; |
| 56 | case OMA_RZ_E_OVERFLOW: return "size overflow"; |
| 57 | default: return "unknown error"; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int oma_rz_check_shape(size_t m, size_t n, size_t max_n) { |
| 62 | if (m == 0 || n == 0) return OMA_RZ_E_ARG; |
| 63 | if (n > max_n || n > OMA_RZ_MAX_N) return OMA_RZ_E_ARG; |
| 64 | if (m > SIZE_MAX / n / 4u) return OMA_RZ_E_OVERFLOW; |
| 65 | return OMA_RZ_OK; |
| 66 | } |
| 67 | |
| 68 | int oma_rz_validate(const int8_t *w, size_t m, size_t n) { |
| 69 | if (!w) return OMA_RZ_E_ARG; |
| 70 | size_t total = m * n; |
| 71 | for (size_t i = 0; i < total; i++) { |
| 72 | int8_t v = w[i]; |
| 73 | if (v < -1 || v > 1) return OMA_RZ_E_TRIT; |
| 74 | } |
| 75 | return OMA_RZ_OK; |
| 76 | } |
| 77 | |
| 78 | void *oma_rz_alloc(size_t bytes) { |
| 79 | if (bytes == 0) bytes = 64; |
| 80 | size_t rounded = (bytes + 63u) & ~(size_t)63u; |
| 81 | if (rounded < bytes) return NULL; |
| 82 | void *p = aligned_alloc(64, rounded); |
| 83 | if (p) memset(p, 0, rounded); |
| 84 | return p; |
| 85 | } |
| 86 | |
| 87 | int oma_rz_oracle(const int8_t *w, size_t m, size_t n, const int8_t *x, int32_t *y) { |
| 88 | if (!w || !x || !y) return OMA_RZ_E_ARG; |
| 89 | int rc = oma_rz_check_shape(m, n, OMA_RZ_MAX_N); |
| 90 | if (rc) return rc; |
| 91 | for (size_t i = 0; i < m; i++) { |
| 92 | int64_t acc = 0; |
| 93 | const int8_t *row = w + i * n; |
| 94 | for (size_t j = 0; j < n; j++) acc += (int64_t)row[j] * (int64_t)x[j]; |
| 95 | y[i] = (int32_t)acc; |
| 96 | } |
| 97 | return OMA_RZ_OK; |
| 98 | } |