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 / omega_matvec_quad.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 | #include "omega_matvec_quad.h" |
| 18 | |
| 19 | #include "aarch64_encoder.h" |
| 20 | #include "aarch64_target.h" |
| 21 | #include "omega_realize.h" |
| 22 | #include "omega_verify.h" |
| 23 | |
| 24 | #include <string.h> |
| 25 | |
| 26 | /* x0 = A, x1 = x, x2 = y, x3 = M, x4 = N. Only caller-saved x5..x17 are used. */ |
| 27 | static int emit_quad4(uint8_t *code, size_t *pos, size_t max) { |
| 28 | int rc = 0; |
| 29 | /* Words 0-1: guards */ |
| 30 | rc |= aarch64_emit_cbz(code, pos, max, true, REG_X3, 50); /* -> ret (50) */ |
| 31 | rc |= aarch64_emit_cbz(code, pos, max, true, REG_X4, 49); /* -> ret (50) */ |
| 32 | /* Words 2-6: rem = N & 3, quad = N - rem, rows = M, stride = 32 bytes */ |
| 33 | rc |= aarch64_emit_movz(code, pos, max, true, REG_X16, 3, 0); |
| 34 | rc |= aarch64_emit_and_reg(code, pos, max, true, REG_X15, REG_X4, REG_X16); |
| 35 | rc |= aarch64_emit_sub_reg(code, pos, max, true, REG_X14, REG_X4, REG_X15); |
| 36 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X5, REG_X3); |
| 37 | rc |= aarch64_emit_movz(code, pos, max, true, REG_X17, 32, 0); |
| 38 | |
| 39 | /* Word 7 (row_loop) */ |
| 40 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X6, REG_X0); |
| 41 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X7, REG_X1); |
| 42 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X8, REG_XZR); |
| 43 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X9, REG_XZR); |
| 44 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X10, REG_XZR); |
| 45 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X11, REG_XZR); |
| 46 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X13, REG_X14); |
| 47 | rc |= aarch64_emit_cbz(code, pos, max, true, REG_X13, 21); /* -> combine (35) */ |
| 48 | |
| 49 | /* Word 15 (quad_loop): four independent products, fixed offsets */ |
| 50 | static const uint8_t acc[4] = { REG_X8, REG_X9, REG_X10, REG_X11 }; |
| 51 | for (int p = 0; p < 4; p++) { |
| 52 | rc |= aarch64_emit_ldr_uoff(code, pos, max, true, REG_X12, REG_X6, (uint16_t)(p * 8)); |
| 53 | rc |= aarch64_emit_ldr_uoff(code, pos, max, true, REG_X16, REG_X7, (uint16_t)(p * 8)); |
| 54 | rc |= aarch64_emit_mul_reg(code, pos, max, true, REG_X12, REG_X12, REG_X16); |
| 55 | rc |= aarch64_emit_add_reg(code, pos, max, true, acc[p], acc[p], REG_X12); |
| 56 | } |
| 57 | /* Word 31: one base update per four elements */ |
| 58 | rc |= aarch64_emit_add_reg(code, pos, max, true, REG_X6, REG_X6, REG_X17); |
| 59 | rc |= aarch64_emit_add_reg(code, pos, max, true, REG_X7, REG_X7, REG_X17); |
| 60 | rc |= aarch64_emit_subs_imm(code, pos, max, true, REG_X13, REG_X13, 4); |
| 61 | rc |= aarch64_emit_b_cond(code, pos, max, COND_NE, -19); /* -> quad_loop (15) */ |
| 62 | |
| 63 | /* Word 35 (combine) */ |
| 64 | rc |= aarch64_emit_add_reg(code, pos, max, true, REG_X8, REG_X8, REG_X9); |
| 65 | rc |= aarch64_emit_add_reg(code, pos, max, true, REG_X10, REG_X10, REG_X11); |
| 66 | rc |= aarch64_emit_add_reg(code, pos, max, true, REG_X8, REG_X8, REG_X10); |
| 67 | rc |= aarch64_emit_cbz(code, pos, max, true, REG_X15, 8); /* -> row_end (46) */ |
| 68 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X13, REG_X15); |
| 69 | |
| 70 | /* Word 40 (rem_loop) */ |
| 71 | rc |= aarch64_emit_ldr_x_post(code, pos, max, REG_X12, REG_X6, 8); |
| 72 | rc |= aarch64_emit_ldr_x_post(code, pos, max, REG_X16, REG_X7, 8); |
| 73 | rc |= aarch64_emit_mul_reg(code, pos, max, true, REG_X12, REG_X12, REG_X16); |
| 74 | rc |= aarch64_emit_add_reg(code, pos, max, true, REG_X8, REG_X8, REG_X12); |
| 75 | rc |= aarch64_emit_subs_imm(code, pos, max, true, REG_X13, REG_X13, 1); |
| 76 | rc |= aarch64_emit_b_cond(code, pos, max, COND_NE, -5); /* -> rem_loop (40) */ |
| 77 | |
| 78 | /* Word 46 (row_end): x6 now points at the next row */ |
| 79 | rc |= aarch64_emit_str_x_post(code, pos, max, REG_X8, REG_X2, 8); |
| 80 | rc |= aarch64_emit_mov_reg(code, pos, max, true, REG_X0, REG_X6); |
| 81 | rc |= aarch64_emit_subs_imm(code, pos, max, true, REG_X5, REG_X5, 1); |
| 82 | rc |= aarch64_emit_b_cond(code, pos, max, COND_NE, -42); /* -> row_loop (7) */ |
| 83 | |
| 84 | /* Word 50 (ret) */ |
| 85 | rc |= aarch64_emit_ret(code, pos, max); |
| 86 | return rc ? -1 : 0; |
| 87 | } |
| 88 | |
| 89 | int omega_matvec_synthesize_quad4(const MatVecSemanticSpec *spec, const OmegaMachineGraph *mg, |
| 90 | MatVecRealization *out) { |
| 91 | if (!spec || !mg || !out) return -1; |
| 92 | memset(out, 0, sizeof(*out)); |
| 93 | out->kind = (MatVecRealizationKind)OMEGA_MATVEC_KIND_QUAD4; |
| 94 | out->name = "matvec_quad4"; |
| 95 | out->unroll_factor = 4; |
| 96 | out->accumulators = 4; |
| 97 | out->realization.target_profile = mg->target_profile; |
| 98 | out->realization.entry_offset = 0; |
| 99 | out->realization.semantic_id = spec->spec_id; |
| 100 | out->realization.machine_id = mg->machine_id; |
| 101 | out->realization.has_machine_id = true; |
| 102 | |
| 103 | size_t pos = 0; |
| 104 | if (emit_quad4(out->realization.code_bytes, &pos, sizeof(out->realization.code_bytes)) != 0) |
| 105 | return -1; |
| 106 | if (pos != 51u * 4u) return -1; /* the branch offsets above assume this layout */ |
| 107 | out->realization.code_len = pos; |
| 108 | |
| 109 | omega_realize_compute_triple_id(&spec->spec_id, &mg->machine_id, &out->realization, |
| 110 | &out->realization_id); |
| 111 | out->realization.realization_id = out->realization_id; |
| 112 | out->realization.has_id = true; |
| 113 | |
| 114 | VerifyReport rep; |
| 115 | memset(&rep, 0, sizeof(rep)); |
| 116 | omega_verify_v0_structural(NULL, &out->realization, &rep); |
| 117 | out->verify_report = rep; |
| 118 | out->is_verified = rep.passed; |
| 119 | return rep.passed ? 0 : -1; |
| 120 | } |