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 / tests / realize / test_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 | /* test_program_fp32.c -- FP32 in the program IR (E1 gap row 12, spec/program-fp32.md). |
| 18 | * Gate OMEGA_PROGRAM_FP32_PASS: type check refuses mixed types, conversions are explicit, |
| 19 | * ids bind the types, evaluation equals an independent host-float evaluator, and the AArch64 |
| 20 | * realizer refuses FP32 programs. Physics-free; opens no device. */ |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <stdint.h> |
| 24 | #include <math.h> |
| 25 | #include "omega_core.h" |
| 26 | #include "omega_program.h" |
| 27 | #include "omega_program_fp32.h" |
| 28 | #include "omega_numeric.h" |
| 29 | |
| 30 | static int fails, total; |
| 31 | #define CHECK(c, msg) do { total++; if (!(c)) { fails++; printf("FAIL: %s (line %d)\n", msg, __LINE__); } } while (0) |
| 32 | |
| 33 | static uint32_t fbits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 34 | static float bf(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 35 | |
| 36 | /* independent evaluator: host float arithmetic (built with -ffp-contract=off) */ |
| 37 | static uint32_t host_ref(const OmegaProgram *p, uint32_t x) { |
| 38 | float v = bf(x); |
| 39 | for (uint16_t i = 0; i < p->body.step_count; ++i) { |
| 40 | uint8_t op = p->body.steps[i].op; |
| 41 | float k = bf((uint32_t)p->body.steps[i].imm); |
| 42 | if (op == OP_ADD) v = v + k; |
| 43 | else if (op == OP_SUB) v = v - k; |
| 44 | else if (op == OP_MUL) v = v * k; |
| 45 | else if (op == OP_DIV) v = v / k; |
| 46 | } |
| 47 | return fbits(v); |
| 48 | } |
| 49 | |
| 50 | int main(void) { |
| 51 | char why[200]; |
| 52 | OmegaProgram a, b, c; |
| 53 | OmegaProgramStep add1[] = { { OP_ADD, fbits(1.0f) } }; |
| 54 | OmegaProgramStep mix[] = { { OP_ADD, fbits(1.0f) }, { OP_MUL, fbits(2.5f) }, { OP_DIV, fbits(3.0f) }, { OP_SUB, fbits(0.5f) } }; |
| 55 | |
| 56 | /* 1. a valid FP32 program builds, checks, has an id */ |
| 57 | CHECK(omega_program_build_fp32(&a, "fp_add1", TYPE_FP32, TYPE_FP32, add1, 1) == 0, "build fp32 add"); |
| 58 | CHECK(omega_program_fp32_check(&a, why, sizeof why) == 0, "check fp32 add"); |
| 59 | CHECK(omega_program_build_fp32(&b, "fp_mix", TYPE_FP32, TYPE_FP32, mix, 4) == 0, "build fp32 mix"); |
| 60 | |
| 61 | /* 2. id binds the type: same steps over u32 is refused, never aliases */ |
| 62 | CHECK(omega_program_build_fp32(&c, "u32_add1", TYPE_UNSIGNED_INT, TYPE_UNSIGNED_INT, add1, 1) != 0, |
| 63 | "ADD on u32 refused without a conversion"); |
| 64 | OmegaProgramStep cv_fp[] = { { OP_CONVERT, TYPE_FP32 } }; |
| 65 | OmegaProgram u2f, f2u; |
| 66 | CHECK(omega_program_build_convert(&u2f, "u2f", TYPE_UNSIGNED_INT, TYPE_FP32) == 0, "build u32->fp32"); |
| 67 | CHECK(omega_program_build_convert(&f2u, "f2u", TYPE_FP32, TYPE_UNSIGNED_INT) == 0, "build fp32->u32"); |
| 68 | CHECK(memcmp(u2f.program_id.bytes, f2u.program_id.bytes, OMEGA_ID_BYTES) != 0, "convert ids differ"); |
| 69 | CHECK(memcmp(a.program_id.bytes, b.program_id.bytes, OMEGA_ID_BYTES) != 0, "fp32 program ids differ by body"); |
| 70 | OmegaProgram a2; |
| 71 | CHECK(omega_program_build_fp32(&a2, "renamed", TYPE_FP32, TYPE_FP32, add1, 1) == 0 && |
| 72 | memcmp(a.program_id.bytes, a2.program_id.bytes, OMEGA_ID_BYTES) == 0, "name is metadata, id stable"); |
| 73 | OmegaProgramStep add_other[] = { { OP_ADD, fbits(2.0f) } }; |
| 74 | OmegaProgram a3; |
| 75 | CHECK(omega_program_build_fp32(&a3, "fp_add2", TYPE_FP32, TYPE_FP32, add_other, 1) == 0 && |
| 76 | memcmp(a.program_id.bytes, a3.program_id.bytes, OMEGA_ID_BYTES) != 0, "constant is part of the id"); |
| 77 | |
| 78 | /* 3. type check refuses mixed types and non-FP32 ops */ |
| 79 | OmegaProgramStep bad_and[] = { { OP_AND, 0xFF } }; |
| 80 | CHECK(omega_program_build_fp32(&c, "and", TYPE_FP32, TYPE_FP32, bad_and, 1) != 0, "AND on FP32 refused"); |
| 81 | OmegaProgramStep mixed[] = { { OP_CONVERT, TYPE_UNSIGNED_INT }, { OP_ADD, 1 } }; |
| 82 | CHECK(omega_program_build_fp32(&c, "mixed", TYPE_FP32, TYPE_UNSIGNED_INT, mixed, 2) != 0, "ADD after CONVERT-to-u32 refused"); |
| 83 | CHECK(omega_program_build_fp32(&c, "wrongout", TYPE_FP32, TYPE_UNSIGNED_INT, add1, 1) != 0, "body type != contract output refused"); |
| 84 | CHECK(omega_program_build_fp32(&c, "noop", TYPE_FP32, TYPE_FP32, cv_fp, 1) != 0, "CONVERT to same type refused"); |
| 85 | OmegaProgramStep big[] = { { OP_ADD, 0x100000000ull } }; |
| 86 | CHECK(omega_program_build_fp32(&c, "big", TYPE_FP32, TYPE_FP32, big, 1) != 0, "constant wider than 32 bits refused"); |
| 87 | /* tampered body: stale id refused */ |
| 88 | OmegaProgram t = a; t.body.steps[0].imm = fbits(9.0f); |
| 89 | CHECK(omega_program_fp32_check(&t, why, sizeof why) != 0, "tampered body refused"); |
| 90 | /* contract width 64 refused */ |
| 91 | t = a; t.contract.input_width = 64; |
| 92 | CHECK(omega_program_fp32_check(&t, why, sizeof why) != 0, "width 64 refused"); |
| 93 | |
| 94 | /* 4. composition: FP32 then FP32 ok; u32 meets FP32 only through convert */ |
| 95 | CHECK(omega_program_compose(&a, &b, &c, why, sizeof why) == 0, "compose fp32 . fp32"); |
| 96 | CHECK(c.body.step_count == 5 && omega_program_fp32_check(&c, why, sizeof why) == 0, "composition is a valid fp32 program"); |
| 97 | CHECK(omega_program_compose(&a, &f2u, &c, why, sizeof why) == 0 && omega_program_fp32_check(&c, why, sizeof why) == 0, |
| 98 | "compose fp32 then convert to u32"); |
| 99 | CHECK(omega_program_compose(&f2u, &a, &c, why, sizeof why) != 0, "u32-out program cannot feed an FP32 program directly"); |
| 100 | CHECK(omega_program_compose(&u2f, &a, &c, why, sizeof why) == 0 && omega_program_fp32_check(&c, why, sizeof why) == 0, |
| 101 | "u32 -> convert -> fp32 op composes"); |
| 102 | |
| 103 | /* 5. evaluation equals the independent host evaluator and the library definitions */ |
| 104 | static const uint32_t pat[] = { 0, 0x80000000u, 0x3f800000u, 0xbf800000u, 0x7f800000u, 0xff800000u, 0x7fc00000u, |
| 105 | 0x00000001u, 0x007fffffu, 0x00800000u, 0x7f7fffffu, 0x4b800000u, 0x33800000u, 0x3effffffu }; |
| 106 | uint32_t xs[4096], ys[4096]; |
| 107 | size_t n = 0; |
| 108 | for (size_t i = 0; i < sizeof pat / sizeof pat[0]; ++i) xs[n++] = pat[i]; |
| 109 | uint64_t s = 0x9e3779b97f4a7c15ull; |
| 110 | while (n < 4096) { s ^= s << 13; s ^= s >> 7; s ^= s << 17; xs[n++] = (uint32_t)(s >> 16); } |
| 111 | const OmegaProgram *progs[] = { &a, &b, &a3 }; |
| 112 | int eq = 1; |
| 113 | for (int pi = 0; pi < 3; ++pi) { |
| 114 | CHECK(omega_program_fp32_eval(progs[pi], xs, n, ys) == 0, "eval fp32"); |
| 115 | for (size_t i = 0; i < n; ++i) { |
| 116 | uint32_t r = host_ref(progs[pi], xs[i]); |
| 117 | int nanboth = isnan(bf(r)) && isnan(bf(ys[i])); |
| 118 | if (r != ys[i] && !nanboth) { eq = 0; printf(" mismatch prog %d x=%08x host=%08x lib=%08x\n", pi, xs[i], r, ys[i]); break; } |
| 119 | } |
| 120 | } |
| 121 | CHECK(eq, "fp32 eval == host float evaluator (14 patterns + 4082 random, 3 programs)"); |
| 122 | |
| 123 | /* conversions equal the library reference definitions */ |
| 124 | CHECK(omega_program_fp32_eval(&u2f, xs, n, ys) == 0, "eval u2f"); |
| 125 | eq = 1; |
| 126 | for (size_t i = 0; i < n; ++i) if (ys[i] != fbits(omega_ref_u2f(xs[i]))) { eq = 0; break; } |
| 127 | CHECK(eq, "u32->fp32 == omega_ref_u2f"); |
| 128 | CHECK(omega_program_fp32_eval(&f2u, xs, n, ys) == 0, "eval f2u"); |
| 129 | eq = 1; |
| 130 | for (size_t i = 0; i < n; ++i) if (ys[i] != omega_ref_f2u(bf(xs[i]))) { eq = 0; break; } |
| 131 | CHECK(eq, "fp32->u32 == omega_ref_f2u"); |
| 132 | |
| 133 | /* a u32 -> convert -> add -> u32 chain, explicit conversions only */ |
| 134 | OmegaProgramStep chain[] = { { OP_CONVERT, TYPE_FP32 }, { OP_ADD, fbits(1.5f) }, { OP_CONVERT, TYPE_UNSIGNED_INT } }; |
| 135 | OmegaProgram ch; |
| 136 | CHECK(omega_program_build_fp32(&ch, "chain", TYPE_UNSIGNED_INT, TYPE_UNSIGNED_INT, chain, 3) == 0, "build explicit chain"); |
| 137 | uint32_t in3[] = { 0, 1, 41, 0xfffffff0u }, out3[4]; |
| 138 | CHECK(omega_program_fp32_eval(&ch, in3, 4, out3) == 0 && out3[0] == 1 && out3[1] == 2 && out3[2] == 42, "chain evaluates 0->1, 1->2, 41->42"); |
| 139 | |
| 140 | /* 6. the AArch64 realizer refuses FP32 programs with a reason */ |
| 141 | char rwhy[200] = ""; |
| 142 | CHECK(omega_program_realize_ex(&a, rwhy, sizeof rwhy) != 0 && rwhy[0] != '\0', "AArch64 realizer refuses FP32 program"); |
| 143 | |
| 144 | printf("GATE OMEGA_PROGRAM_FP32_PASS %s %d/%d\n", fails ? "FAIL" : "PASS", total - fails, total); |
| 145 | return fails ? 1 : 0; |
| 146 | } |