omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_numeric_divsqrt_gb10.h
| 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_DIVSQRT_GB10_H |
| 18 | #define OMEGA_NUMERIC_DIVSQRT_GB10_H |
| 19 | /* |
| 20 | * E1 gap-table row 7: correctly rounded FP32 DIV and SQRT on the GB10 |
| 21 | * (Blackwell sm_121), bit-identical to omega_math_div / omega_math_sqrt |
| 22 | * (round to nearest even, subnormals kept, no FTZ, canonical quiet NaN |
| 23 | * 0x7fc00000 for every NaN result). |
| 24 | * |
| 25 | * The kernel is straight-line integer code (no MUFU, no FP arithmetic, no |
| 26 | * branch): the vecadd prologue (thread index, bounds, LDG a and b) and |
| 27 | * epilogue (STG, EXIT) from omega_blackwell_encode_vecadd, and a body built |
| 28 | * as a small instruction list (OmegaDsInsn) that is |
| 29 | * - encoded into sm_121 words by omega_ds_encode (form templates recorded |
| 30 | * in OMEGA_DS_FORMS, every form decoded by nvdisasm offline), |
| 31 | * - executed on the host by omega_ds_host_exec (same list, same order), |
| 32 | * - checked structurally before submission (omega_ds_check_kernel). |
| 33 | * The body mirrors the integer sequences of src/omega_numeric.c: |
| 34 | * restoring long division (27 quotient bits), digit-by-digit square root |
| 35 | * (27 result bits, 32-bit remainder form), one shared rounding block. |
| 36 | * The only non-integer instruction is I2FP.F32.S32 on a subnormal's |
| 37 | * fraction (< 2^23, so the conversion is exact) to find its leading bit. |
| 38 | * |
| 39 | * E1 row 10 adds the transcendentals (EXP2, LOG2, ..., and the natural EXP |
| 40 | * and LOG of src/omega_numeric.c) to the same frame. Their |
| 41 | * bodies issue the frozen CPU sequence of src/omega_numeric_transc.c in the |
| 42 | * written order: FADD (FSUB = FADD with -Rb), FMUL and FFMA (single |
| 43 | * rounding) in place of the AArch64 FADD/FSUB/FMUL/FMADD, the integer DIV |
| 44 | * body above in place of FDIV, integer bit operations for sign flips, |
| 45 | * classification and powers of two, and every branch computed and chosen |
| 46 | * with ISETP/SEL. Constants are loaded with IADD3 Rd, RZ, imm, RZ. No MUFU. |
| 47 | * The host model runs the FP forms with the AArch64 instructions themselves |
| 48 | * and turns any NaN result into 0x7fffffff (the GB10 canonical NaN); the |
| 49 | * chip run, not the model, establishes GB10 parity. |
| 50 | */ |
| 51 | #include "omega_numeric.h" |
| 52 | |
| 53 | #include <stdbool.h> |
| 54 | #include <stddef.h> |
| 55 | #include <stdint.h> |
| 56 | |
| 57 | /* DIV and SQRT (E1 row 7) and the transcendentals of E1 row 10, each a |
| 58 | * GB10 realization of the frozen CPU sequence in src/omega_numeric_transc.c |
| 59 | * (EXP and LOG: omega_math_exp / omega_math_log in src/omega_numeric.c, |
| 60 | * unfused FMUL/FADD/FSUB, no FFMA). */ |
| 61 | typedef enum { OMEGA_DS_DIV = 0, OMEGA_DS_SQRT = 1, OMEGA_DS_EXP2 = 2, OMEGA_DS_LOG2 = 3, OMEGA_DS_SIGMOID = 4, OMEGA_DS_TANH = 5, OMEGA_DS_SIN = 6, OMEGA_DS_COS = 7, OMEGA_DS_ERF = 8, OMEGA_DS_GELU = 9, OMEGA_DS_RSQRT = 10, OMEGA_DS_EXP = 11, OMEGA_DS_LOG = 12, OMEGA_DS_OP_COUNT = 13 } OmegaDsOp; |
| 62 | |
| 63 | typedef enum { |
| 64 | DSK_IADD3_R = 0, /* IADD3 Rd, PT, PT, [-]Ra, [-]Rb, Rc */ |
| 65 | DSK_IADD3_I, /* IADD3 Rd, PT, PT, [-]Ra, imm, Rc */ |
| 66 | DSK_LOP3_R, /* LOP3.LUT Rd, Ra, Rb, Rc, lut, !PT */ |
| 67 | DSK_LOP3_I, /* LOP3.LUT Rd, Ra, imm, Rc, lut, !PT */ |
| 68 | DSK_SHL_I, /* SHF.L.U32 Rd, Ra, imm, RZ */ |
| 69 | DSK_SHL_R, /* SHF.L.U32 Rd, Ra, Rb, RZ */ |
| 70 | DSK_SHR_I, /* SHF.R.U32.HI Rd, RZ, imm, Rc */ |
| 71 | DSK_SHR_R, /* SHF.R.U32.HI Rd, RZ, Rb, Rc */ |
| 72 | DSK_ISETP_R, /* ISETP.cmp[.U32].AND Pd, PT, Ra, Rb, PT */ |
| 73 | DSK_ISETP_I, /* ISETP.cmp[.U32].AND Pd, PT, Ra, imm, PT */ |
| 74 | DSK_SEL_R, /* SEL Rd, Ra, Rb, [!]Ps */ |
| 75 | DSK_SEL_I, /* SEL Rd, Ra, imm, [!]Ps */ |
| 76 | DSK_I2FP, /* I2FP.F32.S32 Rd, Rb */ |
| 77 | DSK_FADD_R, /* FADD Rd, Ra, [-]Rb (RN, subnormals kept) */ |
| 78 | DSK_FMUL_R, /* FMUL Rd, Ra, Rb (RN, subnormals kept) */ |
| 79 | DSK_FFMA_R, /* FFMA Rd, Ra, Rb, Rc (one rounding, RN) */ |
| 80 | DSK_COUNT |
| 81 | } OmegaDsKind; |
| 82 | |
| 83 | /* ISETP comparison codes (w2 bits 12-14, nvdisasm-decoded). */ |
| 84 | enum { DS_CMP_LT = 1, DS_CMP_EQ = 2, DS_CMP_LE = 3, DS_CMP_GT = 4, DS_CMP_NE = 5, DS_CMP_GE = 6 }; |
| 85 | |
| 86 | #define OMEGA_DS_RZ 255u |
| 87 | #define OMEGA_DS_PT 7u |
| 88 | |
| 89 | typedef struct { |
| 90 | uint8_t kind; |
| 91 | uint8_t d, a, b, c; /* registers (255 = RZ); ISETP: d = predicate */ |
| 92 | uint8_t nega, negb; /* IADD3 operand negation */ |
| 93 | uint8_t lut; /* LOP3 */ |
| 94 | uint8_t cmp, is_signed; /* ISETP */ |
| 95 | uint8_t ps, pneg; /* SEL predicate */ |
| 96 | uint32_t imm; |
| 97 | } OmegaDsInsn; |
| 98 | |
| 99 | #define OMEGA_DS_MAX_BODY 1000u |
| 100 | #define OMEGA_DS_PROLOGUE_INSNS 17u /* vecadd 0x000-0x100 */ |
| 101 | #define OMEGA_DS_EPILOGUE_INSNS 3u /* STG, EXIT, BRA self */ |
| 102 | #define OMEGA_DS_MAX_CODE_BYTES 0x4000u |
| 103 | /* QMD register allocation. The hardware reserves the top two registers of the |
| 104 | * allocation (Xid 13 "Out Of Range Register" when a 32-register QMD ran a body |
| 105 | * using R30/R31, receipt 1dc85ef2), so a body may use R0..R(count-3). 48 is the |
| 106 | * next multiple of 16 that covers R31. */ |
| 107 | #define OMEGA_DS_GPR_COUNT 48u |
| 108 | #define OMEGA_DS_GPR_RESERVED 2u |
| 109 | #define OMEGA_DS_RESULT_REG 9u |
| 110 | /* Control word (w3 & ~0x1ff) of every body instruction: stall 15, no write or |
| 111 | * read barrier, wait on SB4 (the two LDGs of the prologue). The fixed-latency |
| 112 | * pattern of ptxas 13.0.88 -O0 for sm_121 (stall 15 everywhere) with the wait |
| 113 | * mask moved to the LDG barrier the vecadd prologue uses. */ |
| 114 | #define OMEGA_DS_BODY_CTRL 0x010fde00u |
| 115 | |
| 116 | /* Elements per launch the executor accepts (one launch, one buffer each). */ |
| 117 | #define OMEGA_DS_MAX_BATCH (1u << 24) |
| 118 | |
| 119 | /* SHA-256 of each kernel as emitted, after nvdisasm 13.0.85 -b SM121 decoded |
| 120 | * every word to the listing omega_ds_listing prints |
| 121 | * (tools/divsqrt_nvdisasm_check.sh). */ |
| 122 | extern const char *const OMEGA_DS_KERNEL_SHA256[OMEGA_DS_OP_COUNT]; |
| 123 | |
| 124 | const char *omega_ds_op_name(OmegaDsOp op); |
| 125 | |
| 126 | /* Body instruction list of op. Returns the count (0 on error). */ |
| 127 | size_t omega_ds_body(OmegaDsOp op, OmegaDsInsn *out, size_t max); |
| 128 | |
| 129 | /* Encode one body instruction (control word OMEGA_DS_BODY_CTRL). */ |
| 130 | int omega_ds_encode(const OmegaDsInsn *in, uint32_t w[4]); |
| 131 | /* Decode words back into an instruction of a recorded form; -1 if the words |
| 132 | * are not exactly one of the forms (round trip through omega_ds_encode). */ |
| 133 | int omega_ds_decode(const uint32_t w[4], OmegaDsInsn *out); |
| 134 | /* nvdisasm text for one body instruction, without the trailing " ;". */ |
| 135 | int omega_ds_format(const OmegaDsInsn *in, char *buf, size_t len); |
| 136 | |
| 137 | /* Whole kernel (prologue, body, epilogue, NOP pad to 128 bytes). */ |
| 138 | int omega_ds_build_kernel(OmegaDsOp op, uint8_t *code, size_t max, size_t *out_len); |
| 139 | /* Expected nvdisasm listing of the kernel, one line per instruction: 4-digit hex address, a space, the nvdisasm text. */ |
| 140 | int omega_ds_listing(OmegaDsOp op, char *buf, size_t len); |
| 141 | |
| 142 | /* Runs the body instruction list on the host (bit-exact model of the integer |
| 143 | * forms; I2FP via the host's exact int->float conversion; FADD/FMUL/FFMA via the |
| 144 | * AArch64 instructions, NaN results as 0x7fffffff). */ |
| 145 | uint32_t omega_ds_host_exec(OmegaDsOp op, uint32_t a, uint32_t b); |
| 146 | |
| 147 | /* The CPU semantic the chip must equal for DIV, SQRT, EXP and LOG: |
| 148 | * omega_math_div / omega_math_sqrt / omega_math_exp / omega_math_log of |
| 149 | * src/omega_numeric.c (0x7fc00000 for any other op; the row-10 |
| 150 | * transcendental semantic lives in src/omega_numeric_transc.c, which this |
| 151 | * module does not link). */ |
| 152 | uint32_t omega_ds_cpu_semantic(OmegaDsOp op, uint32_t a, uint32_t b); |
| 153 | |
| 154 | /* The whole-program kernel that runs numeric op `op` (DIV, SQRT, EXP, LOG; |
| 155 | * omega_numeric_op_whole_kernel), or OMEGA_DS_OP_COUNT for any other op. */ |
| 156 | OmegaDsOp omega_ds_op_for_numeric(OmegaNumericOp op); |
| 157 | |
| 158 | /* Structural pre-submission check of a kernel image (see CHECK: markers). |
| 159 | * Returns 0 or a negative OMEGA_NUMERIC_ERR_* and writes "ds_<check>: ..." */ |
| 160 | int omega_ds_check_kernel(OmegaDsOp op, const uint8_t *code, size_t len, |
| 161 | uint32_t gpr_count, char *err, size_t err_len); |
| 162 | /* Same plus the recorded nvdisasm-verified digest. */ |
| 163 | int omega_ds_check_digest(OmegaDsOp op, const uint8_t *code, size_t len, char *err, size_t err_len); |
| 164 | /* Argument check: op, buffers, count. */ |
| 165 | int omega_ds_check_args(OmegaDsOp op, const uint32_t *a, const uint32_t *b, |
| 166 | const uint32_t *out, size_t count, char *err, size_t err_len); |
| 167 | /* QMD check: invariants, register count, program address. */ |
| 168 | int omega_ds_check_qmd(const uint32_t *qmd1, uint64_t code_va, char *err, size_t err_len); |
| 169 | |
| 170 | /* Launch op on the GB10 over count elements (bit patterns). Every check above |
| 171 | * runs first; with -DOMEGA_NUMERIC_CPU_ONLY the device is never touched and |
| 172 | * OMEGA_NUMERIC_ERR_DEVICE is returned after the checks. */ |
| 173 | int omega_ds_gb10_run(OmegaDsOp op, const uint32_t *a, const uint32_t *b, uint32_t *out, size_t count); |
| 174 | |
| 175 | #endif |