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_program_fp32.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_program_fp32.h" |
| 18 | #include "omega_numeric.h" |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | static int refuse(char *why, size_t n, const char *fmt, unsigned a, const char *s) { |
| 23 | if (why && n) snprintf(why, n, fmt, a, s); |
| 24 | return -1; |
| 25 | } |
| 26 | |
| 27 | static const char *op_name(uint8_t op) { |
| 28 | switch (op) { |
| 29 | case OP_ADD: return "ADD"; |
| 30 | case OP_SUB: return "SUB"; |
| 31 | case OP_MUL: return "MUL"; |
| 32 | case OP_DIV: return "DIV"; |
| 33 | case OP_AND: return "AND"; |
| 34 | case OP_OR: return "OR"; |
| 35 | case OP_CONVERT: return "CONVERT"; |
| 36 | default: return "an opcode"; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /* Walk the body as the two-state machine of omega_program.c lower_body_fp32 and say why it is refused. */ |
| 41 | static int walk(const OmegaProgram *prog, char *why, size_t n) { |
| 42 | const OmegaContract *c = &prog->contract; |
| 43 | if (!prog->body.has_body) return refuse(why, n, "no semantic body%.0u%s", 0, ""); |
| 44 | if (prog->body.step_count > OMEGA_PROGRAM_MAX_STEPS) return refuse(why, n, "more than 64 steps%.0u%s", 0, ""); |
| 45 | if (c->input_width != 32 || c->output_width != 32) return refuse(why, n, "FP32 programs are width 32%.0u%s", 0, ""); |
| 46 | if ((c->input_type != TYPE_FP32 && c->input_type != TYPE_UNSIGNED_INT) || |
| 47 | (c->output_type != TYPE_FP32 && c->output_type != TYPE_UNSIGNED_INT)) |
| 48 | return refuse(why, n, "contract types must be FP32 or u32%.0u%s", 0, ""); |
| 49 | TypeTag cur = c->input_type; |
| 50 | for (uint16_t i = 0; i < prog->body.step_count; ++i) { |
| 51 | uint8_t op = prog->body.steps[i].op; |
| 52 | uint64_t imm = prog->body.steps[i].imm; |
| 53 | if (op == OP_CONVERT) { |
| 54 | if (imm != TYPE_FP32 && imm != TYPE_UNSIGNED_INT) return refuse(why, n, "step %u: CONVERT target is neither FP32 nor u32%s", i, ""); |
| 55 | if ((TypeTag)imm == cur) return refuse(why, n, "step %u: CONVERT to the type the value already has%s", i, ""); |
| 56 | cur = (TypeTag)imm; |
| 57 | continue; |
| 58 | } |
| 59 | if (cur != TYPE_FP32) |
| 60 | return refuse(why, n, "step %u: %s applied to a u32 value in an FP32 program: mixed types need an explicit CONVERT", i, op_name(op)); |
| 61 | if (op != OP_ADD && op != OP_SUB && op != OP_MUL && op != OP_DIV) |
| 62 | return refuse(why, n, "step %u: %s is not an FP32 operation (ADD SUB MUL DIV CONVERT)", i, op_name(op)); |
| 63 | if (imm > 0xFFFFFFFFull) return refuse(why, n, "step %u: constant wider than 32 bits%s", i, ""); |
| 64 | } |
| 65 | if (cur != c->output_type) return refuse(why, n, "body ends as a different type than the contract output%.0u%s", 0, ""); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int omega_program_fp32_check(const OmegaProgram *prog, char *why, size_t why_len) { |
| 70 | if (!prog) return refuse(why, why_len, "no program%.0u%s", 0, ""); |
| 71 | if (walk(prog, why, why_len) != 0) return -1; |
| 72 | OmegaProgram chk = *prog; |
| 73 | chk.graph = NULL; |
| 74 | if (omega_program_compute_id(&chk) != 0 || memcmp(chk.program_id.bytes, prog->program_id.bytes, OMEGA_ID_BYTES) != 0) |
| 75 | return refuse(why, why_len, "program_id is not the v2 id of this body and contract (stale or forged)%.0u%s", 0, ""); |
| 76 | if (why && why_len) why[0] = '\0'; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int omega_program_build_fp32(OmegaProgram *prog, const char *name, TypeTag in_type, TypeTag out_type, |
| 81 | const OmegaProgramStep *steps, uint16_t count) { |
| 82 | if (!prog || (count && !steps) || count > OMEGA_PROGRAM_MAX_STEPS) return -1; |
| 83 | omega_program_init(prog, name); |
| 84 | prog->contract.input_type = in_type; |
| 85 | prog->contract.input_width = 32; |
| 86 | prog->contract.output_type = out_type; |
| 87 | prog->contract.output_width = 32; |
| 88 | prog->body.has_body = true; |
| 89 | prog->body.step_count = count; |
| 90 | if (count) memcpy(prog->body.steps, steps, count * sizeof(OmegaProgramStep)); |
| 91 | if (walk(prog, NULL, 0) != 0) return -1; |
| 92 | snprintf(prog->contract.precondition, sizeof(prog->contract.precondition), "x is a binary32 or u32 value"); |
| 93 | omega_build_constraint_id(CONST_PRECONDITION, prog->contract.precondition, &prog->contract.precondition_id); |
| 94 | snprintf(prog->contract.postcondition, sizeof(prog->contract.postcondition), "fp32 program of %u steps", (unsigned)count); |
| 95 | omega_build_constraint_id(CONST_POSTCONDITION, prog->contract.postcondition, &prog->contract.postcondition_id); |
| 96 | return omega_program_compute_id(prog) == 0 ? 0 : -1; |
| 97 | } |
| 98 | |
| 99 | int omega_program_build_convert(OmegaProgram *prog, const char *name, TypeTag from, TypeTag to) { |
| 100 | OmegaProgramStep s = { OP_CONVERT, (uint64_t)to }; |
| 101 | return omega_program_build_fp32(prog, name, from, to, &s, 1); |
| 102 | } |
| 103 | |
| 104 | int omega_program_fp32_eval(const OmegaProgram *prog, const uint32_t *xs, size_t n, uint32_t *ys) { |
| 105 | if (!prog || (n && (!xs || !ys))) return -1; |
| 106 | if (omega_program_fp32_check(prog, NULL, 0) != 0) return -1; |
| 107 | for (size_t k = 0; k < n; ++k) { |
| 108 | float v = omega_bits_to_float(xs[k]); /* a u32 travels as its bit pattern, the library's convention */ |
| 109 | for (uint16_t i = 0; i < prog->body.step_count; ++i) { |
| 110 | uint8_t op = prog->body.steps[i].op; |
| 111 | float kc = omega_bits_to_float((uint32_t)prog->body.steps[i].imm), r = 0.0f; |
| 112 | OmegaNumericOp nop; |
| 113 | if (op == OP_CONVERT) { |
| 114 | nop = prog->body.steps[i].imm == TYPE_FP32 ? OMEGA_NOP_I2FP_U32 : OMEGA_NOP_F2U; |
| 115 | if (omega_numeric_cpu_realize(nop, &v, &v, NULL, &r, 1) != OMEGA_NUMERIC_OK) return -1; |
| 116 | } else { |
| 117 | nop = op == OP_ADD ? OMEGA_NOP_FADD : op == OP_SUB ? OMEGA_NOP_FSUB : op == OP_MUL ? OMEGA_NOP_FMUL : OMEGA_NOP_DIV; |
| 118 | if (omega_numeric_cpu_realize(nop, &v, &kc, NULL, &r, 1) != OMEGA_NUMERIC_OK) return -1; |
| 119 | } |
| 120 | v = r; |
| 121 | } |
| 122 | ys[k] = omega_float_to_bits(v); |
| 123 | } |
| 124 | return 0; |
| 125 | } |