omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_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 | * OMEGA-NUMERIC-0 host-pure module: semantic reference tier, CPU realization |
| 19 | * tier, Omega math sequences, op registry, kernel patch words and parity |
| 20 | * comparators. No device access here (see src/omega_numeric_gb10.c). |
| 21 | */ |
| 22 | #include "omega_numeric.h" |
| 23 | #include "omega_numeric_lifecycle.h" |
| 24 | #include "omega_blackwell_encoder.h" |
| 25 | #include "omega_blackwell_qmd.h" |
| 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/auxv.h> |
| 31 | |
| 32 | atomic_int omega_numeric_native_state = ATOMIC_VAR_INIT(OMEGA_NUMERIC_NATIVE_IDLE); |
| 33 | |
| 34 | #if !defined(__aarch64__) |
| 35 | #error "OMEGA-NUMERIC-0 host tiers use AArch64 FP instructions (fmadd, fdiv, fsqrt, fminnm, scvtf, fcvtzs)" |
| 36 | #endif |
| 37 | |
| 38 | bool omega_numeric_bits_equal(float a, float b) { |
| 39 | if (omega_isnan(a) && omega_isnan(b)) { |
| 40 | return true; /* NaN equivalence class */ |
| 41 | } |
| 42 | return omega_float_to_bits(a) == omega_float_to_bits(b); |
| 43 | } |
| 44 | |
| 45 | /* ---- Required FP environment ----------------------------------------------- */ |
| 46 | |
| 47 | uint64_t omega_numeric_read_fpcr(void) { |
| 48 | uint64_t v; |
| 49 | __asm__ volatile("mrs %0, fpcr" : "=r"(v) : : "memory"); |
| 50 | return v; |
| 51 | } |
| 52 | |
| 53 | bool omega_numeric_fpenv_ok(void) { |
| 54 | return (omega_numeric_read_fpcr() & OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR) == 0; |
| 55 | } |
| 56 | |
| 57 | /* ---- Semantic Reference Tier (Zero Libm) --------------------------------- */ |
| 58 | |
| 59 | float omega_ref_fadd(float a, float b) { return a + b; } |
| 60 | float omega_ref_fsub(float a, float b) { return a - b; } |
| 61 | float omega_ref_fmul(float a, float b) { return a * b; } |
| 62 | |
| 63 | float omega_ref_ffma(float a, float b, float c) { |
| 64 | float r; |
| 65 | __asm__("fmadd %s0, %s1, %s2, %s3" : "=w"(r) : "w"(a), "w"(b), "w"(c)); |
| 66 | return r; |
| 67 | } |
| 68 | |
| 69 | float omega_ref_fmin(float a, float b) { |
| 70 | if (omega_isnan(a)) return b; |
| 71 | if (omega_isnan(b)) return a; |
| 72 | if (omega_iszero(a) && omega_iszero(b)) { |
| 73 | return (omega_signbit(a) || omega_signbit(b)) ? omega_bits_to_float(0x80000000U) : 0.0f; |
| 74 | } |
| 75 | return (a < b) ? a : b; |
| 76 | } |
| 77 | |
| 78 | float omega_ref_fmax(float a, float b) { |
| 79 | if (omega_isnan(a)) return b; |
| 80 | if (omega_isnan(b)) return a; |
| 81 | if (omega_iszero(a) && omega_iszero(b)) { |
| 82 | return (!omega_signbit(a) || !omega_signbit(b)) ? 0.0f : omega_bits_to_float(0x80000000U); |
| 83 | } |
| 84 | return (a > b) ? a : b; |
| 85 | } |
| 86 | |
| 87 | bool omega_ref_fsetp_ge(float a, float b) { |
| 88 | if (omega_isnan(a) || omega_isnan(b)) return false; |
| 89 | return (a >= b); |
| 90 | } |
| 91 | |
| 92 | float omega_ref_i2f(int32_t a) { |
| 93 | return (float)a; |
| 94 | } |
| 95 | |
| 96 | int32_t omega_ref_f2i(float a) { |
| 97 | if (omega_isnan(a)) return 0; |
| 98 | if (a >= 2147483647.0f) return 2147483647; |
| 99 | if (a <= -2147483648.0f) return (int32_t)-2147483648LL; |
| 100 | return (int32_t)a; |
| 101 | } |
| 102 | |
| 103 | /* ---- E1 scalar contract: reference definitions ----------------------------- |
| 104 | * Integer operations on the bit patterns only, so nothing here depends on |
| 105 | * FPCR or on how the compiler lowers a float operation. The semantic text is |
| 106 | * docs/numeric/E1_SCALAR_CONTRACT.md. */ |
| 107 | |
| 108 | enum { REF_RTZ, REF_FLOOR, REF_CEIL, REF_RNE }; |
| 109 | |
| 110 | /* Round the FP32 value u to an integer in direction mode, saturate to |
| 111 | * [lo, hi]. NaN -> 0. */ |
| 112 | static int64_t ref_to_int(uint32_t u, int mode, int64_t lo, int64_t hi) { |
| 113 | uint32_t be = (u >> 23) & 0xffU, f = u & 0x007fffffU; |
| 114 | bool neg = (u & 0x80000000U) != 0; |
| 115 | if (be == 0xffU) return f ? 0 : (neg ? lo : hi); |
| 116 | if (be == 0 && f == 0) return 0; |
| 117 | uint64_t m = be ? (f | 0x00800000U) : f; |
| 118 | int32_t e = be ? (int32_t)be - 150 : -149; |
| 119 | uint64_t ip; /* integer part of |x| */ |
| 120 | int frac; /* 0: none, 1: below half, 2: half, 3: above */ |
| 121 | if (e >= 0) { |
| 122 | if (e > 16) return neg ? lo : hi; /* |x| >= 2^40 */ |
| 123 | ip = m << e; |
| 124 | frac = 0; |
| 125 | } else if (-e >= 40) { |
| 126 | ip = 0; |
| 127 | frac = 1; /* 0 < |x| < 2^-16 */ |
| 128 | } else { |
| 129 | int sh = -e; |
| 130 | ip = m >> sh; |
| 131 | uint64_t rem = m & ((1ULL << sh) - 1U), half = 1ULL << (sh - 1); |
| 132 | frac = rem == 0 ? 0 : rem < half ? 1 : rem == half ? 2 : 3; |
| 133 | } |
| 134 | uint64_t mag = ip; |
| 135 | switch (mode) { |
| 136 | case REF_RTZ: break; |
| 137 | case REF_FLOOR: if (neg && frac) mag++; break; |
| 138 | case REF_CEIL: if (!neg && frac) mag++; break; |
| 139 | default: if (frac == 3 || (frac == 2 && (ip & 1U))) mag++; break; |
| 140 | } |
| 141 | int64_t v = neg ? -(int64_t)mag : (int64_t)mag; |
| 142 | return v < lo ? lo : v > hi ? hi : v; |
| 143 | } |
| 144 | |
| 145 | int32_t omega_ref_f2i_floor(float a) { |
| 146 | return (int32_t)ref_to_int(omega_float_to_bits(a), REF_FLOOR, INT32_MIN, INT32_MAX); |
| 147 | } |
| 148 | int32_t omega_ref_f2i_ceil(float a) { |
| 149 | return (int32_t)ref_to_int(omega_float_to_bits(a), REF_CEIL, INT32_MIN, INT32_MAX); |
| 150 | } |
| 151 | int32_t omega_ref_f2i_rni(float a) { |
| 152 | return (int32_t)ref_to_int(omega_float_to_bits(a), REF_RNE, INT32_MIN, INT32_MAX); |
| 153 | } |
| 154 | uint32_t omega_ref_f2u(float a) { |
| 155 | return (uint32_t)ref_to_int(omega_float_to_bits(a), REF_RTZ, 0, UINT32_MAX); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Round sig * 2^exp2 (exact, sig > 0) to a binary format with man explicit |
| 160 | * mantissa bits and exponent bias bias (all-ones exponent field emax_field), |
| 161 | * nearest even, subnormals kept, overflow to infinity. Returns the format's |
| 162 | * bits without the sign. |
| 163 | */ |
| 164 | typedef unsigned __int128 ref_u128; |
| 165 | |
| 166 | static uint32_t ref_round_small(ref_u128 sig, int32_t exp2, int man, int bias, uint32_t emax_field) { |
| 167 | int32_t p = 127; |
| 168 | while (((sig >> p) & 1U) == 0) p--; |
| 169 | int32_t lead = p + exp2; |
| 170 | int32_t be = lead + bias; |
| 171 | if (be >= (int32_t)emax_field) return emax_field << man; |
| 172 | int32_t lsb = (be >= 1) ? lead - man : 1 - bias - man; |
| 173 | int32_t s = lsb - exp2; |
| 174 | uint64_t mant; |
| 175 | if (s <= 0) { |
| 176 | mant = (uint64_t)(sig << -s); |
| 177 | } else if (s > p + 1) { |
| 178 | return 0; |
| 179 | } else { |
| 180 | mant = (s >= 128) ? 0 : (uint64_t)(sig >> s); |
| 181 | bool guard = ((sig >> (s - 1)) & 1U) != 0; |
| 182 | bool rest = (s >= 2) && (sig & ((((ref_u128)1) << (s - 1)) - 1U)) != 0; |
| 183 | if (guard && (rest || (mant & 1U))) mant++; |
| 184 | } |
| 185 | /* normal: mant in [2^man, 2^(man+1)], the carry moves into the exponent */ |
| 186 | uint64_t bits = (be >= 1) ? ((uint64_t)(be - 1) << man) + mant : mant; |
| 187 | if (bits >= ((uint64_t)emax_field << man)) return emax_field << man; |
| 188 | return (uint32_t)bits; |
| 189 | } |
| 190 | |
| 191 | float omega_ref_u2f(uint32_t a) { |
| 192 | if (a == 0) return 0.0f; |
| 193 | return omega_bits_to_float(ref_round_small(a, 0, 23, 127, 0xffU)); |
| 194 | } |
| 195 | |
| 196 | static uint16_t ref_narrow(float a, int man, int bias, uint32_t emax_field) { |
| 197 | uint32_t u = omega_float_to_bits(a); |
| 198 | uint32_t sign = (u >> 16) & 0x8000U; |
| 199 | uint32_t be = (u >> 23) & 0xffU, f = u & 0x007fffffU; |
| 200 | uint32_t inf = emax_field << man; |
| 201 | if (be == 0xffU) { |
| 202 | if (!f) return (uint16_t)(sign | inf); |
| 203 | /* quiet NaN of the target format, top payload bits kept (not semantic) */ |
| 204 | return (uint16_t)(sign | inf | (1U << (man - 1)) | (f >> (23 - man))); |
| 205 | } |
| 206 | if (be == 0 && f == 0) return (uint16_t)sign; |
| 207 | uint64_t m = be ? (f | 0x00800000U) : f; |
| 208 | int32_t e = be ? (int32_t)be - 150 : -149; |
| 209 | return (uint16_t)(sign | ref_round_small(m, e, man, bias, emax_field)); |
| 210 | } |
| 211 | |
| 212 | uint16_t omega_ref_f32_to_f16(float a) { return ref_narrow(a, 10, 15, 0x1fU); } |
| 213 | uint16_t omega_ref_f32_to_bf16(float a) { return ref_narrow(a, 7, 127, 0xffU); } |
| 214 | |
| 215 | float omega_ref_f16_to_f32(uint16_t h) { |
| 216 | uint32_t sign = ((uint32_t)h & 0x8000U) << 16; |
| 217 | uint32_t be = ((uint32_t)h >> 10) & 0x1fU, f = (uint32_t)h & 0x3ffU; |
| 218 | if (be == 0x1fU) return omega_bits_to_float(sign | 0x7f800000U | (f ? 0x00400000U | (f << 13) : 0)); |
| 219 | if (be == 0) { |
| 220 | if (f == 0) return omega_bits_to_float(sign); |
| 221 | int32_t e = -14; /* value f * 2^-24 = (f / 2^10) * 2^-14 */ |
| 222 | while ((f & 0x400U) == 0) { f <<= 1; e--; } |
| 223 | return omega_bits_to_float(sign | ((uint32_t)(e + 127) << 23) | ((f & 0x3ffU) << 13)); |
| 224 | } |
| 225 | return omega_bits_to_float(sign | ((be - 15 + 127) << 23) | (f << 13)); |
| 226 | } |
| 227 | |
| 228 | /* BF16 is the top half of FP32: widening is exact by construction. */ |
| 229 | float omega_ref_bf16_to_f32(uint16_t h) { return omega_bits_to_float((uint32_t)h << 16); } |
| 230 | |
| 231 | /* Finite nonzero FP32 bits u as m * 2^e (m below 2^24, not normalized). */ |
| 232 | static void ref_split(uint32_t u, uint64_t *m, int32_t *e) { |
| 233 | uint32_t be = (u >> 23) & 0xffU, f = u & 0x007fffffU; |
| 234 | *m = be ? (f | 0x00800000U) : f; |
| 235 | *e = be ? (int32_t)be - 150 : -149; |
| 236 | } |
| 237 | |
| 238 | static int32_t ref_msb(ref_u128 v) { |
| 239 | int32_t p = 127; |
| 240 | while (((v >> p) & 1U) == 0) p--; |
| 241 | return p; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * a * b + c, one rounding. The exact product (below 2^48) and c are placed in |
| 246 | * one 128-bit window: the term whose leading bit is higher goes to bit 125, |
| 247 | * the other is shifted to the same scale with every lost bit ORed into bit 0. |
| 248 | * When bits are lost the terms are more than 2^77 apart, so the result keeps |
| 249 | * far more than the 26 bits rounding needs and the sticky bit only breaks |
| 250 | * ties, which is all it has to do. |
| 251 | */ |
| 252 | float omega_ref_ffma_int(float a, float b, float c) { |
| 253 | uint32_t ua = omega_float_to_bits(a), ub = omega_float_to_bits(b), uc = omega_float_to_bits(c); |
| 254 | uint32_t ps = (ua ^ ub) & 0x80000000U, cs = uc & 0x80000000U; |
| 255 | uint32_t xa = ua & 0x7fffffffU, xb = ub & 0x7fffffffU, xc = uc & 0x7fffffffU; |
| 256 | if (xa > OMEGA_INF_POS || xb > OMEGA_INF_POS || xc > OMEGA_INF_POS) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 257 | bool pinf = xa == OMEGA_INF_POS || xb == OMEGA_INF_POS, pzero = xa == 0 || xb == 0; |
| 258 | if (pinf && pzero) return omega_bits_to_float(OMEGA_QNAN_BITS); /* inf * 0 */ |
| 259 | if (pinf) { |
| 260 | if (xc == OMEGA_INF_POS && cs != ps) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 261 | return omega_bits_to_float(ps | OMEGA_INF_POS); |
| 262 | } |
| 263 | if (xc == OMEGA_INF_POS) return c; |
| 264 | if (pzero) return xc ? c : omega_bits_to_float(ps & cs); /* exact zero sum: +0 unless both -0 */ |
| 265 | uint64_t ma, mb, mc; |
| 266 | int32_t ea, eb, ec; |
| 267 | ref_split(xa, &ma, &ea); |
| 268 | ref_split(xb, &mb, &eb); |
| 269 | ref_u128 mp = (ref_u128)ma * mb; |
| 270 | int32_t ep = ea + eb; |
| 271 | if (xc == 0) return omega_bits_to_float(ps | ref_round_small(mp, ep, 23, 127, 0xffU)); |
| 272 | ref_split(xc, &mc, &ec); |
| 273 | /* X: the term with the higher leading bit. */ |
| 274 | ref_u128 X = mp, Y = mc; |
| 275 | int32_t ex = ep, ey = ec; |
| 276 | uint32_t sx = ps, sy = cs; |
| 277 | if (ref_msb(mc) + ec > ref_msb(mp) + ep) { X = mc; ex = ec; sx = cs; Y = mp; ey = ep; sy = ps; } |
| 278 | int32_t up = 125 - ref_msb(X); |
| 279 | X <<= up; |
| 280 | ex -= up; |
| 281 | int32_t d = ex - ey; /* Y is scaled by 2^-d: d >= 0 here */ |
| 282 | if (d < 0) { |
| 283 | Y <<= -d; /* only when Y has fewer bits than X */ |
| 284 | } else if (d >= 128) { |
| 285 | Y = 1; /* all of Y below bit 0: sticky only */ |
| 286 | } else if (d > 0) { |
| 287 | bool lost = (Y & ((((ref_u128)1) << d) - 1U)) != 0; |
| 288 | Y = (Y >> d) | (lost ? 1U : 0U); |
| 289 | } |
| 290 | ref_u128 m; |
| 291 | uint32_t s; |
| 292 | if (sx == sy) { m = X + Y; s = sx; } |
| 293 | else if (X >= Y) { m = X - Y; s = sx; } |
| 294 | else { m = Y - X; s = sy; } |
| 295 | if (m == 0) return 0.0f; /* exact cancellation: +0 under RNE */ |
| 296 | return omega_bits_to_float(s | ref_round_small(m, ex, 23, 127, 0xffU)); |
| 297 | } |
| 298 | |
| 299 | #ifndef HWCAP2_BF16 |
| 300 | #define HWCAP2_BF16 (1UL << 14) |
| 301 | #endif |
| 302 | bool omega_numeric_cpu_has_bf16(void) { |
| 303 | return (getauxval(AT_HWCAP2) & HWCAP2_BF16) != 0; |
| 304 | } |
| 305 | |
| 306 | /* Predicate truth per comparison outcome: bit 0 less, bit 1 equal, bit 2 |
| 307 | * greater, bit 3 unordered. */ |
| 308 | #define PL 1u |
| 309 | #define PE 2u |
| 310 | #define PG 4u |
| 311 | #define PU 8u |
| 312 | static unsigned fsetp_mask(int op) { |
| 313 | switch (op) { |
| 314 | case OMEGA_NOP_FSETP_SEL: return PG | PE; /* GE */ |
| 315 | case OMEGA_NOP_FSETP_LT_SEL: return PL; |
| 316 | case OMEGA_NOP_FSETP_LE_SEL: return PL | PE; |
| 317 | case OMEGA_NOP_FSETP_GT_SEL: return PG; |
| 318 | case OMEGA_NOP_FSETP_EQ_SEL: return PE; |
| 319 | case OMEGA_NOP_FSETP_NE_SEL: return PL | PG; |
| 320 | case OMEGA_NOP_FSETP_NUM_SEL: return PL | PE | PG; |
| 321 | case OMEGA_NOP_FSETP_NAN_SEL: return PU; |
| 322 | case OMEGA_NOP_FSETP_LTU_SEL: return PL | PU; |
| 323 | case OMEGA_NOP_FSETP_LEU_SEL: return PL | PE | PU; |
| 324 | case OMEGA_NOP_FSETP_GTU_SEL: return PG | PU; |
| 325 | case OMEGA_NOP_FSETP_GEU_SEL: return PG | PE | PU; |
| 326 | case OMEGA_NOP_FSETP_EQU_SEL: return PE | PU; |
| 327 | case OMEGA_NOP_FSETP_NEU_SEL: return PL | PG | PU; |
| 328 | default: return 0; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* Comparison outcome from the bit patterns: sign-magnitude to a signed key, |
| 333 | * so -0 and +0 both map to 0. */ |
| 334 | static unsigned ref_order(uint32_t ua, uint32_t ub) { |
| 335 | if ((ua & 0x7fffffffU) > 0x7f800000U || (ub & 0x7fffffffU) > 0x7f800000U) return PU; |
| 336 | int64_t ka = (ua & 0x80000000U) ? -(int64_t)(ua & 0x7fffffffU) : (int64_t)ua; |
| 337 | int64_t kb = (ub & 0x80000000U) ? -(int64_t)(ub & 0x7fffffffU) : (int64_t)ub; |
| 338 | return ka < kb ? PL : ka > kb ? PG : PE; |
| 339 | } |
| 340 | |
| 341 | int omega_ref_fsetp_pred(int op, float a, float b) { |
| 342 | unsigned m = fsetp_mask(op); |
| 343 | if (!m) return -1; |
| 344 | return (m & ref_order(omega_float_to_bits(a), omega_float_to_bits(b))) ? 1 : 0; |
| 345 | } |
| 346 | |
| 347 | float omega_ieee_div(float x, float y) { |
| 348 | float r; |
| 349 | __asm__("fdiv %s0, %s1, %s2" : "=w"(r) : "w"(x), "w"(y)); |
| 350 | return r; |
| 351 | } |
| 352 | |
| 353 | float omega_ieee_sqrt(float x) { |
| 354 | float r; |
| 355 | __asm__("fsqrt %s0, %s1" : "=w"(r) : "w"(x)); |
| 356 | return r; |
| 357 | } |
| 358 | |
| 359 | /* ---- Host seed used only by the raw-seed negative test ------------------- */ |
| 360 | /* Magic-constant reciprocal seed. It is not a model of the hardware MUFU unit. */ |
| 361 | |
| 362 | float omega_numeric_host_rcp_seed(float y) { |
| 363 | return omega_bits_to_float(0x7ef311c0U - omega_float_to_bits(y)); |
| 364 | } |
| 365 | |
| 366 | /* ---- Omega-defined division and square root ------------------------------- |
| 367 | * Both sequences use integer operations only (add, subtract, shift, compare, |
| 368 | * select), which the SIMT integer pipe has. They use no MUFU seed and no FP |
| 369 | * arithmetic, so the result does not depend on the FP rounding mode or on |
| 370 | * flush-to-zero. The rounding step is written once (omega_round_pack) and |
| 371 | * rounds to nearest, ties to even, including into the subnormal range and to |
| 372 | * infinity on overflow. Result: correctly rounded IEEE a / b and sqrt(a). |
| 373 | * |
| 374 | * The earlier Newton sequences (magic-constant seed, two refinements, an |
| 375 | * unfused residual) were up to 1 ULP off and overflowed to NaN when the |
| 376 | * divisor was subnormal; the 4096-element corpus found 946 DIV and 201 SQRT |
| 377 | * mismatches. */ |
| 378 | |
| 379 | /* Split finite nonzero |x| into m * 2^e with m in [2^23, 2^24). */ |
| 380 | static void omega_unpack(uint32_t u, uint32_t *m, int32_t *e) { |
| 381 | uint32_t be = (u >> 23) & 0xffU; |
| 382 | uint32_t f = u & 0x007fffffU; |
| 383 | if (be != 0) { |
| 384 | *m = f | 0x00800000U; |
| 385 | *e = (int32_t)be - 150; |
| 386 | return; |
| 387 | } |
| 388 | int32_t ex = -149; |
| 389 | while ((f & 0x00800000U) == 0) { f <<= 1; ex--; } |
| 390 | *m = f; |
| 391 | *e = ex; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Round sig * 2^exp2 (plus a nonzero amount below the last bit of sig when |
| 396 | * sticky) to FP32, ties to even. sig must be >= 2^25 so there are at least a |
| 397 | * guard bit and one more bit below the 24-bit mantissa. |
| 398 | */ |
| 399 | static uint32_t omega_round_pack(uint32_t sign, uint64_t sig, int32_t exp2, bool sticky) { |
| 400 | int32_t p = 63; |
| 401 | while (((sig >> p) & 1U) == 0) p--; |
| 402 | int32_t lead = p + exp2; /* exponent of the leading bit */ |
| 403 | int32_t be = lead + 127; /* biased exponent if normal */ |
| 404 | if (be >= 255) return sign | OMEGA_INF_POS; |
| 405 | int32_t lsb = (be >= 1) ? lead - 23 : -149; |
| 406 | int32_t s = lsb - exp2; /* bits to drop, >= 2 by the sig bound */ |
| 407 | if (s > p + 1) return sign; /* below half the smallest subnormal */ |
| 408 | uint64_t mant = sig >> s; |
| 409 | bool guard = ((sig >> (s - 1)) & 1U) != 0; |
| 410 | bool rest = sticky || (sig & ((1ULL << (s - 1)) - 1U)) != 0; |
| 411 | if (guard && (rest || (mant & 1U))) mant++; |
| 412 | /* Normal: mant in [2^23, 2^24]; a carry to 2^24 moves into the exponent. |
| 413 | * Subnormal: mant in [0, 2^23]; 2^23 is the smallest normal. */ |
| 414 | uint64_t bits = (be >= 1) ? ((uint64_t)(be - 1) << 23) + mant : mant; |
| 415 | if (bits >= OMEGA_INF_POS) return sign | OMEGA_INF_POS; |
| 416 | return sign | (uint32_t)bits; |
| 417 | } |
| 418 | |
| 419 | float omega_math_div(float x, float y) { |
| 420 | if (omega_isnan(x) || omega_isnan(y)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 421 | uint32_t sign = (omega_float_to_bits(x) ^ omega_float_to_bits(y)) & 0x80000000U; |
| 422 | bool neg = sign != 0; |
| 423 | |
| 424 | if (omega_iszero(y)) { |
| 425 | if (omega_iszero(x)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 426 | return neg ? omega_bits_to_float(OMEGA_INF_NEG) : omega_bits_to_float(OMEGA_INF_POS); |
| 427 | } |
| 428 | if (omega_iszero(x)) return neg ? -0.0f : 0.0f; |
| 429 | if (omega_isinf(x)) { |
| 430 | if (omega_isinf(y)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 431 | return neg ? omega_bits_to_float(OMEGA_INF_NEG) : omega_bits_to_float(OMEGA_INF_POS); |
| 432 | } |
| 433 | if (omega_isinf(y)) return neg ? -0.0f : 0.0f; |
| 434 | |
| 435 | uint32_t mx, my; |
| 436 | int32_t ex, ey; |
| 437 | omega_unpack(omega_float_to_bits(x), &mx, &ex); |
| 438 | omega_unpack(omega_float_to_bits(y), &my, &ey); |
| 439 | |
| 440 | /* Restoring long division: q = floor(mx * 2^26 / my), 27 quotient bits. |
| 441 | * mx / my > 1/2, so q >= 2^25. rem stays below 2^25. */ |
| 442 | uint32_t rem = mx, q = 0; |
| 443 | for (int i = 0; i < 27; i++) { |
| 444 | q <<= 1; |
| 445 | if (rem >= my) { rem -= my; q |= 1U; } |
| 446 | rem <<= 1; |
| 447 | } |
| 448 | return omega_bits_to_float(omega_round_pack(sign, q, ex - ey - 26, rem != 0)); |
| 449 | } |
| 450 | |
| 451 | float omega_math_sqrt(float x) { |
| 452 | if (omega_isnan(x)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 453 | if (omega_iszero(x)) return x; |
| 454 | if (omega_signbit(x)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 455 | if (omega_isinf(x)) return omega_bits_to_float(OMEGA_INF_POS); |
| 456 | |
| 457 | uint32_t m; |
| 458 | int32_t e; |
| 459 | omega_unpack(omega_float_to_bits(x), &m, &e); |
| 460 | uint64_t mm = m; |
| 461 | if (e & 1) { mm <<= 1; e -= 1; } /* even exponent: x = mm * 2^e */ |
| 462 | |
| 463 | /* Bitwise integer square root of M = mm * 2^28: s = floor(sqrt(M)), |
| 464 | * s in [2^25, 2^27). x = M * 2^(e-28), sqrt(x) = sqrt(M) * 2^(e/2-14). */ |
| 465 | uint64_t op = mm << 28, s = 0, one = 1ULL << 62; |
| 466 | while (one > op) one >>= 2; |
| 467 | while (one != 0) { |
| 468 | if (op >= s + one) { op -= s + one; s = (s >> 1) + one; } |
| 469 | else s >>= 1; |
| 470 | one >>= 2; |
| 471 | } |
| 472 | return omega_bits_to_float(omega_round_pack(0, s, e / 2 - 14, op != 0)); |
| 473 | } |
| 474 | |
| 475 | /* ---- Omega-Defined Exp Polynomial Sequence -------------------------------- */ |
| 476 | |
| 477 | float omega_math_exp(float x) { |
| 478 | if (omega_isnan(x)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 479 | if (x < -104.0f) return 0.0f; |
| 480 | if (x > 88.722839f) return omega_bits_to_float(OMEGA_INF_POS); |
| 481 | |
| 482 | const float INV_LN2 = 1.4426950408889634f; |
| 483 | const float LN2_HI = 0.693145751953125f; |
| 484 | const float LN2_LO = 1.4286068203094172e-06f; |
| 485 | |
| 486 | float prod = x * INV_LN2; |
| 487 | int32_t k = (prod >= 0.0f) ? (int32_t)(prod + 0.5f) : (int32_t)(prod - 0.5f); |
| 488 | float fk = (float)k; |
| 489 | float r = (x - fk * LN2_HI) - fk * LN2_LO; |
| 490 | |
| 491 | const float c2 = 0.5f; |
| 492 | const float c3 = 0.1666666716f; |
| 493 | const float c4 = 0.0416666679f; |
| 494 | const float c5 = 0.0083333338f; |
| 495 | |
| 496 | float p = r * (c4 + r * c5); |
| 497 | p = r * (c3 + p); |
| 498 | p = r * (c2 + p); |
| 499 | float poly = 1.0f + r * (1.0f + p); |
| 500 | |
| 501 | /* |
| 502 | * Scale by 2^k. k spans [-150, 128] on the admitted domain, but a single |
| 503 | * FP32 power of two only covers [-126, 127]. Split the scale so each |
| 504 | * factor is a normal power of two: the first multiply is exact, the second |
| 505 | * rounds once (into the subnormal range, or to the top binade). |
| 506 | */ |
| 507 | if (k > 127) { |
| 508 | return (poly * omega_bits_to_float(254U << 23)) * |
| 509 | omega_bits_to_float((uint32_t)(k - 127 + 127) << 23); |
| 510 | } |
| 511 | if (k < -126) { |
| 512 | return (poly * omega_bits_to_float((uint32_t)(k + 100 + 127) << 23)) * |
| 513 | omega_bits_to_float((uint32_t)(-100 + 127) << 23); |
| 514 | } |
| 515 | return poly * omega_bits_to_float((uint32_t)(k + 127) << 23); |
| 516 | } |
| 517 | |
| 518 | /* ---- Omega-Defined Log Polynomial Sequence -------------------------------- */ |
| 519 | |
| 520 | float omega_math_log(float x) { |
| 521 | if (omega_isnan(x)) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 522 | if (x < 0.0f) return omega_bits_to_float(OMEGA_QNAN_BITS); |
| 523 | if (omega_iszero(x)) return omega_bits_to_float(OMEGA_INF_NEG); |
| 524 | if (omega_isinf(x)) return omega_bits_to_float(OMEGA_INF_POS); |
| 525 | |
| 526 | int32_t e_adjust = 0; |
| 527 | if (omega_issubnormal(x)) { |
| 528 | x *= 8388608.0f; /* 2^23, exact: brings every subnormal into the normal range */ |
| 529 | e_adjust = -23; |
| 530 | } |
| 531 | |
| 532 | uint32_t u = omega_float_to_bits(x); |
| 533 | int32_t e = (int32_t)((u >> 23) & 0xff) - 127 + e_adjust; |
| 534 | float m = omega_bits_to_float((u & 0x007fffff) | 0x3f800000); |
| 535 | |
| 536 | if (m > 1.41421356f) { |
| 537 | m *= 0.5f; |
| 538 | e += 1; |
| 539 | } |
| 540 | |
| 541 | float z = omega_math_div(m - 1.0f, m + 1.0f); |
| 542 | float z2 = z * z; |
| 543 | |
| 544 | const float c1 = 0.333333343f; |
| 545 | const float c2 = 0.200000003f; |
| 546 | const float c3 = 0.142857149f; |
| 547 | |
| 548 | float poly = 1.0f + z2 * (c1 + z2 * (c2 + z2 * c3)); |
| 549 | float ln_m = 2.0f * z * poly; |
| 550 | |
| 551 | const float LN2 = 0.6931471805599453f; |
| 552 | return (float)e * LN2 + ln_m; |
| 553 | } |
| 554 | |
| 555 | /* ---- Declared-Order Warp Reduction ---------------------------------------- */ |
| 556 | |
| 557 | float omega_warp_reduce_sum(const float warp_inputs[32]) { |
| 558 | float val[32]; |
| 559 | memcpy(val, warp_inputs, sizeof(val)); |
| 560 | |
| 561 | static const int deltas[5] = { 16, 8, 4, 2, 1 }; |
| 562 | for (int s = 0; s < 5; s++) { |
| 563 | int d = deltas[s]; |
| 564 | for (int i = 0; i < 32; i++) { |
| 565 | if (i + d < 32) { |
| 566 | val[i] = val[i] + val[i + d]; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | return val[0]; |
| 571 | } |
| 572 | |
| 573 | /* ---- Op registry ---------------------------------------------------------- */ |
| 574 | |
| 575 | static const OmegaNumericOpInfo OP_TABLE[OMEGA_NOP_COUNT] = { |
| 576 | { OMEGA_NOP_FADD, "FADD", 2, true, OMEGA_CMP_BIT_EXACT, "a + b (RNE, subnormals kept)", NULL }, |
| 577 | { OMEGA_NOP_FSUB, "FSUB", 2, true, OMEGA_CMP_BIT_EXACT, "a - b (RNE, subnormals kept)", NULL }, |
| 578 | { OMEGA_NOP_FMUL, "FMUL", 2, true, OMEGA_CMP_BIT_EXACT, "a * b (RNE, subnormals kept)", NULL }, |
| 579 | { OMEGA_NOP_FFMA, "FFMA", 3, true, OMEGA_CMP_BIT_EXACT, "fma(a, b, c), c uniform per launch", NULL }, |
| 580 | { OMEGA_NOP_FSETP_SEL, "FSETP_SEL", 2, true, OMEGA_CMP_INT_EXACT, |
| 581 | "(a >= b, false if either is NaN) ? a : b, bits moved unchanged", NULL }, |
| 582 | { OMEGA_NOP_FSEL, "FSEL", 2, true, OMEGA_CMP_INT_EXACT, |
| 583 | "(a >= 0, false if a is NaN) ? b : a, bits moved unchanged", NULL }, |
| 584 | { OMEGA_NOP_FMNMX_MIN, "FMNMX_MIN", 2, true, OMEGA_CMP_BIT_EXACT, |
| 585 | "minNum(a, b), NaN yields the other operand, -0 < +0", NULL }, |
| 586 | { OMEGA_NOP_FMNMX_MAX, "FMNMX_MAX", 2, true, OMEGA_CMP_BIT_EXACT, |
| 587 | "maxNum(a, b), NaN yields the other operand, -0 < +0", NULL }, |
| 588 | { OMEGA_NOP_I2FP, "I2FP", 1, true, OMEGA_CMP_BIT_EXACT, "(float)(int32 bits of a), RNE", NULL }, |
| 589 | { OMEGA_NOP_F2I, "F2I", 1, true, OMEGA_CMP_INT_EXACT, |
| 590 | "int32 truncate of a, NaN -> 0, saturating", NULL }, |
| 591 | { OMEGA_NOP_MUFU_RCP, "MUFU_RCP", 1, true, OMEGA_CMP_SEED_BOUND, |
| 592 | "seed for 1/a: relative error bound only", NULL }, |
| 593 | { OMEGA_NOP_MUFU_RSQ, "MUFU_RSQ", 1, true, OMEGA_CMP_SEED_BOUND, |
| 594 | "seed for 1/sqrt(a): relative error bound only", NULL }, |
| 595 | { OMEGA_NOP_LDS_STS, "LDS_STS", 1, true, OMEGA_CMP_INT_EXACT, |
| 596 | "a[i ^ 63]: each thread stores a[i] to shared word tid, BAR.SYNC, then loads word tid ^ 63 " |
| 597 | "(mirror exchange inside each 64-thread CTA), bits moved unchanged", NULL }, |
| 598 | { OMEGA_NOP_SHFL_DOWN, "SHFL_DOWN", 1, true, OMEGA_CMP_INT_EXACT, |
| 599 | "a[lane+1] within each 32-lane warp, lane 31 keeps its own value", NULL }, |
| 600 | /* DIV and SQRT run the whole-program kernels of |
| 601 | * src/omega_numeric_divsqrt_gb10.c (E1 row 7), not a vecadd patch; EXP |
| 602 | * and LOG run the E1 row 10 whole-program kernels of the same file, the |
| 603 | * unfused FMUL/FADD/FSUB sequence of omega_math_exp / omega_math_log. */ |
| 604 | { OMEGA_NOP_DIV, "DIV", 2, true, OMEGA_CMP_BIT_EXACT, "correctly rounded a / b (integer long division, RNE)", NULL }, |
| 605 | { OMEGA_NOP_SQRT, "SQRT", 1, true, OMEGA_CMP_BIT_EXACT, "correctly rounded sqrt(a) (integer square root, RNE)", NULL }, |
| 606 | { OMEGA_NOP_EXP, "EXP", 1, true, OMEGA_CMP_BIT_EXACT, "omega_math_exp(a)", NULL }, |
| 607 | { OMEGA_NOP_LOG, "LOG", 1, true, OMEGA_CMP_BIT_EXACT, "omega_math_log(a)", NULL }, |
| 608 | { OMEGA_NOP_REDUCE_SUM, "REDUCE_SUM", 1, true, OMEGA_CMP_BIT_EXACT, |
| 609 | "declared pairwise-tree warp sum (" OMEGA_WARP_REDUCTION_DECLARED_ORDER "), lane 0 of each warp", NULL }, |
| 610 | /* E1 scalar contract ops (docs/numeric/E1_SCALAR_CONTRACT.md), GB10 |
| 611 | * encodings added by E1 WP-C. Compare-and-select: FSETP.<P> P0, a, b ; |
| 612 | * FSEL out, a, b, P0, bits moved unchanged. */ |
| 613 | #define E1_SEL(op_, name_, text_) \ |
| 614 | { op_, name_, 2, true, OMEGA_CMP_INT_EXACT, text_ " ? a : b, bits moved unchanged", NULL } |
| 615 | E1_SEL(OMEGA_NOP_FSETP_LT_SEL, "FSETP_LT_SEL", "(a < b, false if either is NaN)"), |
| 616 | E1_SEL(OMEGA_NOP_FSETP_LE_SEL, "FSETP_LE_SEL", "(a <= b, false if either is NaN)"), |
| 617 | E1_SEL(OMEGA_NOP_FSETP_GT_SEL, "FSETP_GT_SEL", "(a > b, false if either is NaN)"), |
| 618 | E1_SEL(OMEGA_NOP_FSETP_EQ_SEL, "FSETP_EQ_SEL", "(a == b, false if either is NaN, -0 == +0)"), |
| 619 | E1_SEL(OMEGA_NOP_FSETP_NE_SEL, "FSETP_NE_SEL", "(a != b, false if either is NaN, -0 == +0)"), |
| 620 | E1_SEL(OMEGA_NOP_FSETP_NUM_SEL, "FSETP_NUM_SEL", "(neither a nor b is NaN)"), |
| 621 | E1_SEL(OMEGA_NOP_FSETP_NAN_SEL, "FSETP_NAN_SEL", "(a or b is NaN)"), |
| 622 | E1_SEL(OMEGA_NOP_FSETP_LTU_SEL, "FSETP_LTU_SEL", "(a < b, true if either is NaN)"), |
| 623 | E1_SEL(OMEGA_NOP_FSETP_LEU_SEL, "FSETP_LEU_SEL", "(a <= b, true if either is NaN)"), |
| 624 | E1_SEL(OMEGA_NOP_FSETP_GTU_SEL, "FSETP_GTU_SEL", "(a > b, true if either is NaN)"), |
| 625 | E1_SEL(OMEGA_NOP_FSETP_GEU_SEL, "FSETP_GEU_SEL", "(a >= b, true if either is NaN)"), |
| 626 | E1_SEL(OMEGA_NOP_FSETP_EQU_SEL, "FSETP_EQU_SEL", "(a == b, true if either is NaN, -0 == +0)"), |
| 627 | E1_SEL(OMEGA_NOP_FSETP_NEU_SEL, "FSETP_NEU_SEL", "(a != b, true if either is NaN, -0 == +0)"), |
| 628 | #undef E1_SEL |
| 629 | { OMEGA_NOP_F2I_FLOOR, "F2I_FLOOR", 1, true, OMEGA_CMP_INT_EXACT, |
| 630 | "int32 floor of a, NaN -> 0, saturating", NULL }, |
| 631 | { OMEGA_NOP_F2I_CEIL, "F2I_CEIL", 1, true, OMEGA_CMP_INT_EXACT, |
| 632 | "int32 ceiling of a, NaN -> 0, saturating", NULL }, |
| 633 | { OMEGA_NOP_F2I_RNI, "F2I_RNI", 1, true, OMEGA_CMP_INT_EXACT, |
| 634 | "int32 round-to-nearest-even of a, NaN -> 0, saturating", NULL }, |
| 635 | { OMEGA_NOP_F2U, "F2U", 1, true, OMEGA_CMP_INT_EXACT, |
| 636 | "uint32 truncate of a, NaN -> 0, negatives -> 0, saturating", NULL }, |
| 637 | { OMEGA_NOP_I2FP_U32, "I2FP_U32", 1, true, OMEGA_CMP_BIT_EXACT, |
| 638 | "(float)(uint32 bits of a), RNE", NULL }, |
| 639 | { OMEGA_NOP_F32_TO_F16, "F32_TO_F16", 1, true, OMEGA_CMP_F16_BITS, |
| 640 | "binary16 of a in bits [15:0] (RNE, overflow to inf, subnormals kept, NaN stays NaN), bits [31:16] zero", NULL }, |
| 641 | { OMEGA_NOP_F32_TO_BF16, "F32_TO_BF16", 1, true, OMEGA_CMP_BF16_BITS, |
| 642 | "bfloat16 of a in bits [15:0] (RNE, overflow to inf, subnormals kept, NaN stays NaN), bits [31:16] zero", NULL }, |
| 643 | { OMEGA_NOP_F16_TO_F32, "F16_TO_F32", 1, true, OMEGA_CMP_BIT_EXACT, |
| 644 | "exact FP32 of the binary16 in bits [15:0] of a (bits [31:16] ignored)", NULL }, |
| 645 | { OMEGA_NOP_BF16_TO_F32, "BF16_TO_F32", 1, true, OMEGA_CMP_BIT_EXACT, |
| 646 | "exact FP32 of the bfloat16 in bits [15:0] of a (bits [31:16] ignored)", NULL }, |
| 647 | { OMEGA_NOP_FFMA_V, "FFMA_V", 3, true, OMEGA_CMP_BIT_EXACT, "fma(a, b, c), c read per element", NULL }, |
| 648 | }; |
| 649 | |
| 650 | size_t omega_numeric_op_count(void) { return OMEGA_NOP_COUNT; } |
| 651 | |
| 652 | bool omega_numeric_op_whole_kernel(OmegaNumericOp op) { |
| 653 | return op == OMEGA_NOP_DIV || op == OMEGA_NOP_SQRT || op == OMEGA_NOP_EXP || op == OMEGA_NOP_LOG; |
| 654 | } |
| 655 | |
| 656 | const OmegaNumericOpInfo *omega_numeric_op_at(size_t index) { |
| 657 | return index < OMEGA_NOP_COUNT ? &OP_TABLE[index] : NULL; |
| 658 | } |
| 659 | |
| 660 | const OmegaNumericOpInfo *omega_numeric_op_find(const char *name) { |
| 661 | if (!name) return NULL; |
| 662 | if (strcmp(name, "LDS") == 0 || strcmp(name, "STS") == 0) name = "LDS_STS"; |
| 663 | if (strcmp(name, "FSETP") == 0) name = "FSETP_SEL"; |
| 664 | for (size_t i = 0; i < OMEGA_NOP_COUNT; i++) { |
| 665 | if (strcmp(OP_TABLE[i].name, name) == 0) return &OP_TABLE[i]; |
| 666 | } |
| 667 | return NULL; |
| 668 | } |
| 669 | |
| 670 | const char *omega_numeric_compare_name(OmegaNumericCompare c) { |
| 671 | switch (c) { |
| 672 | case OMEGA_CMP_BIT_EXACT: return "BIT_EXACT"; |
| 673 | case OMEGA_CMP_INT_EXACT: return "INT_EXACT"; |
| 674 | case OMEGA_CMP_SEED_BOUND: return "SEED_BOUND"; |
| 675 | case OMEGA_CMP_F16_BITS: return "F16_BITS"; |
| 676 | case OMEGA_CMP_BF16_BITS: return "BF16_BITS"; |
| 677 | } |
| 678 | return "UNKNOWN"; |
| 679 | } |
| 680 | |
| 681 | bool omega_numeric_compare_equal(OmegaNumericCompare mode, uint32_t expect, uint32_t got) { |
| 682 | switch (mode) { |
| 683 | case OMEGA_CMP_BIT_EXACT: |
| 684 | return omega_numeric_bits_equal(omega_bits_to_float(expect), omega_bits_to_float(got)); |
| 685 | case OMEGA_CMP_INT_EXACT: |
| 686 | return expect == got; |
| 687 | case OMEGA_CMP_F16_BITS: |
| 688 | case OMEGA_CMP_BF16_BITS: { |
| 689 | /* 16-bit payload in [15:0]; [31:16] must be zero in both words. */ |
| 690 | if ((expect >> 16) != 0 || (got >> 16) != 0) return false; |
| 691 | uint32_t inf = (mode == OMEGA_CMP_F16_BITS) ? 0x7c00U : 0x7f80U; |
| 692 | bool en = (expect & 0x7fffU) > inf, gn = (got & 0x7fffU) > inf; |
| 693 | if (en || gn) return en && gn; /* NaN class: any NaN equals any NaN */ |
| 694 | return expect == got; |
| 695 | } |
| 696 | case OMEGA_CMP_SEED_BOUND: |
| 697 | return false; /* never compared bit for bit */ |
| 698 | } |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | /* ---- Refused variants ------------------------------------------------------ */ |
| 703 | |
| 704 | static const struct { const char *name; const char *reason; } REFUSED_VARIANTS[] = { |
| 705 | { "LDS.U8", "only the 32-bit LDS form is encoded (the old byte-wide words were never a round trip)" }, |
| 706 | { "STS.U8", "only the 32-bit STS form is encoded (the old byte-wide words were never a round trip)" }, |
| 707 | { "LDS.S8", "only the 32-bit LDS form is encoded" }, |
| 708 | { "LDS.U16", "only the 32-bit LDS form is encoded" }, |
| 709 | { "STS.U16", "only the 32-bit STS form is encoded" }, |
| 710 | { "LDS.64", "only the 32-bit LDS form is encoded (no 64-bit register pair form)" }, |
| 711 | { "STS.64", "only the 32-bit STS form is encoded (no 64-bit register pair form)" }, |
| 712 | { "LDS.128", "only the 32-bit LDS form is encoded (no 128-bit form)" }, |
| 713 | { "STS.128", "only the 32-bit STS form is encoded (no 128-bit form)" }, |
| 714 | { "SHFL_UP", "only SHFL.DOWN is encoded" }, |
| 715 | { "SHFL_BFLY", "only SHFL.DOWN is encoded" }, |
| 716 | { "SHFL_IDX", "only SHFL.DOWN is encoded" }, |
| 717 | { "REDUCE_MAX", "only the declared-order FP32 sum is encoded" }, |
| 718 | { "REDUCE_MIN", "only the declared-order FP32 sum is encoded" }, |
| 719 | { "REDUCE_SUM_BLOCK", "only the per-warp (32-lane) sum is encoded, not a CTA-wide one" }, |
| 720 | }; |
| 721 | #define REFUSED_VARIANT_COUNT (sizeof(REFUSED_VARIANTS) / sizeof(REFUSED_VARIANTS[0])) |
| 722 | |
| 723 | size_t omega_numeric_refused_variant_count(void) { return REFUSED_VARIANT_COUNT; } |
| 724 | const char *omega_numeric_refused_variant_at(size_t index) { |
| 725 | return index < REFUSED_VARIANT_COUNT ? REFUSED_VARIANTS[index].name : NULL; |
| 726 | } |
| 727 | |
| 728 | void omega_numeric_launch_shape(size_t count, uint32_t *threads_per_block, uint32_t *grid_width) { |
| 729 | uint32_t g = (uint32_t)((count + OMEGA_NUMERIC_CTA_THREADS - 1u) / OMEGA_NUMERIC_CTA_THREADS); |
| 730 | if (threads_per_block) *threads_per_block = OMEGA_NUMERIC_CTA_THREADS; |
| 731 | if (grid_width) *grid_width = g ? g : 1u; |
| 732 | } |
| 733 | |
| 734 | static int refuse(char *err, size_t err_len, int rc, const char *fmt, const char *a, const char *b) { |
| 735 | if (err && err_len) snprintf(err, err_len, fmt, a ? a : "", b ? b : ""); |
| 736 | return rc; |
| 737 | } |
| 738 | |
| 739 | int omega_numeric_submit_check(const char *op_name, |
| 740 | const float *in_a, const float *in_b, const float *in_c, |
| 741 | const float *out_res, size_t count, |
| 742 | char *err, size_t err_len) { |
| 743 | if (err && err_len) err[0] = '\0'; |
| 744 | for (size_t v = 0; op_name && v < REFUSED_VARIANT_COUNT; v++) { |
| 745 | if (strcmp(op_name, REFUSED_VARIANTS[v].name) == 0) return refuse(err, err_len, OMEGA_NUMERIC_ERR_NOT_ENCODED, "variant %s is not encoded for GB10: %s", op_name, REFUSED_VARIANTS[v].reason); /* CHECK:variant_refused */ |
| 746 | } |
| 747 | const OmegaNumericOpInfo *info = omega_numeric_op_find(op_name); |
| 748 | if (!info) { |
| 749 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, |
| 750 | "unknown numeric op '%s'%s", op_name ? op_name : "(null)", NULL); |
| 751 | } |
| 752 | if (!info->gb10_encoded) { |
| 753 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_NOT_ENCODED, |
| 754 | "op %s is not encoded for GB10: %s", info->name, info->not_encoded_reason); |
| 755 | } |
| 756 | if (!in_a || !out_res || count == 0 || count > OMEGA_NUMERIC_MAX_COUNT) { |
| 757 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, |
| 758 | "op %s: missing input/output buffer or count out of range%s", info->name, NULL); |
| 759 | } |
| 760 | if (info->arity >= 2 && !in_b) { |
| 761 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, |
| 762 | "op %s needs a second input%s", info->name, NULL); |
| 763 | } |
| 764 | if (info->arity >= 3 && !in_c) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "op %s needs a third input (c)%s", info->name, NULL); /* CHECK:third_input */ |
| 765 | if (info->op == OMEGA_NOP_FFMA) { |
| 766 | if (!in_c) { |
| 767 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, |
| 768 | "op %s needs the c input%s", info->name, NULL); |
| 769 | } |
| 770 | uint32_t c0 = omega_float_to_bits(in_c[0]); |
| 771 | for (size_t i = 1; i < count; i++) { |
| 772 | if (omega_float_to_bits(in_c[i]) != c0) { |
| 773 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, |
| 774 | "op %s: c must be the same value in every element (it is carried " |
| 775 | "in the constant bank)%s", info->name, NULL); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | if ((info->op == OMEGA_NOP_SHFL_DOWN || info->op == OMEGA_NOP_REDUCE_SUM) && (count % 32u) != 0) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "op %s: count must be a multiple of 32 (whole warps only)%s", info->name, NULL); /* CHECK:warp_count */ |
| 780 | if (info->op == OMEGA_NOP_LDS_STS && (count % OMEGA_NUMERIC_CTA_THREADS) != 0) return refuse(err, err_len, OMEGA_NUMERIC_ERR_OPERANDS, "op %s: count must be a multiple of the CTA size 64 (whole CTAs: every thread must reach BAR.SYNC)%s", info->name, NULL); /* CHECK:cta_count */ |
| 781 | /* DIV/SQRT/EXP/LOG: whole-program kernels; omega_ds_gb10_run checks the |
| 782 | * kernel image, its nvdisasm-verified digest and the QMD before submission. */ |
| 783 | if (omega_numeric_op_whole_kernel(info->op)) return OMEGA_NUMERIC_OK; |
| 784 | /* The patch this op would submit, checked against a QMD for this launch. */ |
| 785 | OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX]; |
| 786 | int n = omega_numeric_patch_words(info->op, patch); |
| 787 | if (n <= 0) { |
| 788 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_NOT_ENCODED, |
| 789 | "op %s has no patch words%s", info->name, NULL); |
| 790 | } |
| 791 | uint32_t qmd1[OMEGA_BW_QMD_WORDS]; |
| 792 | OmegaBlackwellQmdConfig cfg; |
| 793 | memset(&cfg, 0, sizeof(cfg)); |
| 794 | cfg.num_elements = (uint32_t)count; |
| 795 | omega_numeric_launch_shape(count, &cfg.threads_per_block, &cfg.grid_width); |
| 796 | if (omega_blackwell_build_qmd1(qmd1, &cfg) != 0) { |
| 797 | return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "op %s: launch QMD could not be built%s", |
| 798 | info->name, NULL); |
| 799 | } |
| 800 | return omega_numeric_check_patch(info->op, patch, n, qmd1, err, err_len); |
| 801 | } |
| 802 | |
| 803 | /* ---- Kernel patch words ---------------------------------------------------- |
| 804 | * Register map of the vecadd baseline (src/omega_blackwell_encoder.c): |
| 805 | * R2 = a[i] and R5 = b[i] (both LDG.E, write barrier SB4), R1 = c[0x0][0x37c] |
| 806 | * (driver constant word 223, set per launch), R6:R7 = &out[i], R9 = result. |
| 807 | * Control word fields (w3): stall [12:9], yield [13], write barrier [16:14], |
| 808 | * read barrier [19:17], wait mask [25:20]. |
| 809 | * 0x010fca00 fixed-latency op, waits SB4 (the loads of a and b) |
| 810 | * 0x010e2800 variable-latency op (MUFU, F2I, SHFL), waits SB4, sets SB0 |
| 811 | * 0x001fe200 STG that waits SB0 (after a variable-latency op) |
| 812 | * 0x000fe200 STG with no wait (after a fixed-latency op, as in the baseline) |
| 813 | * Every instruction word below was decoded with nvdisasm 13.0.88 -b SM121 on |
| 814 | * 2026-09-30 (text in the provenance table); control words follow the |
| 815 | * scoreboard pattern ptxas 13.0.88 emits for sm_121 for the same op followed |
| 816 | * by STG. GB10 parity is established only by the chip receipt. |
| 817 | */ |
| 818 | |
| 819 | #define W_EXIT { 0x0000794dU, 0x00000000U, 0x03800000U, 0x000fea00U } |
| 820 | #define W_STG0 { 0x06007986U, 0x00000009U, 0x0c101904U, 0x000fe200U } |
| 821 | #define W_STG1 { 0x06007986U, 0x00000009U, 0x0c101904U, 0x001fe200U } |
| 822 | |
| 823 | #define CTRL_FIXED 0x010fca00U |
| 824 | #define CTRL_VAR 0x010e2800U |
| 825 | #define CTRL_SHFL_NEXT 0x000e2400U /* later SHFL.DOWN: sets SB0, no wait (FADD before it is fixed latency) */ |
| 826 | #define CTRL_FADD_SB0 0x001fca00U /* FADD that waits SB0 (the shuffled value) */ |
| 827 | |
| 828 | int omega_numeric_patch_words(OmegaNumericOp op, |
| 829 | OmegaNumericPatchInsn out[OMEGA_NUMERIC_PATCH_MAX]) { |
| 830 | static const OmegaNumericPatchInsn STG0 = { W_STG0, "STG.E desc[UR4][R6.64], R9", NULL }; |
| 831 | static const OmegaNumericPatchInsn STG1 = { W_STG1, "STG.E desc[UR4][R6.64], R9 (waits SB0)", NULL }; |
| 832 | static const OmegaNumericPatchInsn EXIT = { W_EXIT, "EXIT", NULL }; |
| 833 | int n = 0; |
| 834 | #define PUT(w0_, w1_, w2_, w3_, text_, key_) \ |
| 835 | do { out[n].w[0] = (w0_); out[n].w[1] = (w1_); out[n].w[2] = (w2_); out[n].w[3] = (w3_); \ |
| 836 | out[n].text = (text_); out[n].provenance_key = (key_); n++; } while (0) |
| 837 | switch (op) { |
| 838 | case OMEGA_NOP_FADD: |
| 839 | PUT(0x02097221U, 0x00000005U, 0x00000000U, CTRL_FIXED, "FADD R9, R2, R5", "FADD"); |
| 840 | return n; |
| 841 | case OMEGA_NOP_FSUB: |
| 842 | PUT(0x02097221U, 0x80000005U, 0x00000000U, CTRL_FIXED, "FADD R9, R2, -R5", "FSUB"); |
| 843 | return n; |
| 844 | case OMEGA_NOP_FMUL: |
| 845 | PUT(0x02097220U, 0x00000005U, 0x00400000U, CTRL_FIXED, "FMUL R9, R2, R5", "FMUL"); |
| 846 | return n; |
| 847 | case OMEGA_NOP_FFMA: |
| 848 | PUT(0x02097223U, 0x00000005U, 0x00000001U, CTRL_FIXED, "FFMA R9, R2, R5, R1", "FFMA"); |
| 849 | return n; |
| 850 | case OMEGA_NOP_FSETP_SEL: |
| 851 | PUT(0x0200720bU, 0x00000005U, 0x03f06000U, CTRL_FIXED, |
| 852 | "FSETP.GE.AND P0, PT, R2, R5, PT", "FSETP_GE_R2_R5"); |
| 853 | PUT(0x02097208U, 0x00000005U, 0x00000000U, 0x000fca00U, "FSEL R9, R2, R5, P0", "FSEL_R2_R5_P0"); |
| 854 | out[n++] = STG0; |
| 855 | out[n++] = EXIT; |
| 856 | return n; |
| 857 | case OMEGA_NOP_FSEL: |
| 858 | PUT(0x0200720bU, 0x000000ffU, 0x03f06000U, CTRL_FIXED, |
| 859 | "FSETP.GE.AND P0, PT, R2, RZ, PT", "FSETP_GE_R2_RZ"); |
| 860 | PUT(0x05097208U, 0x00000002U, 0x00000000U, 0x000fca00U, "FSEL R9, R5, R2, P0", "FSEL_R5_R2_P0"); |
| 861 | out[n++] = STG0; |
| 862 | out[n++] = EXIT; |
| 863 | return n; |
| 864 | case OMEGA_NOP_FMNMX_MIN: |
| 865 | PUT(0x02097209U, 0x00000005U, 0x03800000U, CTRL_FIXED, "FMNMX R9, R2, R5, PT", "FMNMX_MIN"); |
| 866 | return n; |
| 867 | case OMEGA_NOP_FMNMX_MAX: |
| 868 | PUT(0x02097209U, 0x00000005U, 0x07800000U, CTRL_FIXED, "FMNMX R9, R2, R5, !PT", "FMNMX_MAX"); |
| 869 | return n; |
| 870 | case OMEGA_NOP_I2FP: |
| 871 | PUT(0x00097245U, 0x00000002U, 0x00201400U, CTRL_FIXED, "I2FP.F32.S32 R9, R2", "I2FP"); |
| 872 | return n; |
| 873 | case OMEGA_NOP_F2I: |
| 874 | PUT(0x00097305U, 0x00000002U, 0x0020f100U, CTRL_VAR, "F2I.TRUNC.NTZ R9, R2", "F2I"); |
| 875 | out[n++] = STG1; |
| 876 | out[n++] = EXIT; |
| 877 | return n; |
| 878 | case OMEGA_NOP_MUFU_RCP: |
| 879 | PUT(0x00097308U, 0x00000002U, 0x00001000U, CTRL_VAR, "MUFU.RCP R9, R2", "MUFU_RCP"); |
| 880 | out[n++] = STG1; |
| 881 | out[n++] = EXIT; |
| 882 | return n; |
| 883 | case OMEGA_NOP_MUFU_RSQ: |
| 884 | PUT(0x00097308U, 0x00000002U, 0x00001400U, CTRL_VAR, "MUFU.RSQ R9, R2", "MUFU_RSQ"); |
| 885 | out[n++] = STG1; |
| 886 | out[n++] = EXIT; |
| 887 | return n; |
| 888 | case OMEGA_NOP_SHFL_DOWN: |
| 889 | PUT(0x02097f89U, 0x08201f00U, 0x000e0000U, CTRL_VAR, "SHFL.DOWN PT, R9, R2, 0x1, 0x1f", "SHFL_DOWN_1"); |
| 890 | out[n++] = STG1; |
| 891 | out[n++] = EXIT; |
| 892 | return n; |
| 893 | case OMEGA_NOP_LDS_STS: |
| 894 | /* Shared window: raw byte offsets from 0 through URZ, as NVK/NAK emit. |
| 895 | * ptxas instead forms a base from SR_CgaCtaId and 0x400; if the chip |
| 896 | * disagrees with the reference, that addressing is the first suspect. */ |
| 897 | PUT(0x00087819U, 0x00000002U, 0x000006ffU, CTRL_FIXED, "SHF.L.U32 R8, R0, 0x2, RZ", "SHF_L_R8_R0_2"); |
| 898 | PUT(0x080a7812U, 0x000000fcU, 0x078e3cffU, 0x000fca00U, |
| 899 | "LOP3.LUT R10, R8, 0xfc, RZ, 0x3c, !PT", "LOP3_R10_R8_XOR_FC"); |
| 900 | PUT(0x08007988U, 0x00000002U, 0x080008ffU, 0x010fe200U, "STS [R8+URZ], R2", "STS_R8_R2"); |
| 901 | PUT(0x00007b1dU, 0x00000000U, 0x00010000U, 0x000fec00U, "BAR.SYNC.DEFER_BLOCKING 0x0", "BAR_SYNC_0"); |
| 902 | PUT(0x0a097984U, 0x000000ffU, 0x08000800U, 0x000e2800U, "LDS R9, [R10+URZ]", "LDS_R9_R10"); |
| 903 | out[n++] = STG1; |
| 904 | out[n++] = EXIT; |
| 905 | return n; |
| 906 | case OMEGA_NOP_REDUCE_SUM: |
| 907 | /* Declared order: val[i] += val[i + d] for d = 16, 8, 4, 2, 1. Lanes |
| 908 | * with i + d >= 32 read their own value (clamp 0x1f) and never feed |
| 909 | * lane 0, so lane 0 is the declared sum. Only lane 0 is compared. */ |
| 910 | PUT(0x02097f89U, 0x0a001f00U, 0x000e0000U, CTRL_VAR, "SHFL.DOWN PT, R9, R2, 0x10, 0x1f", "SHFL_DOWN_16"); |
| 911 | PUT(0x02027221U, 0x00000009U, 0x00000000U, CTRL_FADD_SB0, "FADD R2, R2, R9", "FADD_R2_R2_R9"); |
| 912 | PUT(0x02097f89U, 0x09001f00U, 0x000e0000U, CTRL_SHFL_NEXT, "SHFL.DOWN PT, R9, R2, 0x8, 0x1f", "SHFL_DOWN_8"); |
| 913 | PUT(0x02027221U, 0x00000009U, 0x00000000U, CTRL_FADD_SB0, "FADD R2, R2, R9", "FADD_R2_R2_R9"); |
| 914 | PUT(0x02097f89U, 0x08801f00U, 0x000e0000U, CTRL_SHFL_NEXT, "SHFL.DOWN PT, R9, R2, 0x4, 0x1f", "SHFL_DOWN_4"); |
| 915 | PUT(0x02027221U, 0x00000009U, 0x00000000U, CTRL_FADD_SB0, "FADD R2, R2, R9", "FADD_R2_R2_R9"); |
| 916 | PUT(0x02097f89U, 0x08401f00U, 0x000e0000U, CTRL_SHFL_NEXT, "SHFL.DOWN PT, R9, R2, 0x2, 0x1f", "SHFL_DOWN_2"); |
| 917 | PUT(0x02027221U, 0x00000009U, 0x00000000U, CTRL_FADD_SB0, "FADD R2, R2, R9", "FADD_R2_R2_R9"); |
| 918 | PUT(0x02097f89U, 0x08201f00U, 0x000e0000U, CTRL_SHFL_NEXT, "SHFL.DOWN PT, R9, R2, 0x1, 0x1f", "SHFL_DOWN_1"); |
| 919 | PUT(0x02097221U, 0x00000009U, 0x00000000U, CTRL_FADD_SB0, "FADD R9, R2, R9", "FADD_R9_R2_R9"); |
| 920 | out[n++] = STG0; |
| 921 | out[n++] = EXIT; |
| 922 | return n; |
| 923 | case OMEGA_NOP_DIV: |
| 924 | case OMEGA_NOP_SQRT: |
| 925 | case OMEGA_NOP_EXP: |
| 926 | case OMEGA_NOP_LOG: |
| 927 | return OMEGA_NUMERIC_ERR_NOT_ENCODED; |
| 928 | /* ---- E1 scalar contract ops (E1 WP-C). Every word decoded with nvdisasm |
| 929 | * 13.0.88 -b SM121 (2026-09-30); control words follow ptxas 13.0.88 sm_121 |
| 930 | * output for the same op stored with STG: FSETP/FSEL, I2FP.U32, HADD2.F32 |
| 931 | * and SHF are fixed latency (stall 5 before the baseline STG); F2I and F2F |
| 932 | * set write barrier SB0 and the STG waits on it; the FFMA_V prologue |
| 933 | * (LDC.64, IMAD.WIDE.U32, LDG) mirrors the vecadd a/b address sequence. */ |
| 934 | #define E1_FSETP_SEL(op_, cmp_, text_, key_) \ |
| 935 | case op_: \ |
| 936 | PUT(0x0200720bU, 0x00000005U, 0x03f00000U | ((uint32_t)(cmp_) << 12), CTRL_FIXED, text_, key_); \ |
| 937 | PUT(0x02097208U, 0x00000005U, 0x00000000U, 0x000fca00U, "FSEL R9, R2, R5, P0", "FSEL_R2_R5_P0"); \ |
| 938 | out[n++] = STG0; \ |
| 939 | out[n++] = EXIT; \ |
| 940 | return n; |
| 941 | E1_FSETP_SEL(OMEGA_NOP_FSETP_LT_SEL, 0x1, "FSETP.LT.AND P0, PT, R2, R5, PT", "FSETP_LT_R2_R5") |
| 942 | E1_FSETP_SEL(OMEGA_NOP_FSETP_EQ_SEL, 0x2, "FSETP.EQ.AND P0, PT, R2, R5, PT", "FSETP_EQ_R2_R5") |
| 943 | E1_FSETP_SEL(OMEGA_NOP_FSETP_LE_SEL, 0x3, "FSETP.LE.AND P0, PT, R2, R5, PT", "FSETP_LE_R2_R5") |
| 944 | E1_FSETP_SEL(OMEGA_NOP_FSETP_GT_SEL, 0x4, "FSETP.GT.AND P0, PT, R2, R5, PT", "FSETP_GT_R2_R5") |
| 945 | E1_FSETP_SEL(OMEGA_NOP_FSETP_NE_SEL, 0x5, "FSETP.NE.AND P0, PT, R2, R5, PT", "FSETP_NE_R2_R5") |
| 946 | E1_FSETP_SEL(OMEGA_NOP_FSETP_NUM_SEL, 0x7, "FSETP.NUM.AND P0, PT, R2, R5, PT", "FSETP_NUM_R2_R5") |
| 947 | E1_FSETP_SEL(OMEGA_NOP_FSETP_NAN_SEL, 0x8, "FSETP.NAN.AND P0, PT, R2, R5, PT", "FSETP_NAN_R2_R5") |
| 948 | E1_FSETP_SEL(OMEGA_NOP_FSETP_LTU_SEL, 0x9, "FSETP.LTU.AND P0, PT, R2, R5, PT", "FSETP_LTU_R2_R5") |
| 949 | E1_FSETP_SEL(OMEGA_NOP_FSETP_EQU_SEL, 0xa, "FSETP.EQU.AND P0, PT, R2, R5, PT", "FSETP_EQU_R2_R5") |
| 950 | E1_FSETP_SEL(OMEGA_NOP_FSETP_LEU_SEL, 0xb, "FSETP.LEU.AND P0, PT, R2, R5, PT", "FSETP_LEU_R2_R5") |
| 951 | E1_FSETP_SEL(OMEGA_NOP_FSETP_GTU_SEL, 0xc, "FSETP.GTU.AND P0, PT, R2, R5, PT", "FSETP_GTU_R2_R5") |
| 952 | E1_FSETP_SEL(OMEGA_NOP_FSETP_NEU_SEL, 0xd, "FSETP.NEU.AND P0, PT, R2, R5, PT", "FSETP_NEU_R2_R5") |
| 953 | E1_FSETP_SEL(OMEGA_NOP_FSETP_GEU_SEL, 0xe, "FSETP.GEU.AND P0, PT, R2, R5, PT", "FSETP_GEU_R2_R5") |
| 954 | #undef E1_FSETP_SEL |
| 955 | case OMEGA_NOP_F2I_FLOOR: |
| 956 | PUT(0x00097305U, 0x00000002U, 0x00207100U, CTRL_VAR, "F2I.FLOOR.NTZ R9, R2", "F2I_FLOOR"); |
| 957 | out[n++] = STG1; |
| 958 | out[n++] = EXIT; |
| 959 | return n; |
| 960 | case OMEGA_NOP_F2I_CEIL: |
| 961 | PUT(0x00097305U, 0x00000002U, 0x0020b100U, CTRL_VAR, "F2I.CEIL.NTZ R9, R2", "F2I_CEIL"); |
| 962 | out[n++] = STG1; |
| 963 | out[n++] = EXIT; |
| 964 | return n; |
| 965 | case OMEGA_NOP_F2I_RNI: |
| 966 | PUT(0x00097305U, 0x00000002U, 0x00203100U, CTRL_VAR, "F2I.NTZ R9, R2", "F2I_RNI"); |
| 967 | out[n++] = STG1; |
| 968 | out[n++] = EXIT; |
| 969 | return n; |
| 970 | case OMEGA_NOP_F2U: |
| 971 | PUT(0x00097305U, 0x00000002U, 0x0020f000U, CTRL_VAR, "F2I.U32.TRUNC.NTZ R9, R2", "F2U"); |
| 972 | out[n++] = STG1; |
| 973 | out[n++] = EXIT; |
| 974 | return n; |
| 975 | case OMEGA_NOP_I2FP_U32: |
| 976 | PUT(0x00097245U, 0x00000002U, 0x00201000U, CTRL_FIXED, "I2FP.F32.U32 R9, R2", "I2FP_U32"); |
| 977 | return n; |
| 978 | case OMEGA_NOP_F32_TO_F16: |
| 979 | /* ptxas stores cvt.rn.f16.f32 + cvt.u32.u16 with no mask after F2F, |
| 980 | * i.e. it relies on F2F writing [31:16] = 0. F16_BITS parity requires |
| 981 | * [31:16] == 0 on every chip word, so the receipt is the check. */ |
| 982 | PUT(0x00097304U, 0x00000002U, 0x00200800U, CTRL_VAR, "F2F.F16.F32 R9, R2", "F2F_F16_F32"); |
| 983 | out[n++] = STG1; |
| 984 | out[n++] = EXIT; |
| 985 | return n; |
| 986 | case OMEGA_NOP_F32_TO_BF16: |
| 987 | PUT(0x00097304U, 0x00000002U, 0x00202000U, CTRL_VAR, "F2F.BF16.F32 R9, R2", "F2F_BF16_F32"); |
| 988 | out[n++] = STG1; |
| 989 | out[n++] = EXIT; |
| 990 | return n; |
| 991 | case OMEGA_NOP_F16_TO_F32: |
| 992 | /* -0 + h widened to f32: the sequence ptxas emits for cvt.f32.f16. */ |
| 993 | PUT(0xff097230U, 0x20000002U, 0x00004100U, CTRL_FIXED, "HADD2.F32 R9, -RZ, R2.H0_H0", "HADD2_F32_R2_H0"); |
| 994 | return n; |
| 995 | case OMEGA_NOP_BF16_TO_F32: |
| 996 | /* A 16-bit left shift, as ptxas emits for cvt.f32.bf16. Exact by |
| 997 | * definition; NOT an independent hardware conversion unit. */ |
| 998 | PUT(0x02097819U, 0x00000010U, 0x000006ffU, CTRL_FIXED, "SHF.L.U32 R9, R2, 0x10, RZ", "SHF_L_R9_R2_16"); |
| 999 | return n; |
| 1000 | case OMEGA_NOP_FFMA_V: |
| 1001 | /* c pointer = kernel argument words 8..9 at c[0x0][0x3a0] (the executor |
| 1002 | * uploads it there and copies in_c to device memory); c[i] is loaded |
| 1003 | * like a[i] and b[i], then FFMA. R10/R11 are free in the baseline. */ |
| 1004 | PUT(0xff0a7b82U, 0x0000e800U, 0x00000a00U, CTRL_VAR, "LDC.64 R10, c[0x0][0x3a0]", "LDC64_R10_C3A0"); |
| 1005 | PUT(0x090a7825U, 0x00000004U, 0x078e000aU, 0x001fcc00U, "IMAD.WIDE.U32 R10, R9, 0x4, R10", "IMAD_WIDE_R10_R9_4"); |
| 1006 | PUT(0x0a0b7981U, 0x00000004U, 0x0c1e1900U, 0x000e2800U, "LDG.E R11, desc[UR4][R10.64]", "LDG_R11_R10"); |
| 1007 | PUT(0x02097223U, 0x00000005U, 0x0000000bU, CTRL_FADD_SB0, "FFMA R9, R2, R5, R11", "FFMA_R9_R2_R5_R11"); |
| 1008 | out[n++] = STG0; |
| 1009 | out[n++] = EXIT; |
| 1010 | return n; |
| 1011 | case OMEGA_NOP_COUNT: |
| 1012 | break; |
| 1013 | } |
| 1014 | #undef PUT |
| 1015 | return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1016 | } |
| 1017 | |
| 1018 | int omega_numeric_build_kernel(OmegaNumericOp op, uint8_t *code, size_t code_len, size_t *out_len) { |
| 1019 | OmegaNumericPatchInsn patch[OMEGA_NUMERIC_PATCH_MAX]; |
| 1020 | int n = omega_numeric_patch_words(op, patch); |
| 1021 | if (n <= 0) return n < 0 ? n : OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1022 | size_t len = 0; |
| 1023 | if (omega_blackwell_encode_vecadd(code, code_len, &len) != 0) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1024 | if (len < OMEGA_NUMERIC_PATCH_OFFSET + (size_t)n * 16u) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1025 | for (int i = 0; i < n; i++) { |
| 1026 | memcpy(code + OMEGA_NUMERIC_PATCH_OFFSET + (size_t)i * 16u, patch[i].w, 16); |
| 1027 | } |
| 1028 | if (out_len) *out_len = len; |
| 1029 | return OMEGA_NUMERIC_OK; |
| 1030 | } |
| 1031 | |
| 1032 | /* ---- Structural patch check (before any device is touched) ---------------- |
| 1033 | * Each refusal is one line ending in a CHECK:<name> marker so that |
| 1034 | * tools/numeric_check_sweep.sh can delete it and prove a test notices. */ |
| 1035 | |
| 1036 | static int bad(char *err, size_t err_len, const char *fmt, ...) __attribute__((format(printf, 3, 4))); |
| 1037 | static int bad(char *err, size_t err_len, const char *fmt, ...) { |
| 1038 | if (err && err_len) { |
| 1039 | va_list ap; |
| 1040 | va_start(ap, fmt); |
| 1041 | vsnprintf(err, err_len, fmt, ap); |
| 1042 | va_end(ap); |
| 1043 | } |
| 1044 | return OMEGA_NUMERIC_ERR_OPERANDS; |
| 1045 | } |
| 1046 | |
| 1047 | #define INSN_OP(p) ((p).w[0] & 0xffffu) |
| 1048 | #define INSN_DST(p) (((p).w[0] >> 16) & 0xffu) |
| 1049 | #define INSN_SRCA(p) (((p).w[0] >> 24) & 0xffu) |
| 1050 | #define INSN_SRCB(p) ((p).w[1] & 0xffu) |
| 1051 | #define CTRL_WBAR(w3) (((w3) >> 14) & 7u) |
| 1052 | #define CTRL_WAIT(w3) (((w3) >> 20) & 0x3fu) |
| 1053 | #define CTRL_STALL(w3) (((w3) >> 9) & 0xfu) |
| 1054 | |
| 1055 | #define OPC_STS 0x7988u |
| 1056 | #define OPC_LDS 0x7984u |
| 1057 | #define OPC_BAR 0x7b1du |
| 1058 | #define OPC_SHF 0x7819u |
| 1059 | #define OPC_LOP3 0x7812u |
| 1060 | #define OPC_SHFL 0x7f89u |
| 1061 | #define OPC_FADD 0x7221u |
| 1062 | #define OPC_STG 0x7986u |
| 1063 | #define W_STG_WORD0 0x06007986u /* STG.E desc[UR4][R6.64], R9: address R6:R7, predicate PT */ |
| 1064 | #define W_STG_WORD1 0x00000009u /* value R9, offset 0 */ |
| 1065 | #define W_STG_WORD2 0x0c101904u /* 32-bit width, descriptor UR4 */ |
| 1066 | |
| 1067 | #define SHFL_DELTA(p) (((p).w[1] >> 21) & 0x1fu) |
| 1068 | #define SHFL_CLAMP(p) (((p).w[1] >> 8) & 0x1fu) |
| 1069 | #define SHFL_FIELDS ((0x1fu << 21) | (0x1fu << 8)) |
| 1070 | |
| 1071 | #define OPC_FSETP 0x720bu |
| 1072 | #define OPC_LDC 0x7b82u |
| 1073 | #define OPC_IMADW 0x7825u |
| 1074 | #define OPC_LDG 0x7981u |
| 1075 | #define OPC_FFMA 0x7223u |
| 1076 | |
| 1077 | /* Opcodes (predicate nibble ignored) whose result is not scoreboarded: an ALU result read after a |
| 1078 | * fixed delay. Stores, barriers, EXIT and the variable-latency units (LDG, LDS, LDC, SHFL, MUFU, |
| 1079 | * F2I, F2F) are not producers in this sense; the scoreboard rules cover the latter. */ |
| 1080 | static inline bool fixed_latency_producer(uint32_t o) { |
| 1081 | switch (o & 0x0fffu) { |
| 1082 | case 0x986u: case 0x988u: case 0xb1du: case 0x94du: /* STG STS BAR EXIT */ |
| 1083 | case 0x981u: case 0x984u: case 0xb82u: case 0xf89u: /* LDG LDS LDC SHFL */ |
| 1084 | case 0x308u: case 0x305u: case 0x304u: /* MUFU F2I F2F */ |
| 1085 | return false; |
| 1086 | default: |
| 1087 | return true; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /* FSETP compare code per E1 select op (nvdisasm 13.0.88 -b SM121). */ |
| 1092 | static int e1_fsetp_code(OmegaNumericOp op) { |
| 1093 | switch (op) { |
| 1094 | case OMEGA_NOP_FSETP_LT_SEL: return 0x1; |
| 1095 | case OMEGA_NOP_FSETP_EQ_SEL: return 0x2; |
| 1096 | case OMEGA_NOP_FSETP_LE_SEL: return 0x3; |
| 1097 | case OMEGA_NOP_FSETP_GT_SEL: return 0x4; |
| 1098 | case OMEGA_NOP_FSETP_NE_SEL: return 0x5; |
| 1099 | case OMEGA_NOP_FSETP_NUM_SEL: return 0x7; |
| 1100 | case OMEGA_NOP_FSETP_NAN_SEL: return 0x8; |
| 1101 | case OMEGA_NOP_FSETP_LTU_SEL: return 0x9; |
| 1102 | case OMEGA_NOP_FSETP_EQU_SEL: return 0xa; |
| 1103 | case OMEGA_NOP_FSETP_LEU_SEL: return 0xb; |
| 1104 | case OMEGA_NOP_FSETP_GTU_SEL: return 0xc; |
| 1105 | case OMEGA_NOP_FSETP_NEU_SEL: return 0xd; |
| 1106 | case OMEGA_NOP_FSETP_GEU_SEL: return 0xe; |
| 1107 | default: return -1; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* The one conversion instruction each E1 conversion op must use, R9 <- R2. */ |
| 1112 | typedef struct { OmegaNumericOp op; uint32_t w0, w1, w2; bool variable; const char *text; } E1Cvt; |
| 1113 | static const E1Cvt E1_CVT[] = { |
| 1114 | { OMEGA_NOP_F2I_FLOOR, 0x00097305u, 0x00000002u, 0x00207100u, true, "F2I.FLOOR.NTZ R9, R2" }, |
| 1115 | { OMEGA_NOP_F2I_CEIL, 0x00097305u, 0x00000002u, 0x0020b100u, true, "F2I.CEIL.NTZ R9, R2" }, |
| 1116 | { OMEGA_NOP_F2I_RNI, 0x00097305u, 0x00000002u, 0x00203100u, true, "F2I.NTZ R9, R2 (round to nearest even)" }, |
| 1117 | { OMEGA_NOP_F2U, 0x00097305u, 0x00000002u, 0x0020f000u, true, "F2I.U32.TRUNC.NTZ R9, R2" }, |
| 1118 | { OMEGA_NOP_I2FP_U32, 0x00097245u, 0x00000002u, 0x00201000u, false, "I2FP.F32.U32 R9, R2" }, |
| 1119 | { OMEGA_NOP_F32_TO_F16, 0x00097304u, 0x00000002u, 0x00200800u, true, "F2F.F16.F32 R9, R2" }, |
| 1120 | { OMEGA_NOP_F32_TO_BF16, 0x00097304u, 0x00000002u, 0x00202000u, true, "F2F.BF16.F32 R9, R2" }, |
| 1121 | { OMEGA_NOP_F16_TO_F32, 0xff097230u, 0x20000002u, 0x00004100u, false, "HADD2.F32 R9, -RZ, R2.H0_H0" }, |
| 1122 | { OMEGA_NOP_BF16_TO_F32, 0x02097819u, 0x00000010u, 0x000006ffu, false, "SHF.L.U32 R9, R2, 0x10, RZ" }, |
| 1123 | }; |
| 1124 | static const E1Cvt *e1_cvt(OmegaNumericOp op) { |
| 1125 | for (size_t i = 0; i < sizeof(E1_CVT) / sizeof(E1_CVT[0]); i++) |
| 1126 | if (E1_CVT[i].op == op) return &E1_CVT[i]; |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
| 1130 | int omega_numeric_check_patch(OmegaNumericOp op, const OmegaNumericPatchInsn *p, int n, |
| 1131 | const uint32_t *qmd1, char *err, size_t err_len) { |
| 1132 | if (err && err_len) err[0] = '\0'; |
| 1133 | if (!p || !qmd1 || (unsigned)op >= OMEGA_NOP_COUNT) return bad(err, err_len, "check_patch: missing patch or QMD"); |
| 1134 | if (n < 1 || n > (int)OMEGA_NUMERIC_PATCH_MAX) return bad(err, err_len, "patch length %d outside 1..%u", n, OMEGA_NUMERIC_PATCH_MAX); /* CHECK:patch_len */ |
| 1135 | const char *name = OP_TABLE[op].name; |
| 1136 | if (!(CTRL_WAIT(p[0].w[3]) & (1u << 4))) return bad(err, err_len, "%s: first instruction does not wait on SB4 (the a/b loads)", name); /* CHECK:first_waits_loads */ |
| 1137 | int sts = -1, lds = -1, bar = -1, shf = -1, lop = -1, n_sts = 0, n_lds = 0, n_bar = 0, n_shfl = 0; |
| 1138 | for (int t = 0; t < n; t++) { |
| 1139 | uint32_t o = INSN_OP(p[t]); |
| 1140 | /* a result that arrives through a scoreboard is waited on by the next instruction */ |
| 1141 | if (CTRL_WBAR(p[t].w[3]) <= 5u && (t + 1 >= n || !(CTRL_WAIT(p[t + 1].w[3]) & (1u << CTRL_WBAR(p[t].w[3]))))) return bad(err, err_len, "%s: instruction %d sets SB%u but the next instruction does not wait on it", name, t, CTRL_WBAR(p[t].w[3])); /* CHECK:scoreboard_wait */ |
| 1142 | /* ... and the setter stalls at least 2 cycles first: on GB10 a waiter issued 1 cycle after the |
| 1143 | * setter does not see the barrier (chip, 2026-10-01: LDG stall 1 then FFMA read the stale R11; |
| 1144 | * SHFL stall 2 then FADD is chip-proven in REDUCE_SUM) */ |
| 1145 | if (CTRL_WBAR(p[t].w[3]) <= 5u && CTRL_STALL(p[t].w[3]) < 2u) return bad(err, err_len, "%s: instruction %d sets SB%u with stall %u; the waiter needs a stall of at least 2", name, t, CTRL_WBAR(p[t].w[3]), CTRL_STALL(p[t].w[3])); /* CHECK:scoreboard_set_stall */ |
| 1146 | /* a fixed-latency result has no scoreboard: its own stall is the only thing keeping the next |
| 1147 | * instruction (or the baseline STG) from reading it early. Every chip-proven fixed-latency |
| 1148 | * patch instruction stalls at least 5 (ptxas 13.0.88 sm_121 uses 5 before a dependent STG). */ |
| 1149 | if (CTRL_WBAR(p[t].w[3]) > 5u && fixed_latency_producer(o) && CTRL_STALL(p[t].w[3]) < 5u) return bad(err, err_len, "%s: fixed-latency instruction %d stalls %u cycles; its consumer needs at least 5", name, t, CTRL_STALL(p[t].w[3])); /* CHECK:fixed_latency_stall */ |
| 1150 | if (o == OPC_STS) { |
| 1151 | if ((p[t].w[0] & 0x00ffffffu) != 0x00007988u || (p[t].w[1] & ~0xffu) != 0 || p[t].w[2] != 0x080008ffu) return bad(err, err_len, "%s: STS at %d is not the encoded 32-bit STS [Rx+URZ] form", name, t); /* CHECK:sts_form */ |
| 1152 | sts = t; n_sts++; |
| 1153 | } else if (o == OPC_LDS) { |
| 1154 | if ((p[t].w[0] & 0x0000ffffu) != 0x00007984u || p[t].w[1] != 0x000000ffu || p[t].w[2] != 0x08000800u) return bad(err, err_len, "%s: LDS at %d is not the encoded 32-bit LDS Rd, [Rx+URZ] form", name, t); /* CHECK:lds_form */ |
| 1155 | lds = t; n_lds++; |
| 1156 | } else if (o == OPC_BAR) { |
| 1157 | if (p[t].w[0] != 0x00007b1du || p[t].w[1] != 0 || p[t].w[2] != 0x00010000u) return bad(err, err_len, "%s: barrier at %d is not BAR.SYNC 0", name, t); /* CHECK:bar_form */ |
| 1158 | bar = t; n_bar++; |
| 1159 | } else if ((p[t].w[0] & 0x0fffu) == (OPC_STG & 0x0fffu)) { |
| 1160 | if (p[t].w[0] != W_STG_WORD0 || p[t].w[1] != W_STG_WORD1 || p[t].w[2] != W_STG_WORD2) return bad(err, err_len, "%s: store at %d is not the unpredicated STG.E desc[UR4][R6.64], R9 (32-bit, no offset)", name, t); /* CHECK:stg_form */ |
| 1161 | } else if (o == OPC_SHF) { |
| 1162 | shf = t; |
| 1163 | } else if (o == OPC_LOP3) { |
| 1164 | lop = t; |
| 1165 | } else if (o == OPC_SHFL) { |
| 1166 | if ((p[t].w[0] & 0x0000ffffu) != 0x00007f89u || (p[t].w[1] & ~SHFL_FIELDS) != 0x08000000u || p[t].w[2] != 0x000e0000u) return bad(err, err_len, "%s: shuffle at %d is not the encoded SHFL.DOWN form", name, t); /* CHECK:shfl_form */ |
| 1167 | if (SHFL_CLAMP(p[t]) != 0x1fu) return bad(err, err_len, "%s: shuffle at %d has clamp 0x%x, not 0x1f", name, t, SHFL_CLAMP(p[t])); /* CHECK:shfl_clamp */ |
| 1168 | n_shfl++; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | bool uses_shared = n_sts || n_lds || n_bar; |
| 1173 | if (op == OMEGA_NOP_LDS_STS && !(n_sts && n_lds)) return bad(err, err_len, "%s: patch has no STS/LDS pair", name); /* CHECK:lds_sts_present */ |
| 1174 | if (uses_shared) { |
| 1175 | uint32_t tx = qmd1[34] & 0xffffu; |
| 1176 | uint32_t barriers = (qmd1[35] >> 17) & 0x1fu; |
| 1177 | uint32_t shared_bytes = (qmd1[36] & 0x7ffu) << 7; |
| 1178 | uint32_t need = (tx - 1u) * 4u + 4u; |
| 1179 | if (n_sts != 1 || n_lds != 1 || n_bar != 1) return bad(err, err_len, "%s: expected one STS, one BAR.SYNC, one LDS (got %d, %d, %d)", name, n_sts, n_bar, n_lds); /* CHECK:shared_counts */ |
| 1180 | if (!(sts < bar && bar < lds)) return bad(err, err_len, "%s: order must be STS, BAR.SYNC, LDS (at %d, %d, %d)", name, sts, bar, lds); /* CHECK:shared_order */ |
| 1181 | if (barriers < 1u) return bad(err, err_len, "%s: QMD declares %u barriers; BAR.SYNC 0 needs at least 1", name, barriers); /* CHECK:qmd_barrier */ |
| 1182 | if (tx != OMEGA_NUMERIC_CTA_THREADS) return bad(err, err_len, "%s: QMD CTA width %u is not the %u-thread CTA", name, tx, OMEGA_NUMERIC_CTA_THREADS); /* CHECK:qmd_cta */ |
| 1183 | if (need > shared_bytes) return bad(err, err_len, "%s: needs %u bytes of shared memory, QMD declares %u", name, need, shared_bytes); /* CHECK:qmd_shared_size */ |
| 1184 | if (shf < 0 || shf > sts || INSN_SRCA(p[shf]) != 0u || p[shf].w[1] != 2u || p[shf].w[2] != 0x000006ffu) return bad(err, err_len, "%s: STS address is not SHF.L.U32 of R0 (tid.x) by 2", name); /* CHECK:addr_shift */ |
| 1185 | if (INSN_SRCA(p[sts]) != INSN_DST(p[shf])) return bad(err, err_len, "%s: STS address register R%u is not the shifted tid R%u", name, INSN_SRCA(p[sts]), INSN_DST(p[shf])); /* CHECK:sts_addr_reg */ |
| 1186 | if (INSN_SRCB(p[sts]) != 2u) return bad(err, err_len, "%s: STS stores R%u, not the input value R2 (a[i])", name, INSN_SRCB(p[sts])); /* CHECK:sts_value_reg */ |
| 1187 | if (lop < 0 || lop > lds || INSN_SRCA(p[lop]) != INSN_DST(p[shf]) || p[lop].w[2] != 0x078e3cffu) return bad(err, err_len, "%s: LDS address is not LOP3 XOR of the shifted tid", name); /* CHECK:lop3_form */ |
| 1188 | if (p[lop].w[1] != (tx - 1u) * 4u) return bad(err, err_len, "%s: LDS address mask 0x%x is not (CTA-1)*4 = 0x%x (out of bounds or wrong partner)", name, p[lop].w[1], (tx - 1u) * 4u); /* CHECK:lop3_mask */ |
| 1189 | if (INSN_SRCA(p[lds]) != INSN_DST(p[lop]) || INSN_DST(p[lds]) != 9u) return bad(err, err_len, "%s: LDS must load [R%u] into R9", name, INSN_DST(p[lop])); /* CHECK:lds_regs */ |
| 1190 | } |
| 1191 | |
| 1192 | if (op == OMEGA_NOP_SHFL_DOWN) { |
| 1193 | int t = 0; |
| 1194 | while (t < n && INSN_OP(p[t]) != OPC_SHFL) t++; |
| 1195 | if (n_shfl != 1 || t >= n || SHFL_DELTA(p[t]) != 1u) return bad(err, err_len, "%s: expected one SHFL.DOWN by 1", name); /* CHECK:shfl_down_1 */ |
| 1196 | } |
| 1197 | if (op == OMEGA_NOP_REDUCE_SUM) { |
| 1198 | static const uint32_t DELTA[5] = { 16u, 8u, 4u, 2u, 1u }; |
| 1199 | if (n_shfl != 5 || n != 12) return bad(err, err_len, "%s: expected five SHFL.DOWN + FADD pairs, STG, EXIT (got %d shuffles, %d instructions)", name, n_shfl, n); /* CHECK:reduce_shape */ |
| 1200 | uint32_t acc = INSN_SRCA(p[0]); |
| 1201 | for (int s = 0; s < 5; s++) { |
| 1202 | const OmegaNumericPatchInsn *sh = &p[2 * s], *fa = &p[2 * s + 1]; |
| 1203 | if (INSN_OP(*sh) != OPC_SHFL || SHFL_DELTA(*sh) != DELTA[s]) return bad(err, err_len, "%s: step %d must be SHFL.DOWN by %u (declared order 16,8,4,2,1)", name, s, DELTA[s]); /* CHECK:reduce_delta_order */ |
| 1204 | if (INSN_SRCA(*sh) != acc) return bad(err, err_len, "%s: step %d shuffles R%u, not the running sum R%u", name, s, INSN_SRCA(*sh), acc); /* CHECK:reduce_shfl_src */ |
| 1205 | if (INSN_OP(*fa) != OPC_FADD || (fa->w[1] & ~0xffu) != 0 || fa->w[2] != 0) return bad(err, err_len, "%s: step %d is not followed by a plain FADD (no negate, no modifiers)", name, s); /* CHECK:reduce_fadd_form */ |
| 1206 | if (!(CTRL_WAIT(fa->w[3]) & (1u << CTRL_WBAR(sh->w[3]))) || CTRL_WBAR(sh->w[3]) > 5u) return bad(err, err_len, "%s: FADD of step %d does not wait on the shuffle", name, s); /* CHECK:reduce_fadd_wait */ |
| 1207 | if (INSN_SRCA(*fa) != acc || INSN_SRCB(*fa) != INSN_DST(*sh)) return bad(err, err_len, "%s: FADD of step %d does not add the shuffled value to the running sum", name, s); /* CHECK:reduce_fadd_srcs */ |
| 1208 | if (INSN_DST(*fa) != (s == 4 ? 9u : acc)) return bad(err, err_len, "%s: FADD of step %d writes R%u", name, s, INSN_DST(*fa)); /* CHECK:reduce_fadd_dst */ |
| 1209 | } |
| 1210 | if (p[10].w[0] != W_STG_WORD0 || p[10].w[1] != W_STG_WORD1 || p[10].w[2] != W_STG_WORD2) return bad(err, err_len, "%s: the sum in R9 is not stored by STG.E desc[UR4][R6.64] (unpredicated, 32-bit)", name); /* CHECK:reduce_store */ |
| 1211 | } |
| 1212 | /* ---- E1 scalar ops (E1 WP-C) ---- */ |
| 1213 | int e1_cmp = e1_fsetp_code(op); |
| 1214 | if (e1_cmp >= 0) { |
| 1215 | if (n != 4 || INSN_OP(p[0]) != OPC_FSETP || p[0].w[0] != 0x0200720bu || p[0].w[1] != 5u || (p[0].w[2] & ~0xf000u) != 0x03f00000u) return bad(err, err_len, "%s: patch is not FSETP.<cmp>.AND P0, PT, R2, R5, PT ; FSEL ; STG ; EXIT", name); /* CHECK:e1_fsetp_form */ |
| 1216 | if ((int)((p[0].w[2] >> 12) & 0xfu) != e1_cmp) return bad(err, err_len, "%s: FSETP compare code %u is not the op's code %d", name, (p[0].w[2] >> 12) & 0xfu, e1_cmp); /* CHECK:e1_fsetp_cmp */ |
| 1217 | if (p[1].w[0] != 0x02097208u || p[1].w[1] != 5u || p[1].w[2] != 0u) return bad(err, err_len, "%s: select is not FSEL R9, R2, R5, P0", name); /* CHECK:e1_fsel_form */ |
| 1218 | } |
| 1219 | const E1Cvt *cv = e1_cvt(op); |
| 1220 | if (cv) { |
| 1221 | if (p[0].w[0] != cv->w0 || p[0].w[1] != cv->w1 || p[0].w[2] != cv->w2) return bad(err, err_len, "%s: conversion at 0 is not %s (wrong unit, rounding, type or registers)", name, cv->text); /* CHECK:e1_cvt_mode */ |
| 1222 | if (cv->variable != (CTRL_WBAR(p[0].w[3]) <= 5u) || n != (cv->variable ? 3 : 1)) return bad(err, err_len, "%s: %s must be %s (patch of %d)", name, cv->text, cv->variable ? "a scoreboarded result followed by STG, EXIT" : "fixed latency, the baseline STG stores it", n); /* CHECK:e1_cvt_latency */ |
| 1223 | } |
| 1224 | if (op == OMEGA_NOP_FFMA_V) { |
| 1225 | if (n != 6 || INSN_OP(p[0]) != OPC_LDC || INSN_OP(p[1]) != OPC_IMADW || INSN_OP(p[2]) != OPC_LDG || INSN_OP(p[3]) != OPC_FFMA) return bad(err, err_len, "%s: expected LDC.64, IMAD.WIDE.U32, LDG.E, FFMA, STG, EXIT", name); /* CHECK:ffmav_shape */ |
| 1226 | if (p[0].w[0] != 0xff0a7b82u || p[0].w[1] != 0x0000e800u || p[0].w[2] != 0x00000a00u || CTRL_WBAR(p[0].w[3]) > 5u) return bad(err, err_len, "%s: c pointer is not LDC.64 R10 (free pair R10:R11) of c[0x0][0x3a0] (kernel argument words 8..9) with a write barrier", name); /* CHECK:ffmav_cptr */ |
| 1227 | if (INSN_SRCA(p[1]) != 9u || p[1].w[1] != 4u || p[1].w[2] != 0x078e000au || INSN_DST(p[1]) != INSN_DST(p[0])) return bad(err, err_len, "%s: c address is not IMAD.WIDE.U32 of the index R9 by 4 onto the c pointer", name); /* CHECK:ffmav_addr */ |
| 1228 | if (INSN_SRCA(p[2]) != INSN_DST(p[1]) || INSN_DST(p[2]) != 11u || p[2].w[1] != 4u || p[2].w[2] != 0x0c1e1900u || CTRL_WBAR(p[2].w[3]) > 5u) return bad(err, err_len, "%s: c[i] is not LDG.E (32-bit, desc[UR4]) from the computed address into the free R11, with a write barrier", name); /* CHECK:ffmav_load */ |
| 1229 | if (INSN_DST(p[3]) != 9u || INSN_SRCA(p[3]) != 2u || INSN_SRCB(p[3]) != 5u || (p[3].w[2] & 0xffu) != INSN_DST(p[2]) || (p[3].w[1] & ~0xffu) != 0 || (p[3].w[2] & ~0xffu) != 0) return bad(err, err_len, "%s: FFMA is not R9 = R2 * R5 + c[i] (no negate, no modifiers)", name); /* CHECK:ffmav_fma_regs */ |
| 1230 | } |
| 1231 | /* A multi-instruction patch stores R9 itself and ends; a single one uses the baseline STG. */ |
| 1232 | if (n > 1 && (p[n - 2].w[0] != W_STG_WORD0 || p[n - 2].w[1] != W_STG_WORD1 || p[n - 2].w[2] != W_STG_WORD2 || p[n - 1].w[0] != 0x0000794du || p[n - 1].w[1] != 0u || p[n - 1].w[2] != 0x03800000u)) return bad(err, err_len, "%s: patch does not end with STG.E desc[UR4][R6.64], R9 ; EXIT", name); /* CHECK:tail_store_exit */ |
| 1233 | return OMEGA_NUMERIC_OK; |
| 1234 | } |
| 1235 | |
| 1236 | /* ---- Reference and CPU realization tiers ---------------------------------- */ |
| 1237 | |
| 1238 | static float ref_fsetp_sel(float a, float b) { return omega_ref_fsetp_ge(a, b) ? a : b; } |
| 1239 | static float ref_fsel(float a, float b) { return omega_ref_fsetp_ge(a, 0.0f) ? b : a; } |
| 1240 | |
| 1241 | static float ref_shfl_down(const float *a, size_t i) { |
| 1242 | return ((i % 32u) == 31u) ? a[i] : a[i + 1]; |
| 1243 | } |
| 1244 | |
| 1245 | bool omega_numeric_element_checked(OmegaNumericOp op, size_t index) { |
| 1246 | if (op == OMEGA_NOP_REDUCE_SUM) return (index % 32u) == 0; |
| 1247 | return true; |
| 1248 | } |
| 1249 | |
| 1250 | static bool needs_b(OmegaNumericOp op) { |
| 1251 | return OP_TABLE[op].arity >= 2; |
| 1252 | } |
| 1253 | |
| 1254 | int omega_numeric_reference(OmegaNumericOp op, const float *a, const float *b, |
| 1255 | const float *c, float *out, size_t count) { |
| 1256 | if ((unsigned)op >= OMEGA_NOP_COUNT || !a || !out) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1257 | if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; |
| 1258 | if (needs_b(op) && !b) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1259 | if (OP_TABLE[op].arity >= 3 && !c) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1260 | if ((op == OMEGA_NOP_SHFL_DOWN || op == OMEGA_NOP_REDUCE_SUM) && (count % 32u) != 0) |
| 1261 | return OMEGA_NUMERIC_ERR_OPERANDS; |
| 1262 | if (op == OMEGA_NOP_LDS_STS && (count % OMEGA_NUMERIC_CTA_THREADS) != 0) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 1263 | for (size_t i = 0; i < count; i++) { |
| 1264 | float r = 0.0f; |
| 1265 | switch (op) { |
| 1266 | case OMEGA_NOP_FADD: r = omega_ref_fadd(a[i], b[i]); break; |
| 1267 | case OMEGA_NOP_FSUB: r = omega_ref_fsub(a[i], b[i]); break; |
| 1268 | case OMEGA_NOP_FMUL: r = omega_ref_fmul(a[i], b[i]); break; |
| 1269 | case OMEGA_NOP_FFMA: r = omega_ref_ffma(a[i], b[i], c[i]); break; |
| 1270 | case OMEGA_NOP_FSETP_SEL: r = ref_fsetp_sel(a[i], b[i]); break; |
| 1271 | case OMEGA_NOP_FSEL: r = ref_fsel(a[i], b[i]); break; |
| 1272 | case OMEGA_NOP_FMNMX_MIN: r = omega_ref_fmin(a[i], b[i]); break; |
| 1273 | case OMEGA_NOP_FMNMX_MAX: r = omega_ref_fmax(a[i], b[i]); break; |
| 1274 | case OMEGA_NOP_I2FP: r = omega_ref_i2f((int32_t)omega_float_to_bits(a[i])); break; |
| 1275 | case OMEGA_NOP_F2I: r = omega_bits_to_float((uint32_t)omega_ref_f2i(a[i])); break; |
| 1276 | case OMEGA_NOP_MUFU_RCP: r = omega_ieee_div(1.0f, a[i]); break; |
| 1277 | case OMEGA_NOP_MUFU_RSQ: r = omega_ieee_div(1.0f, omega_ieee_sqrt(a[i])); break; |
| 1278 | case OMEGA_NOP_LDS_STS: r = a[i ^ (OMEGA_NUMERIC_CTA_THREADS - 1u)]; break; |
| 1279 | case OMEGA_NOP_SHFL_DOWN: r = ref_shfl_down(a, i); break; |
| 1280 | case OMEGA_NOP_DIV: r = omega_ieee_div(a[i], b[i]); break; |
| 1281 | case OMEGA_NOP_SQRT: r = omega_ieee_sqrt(a[i]); break; |
| 1282 | case OMEGA_NOP_EXP: r = omega_math_exp(a[i]); break; |
| 1283 | case OMEGA_NOP_LOG: r = omega_math_log(a[i]); break; |
| 1284 | case OMEGA_NOP_REDUCE_SUM: |
| 1285 | r = ((i % 32u) == 0) ? omega_warp_reduce_sum(&a[i]) : 0.0f; |
| 1286 | break; |
| 1287 | case OMEGA_NOP_FSETP_LT_SEL: |
| 1288 | case OMEGA_NOP_FSETP_LE_SEL: |
| 1289 | case OMEGA_NOP_FSETP_GT_SEL: |
| 1290 | case OMEGA_NOP_FSETP_EQ_SEL: |
| 1291 | case OMEGA_NOP_FSETP_NE_SEL: |
| 1292 | case OMEGA_NOP_FSETP_NUM_SEL: |
| 1293 | case OMEGA_NOP_FSETP_NAN_SEL: |
| 1294 | case OMEGA_NOP_FSETP_LTU_SEL: |
| 1295 | case OMEGA_NOP_FSETP_LEU_SEL: |
| 1296 | case OMEGA_NOP_FSETP_GTU_SEL: |
| 1297 | case OMEGA_NOP_FSETP_GEU_SEL: |
| 1298 | case OMEGA_NOP_FSETP_EQU_SEL: |
| 1299 | case OMEGA_NOP_FSETP_NEU_SEL: |
| 1300 | r = omega_ref_fsetp_pred((int)op, a[i], b[i]) == 1 ? a[i] : b[i]; /* bits moved, no FP op */ |
| 1301 | break; |
| 1302 | case OMEGA_NOP_F2I_FLOOR: r = omega_bits_to_float((uint32_t)omega_ref_f2i_floor(a[i])); break; |
| 1303 | case OMEGA_NOP_F2I_CEIL: r = omega_bits_to_float((uint32_t)omega_ref_f2i_ceil(a[i])); break; |
| 1304 | case OMEGA_NOP_F2I_RNI: r = omega_bits_to_float((uint32_t)omega_ref_f2i_rni(a[i])); break; |
| 1305 | case OMEGA_NOP_F2U: r = omega_bits_to_float(omega_ref_f2u(a[i])); break; |
| 1306 | case OMEGA_NOP_I2FP_U32: r = omega_ref_u2f(omega_float_to_bits(a[i])); break; |
| 1307 | case OMEGA_NOP_F32_TO_F16: r = omega_bits_to_float(omega_ref_f32_to_f16(a[i])); break; |
| 1308 | case OMEGA_NOP_F32_TO_BF16: r = omega_bits_to_float(omega_ref_f32_to_bf16(a[i])); break; |
| 1309 | case OMEGA_NOP_F16_TO_F32: |
| 1310 | r = omega_ref_f16_to_f32((uint16_t)(omega_float_to_bits(a[i]) & 0xffffU)); |
| 1311 | break; |
| 1312 | case OMEGA_NOP_BF16_TO_F32: |
| 1313 | r = omega_ref_bf16_to_f32((uint16_t)(omega_float_to_bits(a[i]) & 0xffffU)); |
| 1314 | break; |
| 1315 | case OMEGA_NOP_FFMA_V: r = omega_ref_ffma_int(a[i], b[i], c[i]); break; |
| 1316 | case OMEGA_NOP_COUNT: return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1317 | } |
| 1318 | out[i] = r; |
| 1319 | } |
| 1320 | return OMEGA_NUMERIC_OK; |
| 1321 | } |
| 1322 | |
| 1323 | /* Independent spelling of the declared order: explicit per-level arrays. */ |
| 1324 | static float cpu_tree_sum32(const float *a) { |
| 1325 | float l16[16], l8[8], l4[4], l2[2]; |
| 1326 | for (int i = 0; i < 16; i++) l16[i] = a[i] + a[i + 16]; |
| 1327 | for (int i = 0; i < 8; i++) l8[i] = l16[i] + l16[i + 8]; |
| 1328 | for (int i = 0; i < 4; i++) l4[i] = l8[i] + l8[i + 4]; |
| 1329 | for (int i = 0; i < 2; i++) l2[i] = l4[i] + l4[i + 2]; |
| 1330 | return l2[0] + l2[1]; |
| 1331 | } |
| 1332 | |
| 1333 | int omega_numeric_cpu_realize(OmegaNumericOp op, const float *a, const float *b, |
| 1334 | const float *c, float *out, size_t count) { |
| 1335 | if ((unsigned)op >= OMEGA_NOP_COUNT || !a || !out) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1336 | if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; |
| 1337 | if (needs_b(op) && !b) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1338 | if (OP_TABLE[op].arity >= 3 && !c) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1339 | if ((op == OMEGA_NOP_SHFL_DOWN || op == OMEGA_NOP_REDUCE_SUM) && (count % 32u) != 0) |
| 1340 | return OMEGA_NUMERIC_ERR_OPERANDS; |
| 1341 | if (op == OMEGA_NOP_LDS_STS && (count % OMEGA_NUMERIC_CTA_THREADS) != 0) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 1342 | const bool bf16 = op == OMEGA_NOP_F32_TO_BF16 && omega_numeric_cpu_has_bf16(); |
| 1343 | for (size_t i = 0; i < count; i++) { |
| 1344 | float r = 0.0f; |
| 1345 | float x = a[i]; |
| 1346 | float y = b ? b[i] : 0.0f; |
| 1347 | switch (op) { |
| 1348 | case OMEGA_NOP_FADD: __asm__ volatile("fadd %s0, %s1, %s2" : "=w"(r) : "w"(x), "w"(y)); break; |
| 1349 | case OMEGA_NOP_FSUB: __asm__ volatile("fsub %s0, %s1, %s2" : "=w"(r) : "w"(x), "w"(y)); break; |
| 1350 | case OMEGA_NOP_FMUL: __asm__ volatile("fmul %s0, %s1, %s2" : "=w"(r) : "w"(x), "w"(y)); break; |
| 1351 | case OMEGA_NOP_FFMA: |
| 1352 | __asm__ volatile("fmadd %s0, %s1, %s2, %s3" : "=w"(r) : "w"(x), "w"(y), "w"(c[i])); |
| 1353 | break; |
| 1354 | case OMEGA_NOP_FSETP_SEL: |
| 1355 | /* fcmp: unordered sets NZCV=0011, so GE (N==V) is false for NaN */ |
| 1356 | __asm__ volatile("fcmp %s1, %s2\n\tfcsel %s0, %s1, %s2, ge" |
| 1357 | : "=&w"(r) : "w"(x), "w"(y) : "cc"); |
| 1358 | break; |
| 1359 | case OMEGA_NOP_FSEL: |
| 1360 | __asm__ volatile("fcmp %s1, #0.0\n\tfcsel %s0, %s2, %s1, ge" |
| 1361 | : "=&w"(r) : "w"(x), "w"(y) : "cc"); |
| 1362 | break; |
| 1363 | case OMEGA_NOP_FMNMX_MIN: |
| 1364 | case OMEGA_NOP_FMNMX_MAX: { |
| 1365 | /* FMINNM/FMAXNM follow IEEE 754-2008 minNum, which returns NaN for a |
| 1366 | * signaling NaN. Omega's minimum treats every NaN as missing data |
| 1367 | * (754-2019 minimumNumber), so quiet signaling NaNs first. */ |
| 1368 | float qx = omega_isnan(x) ? omega_bits_to_float(omega_float_to_bits(x) | 0x00400000U) : x; |
| 1369 | float qy = omega_isnan(y) ? omega_bits_to_float(omega_float_to_bits(y) | 0x00400000U) : y; |
| 1370 | if (op == OMEGA_NOP_FMNMX_MIN) __asm__ volatile("fminnm %s0, %s1, %s2" : "=w"(r) : "w"(qx), "w"(qy)); |
| 1371 | else __asm__ volatile("fmaxnm %s0, %s1, %s2" : "=w"(r) : "w"(qx), "w"(qy)); |
| 1372 | break; |
| 1373 | } |
| 1374 | case OMEGA_NOP_I2FP: { |
| 1375 | int32_t iv = (int32_t)omega_float_to_bits(x); |
| 1376 | __asm__ volatile("scvtf %s0, %w1" : "=w"(r) : "r"(iv)); |
| 1377 | break; |
| 1378 | } |
| 1379 | case OMEGA_NOP_F2I: { |
| 1380 | int32_t iv; |
| 1381 | __asm__ volatile("fcvtzs %w0, %s1" : "=r"(iv) : "w"(x)); |
| 1382 | r = omega_bits_to_float((uint32_t)iv); |
| 1383 | break; |
| 1384 | } |
| 1385 | case OMEGA_NOP_MUFU_RCP: __asm__ volatile("fdiv %s0, %s1, %s2" : "=w"(r) : "w"(1.0f), "w"(x)); break; |
| 1386 | case OMEGA_NOP_MUFU_RSQ: { |
| 1387 | float s; |
| 1388 | __asm__ volatile("fsqrt %s0, %s1" : "=w"(s) : "w"(x)); |
| 1389 | __asm__ volatile("fdiv %s0, %s1, %s2" : "=w"(r) : "w"(1.0f), "w"(s)); |
| 1390 | break; |
| 1391 | } |
| 1392 | case OMEGA_NOP_LDS_STS: { |
| 1393 | /* independent spelling: mirror position inside the CTA */ |
| 1394 | size_t base = i - (i % OMEGA_NUMERIC_CTA_THREADS); |
| 1395 | r = a[base + (OMEGA_NUMERIC_CTA_THREADS - 1u) - (i - base)]; |
| 1396 | break; |
| 1397 | } |
| 1398 | case OMEGA_NOP_SHFL_DOWN: { |
| 1399 | size_t lane = i & 31u, base = i - lane; |
| 1400 | size_t src = lane + 1u > 31u ? lane : lane + 1u; |
| 1401 | r = a[base + src]; |
| 1402 | break; |
| 1403 | } |
| 1404 | case OMEGA_NOP_DIV: r = omega_math_div(x, y); break; |
| 1405 | case OMEGA_NOP_SQRT: r = omega_math_sqrt(x); break; |
| 1406 | case OMEGA_NOP_EXP: r = omega_math_exp(x); break; |
| 1407 | case OMEGA_NOP_LOG: r = omega_math_log(x); break; |
| 1408 | case OMEGA_NOP_REDUCE_SUM: r = ((i & 31u) == 0) ? cpu_tree_sum32(&a[i]) : 0.0f; break; |
| 1409 | /* |
| 1410 | * E1 compare-and-select: FCMP then FCSEL on the AArch64 condition that |
| 1411 | * is the predicate. FCMP flags: less 1000, equal 0110, greater 0010, |
| 1412 | * unordered 0011 (NZCV). NE and EQU have no single condition, so they |
| 1413 | * chain two FCSELs. |
| 1414 | */ |
| 1415 | #define CPU_SEL(cond_) __asm__ volatile("fcmp %s1, %s2\n\tfcsel %s0, %s1, %s2, " cond_ \ |
| 1416 | : "=&w"(r) : "w"(x), "w"(y) : "cc") |
| 1417 | case OMEGA_NOP_FSETP_LT_SEL: CPU_SEL("mi"); break; /* N */ |
| 1418 | case OMEGA_NOP_FSETP_LE_SEL: CPU_SEL("ls"); break; /* C==0 or Z */ |
| 1419 | case OMEGA_NOP_FSETP_GT_SEL: CPU_SEL("gt"); break; /* !Z, N==V */ |
| 1420 | case OMEGA_NOP_FSETP_EQ_SEL: CPU_SEL("eq"); break; |
| 1421 | case OMEGA_NOP_FSETP_NUM_SEL: CPU_SEL("vc"); break; /* ordered */ |
| 1422 | case OMEGA_NOP_FSETP_NAN_SEL: CPU_SEL("vs"); break; /* unordered */ |
| 1423 | case OMEGA_NOP_FSETP_LTU_SEL: CPU_SEL("lt"); break; /* N!=V */ |
| 1424 | case OMEGA_NOP_FSETP_LEU_SEL: CPU_SEL("le"); break; /* Z or N!=V */ |
| 1425 | case OMEGA_NOP_FSETP_GTU_SEL: CPU_SEL("hi"); break; /* C and !Z */ |
| 1426 | case OMEGA_NOP_FSETP_GEU_SEL: CPU_SEL("pl"); break; /* N==0 */ |
| 1427 | case OMEGA_NOP_FSETP_NEU_SEL: CPU_SEL("ne"); break; /* !Z */ |
| 1428 | #undef CPU_SEL |
| 1429 | case OMEGA_NOP_FSETP_NE_SEL: { /* less or greater */ |
| 1430 | float t; |
| 1431 | __asm__ volatile("fcmp %s2, %s3\n\tfcsel %s1, %s2, %s3, gt\n\tfcsel %s0, %s2, %s1, mi" |
| 1432 | : "=&w"(r), "=&w"(t) : "w"(x), "w"(y) : "cc"); |
| 1433 | break; |
| 1434 | } |
| 1435 | case OMEGA_NOP_FSETP_EQU_SEL: { /* equal or unordered */ |
| 1436 | float t; |
| 1437 | __asm__ volatile("fcmp %s2, %s3\n\tfcsel %s1, %s2, %s3, vs\n\tfcsel %s0, %s2, %s1, eq" |
| 1438 | : "=&w"(r), "=&w"(t) : "w"(x), "w"(y) : "cc"); |
| 1439 | break; |
| 1440 | } |
| 1441 | /* E1 conversions: the AArch64 conversion instructions. NaN -> 0 and |
| 1442 | * saturation are architectural for FCVT*S / FCVT*U. */ |
| 1443 | #define CPU_F2I(insn_) do { int32_t iv_; \ |
| 1444 | __asm__ volatile(insn_ " %w0, %s1" : "=r"(iv_) : "w"(x)); \ |
| 1445 | r = omega_bits_to_float((uint32_t)iv_); } while (0) |
| 1446 | case OMEGA_NOP_F2I_FLOOR: CPU_F2I("fcvtms"); break; |
| 1447 | case OMEGA_NOP_F2I_CEIL: CPU_F2I("fcvtps"); break; |
| 1448 | case OMEGA_NOP_F2I_RNI: CPU_F2I("fcvtns"); break; |
| 1449 | case OMEGA_NOP_F2U: CPU_F2I("fcvtzu"); break; |
| 1450 | #undef CPU_F2I |
| 1451 | case OMEGA_NOP_I2FP_U32: { |
| 1452 | uint32_t uv = omega_float_to_bits(x); |
| 1453 | __asm__ volatile("ucvtf %s0, %w1" : "=w"(r) : "r"(uv)); |
| 1454 | break; |
| 1455 | } |
| 1456 | case OMEGA_NOP_F32_TO_F16: { |
| 1457 | uint32_t hv; |
| 1458 | __asm__ volatile("fcvt h16, %s1\n\tfmov %w0, s16" : "=r"(hv) : "w"(x) : "v16"); |
| 1459 | r = omega_bits_to_float(hv & 0xffffU); |
| 1460 | break; |
| 1461 | } |
| 1462 | case OMEGA_NOP_F32_TO_BF16: { |
| 1463 | if (!bf16) return OMEGA_NUMERIC_ERR_NOT_ENCODED; |
| 1464 | uint32_t hv; |
| 1465 | __asm__ volatile(".arch_extension bf16\n\tbfcvt h16, %s1\n\tfmov %w0, s16" : "=r"(hv) : "w"(x) : "v16"); |
| 1466 | r = omega_bits_to_float(hv & 0xffffU); |
| 1467 | break; |
| 1468 | } |
| 1469 | case OMEGA_NOP_F16_TO_F32: { |
| 1470 | uint32_t hv = omega_float_to_bits(x) & 0xffffU; |
| 1471 | __asm__ volatile("fmov s16, %w1\n\tfcvt %s0, h16" : "=w"(r) : "r"(hv) : "v16"); |
| 1472 | break; |
| 1473 | } |
| 1474 | case OMEGA_NOP_BF16_TO_F32: { |
| 1475 | /* AArch64 has no scalar BF16 -> FP32 instruction: widening is a |
| 1476 | * 16-bit left shift (what SHLL / BFCVTN's inverse do). Same |
| 1477 | * definition as the reference, so this is not an independent check. */ |
| 1478 | uint32_t hv = omega_float_to_bits(x) & 0xffffU, wv; |
| 1479 | __asm__ volatile("lsl %w0, %w1, #16" : "=r"(wv) : "r"(hv)); |
| 1480 | r = omega_bits_to_float(wv); |
| 1481 | break; |
| 1482 | } |
| 1483 | case OMEGA_NOP_FFMA_V: |
| 1484 | __asm__ volatile("fmadd %s0, %s1, %s2, %s3" : "=w"(r) : "w"(x), "w"(y), "w"(c[i])); |
| 1485 | break; |
| 1486 | case OMEGA_NOP_COUNT: return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1487 | } |
| 1488 | out[i] = r; |
| 1489 | } |
| 1490 | return OMEGA_NUMERIC_OK; |
| 1491 | } |
| 1492 | |
| 1493 | /* ---- Parity --------------------------------------------------------------- */ |
| 1494 | |
| 1495 | int omega_numeric_parity(OmegaNumericOp op, const float *expect, const float *got, |
| 1496 | size_t count, OmegaParityTrace *trace) { |
| 1497 | if ((unsigned)op >= OMEGA_NOP_COUNT || !expect || !got || !trace) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1498 | memset(trace, 0, sizeof(*trace)); |
| 1499 | trace->first_index = -1; |
| 1500 | OmegaNumericCompare mode = OP_TABLE[op].compare; |
| 1501 | if (mode == OMEGA_CMP_SEED_BOUND) return OMEGA_NUMERIC_ERR_OPERANDS; /* never bit-compare MUFU */ |
| 1502 | for (size_t i = 0; i < count; i++) { |
| 1503 | if (!omega_numeric_element_checked(op, i)) continue; |
| 1504 | trace->checked++; |
| 1505 | uint32_t ue = omega_float_to_bits(expect[i]); |
| 1506 | uint32_t ug = omega_float_to_bits(got[i]); |
| 1507 | if (mode == OMEGA_CMP_BIT_EXACT && omega_issubnormal(expect[i])) trace->subnormal_expected++; |
| 1508 | bool same = omega_numeric_compare_equal(mode, ue, ug); |
| 1509 | if (!same) { |
| 1510 | if (trace->mismatches == 0) { |
| 1511 | trace->first_index = (long)i; |
| 1512 | trace->first_expect = ue; |
| 1513 | trace->first_got = ug; |
| 1514 | } |
| 1515 | trace->mismatches++; |
| 1516 | } |
| 1517 | } |
| 1518 | return OMEGA_NUMERIC_OK; |
| 1519 | } |
| 1520 | |
| 1521 | static bool is_finite_normal(float x) { |
| 1522 | uint32_t e = (omega_float_to_bits(x) >> 23) & 0xffU; |
| 1523 | return e != 0 && e != 0xffU; |
| 1524 | } |
| 1525 | |
| 1526 | int omega_numeric_seed_bound(OmegaNumericOp op, const float *a, const float *got, |
| 1527 | size_t count, OmegaParityTrace *trace) { |
| 1528 | if ((unsigned)op >= OMEGA_NOP_COUNT || !a || !got || !trace) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1529 | if (OP_TABLE[op].compare != OMEGA_CMP_SEED_BOUND) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 1530 | if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; |
| 1531 | memset(trace, 0, sizeof(*trace)); |
| 1532 | trace->first_index = -1; |
| 1533 | const double bound = 1.0 / 1048576.0; /* 2^-20 relative */ |
| 1534 | for (size_t i = 0; i < count; i++) { |
| 1535 | float x = a[i]; |
| 1536 | if (!is_finite_normal(x) || (op == OMEGA_NOP_MUFU_RSQ && omega_signbit(x))) { |
| 1537 | trace->bound_skipped++; |
| 1538 | continue; |
| 1539 | } |
| 1540 | float e = (op == OMEGA_NOP_MUFU_RCP) ? omega_ieee_div(1.0f, x) |
| 1541 | : omega_ieee_div(1.0f, omega_ieee_sqrt(x)); |
| 1542 | if (!is_finite_normal(e)) { |
| 1543 | trace->bound_skipped++; |
| 1544 | continue; |
| 1545 | } |
| 1546 | trace->checked++; |
| 1547 | double d = (double)got[i] - (double)e; |
| 1548 | if (d < 0) d = -d; |
| 1549 | double m = (double)e; |
| 1550 | if (m < 0) m = -m; |
| 1551 | if (omega_isnan(got[i]) || !(d <= m * bound)) { |
| 1552 | if (trace->out_of_bound == 0) { |
| 1553 | trace->first_index = (long)i; |
| 1554 | trace->first_expect = omega_float_to_bits(e); |
| 1555 | trace->first_got = omega_float_to_bits(got[i]); |
| 1556 | } |
| 1557 | trace->out_of_bound++; |
| 1558 | } |
| 1559 | } |
| 1560 | return OMEGA_NUMERIC_OK; |
| 1561 | } |
| 1562 | |
| 1563 | /* ---- Flush-to-zero model (negative test only) ----------------------------- */ |
| 1564 | |
| 1565 | static float ftz(float x) { |
| 1566 | return omega_issubnormal(x) ? omega_bits_to_float(omega_float_to_bits(x) & 0x80000000U) : x; |
| 1567 | } |
| 1568 | |
| 1569 | int omega_numeric_reference_ftz(OmegaNumericOp op, const float *a, const float *b, |
| 1570 | const float *c, float *out, size_t count) { |
| 1571 | if ((unsigned)op >= OMEGA_NOP_COUNT || !a || !out) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1572 | if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; |
| 1573 | if (needs_b(op) && !b) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1574 | if (OP_TABLE[op].arity >= 3 && !c) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 1575 | for (size_t i = 0; i < count; i++) { |
| 1576 | float x = ftz(a[i]); |
| 1577 | float y = b ? ftz(b[i]) : 0.0f; |
| 1578 | float r; |
| 1579 | switch (op) { |
| 1580 | case OMEGA_NOP_FADD: r = omega_ref_fadd(x, y); break; |
| 1581 | case OMEGA_NOP_FSUB: r = omega_ref_fsub(x, y); break; |
| 1582 | case OMEGA_NOP_FMUL: r = omega_ref_fmul(x, y); break; |
| 1583 | case OMEGA_NOP_FFMA: r = omega_ref_ffma(x, y, ftz(c[i])); break; |
| 1584 | case OMEGA_NOP_FFMA_V: r = omega_ref_ffma_int(x, y, ftz(c[i])); break; |
| 1585 | case OMEGA_NOP_FMNMX_MIN: r = omega_ref_fmin(x, y); break; |
| 1586 | case OMEGA_NOP_FMNMX_MAX: r = omega_ref_fmax(x, y); break; |
| 1587 | default: return OMEGA_NUMERIC_ERR_OPERANDS; /* only arithmetic ops have an FTZ model */ |
| 1588 | } |
| 1589 | out[i] = ftz(r); |
| 1590 | } |
| 1591 | return OMEGA_NUMERIC_OK; |
| 1592 | } |