interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_numeric.h

440 lines · 19951 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef OMEGA_NUMERIC_H
18#define OMEGA_NUMERIC_H
19
20/*
21 * OMEGA-NUMERIC-0 (M19R Gate 5): FP32 substrate.
22 *
23 * This file is host-pure: the semantic reference tier, the CPU realization
24 * tier, the Omega math sequences, the op registry (which ops have a GB10
25 * encoding and how their results are compared), the kernel patch words, and
26 * the parity comparators. Nothing here opens a device. The GB10 executor is
27 * src/omega_numeric_gb10.c.
28 *
29 * Build with -ffp-contract=off: the reference and CPU tiers must not depend
30 * on whether the compiler fuses a*b+c. Fused operations are written as
31 * explicit AArch64 instructions.
32 */
33
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdint.h>
37
38/*
39 * Frozen numerical reduction order specification:
40 * Pairwise binary tree across 32 lanes (delta = 16, 8, 4, 2, 1).
41 */
42#define OMEGA_WARP_REDUCTION_DECLARED_ORDER "PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1"
43
44/*
45 * Bitcast utility functions
46 */
47static inline uint32_t omega_float_to_bits(float f) {
48 union { float f; uint32_t u; } u;
49 u.f = f;
50 return u.u;
51}
52
53static inline float omega_bits_to_float(uint32_t u) {
54 union { float f; uint32_t u; } un;
55 un.u = u;
56 return un.f;
57}
58
59/*
60 * Bit-level IEEE-754 classification and manipulation (Zero Libm)
61 */
62#define OMEGA_INF_POS 0x7f800000U
63#define OMEGA_INF_NEG 0xff800000U
64#define OMEGA_QNAN_BITS 0x7fc00000U
65
66static inline float omega_fabs(float x) {
67 return omega_bits_to_float(omega_float_to_bits(x) & 0x7fffffffU);
68}
69
70static inline bool omega_isnan(float x) {
71 uint32_t u = omega_float_to_bits(x);
72 return ((u & 0x7f800000U) == 0x7f800000U) && ((u & 0x007fffffU) != 0);
73}
74
75static inline bool omega_isinf(float x) {
76 uint32_t u = omega_float_to_bits(x);
77 return (u & 0x7fffffffU) == 0x7f800000U;
78}
79
80static inline bool omega_signbit(float x) {
81 return (omega_float_to_bits(x) & 0x80000000U) != 0;
82}
83
84static inline bool omega_issubnormal(float x) {
85 uint32_t u = omega_float_to_bits(x);
86 return ((u & 0x7f800000U) == 0) && ((u & 0x007fffffU) != 0);
87}
88
89static inline bool omega_iszero(float x) {
90 return (omega_float_to_bits(x) & 0x7fffffffU) == 0;
91}
92
93/*
94 * Required host FP environment. The reference and CPU tiers compute with the
95 * host FPU, so a caller that changed the rounding mode or turned on flush to
96 * zero would make both tiers agree on the same wrong answer. These FPCR bits
97 * must be clear: RMode [23:22] (00 = round to nearest even), FZ [24], DN [25],
98 * FZ16 [19], AHP [26] (alternative half precision: F16 conversions must use
99 * IEEE binary16), and the FEAT_AFP controls FIZ [0], AH [1], NEP [2].
100 * omega_numeric_reference, omega_numeric_cpu_realize,
101 * omega_numeric_reference_ftz and omega_numeric_seed_bound refuse with
102 * OMEGA_NUMERIC_ERR_FPENV (and compute nothing) when any is set. They never
103 * change FPCR themselves.
104 */
105#define OMEGA_NUMERIC_FPCR_REQUIRED_CLEAR \
106 ((1ULL << 0) | (1ULL << 1) | (1ULL << 2) | (1ULL << 19) | (3ULL << 22) | (1ULL << 24) | (1ULL << 25) | \
107 (1ULL << 26))
108uint64_t omega_numeric_read_fpcr(void);
109bool omega_numeric_fpenv_ok(void);
110
111/*
112 * Semantic Reference Tier:
113 * Exact IEEE 754-2008 single-precision specification, round to nearest even.
114 * Subnormals are preserved bit-exactly (Flush-To-Zero is strictly rejected).
115 */
116float omega_ref_fadd(float a, float b);
117float omega_ref_fsub(float a, float b);
118float omega_ref_fmul(float a, float b);
119float omega_ref_ffma(float a, float b, float c);
120float omega_ref_fmin(float a, float b);
121float omega_ref_fmax(float a, float b);
122bool omega_ref_fsetp_ge(float a, float b);
123float omega_ref_i2f(int32_t a);
124int32_t omega_ref_f2i(float a);
125/*
126 * E1 scalar contract (docs/numeric/E1_SCALAR_CONTRACT.md). Explicit,
127 * FPCR-independent reference definitions written with integer operations on
128 * the IEEE bit patterns; no host conversion or compare instruction is used.
129 *
130 * Integer conversions: NaN -> 0; the value is rounded to an integer in the
131 * named direction, then saturated to the target range (F2U: negatives -> 0).
132 * RNI is round to nearest, ties to even.
133 */
134int32_t omega_ref_f2i_floor(float a);
135int32_t omega_ref_f2i_ceil(float a);
136int32_t omega_ref_f2i_rni(float a);
137uint32_t omega_ref_f2u(float a); /* truncate */
138float omega_ref_u2f(uint32_t a); /* RNE */
139/* Narrowing: RNE, overflow to signed infinity, subnormal results kept, NaN
140 * stays NaN (payload not semantic). Widening is exact. */
141uint16_t omega_ref_f32_to_f16(float a);
142uint16_t omega_ref_f32_to_bf16(float a);
143float omega_ref_f16_to_f32(uint16_t h);
144float omega_ref_bf16_to_f32(uint16_t h);
145/*
146 * Compare predicate of a compare-and-select op (FSETP_SEL and the
147 * FSETP_<P>_SEL ops): 1 if the predicate holds for (a, b), 0 if not, -1 if
148 * op is not a compare-and-select op. Ordered predicates are false when either
149 * operand is NaN, unordered (U) ones true; -0 equals +0.
150 */
151int omega_ref_fsetp_pred(int op, float a, float b);
152/* a * b + c rounded once (RNE, subnormals kept), integer arithmetic on the bit
153 * patterns (no FMADD). NaN results are the canonical quiet NaN. */
154float omega_ref_ffma_int(float a, float b, float c);
155/* True when the host CPU has FEAT_BF16 (BFCVT); the CPU realization of
156 * F32_TO_BF16 then uses BFCVT, otherwise it refuses (OMEGA_NUMERIC_ERR_NOT_ENCODED). */
157bool omega_numeric_cpu_has_bf16(void);
158
159/* Correctly rounded IEEE division and square root (the AArch64 FDIV/FSQRT
160 * instructions; not libm). The semantic target for omega_math_div/sqrt. */
161float omega_ieee_div(float x, float y);
162float omega_ieee_sqrt(float x);
163
164/*
165 * Omega-defined sequences for division and square root: correctly rounded
166 * (round to nearest, ties to even, subnormal results kept, overflow to
167 * infinity), computed with integer operations only (restoring long division,
168 * bitwise integer square root, one shared rounding step). No MUFU seed and no
169 * FP arithmetic, so the result does not depend on FPCR. Compared bit for bit
170 * against omega_ieee_div / omega_ieee_sqrt. Host sweep on 2026-09-30: SQRT
171 * over all 2^32 inputs and DIV over 10^9 stratified random pairs, zero
172 * mismatches.
173 */
174float omega_math_div(float x, float y);
175float omega_math_sqrt(float x);
176
177/*
178 * Omega-defined polynomial approximations for Transcendentals:
179 * Zero dependency on libm or CUDA math libraries.
180 * Range reduction, coefficients, and evaluation order are frozen.
181 */
182float omega_math_exp(float x);
183float omega_math_log(float x);
184
185/*
186 * Declared-order warp reduction:
187 * Reduces 32 float inputs across a warp using the frozen pairwise-tree order.
188 */
189float omega_warp_reduce_sum(const float warp_inputs[32]);
190
191/*
192 * Equivalence comparator:
193 * Verifies that two floats match bit-for-bit, except that all quiet/signaling
194 * NaNs are considered equivalent in the NaN class.
195 */
196bool omega_numeric_bits_equal(float a, float b);
197
198/* ---- Op registry --------------------------------------------------------- */
199
200typedef enum {
201 OMEGA_NOP_FADD = 0,
202 OMEGA_NOP_FSUB,
203 OMEGA_NOP_FMUL,
204 OMEGA_NOP_FFMA, /* a*b + c, c uniform per launch (constant bank) */
205 OMEGA_NOP_FSETP_SEL, /* FSETP.GE P0, a, b ; FSEL out = P0 ? a : b */
206 OMEGA_NOP_FSEL, /* FSETP.GE P0, a, RZ ; FSEL out = P0 ? b : a */
207 OMEGA_NOP_FMNMX_MIN,
208 OMEGA_NOP_FMNMX_MAX,
209 OMEGA_NOP_I2FP, /* input bits read as int32, output FP32 */
210 OMEGA_NOP_F2I, /* input FP32, output bits are int32 */
211 OMEGA_NOP_MUFU_RCP, /* seed only: never compared bit-exactly */
212 OMEGA_NOP_MUFU_RSQ, /* seed only: never compared bit-exactly */
213 OMEGA_NOP_LDS_STS, /* STS, BAR.SYNC, LDS: out[i] = a[i ^ 63] per CTA */
214 OMEGA_NOP_SHFL_DOWN, /* SHFL.DOWN by 1, clamp 0x1f */
215 OMEGA_NOP_DIV,
216 OMEGA_NOP_SQRT,
217 OMEGA_NOP_EXP,
218 OMEGA_NOP_LOG,
219 OMEGA_NOP_REDUCE_SUM,
220 /* E1 scalar contract ops (docs/numeric/E1_SCALAR_CONTRACT.md): semantic
221 * reference, CPU realization and GB10 encoding (E1 WP-C; GB10 parity is
222 * established by the Gate 5 chip receipt only). Compare-and-select: out = P(a, b) ? a : b, bits
223 * moved unchanged (FSETP.<P> P0, a, b ; FSEL out, a, b, P0). */
224 OMEGA_NOP_FSETP_LT_SEL,
225 OMEGA_NOP_FSETP_LE_SEL,
226 OMEGA_NOP_FSETP_GT_SEL,
227 OMEGA_NOP_FSETP_EQ_SEL,
228 OMEGA_NOP_FSETP_NE_SEL, /* ordered: false if either is NaN */
229 OMEGA_NOP_FSETP_NUM_SEL, /* neither is NaN */
230 OMEGA_NOP_FSETP_NAN_SEL, /* either is NaN */
231 OMEGA_NOP_FSETP_LTU_SEL,
232 OMEGA_NOP_FSETP_LEU_SEL,
233 OMEGA_NOP_FSETP_GTU_SEL,
234 OMEGA_NOP_FSETP_GEU_SEL,
235 OMEGA_NOP_FSETP_EQU_SEL,
236 OMEGA_NOP_FSETP_NEU_SEL, /* IEEE !=: true if either is NaN */
237 OMEGA_NOP_F2I_FLOOR, /* input FP32, output bits are int32 */
238 OMEGA_NOP_F2I_CEIL,
239 OMEGA_NOP_F2I_RNI,
240 OMEGA_NOP_F2U, /* input FP32, output bits are uint32 */
241 OMEGA_NOP_I2FP_U32, /* input bits read as uint32, output FP32 */
242 OMEGA_NOP_F32_TO_F16, /* output: F16 bits in [15:0], [31:16] zero */
243 OMEGA_NOP_F32_TO_BF16, /* output: BF16 bits in [15:0], [31:16] zero */
244 OMEGA_NOP_F16_TO_F32, /* input: F16 bits in [15:0], [31:16] ignored */
245 OMEGA_NOP_BF16_TO_F32, /* input: BF16 bits in [15:0], [31:16] ignored */
246 OMEGA_NOP_FFMA_V, /* a*b + c, c read per element */
247 OMEGA_NOP_COUNT
248} OmegaNumericOp;
249
250typedef enum {
251 OMEGA_CMP_BIT_EXACT = 0, /* FP32 bits equal; any NaN equals any NaN */
252 OMEGA_CMP_INT_EXACT, /* raw 32-bit words equal (integer results) */
253 OMEGA_CMP_SEED_BOUND, /* MUFU: relative-error bound only, never bits */
254 OMEGA_CMP_F16_BITS, /* [31:16] zero; [15:0] F16 bits equal, any F16 NaN equals any F16 NaN */
255 OMEGA_CMP_BF16_BITS /* [31:16] zero; [15:0] BF16 bits equal, any BF16 NaN equals any BF16 NaN */
256} OmegaNumericCompare;
257
258/* The equality a parity comparison uses for one element under mode (not
259 * defined for SEED_BOUND, which returns false). */
260bool omega_numeric_compare_equal(OmegaNumericCompare mode, uint32_t expect, uint32_t got);
261
262typedef struct {
263 OmegaNumericOp op;
264 const char *name;
265 int arity; /* inputs used: 1 (a), 2 (a,b), 3 (a,b,c) */
266 bool gb10_encoded; /* a real kernel exists for this op */
267 OmegaNumericCompare compare;
268 const char *reference; /* what the expected value is */
269 const char *not_encoded_reason;
270} OmegaNumericOpInfo;
271
272size_t omega_numeric_op_count(void);
273const OmegaNumericOpInfo *omega_numeric_op_at(size_t index);
274/* Finds an op by name. "LDS" and "STS" are aliases of LDS_STS, "FSETP" of
275 * FSETP_SEL. Returns NULL for unknown names. */
276const OmegaNumericOpInfo *omega_numeric_op_find(const char *name);
277const char *omega_numeric_compare_name(OmegaNumericCompare c);
278
279/* Ops whose GB10 kernel is a whole program from
280 * src/omega_numeric_divsqrt_gb10.c (DIV, SQRT, EXP, LOG) instead of a patch into the
281 * calibrated vecadd: they have no patch words, omega_numeric_build_kernel
282 * refuses them, and their structural checks (omega_ds_check_args, _kernel,
283 * _digest, _qmd) run inside omega_ds_gb10_run before the device is opened. */
284bool omega_numeric_op_whole_kernel(OmegaNumericOp op);
285
286/* Return codes of omega_numeric_submit_check and omega_gb10_execute_simt_op */
287#define OMEGA_NUMERIC_OK 0
288#define OMEGA_NUMERIC_ERR_BAD_ARGS -1 /* unknown op or missing buffers */
289#define OMEGA_NUMERIC_ERR_NOT_ENCODED -2 /* op known, no GB10 kernel exists */
290#define OMEGA_NUMERIC_ERR_OPERANDS -3 /* operand shape the kernel can't do */
291#define OMEGA_NUMERIC_ERR_DEVICE -4 /* device open/alloc/submit/wait */
292#define OMEGA_NUMERIC_ERR_FPENV -5 /* host FPCR is not RNE with FZ clear */
293
294#define OMEGA_NUMERIC_MAX_COUNT 65536u
295
296/*
297 * Threads per CTA of every numeric launch (QMD CTA_THREAD_DIMENSION0 and the
298 * grid width in src/omega_numeric_gb10.c). LDS_STS exchanges values inside
299 * one CTA, so its count must be a whole number of CTAs: a partial CTA would
300 * have exited threads that never reach BAR.SYNC.
301 */
302#define OMEGA_NUMERIC_CTA_THREADS 64u
303/* Bytes of shared memory LDS_STS touches per CTA: one 32-bit word per thread. */
304#define OMEGA_NUMERIC_LDS_STS_SHARED_BYTES (OMEGA_NUMERIC_CTA_THREADS * 4u)
305
306/*
307 * Variants of the shared-memory and warp ops that have no GB10 encoding.
308 * omega_numeric_submit_check refuses each with OMEGA_NUMERIC_ERR_NOT_ENCODED
309 * and names it, so a caller asking for LDS.U8 or SHFL_UP learns it is
310 * unencoded instead of being handed the 32-bit or SHFL.DOWN kernel.
311 */
312size_t omega_numeric_refused_variant_count(void);
313const char *omega_numeric_refused_variant_at(size_t index);
314
315/*
316 * Everything that must be true before any device is touched. Writes a
317 * one-line reason into err (if non-NULL) on refusal.
318 */
319int omega_numeric_submit_check(const char *op_name,
320 const float *in_a, const float *in_b, const float *in_c,
321 const float *out_res, size_t count,
322 char *err, size_t err_len);
323
324/*
325 * Kernel patch: the instruction words written at byte offset
326 * OMEGA_NUMERIC_PATCH_OFFSET of the vecadd kernel (replacing its IADD3, and
327 * for multi-instruction ops also its STG/EXIT). Returns the number of 128-bit
328 * instructions, or a negative OMEGA_NUMERIC_ERR_* code for ops without an
329 * encoding.
330 */
331#define OMEGA_NUMERIC_PATCH_OFFSET 0x110u
332#define OMEGA_NUMERIC_PATCH_MAX 15u /* slots 0x110..0x1f0 of the 0x200-byte kernel */
333typedef struct {
334 uint32_t w[4];
335 const char *text;
336 const char *provenance_key; /* NULL for vecadd baseline STG / EXIT */
337} OmegaNumericPatchInsn;
338
339int omega_numeric_patch_words(OmegaNumericOp op,
340 OmegaNumericPatchInsn out[OMEGA_NUMERIC_PATCH_MAX]);
341
342/* Encodes the vecadd baseline and applies the patch for op. */
343int omega_numeric_build_kernel(OmegaNumericOp op, uint8_t *code, size_t code_len,
344 size_t *out_len);
345
346/*
347 * Structural check of a patch against the launch it will run in. Called by
348 * omega_numeric_submit_check (on a QMD built for count) and again by the GB10
349 * executor on the exact QMD it submits. It decodes the words, so it catches a
350 * wrong patch even when the patch table itself was edited:
351 * every patch length 1..PATCH_MAX; a variable-latency result (write
352 * barrier set) is waited on by the very next instruction;
353 * STS/LDS/BAR/SHFL words match their one encoded form in every
354 * non-register bit (no .U8/.64/.128, no offsets, no other
355 * barrier id, no SHFL mode other than DOWN);
356 * shared use STS before BAR.SYNC 0 before LDS; the QMD declares at least
357 * one barrier; the address is SHF.L R0 (tid.x) by exactly 2,
358 * the LDS address is that value XOR (CTA-1)*4; the CTA width
359 * in the QMD is OMEGA_NUMERIC_CTA_THREADS (a power of two);
360 * (CTA-1)*4 + 4 bytes fit in the QMD shared-memory size;
361 * REDUCE_SUM exactly five SHFL.DOWN, deltas 16, 8, 4, 2, 1 in that
362 * order, clamp 0x1f, each followed by an FADD (no negate, no
363 * modifiers) that waits on the shuffle, adds the shuffled value
364 * to the running sum, and the last FADD writes R9, which the
365 * STG stores.
366 * qmd1_words is the 96-word compute QMD. Returns OMEGA_NUMERIC_OK or
367 * OMEGA_NUMERIC_ERR_OPERANDS with a one-line reason in err.
368 */
369int omega_numeric_check_patch(OmegaNumericOp op, const OmegaNumericPatchInsn *patch, int n,
370 const uint32_t *qmd1_words, char *err, size_t err_len);
371
372/* Launch shape for count elements: OMEGA_NUMERIC_CTA_THREADS threads per CTA
373 * and enough CTAs to cover count. Shared by submit_check and the executor. */
374void omega_numeric_launch_shape(size_t count, uint32_t *threads_per_block, uint32_t *grid_width);
375
376/* ---- Tiers and comparison ------------------------------------------------- */
377
378/* Semantic reference tier for every op. c may be NULL except for FFMA and
379 * FFMA_V (arity 3). */
380int omega_numeric_reference(OmegaNumericOp op, const float *a, const float *b,
381 const float *c, float *out, size_t count);
382
383/* CPU realization tier: the AArch64 instruction (or the Omega sequence for
384 * DIV/SQRT/EXP/LOG) that realizes each op on the host. */
385int omega_numeric_cpu_realize(OmegaNumericOp op, const float *a, const float *b,
386 const float *c, float *out, size_t count);
387
388/* Elements that carry a result for op (REDUCE_SUM: lane 0 of each warp). */
389bool omega_numeric_element_checked(OmegaNumericOp op, size_t index);
390
391typedef struct {
392 size_t checked; /* elements compared */
393 size_t mismatches;
394 long first_index; /* -1 when none */
395 uint32_t first_expect;
396 uint32_t first_got;
397 size_t subnormal_expected; /* checked elements whose expected value is subnormal */
398 size_t out_of_bound; /* SEED_BOUND only: results outside the bound */
399 size_t bound_skipped; /* SEED_BOUND only: elements outside the bounded domain */
400} OmegaParityTrace;
401
402/*
403 * Bit parity: refuses (returns OMEGA_NUMERIC_ERR_OPERANDS) for a SEED_BOUND
404 * op. That refusal is the structural guarantee behind
405 * MUFU_SEED_ONLY_NOT_COMPARED.
406 */
407int omega_numeric_parity(OmegaNumericOp op, const float *expect, const float *got,
408 size_t count, OmegaParityTrace *trace);
409
410/* Seed bound for MUFU: compares against the true 1/x or 1/sqrt(x) (IEEE)
411 * with relative error <= 2^-20 on inputs whose result is a finite normal.
412 * Refuses non-SEED_BOUND ops. */
413int omega_numeric_seed_bound(OmegaNumericOp op, const float *a, const float *got,
414 size_t count, OmegaParityTrace *trace);
415
416/* Flush-to-zero model used by the negative test: subnormal inputs and outputs
417 * replaced by signed zero. */
418int omega_numeric_reference_ftz(OmegaNumericOp op, const float *a, const float *b,
419 const float *c, float *out, size_t count);
420
421/* Host magic-constant seeds (NOT a model of hardware MUFU). Used only to show
422 * that an unrefined seed fails bit parity. */
423float omega_numeric_host_rcp_seed(float y);
424
425/* ---- GB10 executor (src/omega_numeric_gb10.c) ----------------------------- */
426
427/*
428 * Execution on physical GB10 silicon: runs omega_numeric_submit_check first
429 * (no device is opened when it refuses), then patches the calibrated vecadd
430 * kernel and dispatches it through the GPFIFO channel. FFMA's c operand must
431 * be uniform (it is carried in the constant bank).
432 */
433int omega_gb10_execute_simt_op(const char *op_name,
434 const float *in_a,
435 const float *in_b,
436 const float *in_c,
437 float *out_res,
438 size_t count);
439
440#endif /* OMEGA_NUMERIC_H */