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