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 / test_omega_numeric.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 | /* |
| 18 | * Gate 5: OMEGA-NUMERIC-0 qualification suite. |
| 19 | * |
| 20 | * Two builds: |
| 21 | * -DOMEGA_NUMERIC_CPU_ONLY host tiers only (reference and CPU realization). |
| 22 | * Opens no device. Gate items that need GB10 print |
| 23 | * [SKIP], never [PASS]. The hardware descriptor |
| 24 | * line is a marked fake. |
| 25 | * (default) adds the GB10 tier through |
| 26 | * omega_gb10_execute_simt_op and a FORGE hardware |
| 27 | * probe. Run only through tests/run_numeric_gates.sh. |
| 28 | * |
| 29 | * Machine-readable lines (parsed by tests/run_numeric_gates.sh): |
| 30 | * [PASS] ID / [FAIL] ID / [SKIP] ID |
| 31 | * OMEGA_NUMERIC_HWDESC_JSON:{...} |
| 32 | * OMEGA_NUMERIC_PARITY_JSON:{...} one per op per tier |
| 33 | * OMEGA_NUMERIC_FINDING_JSON:{...} |
| 34 | * OMEGA_NUMERIC_ORACLE_JSON:{...} CPU tier against the independent oracle |
| 35 | * in tests/numeric_oracle.h (host only) |
| 36 | */ |
| 37 | #include "omega_numeric.h" |
| 38 | #include "omega_numeric_provenance.h" |
| 39 | #include "omega_numeric_divsqrt_gb10.h" |
| 40 | #include "omega_blackwell_codegen.h" |
| 41 | #include "omega_blackwell_qmd.h" |
| 42 | |
| 43 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 44 | #include "forge_descriptor.h" |
| 45 | #endif |
| 46 | |
| 47 | #include "sha256.h" |
| 48 | #include "numeric_oracle.h" |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | #include <sys/mman.h> |
| 54 | #include <sys/wait.h> |
| 55 | #include <unistd.h> |
| 56 | |
| 57 | static int g_total = 0, g_passed = 0, g_failed = 0, g_skipped = 0; |
| 58 | |
| 59 | static void report(const char *name, int ok) { |
| 60 | g_total++; |
| 61 | if (ok) { g_passed++; printf("[PASS] %s\n", name); } |
| 62 | else { g_failed++; printf("[FAIL] %s\n", name); } |
| 63 | } |
| 64 | |
| 65 | /* Test IDs that only a GB10 chip can decide. The CPU-only build (make |
| 66 | * test-numeric-cpu) must SKIP exactly these and nothing else. */ |
| 67 | static const char *const CHIP_ONLY_IDS[] = { |
| 68 | "CPU_GB10_BIT_PARITY", "SUBNORMALS_PRESERVED_NO_FTZ", "MUFU_SEED_ONLY_NOT_COMPARED", |
| 69 | "EDGE_CLASS_BEHAVIOR_VERIFIED", "HARDWARE_DESCRIPTOR_PROBED", |
| 70 | }; |
| 71 | #define CHIP_ONLY_COUNT (sizeof(CHIP_ONLY_IDS) / sizeof(CHIP_ONLY_IDS[0])) |
| 72 | static bool g_chip_only_skipped[CHIP_ONLY_COUNT]; |
| 73 | static int g_undeclared_skips = 0; |
| 74 | |
| 75 | static void skip(const char *name, const char *why) { |
| 76 | g_skipped++; |
| 77 | bool declared = false; |
| 78 | for (size_t k = 0; k < CHIP_ONLY_COUNT; k++) { |
| 79 | if (strcmp(name, CHIP_ONLY_IDS[k]) == 0 && !g_chip_only_skipped[k]) { |
| 80 | g_chip_only_skipped[k] = true; |
| 81 | declared = true; |
| 82 | } |
| 83 | } |
| 84 | if (!declared) g_undeclared_skips++; |
| 85 | printf("[SKIP] %s (%s)\n", name, why); |
| 86 | } |
| 87 | |
| 88 | static uint32_t lcg_next(uint32_t *state) { |
| 89 | *state = (*state * 1664525u + 1013904223u); |
| 90 | return *state; |
| 91 | } |
| 92 | |
| 93 | static float F(uint32_t u) { return omega_bits_to_float(u); } |
| 94 | static uint32_t U(float f) { return omega_float_to_bits(f); } |
| 95 | |
| 96 | /* ---- Corpus ---------------------------------------------------------------- */ |
| 97 | |
| 98 | #define N 4096u |
| 99 | #define EDGE_COUNT 32u |
| 100 | |
| 101 | static const uint32_t EDGE[EDGE_COUNT] = { |
| 102 | 0x00000000u, 0x80000000u, 0x7f800000u, 0xff800000u, /* +0 -0 +inf -inf */ |
| 103 | 0x7fc00000u, 0xffc00000u, 0x7f800001u, 0x7fbfffffu, /* +qNaN -qNaN sNaN sNaN */ |
| 104 | 0x00000001u, 0x80000001u, 0x00000002u, 0x00000003u, /* subnormals */ |
| 105 | 0x0000ffffu, 0x007fffffu, 0x807fffffu, 0x00400000u, |
| 106 | 0x00800000u, 0x80800000u, 0x00800001u, 0x7f7fffffu, /* min normal, max normal */ |
| 107 | 0xff7fffffu, 0x3f800000u, 0xbf800000u, 0x3f000000u, /* +-max, +-1, 0.5 */ |
| 108 | 0x40000000u, 0x40400000u, 0x0d800000u, 0x2b800000u, /* 2, 3, 2^-100, 2^-40 */ |
| 109 | 0x71800000u, 0x7e800000u, 0x3f7fffffu, 0x4f000000u, /* 2^100, 2^126, 1-ulp, 2^31 */ |
| 110 | }; |
| 111 | |
| 112 | enum { CL_ZERO, CL_INF, CL_NAN, CL_SUB, CL_NORMAL, CL_COUNT }; |
| 113 | static const char *CL_NAME[CL_COUNT] = { "zero", "inf", "nan", "subnormal", "normal" }; |
| 114 | |
| 115 | static int classify(float x) { |
| 116 | if (omega_iszero(x)) return CL_ZERO; |
| 117 | if (omega_isinf(x)) return CL_INF; |
| 118 | if (omega_isnan(x)) return CL_NAN; |
| 119 | if (omega_issubnormal(x)) return CL_SUB; |
| 120 | return CL_NORMAL; |
| 121 | } |
| 122 | |
| 123 | static float g_a[N], g_b[N]; |
| 124 | |
| 125 | static void build_corpus(void) { |
| 126 | uint32_t s = 0x19283746u; |
| 127 | size_t i = 0; |
| 128 | /* 0..1023: edge class cross product (a from EDGE[i/32], b from EDGE[i%32]) */ |
| 129 | for (; i < 1024; i++) { g_a[i] = F(EDGE[i / 32]); g_b[i] = F(EDGE[i % 32]); } |
| 130 | /* 1024..1535: subnormal pairs with random signs: sums and differences stay subnormal */ |
| 131 | for (; i < 1536; i++) { |
| 132 | uint32_t r1 = lcg_next(&s), r2 = lcg_next(&s); |
| 133 | g_a[i] = F((r1 & 0x80000000u) | ((r1 >> 3) & 0x003fffffu) | 1u); |
| 134 | g_b[i] = F((r2 & 0x80000000u) | ((r2 >> 3) & 0x003fffffu)); |
| 135 | } |
| 136 | /* 1536..2047: products that underflow into the subnormal range */ |
| 137 | for (; i < 2048; i++) { |
| 138 | uint32_t r1 = lcg_next(&s), r2 = lcg_next(&s); |
| 139 | uint32_t e1 = 61u + (r1 >> 28) % 6u, e2 = 61u + (r2 >> 28) % 6u; /* 2^-66 .. 2^-61 */ |
| 140 | g_a[i] = F((r1 & 0x80000000u) | (e1 << 23) | (r1 & 0x007fffffu)); |
| 141 | g_b[i] = F((r2 & 0x80000000u) | (e2 << 23) | (r2 & 0x007fffffu)); |
| 142 | } |
| 143 | /* 2048..3071: raw random bit patterns */ |
| 144 | for (; i < 3072; i++) { g_a[i] = F(lcg_next(&s)); g_b[i] = F(lcg_next(&s)); } |
| 145 | /* 3072..3199: a = -(1 - k 2^-24), b = 2^-126: with c = min normal, FFMA lands subnormal */ |
| 146 | for (uint32_t k = 1; i < 3200; i++, k++) { g_a[i] = F(0xbf800000u - k); g_b[i] = F(0x00800000u); } |
| 147 | /* 3200..3455: conversion boundaries */ |
| 148 | static const uint32_t CONV[] = { |
| 149 | 0x4f000000u, 0xcf000000u, 0x4effffffu, 0xceffffffu, 0x4f000001u, 0xcf000001u, |
| 150 | 0x4b800000u, 0xcb800000u, 0x4b800001u, 0x3f000000u, 0xbf000000u, 0x3fc00000u, |
| 151 | 0xbfc00000u, 0x3f7fffffu, 0xbf7fffffu, 0x7fffffffu, |
| 152 | }; |
| 153 | for (size_t k = 0; i < 3456; i++, k++) { |
| 154 | g_a[i] = F(CONV[k % (sizeof(CONV) / sizeof(CONV[0]))]); |
| 155 | uint32_t r = lcg_next(&s); |
| 156 | g_b[i] = F((r & 0x807fffffu) | (0x7fu << 23)); |
| 157 | } |
| 158 | /* 3456..4095: ordinary finite values, mixed magnitudes */ |
| 159 | for (; i < N; i++) { |
| 160 | uint32_t r1 = lcg_next(&s), r2 = lcg_next(&s); |
| 161 | g_a[i] = F((r1 & 0x807fffffu) | ((100u + (r1 >> 25) % 56u) << 23)); |
| 162 | g_b[i] = F((r2 & 0x807fffffu) | ((100u + (r2 >> 25) % 56u) << 23)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static const uint32_t FFMA_C[] = { |
| 167 | 0x3f800000u, 0x80000000u, 0x00800000u, 0xbf800000u, |
| 168 | 0x7fc00000u, 0x7f800000u, 0x00000003u, 0x80800000u, |
| 169 | }; |
| 170 | #define FFMA_C_COUNT (sizeof(FFMA_C) / sizeof(FFMA_C[0])) |
| 171 | |
| 172 | static bool ftz_sensitive(OmegaNumericOp op) { |
| 173 | return op == OMEGA_NOP_FADD || op == OMEGA_NOP_FSUB || op == OMEGA_NOP_FMUL || |
| 174 | op == OMEGA_NOP_FFMA || op == OMEGA_NOP_FFMA_V || op == OMEGA_NOP_FMNMX_MIN || op == OMEGA_NOP_FMNMX_MAX; |
| 175 | } |
| 176 | |
| 177 | /* Per-class parity on top of omega_numeric_parity (class of input a). */ |
| 178 | typedef struct { size_t n[CL_COUNT]; size_t bad[CL_COUNT]; } ClassTrace; |
| 179 | |
| 180 | static void class_trace(OmegaNumericOp op, const float *a, const float *expect, const float *got, |
| 181 | size_t count, ClassTrace *ct) { |
| 182 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 183 | memset(ct, 0, sizeof(*ct)); |
| 184 | for (size_t i = 0; i < count; i++) { |
| 185 | if (!omega_numeric_element_checked(op, i)) continue; |
| 186 | int c = classify(a[i]); |
| 187 | ct->n[c]++; |
| 188 | bool same = omega_numeric_compare_equal(info->compare, U(expect[i]), U(got[i])); |
| 189 | if (!same) ct->bad[c]++; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static void print_parity_json(const char *op, const char *tier, const char *compare, uint32_t c_bits, |
| 194 | bool has_c, size_t n, const OmegaParityTrace *t, const ClassTrace *ct) { |
| 195 | printf("OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"%s\",\"tier\":\"%s\",\"compare\":\"%s\",", op, tier, compare); |
| 196 | if (has_c) printf("\"c_bits\":\"0x%08x\",", c_bits); |
| 197 | if (strcmp(op, "REDUCE_SUM") == 0) printf("\"reduction_order\":\"%s\",", OMEGA_WARP_REDUCTION_DECLARED_ORDER); |
| 198 | printf("\"n\":%zu,\"checked\":%zu,\"mismatches\":%zu,\"subnormal_expected\":%zu,", |
| 199 | n, t->checked, t->mismatches, t->subnormal_expected); |
| 200 | if (t->first_index >= 0) { |
| 201 | printf("\"first_mismatch\":{\"index\":%ld,\"expect\":\"0x%08x\",\"got\":\"0x%08x\"},", |
| 202 | t->first_index, t->first_expect, t->first_got); |
| 203 | } else { |
| 204 | printf("\"first_mismatch\":null,"); |
| 205 | } |
| 206 | printf("\"by_input_class\":{"); |
| 207 | for (int c = 0; c < CL_COUNT; c++) { |
| 208 | printf("%s\"%s\":{\"n\":%zu,\"mismatches\":%zu}", c ? "," : "", CL_NAME[c], |
| 209 | ct ? ct->n[c] : 0, ct ? ct->bad[c] : 0); |
| 210 | } |
| 211 | printf("}}\n"); |
| 212 | } |
| 213 | |
| 214 | static __attribute__((unused)) void print_bound_json(const char *op, const char *tier, size_t n, const OmegaParityTrace *t) { |
| 215 | printf("OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"%s\",\"tier\":\"%s\",\"compare\":\"SEED_BOUND\"," |
| 216 | "\"bound\":\"rel<=2^-20 vs IEEE, finite normal in and out\",\"n\":%zu,\"checked\":%zu," |
| 217 | "\"out_of_bound\":%zu,\"skipped\":%zu,", op, tier, n, t->checked, t->out_of_bound, t->bound_skipped); |
| 218 | if (t->first_index >= 0) |
| 219 | printf("\"first_violation\":{\"index\":%ld,\"expect\":\"0x%08x\",\"got\":\"0x%08x\"}}\n", |
| 220 | t->first_index, t->first_expect, t->first_got); |
| 221 | else |
| 222 | printf("\"first_violation\":null}\n"); |
| 223 | } |
| 224 | |
| 225 | static void fill_c(float *c, uint32_t bits) { |
| 226 | for (size_t i = 0; i < N; i++) c[i] = F(bits); |
| 227 | } |
| 228 | |
| 229 | /* ---- Tier checks -------------------------------------------------------------- */ |
| 230 | |
| 231 | typedef struct { |
| 232 | bool ok; /* zero mismatches everywhere it applies */ |
| 233 | bool subnormals_seen; /* FTZ-sensitive ops: subnormal expected results */ |
| 234 | } TierResult; |
| 235 | |
| 236 | static float g_c[N], g_ref[N], g_cpu[N], g_dev[N]; |
| 237 | |
| 238 | /* CPU tier against reference for one op (all c values for FFMA). */ |
| 239 | static TierResult cpu_tier(OmegaNumericOp op) { |
| 240 | TierResult tr = { true, false }; |
| 241 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 242 | size_t launches = (op == OMEGA_NOP_FFMA) ? FFMA_C_COUNT : 1; |
| 243 | size_t sub_total = 0; |
| 244 | for (size_t l = 0; l < launches; l++) { |
| 245 | const float *c = NULL; |
| 246 | if (op == OMEGA_NOP_FFMA) { fill_c(g_c, FFMA_C[l]); c = g_c; } |
| 247 | if (op == OMEGA_NOP_FFMA_V) { for (size_t i = 0; i < N; i++) g_c[i] = g_a[N - 1 - i]; c = g_c; } |
| 248 | if (omega_numeric_reference(op, g_a, g_b, c, g_ref, N) != 0 || |
| 249 | omega_numeric_cpu_realize(op, g_a, g_b, c, g_cpu, N) != 0) { |
| 250 | tr.ok = false; |
| 251 | continue; |
| 252 | } |
| 253 | OmegaParityTrace t; |
| 254 | if (info->compare == OMEGA_CMP_SEED_BOUND) { |
| 255 | /* Host tier has no MUFU; its realization is the IEEE value itself. */ |
| 256 | memset(&t, 0, sizeof(t)); t.first_index = -1; |
| 257 | for (size_t i = 0; i < N; i++) { |
| 258 | t.checked++; |
| 259 | if (!omega_numeric_bits_equal(g_ref[i], g_cpu[i])) { |
| 260 | if (!t.mismatches) { t.first_index = (long)i; t.first_expect = U(g_ref[i]); t.first_got = U(g_cpu[i]); } |
| 261 | t.mismatches++; |
| 262 | } |
| 263 | } |
| 264 | /* Reference and CPU tier both run the host fdiv/fsqrt here, so this line |
| 265 | * only shows they agree with each other. The independent check is |
| 266 | * the integer oracle (CPU_TIER_INDEPENDENT_ORACLE). */ |
| 267 | print_parity_json(info->name, "cpu", "SELF_CONSISTENT_HOST_FDIV", 0, false, N, &t, NULL); |
| 268 | } else { |
| 269 | if (omega_numeric_parity(op, g_ref, g_cpu, N, &t) != 0) { tr.ok = false; continue; } |
| 270 | ClassTrace ct; |
| 271 | class_trace(op, g_a, g_ref, g_cpu, N, &ct); |
| 272 | /* EXP and LOG: reference and CPU tier call the same omega_math_* sequence, |
| 273 | * so agreement is self-consistency only. Accuracy is checked against the |
| 274 | * binary128 oracle with a stated ulp bound. */ |
| 275 | bool self_only = op == OMEGA_NOP_EXP || op == OMEGA_NOP_LOG; |
| 276 | print_parity_json(info->name, "cpu", self_only ? "SELF_CONSISTENT" : omega_numeric_compare_name(info->compare), |
| 277 | op == OMEGA_NOP_FFMA ? FFMA_C[l] : 0, op == OMEGA_NOP_FFMA, N, &t, &ct); |
| 278 | } |
| 279 | if (t.mismatches) tr.ok = false; |
| 280 | sub_total += t.subnormal_expected; |
| 281 | } |
| 282 | tr.subnormals_seen = sub_total > 0; |
| 283 | return tr; |
| 284 | } |
| 285 | |
| 286 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 287 | /* GB10 tier against reference for one encoded op. */ |
| 288 | static TierResult gb10_tier(OmegaNumericOp op, bool *class_ok) { |
| 289 | TierResult tr = { true, false }; |
| 290 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 291 | size_t launches = (op == OMEGA_NOP_FFMA) ? FFMA_C_COUNT : 1; |
| 292 | size_t sub_total = 0; |
| 293 | for (size_t l = 0; l < launches; l++) { |
| 294 | const float *c = NULL; |
| 295 | if (op == OMEGA_NOP_FFMA) { fill_c(g_c, FFMA_C[l]); c = g_c; } |
| 296 | if (op == OMEGA_NOP_FFMA_V) { for (size_t i = 0; i < N; i++) g_c[i] = g_a[N - 1 - i]; c = g_c; } |
| 297 | int rc = omega_numeric_reference(op, g_a, g_b, c, g_ref, N); |
| 298 | if (rc != 0) { |
| 299 | printf("OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"%s\",\"tier\":\"gb10\",\"error\":%d,\"stage\":\"reference\"}\n", info->name, rc); |
| 300 | tr.ok = false; |
| 301 | *class_ok = false; |
| 302 | continue; |
| 303 | } |
| 304 | rc = omega_gb10_execute_simt_op(info->name, g_a, info->arity >= 2 ? g_b : NULL, c, g_dev, N); |
| 305 | if (rc != 0) { |
| 306 | printf("OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"%s\",\"tier\":\"gb10\",\"error\":%d}\n", info->name, rc); |
| 307 | tr.ok = false; |
| 308 | *class_ok = false; |
| 309 | continue; |
| 310 | } |
| 311 | OmegaParityTrace t; |
| 312 | if (info->compare == OMEGA_CMP_SEED_BOUND) { |
| 313 | rc = omega_numeric_seed_bound(op, g_a, g_dev, N, &t); |
| 314 | if (rc != 0) { |
| 315 | printf("OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"%s\",\"tier\":\"gb10\",\"error\":%d,\"stage\":\"seed_bound\"}\n", info->name, rc); |
| 316 | tr.ok = false; |
| 317 | continue; |
| 318 | } |
| 319 | print_bound_json(info->name, "gb10", N, &t); |
| 320 | if (t.out_of_bound || t.checked == 0) tr.ok = false; |
| 321 | continue; |
| 322 | } |
| 323 | rc = omega_numeric_parity(op, g_ref, g_dev, N, &t); |
| 324 | if (rc != 0) { |
| 325 | printf("OMEGA_NUMERIC_PARITY_JSON:{\"op\":\"%s\",\"tier\":\"gb10\",\"error\":%d,\"stage\":\"parity\"}\n", info->name, rc); |
| 326 | tr.ok = false; |
| 327 | *class_ok = false; |
| 328 | continue; |
| 329 | } |
| 330 | ClassTrace ct; |
| 331 | class_trace(op, g_a, g_ref, g_dev, N, &ct); |
| 332 | print_parity_json(info->name, "gb10", omega_numeric_compare_name(info->compare), |
| 333 | op == OMEGA_NOP_FFMA ? FFMA_C[l] : 0, op == OMEGA_NOP_FFMA, N, &t, &ct); |
| 334 | for (int k = 0; k < CL_COUNT; k++) if (ct.bad[k]) *class_ok = false; |
| 335 | if (t.mismatches) tr.ok = false; |
| 336 | sub_total += t.subnormal_expected; |
| 337 | } |
| 338 | tr.subnormals_seen = sub_total > 0; |
| 339 | return tr; |
| 340 | } |
| 341 | |
| 342 | static int print_hw_descriptor(void) { |
| 343 | ForgeMachineDescriptor d; |
| 344 | memset(&d, 0, sizeof(d)); |
| 345 | uint8_t dig[FORGE_DESC_DIGEST_LEN]; |
| 346 | char hex[65]; |
| 347 | if (forge_probe_hardware(&d) != 0 || forge_descriptor_normalize(&d) != 0 || |
| 348 | forge_descriptor_compute_digest(&d, dig) != 0) { |
| 349 | printf("OMEGA_NUMERIC_HWDESC_JSON:{\"source\":\"FORGE_PROBE_FAILED\"}\n"); |
| 350 | return -1; |
| 351 | } |
| 352 | forge_descriptor_digest_hex(dig, hex); |
| 353 | const char *alias = forge_descriptor_derived_alias(&d); |
| 354 | printf("OMEGA_NUMERIC_HWDESC_JSON:{\"source\":\"FORGE_PROBE\",\"descriptor_digest\":\"%s\"," |
| 355 | "\"derived_alias\":\"%s\"}\n", hex, alias ? alias : ""); |
| 356 | return 0; |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | /* Binds the log to this run and this binary: the run id the qualifier set in |
| 361 | * OMEGA_NUMERIC_RUN_ID and the SHA-256 of /proc/self/exe, which the qualifier |
| 362 | * compares with the file it built. */ |
| 363 | static void print_run_line(void) { |
| 364 | const char *id = getenv("OMEGA_NUMERIC_RUN_ID"); |
| 365 | size_t n = id ? strlen(id) : 0; |
| 366 | if (n == 0 || n > 128) id = ""; |
| 367 | for (size_t i = 0; id[0] && i < n; i++) { |
| 368 | char ch = id[i]; |
| 369 | if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || |
| 370 | ch == '-' || ch == '_' || ch == '.')) { id = ""; break; } |
| 371 | } |
| 372 | char hex[65] = ""; |
| 373 | FILE *f = fopen("/proc/self/exe", "rb"); |
| 374 | if (f) { |
| 375 | sha256_ctx ctx; |
| 376 | uint8_t buf[65536], dig[SHA256_DIGEST_SIZE]; |
| 377 | size_t got; |
| 378 | sha256_init(&ctx); |
| 379 | while ((got = fread(buf, 1, sizeof(buf), f)) > 0) sha256_update(&ctx, buf, got); |
| 380 | if (!ferror(f)) { |
| 381 | sha256_final(&ctx, dig); |
| 382 | for (int i = 0; i < SHA256_DIGEST_SIZE; i++) snprintf(hex + 2 * i, 3, "%02x", dig[i]); |
| 383 | } |
| 384 | fclose(f); |
| 385 | } |
| 386 | printf("OMEGA_NUMERIC_RUN_JSON:{\"run_id\":\"%s\",\"binary_sha256\":\"%s\"}\n", id, hex); |
| 387 | } |
| 388 | |
| 389 | /* ---- Main ----------------------------------------------------------------------- */ |
| 390 | |
| 391 | /* ---- Independent oracle (tests/numeric_oracle.h) ---------------------------- */ |
| 392 | |
| 393 | /* Ulp bounds for the Omega EXP and LOG polynomials against the binary128 |
| 394 | * value rounded once to FP32. They are accuracy bounds, not bit-exactness: |
| 395 | * the degree-5 exp polynomial is not correctly rounded. Measured 2026-09-30 |
| 396 | * on every 257th FP32 bit pattern (16.7 million inputs): EXP worst 39 ulp |
| 397 | * (normal results, near |r| = ln2/2) and 19 ulp (subnormal results, near |
| 398 | * x = -87.68); LOG worst 3 ulp. On the 4096-element Gate 5 corpus: EXP 37, |
| 399 | * LOG 1. The bounds below are those sweep maxima plus one. */ |
| 400 | #define NUM_ORACLE_EXP_ULP 40u |
| 401 | #define NUM_ORACLE_LOG_ULP 4u |
| 402 | |
| 403 | static uint32_t oracle_value(OmegaNumericOp op, size_t i, const float *c) { |
| 404 | uint32_t a = U(g_a[i]), b = U(g_b[i]); |
| 405 | switch (op) { |
| 406 | case OMEGA_NOP_FADD: return or_add(a, b); |
| 407 | case OMEGA_NOP_FSUB: return or_sub(a, b); |
| 408 | case OMEGA_NOP_FMUL: return or_mul(a, b); |
| 409 | case OMEGA_NOP_FFMA: return or_fma(a, b, U(c[i])); |
| 410 | case OMEGA_NOP_I2FP: return or_i2f((int32_t)a); |
| 411 | case OMEGA_NOP_DIV: return or_div(a, b); |
| 412 | case OMEGA_NOP_SQRT: return or_sqrt(a); |
| 413 | case OMEGA_NOP_MUFU_RCP: return or_rcp(a); |
| 414 | case OMEGA_NOP_MUFU_RSQ: return or_rsq(a); |
| 415 | default: return OR_QNAN; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static void print_oracle_json(const char *op, const char *against, bool has_c, uint32_t c_bits, |
| 420 | size_t n, size_t bad, long first, uint32_t want, uint32_t got) { |
| 421 | printf("OMEGA_NUMERIC_ORACLE_JSON:{\"op\":\"%s\",\"oracle\":\"integer_softfloat_rne\",\"against\":\"%s\",", |
| 422 | op, against); |
| 423 | if (has_c) printf("\"c_bits\":\"0x%08x\",", c_bits); |
| 424 | printf("\"n\":%zu,\"mismatches\":%zu,", n, bad); |
| 425 | if (first >= 0) |
| 426 | printf("\"first_mismatch\":{\"index\":%ld,\"oracle\":\"0x%08x\",\"got\":\"0x%08x\"}}\n", first, want, got); |
| 427 | else |
| 428 | printf("\"first_mismatch\":null}\n"); |
| 429 | } |
| 430 | |
| 431 | /* Bit-exact ops: the reference and the CPU realization must both equal the |
| 432 | * integer soft-float answer on every element. */ |
| 433 | static bool oracle_exact(OmegaNumericOp op) { |
| 434 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 435 | size_t launches = (op == OMEGA_NOP_FFMA) ? FFMA_C_COUNT : 1; |
| 436 | bool ok = true; |
| 437 | for (size_t l = 0; l < launches; l++) { |
| 438 | const float *c = NULL; |
| 439 | if (op == OMEGA_NOP_FFMA) { fill_c(g_c, FFMA_C[l]); c = g_c; } |
| 440 | if (omega_numeric_reference(op, g_a, g_b, c, g_ref, N) != 0 || |
| 441 | omega_numeric_cpu_realize(op, g_a, g_b, c, g_cpu, N) != 0) { |
| 442 | printf("OMEGA_NUMERIC_ORACLE_JSON:{\"op\":\"%s\",\"error\":\"tier refused\"}\n", info->name); |
| 443 | ok = false; |
| 444 | continue; |
| 445 | } |
| 446 | const float *tier[2] = { g_ref, g_cpu }; |
| 447 | static const char *const tname[2] = { "reference", "cpu" }; |
| 448 | for (int t = 0; t < 2; t++) { |
| 449 | size_t bad = 0; long first = -1; uint32_t fw = 0, fg = 0; |
| 450 | for (size_t i = 0; i < N; i++) { |
| 451 | uint32_t want = oracle_value(op, i, c), got = U(tier[t][i]); |
| 452 | if (!or_same(want, got)) { |
| 453 | if (!bad) { first = (long)i; fw = want; fg = got; } |
| 454 | bad++; |
| 455 | } |
| 456 | } |
| 457 | print_oracle_json(info->name, tname[t], op == OMEGA_NOP_FFMA, |
| 458 | op == OMEGA_NOP_FFMA ? FFMA_C[l] : 0, N, bad, first, fw, fg); |
| 459 | if (bad) ok = false; |
| 460 | } |
| 461 | } |
| 462 | return ok; |
| 463 | } |
| 464 | |
| 465 | /* EXP / LOG: ulp distance from the binary128 answer rounded once. NaN |
| 466 | * results must match as a class; every other result must be within bound. */ |
| 467 | static bool oracle_ulp(OmegaNumericOp op, uint32_t bound) { |
| 468 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 469 | if (omega_numeric_reference(op, g_a, NULL, NULL, g_ref, N) != 0) { |
| 470 | printf("OMEGA_NUMERIC_ORACLE_JSON:{\"op\":\"%s\",\"error\":\"reference refused\"}\n", info->name); |
| 471 | return false; |
| 472 | } |
| 473 | uint32_t max_norm = 0, max_sub = 0; |
| 474 | size_t n_norm = 0, n_sub = 0, nan_bad = 0, over = 0; |
| 475 | long first = -1; uint32_t fw = 0, fg = 0; |
| 476 | for (size_t i = 0; i < N; i++) { |
| 477 | uint32_t x = U(g_a[i]); |
| 478 | uint32_t want = (op == OMEGA_NOP_EXP) ? or_exp(x) : or_log(x); |
| 479 | uint32_t got = U(g_ref[i]); |
| 480 | if (or_is_nan(want) || or_is_nan(got)) { |
| 481 | if (!(or_is_nan(want) && or_is_nan(got))) { |
| 482 | nan_bad++; |
| 483 | if (first < 0) { first = (long)i; fw = want; fg = got; } |
| 484 | } |
| 485 | continue; |
| 486 | } |
| 487 | uint32_t d = or_ulp_distance(want, got); |
| 488 | bool sub = (want & 0x7f800000u) == 0; /* subnormal or zero result */ |
| 489 | if (sub) { n_sub++; if (d > max_sub) max_sub = d; } |
| 490 | else { n_norm++; if (d > max_norm) max_norm = d; } |
| 491 | if (d > bound) { |
| 492 | over++; |
| 493 | if (first < 0) { first = (long)i; fw = want; fg = got; } |
| 494 | } |
| 495 | } |
| 496 | printf("OMEGA_NUMERIC_ORACLE_JSON:{\"op\":\"%s\",\"oracle\":\"binary128_series_rounded_once\"," |
| 497 | "\"against\":\"reference\",\"compare\":\"ULP_BOUND\",\"bound_ulp\":%u,\"n\":%u," |
| 498 | "\"normal_or_inf_results\":%zu,\"max_ulp_normal_or_inf\":%u," |
| 499 | "\"subnormal_or_zero_results\":%zu,\"max_ulp_subnormal_or_zero\":%u," |
| 500 | "\"nan_class_mismatches\":%zu,\"over_bound\":%zu,", |
| 501 | info->name, bound, N, n_norm, max_norm, n_sub, max_sub, nan_bad, over); |
| 502 | if (first >= 0) |
| 503 | printf("\"first_violation\":{\"index\":%ld,\"input\":\"0x%08x\",\"oracle\":\"0x%08x\",\"got\":\"0x%08x\"}}\n", |
| 504 | first, U(g_a[first]), fw, fg); |
| 505 | else |
| 506 | printf("\"first_violation\":null}\n"); |
| 507 | return nan_bad == 0 && over == 0 && n_norm > 0 && n_sub > 0; |
| 508 | } |
| 509 | |
| 510 | /* LDS_STS: a host has no shared memory, so check the declared permutation |
| 511 | * itself on index-tagged data: out[i] carries tag (i ^ 63), and applying the |
| 512 | * op twice gives the input back. */ |
| 513 | static bool oracle_lds(void) { |
| 514 | static float tag[N], once[N], twice[N], cpu[N]; |
| 515 | for (size_t i = 0; i < N; i++) tag[i] = F(0x3f800000u + (uint32_t)i); |
| 516 | if (omega_numeric_reference(OMEGA_NOP_LDS_STS, tag, NULL, NULL, once, N) != 0 || |
| 517 | omega_numeric_reference(OMEGA_NOP_LDS_STS, once, NULL, NULL, twice, N) != 0 || |
| 518 | omega_numeric_cpu_realize(OMEGA_NOP_LDS_STS, tag, NULL, NULL, cpu, N) != 0) return false; |
| 519 | size_t bad = 0, fixed = 0, not_inv = 0; |
| 520 | for (size_t i = 0; i < N; i++) { |
| 521 | size_t base = i & ~(size_t)63, want = base + (63 - (i & 63)); |
| 522 | if (U(once[i]) != 0x3f800000u + (uint32_t)want || U(cpu[i]) != U(once[i])) bad++; |
| 523 | if (U(once[i]) == U(tag[i])) fixed++; |
| 524 | if (U(twice[i]) != U(tag[i])) not_inv++; |
| 525 | } |
| 526 | printf("OMEGA_NUMERIC_ORACLE_JSON:{\"op\":\"LDS_STS\",\"oracle\":\"index_tagged_mirror\",\"n\":%u," |
| 527 | "\"wrong_source\":%zu,\"fixed_points\":%zu,\"not_involution\":%zu}\n", N, bad, fixed, not_inv); |
| 528 | return bad == 0 && fixed == 0 && not_inv == 0; |
| 529 | } |
| 530 | |
| 531 | static bool oracle_tier(void) { |
| 532 | static const OmegaNumericOp EXACT[] = { |
| 533 | OMEGA_NOP_FADD, OMEGA_NOP_FSUB, OMEGA_NOP_FMUL, OMEGA_NOP_FFMA, OMEGA_NOP_I2FP, |
| 534 | OMEGA_NOP_DIV, OMEGA_NOP_SQRT, OMEGA_NOP_MUFU_RCP, OMEGA_NOP_MUFU_RSQ, |
| 535 | }; |
| 536 | bool ok = true; |
| 537 | for (size_t k = 0; k < sizeof(EXACT) / sizeof(EXACT[0]); k++) |
| 538 | if (!oracle_exact(EXACT[k])) { ok = false; printf(" oracle mismatch: %s\n", omega_numeric_op_at(EXACT[k])->name); } |
| 539 | if (!oracle_ulp(OMEGA_NOP_EXP, NUM_ORACLE_EXP_ULP)) { ok = false; printf(" oracle bound: EXP\n"); } |
| 540 | if (!oracle_ulp(OMEGA_NOP_LOG, NUM_ORACLE_LOG_ULP)) { ok = false; printf(" oracle bound: LOG\n"); } |
| 541 | if (!oracle_lds()) { ok = false; printf(" oracle: LDS_STS permutation\n"); } |
| 542 | return ok; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | |
| 547 | /* ---- E1 scalar contract (docs/numeric/E1_SCALAR_CONTRACT.md) ------------------------ |
| 548 | * Three derivations per element: the integer reference (src), the CPU tier |
| 549 | * (AArch64 instructions, src) and the second integer oracle (numeric_oracle.h). |
| 550 | * Results are compared with omega_numeric_compare_equal under the op's mode. */ |
| 551 | |
| 552 | #define E1_FIRST OMEGA_NOP_FSETP_LT_SEL |
| 553 | #define E1_PRED_LAST OMEGA_NOP_FSETP_NEU_SEL |
| 554 | #define E1_BATCH 65536u |
| 555 | #ifndef E1_RANDOM_PER_OP |
| 556 | #define E1_RANDOM_PER_OP (1u << 22) |
| 557 | #endif |
| 558 | |
| 559 | /* Predicate per FSETP_*_SEL op, in enum order; checked against the op name. */ |
| 560 | static const char *const E1_PRED[] = { "LT", "LE", "GT", "EQ", "NE", "NUM", "NAN", |
| 561 | "LTU", "LEU", "GTU", "GEU", "EQU", "NEU" }; |
| 562 | |
| 563 | static bool e1_is_pred(OmegaNumericOp op) { return op >= E1_FIRST && op <= E1_PRED_LAST; } |
| 564 | |
| 565 | static bool e1_pred_table_ok(void) { |
| 566 | if (sizeof(E1_PRED) / sizeof(E1_PRED[0]) != (size_t)(E1_PRED_LAST - E1_FIRST + 1)) return false; |
| 567 | for (int op = E1_FIRST; op <= E1_PRED_LAST; op++) { |
| 568 | char want[32]; |
| 569 | snprintf(want, sizeof(want), "FSETP_%s_SEL", E1_PRED[op - E1_FIRST]); |
| 570 | if (strcmp(want, omega_numeric_op_at((size_t)op)->name) != 0) return false; |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | static uint32_t e1_oracle(OmegaNumericOp op, uint32_t a, uint32_t b, uint32_t c) { |
| 576 | if (e1_is_pred(op)) return or_pred(E1_PRED[op - E1_FIRST], a, b) ? a : b; |
| 577 | switch (op) { |
| 578 | case OMEGA_NOP_F2I_FLOOR: return (uint32_t)(int32_t)or_to_int(a, OR_FLOOR, INT32_MIN, INT32_MAX); |
| 579 | case OMEGA_NOP_F2I_CEIL: return (uint32_t)(int32_t)or_to_int(a, OR_CEIL, INT32_MIN, INT32_MAX); |
| 580 | case OMEGA_NOP_F2I_RNI: return (uint32_t)(int32_t)or_to_int(a, OR_RNE, INT32_MIN, INT32_MAX); |
| 581 | case OMEGA_NOP_F2U: return (uint32_t)or_to_int(a, OR_RTZ, 0, UINT32_MAX); |
| 582 | case OMEGA_NOP_I2FP_U32: return or_u2f(a); |
| 583 | case OMEGA_NOP_F32_TO_F16: return or_f32_to_f16(a); |
| 584 | case OMEGA_NOP_F32_TO_BF16: return or_f32_to_bf16(a); |
| 585 | case OMEGA_NOP_F16_TO_F32: return or_f16_to_f32(a & 0xffffu); |
| 586 | case OMEGA_NOP_BF16_TO_F32: return or_bf16_to_f32(a & 0xffffu); |
| 587 | case OMEGA_NOP_FFMA_V: return or_fma(a, b, c); |
| 588 | default: return OR_QNAN; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | typedef struct { |
| 593 | size_t n, cpu_vs_ref, oracle_vs_ref, oracle_vs_cpu, refused; |
| 594 | long first; uint32_t fa, fb, fc, fref, fcpu, forc; |
| 595 | } E1Count; |
| 596 | |
| 597 | static float g_e1a[E1_BATCH], g_e1b[E1_BATCH], g_e1c[E1_BATCH], g_e1r[E1_BATCH], g_e1p[E1_BATCH]; |
| 598 | |
| 599 | /* Runs n (<= E1_BATCH) elements of g_e1a/b/c through all three derivations. */ |
| 600 | static void e1_batch(OmegaNumericOp op, size_t n, E1Count *k) { |
| 601 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 602 | const float *b = info->arity >= 2 ? g_e1b : NULL, *c = info->arity >= 3 ? g_e1c : NULL; |
| 603 | if (omega_numeric_reference(op, g_e1a, b, c, g_e1r, n) != 0 || |
| 604 | omega_numeric_cpu_realize(op, g_e1a, b, c, g_e1p, n) != 0) { k->refused++; return; } |
| 605 | for (size_t i = 0; i < n; i++) { |
| 606 | uint32_t ua = U(g_e1a[i]), ub = U(g_e1b[i]), uc = U(g_e1c[i]); |
| 607 | uint32_t r = U(g_e1r[i]), p = U(g_e1p[i]), o = e1_oracle(op, ua, ub, uc); |
| 608 | bool bad = false; |
| 609 | if (!omega_numeric_compare_equal(info->compare, r, p)) { k->cpu_vs_ref++; bad = true; } |
| 610 | if (!omega_numeric_compare_equal(info->compare, o, r)) { k->oracle_vs_ref++; bad = true; } |
| 611 | if (!omega_numeric_compare_equal(info->compare, o, p)) { k->oracle_vs_cpu++; bad = true; } |
| 612 | if (bad && k->first < 0) { |
| 613 | k->first = (long)(k->n + i); k->fa = ua; k->fb = ub; k->fc = uc; |
| 614 | k->fref = r; k->fcpu = p; k->forc = o; |
| 615 | } |
| 616 | } |
| 617 | k->n += n; |
| 618 | } |
| 619 | |
| 620 | static void e1_print(const char *what, OmegaNumericOp op, const E1Count *k) { |
| 621 | printf("OMEGA_NUMERIC_E1_JSON:{\"op\":\"%s\",\"set\":\"%s\",\"n\":%zu,\"cpu_vs_ref\":%zu," |
| 622 | "\"oracle_vs_ref\":%zu,\"oracle_vs_cpu\":%zu,\"refused\":%zu,", |
| 623 | omega_numeric_op_at(op)->name, what, k->n, k->cpu_vs_ref, k->oracle_vs_ref, k->oracle_vs_cpu, k->refused); |
| 624 | if (k->first >= 0) |
| 625 | printf("\"first\":{\"index\":%ld,\"a\":\"0x%08x\",\"b\":\"0x%08x\",\"c\":\"0x%08x\"," |
| 626 | "\"ref\":\"0x%08x\",\"cpu\":\"0x%08x\",\"oracle\":\"0x%08x\"}}\n", |
| 627 | k->first, k->fa, k->fb, k->fc, k->fref, k->fcpu, k->forc); |
| 628 | else |
| 629 | printf("\"first\":null}\n"); |
| 630 | } |
| 631 | |
| 632 | /* Structured random FP32 pattern: half plain random bits, half aimed at the |
| 633 | * places the E1 ops round, saturate or change class. */ |
| 634 | static uint32_t e1_pattern(uint32_t *s) { |
| 635 | uint32_t r = lcg_next(s), q = lcg_next(s), sign = q & OR_SIGN; |
| 636 | switch (r & 15u) { |
| 637 | case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: |
| 638 | return q ^ (lcg_next(s) >> 7); /* any bits */ |
| 639 | case 8: return sign | ((118u + (r >> 4) % 44u) << 23) | (lcg_next(s) & 0x7fffffu); /* |x| ~ 2^-9 .. 2^34 */ |
| 640 | case 9: return sign | ((96u + (r >> 4) % 48u) << 23) | (lcg_next(s) & 0x7fe000u) | 0x1000u; /* F16 ties */ |
| 641 | case 10: return (lcg_next(s) & 0xffff0000u) | 0x8000u; /* BF16 ties */ |
| 642 | case 11: { /* k + 1/2: integer ties */ |
| 643 | int32_t k = (int32_t)(lcg_next(s) >> ((r >> 4) % 31u)) * ((q & 1u) ? -1 : 1); |
| 644 | return U((float)k + 0.5f); |
| 645 | } |
| 646 | case 12: return sign | (lcg_next(s) & 0x00ffffffu); /* subnormals and the smallest normals */ |
| 647 | case 13: return sign | ((254u - (r >> 4) % 4u) << 23) | (lcg_next(s) & 0x7fffffu); /* near max */ |
| 648 | case 14: return (r & 0x100u) ? (sign | 0x7f800000u | (lcg_next(s) & 0x7fffffu)) : (sign | 0x7f800000u); /* NaN / inf */ |
| 649 | default: return U((float)(lcg_next(s) >> ((r >> 4) % 32u))); /* integers, exactly or rounded */ |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* Second and third operand, aimed at equality, sign and cancellation. */ |
| 654 | static void e1_partners(uint32_t *s, uint32_t a, uint32_t *b, uint32_t *c) { |
| 655 | uint32_t r = lcg_next(s); |
| 656 | switch (r & 7u) { |
| 657 | case 0: *b = a; break; |
| 658 | case 1: *b = a ^ OR_SIGN; break; |
| 659 | case 2: *b = a + 1u; break; |
| 660 | case 3: *b = a - 1u; break; |
| 661 | default: *b = e1_pattern(s); break; |
| 662 | } |
| 663 | switch ((r >> 3) & 3u) { |
| 664 | case 0: *c = U(-(F(a) * F(*b))); break; /* near-total cancellation */ |
| 665 | case 1: *c = U(F(a) * F(*b)) ^ ((r >> 5) & 1u); break; /* same sign, last-bit nudge */ |
| 666 | default: *c = e1_pattern(s); break; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* Random differential over every E1 op. Returns the two verdicts. */ |
| 671 | static void e1_random(bool *cpu_ok, bool *oracle_ok) { |
| 672 | *cpu_ok = *oracle_ok = true; |
| 673 | for (int op = E1_FIRST; op < OMEGA_NOP_COUNT; op++) { |
| 674 | E1Count k; memset(&k, 0, sizeof(k)); k.first = -1; |
| 675 | uint32_t s = 0x9e3779b9u ^ (uint32_t)op * 0x85ebca6bu; |
| 676 | for (size_t done = 0; done < E1_RANDOM_PER_OP; done += E1_BATCH) { |
| 677 | for (size_t i = 0; i < E1_BATCH; i++) { |
| 678 | uint32_t a = e1_pattern(&s), b, c; |
| 679 | e1_partners(&s, a, &b, &c); |
| 680 | g_e1a[i] = F(a); g_e1b[i] = F(b); g_e1c[i] = F(c); |
| 681 | } |
| 682 | e1_batch((OmegaNumericOp)op, E1_BATCH, &k); |
| 683 | } |
| 684 | e1_print("random", (OmegaNumericOp)op, &k); |
| 685 | if (k.cpu_vs_ref || k.refused) *cpu_ok = false; |
| 686 | if (k.oracle_vs_ref || k.oracle_vs_cpu || k.refused) *oracle_ok = false; |
| 687 | } |
| 688 | /* The widening ops read 16 bits: all 65536 patterns, with junk above. */ |
| 689 | for (int op = OMEGA_NOP_F16_TO_F32; op <= OMEGA_NOP_BF16_TO_F32; op++) { |
| 690 | E1Count k; memset(&k, 0, sizeof(k)); k.first = -1; |
| 691 | for (uint32_t h = 0; h < 65536u; h++) g_e1a[h] = F(h | ((h * 0x9e37u) << 16)); |
| 692 | e1_batch((OmegaNumericOp)op, 65536u, &k); |
| 693 | e1_print("exhaustive16", (OmegaNumericOp)op, &k); |
| 694 | if (k.cpu_vs_ref || k.refused) *cpu_ok = false; |
| 695 | if (k.oracle_vs_ref || k.oracle_vs_cpu || k.refused) *oracle_ok = false; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* Hand-worked boundary values (expected bits written out, not computed). */ |
| 700 | typedef struct { OmegaNumericOp op; uint32_t a, b, c, want; } E1Case; |
| 701 | static const E1Case E1_CASES[] = { |
| 702 | { OMEGA_NOP_F2I_FLOOR, 0x80000001u, 0, 0, 0xffffffffu }, /* -min subnormal -> -1 */ |
| 703 | { OMEGA_NOP_F2I_FLOOR, 0xbfc00000u, 0, 0, 0xfffffffeu }, /* -1.5 -> -2 */ |
| 704 | { OMEGA_NOP_F2I_FLOOR, 0x4f000000u, 0, 0, 0x7fffffffu }, /* 2^31 saturates */ |
| 705 | { OMEGA_NOP_F2I_FLOOR, 0xcf000000u, 0, 0, 0x80000000u }, /* -2^31 exact */ |
| 706 | { OMEGA_NOP_F2I_FLOOR, 0xff800000u, 0, 0, 0x80000000u }, /* -inf */ |
| 707 | { OMEGA_NOP_F2I_FLOOR, 0x7fc00000u, 0, 0, 0x00000000u }, /* NaN -> 0 */ |
| 708 | { OMEGA_NOP_F2I_CEIL, 0x00000001u, 0, 0, 0x00000001u }, /* min subnormal -> 1 */ |
| 709 | { OMEGA_NOP_F2I_CEIL, 0xbf000000u, 0, 0, 0x00000000u }, /* -0.5 -> 0 */ |
| 710 | { OMEGA_NOP_F2I_CEIL, 0x3fc00000u, 0, 0, 0x00000002u }, /* 1.5 -> 2 */ |
| 711 | { OMEGA_NOP_F2I_RNI, 0x40200000u, 0, 0, 0x00000002u }, /* 2.5 -> 2 */ |
| 712 | { OMEGA_NOP_F2I_RNI, 0xc0200000u, 0, 0, 0xfffffffeu }, /* -2.5 -> -2 */ |
| 713 | { OMEGA_NOP_F2I_RNI, 0x3fc00000u, 0, 0, 0x00000002u }, /* 1.5 -> 2 */ |
| 714 | { OMEGA_NOP_F2I_RNI, 0x3f000000u, 0, 0, 0x00000000u }, /* 0.5 -> 0 */ |
| 715 | { OMEGA_NOP_F2I_RNI, 0x4affffffu, 0, 0, 0x00800000u }, /* 8388607.5 -> 8388608 */ |
| 716 | { OMEGA_NOP_F2U, 0xbf800000u, 0, 0, 0x00000000u }, /* -1 -> 0 */ |
| 717 | { OMEGA_NOP_F2U, 0x4f800000u, 0, 0, 0xffffffffu }, /* 2^32 saturates */ |
| 718 | { OMEGA_NOP_F2U, 0x4f7fffffu, 0, 0, 0xffffff00u }, /* largest float below 2^32 */ |
| 719 | { OMEGA_NOP_F2U, 0x4079999au, 0, 0, 0x00000003u }, /* 3.9 -> 3 */ |
| 720 | { OMEGA_NOP_F2U, 0xffc00000u, 0, 0, 0x00000000u }, /* NaN -> 0 */ |
| 721 | { OMEGA_NOP_I2FP_U32, 0x01000001u, 0, 0, 0x4b800000u }, /* 2^24+1: tie to even, down */ |
| 722 | { OMEGA_NOP_I2FP_U32, 0x01000003u, 0, 0, 0x4b800002u }, /* 2^24+3: tie to even, up */ |
| 723 | { OMEGA_NOP_I2FP_U32, 0xffffff80u, 0, 0, 0x4f800000u }, /* tie up to 2^32 */ |
| 724 | { OMEGA_NOP_I2FP_U32, 0x80000000u, 0, 0, 0x4f000000u }, /* unsigned, not -2^31 */ |
| 725 | { OMEGA_NOP_F32_TO_F16, 0x477ff000u, 0, 0, 0x7c00u }, /* 65520: tie, rounds to inf */ |
| 726 | { OMEGA_NOP_F32_TO_F16, 0x477fefffu, 0, 0, 0x7bffu }, /* just below: 65504 */ |
| 727 | { OMEGA_NOP_F32_TO_F16, 0x33000000u, 0, 0, 0x0000u }, /* 2^-25: tie to 0 */ |
| 728 | { OMEGA_NOP_F32_TO_F16, 0x33000001u, 0, 0, 0x0001u }, /* above the tie */ |
| 729 | { OMEGA_NOP_F32_TO_F16, 0x33c00000u, 0, 0, 0x0002u }, /* 1.5 * 2^-24: tie to even */ |
| 730 | { OMEGA_NOP_F32_TO_F16, 0x3f801000u, 0, 0, 0x3c00u }, /* 1 + half ulp: even, down */ |
| 731 | { OMEGA_NOP_F32_TO_F16, 0x3f803000u, 0, 0, 0x3c02u }, /* 1 + 1.5 ulp: even, up */ |
| 732 | { OMEGA_NOP_F32_TO_F16, 0x80000000u, 0, 0, 0x8000u }, /* -0 */ |
| 733 | { OMEGA_NOP_F32_TO_F16, 0x38800000u, 0, 0, 0x0400u }, /* 2^-14: smallest normal */ |
| 734 | { OMEGA_NOP_F32_TO_F16, 0x7fc00000u, 0, 0, 0x7e00u }, /* NaN stays NaN */ |
| 735 | { OMEGA_NOP_F32_TO_BF16, 0x3f808000u, 0, 0, 0x3f80u }, /* tie, even, down */ |
| 736 | { OMEGA_NOP_F32_TO_BF16, 0x3f818000u, 0, 0, 0x3f82u }, /* tie, even, up */ |
| 737 | { OMEGA_NOP_F32_TO_BF16, 0x7f7fffffu, 0, 0, 0x7f80u }, /* max float rounds to inf */ |
| 738 | { OMEGA_NOP_F32_TO_BF16, 0x00018000u, 0, 0, 0x0002u }, /* subnormal tie, kept */ |
| 739 | { OMEGA_NOP_F32_TO_BF16, 0xff800001u, 0, 0, 0xffc0u }, /* NaN stays NaN */ |
| 740 | { OMEGA_NOP_F16_TO_F32, 0x0001u, 0, 0, 0x33800000u }, /* 2^-24 */ |
| 741 | { OMEGA_NOP_F16_TO_F32, 0x03ffu, 0, 0, 0x387fc000u }, /* largest subnormal */ |
| 742 | { OMEGA_NOP_F16_TO_F32, 0x7bffu, 0, 0, 0x477fe000u }, /* 65504 */ |
| 743 | { OMEGA_NOP_F16_TO_F32, 0xfc00u, 0, 0, 0xff800000u }, /* -inf */ |
| 744 | { OMEGA_NOP_F16_TO_F32, 0xabcd3c00u, 0, 0, 0x3f800000u }, /* bits [31:16] ignored */ |
| 745 | { OMEGA_NOP_BF16_TO_F32, 0x0001u, 0, 0, 0x00010000u }, /* subnormal kept */ |
| 746 | { OMEGA_NOP_BF16_TO_F32, 0x7f80u, 0, 0, 0x7f800000u }, /* inf */ |
| 747 | { OMEGA_NOP_BF16_TO_F32, 0x12343f80u, 0, 0, 0x3f800000u },/* bits [31:16] ignored */ |
| 748 | { OMEGA_NOP_FFMA_V, 0x3f800800u, 0x3f800800u, 0xbf800000u, 0x3a000400u }, /* one rounding */ |
| 749 | { OMEGA_NOP_FFMA_V, 0x7f7fffffu, 0x40000000u, 0xff7fffffu, 0x7f7fffffu }, /* no overflow inside */ |
| 750 | { OMEGA_NOP_FFMA_V, 0x1a000000u, 0x1a000000u, 0x80000000u, 0x00000000u }, /* 2^-150 + -0 -> +0 */ |
| 751 | { OMEGA_NOP_FFMA_V, 0x0d800000u, 0x2b800000u, 0x00000000u, 0x00000200u }, /* subnormal 2^-140 */ |
| 752 | { OMEGA_NOP_FFMA_V, 0x7f800000u, 0x00000000u, 0x3f800000u, 0x7fc00000u }, /* inf * 0 -> NaN */ |
| 753 | { OMEGA_NOP_FFMA_V, 0x3f800800u, 0x3f800800u, 0x00000001u, 0x3f801001u }, /* 1+2^-11+2^-24 tie broken up by c = 2^-149 (sticky bit) */ |
| 754 | { OMEGA_NOP_FFMA_V, 0x3f800800u, 0x3f800800u, 0x80000001u, 0x3f801000u }, /* the same tie broken down by c = -2^-149 */ |
| 755 | }; |
| 756 | |
| 757 | /* Predicate truth per relation class, bits L E G U (a<b, a==b, a>b, unordered). */ |
| 758 | static const uint8_t E1_PRED_TRUTH[] = { |
| 759 | 0x8, 0xc, 0x2, 0x4, 0xa, 0xe, 0x1, /* LT LE GT EQ NE NUM NAN */ |
| 760 | 0x9, 0xd, 0x3, 0x7, 0x5, 0xb, /* LTU LEU GTU GEU EQU NEU */ |
| 761 | }; |
| 762 | typedef struct { uint32_t a, b; uint8_t rel; } E1Pair; /* rel: 8 L, 4 E, 2 G, 1 U */ |
| 763 | static const E1Pair E1_PAIRS[] = { |
| 764 | { 0x3f800000u, 0x40000000u, 8 }, { 0x40000000u, 0x3f800000u, 2 }, |
| 765 | { 0x80000000u, 0x00000000u, 4 }, { 0x00000000u, 0x80000000u, 4 }, |
| 766 | { 0x7fc00000u, 0x3f800000u, 1 }, { 0x3f800000u, 0xffc00001u, 1 }, |
| 767 | { 0x7f800001u, 0x7f800001u, 1 }, { 0xff800000u, 0x7f800000u, 8 }, |
| 768 | { 0x7f800000u, 0x7f800000u, 4 }, { 0x00000001u, 0x80000000u, 2 }, |
| 769 | { 0xc0000000u, 0xbf800000u, 8 }, { 0x80000001u, 0x00000001u, 8 }, |
| 770 | }; |
| 771 | |
| 772 | static bool e1_boundary(void) { |
| 773 | bool ok = e1_pred_table_ok(); |
| 774 | if (!ok) printf(" E1 predicate table does not match the op names\n"); |
| 775 | size_t cases = 0, bad = 0; |
| 776 | float a[1], b[1], c[1], r[1], p[1]; |
| 777 | for (size_t t = 0; t < sizeof(E1_CASES) / sizeof(E1_CASES[0]) + |
| 778 | (sizeof(E1_PRED_TRUTH)) * (sizeof(E1_PAIRS) / sizeof(E1_PAIRS[0])); t++) { |
| 779 | OmegaNumericOp op; uint32_t ua, ub = 0, uc = 0, want; |
| 780 | if (t < sizeof(E1_CASES) / sizeof(E1_CASES[0])) { |
| 781 | const E1Case *e = &E1_CASES[t]; |
| 782 | op = e->op; ua = e->a; ub = e->b; uc = e->c; want = e->want; |
| 783 | } else { |
| 784 | size_t k = t - sizeof(E1_CASES) / sizeof(E1_CASES[0]); |
| 785 | size_t np = sizeof(E1_PAIRS) / sizeof(E1_PAIRS[0]); |
| 786 | size_t pi = k / np; const E1Pair *pr = &E1_PAIRS[k % np]; |
| 787 | op = (OmegaNumericOp)(E1_FIRST + (int)pi); |
| 788 | ua = pr->a; ub = pr->b; |
| 789 | want = (E1_PRED_TRUTH[pi] & pr->rel) ? ua : ub; |
| 790 | } |
| 791 | const OmegaNumericOpInfo *info = omega_numeric_op_at(op); |
| 792 | a[0] = F(ua); b[0] = F(ub); c[0] = F(uc); |
| 793 | int rr = omega_numeric_reference(op, a, info->arity >= 2 ? b : NULL, info->arity >= 3 ? c : NULL, r, 1); |
| 794 | int pr = omega_numeric_cpu_realize(op, a, info->arity >= 2 ? b : NULL, info->arity >= 3 ? c : NULL, p, 1); |
| 795 | uint32_t o = e1_oracle(op, ua, ub, uc); |
| 796 | cases++; |
| 797 | if (rr || pr || !omega_numeric_compare_equal(info->compare, want, U(r[0])) || |
| 798 | !omega_numeric_compare_equal(info->compare, want, U(p[0])) || |
| 799 | !omega_numeric_compare_equal(info->compare, want, o)) { |
| 800 | bad++; |
| 801 | printf(" E1 boundary %s a=0x%08x b=0x%08x c=0x%08x want 0x%08x ref 0x%08x cpu 0x%08x oracle 0x%08x\n", |
| 802 | info->name, ua, ub, uc, want, U(r[0]), U(p[0]), o); |
| 803 | } |
| 804 | } |
| 805 | printf("OMEGA_NUMERIC_E1_JSON:{\"set\":\"boundary\",\"cases\":%zu,\"bad\":%zu}\n", cases, bad); |
| 806 | return ok && bad == 0; |
| 807 | } |
| 808 | |
| 809 | /* --e1-exhaustive: every 2^32 input pattern of each unary E1 op through all |
| 810 | * three derivations, one child process per op. Not part of the Gate 5 IDs. */ |
| 811 | static int e1_exhaustive(void) { |
| 812 | static const OmegaNumericOp OPS[] = { OMEGA_NOP_F2I_FLOOR, OMEGA_NOP_F2I_CEIL, OMEGA_NOP_F2I_RNI, |
| 813 | OMEGA_NOP_F2U, OMEGA_NOP_I2FP_U32, OMEGA_NOP_F32_TO_F16, |
| 814 | OMEGA_NOP_F32_TO_BF16, OMEGA_NOP_F16_TO_F32, OMEGA_NOP_BF16_TO_F32 }; |
| 815 | const size_t nops = sizeof(OPS) / sizeof(OPS[0]); |
| 816 | pid_t pids[sizeof(OPS) / sizeof(OPS[0])]; |
| 817 | fflush(stdout); |
| 818 | for (size_t j = 0; j < nops; j++) { |
| 819 | pids[j] = fork(); |
| 820 | if (pids[j] < 0) { perror("fork"); return 1; } |
| 821 | if (pids[j] == 0) { |
| 822 | E1Count k; memset(&k, 0, sizeof(k)); k.first = -1; |
| 823 | for (uint64_t base = 0; base < (1ull << 32); base += E1_BATCH) { |
| 824 | for (uint32_t i = 0; i < E1_BATCH; i++) g_e1a[i] = F((uint32_t)(base + i)); |
| 825 | e1_batch(OPS[j], E1_BATCH, &k); |
| 826 | } |
| 827 | e1_print("exhaustive32", OPS[j], &k); |
| 828 | fflush(stdout); |
| 829 | _exit((k.cpu_vs_ref || k.oracle_vs_ref || k.oracle_vs_cpu || k.refused || k.n != (1ull << 32)) ? 1 : 0); |
| 830 | } |
| 831 | } |
| 832 | int fails = 0; |
| 833 | for (size_t j = 0; j < nops; j++) { |
| 834 | int st = 0; |
| 835 | if (waitpid(pids[j], &st, 0) < 0 || !WIFEXITED(st) || WEXITSTATUS(st) != 0) fails++; |
| 836 | } |
| 837 | printf("E1 exhaustive verdict: %s (%zu ops x 2^32 inputs, %d failed)\n", fails ? "FAIL" : "PASS", nops, fails); |
| 838 | return fails ? 1 : 0; |
| 839 | } |
| 840 | |
| 841 | /* --ulp-exhaustive EXP|LOG: ulp distance of omega_math_exp / omega_math_log |
| 842 | * from the binary128 oracle (or_exp / or_log, rounded once) on every 2^32 |
| 843 | * input, in OMEGA_ULP_PROCS (default 5) child processes that interleave |
| 844 | * 2^16-input blocks. Prints the maxima, the smallest input bit pattern that |
| 845 | * reaches each maximum, and a distance histogram; exits 0 only when every |
| 846 | * input was visited, NaN classes match and no distance exceeds the declared |
| 847 | * bound. Not part of the Gate 5 IDs. */ |
| 848 | typedef struct { |
| 849 | uint64_t n, n_norm, n_sub, nan_bad, over, hist[65]; |
| 850 | uint32_t max_norm, arg_norm, max_sub, arg_sub; |
| 851 | } UlpCount; |
| 852 | |
| 853 | static void ulp_take(uint32_t d, uint32_t x, uint32_t *max, uint32_t *arg) { |
| 854 | if (d > *max || (d == *max && x < *arg)) { *max = d; *arg = x; } |
| 855 | } |
| 856 | |
| 857 | static int ulp_exhaustive(const char *name) { |
| 858 | OmegaNumericOp op; |
| 859 | uint32_t bound; |
| 860 | if (strcmp(name, "EXP") == 0) { op = OMEGA_NOP_EXP; bound = NUM_ORACLE_EXP_ULP; } |
| 861 | else if (strcmp(name, "LOG") == 0) { op = OMEGA_NOP_LOG; bound = NUM_ORACLE_LOG_ULP; } |
| 862 | else { fprintf(stderr, "--ulp-exhaustive: op must be EXP or LOG\n"); return 2; } |
| 863 | const char *pe = getenv("OMEGA_ULP_PROCS"); |
| 864 | int procs = pe ? atoi(pe) : 5; |
| 865 | if (procs < 1 || procs > 64) { fprintf(stderr, "OMEGA_ULP_PROCS must be 1..64\n"); return 2; } |
| 866 | UlpCount *c = mmap(NULL, sizeof(UlpCount) * (size_t)procs, PROT_READ | PROT_WRITE, |
| 867 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 868 | if (c == MAP_FAILED) { perror("mmap"); return 1; } |
| 869 | memset(c, 0, sizeof(UlpCount) * (size_t)procs); |
| 870 | pid_t pids[64]; |
| 871 | fflush(stdout); |
| 872 | for (int j = 0; j < procs; j++) { |
| 873 | pids[j] = fork(); |
| 874 | if (pids[j] < 0) { perror("fork"); return 1; } |
| 875 | if (pids[j] != 0) continue; |
| 876 | UlpCount *k = &c[j]; |
| 877 | for (uint64_t blk = (uint64_t)j; blk < (1ull << 16); blk += (uint64_t)procs) { |
| 878 | for (uint32_t i = 0; i < (1u << 16); i++) { |
| 879 | uint32_t x = (uint32_t)(blk << 16) | i; |
| 880 | uint32_t want = op == OMEGA_NOP_EXP ? or_exp(x) : or_log(x); |
| 881 | uint32_t got = U(op == OMEGA_NOP_EXP ? omega_math_exp(F(x)) : omega_math_log(F(x))); |
| 882 | k->n++; |
| 883 | if (or_is_nan(want) || or_is_nan(got)) { |
| 884 | if (!(or_is_nan(want) && or_is_nan(got))) k->nan_bad++; |
| 885 | continue; |
| 886 | } |
| 887 | uint32_t d = or_ulp_distance(want, got); |
| 888 | k->hist[d < 64 ? d : 64]++; |
| 889 | if (d > bound) k->over++; |
| 890 | if ((want & 0x7f800000u) == 0) { k->n_sub++; ulp_take(d, x, &k->max_sub, &k->arg_sub); } |
| 891 | else { k->n_norm++; ulp_take(d, x, &k->max_norm, &k->arg_norm); } |
| 892 | } |
| 893 | } |
| 894 | _exit(0); |
| 895 | } |
| 896 | int fails = 0; |
| 897 | for (int j = 0; j < procs; j++) { |
| 898 | int st = 0; |
| 899 | if (waitpid(pids[j], &st, 0) < 0 || !WIFEXITED(st) || WEXITSTATUS(st) != 0) fails++; |
| 900 | } |
| 901 | UlpCount t; |
| 902 | memset(&t, 0, sizeof(t)); |
| 903 | t.arg_norm = t.arg_sub = 0xffffffffu; |
| 904 | for (int j = 0; j < procs; j++) { |
| 905 | t.n += c[j].n; t.n_norm += c[j].n_norm; t.n_sub += c[j].n_sub; |
| 906 | t.nan_bad += c[j].nan_bad; t.over += c[j].over; |
| 907 | for (int h = 0; h < 65; h++) t.hist[h] += c[j].hist[h]; |
| 908 | if (c[j].n_norm) ulp_take(c[j].max_norm, c[j].arg_norm, &t.max_norm, &t.arg_norm); |
| 909 | if (c[j].n_sub) ulp_take(c[j].max_sub, c[j].arg_sub, &t.max_sub, &t.arg_sub); |
| 910 | } |
| 911 | printf("OMEGA_NUMERIC_ULP_JSON:{\"op\":\"%s\",\"oracle\":\"binary128_series_rounded_once\",\"inputs\":%llu," |
| 912 | "\"bound_ulp\":%u,\"normal_or_inf_results\":%llu,\"max_ulp_normal_or_inf\":%u,\"argmax_normal\":\"0x%08x\"," |
| 913 | "\"subnormal_or_zero_results\":%llu,\"max_ulp_subnormal_or_zero\":%u,\"argmax_subnormal\":\"0x%08x\"," |
| 914 | "\"nan_class_mismatches\":%llu,\"over_bound\":%llu,\"hist\":[", |
| 915 | name, (unsigned long long)t.n, bound, (unsigned long long)t.n_norm, t.max_norm, t.arg_norm, |
| 916 | (unsigned long long)t.n_sub, t.max_sub, t.arg_sub, (unsigned long long)t.nan_bad, |
| 917 | (unsigned long long)t.over); |
| 918 | for (int h = 0; h < 65; h++) printf("%s%llu", h ? "," : "", (unsigned long long)t.hist[h]); |
| 919 | printf("]}\n"); |
| 920 | bool pass = fails == 0 && t.n == (1ull << 32) && t.nan_bad == 0 && t.over == 0; |
| 921 | printf("ULP exhaustive %s verdict: %s\n", name, pass ? "PASS" : "FAIL"); |
| 922 | return pass ? 0 : 1; |
| 923 | } |
| 924 | |
| 925 | int main(int argc, char **argv) { |
| 926 | if (argc == 2 && strcmp(argv[1], "--e1-exhaustive") == 0) return e1_exhaustive(); |
| 927 | if (argc == 3 && strcmp(argv[1], "--ulp-exhaustive") == 0) return ulp_exhaustive(argv[2]); |
| 928 | if (argc > 1) { fprintf(stderr, "usage: %s [--e1-exhaustive | --ulp-exhaustive EXP|LOG]\n", argv[0]); return 2; } |
| 929 | #ifdef OMEGA_NUMERIC_CPU_ONLY |
| 930 | const bool gb10 = false; |
| 931 | #else |
| 932 | const bool gb10 = true; |
| 933 | #endif |
| 934 | printf("============================================================\n"); |
| 935 | printf(" GATE 5: OMEGA-NUMERIC-0 QUALIFICATION SUITE \n"); |
| 936 | printf(" mode: %s\n", gb10 ? "GB10 + CPU tiers" : "CPU tiers only (no device)"); |
| 937 | printf("============================================================\n\n"); |
| 938 | |
| 939 | #ifdef OMEGA_NUMERIC_CPU_ONLY |
| 940 | printf("OMEGA_NUMERIC_HWDESC_JSON:{\"source\":\"FAKE_NON_HARDWARE_CPU_ONLY\",\"fake\":true," |
| 941 | "\"descriptor_digest\":\"0000000000000000000000000000000000000000000000000000000000000000\"}\n"); |
| 942 | #else |
| 943 | int hw_rc = print_hw_descriptor(); |
| 944 | #endif |
| 945 | |
| 946 | print_run_line(); |
| 947 | build_corpus(); |
| 948 | |
| 949 | /* The host tiers need round to nearest even with FZ (and DN, AH, FIZ, NEP, |
| 950 | * FZ16) clear; otherwise both tiers can agree on the same wrong result. */ |
| 951 | uint64_t fpcr = omega_numeric_read_fpcr(); |
| 952 | printf("[*] FPCR 0x%016llx, required-clear mask 0x%016llx\n", (unsigned long long)fpcr, |
| 953 | (unsigned long long)OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR); |
| 954 | report("FPCR_RNE_NO_FTZ_REQUIRED", |
| 955 | (fpcr & OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR) == 0 && omega_numeric_fpenv_ok()); |
| 956 | |
| 957 | /* ---- Encoding and provenance ------------------------------------------------ */ |
| 958 | int fixtures_rc = omega_blackwell_verify_codegen_fixtures(); |
| 959 | int prov_problems = omega_numeric_verify_all_fixtures(); |
| 960 | size_t encoded = 0; |
| 961 | int build_ok = 1; |
| 962 | printf("[*] Op registry:\n"); |
| 963 | for (size_t i = 0; i < omega_numeric_op_count(); i++) { |
| 964 | const OmegaNumericOpInfo *info = omega_numeric_op_at(i); |
| 965 | printf(" %-11s %-10s %-10s %s\n", info->name, info->gb10_encoded ? "encoded" : "REFUSED", |
| 966 | omega_numeric_compare_name(info->compare), info->reference); |
| 967 | printf("OMEGA_NUMERIC_REGISTRY_JSON:{\"op\":\"%s\",\"encoded\":%s,\"compare\":\"%s\",\"launches\":%zu}\n", |
| 968 | info->name, info->gb10_encoded ? "true" : "false", omega_numeric_compare_name(info->compare), |
| 969 | info->op == OMEGA_NOP_FFMA ? (size_t)FFMA_C_COUNT : (size_t)1); |
| 970 | if (!info->gb10_encoded) continue; |
| 971 | encoded++; |
| 972 | if (omega_numeric_op_whole_kernel(info->op)) { |
| 973 | /* DIV/SQRT/EXP/LOG: whole-program kernel, structural check and the |
| 974 | * nvdisasm-verified digest (src/omega_numeric_divsqrt_gb10.c). The |
| 975 | * vecadd patch path must refuse it. */ |
| 976 | static uint8_t dcode[OMEGA_DS_MAX_CODE_BYTES]; |
| 977 | size_t dlen = 0; |
| 978 | char derr[256]; |
| 979 | OmegaDsOp dop = omega_ds_op_for_numeric(info->op); |
| 980 | OmegaNumericPatchInsn dp[OMEGA_NUMERIC_PATCH_MAX]; |
| 981 | uint8_t pcode[0x200]; |
| 982 | if (omega_ds_build_kernel(dop, dcode, sizeof(dcode), &dlen) != 0 || |
| 983 | omega_ds_check_kernel(dop, dcode, dlen, OMEGA_DS_GPR_COUNT, derr, sizeof(derr)) != 0 || |
| 984 | omega_ds_check_digest(dop, dcode, dlen, derr, sizeof(derr)) != 0 || |
| 985 | omega_numeric_patch_words(info->op, dp) != OMEGA_NUMERIC_ERR_NOT_ENCODED || |
| 986 | omega_numeric_build_kernel(info->op, pcode, sizeof(pcode), &dlen) == 0) { |
| 987 | printf(" %s whole-program kernel refused: %s\n", info->name, derr); |
| 988 | build_ok = 0; |
| 989 | } |
| 990 | continue; |
| 991 | } |
| 992 | uint8_t code[0x200]; |
| 993 | size_t len = 0; |
| 994 | if (omega_numeric_build_kernel(info->op, code, sizeof(code), &len) != 0) build_ok = 0; |
| 995 | } |
| 996 | printf("[*] Provenance entries: %zu, encoded ops: %zu\n", omega_numeric_get_opcode_count(), encoded); |
| 997 | report("PROVENANCE_MATCHES_EXECUTOR", |
| 998 | fixtures_rc == 0 && prov_problems == 0 && build_ok && encoded == 42 && |
| 999 | omega_numeric_get_opcode_count() == 52); |
| 1000 | |
| 1001 | /* Refusal before submission: never a silent wrong instruction. */ |
| 1002 | int refuse_ok = 1; |
| 1003 | size_t refused_ops = 0; |
| 1004 | for (size_t oi = 0; oi < omega_numeric_op_count(); oi++) { |
| 1005 | const OmegaNumericOpInfo *ninfo = omega_numeric_op_at(oi); |
| 1006 | if (ninfo->gb10_encoded) continue; |
| 1007 | const char *nname = ninfo->name; |
| 1008 | const float *nc = ninfo->arity >= 3 ? g_c : NULL; |
| 1009 | char err[256]; |
| 1010 | refused_ops++; |
| 1011 | int rc = omega_numeric_submit_check(nname, g_a, g_b, nc, g_dev, N, err, sizeof(err)); |
| 1012 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 1013 | int xrc = omega_gb10_execute_simt_op(nname, g_a, g_b, nc, g_dev, N); |
| 1014 | if (xrc != OMEGA_NUMERIC_ERR_NOT_ENCODED) refuse_ok = 0; |
| 1015 | #endif |
| 1016 | if (rc != OMEGA_NUMERIC_ERR_NOT_ENCODED || err[0] == '\0') refuse_ok = 0; |
| 1017 | OmegaNumericPatchInsn p[OMEGA_NUMERIC_PATCH_MAX]; |
| 1018 | if (omega_numeric_patch_words(ninfo->op, p) != OMEGA_NUMERIC_ERR_NOT_ENCODED) |
| 1019 | refuse_ok = 0; |
| 1020 | printf(" refused %-14s rc=%d: %s\n", nname, rc, err); |
| 1021 | } |
| 1022 | /* no registry op remains refused (the 23 E1 scalar ops are encoded, E1 |
| 1023 | * WP-C; DIV SQRT run the E1 row 7 and EXP LOG the E1 row 10 |
| 1024 | * whole-program kernels) */ |
| 1025 | if (refused_ops != omega_numeric_op_count() - 42) refuse_ok = 0; |
| 1026 | { |
| 1027 | char err[256]; |
| 1028 | fill_c(g_c, 0x3f800000u); |
| 1029 | g_c[7] = 2.0f; |
| 1030 | if (omega_numeric_submit_check("FFMA", g_a, g_b, g_c, g_dev, N, err, sizeof(err)) != OMEGA_NUMERIC_ERR_OPERANDS) |
| 1031 | refuse_ok = 0; |
| 1032 | printf(" refused FFMA non-uniform c: %s\n", err); |
| 1033 | if (omega_numeric_submit_check("SHFL_DOWN", g_a, NULL, NULL, g_dev, 33, err, sizeof(err)) != OMEGA_NUMERIC_ERR_OPERANDS) |
| 1034 | refuse_ok = 0; |
| 1035 | printf(" refused SHFL_DOWN count 33: %s\n", err); |
| 1036 | if (omega_numeric_submit_check("FADD", g_a, NULL, NULL, g_dev, N, err, sizeof(err)) != OMEGA_NUMERIC_ERR_BAD_ARGS) |
| 1037 | refuse_ok = 0; |
| 1038 | } |
| 1039 | { |
| 1040 | /* Variants of the shared-memory and warp ops that have no encoding: |
| 1041 | * refused by name, never mapped onto the 32-bit or SHFL.DOWN kernel. */ |
| 1042 | size_t nv = omega_numeric_refused_variant_count(); |
| 1043 | for (size_t v = 0; v < nv; v++) { |
| 1044 | char err[256]; |
| 1045 | const char *name = omega_numeric_refused_variant_at(v); |
| 1046 | int rc = omega_numeric_submit_check(name, g_a, g_b, NULL, g_dev, N, err, sizeof(err)); |
| 1047 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 1048 | if (omega_gb10_execute_simt_op(name, g_a, g_b, NULL, g_dev, N) != OMEGA_NUMERIC_ERR_NOT_ENCODED) refuse_ok = 0; |
| 1049 | #endif |
| 1050 | if (rc != OMEGA_NUMERIC_ERR_NOT_ENCODED || !strstr(err, name)) refuse_ok = 0; |
| 1051 | } |
| 1052 | printf(" refused %zu unencoded variants (LDS.U8 ... REDUCE_SUM_BLOCK) by name\n", nv); |
| 1053 | if (nv < 12 || omega_numeric_refused_variant_at(nv) != NULL) refuse_ok = 0; |
| 1054 | } |
| 1055 | report("NOT_ENCODED_OPS_REFUSED_BEFORE_SUBMISSION", refuse_ok); |
| 1056 | |
| 1057 | /* Shapes the shared-memory and reduction kernels cannot carry, refused |
| 1058 | * before submission with the reason named. */ |
| 1059 | { |
| 1060 | struct { const char *op; size_t count; const char *why; } SHAPES[] = { |
| 1061 | { "LDS_STS", 32, "CTA size 64" }, /* half a CTA: threads 32..63 never reach BAR.SYNC */ |
| 1062 | { "LDS_STS", 96, "CTA size 64" }, |
| 1063 | { "LDS_STS", 4097, "CTA size 64" }, |
| 1064 | { "REDUCE_SUM", 33, "multiple of 32" }, |
| 1065 | { "REDUCE_SUM", 4080, "multiple of 32" }, |
| 1066 | { "SHFL_DOWN", 33, "multiple of 32" }, |
| 1067 | }; |
| 1068 | int shapes_ok = 1; |
| 1069 | for (size_t k = 0; k < sizeof(SHAPES) / sizeof(SHAPES[0]); k++) { |
| 1070 | char err[256]; |
| 1071 | int rc = omega_numeric_submit_check(SHAPES[k].op, g_a, NULL, NULL, g_dev, SHAPES[k].count, err, sizeof(err)); |
| 1072 | if (rc != OMEGA_NUMERIC_ERR_OPERANDS || !strstr(err, SHAPES[k].why)) shapes_ok = 0; |
| 1073 | printf(" refused %-10s count %-5zu rc=%d: %s\n", SHAPES[k].op, SHAPES[k].count, rc, err); |
| 1074 | } |
| 1075 | char err[256]; |
| 1076 | if (omega_numeric_submit_check("LDS_STS", g_a, NULL, NULL, g_dev, N, err, sizeof(err)) != 0 || |
| 1077 | omega_numeric_submit_check("REDUCE_SUM", g_a, NULL, NULL, g_dev, N, err, sizeof(err)) != 0 || |
| 1078 | omega_numeric_submit_check("LDS_STS", g_a, NULL, NULL, g_dev, 64, err, sizeof(err)) != 0 || |
| 1079 | omega_numeric_submit_check("REDUCE_SUM", g_a, NULL, NULL, g_dev, 32, err, sizeof(err)) != 0) { |
| 1080 | printf(" a valid shape was refused: %s\n", err); |
| 1081 | shapes_ok = 0; |
| 1082 | } |
| 1083 | report("NEG_BAD_SHARED_AND_WARP_SHAPES_REFUSED", shapes_ok); |
| 1084 | } |
| 1085 | |
| 1086 | /* Structural patch check: every encoded op passes on the launch QMD, and |
| 1087 | * one corruption per check is refused with that check's reason. */ |
| 1088 | { |
| 1089 | int clean_ok = 1; |
| 1090 | uint32_t qmd[OMEGA_BW_QMD_WORDS]; |
| 1091 | OmegaBlackwellQmdConfig cfg; |
| 1092 | memset(&cfg, 0, sizeof(cfg)); |
| 1093 | cfg.num_elements = N; |
| 1094 | omega_numeric_launch_shape(N, &cfg.threads_per_block, &cfg.grid_width); |
| 1095 | omega_blackwell_build_qmd1(qmd, &cfg); |
| 1096 | for (size_t i = 0; i < omega_numeric_op_count(); i++) { |
| 1097 | const OmegaNumericOpInfo *info = omega_numeric_op_at(i); |
| 1098 | if (!info->gb10_encoded) continue; |
| 1099 | if (omega_numeric_op_whole_kernel(info->op)) continue; /* no patch: checked in PROVENANCE_MATCHES_EXECUTOR */ |
| 1100 | OmegaNumericPatchInsn p[OMEGA_NUMERIC_PATCH_MAX]; |
| 1101 | int n = omega_numeric_patch_words(info->op, p); |
| 1102 | char err[256]; |
| 1103 | if (omega_numeric_check_patch(info->op, p, n, qmd, err, sizeof(err)) != 0) { |
| 1104 | clean_ok = 0; |
| 1105 | printf(" clean %s refused: %s\n", info->name, err); |
| 1106 | } |
| 1107 | } |
| 1108 | int caught = 0, cases = 0; |
| 1109 | /* 32..43: E1 scalar ops (E1 WP-C) */ |
| 1110 | static const OmegaNumericOp E1_NEG_OP[22] = { |
| 1111 | OMEGA_NOP_FSETP_LT_SEL, OMEGA_NOP_FSETP_LT_SEL, OMEGA_NOP_FSETP_EQ_SEL, OMEGA_NOP_F2I_FLOOR, |
| 1112 | OMEGA_NOP_F32_TO_F16, OMEGA_NOP_F2I_FLOOR, OMEGA_NOP_I2FP_U32, OMEGA_NOP_FFMA_V, |
| 1113 | OMEGA_NOP_FFMA_V, OMEGA_NOP_FFMA_V, OMEGA_NOP_FFMA_V, OMEGA_NOP_FFMA_V, |
| 1114 | OMEGA_NOP_I2FP_U32, OMEGA_NOP_FSETP_LT_SEL, OMEGA_NOP_FFMA_V, OMEGA_NOP_FFMA_V, |
| 1115 | OMEGA_NOP_FSETP_LT_SEL, OMEGA_NOP_FFMA_V, OMEGA_NOP_I2FP_U32, OMEGA_NOP_FFMA_V, |
| 1116 | OMEGA_NOP_FFMA_V, OMEGA_NOP_FSETP_LT_SEL }; |
| 1117 | for (int m = 0; m < 54; m++) { |
| 1118 | OmegaNumericOp op = OMEGA_NOP_LDS_STS; |
| 1119 | if (m >= 18 && m != 27 && m != 31) op = OMEGA_NOP_REDUCE_SUM; |
| 1120 | if (m == 17) op = OMEGA_NOP_SHFL_DOWN; |
| 1121 | if (m >= 32) op = E1_NEG_OP[m - 32]; |
| 1122 | OmegaNumericPatchInsn p[OMEGA_NUMERIC_PATCH_MAX]; |
| 1123 | int n = omega_numeric_patch_words(op, p); |
| 1124 | uint32_t q[OMEGA_BW_QMD_WORDS]; |
| 1125 | memcpy(q, qmd, sizeof(q)); |
| 1126 | const char *want = ""; |
| 1127 | const char *what = ""; |
| 1128 | OmegaNumericOp check_op = op; |
| 1129 | /* LDS_STS: 0 SHF, 1 LOP3, 2 STS, 3 BAR, 4 LDS, 5 STG, 6 EXIT |
| 1130 | * REDUCE: 2s SHFL, 2s+1 FADD (s = 0..4), 10 STG, 11 EXIT */ |
| 1131 | switch (m) { |
| 1132 | case 0: what = "empty patch"; n = 0; want = "patch length"; break; |
| 1133 | case 1: what = "STG after LDS does not wait"; p[5].w[3] = 0x000fe200u; want = "does not wait on it"; break; |
| 1134 | case 2: what = "STS word changed (not the 32-bit form)"; p[2].w[2] ^= 0x00000200u; want = "STS at"; break; |
| 1135 | case 3: what = "LDS with an immediate offset"; p[4].w[1] = 0x000100ffu; want = "LDS at"; break; |
| 1136 | case 4: what = "barrier other than BAR.SYNC 0"; p[3].w[1] = 1u; want = "barrier at"; break; |
| 1137 | case 5: what = "FADD words submitted as LDS_STS"; check_op = OMEGA_NOP_LDS_STS; |
| 1138 | n = omega_numeric_patch_words(OMEGA_NOP_FADD, p); want = "no STS/LDS pair"; break; |
| 1139 | case 6: what = "BAR removed"; memmove(&p[3], &p[4], 3 * sizeof(p[0])); n = 6; want = "expected one STS"; break; |
| 1140 | case 7: what = "BAR before STS"; { OmegaNumericPatchInsn t = p[2]; p[2] = p[3]; p[3] = t; } want = "order must be"; break; |
| 1141 | case 8: what = "QMD declares no barrier"; q[35] &= ~(0x1fu << 17); want = "barriers"; break; |
| 1142 | case 9: what = "QMD CTA width 32"; q[34] = (q[34] & ~0xffffu) | 32u; want = "CTA width"; break; |
| 1143 | case 10: what = "QMD shared size 128 bytes"; q[36] = (q[36] & ~0x7ffu) | 1u; want = "bytes of shared memory"; break; |
| 1144 | case 11: what = "address shift 3 (tid * 8)"; p[0].w[1] = 3u; want = "SHF.L.U32"; break; |
| 1145 | case 12: what = "STS address from R10"; p[2].w[0] = 0x0a007988u; want = "STS address register"; break; |
| 1146 | case 13: what = "LOP3 AND instead of XOR"; p[1].w[2] = 0x078ec0ffu; want = "LOP3 XOR"; break; |
| 1147 | case 14: what = "partner mask 0x1fc (out of bounds)"; p[1].w[1] = 0x1fcu; want = "(CTA-1)*4"; break; |
| 1148 | case 15: what = "LDS into R8"; p[4].w[0] = 0x0a087984u; want = "LDS must load"; break; |
| 1149 | case 16: what = "SHFL mode bits changed"; check_op = OMEGA_NOP_REDUCE_SUM; |
| 1150 | n = omega_numeric_patch_words(OMEGA_NOP_REDUCE_SUM, p); p[0].w[2] ^= 0x1u; want = "SHFL.DOWN form"; break; |
| 1151 | case 17: what = "SHFL_DOWN by 2"; p[0].w[1] = 0x08401f00u; want = "SHFL.DOWN by 1"; break; |
| 1152 | case 18: what = "last pair dropped"; p[8] = p[10]; p[9] = p[11]; n = 10; want = "five SHFL.DOWN"; break; |
| 1153 | case 19: what = "deltas 16 and 8 swapped"; { uint32_t t = p[0].w[1]; p[0].w[1] = p[2].w[1]; p[2].w[1] = t; } |
| 1154 | want = "declared order"; break; |
| 1155 | case 20: what = "clamp 0x0f"; p[4].w[1] = (p[4].w[1] & ~(0x1fu << 8)) | (0x0fu << 8); want = "clamp"; break; |
| 1156 | case 21: what = "shuffle of R5"; p[2].w[0] = 0x05097f89u; want = "shuffles R"; break; |
| 1157 | case 22: what = "FADD negates"; p[3].w[1] |= 0x80000000u; want = "plain FADD"; break; |
| 1158 | case 23: what = "shuffle sets no barrier"; p[6].w[3] |= (7u << 14); want = "does not wait on the shuffle"; break; |
| 1159 | case 24: what = "FADD adds R5"; p[5].w[1] = 5u; want = "does not add the shuffled"; break; |
| 1160 | case 25: what = "FADD writes R3"; p[1].w[0] = 0x02037221u; want = "writes R3"; break; |
| 1161 | case 26: what = "STG replaced by EXIT"; p[10] = p[11]; want = "is not stored"; break; |
| 1162 | case 27: what = "STS stores R5 instead of a[i] in R2"; p[2].w[1] = 5u; want = "not the input value R2"; break; |
| 1163 | case 28: what = "sum stored through address R8"; p[10].w[0] = 0x08007986u; want = "STG.E desc[UR4][R6.64]"; break; |
| 1164 | case 29: what = "sum store predicated on P0"; p[10].w[0] = 0x06000986u; want = "STG.E desc[UR4][R6.64]"; break; |
| 1165 | case 30: what = "sum store width changed"; p[10].w[2] ^= 0x00000200u; want = "STG.E desc[UR4][R6.64]"; break; |
| 1166 | case 31: what = "LDS_STS result stored through address R8"; p[5].w[0] = 0x08007986u; want = "STG.E desc[UR4][R6.64]"; break; |
| 1167 | case 32: what = "FSETP_LT_SEL with the GT compare code"; p[0].w[2] = 0x03f04000u; want = "FSETP compare code"; break; |
| 1168 | case 33: what = "FSETP compares a with R6"; p[0].w[1] = 6u; want = "patch is not FSETP"; break; |
| 1169 | case 34: what = "FSEL operands swapped"; p[1].w[0] = 0x05097208u; p[1].w[1] = 2u; want = "select is not FSEL"; break; |
| 1170 | case 35: what = "F2I_FLOOR submitted with CEIL rounding"; p[0].w[2] = 0x0020b100u; want = "conversion at 0"; break; |
| 1171 | case 36: what = "F32_TO_F16 submitted as F2F.BF16"; p[0].w[2] = 0x00202000u; want = "conversion at 0"; break; |
| 1172 | case 37: what = "F2I_FLOOR result not scoreboarded"; p[0].w[3] = 0x010fca00u; want = "must be a scoreboarded"; break; |
| 1173 | case 38: what = "I2FP_U32 with an extra STG, EXIT"; p[1] = p[0]; p[1].w[0] = 0x06007986u; p[1].w[1] = 9u; p[1].w[2] = 0x0c101904u; |
| 1174 | p[1].w[3] = 0x000fe200u; p[2].w[0] = 0x0000794du; p[2].w[1] = 0; p[2].w[2] = 0x03800000u; p[2].w[3] = 0x000fea00u; |
| 1175 | n = 3; want = "must be fixed latency"; break; |
| 1176 | case 39: what = "FFMA_V without the c load"; p[2] = p[3]; p[3] = p[4]; p[4] = p[5]; n = 5; want = "expected LDC.64"; break; |
| 1177 | case 40: what = "FFMA_V c pointer from c[0x0][0x3a8]"; p[0].w[1] = 0x0000ea00u; want = "c pointer is not"; break; |
| 1178 | case 41: what = "FFMA_V c address indexed by R2"; p[1].w[0] = 0x020a7825u; want = "c address is not"; break; |
| 1179 | case 42: what = "FFMA_V c loaded from R12"; p[2].w[0] = 0x0c0b7981u; want = "c[i] is not LDG"; break; |
| 1180 | case 43: what = "FFMA_V adds R1 instead of c[i]"; p[3].w[2] = 0x00000001u; want = "FFMA is not"; break; |
| 1181 | case 44: what = "I2FP_U32 does not wait on the a/b loads"; p[0].w[3] = 0x000fca00u; want = "does not wait on SB4"; break; |
| 1182 | case 45: what = "FSETP_LT_SEL store replaced by EXIT"; p[2] = p[3]; want = "does not end with STG"; break; |
| 1183 | case 46: what = "FFMA_V c pointer into R2:R3 (clobbers a[i])"; p[0].w[0] = 0xff027b82u; p[1].w[0] = 0x09027825u; p[1].w[2] = 0x078e0002u; p[2].w[0] = 0x02037981u; p[3].w[2] = 3u; want = "c pointer is not"; break; |
| 1184 | case 47: what = "FFMA_V c load sets no barrier"; p[2].w[3] = 0x000fe200u; want = "c[i] is not LDG"; break; |
| 1185 | case 48: what = "early store through address R8 before the select"; p[1].w[0] = 0x08007986u; p[1].w[1] = 9u; p[1].w[2] = 0x0c101904u; p[1].w[3] = 0x000fe200u; want = "store at 1"; break; |
| 1186 | case 49: what = "FFMA_V c load stalls 1 cycle before its waiter (chip FAIL 2026-10-01)"; p[2].w[3] = 0x000e2200u; want = "with stall 1"; break; |
| 1187 | case 50: what = "I2FP stall cleared: baseline STG would read R9 early (Codex review)"; p[0].w[3] &= ~0x1e00u; want = "stalls 0 cycles"; break; |
| 1188 | case 51: what = "FFMA_V LDG descriptor/offset word changed (Codex review)"; p[2].w[1] = 0x00000005u; want = "c[i] is not LDG"; break; |
| 1189 | case 52: what = "FFMA_V IMAD.WIDE upper w2 bits changed (Codex review)"; p[1].w[2] = 0x078e010au; want = "c address is not IMAD"; break; |
| 1190 | case 53: what = "FSETP stalls 1 before the FSEL that reads P0"; p[0].w[3] = 0x010fc200u; want = "stalls 1 cycles"; break; |
| 1191 | } |
| 1192 | char err[256]; |
| 1193 | int rc = omega_numeric_check_patch(check_op, p, n, q, err, sizeof(err)); |
| 1194 | bool ok = rc == OMEGA_NUMERIC_ERR_OPERANDS && strstr(err, want) != NULL; |
| 1195 | cases++; |
| 1196 | if (ok) caught++; |
| 1197 | else printf(" NOT CAUGHT (%s): rc=%d reason '%s', wanted '%s'\n", what, rc, err, want); |
| 1198 | } |
| 1199 | { /* FFMA_V reads c per element: a submission without c is refused. */ |
| 1200 | char e3[256]; |
| 1201 | int rc3 = omega_numeric_submit_check("FFMA_V", g_a, g_b, NULL, g_dev, N, e3, sizeof(e3)); |
| 1202 | cases++; |
| 1203 | if (rc3 == OMEGA_NUMERIC_ERR_BAD_ARGS && strstr(e3, "third input")) caught++; |
| 1204 | else printf(" NOT CAUGHT (FFMA_V without c): rc=%d reason '%s'\n", rc3, e3); |
| 1205 | } |
| 1206 | printf(" patch corruptions refused with the right reason: %d of %d; clean patches accepted: %s\n", |
| 1207 | caught, cases, clean_ok ? "yes" : "no"); |
| 1208 | report("NEG_PATCH_STRUCTURE_CHECKED_BEFORE_SUBMISSION", caught == cases && clean_ok); |
| 1209 | } |
| 1210 | |
| 1211 | /* Gate item: the spec's vocabulary includes shared-memory load/store and a |
| 1212 | * warp reduction primitive. Both are encoded, keyed to provenance, and |
| 1213 | * pass the structural check on the launch QMD for the corpus size. Parity |
| 1214 | * on silicon is the GB10 tier's job, not this one. */ |
| 1215 | { |
| 1216 | bool lds = omega_numeric_op_find("LDS_STS")->gb10_encoded; |
| 1217 | bool red = omega_numeric_op_find("REDUCE_SUM")->gb10_encoded; |
| 1218 | char e1[256], e2[256]; |
| 1219 | bool lds_sub = omega_numeric_submit_check("LDS_STS", g_a, NULL, NULL, g_dev, N, e1, sizeof(e1)) == 0; |
| 1220 | bool red_sub = omega_numeric_submit_check("REDUCE_SUM", g_a, NULL, NULL, g_dev, N, e2, sizeof(e2)) == 0; |
| 1221 | if (!lds || !red || !lds_sub || !red_sub) |
| 1222 | printf(" FP32_SIMT_OPCODES_ENCODED: LDS_STS encoded %d accepted %d (%s); REDUCE_SUM encoded %d accepted %d (%s)\n", |
| 1223 | lds, lds_sub, e1, red, red_sub, e2); |
| 1224 | report("FP32_SIMT_OPCODES_ENCODED", |
| 1225 | fixtures_rc == 0 && prov_problems == 0 && build_ok && lds && red && lds_sub && red_sub); |
| 1226 | } |
| 1227 | |
| 1228 | /* ---- Math sequences --------------------------------------------------------- */ |
| 1229 | { |
| 1230 | int seq_ok = 1; |
| 1231 | OmegaParityTrace t; |
| 1232 | size_t div_bad = 0, sqrt_bad = 0; |
| 1233 | if (omega_numeric_reference(OMEGA_NOP_DIV, g_a, g_b, NULL, g_ref, N) == 0 && |
| 1234 | omega_numeric_cpu_realize(OMEGA_NOP_DIV, g_a, g_b, NULL, g_cpu, N) == 0) { |
| 1235 | if (omega_numeric_parity(OMEGA_NOP_DIV, g_ref, g_cpu, N, &t) != 0) { seq_ok = 0; t.mismatches = 1; } |
| 1236 | ClassTrace ct; class_trace(OMEGA_NOP_DIV, g_a, g_ref, g_cpu, N, &ct); |
| 1237 | print_parity_json("DIV", "cpu", "BIT_EXACT", 0, false, N, &t, &ct); |
| 1238 | div_bad = t.mismatches; |
| 1239 | } else seq_ok = 0; |
| 1240 | if (omega_numeric_reference(OMEGA_NOP_SQRT, g_a, NULL, NULL, g_ref, N) == 0 && |
| 1241 | omega_numeric_cpu_realize(OMEGA_NOP_SQRT, g_a, NULL, NULL, g_cpu, N) == 0) { |
| 1242 | if (omega_numeric_parity(OMEGA_NOP_SQRT, g_ref, g_cpu, N, &t) != 0) { seq_ok = 0; t.mismatches = 1; } |
| 1243 | ClassTrace ct; class_trace(OMEGA_NOP_SQRT, g_a, g_ref, g_cpu, N, &ct); |
| 1244 | print_parity_json("SQRT", "cpu", "BIT_EXACT", 0, false, N, &t, &ct); |
| 1245 | sqrt_bad = t.mismatches; |
| 1246 | } else seq_ok = 0; |
| 1247 | /* Named hard cases the review found (old Newton sequences failed each): |
| 1248 | * 1/3 was 1 ULP low, max/min-subnormal gave NaN, sqrt(min normal) was |
| 1249 | * 1 ULP low. Plus ties and results in the subnormal range. */ |
| 1250 | static const uint32_t DIV_HARD[][2] = { |
| 1251 | { 0x3f800000u, 0x40400000u }, { 0x7f7fffffu, 0x00000001u }, { 0xff7fffffu, 0x00000001u }, |
| 1252 | { 0x00000001u, 0x7f7fffffu }, { 0x00800000u, 0x40000000u }, { 0x00000003u, 0x40000000u }, |
| 1253 | { 0x00000001u, 0x40000000u }, { 0x00000001u, 0x3f7fffffu }, { 0x007fffffu, 0x3f800001u }, |
| 1254 | { 0x7f7fffffu, 0x3f7fffffu }, { 0x3f7fffffu, 0x7f7fffffu }, { 0x00400000u, 0x00000003u }, |
| 1255 | }; |
| 1256 | static const uint32_t SQRT_HARD[] = { |
| 1257 | 0x00800000u, 0x00000001u, 0x00000002u, 0x007fffffu, 0x7f7fffffu, 0x3f7fffffu, 0x40000000u, |
| 1258 | }; |
| 1259 | size_t hard_bad = 0; |
| 1260 | for (size_t k = 0; k < sizeof(DIV_HARD) / sizeof(DIV_HARD[0]); k++) { |
| 1261 | float x = F(DIV_HARD[k][0]), y = F(DIV_HARD[k][1]); |
| 1262 | float want = omega_ieee_div(x, y), got = omega_math_div(x, y); |
| 1263 | if (!omega_numeric_bits_equal(want, got)) { |
| 1264 | hard_bad++; |
| 1265 | printf(" DIV 0x%08x / 0x%08x: want 0x%08x got 0x%08x\n", U(x), U(y), U(want), U(got)); |
| 1266 | } |
| 1267 | } |
| 1268 | for (size_t k = 0; k < sizeof(SQRT_HARD) / sizeof(SQRT_HARD[0]); k++) { |
| 1269 | float x = F(SQRT_HARD[k]); |
| 1270 | float want = omega_ieee_sqrt(x), got = omega_math_sqrt(x); |
| 1271 | if (!omega_numeric_bits_equal(want, got)) { |
| 1272 | hard_bad++; |
| 1273 | printf(" SQRT 0x%08x: want 0x%08x got 0x%08x\n", U(x), U(want), U(got)); |
| 1274 | } |
| 1275 | } |
| 1276 | printf("OMEGA_NUMERIC_FINDING_JSON:{\"id\":\"DIV_SQRT_CORRECT_ROUNDING\",\"corpus\":%u," |
| 1277 | "\"div_mismatches\":%zu,\"sqrt_mismatches\":%zu,\"hard_case_mismatches\":%zu}\n", |
| 1278 | N, div_bad, sqrt_bad, hard_bad); |
| 1279 | if (hard_bad) seq_ok = 0; |
| 1280 | if (div_bad || sqrt_bad) seq_ok = 0; |
| 1281 | |
| 1282 | /* exp / log: the Omega sequences are the definition; check edge |
| 1283 | * behavior and self-consistency (no libm available as an oracle). */ |
| 1284 | int el_ok = 1; |
| 1285 | float ln2 = 0.693147182f; |
| 1286 | if (U(omega_math_exp(0.0f)) != 0x3f800000u) el_ok = 0; |
| 1287 | if (U(omega_math_exp(F(0xff800000u))) != 0) el_ok = 0; |
| 1288 | if (U(omega_math_exp(F(0x7f800000u))) != 0x7f800000u) el_ok = 0; |
| 1289 | if (!omega_isnan(omega_math_exp(F(0x7fc00000u)))) el_ok = 0; |
| 1290 | if (!omega_isinf(omega_math_exp(100.0f))) el_ok = 0; |
| 1291 | float e887 = omega_math_exp(88.7f); /* k = 128: needs the split scale */ |
| 1292 | if (omega_isinf(e887) || omega_isnan(e887) || e887 < 3.0e38f) el_ok = 0; |
| 1293 | float em100 = omega_math_exp(-100.0f), em50 = omega_math_exp(-50.0f); |
| 1294 | if (!omega_issubnormal(em100)) el_ok = 0; |
| 1295 | if (omega_fabs(em100 - em50 * em50) > 1e-5f * em50 * em50 + F(0x00000002u)) el_ok = 0; |
| 1296 | for (int k = -120; k <= 120; k += 8) { |
| 1297 | float x = (float)k * ln2; |
| 1298 | float e = omega_math_exp(x); |
| 1299 | float want = F((uint32_t)(k + 127) << 23); |
| 1300 | /* x itself is rounded (half an ulp of k ln2), which alone moves exp by up to |
| 1301 | * |x| 2^-24 relative; allow that plus a few ulps. */ |
| 1302 | if (omega_fabs(e - want) > (omega_fabs(x) * 6e-8f + 1e-6f) * want) el_ok = 0; |
| 1303 | } |
| 1304 | if (U(omega_math_log(1.0f)) != 0 && U(omega_math_log(1.0f)) != 0x80000000u) el_ok = 0; |
| 1305 | if (U(omega_math_log(0.0f)) != 0xff800000u) el_ok = 0; |
| 1306 | if (!omega_isnan(omega_math_log(-1.0f))) el_ok = 0; |
| 1307 | if (U(omega_math_log(F(0x7f800000u))) != 0x7f800000u) el_ok = 0; |
| 1308 | float lmin = omega_math_log(F(0x00000001u)); /* -149 ln2 = -103.27893 */ |
| 1309 | if (omega_fabs(lmin - (-103.278929903f)) > 2e-5f) el_ok = 0; |
| 1310 | float lsub = omega_math_log(F(0x00400000u)); /* 2^-127 */ |
| 1311 | if (omega_fabs(lsub - (-127.0f * ln2)) > 2e-5f) el_ok = 0; |
| 1312 | for (int k = -100; k <= 100; k += 5) { |
| 1313 | float x = (float)k * 0.37f; |
| 1314 | float r = omega_math_log(omega_math_exp(x)); |
| 1315 | if (omega_fabs(r - x) > 2e-6f * (omega_fabs(x) + 1.0f)) el_ok = 0; |
| 1316 | } |
| 1317 | printf("[*] exp/log edge and consistency checks: %s\n", el_ok ? "ok" : "FAILED"); |
| 1318 | if (!el_ok) seq_ok = 0; |
| 1319 | report("OMEGA_MATH_SEQUENCES_QUALIFIED", seq_ok); |
| 1320 | } |
| 1321 | |
| 1322 | /* ---- Declared reduction order ------------------------------------------------ */ |
| 1323 | { |
| 1324 | int ord_ok = 1; |
| 1325 | if (omega_numeric_reference(OMEGA_NOP_REDUCE_SUM, g_a, NULL, NULL, g_ref, N) != 0 || |
| 1326 | omega_numeric_cpu_realize(OMEGA_NOP_REDUCE_SUM, g_a, NULL, NULL, g_cpu, N) != 0) { |
| 1327 | ord_ok = 0; |
| 1328 | } else { |
| 1329 | OmegaParityTrace t; |
| 1330 | if (omega_numeric_parity(OMEGA_NOP_REDUCE_SUM, g_ref, g_cpu, N, &t) != 0) ord_ok = 0; |
| 1331 | print_parity_json("REDUCE_SUM", "cpu", "BIT_EXACT", 0, false, N, &t, NULL); |
| 1332 | if (t.mismatches || t.checked != N / 32) ord_ok = 0; |
| 1333 | } |
| 1334 | printf("[*] Declared Reduction Order: %s\n", OMEGA_WARP_REDUCTION_DECLARED_ORDER); |
| 1335 | report("WARP_REDUCTION_ORDER_DECLARED", ord_ok); |
| 1336 | } |
| 1337 | |
| 1338 | /* ---- CPU tier against the reference, every op ---------------------------------- */ |
| 1339 | int cpu_ok = 1, cpu_sub_ok = 1; |
| 1340 | for (size_t i = 0; i < omega_numeric_op_count(); i++) { |
| 1341 | OmegaNumericOp op = omega_numeric_op_at(i)->op; |
| 1342 | if (op == OMEGA_NOP_DIV || op == OMEGA_NOP_SQRT || op == OMEGA_NOP_REDUCE_SUM) continue; /* above */ |
| 1343 | TierResult tr = cpu_tier(op); |
| 1344 | if (!tr.ok) { cpu_ok = 0; printf(" CPU tier mismatch: %s\n", omega_numeric_op_at(i)->name); } |
| 1345 | if (ftz_sensitive(op) && !tr.subnormals_seen) cpu_sub_ok = 0; |
| 1346 | } |
| 1347 | report("CPU_TIER_EQUALS_REFERENCE", cpu_ok); |
| 1348 | report("CPU_TIER_SUBNORMALS_PRESERVED", cpu_ok && cpu_sub_ok); |
| 1349 | printf("\n[*] Independent oracle: integer soft-float, binary128 EXP/LOG, tagged LDS_STS\n"); |
| 1350 | report("CPU_TIER_INDEPENDENT_ORACLE", oracle_tier()); |
| 1351 | |
| 1352 | /* ---- E1 scalar contract: boundary values, random differential, oracle ------------ */ |
| 1353 | printf("\n[*] E1 scalar contract: %u random inputs per op, 23 ops, three derivations\n", (unsigned)E1_RANDOM_PER_OP); |
| 1354 | report("E1_SCALAR_BOUNDARY_VALUES", e1_boundary()); |
| 1355 | bool e1_cpu_ok = false, e1_oracle_ok = false; |
| 1356 | e1_random(&e1_cpu_ok, &e1_oracle_ok); |
| 1357 | report("E1_SCALAR_CPU_EQUALS_REFERENCE", e1_cpu_ok); |
| 1358 | report("E1_SCALAR_INDEPENDENT_ORACLE", e1_oracle_ok); |
| 1359 | |
| 1360 | /* ---- GB10 tier ----------------------------------------------------------------- */ |
| 1361 | if (!gb10) { |
| 1362 | skip("CPU_GB10_BIT_PARITY", "needs GB10"); |
| 1363 | skip("SUBNORMALS_PRESERVED_NO_FTZ", "needs GB10"); |
| 1364 | skip("MUFU_SEED_ONLY_NOT_COMPARED", "needs GB10"); |
| 1365 | skip("EDGE_CLASS_BEHAVIOR_VERIFIED", "needs GB10"); |
| 1366 | skip("HARDWARE_DESCRIPTOR_PROBED", "needs GB10"); |
| 1367 | } |
| 1368 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 1369 | else { |
| 1370 | printf("\n[*] GB10 tier: %u elements per launch\n", N); |
| 1371 | int bit_ok = 1, sub_ok = 1, mufu_ok = 1, class_ok_all = 1; |
| 1372 | for (size_t i = 0; i < omega_numeric_op_count(); i++) { |
| 1373 | const OmegaNumericOpInfo *info = omega_numeric_op_at(i); |
| 1374 | if (!info->gb10_encoded) continue; |
| 1375 | bool class_ok = true; |
| 1376 | TierResult tr = gb10_tier(info->op, &class_ok); |
| 1377 | if (info->compare == OMEGA_CMP_SEED_BOUND) { |
| 1378 | if (!tr.ok) mufu_ok = 0; |
| 1379 | continue; |
| 1380 | } |
| 1381 | if (!tr.ok) bit_ok = 0; |
| 1382 | if (!class_ok) class_ok_all = 0; |
| 1383 | if (ftz_sensitive(info->op) && (!tr.ok || !tr.subnormals_seen)) sub_ok = 0; |
| 1384 | } |
| 1385 | OmegaParityTrace t; |
| 1386 | mufu_ok = mufu_ok && |
| 1387 | omega_numeric_parity(OMEGA_NOP_MUFU_RCP, g_ref, g_dev, N, &t) == OMEGA_NUMERIC_ERR_OPERANDS && |
| 1388 | omega_numeric_parity(OMEGA_NOP_MUFU_RSQ, g_ref, g_dev, N, &t) == OMEGA_NUMERIC_ERR_OPERANDS; |
| 1389 | report("CPU_GB10_BIT_PARITY", bit_ok && cpu_ok); |
| 1390 | report("SUBNORMALS_PRESERVED_NO_FTZ", sub_ok && cpu_sub_ok); |
| 1391 | report("MUFU_SEED_ONLY_NOT_COMPARED", mufu_ok); |
| 1392 | report("EDGE_CLASS_BEHAVIOR_VERIFIED", class_ok_all && bit_ok); |
| 1393 | report("HARDWARE_DESCRIPTOR_PROBED", hw_rc == 0); |
| 1394 | } |
| 1395 | #endif |
| 1396 | |
| 1397 | /* ---- Negative tests: each must catch a broken property ------------------------- */ |
| 1398 | printf("\n[*] Negative tests\n"); |
| 1399 | { |
| 1400 | /* FTZ model through the same comparator must mismatch for every FTZ-sensitive op */ |
| 1401 | int ftz_ok = 1; |
| 1402 | for (size_t i = 0; i < omega_numeric_op_count(); i++) { |
| 1403 | OmegaNumericOp op = omega_numeric_op_at(i)->op; |
| 1404 | if (!ftz_sensitive(op)) continue; |
| 1405 | const float *c = NULL; |
| 1406 | if (op == OMEGA_NOP_FFMA) { fill_c(g_c, 0x00800000u); c = g_c; } |
| 1407 | if (op == OMEGA_NOP_FFMA_V) { for (size_t k = 0; k < N; k++) g_c[k] = g_a[N - 1 - k]; c = g_c; } |
| 1408 | OmegaParityTrace t; |
| 1409 | if (omega_numeric_reference(op, g_a, g_b, c, g_ref, N) != 0 || |
| 1410 | omega_numeric_reference_ftz(op, g_a, g_b, c, g_cpu, N) != 0 || |
| 1411 | omega_numeric_parity(op, g_ref, g_cpu, N, &t) != 0 || t.mismatches == 0) { |
| 1412 | ftz_ok = 0; |
| 1413 | } |
| 1414 | printf(" FTZ model vs reference, %-9s: %zu mismatches\n", omega_numeric_op_at(i)->name, t.mismatches); |
| 1415 | } |
| 1416 | report("NEG_FTZ_DETECTED_AND_REJECTED", ftz_ok); |
| 1417 | } |
| 1418 | { |
| 1419 | /* A sequential sum disagrees with the declared tree on some warp */ |
| 1420 | float w[32]; |
| 1421 | w[0] = 16777216.0f; |
| 1422 | for (int i = 1; i < 32; i++) w[i] = 1.0f; |
| 1423 | float seq = 0.0f; |
| 1424 | for (int i = 0; i < 32; i++) seq += w[i]; |
| 1425 | float tree = omega_warp_reduce_sum(w); |
| 1426 | printf(" sequential %.1f vs declared tree %.1f\n", seq, tree); |
| 1427 | report("NEG_UNORDERED_REDUCTION_DIVERGENCE_CAUGHT", U(seq) != U(tree)); |
| 1428 | } |
| 1429 | { |
| 1430 | /* Raw seed must fail bit parity against the IEEE reciprocal, and the |
| 1431 | * comparator must refuse any bit comparison of a MUFU op. */ |
| 1432 | size_t bad = 0, n = 0; |
| 1433 | for (size_t i = 3456; i < N; i++) { |
| 1434 | float y = g_a[i]; |
| 1435 | n++; |
| 1436 | if (!omega_numeric_bits_equal(omega_numeric_host_rcp_seed(y), omega_ieee_div(1.0f, y))) bad++; |
| 1437 | } |
| 1438 | OmegaParityTrace t; |
| 1439 | int refused = omega_numeric_parity(OMEGA_NOP_MUFU_RCP, g_ref, g_cpu, N, &t) == OMEGA_NUMERIC_ERR_OPERANDS && |
| 1440 | omega_numeric_parity(OMEGA_NOP_MUFU_RSQ, g_ref, g_cpu, N, &t) == OMEGA_NUMERIC_ERR_OPERANDS; |
| 1441 | printf(" raw seed vs IEEE reciprocal: %zu of %zu differ; MUFU bit compare refused: %s\n", |
| 1442 | bad, n, refused ? "yes" : "no"); |
| 1443 | report("NEG_RAW_MUFU_APPROX_REJECTED_WITHOUT_REFINEMENT", bad > 0 && refused); |
| 1444 | } |
| 1445 | { |
| 1446 | char err[256]; |
| 1447 | int rc = omega_numeric_submit_check("FDIV_TYPO", g_a, g_b, NULL, g_dev, N, err, sizeof(err)); |
| 1448 | int ok = rc == OMEGA_NUMERIC_ERR_BAD_ARGS && omega_numeric_op_find("FDIV_TYPO") == NULL; |
| 1449 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 1450 | ok = ok && omega_gb10_execute_simt_op("FDIV_TYPO", g_a, g_b, NULL, g_dev, N) == OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1451 | #endif |
| 1452 | report("NEG_UNKNOWN_OPCODE_FAILS_CLOSED", ok); |
| 1453 | } |
| 1454 | { |
| 1455 | /* Corrupted provenance copies must each be rejected */ |
| 1456 | size_t n = omega_numeric_get_opcode_count(); |
| 1457 | OmegaOpcodeProvenance copy[64 + 1]; /* table limit 64 (verify_fixture_table) + the m == 4 extra */ |
| 1458 | int too_big = n > 64; |
| 1459 | if (too_big) n = 64; |
| 1460 | int caught = 0, cases = 0; |
| 1461 | for (int m = 0; m < 5; m++) { |
| 1462 | for (size_t i = 0; i < n; i++) copy[i] = *omega_numeric_get_opcode(i); |
| 1463 | size_t cn = n; |
| 1464 | for (size_t i = 0; i < n; i++) { |
| 1465 | if (m == 0 && strcmp(copy[i].key, "FSETP_GE_R2_R5") == 0) copy[i].fixture_w2 = 0x03f0e000u; /* GEU */ |
| 1466 | if (m == 1 && strcmp(copy[i].key, "FADD") == 0) copy[i].opcode = 0; |
| 1467 | if (m == 2 && strcmp(copy[i].key, "FSEL_R2_R5_P0") == 0) copy[i].fixture_w2 = 0x04000000u; /* !P0 */ |
| 1468 | if (m == 3 && strcmp(copy[i].key, "MUFU_RCP") == 0) copy[i].fixture_w1 = 0x00000005u; |
| 1469 | } |
| 1470 | if (m == 4) { |
| 1471 | copy[cn] = copy[0]; |
| 1472 | copy[cn].key = "DIV"; |
| 1473 | copy[cn].mnemonic = "DIV (claimed, never submitted)"; |
| 1474 | cn++; |
| 1475 | } |
| 1476 | cases++; |
| 1477 | if (omega_numeric_verify_fixture_table(copy, cn, false) > 0) caught++; |
| 1478 | } |
| 1479 | for (size_t i = 0; i < n; i++) copy[i] = *omega_numeric_get_opcode(i); |
| 1480 | int clean = omega_numeric_verify_fixture_table(copy, n, false) == 0; |
| 1481 | printf(" corrupted provenance copies rejected: %d of %d; clean copy accepted: %s\n", |
| 1482 | caught, cases, clean ? "yes" : "no"); |
| 1483 | report("NEG_OPCODE_PROVENANCE_INTEGRITY_VERIFIED", caught == cases && clean && !too_big); |
| 1484 | } |
| 1485 | { |
| 1486 | /* A non-default FP environment must be refused by every host tier, not |
| 1487 | * used silently: under round-up, 1 + 2^-24 is the next float above 1 |
| 1488 | * in both the reference and the CPU tier, and they would agree. */ |
| 1489 | uint64_t saved = omega_numeric_read_fpcr(); |
| 1490 | static const uint64_t BAD_ENV[] = { 1ULL << 22 /* RMode RP */, 3ULL << 22 /* RMode RZ */, |
| 1491 | 1ULL << 24 /* FZ */ }; |
| 1492 | static const char *BAD_NAME[] = { "round-up", "round-to-zero", "flush-to-zero" }; |
| 1493 | int cases_ok = 0; |
| 1494 | for (size_t k = 0; k < 3; k++) { |
| 1495 | uint64_t v = (saved & ~OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR) | BAD_ENV[k]; |
| 1496 | __asm__ volatile("msr fpcr, %0" : : "r"(v) : "memory"); |
| 1497 | volatile float one = 1.0f, tiny = F(0x33800000u), half = F(0x33000000u), sub = F(0x00000001u); |
| 1498 | volatile float up = one + tiny, down = one - half, flushed = sub * one; |
| 1499 | bool env_ok = omega_numeric_fpenv_ok(); |
| 1500 | int r_ref = omega_numeric_reference(OMEGA_NOP_FADD, g_a, g_b, NULL, g_ref, N); |
| 1501 | int r_cpu = omega_numeric_cpu_realize(OMEGA_NOP_FADD, g_a, g_b, NULL, g_cpu, N); |
| 1502 | int r_ftz = omega_numeric_reference_ftz(OMEGA_NOP_FADD, g_a, g_b, NULL, g_cpu, N); |
| 1503 | OmegaParityTrace t; |
| 1504 | int r_seed = omega_numeric_seed_bound(OMEGA_NOP_MUFU_RCP, g_a, g_cpu, N, &t); |
| 1505 | __asm__ volatile("msr fpcr, %0" : : "r"(saved) : "memory"); |
| 1506 | /* the environment really changed (else the probe proves nothing) */ |
| 1507 | bool changed = (k == 0) ? U(up) == 0x3f800001u |
| 1508 | : (k == 1) ? U(down) == 0x3f7fffffu : U(flushed) == 0u; |
| 1509 | bool refused = !env_ok && r_ref == OMEGA_NUMERIC_ERR_FPENV && r_cpu == OMEGA_NUMERIC_ERR_FPENV && |
| 1510 | r_ftz == OMEGA_NUMERIC_ERR_FPENV && r_seed == OMEGA_NUMERIC_ERR_FPENV; |
| 1511 | printf(" FPCR %-13s: environment changed %s, tiers refused %s (rc %d %d %d %d)\n", BAD_NAME[k], |
| 1512 | changed ? "yes" : "no", refused ? "yes" : "no", r_ref, r_cpu, r_ftz, r_seed); |
| 1513 | if (changed && refused) cases_ok++; |
| 1514 | } |
| 1515 | bool restored = omega_numeric_read_fpcr() == saved && omega_numeric_fpenv_ok(); |
| 1516 | report("NEG_NONDEFAULT_FPCR_REFUSED", cases_ok == 3 && restored); |
| 1517 | } |
| 1518 | { |
| 1519 | /* Comparator sanity: one flipped low bit, and NaN vs number, are caught */ |
| 1520 | int ref_rc = omega_numeric_reference(OMEGA_NOP_FADD, g_a, g_b, NULL, g_ref, N); |
| 1521 | memcpy(g_cpu, g_ref, sizeof(g_cpu)); |
| 1522 | g_cpu[3500] = F(U(g_cpu[3500]) ^ 1u); |
| 1523 | g_cpu[3600] = F(0x7fc00000u); |
| 1524 | OmegaParityTrace t; |
| 1525 | int par_rc = omega_numeric_parity(OMEGA_NOP_FADD, g_ref, g_cpu, N, &t); |
| 1526 | report("NEG_COMPARATOR_CATCHES_ONE_BIT", ref_rc == 0 && par_rc == 0 && |
| 1527 | t.mismatches == 2 && t.first_index == 3500); |
| 1528 | } |
| 1529 | |
| 1530 | printf("\nGate 5 Results: TOTAL=%d PASSED=%d FAILED=%d SKIPPED=%d\n", g_total, g_passed, g_failed, g_skipped); |
| 1531 | /* |
| 1532 | * Exit status. 0: nothing failed and every SKIP is accounted for. |
| 1533 | * 1: a real failure (a FAIL, or a SKIP that is not allowed here). |
| 1534 | * CPU-only build: the SKIPs must be exactly the CHIP_ONLY_IDS (the known |
| 1535 | * gap a host cannot close). Chip build: no SKIP is allowed at all. |
| 1536 | */ |
| 1537 | size_t chip_only_seen = 0; |
| 1538 | for (size_t k = 0; k < CHIP_ONLY_COUNT; k++) chip_only_seen += g_chip_only_skipped[k]; |
| 1539 | #ifdef OMEGA_NUMERIC_CPU_ONLY |
| 1540 | bool gap_ok = g_undeclared_skips == 0 && chip_only_seen == CHIP_ONLY_COUNT; |
| 1541 | const char *build = "cpu-only"; |
| 1542 | #else |
| 1543 | bool gap_ok = g_skipped == 0; |
| 1544 | const char *build = "chip"; |
| 1545 | #endif |
| 1546 | const char *verdict = g_failed ? "HOST_REGRESSION" : !gap_ok ? "UNDECLARED_SKIP" |
| 1547 | : g_skipped ? "PASS_EXCEPT_DECLARED_CHIP_ONLY" : "PASS"; |
| 1548 | printf("Gate 5 Verdict: %s (build %s, declared chip-only skips %zu of %zu, undeclared skips %d)\n", |
| 1549 | verdict, build, chip_only_seen, CHIP_ONLY_COUNT, g_undeclared_skips); |
| 1550 | return (g_failed == 0 && gap_ok) ? 0 : 1; |
| 1551 | } |