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_divsqrt_gb10.c

1812 lines · 96363 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/*
18 * E1 row 7: correctly rounded FP32 DIV and SQRT realized on the GB10.
19 * See omega_numeric_divsqrt_gb10.h for the contract and
20 * docs/numeric/E1_DIVSQRT_GB10.md for the algorithm and the evidence.
21 *
22 * Every pre-submission check carries a CHECK: marker;
23 * tools/divsqrt_check_sweep.sh deletes each one in a scratch copy and proves
24 * a host test then fails. With -DOMEGA_NUMERIC_CPU_ONLY no device is touched.
25 */
26#include "omega_numeric_divsqrt_gb10.h"
27#include "omega_numeric.h"
28#include "omega_numeric_transc_tables.h"
29#include "omega_blackwell_encoder.h"
30#include "omega_blackwell_qmd.h"
31
32#include <stdarg.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37#include <time.h>
38
39#ifndef OMEGA_NUMERIC_CPU_ONLY
40#include "omega_blackwell_submit.h"
41#include "omega_numeric_native.h"
42#include "omega_gpu_code_alloc.h"
43#endif
44
45/* Recorded after tools/divsqrt_nvdisasm_check.sh matched every word of the
46 * emitted kernel against omega_ds_listing (nvdisasm 13.0.85 -b SM121). */
47const char *const OMEGA_DS_KERNEL_SHA256[OMEGA_DS_OP_COUNT] = {
48 "d847b9bd3ac4fe90405f86a2528ff952d47bdb95679d6d029e52ea7aa526675f", /* DIV, 264 words */
49 "222c9aa5cf7b2d66d48de3ee4b7ce346627e888ab0786713d8986815bdca10d7", /* SQRT, 336 words */
50 "e72d5cbf556dce39c83ce7e76335a7eef7eda3ebccd36f250bdddb272db7474c", /* EXP2, 72 words */
51 "14e461ec628b685ea23d197c060f7d35a302c88e794934628eacd6098a44f394", /* LOG2, 576 words */
52 "8607f5659b4d42a57fdb4ba2f26427a28d8e6e1497f3daa8529ae5da1f629b70", /* SIGMOID, 344 words */
53 "4edc065511340d58d26ed8ef7c1464341b279caccd7abc18b62d2a9880694494", /* TANH, 352 words */
54 "0fe5484675cca0667c95a286b65560f204013683aee4e9ad001dbcf1d78cfc10", /* SIN, 120 words */
55 "ed7cde5ac1dd3ddccc1b78885a2227600cf1bc4a7d775f27cceb110ed37a2ac4", /* COS, 120 words */
56 "db3a51ff06cc24dc749d98dbf91485f0ceb86ba2bfc8cc55e554f62e6f7edff5", /* ERF, 360 words */
57 "94992a5fdbe00a7943dac997d6a59ddd6096ac2fec35d4ff286b6552e8cea36d", /* GELU, 424 words */
58 "adb2864a0842a36fb9abf144d6654a69668506e2f8e98ac1c7ecb40ca4a1ff85", /* RSQRT, 344 words */
59 "b0214f4b0c8416411fdd897d640b45b27da16b92e117f57b02c6abb2f1350cb6", /* EXP, 88 words */
60 "be4f782381c34169670e45e500fd1f0afa0cc7bcfcec9f8e6aca8a41b0029a9e", /* LOG, 312 words */
61};
62
63const char *omega_ds_op_name(OmegaDsOp op) {
64 return op == OMEGA_DS_DIV ? "DIV" : op == OMEGA_DS_SQRT ? "SQRT" : op == OMEGA_DS_EXP2 ? "EXP2" : op == OMEGA_DS_LOG2 ? "LOG2" : op == OMEGA_DS_SIGMOID ? "SIGMOID" : op == OMEGA_DS_TANH ? "TANH" : op == OMEGA_DS_SIN ? "SIN" : op == OMEGA_DS_COS ? "COS" : op == OMEGA_DS_ERF ? "ERF" : op == OMEGA_DS_GELU ? "GELU" : op == OMEGA_DS_RSQRT ? "RSQRT" : op == OMEGA_DS_EXP ? "EXP" : op == OMEGA_DS_LOG ? "LOG" : "?";
65}
66
67/* ---- Forms ---------------------------------------------------------------
68 * Opcode (w0 bits 0-11) and the fixed part of w2 per form. Field positions:
69 * guard predicate w0 bits 12-15 (7 = PT), Rd w0 16-23, Ra w0 24-31, Rb or
70 * the 32-bit immediate in w1 (IADD3 -Rb: w1 bit 31), Rc w2 0-7, IADD3 -Ra
71 * w2 bit 8, LOP3 lut w2 8-15, ISETP cmp w2 12-14, signed w2 bit 9,
72 * Pd w2 17-19, SEL Ps w2 23-25 and !Ps w2 bit 26. Templates come from
73 * ptxas 13.0.88 -O0 oracle output for each class and from single-word probes;
74 * the kernel is accepted only after tools/divsqrt_nvdisasm_check.sh shows that
75 * nvdisasm 13.0.85 -b SM121 decodes every emitted word to the text
76 * omega_ds_format gives for it. */
77typedef struct {
78 uint8_t kind;
79 uint16_t opcode;
80 uint32_t w2_fixed;
81} DsForm;
82
83static const DsForm FORMS[DSK_COUNT] = {
84 { DSK_IADD3_R, 0x210, 0x07ffe000 },
85 { DSK_IADD3_I, 0x810, 0x07ffe000 },
86 { DSK_LOP3_R, 0x212, 0x078e0000 },
87 { DSK_LOP3_I, 0x812, 0x078e0000 },
88 { DSK_SHL_I, 0x819, 0x000006ff },
89 { DSK_SHL_R, 0x219, 0x000006ff },
90 { DSK_SHR_I, 0x819, 0x00011600 },
91 { DSK_SHR_R, 0x219, 0x00011600 },
92 { DSK_ISETP_R, 0x20c, 0x03f00070 },
93 { DSK_ISETP_I, 0x80c, 0x03f00070 },
94 { DSK_SEL_R, 0x207, 0x00000000 },
95 { DSK_SEL_I, 0x807, 0x00000000 },
96 { DSK_I2FP, 0x245, 0x00201400 },
97 /* FP forms: the FADD/FSUB/FMUL/FFMA patch words of src/omega_numeric.c
98 * (nvdisasm 13.0.88 -b SM121, Gate 5 chip parity), registers generalized. */
99 { DSK_FADD_R, 0x221, 0x00000000 },
100 { DSK_FMUL_R, 0x220, 0x00400000 },
101 { DSK_FFMA_R, 0x223, 0x00000000 },
102};
103
104int omega_ds_encode(const OmegaDsInsn *in, uint32_t w[4]) {
105 if (!in || !w || in->kind >= DSK_COUNT) return -1;
106 const DsForm *f = &FORMS[in->kind];
107 uint32_t w0 = f->opcode | 0x7000u, w1 = 0, w2 = f->w2_fixed;
108 switch (in->kind) {
109 case DSK_IADD3_R:
110 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
111 w1 = in->b | (in->negb ? 0x80000000u : 0);
112 w2 |= in->c | (in->nega ? 0x100u : 0);
113 break;
114 case DSK_IADD3_I:
115 if (in->negb) return -1;
116 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
117 w1 = in->imm;
118 w2 |= in->c | (in->nega ? 0x100u : 0);
119 break;
120 case DSK_LOP3_R:
121 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
122 w1 = in->b;
123 w2 |= in->c | (uint32_t)in->lut << 8;
124 break;
125 case DSK_LOP3_I:
126 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
127 w1 = in->imm;
128 w2 |= in->c | (uint32_t)in->lut << 8;
129 break;
130 case DSK_SHL_I:
131 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
132 w1 = in->imm;
133 break;
134 case DSK_SHL_R:
135 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
136 w1 = in->b;
137 break;
138 case DSK_SHR_I:
139 w0 |= (uint32_t)in->d << 16 | 0xffu << 24;
140 w1 = in->imm;
141 w2 |= in->c;
142 break;
143 case DSK_SHR_R:
144 w0 |= (uint32_t)in->d << 16 | 0xffu << 24;
145 w1 = in->b;
146 w2 |= in->c;
147 break;
148 case DSK_ISETP_R:
149 case DSK_ISETP_I:
150 if (in->d > 6 || in->cmp < 1 || in->cmp > 6) return -1;
151 w0 |= (uint32_t)in->a << 24;
152 w1 = in->kind == DSK_ISETP_R ? in->b : in->imm;
153 w2 |= (uint32_t)in->cmp << 12 | (in->is_signed ? 0x200u : 0) | (uint32_t)in->d << 17;
154 break;
155 case DSK_SEL_R:
156 case DSK_SEL_I:
157 if (in->ps > 6) return -1;
158 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
159 w1 = in->kind == DSK_SEL_R ? in->b : in->imm;
160 w2 |= (uint32_t)in->ps << 23 | (in->pneg ? 0x04000000u : 0);
161 break;
162 case DSK_I2FP:
163 w0 |= (uint32_t)in->d << 16;
164 w1 = in->b;
165 break;
166 case DSK_FADD_R:
167 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
168 w1 = in->b | (in->negb ? 0x80000000u : 0);
169 break;
170 case DSK_FMUL_R:
171 if (in->negb) return -1;
172 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
173 w1 = in->b;
174 break;
175 case DSK_FFMA_R:
176 if (in->negb) return -1;
177 w0 |= (uint32_t)in->d << 16 | (uint32_t)in->a << 24;
178 w1 = in->b;
179 w2 |= in->c;
180 break;
181 default:
182 return -1;
183 }
184 w[0] = w0; w[1] = w1; w[2] = w2; w[3] = OMEGA_DS_BODY_CTRL;
185 return 0;
186}
187
188int omega_ds_decode(const uint32_t w[4], OmegaDsInsn *out) {
189 OmegaDsInsn x;
190 memset(&x, 0, sizeof(x));
191 uint32_t opc = w[0] & 0xfffu;
192 int kind = -1;
193 for (int k = 0; k < DSK_COUNT; k++) {
194 if (FORMS[k].opcode != opc) continue;
195 /* SHF: L and R forms share the opcode; the w2 template tells them apart */
196 if ((opc & 0xffu) == 0x19 && (w[2] & 0xffffff00u) != (FORMS[k].w2_fixed & 0xffffff00u)) continue;
197 kind = k;
198 break;
199 }
200 if (kind < 0) return -1;
201 x.kind = (uint8_t)kind;
202 x.d = (uint8_t)(w[0] >> 16);
203 x.a = (uint8_t)(w[0] >> 24);
204 switch (kind) {
205 case DSK_IADD3_R: x.b = (uint8_t)w[1]; x.negb = (w[1] >> 31) & 1; x.c = (uint8_t)w[2]; x.nega = (w[2] >> 8) & 1; break;
206 case DSK_IADD3_I: x.imm = w[1]; x.c = (uint8_t)w[2]; x.nega = (w[2] >> 8) & 1; break;
207 case DSK_LOP3_R: x.b = (uint8_t)w[1]; x.c = (uint8_t)w[2]; x.lut = (uint8_t)(w[2] >> 8); break;
208 case DSK_LOP3_I: x.imm = w[1]; x.c = (uint8_t)w[2]; x.lut = (uint8_t)(w[2] >> 8); break;
209 case DSK_SHL_I: x.imm = w[1]; break;
210 case DSK_SHL_R: x.b = (uint8_t)w[1]; break;
211 case DSK_SHR_I: x.imm = w[1]; x.c = (uint8_t)w[2]; x.a = 0; break;
212 case DSK_SHR_R: x.b = (uint8_t)w[1]; x.c = (uint8_t)w[2]; x.a = 0; break;
213 case DSK_ISETP_R: case DSK_ISETP_I:
214 if (kind == DSK_ISETP_R) x.b = (uint8_t)w[1]; else x.imm = w[1];
215 x.cmp = (uint8_t)((w[2] >> 12) & 7); x.is_signed = (w[2] >> 9) & 1; x.d = (uint8_t)((w[2] >> 17) & 7);
216 break;
217 case DSK_SEL_R: case DSK_SEL_I:
218 if (kind == DSK_SEL_R) x.b = (uint8_t)w[1]; else x.imm = w[1];
219 x.ps = (uint8_t)((w[2] >> 23) & 7); x.pneg = (w[2] >> 26) & 1;
220 break;
221 case DSK_I2FP: x.b = (uint8_t)w[1]; x.a = 0; break;
222 case DSK_FADD_R: x.b = (uint8_t)w[1]; x.negb = (w[1] >> 31) & 1; break;
223 case DSK_FMUL_R: x.b = (uint8_t)w[1]; break;
224 case DSK_FFMA_R: x.b = (uint8_t)w[1]; x.c = (uint8_t)w[2]; break;
225 }
226 uint32_t re[4];
227 if (omega_ds_encode(&x, re) != 0) return -1;
228 if (re[0] != w[0] || re[1] != w[1] || re[2] != w[2]) return -1;
229 if (out) *out = x;
230 return 0;
231}
232
233static void reg_name(uint8_t r, char *b) {
234 if (r == OMEGA_DS_RZ) strcpy(b, "RZ"); else sprintf(b, "R%u", (unsigned)r);
235}
236
237static void simm(uint32_t v, char *b) {
238 if (v & 0x80000000u) sprintf(b, "-0x%x", (unsigned)(0u - v)); else sprintf(b, "0x%x", (unsigned)v);
239}
240
241static const char *cmp_name(uint8_t c) {
242 static const char *const N[8] = { "F", "LT", "EQ", "LE", "GT", "NE", "GE", "T" };
243 return N[c & 7];
244}
245
246int omega_ds_format(const OmegaDsInsn *in, char *buf, size_t len) {
247 char d[8], a[8], b[8], c[8], im[16];
248 if (!in || !buf || in->kind >= DSK_COUNT) return -1;
249 reg_name(in->d, d); reg_name(in->a, a); reg_name(in->b, b); reg_name(in->c, c);
250 switch (in->kind) {
251 case DSK_IADD3_R:
252 snprintf(buf, len, "IADD3 %s, PT, PT, %s%s, %s%s, %s", d, in->nega ? "-" : "", a, in->negb ? "-" : "", b, c); break;
253 case DSK_IADD3_I:
254 simm(in->imm, im);
255 snprintf(buf, len, "IADD3 %s, PT, PT, %s%s, %s, %s", d, in->nega ? "-" : "", a, im, c); break;
256 case DSK_LOP3_R:
257 snprintf(buf, len, "LOP3.LUT %s, %s, %s, %s, 0x%x, !PT", d, a, b, c, in->lut); break;
258 case DSK_LOP3_I:
259 snprintf(buf, len, "LOP3.LUT %s, %s, 0x%x, %s, 0x%x, !PT", d, a, (unsigned)in->imm, c, in->lut); break;
260 case DSK_SHL_I:
261 snprintf(buf, len, "SHF.L.U32 %s, %s, 0x%x, RZ", d, a, (unsigned)in->imm); break;
262 case DSK_SHL_R:
263 snprintf(buf, len, "SHF.L.U32 %s, %s, %s, RZ", d, a, b); break;
264 case DSK_SHR_I:
265 snprintf(buf, len, "SHF.R.U32.HI %s, RZ, 0x%x, %s", d, (unsigned)in->imm, c); break;
266 case DSK_SHR_R:
267 snprintf(buf, len, "SHF.R.U32.HI %s, RZ, %s, %s", d, b, c); break;
268 case DSK_ISETP_R:
269 snprintf(buf, len, "ISETP.%s%s.AND P%u, PT, %s, %s, PT", cmp_name(in->cmp), in->is_signed ? "" : ".U32",
270 (unsigned)in->d, a, b); break;
271 case DSK_ISETP_I:
272 simm(in->imm, im);
273 snprintf(buf, len, "ISETP.%s%s.AND P%u, PT, %s, %s, PT", cmp_name(in->cmp), in->is_signed ? "" : ".U32",
274 (unsigned)in->d, a, im); break;
275 case DSK_SEL_R:
276 snprintf(buf, len, "SEL %s, %s, %s, %sP%u", d, a, b, in->pneg ? "!" : "", (unsigned)in->ps); break;
277 case DSK_SEL_I:
278 snprintf(buf, len, "SEL %s, %s, 0x%x, %sP%u", d, a, (unsigned)in->imm, in->pneg ? "!" : "", (unsigned)in->ps); break;
279 case DSK_I2FP:
280 snprintf(buf, len, "I2FP.F32.S32 %s, %s", d, b); break;
281 case DSK_FADD_R:
282 snprintf(buf, len, "FADD %s, %s, %s%s", d, a, in->negb ? "-" : "", b); break;
283 case DSK_FMUL_R:
284 snprintf(buf, len, "FMUL %s, %s, %s", d, a, b); break;
285 case DSK_FFMA_R:
286 snprintf(buf, len, "FFMA %s, %s, %s, %s", d, a, b, c); break;
287 }
288 return 0;
289}
290
291/* ---- Body builder ---------------------------------------------------------- */
292
293typedef struct { OmegaDsInsn *v; size_t n, max; int bad; } Bld;
294
295static void emit(Bld *B, OmegaDsInsn x) {
296 if (B->n >= B->max) { B->bad = 1; return; }
297 B->v[B->n++] = x;
298}
299#define RZ OMEGA_DS_RZ
300static void iadd(Bld *B, int d, int a, int b, int c) { emit(B, (OmegaDsInsn){ .kind = DSK_IADD3_R, .d = d, .a = a, .b = b, .c = c }); }
301static void isub(Bld *B, int d, int a, int b) { emit(B, (OmegaDsInsn){ .kind = DSK_IADD3_R, .d = d, .a = a, .b = b, .c = RZ, .negb = 1 }); }
302static void iaddi(Bld *B, int d, int a, int32_t imm, int c) { emit(B, (OmegaDsInsn){ .kind = DSK_IADD3_I, .d = d, .a = a, .imm = (uint32_t)imm, .c = c }); }
303static void rsubi(Bld *B, int d, int a, int32_t imm) { emit(B, (OmegaDsInsn){ .kind = DSK_IADD3_I, .d = d, .a = a, .imm = (uint32_t)imm, .c = RZ, .nega = 1 }); }
304static void lop(Bld *B, int d, int a, int b, int c, int lut) { emit(B, (OmegaDsInsn){ .kind = DSK_LOP3_R, .d = d, .a = a, .b = b, .c = c, .lut = lut }); }
305static void lopi(Bld *B, int d, int a, uint32_t imm, int lut) { emit(B, (OmegaDsInsn){ .kind = DSK_LOP3_I, .d = d, .a = a, .imm = imm, .c = RZ, .lut = lut }); }
306static void shli(Bld *B, int d, int a, uint32_t s) { emit(B, (OmegaDsInsn){ .kind = DSK_SHL_I, .d = d, .a = a, .imm = s, .b = 0, .c = RZ }); }
307static void shlr(Bld *B, int d, int a, int s) { emit(B, (OmegaDsInsn){ .kind = DSK_SHL_R, .d = d, .a = a, .b = s, .c = RZ }); }
308static void shri(Bld *B, int d, int c, uint32_t s) { emit(B, (OmegaDsInsn){ .kind = DSK_SHR_I, .d = d, .a = 0, .imm = s, .c = c }); }
309static void shrr(Bld *B, int d, int c, int s) { emit(B, (OmegaDsInsn){ .kind = DSK_SHR_R, .d = d, .a = 0, .b = s, .c = c }); }
310static void setp(Bld *B, int p, int cmp, int sgn, int a, int b) { emit(B, (OmegaDsInsn){ .kind = DSK_ISETP_R, .d = p, .a = a, .b = b, .cmp = cmp, .is_signed = sgn }); }
311static void setpi(Bld *B, int p, int cmp, int sgn, int a, uint32_t imm) { emit(B, (OmegaDsInsn){ .kind = DSK_ISETP_I, .d = p, .a = a, .imm = imm, .cmp = cmp, .is_signed = sgn }); }
312static void sel(Bld *B, int d, int a, int b, int p, int neg) { emit(B, (OmegaDsInsn){ .kind = DSK_SEL_R, .d = d, .a = a, .b = b, .ps = p, .pneg = neg }); }
313static void seli(Bld *B, int d, int a, uint32_t imm, int p, int neg) { emit(B, (OmegaDsInsn){ .kind = DSK_SEL_I, .d = d, .a = a, .imm = imm, .ps = p, .pneg = neg }); }
314static void i2fp(Bld *B, int d, int s) { emit(B, (OmegaDsInsn){ .kind = DSK_I2FP, .d = d, .a = 0, .b = s }); }
315
316#define LUT_AND 0xc0 /* a & b */
317#define LUT_OR 0xfc /* a | b */
318#define LUT_XOR 0x3c /* a ^ b */
319#define LUT_A_AND_B_OR_C 0xe0 /* a & (b | c) */
320
321/* Registers. R2 = a bits and R5 = b bits from the prologue; R6:R7 is the
322 * output address and R1 is never written; the result goes to R9. */
323enum { A_ = 2, B_ = 5, AX = 8, BY = 10, SGN = 11, MA = 12, EA = 13, MB = 14, EB = 15,
324 T0 = 16, T1 = 17, T2 = 18, REM = 19, Q = 20, BIT = 21, EXP2 = 22, PB = 23, BE = 24,
325 S = 25, MANT = 26, T3 = 27, RES = 9 };
326
327/* Finite nonzero |x| (in ax) = m * 2^e, m in [2^23, 2^24) (omega_unpack).
328 * Subnormal: the leading bit of ax comes from I2FP (ax < 2^23, exact). */
329static void unpack(Bld *B, int ax, int m, int e) {
330 shri(B, T0, ax, 23); /* biased exponent */
331 lopi(B, m, ax, 0x007fffffu, LUT_AND);
332 lopi(B, m, m, 0x00800000u, LUT_OR); /* normal: f | 2^23 */
333 iaddi(B, e, T0, -150, RZ); /* normal: be - 150 */
334 i2fp(B, T1, ax);
335 shri(B, T1, T1, 23); /* 127 + msb(ax) */
336 rsubi(B, T2, T1, 150); /* n = 23 - msb */
337 shlr(B, T2, ax, T2); /* subnormal m = ax << n */
338 iaddi(B, T1, T1, -299, RZ); /* subnormal e = -149 - n */
339 setp(B, 0, DS_CMP_EQ, 0, T0, RZ);
340 sel(B, m, T2, m, 0, 0);
341 sel(B, e, T1, e, 0, 0);
342}
343
344/* omega_round_pack(sign, sig, exp2, sticky) for sig in [2^25, 2^27):
345 * result into RES. sticky != 0 means nonzero bits below sig. */
346static void round_pack(Bld *B, int sign, int sig, int exp2, int sticky) {
347 setpi(B, 2, DS_CMP_GE, 0, sig, 0x04000000u);
348 seli(B, PB, RZ, 1, 2, 1); /* p - 25 */
349 iaddi(B, BE, exp2, 152, PB); /* be = p + exp2 + 127 */
350 iaddi(B, T0, PB, 2, RZ); /* normal: s = p - 23 */
351 rsubi(B, T1, exp2, -149); /* subnormal: s = -149 - exp2 */
352 setp(B, 3, DS_CMP_GT, 1, BE, RZ); /* P3: be >= 1 */
353 sel(B, S, T0, T1, 3, 0);
354 iaddi(B, T2, PB, 26, RZ); /* p + 1 */
355 setp(B, 4, DS_CMP_GT, 1, S, T2); /* P4: s > p + 1 -> zero */
356 seli(B, S, S, 2, 4, 1); /* keep shifts in range */
357 shrr(B, MANT, sig, S); /* mant = sig >> s */
358 iaddi(B, T0, S, -1, RZ); /* s - 1 in [1, 27] */
359 shrr(B, T1, sig, T0);
360 lopi(B, T1, T1, 1, LUT_AND); /* guard */
361 rsubi(B, T2, T0, 32);
362 shlr(B, T2, sig, T2); /* bits below the guard, on top */
363 lop(B, T2, T2, sticky, RZ, LUT_OR);
364 setp(B, 5, DS_CMP_NE, 0, T2, RZ);
365 seli(B, T2, RZ, 1, 5, 1); /* rest */
366 lop(B, T1, T1, T2, MANT, LUT_A_AND_B_OR_C); /* guard & (rest | lsb) */
367 iadd(B, MANT, MANT, T1, RZ);
368 iaddi(B, T3, BE, -1, RZ);
369 shli(B, T3, T3, 23);
370 sel(B, T3, T3, RZ, 3, 0); /* normal: (be - 1) << 23 */
371 iadd(B, T3, T3, MANT, RZ);
372 setpi(B, 6, DS_CMP_GE, 0, T3, 0x7f800000u);
373 seli(B, T3, T3, 0x7f800000u, 6, 1); /* rounded up to infinity */
374 setpi(B, 6, DS_CMP_GT, 1, BE, 254);
375 seli(B, T3, T3, 0x7f800000u, 6, 1); /* be >= 255 */
376 sel(B, T3, RZ, T3, 4, 0); /* below half the min subnormal */
377 lop(B, RES, T3, sign, RZ, LUT_OR);
378}
379
380/* Correctly rounded ra / rb into RES (omega_math_div). Reads ra and rb only
381 * in its first three instructions; writes R8, R9 (RES), R10-R27 and P0-P6. */
382static void div_core(Bld *B, int ra, int rb) {
383 lopi(B, AX, ra, 0x7fffffffu, LUT_AND);
384 lopi(B, BY, rb, 0x7fffffffu, LUT_AND);
385 lop(B, SGN, ra, rb, RZ, LUT_XOR);
386 lopi(B, SGN, SGN, 0x80000000u, LUT_AND);
387 unpack(B, AX, MA, EA);
388 unpack(B, BY, MB, EB);
389 /* restoring long division, 27 quotient bits (omega_math_div) */
390 for (int i = 0; i < 27; i++) {
391 isub(B, T0, MA, MB);
392 setp(B, 1, DS_CMP_GE, 0, MA, MB);
393 sel(B, MA, T0, MA, 1, 0);
394 seli(B, BIT, RZ, 1, 1, 1);
395 if (i == 0) iadd(B, Q, RZ, RZ, BIT); else iadd(B, Q, Q, Q, BIT);
396 iadd(B, MA, MA, MA, RZ);
397 }
398 isub(B, T0, EA, EB);
399 iaddi(B, EXP2, T0, -26, RZ);
400 round_pack(B, SGN, Q, EXP2, MA);
401 /* special operands, lowest priority first (omega_math_div order) */
402 setp(B, 0, DS_CMP_EQ, 0, AX, RZ);
403 sel(B, RES, SGN, RES, 0, 0); /* 0 / y */
404 setpi(B, 0, DS_CMP_EQ, 0, BY, 0x7f800000u);
405 sel(B, RES, SGN, RES, 0, 0); /* x / inf */
406 lopi(B, T0, SGN, 0x7f800000u, LUT_OR);
407 setp(B, 0, DS_CMP_EQ, 0, BY, RZ);
408 sel(B, RES, T0, RES, 0, 0); /* x / 0 */
409 setpi(B, 0, DS_CMP_EQ, 0, AX, 0x7f800000u);
410 sel(B, RES, T0, RES, 0, 0); /* inf / y */
411 lop(B, T0, AX, BY, RZ, LUT_OR);
412 setp(B, 0, DS_CMP_EQ, 0, T0, RZ);
413 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* 0 / 0 */
414 lop(B, T0, AX, BY, RZ, LUT_AND);
415 setpi(B, 0, DS_CMP_EQ, 0, T0, 0x7f800000u);
416 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* inf / inf (or NaN) */
417 setpi(B, 0, DS_CMP_GT, 0, AX, 0x7f800000u);
418 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN / y */
419 setpi(B, 0, DS_CMP_GT, 0, BY, 0x7f800000u);
420 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* x / NaN */
421}
422
423static void body_div(Bld *B) { div_core(B, A_, B_); }
424
425/* ---- Transcendentals (E1 row 10) -------------------------------------------
426 * Each body issues the frozen sequence of src/omega_numeric_transc.c in its
427 * written order. FP constants are loaded with IADD3 Rd, RZ, imm, RZ from
428 * their exact binary32 bit patterns (written below as the same hex literals
429 * the CPU source uses). fneg(v) is LOP3 v ^ 0x80000000 as on the CPU. */
430static void ffadd(Bld *B, int d, int a, int b) { emit(B, (OmegaDsInsn){ .kind = DSK_FADD_R, .d = d, .a = a, .b = b }); }
431static void ffsub(Bld *B, int d, int a, int b) { emit(B, (OmegaDsInsn){ .kind = DSK_FADD_R, .d = d, .a = a, .b = b, .negb = 1 }); }
432static void ffmul(Bld *B, int d, int a, int b) { emit(B, (OmegaDsInsn){ .kind = DSK_FMUL_R, .d = d, .a = a, .b = b }); }
433static void fffma(Bld *B, int d, int a, int b, int c) { emit(B, (OmegaDsInsn){ .kind = DSK_FFMA_R, .d = d, .a = a, .b = b, .c = c }); }
434static void movi(Bld *B, int d, uint32_t imm) { iaddi(B, d, RZ, (int32_t)imm, RZ); }
435static void movf(Bld *B, int d, float v) { movi(B, d, omega_float_to_bits(v)); }
436static void mov(Bld *B, int d, int s) { iadd(B, d, s, RZ, RZ); }
437/* p = ffma(p, f, c) with c loaded into tmp first. */
438static void horner(Bld *B, int p, int f, float c, int tmp) { movf(B, tmp, c); fffma(B, p, p, f, tmp); }
439
440/* scale2(m, k) of omega_numeric_transc.c into out: all three branches are
441 * computed, then chosen (k > 127 first, else k < -126, else the plain one).
442 * Writes t, q, ra, rb, rc and P1. */
443static void scale2_core(Bld *B, int out, int m, int k, int t, int q, int ra, int rb, int rc) {
444 iaddi(B, t, k, 127, RZ);
445 shli(B, t, t, 23); /* pow2i(k) */
446 ffmul(B, rc, m, t);
447 movi(B, t, 0x7f000000u); /* pow2i(127) */
448 ffmul(B, q, m, t);
449 shli(B, t, k, 23); /* pow2i(k - 127) */
450 ffmul(B, ra, q, t);
451 iaddi(B, t, k, 253, RZ);
452 shli(B, t, t, 23); /* pow2i(k + 126) */
453 ffmul(B, q, m, t);
454 movi(B, t, 0x00800000u); /* pow2i(-126) */
455 ffmul(B, rb, q, t);
456 setpi(B, 1, DS_CMP_LT, 1, k, (uint32_t)-126);
457 sel(B, out, rb, rc, 1, 0);
458 setpi(B, 1, DS_CMP_GT, 1, k, 127);
459 sel(B, out, ra, out, 1, 0);
460}
461
462/* omega_math_exp2. x in R2. */
463static void body_exp2(Bld *B) {
464 enum { X = 2, AX_ = 8, C = 10, T = 11, KF = 12, K = 13, F = 14, P = 15, M = 16,
465 U = 17, Q2 = 18, RA = 19, RB = 20, RC = 21, T4 = 22 };
466 movi(B, C, 0x4b400000u); /* MAGIC = 1.5 * 2^23 */
467 ffadd(B, T, X, C); /* t = x + MAGIC */
468 ffsub(B, KF, T, C); /* kf = t - MAGIC */
469 isub(B, K, T, C); /* k = tb(t) - tb(MAGIC) */
470 ffsub(B, F, X, KF); /* f = x - kf */
471 movf(B, P, 0xB16011p-43f);
472 horner(B, P, F, 0xFFE5FEp-40f, C);
473 horner(B, P, F, 0xA18489p-36f, C);
474 horner(B, P, F, 0xAEC3FFp-33f, C);
475 horner(B, P, F, 0x9D955Bp-30f, C);
476 horner(B, P, F, 0xE35847p-28f, C);
477 horner(B, P, F, 0xF5FDF0p-26f, C);
478 horner(B, P, F, 0xB17218p-24f, C);
479 movf(B, C, 1.0f);
480 fffma(B, M, P, F, C); /* m = ffma(p, f, 1) */
481 scale2_core(B, RES, M, K, U, Q2, RA, RB, RC);
482 /* special inputs, lowest priority first */
483 lopi(B, AX_, X, 0x7fffffffu, LUT_AND);
484 setpi(B, 0, DS_CMP_GT, 0, AX_, 0x43170000u); /* |x| > 151 */
485 setpi(B, 2, DS_CMP_LT, 1, X, 0); /* sign bit set */
486 sel(B, T4, RZ, RES, 0, 0);
487 sel(B, RES, T4, RES, 2, 0); /* x < -151: +0 */
488 setpi(B, 0, DS_CMP_GE, 1, X, 0x43000000u);
489 seli(B, RES, RES, 0x7f800000u, 0, 1); /* x >= 128: +inf */
490 setpi(B, 0, DS_CMP_GT, 0, AX_, 0x7f800000u);
491 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
492}
493
494/* omega_math_log2. x in R2, kept in R3; R0, R4, R5, R28-R31 hold the values
495 * that live across the two divisions (div_core writes R8-R27). */
496static void body_log2(Bld *B) {
497 enum { X = 3, Z = 0, NZ = 4, RZL = 5, NUM = 28, DEN = 29, DL = 30, E = 31, ZL = 2 };
498 mov(B, X, A_);
499 /* subnormal: x * 2^23 (exact), eadj = -23 */
500 movi(B, 10, 0x4b000000u);
501 ffmul(B, 11, A_, 10);
502 lopi(B, 12, A_, 0x7f800000u, LUT_AND);
503 setp(B, 0, DS_CMP_EQ, 0, 12, RZ);
504 sel(B, 13, 11, A_, 0, 0); /* u */
505 seli(B, 14, RZ, (uint32_t)-23, 0, 1); /* eadj */
506 shri(B, 15, 13, 23);
507 lopi(B, 15, 15, 0xffu, LUT_AND);
508 iaddi(B, E, 15, -127, 14); /* e */
509 lopi(B, 16, 13, 0x007fffffu, LUT_AND);
510 lopi(B, 16, 16, 0x3f800000u, LUT_OR); /* m in [1, 2) */
511 setpi(B, 1, DS_CMP_GT, 0, 16, omega_float_to_bits(0xB504F3p-23f));
512 movf(B, 17, 0.5f);
513 ffmul(B, 18, 16, 17);
514 sel(B, 16, 18, 16, 1, 0); /* m > sqrt2: m * 0.5 */
515 seli(B, 19, RZ, 1, 1, 1);
516 iadd(B, E, E, 19, RZ); /* e + 1 */
517 movf(B, 20, 1.0f);
518 ffsub(B, NUM, 16, 20); /* num = m - 1 */
519 ffadd(B, DEN, 16, 20); /* two_sum(m, 1): den */
520 ffsub(B, 21, DEN, 16); /* bv */
521 ffsub(B, 22, DEN, 21); /* av */
522 ffsub(B, 23, 16, 22); /* m - av */
523 ffsub(B, 24, 20, 21); /* 1 - bv */
524 ffadd(B, DL, 23, 24); /* dl */
525 div_core(B, NUM, DEN); /* z = num / den */
526 mov(B, Z, RES);
527 lopi(B, NZ, Z, 0x80000000u, LUT_XOR); /* -z */
528 fffma(B, RZL, NZ, DEN, NUM); /* rz = -z den + num */
529 fffma(B, RZL, NZ, DL, RZL); /* rz = -z dl + rz */
530 div_core(B, RZL, DEN); /* zl = rz / den */
531 mov(B, ZL, RES);
532 movf(B, 10, 0xB8AA3Bp-22f); /* C_HI = 2/ln2 */
533 ffmul(B, 11, Z, Z); /* z2 */
534 movf(B, 12, 0x864D42p-25f);
535 horner(B, 12, 11, 0xA4258Ap-25f, 13);
536 horner(B, 12, 11, 0xD30BB1p-25f, 13);
537 horner(B, 12, 11, 0x93BB63p-24f, 13);
538 horner(B, 12, 11, 0xF6384Fp-24f, 13); /* p */
539 ffmul(B, 14, Z, 11); /* z3 = z * z2 */
540 ffmul(B, 15, Z, 10); /* ph = z * C_HI */
541 lopi(B, 16, 15, 0x80000000u, LUT_XOR);
542 fffma(B, 17, Z, 10, 16); /* pl = z C_HI - ph */
543 ffmul(B, 18, ZL, 10); /* zl * C_HI */
544 movf(B, 19, 0xA57060p-48f); /* C_LO */
545 fffma(B, 18, Z, 19, 18); /* z C_LO + zl C_HI */
546 fffma(B, 20, 14, 12, 18); /* lo = z3 p + ... */
547 i2fp(B, 21, E); /* (float)e, exact */
548 ffadd(B, 22, 21, 15); /* two_sum(e, ph): s */
549 ffsub(B, 23, 22, 21); /* bv */
550 ffsub(B, 24, 22, 23); /* av */
551 ffsub(B, 25, 21, 24); /* e - av */
552 ffsub(B, 26, 15, 23); /* ph - bv */
553 ffadd(B, 27, 25, 26); /* se */
554 ffadd(B, 25, 17, 20); /* pl + lo */
555 ffadd(B, 26, 27, 25); /* se + (pl + lo) */
556 ffadd(B, RES, 22, 26); /* s + (...) */
557 /* special inputs, lowest priority first (omega_math_log2 order) */
558 lopi(B, 8, X, 0x7fffffffu, LUT_AND);
559 setpi(B, 0, DS_CMP_EQ, 0, X, 0x7f800000u);
560 seli(B, RES, RES, 0x7f800000u, 0, 1); /* +inf */
561 setpi(B, 0, DS_CMP_LT, 1, X, 0);
562 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* x < 0, incl. -inf */
563 setp(B, 0, DS_CMP_EQ, 0, 8, RZ);
564 seli(B, RES, RES, 0xff800000u, 0, 1); /* +-0: -inf */
565 setpi(B, 0, DS_CMP_GT, 0, 8, 0x7f800000u);
566 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
567}
568
569/* E1 row 10, natural EXP and LOG: omega_math_exp / omega_math_log of
570 * src/omega_numeric.c. That file is built with -ffp-contract=off, so every C
571 * operator is one unfused binary32 operation: the bodies issue FMUL, FADD and
572 * FSUB (never FFMA) in the order the C writes them. OMEGA_DS_MUTATE=1 or 2
573 * (test builds only) moves the EXP c2 or the LOG c1 coefficient up one ulp,
574 * a mutant the host tier must catch. */
575#ifndef OMEGA_DS_MUTATE
576#define OMEGA_DS_MUTATE 0
577#endif
578static void movc(Bld *B, int d, float v, int mutant) {
579 movi(B, d, omega_float_to_bits(v) + (OMEGA_DS_MUTATE == mutant ? 1u : 0u));
580}
581
582/* omega_math_exp. x in R2. (int32_t)(prod +- 0.5f) truncates toward zero; the
583 * body truncates t (|t| >= 0.5) from its bits: |t| = mant * 2^(be - 150), so
584 * trunc|t| = mant >> (150 - be), exact, with the shift clamped to 31
585 * (mant < 2^24: every shift >= 24, i.e. |t| < 1, gives 0). The CPU picks
586 * prod + 0.5f when prod >= 0; the body picks it when the sign bit of prod is
587 * clear. They differ only at prod = -0, where both give k = 0. */
588static void body_exp(Bld *B) {
589 enum { X = 2, C = 8, PROD = 10, TP = 11, TM = 12, T = 13, BX = 14, MN = 15, SH = 16, V = 17, NV = 18,
590 K = 19, FK = 20, R = 21, U = 22, P = 23, PO = 24, Q = 25, RA = 26, RB = 27, RC = 28 };
591 movf(B, C, 1.4426950408889634f); /* INV_LN2 */
592 ffmul(B, PROD, X, C); /* prod = x * INV_LN2 */
593 movf(B, C, 0.5f);
594 ffadd(B, TP, PROD, C); /* prod + 0.5f */
595 ffsub(B, TM, PROD, C); /* prod - 0.5f */
596 setpi(B, 0, DS_CMP_LT, 1, PROD, 0); /* sign bit of prod */
597 sel(B, T, TM, TP, 0, 0); /* t */
598 shri(B, BX, T, 23);
599 lopi(B, BX, BX, 0xffu, LUT_AND); /* biased exponent of t */
600 lopi(B, MN, T, 0x007fffffu, LUT_AND);
601 lopi(B, MN, MN, 0x00800000u, LUT_OR); /* significand of t */
602 rsubi(B, SH, BX, 150); /* 150 - be */
603 setpi(B, 1, DS_CMP_GT, 0, SH, 31);
604 seli(B, SH, SH, 31u, 1, 1); /* clamp the shift to 31 */
605 shrr(B, V, MN, SH); /* trunc |t| */
606 rsubi(B, NV, V, 0);
607 sel(B, K, NV, V, 0, 0); /* k = (int32_t)t */
608 i2fp(B, FK, K); /* fk = (float)k, exact */
609 movf(B, C, 0.693145751953125f); /* LN2_HI */
610 ffmul(B, U, FK, C);
611 ffsub(B, R, X, U); /* x - fk * LN2_HI */
612 movf(B, C, 1.4286068203094172e-06f); /* LN2_LO */
613 ffmul(B, U, FK, C);
614 ffsub(B, R, R, U); /* r = (...) - fk * LN2_LO */
615 movf(B, C, 0.0083333338f); /* c5 */
616 ffmul(B, U, R, C); /* r * c5 */
617 movf(B, C, 0.0416666679f); /* c4 */
618 ffadd(B, U, C, U);
619 ffmul(B, P, R, U); /* p = r * (c4 + r * c5) */
620 movf(B, C, 0.1666666716f); /* c3 */
621 ffadd(B, U, C, P);
622 ffmul(B, P, R, U); /* p = r * (c3 + p) */
623 movc(B, C, 0.5f, 1); /* c2 */
624 ffadd(B, U, C, P);
625 ffmul(B, P, R, U); /* p = r * (c2 + p) */
626 movf(B, C, 1.0f);
627 ffadd(B, U, C, P); /* 1 + p */
628 ffmul(B, U, R, U); /* r * (1 + p) */
629 ffadd(B, PO, C, U); /* poly = 1 + r * (1 + p) */
630 /* scale by 2^k: all three branches, then chosen */
631 iaddi(B, U, K, 127, RZ);
632 shli(B, U, U, 23);
633 ffmul(B, RC, PO, U); /* poly * 2^k */
634 movi(B, U, 254u << 23);
635 ffmul(B, Q, PO, U); /* poly * 2^127 */
636 shli(B, U, K, 23);
637 ffmul(B, RA, Q, U); /* * bits((k - 127 + 127) << 23) */
638 iaddi(B, U, K, 227, RZ);
639 shli(B, U, U, 23);
640 ffmul(B, Q, PO, U); /* poly * 2^(k + 100) */
641 movi(B, U, 27u << 23);
642 ffmul(B, RB, Q, U); /* * 2^-100 */
643 setpi(B, 1, DS_CMP_LT, 1, K, (uint32_t)-126);
644 sel(B, RES, RB, RC, 1, 0);
645 setpi(B, 1, DS_CMP_GT, 1, K, 127);
646 sel(B, RES, RA, RES, 1, 0);
647 /* special inputs, lowest priority first (x is not NaN for the first two) */
648 setpi(B, 0, DS_CMP_GT, 0, X, 0xc2d00000u);
649 sel(B, RES, RZ, RES, 0, 0); /* x < -104: +0 */
650 setpi(B, 0, DS_CMP_GT, 1, X, omega_float_to_bits(88.722839f));
651 seli(B, RES, RES, 0x7f800000u, 0, 1); /* x > 88.722839: +inf */
652 lopi(B, U, X, 0x7fffffffu, LUT_AND);
653 setpi(B, 0, DS_CMP_GT, 0, U, 0x7f800000u);
654 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
655}
656
657/* omega_math_log. x in R2, kept in R3; e lives in R31 across the division
658 * (div_core writes R8-R27). z = omega_math_div(m - 1, m + 1) is the
659 * correctly rounded DIV body of E1 row 7. */
660static void body_log(Bld *B) {
661 enum { X = 3, Z = 0, NUM = 28, DEN = 29, E = 31 };
662 mov(B, X, A_);
663 /* subnormal: x * 2^23 (exact), e_adjust = -23 */
664 movi(B, 10, 0x4b000000u);
665 ffmul(B, 11, A_, 10);
666 lopi(B, 12, A_, 0x7f800000u, LUT_AND);
667 setp(B, 0, DS_CMP_EQ, 0, 12, RZ);
668 sel(B, 13, 11, A_, 0, 0); /* u */
669 seli(B, 14, RZ, (uint32_t)-23, 0, 1); /* e_adjust */
670 shri(B, 15, 13, 23);
671 lopi(B, 15, 15, 0xffu, LUT_AND);
672 iaddi(B, E, 15, -127, 14); /* e */
673 lopi(B, 16, 13, 0x007fffffu, LUT_AND);
674 lopi(B, 16, 16, 0x3f800000u, LUT_OR); /* m in [1, 2) */
675 setpi(B, 1, DS_CMP_GT, 0, 16, omega_float_to_bits(1.41421356f));
676 movf(B, 17, 0.5f);
677 ffmul(B, 18, 16, 17);
678 sel(B, 16, 18, 16, 1, 0); /* m > 1.41421356f: m * 0.5 */
679 seli(B, 19, RZ, 1, 1, 1);
680 iadd(B, E, E, 19, RZ); /* e + 1 */
681 movf(B, 20, 1.0f);
682 ffsub(B, NUM, 16, 20); /* m - 1 */
683 ffadd(B, DEN, 16, 20); /* m + 1 */
684 div_core(B, NUM, DEN); /* z = div(m - 1, m + 1) */
685 mov(B, Z, RES);
686 ffmul(B, 10, Z, Z); /* z2 */
687 movf(B, 11, 0.142857149f); /* c3 */
688 ffmul(B, 12, 10, 11); /* z2 * c3 */
689 movf(B, 11, 0.200000003f); /* c2 */
690 ffadd(B, 12, 11, 12);
691 ffmul(B, 12, 10, 12); /* z2 * (c2 + ...) */
692 movc(B, 11, 0.333333343f, 2); /* c1 */
693 ffadd(B, 12, 11, 12);
694 ffmul(B, 12, 10, 12); /* z2 * (c1 + ...) */
695 movf(B, 11, 1.0f);
696 ffadd(B, 13, 11, 12); /* poly */
697 movf(B, 11, 2.0f);
698 ffmul(B, 14, 11, Z); /* 2 * z */
699 ffmul(B, 15, 14, 13); /* ln_m = 2 * z * poly */
700 i2fp(B, 16, E); /* (float)e, exact */
701 movf(B, 11, 0.6931471805599453f); /* LN2 */
702 ffmul(B, 17, 16, 11);
703 ffadd(B, RES, 17, 15); /* e * LN2 + ln_m */
704 /* special inputs, lowest priority first (omega_math_log order) */
705 lopi(B, 8, X, 0x7fffffffu, LUT_AND);
706 setpi(B, 0, DS_CMP_EQ, 0, X, 0x7f800000u);
707 seli(B, RES, RES, 0x7f800000u, 0, 1); /* +inf */
708 setpi(B, 0, DS_CMP_LT, 1, X, 0);
709 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* sign bit set */
710 setp(B, 0, DS_CMP_EQ, 0, 8, RZ);
711 seli(B, RES, RES, 0xff800000u, 0, 1); /* +-0: -inf (-0 is not < 0) */
712 setpi(B, 0, DS_CMP_GT, 0, 8, 0x7f800000u);
713 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
714}
715
716
717/* exp_core(ah, +0) of omega_numeric_transc.c: e^ah = m * 2^k (Cody-Waite,
718 * degree-8 Horner). ah must not be one of R10-R17 (read after they are
719 * written).
720 * Writes m, k and R10-R17. */
721static void exp_core_body(Bld *B, int ah, int m, int k) {
722 enum { C = 10, T = 11, KF = 12, NKF = 13, R = 14, P = 15, MG = 16, ZERO = 17 };
723 movf(B, C, 0xB8AA3Bp-23f); /* INVLN2 */
724 ffmul(B, T, ah, C);
725 movi(B, MG, 0x4b400000u); /* MAGIC */
726 ffadd(B, T, T, MG); /* t = ah INVLN2 + MAGIC */
727 ffsub(B, KF, T, MG); /* kf = t - MAGIC */
728 isub(B, k, T, MG); /* k = tb(t) - tb(MAGIC) */
729 lopi(B, NKF, KF, 0x80000000u, LUT_XOR); /* -kf */
730 movf(B, C, 0xB17218p-24f); /* LN2_HI */
731 fffma(B, R, NKF, C, ah); /* r = -kf LN2_HI + ah */
732 movf(B, C, -0x82E308p-52f); /* LN2_LO */
733 fffma(B, R, NKF, C, R); /* r = -kf LN2_LO + r */
734 movi(B, ZERO, 0);
735 ffadd(B, R, R, ZERO); /* r = r + al (al = +0) */
736 movf(B, P, 0xD00D01p-39f); /* 1/8! */
737 horner(B, P, R, 0xD00D01p-36f, C);
738 horner(B, P, R, 0xB60B61p-33f, C);
739 horner(B, P, R, 0x888889p-30f, C);
740 horner(B, P, R, 0xAAAAABp-28f, C);
741 horner(B, P, R, 0xAAAAABp-26f, C);
742 horner(B, P, R, 0.5f, C);
743 horner(B, P, R, 1.0f, C);
744 movf(B, C, 1.0f);
745 fffma(B, m, P, R, C); /* m = ffma(p, r, 1) */
746}
747
748/* omega_math_sigmoid. x in R2 (never written). m, k, t, d and 1.0 live
749 * across the division in R28-R31, R0, R3, R4. */
750static void body_sigmoid(Bld *B) {
751 enum { X = 2, M = 28, K = 29, TT = 30, ONE = 31, D = 0, Q = 3, NUMR = 4 };
752 lopi(B, 10, X, 0x80000000u, LUT_OR); /* fneg(fabs(x)) */
753 mov(B, 18, 10);
754 exp_core_body(B, 18, M, K);
755 scale2_core(B, TT, M, K, 10, 11, 12, 13, 14); /* t = scale2(m, k) */
756 movf(B, ONE, 1.0f);
757 ffadd(B, D, ONE, TT); /* d = 1 + t */
758 setpi(B, 0, DS_CMP_LT, 1, X, 0); /* sign bit set */
759 sel(B, NUMR, M, ONE, 0, 0); /* x < 0: m, else 1 */
760 div_core(B, NUMR, D); /* q = num / d */
761 mov(B, Q, RES);
762 scale2_core(B, NUMR, Q, K, 10, 11, 12, 13, 14);/* x < 0: scale2(q, k) */
763 setpi(B, 0, DS_CMP_LT, 1, X, 0);
764 sel(B, RES, NUMR, Q, 0, 0);
765 /* special inputs, lowest priority first */
766 lopi(B, AX, X, 0x7fffffffu, LUT_AND);
767 setpi(B, 0, DS_CMP_GE, 0, AX, 0x42d00000u); /* |x| >= 104 */
768 setpi(B, 2, DS_CMP_LT, 1, X, 0); /* sign bit set */
769 sel(B, 12, RZ, RES, 0, 0);
770 sel(B, RES, 12, RES, 2, 0); /* x <= -104: +0 */
771 setpi(B, 0, DS_CMP_GE, 1, X, 0x41900000u);
772 seli(B, RES, RES, 0x3f800000u, 0, 1); /* x >= 18: 1 */
773 setpi(B, 0, DS_CMP_GT, 0, AX, 0x7f800000u);
774 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
775}
776
777/* omega_math_tanh. x in R2 (never written); sign, |x| and the series
778 * result live across the division in R0, R3, R4. */
779static void body_tanh(Bld *B) {
780 enum { X = 2, SG = 0, AXX = 3, RP = 4, M = 28, K = 29, DEN = 30, TWO = 31 };
781 lopi(B, SG, X, 0x80000000u, LUT_AND);
782 lopi(B, AXX, X, 0x7fffffffu, LUT_AND);
783 /* |x| < 0.5625: odd Taylor series */
784 ffmul(B, 10, AXX, AXX); /* x2 */
785 movf(B, 11, 0xCB3F0Cp-37f);
786 horner(B, 11, 10, -0xFABEBCp-36f, 12);
787 horner(B, 11, 10, 0x9AAC12p-34f, 12);
788 horner(B, 11, 10, -0xBED1B2p-33f, 12);
789 horner(B, 11, 10, 0xEB69E8p-32f, 12);
790 horner(B, 11, 10, -0x91371Bp-30f, 12);
791 horner(B, 11, 10, 0xB327A4p-29f, 12);
792 horner(B, 11, 10, -0xDD0DD1p-28f, 12);
793 horner(B, 11, 10, 0x888889p-26f, 12);
794 horner(B, 11, 10, -0xAAAAABp-25f, 12);
795 ffmul(B, 13, AXX, 10); /* ax x2 */
796 fffma(B, RP, 13, 11, AXX); /* r = ax x2 p + ax */
797 /* otherwise: 1 - 2/(e^(2|x|) + 1) */
798 ffadd(B, 18, AXX, AXX);
799 exp_core_body(B, 18, M, K);
800 scale2_core(B, 19, M, K, 10, 11, 12, 13, 14); /* e2 */
801 movf(B, 10, 1.0f);
802 ffadd(B, DEN, 19, 10); /* e2 + 1 */
803 movf(B, TWO, 2.0f);
804 div_core(B, TWO, DEN);
805 movf(B, 10, 1.0f);
806 ffsub(B, 11, 10, RES); /* 1 - 2/(e2+1) */
807 setpi(B, 0, DS_CMP_LT, 0, AXX, 0x3f100000u); /* |x| < 0.5625 */
808 sel(B, 11, RP, 11, 0, 0);
809 setpi(B, 0, DS_CMP_GE, 0, AXX, 0x41180000u); /* |x| >= 9.5: 1 */
810 seli(B, 11, 11, 0x3f800000u, 0, 1);
811 lop(B, RES, 11, SG, RZ, LUT_OR); /* r | sign */
812 setpi(B, 0, DS_CMP_GT, 0, AXX, 0x7f800000u);
813 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
814}
815static void body_sqrt(Bld *B) {
816 lopi(B, AX, A_, 0x7fffffffu, LUT_AND);
817 unpack(B, AX, MA, EA);
818 lopi(B, T0, EA, 1, LUT_AND);
819 shlr(B, MB, MA, T0); /* mm: exponent made even */
820 isub(B, EA, EA, T0);
821 /* digit-by-digit square root of M = mm * 2^28 (27 bit pairs, the first
822 * 13 from mm as a 26-bit number, then 14 zero pairs): s = floor(sqrt M),
823 * REM = M - s^2 < 2^28. */
824 for (int i = 0; i < 27; i++) {
825 if (i < 13) {
826 shri(B, T0, MB, (uint32_t)(24 - 2 * i));
827 lopi(B, T0, T0, 3, LUT_AND);
828 if (i == 0) iadd(B, REM, T0, RZ, RZ);
829 else { shli(B, REM, REM, 2); iadd(B, REM, REM, T0, RZ); }
830 } else {
831 shli(B, REM, REM, 2);
832 }
833 if (i == 0) iaddi(B, T1, RZ, 1, RZ);
834 else { shli(B, T1, S, 2); lopi(B, T1, T1, 1, LUT_OR); } /* 4s + 1 */
835 setp(B, 1, DS_CMP_GE, 0, REM, T1);
836 isub(B, T2, REM, T1);
837 sel(B, REM, T2, REM, 1, 0);
838 seli(B, BIT, RZ, 1, 1, 1);
839 if (i == 0) iadd(B, S, RZ, RZ, BIT); else iadd(B, S, S, S, BIT);
840 }
841 /* exp2 = e / 2 - 14 (e even, >= -172) */
842 iaddi(B, T0, EA, 512, RZ);
843 shri(B, T0, T0, 1);
844 iaddi(B, EXP2, T0, -270, RZ);
845 /* round_pack writes T0..T3, PB, BE, MANT; sig = S, sticky = REM */
846 {
847 /* S is overwritten inside round_pack (shift amount); copy sig first */
848 iadd(B, Q, S, RZ, RZ);
849 round_pack(B, RZ, Q, EXP2, REM);
850 }
851 setpi(B, 0, DS_CMP_EQ, 0, A_, 0x7f800000u);
852 seli(B, RES, RES, 0x7f800000u, 0, 1); /* +inf */
853 setpi(B, 0, DS_CMP_GT, 0, A_, 0x7f800000u);
854 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN, x < 0 */
855 setp(B, 0, DS_CMP_EQ, 0, AX, RZ);
856 sel(B, RES, A_, RES, 0, 0); /* +-0 */
857}
858
859
860/* ---- SIN / COS (E1 row 10) -------------------------------------------------
861 * sincos_core of omega_numeric_transc.c: the reduction x = k pi/2 + (rh + rl) is
862 * computed for every input, the |x| <= pi/4 branch is chosen with a predicate,
863 * both polynomials run, and the quadrant picks one and its sign. x in R2. */
864static void two_sum_b(Bld *B, int s, int e, int a, int b, int t1, int t2) {
865 ffadd(B, s, a, b); /* x = a + b */
866 ffsub(B, t1, s, a); /* bv = x - a */
867 ffsub(B, t2, s, t1); /* av = x - bv */
868 ffsub(B, t2, a, t2); /* a - av */
869 ffsub(B, t1, b, t1); /* b - bv */
870 ffadd(B, e, t2, t1);
871}
872
873static void body_sincos(Bld *B, int qoff) {
874 enum { X = 2, AX_ = 3, C = 4, T = 10, KF = 11, K = 12, NKF = 13, R1 = 14, TH = 15, TL = 16,
875 S1 = 17, E1 = 18, S2 = 19, E2 = 20, LO = 21, RH = 22, RL = 23, U = 24, V = 25,
876 MG = 26, Q = 27, R2 = 28, SP = 29, CP = 30, W = 31, HR = 32, EH = 33, R2L = 34, NR = 35 };
877 lopi(B, AX_, X, 0x7fffffffu, LUT_AND);
878 /* reduction (computed for every input; used when |x| > pi/4) */
879 movf(B, C, 0xA2F983p-24f); /* 2/pi */
880 ffmul(B, T, X, C);
881 movi(B, MG, 0x4b400000u); /* MAGIC */
882 ffadd(B, T, T, MG);
883 ffsub(B, KF, T, MG); /* kf */
884 isub(B, K, T, MG); /* k */
885 lopi(B, NKF, KF, 0x80000000u, LUT_XOR); /* -kf */
886 movf(B, C, 0xC90FDBp-23f); /* P1 */
887 fffma(B, R1, NKF, C, X); /* r1 = -kf P1 + x */
888 movf(B, C, -0xBBBD2Ep-48f); /* P2 */
889 ffmul(B, TH, KF, C); /* th = kf P2 */
890 lopi(B, U, TH, 0x80000000u, LUT_XOR); /* -th */
891 fffma(B, TL, KF, C, U); /* tl = kf P2 - th */
892 two_sum_b(B, S1, E1, R1, U, V, W); /* (s1, e1) = r1 + (-th) */
893 lopi(B, U, TL, 0x80000000u, LUT_XOR); /* -tl */
894 two_sum_b(B, S2, E2, S1, U, V, W); /* (s2, e2) = s1 + (-tl) */
895 movf(B, C, -0xF72CEDp-73f); /* P3 */
896 ffmul(B, T, KF, C); /* kf P3 */
897 ffadd(B, U, E1, E2);
898 ffsub(B, LO, U, T); /* lo = (e1 + e2) - kf P3 */
899 ffadd(B, RH, S2, LO); /* rh */
900 ffsub(B, U, RH, S2);
901 ffsub(B, RL, LO, U); /* rl = lo - (rh - s2) */
902 /* |x| <= pi/4 (0xC90FDBp-24 rounded): k = 0, rh = x, rl = 0 */
903 setpi(B, 0, DS_CMP_LE, 0, AX_, omega_float_to_bits(0xC90FDBp-24f));
904 sel(B, RH, X, RH, 0, 0);
905 sel(B, RL, RZ, RL, 0, 0);
906 sel(B, K, RZ, K, 0, 0);
907 iaddi(B, Q, K, qoff, RZ);
908 lopi(B, Q, Q, 3, LUT_AND); /* q = (k + qoff) & 3 */
909 /* sin_poly(rh, rl) -> SP */
910 ffmul(B, R2, RH, RH); /* r2 */
911 movf(B, SP, 0xB09231p-56f);
912 horner(B, SP, R2, -0xD7322Bp-49f, C);
913 horner(B, SP, R2, 0xB8EF1Dp-42f, C);
914 horner(B, SP, R2, -0xD00D01p-36f, C);
915 horner(B, SP, R2, 0x888889p-30f, C);
916 horner(B, SP, R2, -0xAAAAABp-26f, C);
917 ffmul(B, T, RH, R2); /* t = rh r2 */
918 movf(B, C, -0.5f);
919 ffmul(B, U, R2, C); /* r2 (-0.5) */
920 fffma(B, U, U, RL, RL); /* rlc = (r2 -0.5) rl + rl */
921 fffma(B, U, T, SP, U); /* t S + rlc */
922 ffadd(B, SP, RH, U); /* sin */
923 /* cos_poly(rh, rl) -> CP */
924 lopi(B, NR, R2, 0x80000000u, LUT_XOR);
925 fffma(B, R2L, RH, RH, NR); /* r2l = rh rh - r2 (exact) */
926 movf(B, CP, -0xC9CBA5p-60f);
927 horner(B, CP, R2, 0x8F76C7p-52f, C);
928 horner(B, CP, R2, -0x93F27Ep-45f, C);
929 horner(B, CP, R2, 0xD00D01p-39f, C);
930 horner(B, CP, R2, -0xB60B61p-33f, C);
931 horner(B, CP, R2, 0xAAAAABp-28f, C);
932 movf(B, C, 0.5f);
933 ffmul(B, HR, R2, C); /* hr = r2 0.5 */
934 movf(B, C, 1.0f);
935 ffsub(B, U, C, HR); /* h = 1 - hr */
936 ffsub(B, V, C, U);
937 ffsub(B, EH, V, HR); /* eh = (1 - h) - hr */
938 movf(B, C, -0.5f);
939 fffma(B, V, R2L, C, EH); /* c1 = r2l (-0.5) + eh */
940 lopi(B, W, RL, 0x80000000u, LUT_XOR); /* -rl */
941 fffma(B, V, W, RH, V); /* c2 = (-rl) rh + c1 */
942 ffmul(B, T, R2, R2); /* r4 */
943 fffma(B, V, T, CP, V); /* r4 C + c2 */
944 ffadd(B, CP, U, V); /* cos = h + (...) */
945 /* quadrant: odd q -> cos, even -> sin; q & 2 flips the sign */
946 lopi(B, T, Q, 1, LUT_AND);
947 setpi(B, 0, DS_CMP_NE, 0, T, 0);
948 sel(B, U, CP, SP, 0, 0);
949 lopi(B, V, U, 0x80000000u, LUT_XOR);
950 lopi(B, T, Q, 2, LUT_AND);
951 setpi(B, 1, DS_CMP_NE, 0, T, 0);
952 sel(B, RES, V, U, 1, 0);
953 /* special inputs, lowest priority first */
954 setpi(B, 0, DS_CMP_GT, 0, AX_, 0x4a800000u /* 2^22 */);
955 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN, +-inf, |x| > 2^22: NaN */
956 if (qoff == 0) {
957 setp(B, 0, DS_CMP_EQ, 0, AX_, RZ);
958 sel(B, RES, X, RES, 0, 0); /* sin(+-0) = +-0 */
959 }
960}
961static void body_sin(Bld *B) { body_sincos(B, 0); }
962static void body_cos(Bld *B) { body_sincos(B, 1); }
963
964/* ---- ERF / GELU (E1 row 10) ------------------------------------------------
965 * Shared pieces of omega_math_erf and omega_math_gelu (omega_numeric_transc.c),
966 * issued in the written order. The 17-interval erfcx table is chosen by a
967 * chain of ISETP/SEL (the largest j with y >= ERFCX_LO[j] wins, the order of
968 * the CPU's while loop); the constants come from omega_numeric_transc_tables.h,
969 * the same header the CPU sequence reads. */
970
971/* exp_core2(ah, al) -> m, k, ml. Temps R10-R17; ah and al must not be there. */
972static void exp_core2_body(Bld *B, int ah, int al, int m, int k, int ml) {
973 enum { C = 10, T = 11, KF = 12, NKF = 13, R = 14, P = 15, MG = 16, Q = 17 };
974 movf(B, C, 0xB8AA3Bp-23f); /* INVLN2 */
975 ffmul(B, T, ah, C);
976 movi(B, MG, 0x4b400000u); /* MAGIC */
977 ffadd(B, T, T, MG);
978 ffsub(B, KF, T, MG); /* kf */
979 isub(B, k, T, MG); /* k = tb(t) - tb(MAGIC) */
980 lopi(B, NKF, KF, 0x80000000u, LUT_XOR); /* -kf */
981 movf(B, C, 0xB17218p-24f); /* LN2_HI */
982 fffma(B, R, NKF, C, ah);
983 movf(B, C, -0x82E308p-52f); /* LN2_LO */
984 fffma(B, R, NKF, C, R);
985 ffadd(B, R, R, al); /* r += al */
986 movf(B, P, 0xD00D01p-39f); /* 1/8! */
987 horner(B, P, R, 0xD00D01p-36f, C);
988 horner(B, P, R, 0xB60B61p-33f, C);
989 horner(B, P, R, 0x888889p-30f, C);
990 horner(B, P, R, 0xAAAAABp-28f, C);
991 horner(B, P, R, 0xAAAAABp-26f, C);
992 horner(B, P, R, 0.5f, C);
993 horner(B, P, R, 1.0f, C);
994 movf(B, C, 1.0f);
995 fffma(B, m, P, R, C); /* m = ffma(p, r, 1) */
996 ffsub(B, Q, C, m); /* 1 - m (exact) */
997 fffma(B, ml, P, R, Q); /* ml = ffma(p, r, 1 - m) */
998}
999
1000/* erf_small(y) -> out (|y| < 0.5 and tiny y). Temps R10-R16. */
1001static void erf_small_body(Bld *B, int y, int out) {
1002 enum { Y2 = 10, Q = 11, W = 12, A = 13, NA = 14, AL = 15, BB = 16, TMP = 17 };
1003 ffmul(B, Y2, y, y);
1004 movf(B, Q, -0xDDEBBDp-40f); /* n = 7 */
1005 horner(B, Q, Y2, 0xE00E01p-37f, TMP);
1006 horner(B, Q, Y2, -0xC6980Cp-34f, TMP);
1007 horner(B, Q, Y2, 0x97B426p-31f, TMP);
1008 horner(B, Q, Y2, -0xC30C31p-29f, TMP);
1009 horner(B, Q, Y2, 0xCCCCCDp-27f, TMP);
1010 horner(B, Q, Y2, -0xAAAAABp-25f, TMP); /* n = 1: -1/3 */
1011 ffmul(B, W, Y2, Q); /* w = y2 q */
1012 movf(B, TMP, 0x906EBBp-23f); /* TSP_HI */
1013 ffmul(B, A, y, TMP);
1014 lopi(B, NA, A, 0x80000000u, LUT_XOR);
1015 fffma(B, AL, y, TMP, NA); /* Al = y TSP_HI - A */
1016 movf(B, TMP, -0xFBD649p-48f); /* TSP_LO */
1017 fffma(B, BB, y, TMP, AL); /* B = y TSP_LO + Al */
1018 fffma(B, TMP, A, W, BB); /* A w + B */
1019 ffadd(B, out, A, TMP);
1020}
1021
1022/* Chooses the erfcx interval of y (>= 0) into the table registers
1023 * A[n] = R10 + n (n = 0..10), A0LO = R21, CC = R22; P0 is clobbered. */
1024static void erfcx_table_body(Bld *B, int y) {
1025 enum { A0 = 10, A0LO = 21, CC = 22 };
1026 for (int n = 0; n <= 10; n++) movf(B, A0 + n, ERFCX_A[0][n]);
1027 movf(B, A0LO, ERFCX_A0LO[0]);
1028 movf(B, CC, ERFCX_C[0]);
1029 for (int j = 1; j < ERFCX_N; j++) {
1030 setpi(B, 0, DS_CMP_GE, 0, y, omega_float_to_bits(ERFCX_LO[j]));
1031 for (int n = 0; n <= 10; n++) seli(B, A0 + n, A0 + n, omega_float_to_bits(ERFCX_A[j][n]), 0, 1);
1032 seli(B, A0LO, A0LO, omega_float_to_bits(ERFCX_A0LO[j]), 0, 1);
1033 seli(B, CC, CC, omega_float_to_bits(ERFCX_C[j]), 0, 1);
1034 }
1035}
1036
1037/* erfcx_eval2(y) -> c, cl from the table registers (see erfcx_table_body).
1038 * t and p are scratch; writes c, cl, t, p. */
1039static void erfcx_eval_body(Bld *B, int y, int c, int cl, int t, int p, int want_cl) {
1040 enum { A0 = 10, A0LO = 21, CC = 22 };
1041 ffsub(B, t, y, CC); /* t = y - centre (exact) */
1042 fffma(B, p, A0 + 10, t, A0 + 9);
1043 for (int n = 8; n >= 1; n--) fffma(B, p, p, t, A0 + n);
1044 fffma(B, c, p, t, A0); /* c = p t + a0 */
1045 if (want_cl) {
1046 ffsub(B, cl, A0, c); /* a0 - c */
1047 fffma(B, cl, p, t, cl);
1048 ffadd(B, cl, cl, A0LO);
1049 }
1050}
1051
1052/* omega_math_erf. x in R2. */
1053static void body_erf(Bld *B) {
1054 enum { X = 2, AXR = 3, SG = 4, SMALLR = 23, H = 24, NH = 25, HL = 26, NHL = 27, M = 28, K = 29, ML = 30,
1055 T = 31, P = 32, C = 33, MC = 34, S = 35, ONE = 36, BIGR = 37 };
1056 lopi(B, AXR, X, 0x7fffffffu, LUT_AND);
1057 lopi(B, SG, X, 0x80000000u, LUT_AND);
1058 erf_small_body(B, AXR, SMALLR);
1059 ffmul(B, H, AXR, AXR); /* h = ax ax */
1060 lopi(B, NH, H, 0x80000000u, LUT_XOR);
1061 fffma(B, HL, AXR, AXR, NH); /* hl = ax ax - h (exact) */
1062 lopi(B, NHL, HL, 0x80000000u, LUT_XOR);
1063 exp_core2_body(B, NH, NHL, M, K, ML); /* e^-(h + hl) = m 2^k */
1064 erfcx_table_body(B, AXR);
1065 erfcx_eval_body(B, AXR, C, 0, T, P, 0);
1066 ffmul(B, MC, M, C); /* erfc = m erfcx */
1067 scale2_core(B, S, MC, K, 10, 11, 12, 13, 14);
1068 movf(B, ONE, 1.0f);
1069 ffsub(B, BIGR, ONE, S); /* 1 - erfc */
1070 setpi(B, 0, DS_CMP_LT, 0, AXR, 0x3f000000u); /* |x| < 0.5 */
1071 sel(B, RES, SMALLR, BIGR, 0, 0);
1072 setpi(B, 0, DS_CMP_GE, 0, AXR, 0x40800000u); /* |x| >= 4: 1 */
1073 seli(B, RES, RES, 0x3f800000u, 0, 1);
1074 lop(B, RES, RES, SG, RZ, LUT_OR); /* r | sign */
1075 setpi(B, 0, DS_CMP_GT, 0, AXR, 0x7f800000u);
1076 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
1077}
1078
1079/* omega_math_gelu (erf form). x in R2. Every path is computed, then the
1080 * CPU's branch order is replayed with selects, lowest priority first. */
1081static void body_gelu(Bld *B) {
1082 enum { X = 2, AXR = 3, SG = 4, Y = 23, AY = 24, HX = 25, SMALLR = 26, P = 27, PL = 28, H = 29, HL = 30,
1083 NH = 31, NHL = 32, ME = 33, K = 34, MEL = 35, T = 36, PP = 37, C = 38, CL = 39, U = 40, V = 41,
1084 W = 42, LARGE = 43, S = 44 };
1085 lopi(B, AXR, X, 0x7fffffffu, LUT_AND);
1086 lopi(B, SG, X, 0x80000000u, LUT_AND);
1087 movf(B, U, 0xB504F3p-24f); /* INV_SQRT2 */
1088 ffmul(B, Y, X, U); /* y = x INV_SQRT2 */
1089 lopi(B, AY, Y, 0x7fffffffu, LUT_AND);
1090 movf(B, V, 0.5f);
1091 ffmul(B, HX, X, V); /* hx = x 0.5 */
1092 /* ay < 0.5: x/2 + x/2 erf(y) */
1093 erf_small_body(B, AY, V); /* e = erf_small(ay) */
1094 lopi(B, W, Y, 0x80000000u, LUT_AND);
1095 lop(B, V, V, W, RZ, LUT_OR); /* e | sign(y) */
1096 fffma(B, W, HX, V, HX); /* r = hx e + hx */
1097 lop(B, SMALLR, W, SG, RZ, LUT_OR); /* r | sign(x) */
1098 /* large path */
1099 ffmul(B, P, X, X); /* P = x x */
1100 lopi(B, U, P, 0x80000000u, LUT_XOR);
1101 fffma(B, PL, X, X, U); /* Pl = x x - P */
1102 movf(B, U, 0.5f);
1103 ffmul(B, H, P, U); /* h = P 0.5 */
1104 ffmul(B, HL, PL, U); /* hl = Pl 0.5 */
1105 lopi(B, NH, H, 0x80000000u, LUT_XOR);
1106 lopi(B, NHL, HL, 0x80000000u, LUT_XOR);
1107 exp_core2_body(B, NH, NHL, ME, K, MEL); /* e^(-y^2) ~ (me + mel) 2^k */
1108 erfcx_table_body(B, AY);
1109 erfcx_eval_body(B, AY, C, CL, T, PP, 1);/* erfcx(|y|) ~ c + cl */
1110 /* yl = x/sqrt2 - y to first order; ayl = sign(x) ? -yl : yl */
1111 movf(B, U, 0xB504F3p-24f);
1112 lopi(B, V, Y, 0x80000000u, LUT_XOR);
1113 fffma(B, W, X, U, V); /* yl = x INV_SQRT2 - y (exact) */
1114 movf(B, U, 0xCFE77Ap-50f); /* INV_SQRT2_LO */
1115 fffma(B, W, X, U, W); /* yl += x INV_SQRT2_LO */
1116 setpi(B, 1, DS_CMP_NE, 0, SG, 0); /* P1: x negative */
1117 lopi(B, V, W, 0x80000000u, LUT_XOR);
1118 sel(B, W, V, W, 1, 0); /* ayl */
1119 /* dc = ffma(ay 2, c, -TSP_HI); cl = ffma(ayl, dc, cl) */
1120 movf(B, U, 2.0f);
1121 ffmul(B, V, AY, U);
1122 movf(B, U, -0x906EBBp-23f);
1123 fffma(B, V, V, C, U); /* dc */
1124 fffma(B, CL, W, V, CL);
1125 /* p = me c; pl = ffma(me, c, -p) + me cl + mel c */
1126 ffmul(B, PP, ME, C);
1127 lopi(B, U, PP, 0x80000000u, LUT_XOR);
1128 fffma(B, V, ME, C, U); /* pl = me c - p */
1129 fffma(B, V, ME, CL, V);
1130 fffma(B, V, MEL, C, V); /* pl */
1131 /* x >= 0: ffma(-hx, scale2(p, k), x) */
1132 scale2_core(B, S, PP, K, 10, 11, 12, 13, 14);
1133 lopi(B, U, HX, 0x80000000u, LUT_XOR); /* -hx */
1134 fffma(B, LARGE, U, S, X);
1135 /* x < 0: scale2(ffma(hx, p, hx pl), k) */
1136 ffmul(B, W, HX, V); /* hx pl */
1137 fffma(B, W, HX, PP, W); /* hx p + hx pl */
1138 scale2_core(B, S, W, K, 10, 11, 12, 13, 14);
1139 setpi(B, 1, DS_CMP_NE, 0, SG, 0);
1140 sel(B, LARGE, S, LARGE, 1, 0); /* x < 0 -> negative form */
1141 /* branches, lowest priority first */
1142 setpi(B, 0, DS_CMP_LT, 0, AY, 0x3f000000u); /* ay < 0.5 */
1143 sel(B, RES, SMALLR, LARGE, 0, 0);
1144 /* |x| < 2^-125: integer only, tie broken toward +inf */
1145 iaddi(B, V, AXR, 1, RZ);
1146 shri(B, V, V, 1); /* (m + 1) >> 1 */
1147 shri(B, W, AXR, 1); /* m >> 1 */
1148 setpi(B, 1, DS_CMP_NE, 0, SG, 0);
1149 sel(B, V, W, V, 1, 0); /* sign: m >> 1, else (m+1) >> 1 */
1150 lop(B, V, V, SG, RZ, LUT_OR);
1151 setpi(B, 0, DS_CMP_LT, 0, AXR, 0x01000000u);
1152 sel(B, RES, V, RES, 0, 0);
1153 /* x < -15.5: -0 ; x >= 8: x ; zero is covered by the tiny branch */
1154 setpi(B, 0, DS_CMP_GT, 0, AXR, 0x41780000u); /* |x| > 15.5 */
1155 movi(B, W, 0x80000000u);
1156 sel(B, V, W, RES, 0, 0);
1157 setpi(B, 1, DS_CMP_NE, 0, SG, 0);
1158 sel(B, RES, V, RES, 1, 0); /* ... and x negative: -0 */
1159 setpi(B, 0, DS_CMP_GE, 1, X, 0x41000000u);
1160 sel(B, RES, X, RES, 0, 0); /* x >= 8: x (signed compare) */
1161 setpi(B, 0, DS_CMP_GT, 0, AXR, 0x7f800000u);
1162 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
1163}
1164
1165/* ---- RSQRT (E1 row 10) ---------------------------------------------------------
1166 * Correctly rounded 1/sqrt(x), bit-identical to omega_math_rsqrt. The CPU tier
1167 * decides the final rounding with a 128-bit integer midpoint test (mid_below);
1168 * this frame has no integer multiply, so the same decision is made with
1169 * binary32 FMA error-free transforms.
1170 *
1171 * |x| (positive, finite, nonzero) is normalized to x' in [1,4) (a subnormal by
1172 * an exact x 2^24, the parity of the exponent folded into x') so that
1173 * y = 1/sqrt(x') lies in (0.5, 1]; the result is y 2^k (2^-12 more for a
1174 * subnormal input), both exact FMULs.
1175 * 1. y0: magic-number seed, three FMA Newton steps, one FMA residual
1176 * correction. y0 is within one ulp of the correctly rounded y.
1177 * 2. mu = y0 + 2^-25 and ml = y0 - 2^-25 are at most the midpoints to the
1178 * neighbours of y0 (the gap on either side of y0 in [0.5, 1] is 2^-24 or
1179 * 2^-23 above 1... never below 2^-24), and 1/sqrt(x') = 1 only for x' = 1.
1180 * The correctly rounded result is y0 + [mu^2 x' < 1] + [ml^2 x' < 1] - 1.
1181 * 3. The sign of m^2 x' - 1 is exact. For m = y0 + s 2^-25 it is the sign of
1182 * (x' y0^2 - 1) + s x' y0 2^-24 + x' 2^-50.
1183 * Products are split by FMA into (hi, lo) with no rounding error, the
1184 * terms are summed exactly into a nonoverlapping expansion (Shewchuk
1185 * GROW-EXPANSION over Knuth TwoSum) and the sign of an expansion is the
1186 * sign of its highest nonzero component. No midpoint equals 1/sqrt(x'),
1187 * so no expansion is zero.
1188 * The host model equals the CPU tier on every 32-bit input
1189 * (test_omega_numeric_transc_gb10 --host-all RSQRT). */
1190static void grow_expansion(Bld *B, const int *e, int n, int b, int qa, int qb, int t1, int t2) {
1191 /* e[0..n-1] += b exactly; e[0..n] on return (e[n] the highest component).
1192 * b is read but never written. */
1193 int q = b;
1194 for (int i = 0; i < n; i++) {
1195 int s = (i == n - 1) ? e[n] : (q == qa ? qb : qa);
1196 two_sum_b(B, s, e[i], q, e[i], t1, t2);
1197 q = s;
1198 }
1199}
1200
1201/* out := the highest nonzero component of e[0..n-1] (its sign bit is the sign). */
1202static void expansion_sign(Bld *B, const int *e, int n, int out, int t) {
1203 mov(B, out, e[0]);
1204 for (int i = 1; i < n; i++) {
1205 shli(B, t, e[i], 1);
1206 setpi(B, 0, DS_CMP_NE, 0, t, 0);
1207 sel(B, out, e[i], out, 0, 0);
1208 }
1209}
1210
1211static void body_rsqrt(Bld *B) {
1212 enum { AXR = 8, XB = 10, BE = 11, PAR = 12, X = 13, NX = 14, SC = 15, POST = 16, Y = 17,
1213 T = 18, NTS = 19, NTL = 20, E = 21, H = 22, ONE = 23, HALF = 24, P = 25, PL = 26, HH = 27,
1214 HL = 28, GG = 29, GL = 30, Q = 31, QL = 32, G = 33, TS = 34, TL = 35, W = 36,
1215 SU = 44, SL = 45 };
1216 const int SH[5] = { 37, 38, 39, 40, 41 };
1217 const int WK[7] = { 25, 26, 27, 28, 29, 30, 31 };
1218 const int T1 = 10, T2 = 11, QA = 12, QB = 14; /* TwoSum temps, free after the setup */
1219 lopi(B, AXR, A_, 0x7fffffffu, LUT_AND);
1220 /* subnormal: x 2^24 exact; XB = normal float bits */
1221 movi(B, T, 0x4b800000u);
1222 ffmul(B, E, AXR, T);
1223 setpi(B, 0, DS_CMP_LT, 0, AXR, 0x00800000u);
1224 sel(B, XB, E, AXR, 0, 0);
1225 movi(B, T, 0x45800000u);
1226 seli(B, POST, T, 0x3f800000u, 0, 0); /* 2^12 for a subnormal, else 1 */
1227 shri(B, BE, XB, 23);
1228 iaddi(B, T, BE, 1, RZ);
1229 lopi(B, PAR, T, 1, LUT_AND); /* 1 when the exponent is even */
1230 lopi(B, X, XB, 0x007fffffu, LUT_AND);
1231 lopi(B, X, X, 0x3f800000u, LUT_OR);
1232 shli(B, T, PAR, 23);
1233 iadd(B, X, X, T, RZ); /* x' in [1,4) */
1234 isub(B, T, BE, PAR);
1235 iaddi(B, T, T, 129, RZ);
1236 shri(B, T, T, 1);
1237 rsubi(B, T, T, 255);
1238 shli(B, SC, T, 23); /* 2^-(e' / 2) */
1239 lopi(B, NX, X, 0x80000000u, LUT_XOR);
1240 movf(B, ONE, 1.0f);
1241 movf(B, HALF, 0.5f);
1242 /* y0 */
1243 shri(B, T, X, 1);
1244 rsubi(B, Y, T, 0x5f3759dfu);
1245 for (int k = 0; k < 3; k++) {
1246 ffmul(B, T, Y, Y);
1247 fffma(B, E, NX, T, ONE); /* 1 - x y^2 */
1248 ffmul(B, H, Y, HALF);
1249 fffma(B, Y, H, E, Y);
1250 }
1251 ffmul(B, G, X, Y);
1252 ffmul(B, H, Y, HALF);
1253 lopi(B, T, G, 0x80000000u, LUT_XOR);
1254 fffma(B, E, T, H, HALF); /* 1/2 - g h */
1255 fffma(B, G, G, E, G);
1256 fffma(B, H, H, E, H);
1257 ffadd(B, Y, H, H);
1258 /* error-free terms of x' (y0 + s 2^-25)^2 - 1 */
1259 ffmul(B, P, Y, Y);
1260 lopi(B, T, P, 0x80000000u, LUT_XOR);
1261 fffma(B, PL, Y, Y, T); /* y0^2 = P + PL */
1262 ffmul(B, HH, X, P);
1263 lopi(B, T, HH, 0x80000000u, LUT_XOR);
1264 fffma(B, HL, X, P, T);
1265 ffmul(B, GG, X, PL);
1266 lopi(B, T, GG, 0x80000000u, LUT_XOR);
1267 fffma(B, GL, X, PL, T);
1268 ffsub(B, SH[0], HH, ONE); /* HH - 1, exact (Sterbenz) */
1269 ffmul(B, Q, X, Y);
1270 lopi(B, T, Q, 0x80000000u, LUT_XOR);
1271 fffma(B, QL, X, Y, T); /* x y0 = Q + QL */
1272 movf(B, T, 0x1p-24f);
1273 ffmul(B, TS, Q, T);
1274 ffmul(B, TL, QL, T);
1275 movf(B, T, 0x1p-50f);
1276 ffmul(B, W, X, T);
1277 lopi(B, NTS, TS, 0x80000000u, LUT_XOR);
1278 lopi(B, NTL, TL, 0x80000000u, LUT_XOR);
1279 grow_expansion(B, SH, 1, HL, QA, QB, T1, T2);
1280 grow_expansion(B, SH, 2, GG, QA, QB, T1, T2);
1281 grow_expansion(B, SH, 3, GL, QA, QB, T1, T2);
1282 grow_expansion(B, SH, 4, W, QA, QB, T1, T2);
1283 /* mu: + s x y0 2^-24 with s = +1; r > mu exactly when the sum is negative */
1284 for (int i = 0; i < 5; i++) mov(B, WK[i], SH[i]);
1285 grow_expansion(B, WK, 5, TS, QA, QB, T1, T2);
1286 grow_expansion(B, WK, 6, TL, QA, QB, T1, T2);
1287 expansion_sign(B, WK, 7, SU, T);
1288 shri(B, SU, SU, 31); /* 1: round up */
1289 /* ml: s = -1; r < ml exactly when the sum is positive */
1290 for (int i = 0; i < 5; i++) mov(B, WK[i], SH[i]);
1291 grow_expansion(B, WK, 5, NTS, QA, QB, T1, T2);
1292 grow_expansion(B, WK, 6, NTL, QA, QB, T1, T2);
1293 expansion_sign(B, WK, 7, SL, T);
1294 shri(B, SL, SL, 31); /* 1: ml is not below r */
1295 iadd(B, T, Y, SU, SL);
1296 iaddi(B, T, T, -1, RZ); /* y0 + up + (1 - down) - 1 */
1297 ffmul(B, T, T, SC);
1298 ffmul(B, RES, T, POST);
1299 /* special inputs, lowest priority first */
1300 setpi(B, 0, DS_CMP_GE, 0, A_, 0x80000000u);
1301 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* x < 0: NaN */
1302 lopi(B, T, A_, 0x7f800000u, LUT_OR);
1303 setp(B, 0, DS_CMP_EQ, 0, AXR, RZ);
1304 sel(B, RES, T, RES, 0, 0); /* +-0: +-inf */
1305 setpi(B, 0, DS_CMP_EQ, 0, A_, 0x7f800000u);
1306 sel(B, RES, RZ, RES, 0, 0); /* +inf: +0 */
1307 setpi(B, 0, DS_CMP_GT, 0, AXR, 0x7f800000u);
1308 seli(B, RES, RES, 0x7fc00000u, 0, 1); /* NaN */
1309}
1310size_t omega_ds_body(OmegaDsOp op, OmegaDsInsn *out, size_t max) {
1311 Bld B = { out, 0, max, 0 };
1312 if (!out) return 0;
1313 if (op == OMEGA_DS_DIV) body_div(&B);
1314 else if (op == OMEGA_DS_SQRT) body_sqrt(&B);
1315 else if (op == OMEGA_DS_EXP2) body_exp2(&B);
1316 else if (op == OMEGA_DS_LOG2) body_log2(&B);
1317 else if (op == OMEGA_DS_SIGMOID) body_sigmoid(&B);
1318 else if (op == OMEGA_DS_TANH) body_tanh(&B);
1319 else if (op == OMEGA_DS_SIN) body_sin(&B);
1320 else if (op == OMEGA_DS_COS) body_cos(&B);
1321 else if (op == OMEGA_DS_ERF) body_erf(&B);
1322 else if (op == OMEGA_DS_GELU) body_gelu(&B);
1323 else if (op == OMEGA_DS_RSQRT) body_rsqrt(&B);
1324 else if (op == OMEGA_DS_EXP) body_exp(&B);
1325 else if (op == OMEGA_DS_LOG) body_log(&B);
1326 else return 0;
1327 return B.bad ? 0 : B.n;
1328}
1329#undef RZ
1330
1331/* ---- Host model ------------------------------------------------------------ */
1332
1333static uint32_t lut3(uint32_t a, uint32_t b, uint32_t c, uint8_t lut) {
1334 uint32_t r = 0;
1335 for (int i = 0; i < 8; i++) {
1336 if (!((lut >> i) & 1)) continue;
1337 r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c);
1338 }
1339 return r;
1340}
1341
1342static bool icmp(uint8_t cmp, bool sgn, uint32_t a, uint32_t b) {
1343 int lt = sgn ? ((int32_t)a < (int32_t)b) : (a < b);
1344 int eq = a == b;
1345 switch (cmp) {
1346 case DS_CMP_LT: return lt;
1347 case DS_CMP_EQ: return eq;
1348 case DS_CMP_LE: return lt || eq;
1349 case DS_CMP_GT: return !lt && !eq;
1350 case DS_CMP_NE: return !eq;
1351 case DS_CMP_GE: return !lt;
1352 }
1353 return false;
1354}
1355
1356
1357/* FADD / FMUL / FFMA on the host: the AArch64 binary32 instructions (round to
1358 * nearest even, subnormals kept: FPCR is left at its Linux default), any NaN
1359 * result replaced by 0x7fffffff, the canonical NaN the GB10 writes. */
1360#if !defined(__aarch64__)
1361#error "the host model of the FP forms issues AArch64 FADD/FMUL/FMADD"
1362#endif
1363static uint32_t hfp(int kind, uint32_t a, uint32_t b, uint32_t c) {
1364 float x = omega_bits_to_float(a), y = omega_bits_to_float(b), z = omega_bits_to_float(c), r;
1365 if (kind == 0) __asm__ volatile("fadd %s0, %s1, %s2" : "=w"(r) : "w"(x), "w"(y));
1366 else if (kind == 1) __asm__ volatile("fmul %s0, %s1, %s2" : "=w"(r) : "w"(x), "w"(y));
1367 else __asm__ volatile("fmadd %s0, %s1, %s2, %s3" : "=w"(r) : "w"(x), "w"(y), "w"(z));
1368 uint32_t u = omega_float_to_bits(r);
1369 return (u & 0x7fffffffu) > 0x7f800000u ? 0x7fffffffu : u;
1370}
1371static uint32_t run_body(const OmegaDsInsn *v, size_t n, uint32_t a, uint32_t b) {
1372 uint32_t r[256];
1373 bool p[8];
1374 for (int i = 0; i < 256; i++) r[i] = 0xdeadbeefu;
1375 for (int i = 0; i < 8; i++) p[i] = false;
1376 r[OMEGA_DS_RZ] = 0; p[OMEGA_DS_PT] = true;
1377 r[2] = a; r[5] = b;
1378 for (size_t k = 0; k < n; k++) {
1379 const OmegaDsInsn *x = &v[k];
1380 uint32_t ra = r[x->a], rb = r[x->b], rc = r[x->c], res = 0;
1381 switch (x->kind) {
1382 case DSK_IADD3_R: res = (x->nega ? 0u - ra : ra) + (x->negb ? 0u - rb : rb) + rc; break;
1383 case DSK_IADD3_I: res = (x->nega ? 0u - ra : ra) + x->imm + rc; break;
1384 case DSK_LOP3_R: res = lut3(ra, rb, rc, x->lut); break;
1385 case DSK_LOP3_I: res = lut3(ra, x->imm, rc, x->lut); break;
1386 case DSK_SHL_I: res = x->imm >= 32 ? 0 : ra << x->imm; break;
1387 case DSK_SHL_R: res = rb >= 32 ? 0 : ra << rb; break;
1388 case DSK_SHR_I: res = x->imm >= 32 ? 0 : rc >> x->imm; break;
1389 case DSK_SHR_R: res = rb >= 32 ? 0 : rc >> rb; break;
1390 case DSK_ISETP_R: p[x->d] = icmp(x->cmp, x->is_signed, ra, rb); continue;
1391 case DSK_ISETP_I: p[x->d] = icmp(x->cmp, x->is_signed, ra, x->imm); continue;
1392 case DSK_SEL_R: res = (p[x->ps] ^ (x->pneg != 0)) ? ra : rb; break;
1393 case DSK_SEL_I: res = (p[x->ps] ^ (x->pneg != 0)) ? ra : x->imm; break;
1394 case DSK_I2FP: res = omega_float_to_bits((float)(int32_t)rb); break;
1395 case DSK_FADD_R: res = hfp(0, ra, x->negb ? rb ^ 0x80000000u : rb, 0); break;
1396 case DSK_FMUL_R: res = hfp(1, ra, rb, 0); break;
1397 case DSK_FFMA_R: res = hfp(2, ra, rb, rc); break;
1398 }
1399 if (x->d != OMEGA_DS_RZ) r[x->d] = res;
1400 }
1401 return r[OMEGA_DS_RESULT_REG];
1402}
1403
1404uint32_t omega_ds_host_exec(OmegaDsOp op, uint32_t a, uint32_t b) {
1405 static OmegaDsInsn cache[OMEGA_DS_OP_COUNT][OMEGA_DS_MAX_BODY];
1406 static size_t cn[OMEGA_DS_OP_COUNT];
1407 if ((unsigned)op >= OMEGA_DS_OP_COUNT) return 0;
1408 if (!cn[op]) cn[op] = omega_ds_body(op, cache[op], OMEGA_DS_MAX_BODY);
1409 return run_body(cache[op], cn[op], a, b);
1410}
1411
1412uint32_t omega_ds_cpu_semantic(OmegaDsOp op, uint32_t a, uint32_t b) {
1413 if (op == OMEGA_DS_DIV) return omega_float_to_bits(omega_math_div(omega_bits_to_float(a), omega_bits_to_float(b)));
1414 if (op == OMEGA_DS_EXP) return omega_float_to_bits(omega_math_exp(omega_bits_to_float(a)));
1415 if (op == OMEGA_DS_LOG) return omega_float_to_bits(omega_math_log(omega_bits_to_float(a)));
1416 if (op != OMEGA_DS_SQRT) return 0x7fc00000u;
1417 return omega_float_to_bits(omega_math_sqrt(omega_bits_to_float(a)));
1418}
1419
1420OmegaDsOp omega_ds_op_for_numeric(OmegaNumericOp op) {
1421 return op == OMEGA_NOP_DIV ? OMEGA_DS_DIV : op == OMEGA_NOP_SQRT ? OMEGA_DS_SQRT
1422 : op == OMEGA_NOP_EXP ? OMEGA_DS_EXP : op == OMEGA_NOP_LOG ? OMEGA_DS_LOG : OMEGA_DS_OP_COUNT;
1423}
1424
1425/* ---- Kernel image ------------------------------------------------------------ */
1426
1427static const char *const PROLOGUE_TEXT[OMEGA_DS_PROLOGUE_INSNS] = {
1428 "LDC R1, c[0x0][0x37c]", "S2R R9, SR_CTAID.X", "LDCU UR4, c[0x0][0x360]", "S2R R0, SR_TID.X",
1429 "LDCU UR5, c[0x0][0x398]", "IMAD R9, R9, UR4, R0", "ISETP.GE.U32.AND P0, PT, R9, UR5, PT", "@P0 EXIT",
1430 "LDC.64 R2, c[0x0][0x380]", "LDCU.64 UR4, c[0x0][0x358]", "LDC.64 R4, c[0x0][0x388]",
1431 "LDC.64 R6, c[0x0][0x390]", "IMAD.WIDE.U32 R2, R9, 0x4, R2", "LDG.E R2, desc[UR4][R2.64]",
1432 "IMAD.WIDE.U32 R4, R9, 0x4, R4", "LDG.E R5, desc[UR4][R4.64]", "IMAD.WIDE.U32 R6, R9, 0x4, R6",
1433};
1434#define VA_STG 18u
1435#define VA_EXIT 19u
1436#define VA_BRA 20u
1437#define VA_NOP 21u
1438
1439static void put_words(uint8_t *p, const uint32_t w[4]) {
1440 for (int i = 0; i < 4; i++) {
1441 p[4 * i] = (uint8_t)w[i]; p[4 * i + 1] = (uint8_t)(w[i] >> 8);
1442 p[4 * i + 2] = (uint8_t)(w[i] >> 16); p[4 * i + 3] = (uint8_t)(w[i] >> 24);
1443 }
1444}
1445
1446static void get_words(const uint8_t *p, uint32_t w[4]) {
1447 for (int i = 0; i < 4; i++)
1448 w[i] = (uint32_t)p[4 * i] | (uint32_t)p[4 * i + 1] << 8 | (uint32_t)p[4 * i + 2] << 16 | (uint32_t)p[4 * i + 3] << 24;
1449}
1450
1451static size_t padded_insns(size_t body) {
1452 size_t n = OMEGA_DS_PROLOGUE_INSNS + body + OMEGA_DS_EPILOGUE_INSNS;
1453 return (n + 7) & ~(size_t)7;
1454}
1455
1456int omega_ds_build_kernel(OmegaDsOp op, uint8_t *code, size_t max, size_t *out_len) {
1457 uint8_t va[OMEGA_BW_VECADD_CODE_SIZE];
1458 size_t vl = 0;
1459 OmegaDsInsn body[OMEGA_DS_MAX_BODY];
1460 if (!code) return -1;
1461 size_t nb = omega_ds_body(op, body, OMEGA_DS_MAX_BODY);
1462 if (nb == 0) return -1;
1463 if (omega_blackwell_encode_vecadd(va, sizeof(va), &vl) != 0 || vl != OMEGA_BW_VECADD_CODE_SIZE) return -1;
1464 size_t total = padded_insns(nb);
1465 if (total * 16 > max || total * 16 > OMEGA_DS_MAX_CODE_BYTES) return -1;
1466 size_t k = 0;
1467 for (; k < OMEGA_DS_PROLOGUE_INSNS; k++) memcpy(code + 16 * k, va + 16 * k, 16);
1468 for (size_t i = 0; i < nb; i++, k++) {
1469 uint32_t w[4];
1470 if (omega_ds_encode(&body[i], w) != 0) return -1;
1471 put_words(code + 16 * k, w);
1472 }
1473 memcpy(code + 16 * k++, va + 16 * VA_STG, 16);
1474 memcpy(code + 16 * k++, va + 16 * VA_EXIT, 16);
1475 memcpy(code + 16 * k++, va + 16 * VA_BRA, 16);
1476 for (; k < total; k++) memcpy(code + 16 * k, va + 16 * VA_NOP, 16);
1477 if (out_len) *out_len = total * 16;
1478 return 0;
1479}
1480
1481int omega_ds_listing(OmegaDsOp op, char *buf, size_t len) {
1482 OmegaDsInsn body[OMEGA_DS_MAX_BODY];
1483 size_t nb = omega_ds_body(op, body, OMEGA_DS_MAX_BODY);
1484 if (!buf || nb == 0) return -1;
1485 size_t pos = 0, k = 0, total = padded_insns(nb);
1486 char t[96];
1487#define LINE(...) do { int w_ = snprintf(buf + pos, pos < len ? len - pos : 0, __VA_ARGS__); \
1488 if (w_ < 0 || pos + (size_t)w_ >= len) { return -1; } \
1489 pos += (size_t)w_; } while (0)
1490 for (; k < OMEGA_DS_PROLOGUE_INSNS; k++) LINE("%04zx %s ;\n", k * 16, PROLOGUE_TEXT[k]);
1491 for (size_t i = 0; i < nb; i++, k++) { omega_ds_format(&body[i], t, sizeof(t)); LINE("%04zx %s ;\n", k * 16, t); }
1492 LINE("%04zx STG.E desc[UR4][R6.64], R9 ;\n", k * 16); k++;
1493 LINE("%04zx EXIT ;\n", k * 16); k++;
1494 LINE("%04zx BRA 0x%zx;\n", k * 16, k * 16); k++;
1495 for (; k < total; k++) LINE("%04zx NOP;\n", k * 16);
1496#undef LINE
1497 return 0;
1498}
1499
1500/* ---- Pre-submission checks ---------------------------------------------------- */
1501
1502static int refuse(char *err, size_t err_len, int code, const char *fmt, ...) {
1503 if (err && err_len) {
1504 va_list ap;
1505 va_start(ap, fmt);
1506 vsnprintf(err, err_len, fmt, ap);
1507 va_end(ap);
1508 }
1509 return code;
1510}
1511
1512static bool writes_reg(const OmegaDsInsn *x) { return x->kind != DSK_ISETP_R && x->kind != DSK_ISETP_I; }
1513
1514/* Source registers an instruction reads (RZ excluded). */
1515static int src_regs(const OmegaDsInsn *x, uint8_t out[3]) {
1516 int n = 0;
1517 switch (x->kind) {
1518 case DSK_IADD3_R: case DSK_LOP3_R: out[n++] = x->a; out[n++] = x->b; out[n++] = x->c; break;
1519 case DSK_IADD3_I: case DSK_LOP3_I: out[n++] = x->a; out[n++] = x->c; break;
1520 case DSK_SHL_I: out[n++] = x->a; break;
1521 case DSK_SHL_R: out[n++] = x->a; out[n++] = x->b; break;
1522 case DSK_SHR_I: out[n++] = x->c; break;
1523 case DSK_SHR_R: out[n++] = x->b; out[n++] = x->c; break;
1524 case DSK_ISETP_R: case DSK_SEL_R: out[n++] = x->a; out[n++] = x->b; break;
1525 case DSK_ISETP_I: case DSK_SEL_I: out[n++] = x->a; break;
1526 case DSK_I2FP: out[n++] = x->b; break;
1527 case DSK_FADD_R: case DSK_FMUL_R: out[n++] = x->a; out[n++] = x->b; break;
1528 case DSK_FFMA_R: out[n++] = x->a; out[n++] = x->b; out[n++] = x->c; break;
1529 }
1530 return n;
1531}
1532
1533/* Each check is one line ending in its CHECK: marker, so the mutation sweep
1534 * can delete exactly that check. */
1535#define E_OP OMEGA_NUMERIC_ERR_OPERANDS
1536int omega_ds_check_kernel(OmegaDsOp op, const uint8_t *code, size_t len,
1537 uint32_t gpr_count, char *err, size_t err_len) {
1538 if (err && err_len) err[0] = '\0';
1539 if ((unsigned)op >= OMEGA_DS_OP_COUNT) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "ds_op: unknown op %d", (int)op); /* CHECK:ds_op */
1540 if (!code) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "ds_len: no code");
1541 bool bad_len = len % 128 != 0 || len > OMEGA_DS_MAX_CODE_BYTES || len < 16 * (OMEGA_DS_PROLOGUE_INSNS + OMEGA_DS_EPILOGUE_INSNS + 1);
1542 if (bad_len) return refuse(err, err_len, E_OP, "ds_len: kernel length %zu", len); /* CHECK:ds_len */
1543 uint8_t va[OMEGA_BW_VECADD_CODE_SIZE];
1544 size_t vl = 0;
1545 if (omega_blackwell_encode_vecadd(va, sizeof(va), &vl) != 0) return refuse(err, err_len, E_OP, "ds_prologue: vecadd encoder failed");
1546 if (memcmp(code, va, 16 * OMEGA_DS_PROLOGUE_INSNS) != 0) return refuse(err, err_len, E_OP, "ds_prologue: prologue differs from the vecadd words"); /* CHECK:ds_prologue */
1547 /* the body ends at the first STG after the prologue */
1548 size_t n = len / 16, stg = n;
1549 for (size_t k = OMEGA_DS_PROLOGUE_INSNS; k < n && stg == n; k++)
1550 if (memcmp(code + 16 * k, va + 16 * VA_STG, 16) == 0) stg = k;
1551 bool no_tail = stg + 3 > n || memcmp(code + 16 * (stg + 1), va + 16 * VA_EXIT, 16) != 0 || memcmp(code + 16 * (stg + 2), va + 16 * VA_BRA, 16) != 0;
1552 if (no_tail) return refuse(err, err_len, E_OP, "ds_epilogue: STG, EXIT, BRA not found after the body"); /* CHECK:ds_epilogue */
1553 size_t nb = stg - OMEGA_DS_PROLOGUE_INSNS;
1554 if (nb == 0 || nb > OMEGA_DS_MAX_BODY) return refuse(err, err_len, E_OP, "ds_body: body of %zu instructions", nb); /* CHECK:ds_body */
1555 size_t bad_pad = 0;
1556 for (size_t k = stg + 3; k < n && !bad_pad; k++)
1557 if (memcmp(code + 16 * k, va + 16 * VA_NOP, 16) != 0) bad_pad = k;
1558 if (bad_pad) return refuse(err, err_len, E_OP, "ds_pad: word %zu after BRA is not NOP", bad_pad); /* CHECK:ds_pad */
1559 bool defr[256] = { false }, defp[8] = { false };
1560 defr[OMEGA_DS_RZ] = defr[2] = defr[5] = true;
1561 defp[OMEGA_DS_PT] = true;
1562 unsigned maxreg = 0;
1563 OmegaDsInsn last;
1564 memset(&last, 0, sizeof(last));
1565 last.kind = DSK_ISETP_R;
1566 for (size_t i = 0; i < nb; i++) {
1567 uint32_t w[4];
1568 OmegaDsInsn x;
1569 uint8_t s[3];
1570 get_words(code + 16 * (OMEGA_DS_PROLOGUE_INSNS + i), w);
1571 if (((w[0] >> 12) & 0xfu) != 0x7u) return refuse(err, err_len, E_OP, "ds_guard: body insn %zu is predicated", i); /* CHECK:ds_guard */
1572 if (w[3] != OMEGA_DS_BODY_CTRL) return refuse(err, err_len, E_OP, "ds_ctrl: body insn %zu control 0x%08x", i, w[3]); /* CHECK:ds_ctrl */
1573 memset(&x, 0, sizeof(x));
1574 int dec = omega_ds_decode(w, &x); /* decode kept off the CHECK line so deleting the check leaves x initialized */
1575 if (dec != 0) return refuse(err, err_len, E_OP, "ds_forms: body insn %zu is not a recorded form", i); /* CHECK:ds_forms */
1576 int ns = src_regs(&x, s);
1577 for (int j = 0; j < ns; j++) {
1578 if (!defr[s[j]]) return refuse(err, err_len, E_OP, "ds_def_use: body insn %zu reads R%u before any write", i, (unsigned)s[j]); /* CHECK:ds_def_use */
1579 if (s[j] != OMEGA_DS_RZ && s[j] > maxreg) maxreg = s[j];
1580 }
1581 bool sel = x.kind == DSK_SEL_R || x.kind == DSK_SEL_I;
1582 if (sel && !defp[x.ps]) return refuse(err, err_len, E_OP, "ds_pred_def_use: body insn %zu reads P%u before any write", i, (unsigned)x.ps); /* CHECK:ds_pred_def_use */
1583 bool wr = writes_reg(&x);
1584 if (wr && (x.d == 1 || x.d == 6 || x.d == 7)) return refuse(err, err_len, E_OP, "ds_reserved: body insn %zu writes R%u", i, (unsigned)x.d); /* CHECK:ds_reserved */
1585 if (wr) { defr[x.d] = true; if (x.d != OMEGA_DS_RZ && x.d > maxreg) maxreg = x.d; }
1586 else defp[x.d] = true;
1587 last = x;
1588 }
1589 if (maxreg + 1 + OMEGA_DS_GPR_RESERVED > gpr_count || gpr_count > 255) return refuse(err, err_len, E_OP, "ds_gpr: R%u used, %u registers allocated (top %u reserved)", maxreg, gpr_count, OMEGA_DS_GPR_RESERVED); /* CHECK:ds_gpr */
1590 if (!writes_reg(&last) || last.d != OMEGA_DS_RESULT_REG) return refuse(err, err_len, E_OP, "ds_result: last body insn does not write R9"); /* CHECK:ds_result */
1591 return OMEGA_NUMERIC_OK;
1592}
1593
1594int omega_ds_check_digest(OmegaDsOp op, const uint8_t *code, size_t len, char *err, size_t err_len) {
1595 uint8_t dg[32];
1596 char hex[65];
1597 if ((unsigned)op >= OMEGA_DS_OP_COUNT || !code) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "ds_digest: bad op or code");
1598 if (omega_blackwell_compute_code_digest(code, len, dg) != 0) return refuse(err, err_len, E_OP, "ds_digest: digest failed");
1599 for (int i = 0; i < 32; i++) sprintf(hex + 2 * i, "%02x", dg[i]);
1600 if (strcmp(hex, OMEGA_DS_KERNEL_SHA256[op]) != 0) return refuse(err, err_len, E_OP, "ds_digest: %s kernel %s is not the nvdisasm-verified one", omega_ds_op_name(op), hex); /* CHECK:ds_digest */
1601 return OMEGA_NUMERIC_OK;
1602}
1603
1604int omega_ds_check_args(OmegaDsOp op, const uint32_t *a, const uint32_t *b,
1605 const uint32_t *out, size_t count, char *err, size_t err_len) {
1606 if (err && err_len) err[0] = '\0';
1607 if ((unsigned)op >= OMEGA_DS_OP_COUNT) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "ds_args_op: unknown op %d", (int)op); /* CHECK:ds_args_op */
1608 if (!a || !out || (op == OMEGA_DS_DIV && !b)) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "ds_buffers: missing buffer"); /* CHECK:ds_buffers */
1609 if (count == 0 || count > OMEGA_DS_MAX_BATCH) return refuse(err, err_len, E_OP, "ds_count: count %zu outside 1..%u", count, OMEGA_DS_MAX_BATCH); /* CHECK:ds_count */
1610 return OMEGA_NUMERIC_OK;
1611}
1612
1613int omega_ds_check_qmd(const uint32_t *qmd1, uint64_t code_va, char *err, size_t err_len) {
1614 if (!qmd1) return refuse(err, err_len, OMEGA_NUMERIC_ERR_BAD_ARGS, "ds_qmd: no QMD");
1615 if (omega_blackwell_verify_qmd_invariants(qmd1) != 0) return refuse(err, err_len, E_OP, "ds_qmd: QMD invariants fail"); /* CHECK:ds_qmd */
1616 uint32_t gpr = (qmd1[35] >> 8) & 0x1ffu;
1617 if (gpr != OMEGA_DS_GPR_COUNT) return refuse(err, err_len, E_OP, "ds_qmd_gpr: QMD allocates %u registers, kernel needs %u", gpr, OMEGA_DS_GPR_COUNT); /* CHECK:ds_qmd_gpr */
1618 if (qmd1[32] != (uint32_t)(code_va >> 4) || (qmd1[33] & 0x1fffffu) != (uint32_t)((code_va >> 36) & 0x1fffffu)) return refuse(err, err_len, E_OP, "ds_qmd_code: QMD program address is not the kernel"); /* CHECK:ds_qmd_code */
1619 return OMEGA_NUMERIC_OK;
1620}
1621#undef E_OP
1622
1623/* ---- Executor ------------------------------------------------------------------ */
1624
1625#ifndef OMEGA_NUMERIC_CPU_ONLY
1626/* Device-failure diagnostics (M20 GB10 instrumentation). Every
1627 * OMEGA_NUMERIC_ERR_DEVICE return below goes through gb10_devfail, which
1628 * prints one GB10_DEVFAIL line to stderr: the step that failed, the driver
1629 * return code, errno, the nvrm error text, the wait value and the marker word
1630 * for waits, and the elapsed ms since the launch began. The lifecycle wrapper
1631 * retains resources after uncertain completion and refuses later numeric
1632 * launches. There is no retry and no wait value changes. */
1633static double gb10_ms_since(const struct timespec *t0) {
1634 struct timespec t;
1635 timespec_get(&t, TIME_UTC); /* C11; no feature macro needed */
1636 return (double)(t.tv_sec - t0->tv_sec) * 1e3 + (double)(t.tv_nsec - t0->tv_nsec) / 1e6;
1637}
1638
1639static int gb10_devfail(const char *fn, const char *step, M16NativeContext *ctx, int do_close, int drv_rc,
1640 int saved_errno, long wait_ms, const volatile uint32_t *word, uint32_t want,
1641 const struct timespec *t0) {
1642 double ms = gb10_ms_since(t0);
1643 fprintf(stderr, "GB10_DEVFAIL fn=%s step=%s drv_rc=%d errno=%d rm_err=\"%s\" live_allocs=%u faulted=%u",
1644 fn, step, drv_rc, saved_errno, ctx->rm.err, (unsigned)ctx->rm.live_count, (unsigned)ctx->rm.faulted);
1645 if (wait_ms >= 0)
1646 fprintf(stderr, " wait_ms=%ld word=0x%08x want=0x%08x", wait_ms, word ? (unsigned)*word : 0u, (unsigned)want);
1647 fprintf(stderr, " elapsed_ms=%.3f\n", ms);
1648 if (do_close) omega_numeric_native_close(ctx);
1649 return OMEGA_NUMERIC_ERR_DEVICE;
1650}
1651/* Plain step: drv_rc is the value the call returned. */
1652#define GB10_FAIL(step, close_, rc_) gb10_devfail(__func__, (step), &ctx, (close_), (rc_), errno, -1, NULL, 0u, &t0)
1653/* Wait step: also log the wait value and the word read. */
1654#define GB10_FAIL_WAIT(step, rc_, ms_, w_, want_) \
1655 gb10_devfail(__func__, (step), &ctx, 1, (rc_), errno, (long)(ms_), (w_), (want_), &t0)
1656#endif
1657
1658int omega_ds_gb10_run(OmegaDsOp op, const uint32_t *a, const uint32_t *b, uint32_t *out, size_t count) {
1659 char err[256];
1660 int rc = omega_ds_check_args(op, a, b, out, count, err, sizeof(err));
1661 if (rc != OMEGA_NUMERIC_OK) { fprintf(stderr, "omega_ds_gb10_run: refused: %s\n", err); return rc; }
1662 uint8_t code[OMEGA_DS_MAX_CODE_BYTES];
1663 size_t code_len = 0;
1664 if (omega_ds_build_kernel(op, code, sizeof(code), &code_len) != 0) return OMEGA_NUMERIC_ERR_OPERANDS;
1665 if ((rc = omega_ds_check_kernel(op, code, code_len, OMEGA_DS_GPR_COUNT, err, sizeof(err))) != OMEGA_NUMERIC_OK ||
1666 (rc = omega_ds_check_digest(op, code, code_len, err, sizeof(err))) != OMEGA_NUMERIC_OK) {
1667 fprintf(stderr, "omega_ds_gb10_run: refused: %s\n", err);
1668 return rc;
1669 }
1670 {
1671 /* the QMD as it will be built, checked before any device work */
1672 uint32_t q[OMEGA_BW_QMD_WORDS];
1673 OmegaBlackwellQmdConfig c = { .code_va = 0x200000000ull, .cbank_va = 0x200100000ull,
1674 .scratch_va = 0x200204000ull, .sem_va = 0x200202000ull,
1675 .qmd0_va = 0x200200000ull, .qmd1_va = 0x200201000ull,
1676 .num_elements = (uint32_t)count, .gpr_count = OMEGA_DS_GPR_COUNT };
1677 omega_numeric_launch_shape(count, &c.threads_per_block, &c.grid_width);
1678 if (omega_blackwell_build_qmd1(q, &c) != 0 ||
1679 (rc = omega_ds_check_qmd(q, c.code_va, err, sizeof(err))) != OMEGA_NUMERIC_OK) {
1680 fprintf(stderr, "omega_ds_gb10_run: refused: %s\n", err);
1681 return rc ? rc : OMEGA_NUMERIC_ERR_OPERANDS;
1682 }
1683 }
1684#ifdef OMEGA_NUMERIC_CPU_ONLY
1685 return OMEGA_NUMERIC_ERR_DEVICE;
1686#else
1687 static const uint32_t SETUP[18] = {
1688 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
1689 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
1690 0x20012559, 0x00000000,
1691 };
1692 struct timespec t0;
1693 timespec_get(&t0, TIME_UTC);
1694 M16NativeContext ctx;
1695 int drc_;
1696 if ((drc_ = omega_numeric_native_open(&ctx)) != 0) return GB10_FAIL("open", 0, drc_);
1697 if ((drc_ = m16_native_create_channel(&ctx)) != 0) return GB10_FAIL("channel", 1, drc_);
1698 NvrmMem large_pb;
1699 if ((drc_ = nvrm_alloc(&ctx.rm, 0x10000, &large_pb)) != 0) return GB10_FAIL("alloc_pb", 1, drc_);
1700 ctx.pb_mem = large_pb;
1701 size_t bytes = (count * 4 + 0xfffULL) & ~0xfffULL;
1702 NvrmMem code_mem, cbank_mem, a_mem, b_mem, out_mem, marker_mem, qmd_mem;
1703 if (nvrm_alloc(&ctx.rm, omega_gpu_code_alloc_bytes(OMEGA_DS_MAX_CODE_BYTES), &code_mem) /* + 2 KB prefetch tail */ != 0 ||
1704 nvrm_alloc(&ctx.rm, 0x1000, &cbank_mem) != 0 ||
1705 nvrm_alloc(&ctx.rm, bytes, &a_mem) != 0 ||
1706 nvrm_alloc(&ctx.rm, bytes, &b_mem) != 0 ||
1707 nvrm_alloc(&ctx.rm, bytes, &out_mem) != 0 ||
1708 nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem) != 0 ||
1709 nvrm_alloc(&ctx.rm, 0x10000, &qmd_mem) != 0) {
1710 return GB10_FAIL("alloc_buffers", 1, -1);
1711 }
1712 memcpy(a_mem.cpu, a, count * 4);
1713 if (b) memcpy(b_mem.cpu, b, count * 4); else memset(b_mem.cpu, 0, count * 4);
1714 memset(out_mem.cpu, 0x55, count * 4);
1715 memcpy(code_mem.cpu, code, code_len);
1716
1717 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
1718 omega_blackwell_build_cbank_driver(cbank_data, cbank_mem.va);
1719 cbank_data[223] = 0;
1720 uint32_t args[10] = { (uint32_t)a_mem.va, (uint32_t)(a_mem.va >> 32), (uint32_t)b_mem.va, (uint32_t)(b_mem.va >> 32),
1721 (uint32_t)out_mem.va, (uint32_t)(out_mem.va >> 32), (uint32_t)count, 0, 0, 0 };
1722 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data));
1723 memcpy((uint8_t *)cbank_mem.cpu + 0x380, args, sizeof(args));
1724
1725 uint64_t qmd0_va = qmd_mem.va, qmd1_va = qmd_mem.va + 0x1000, sem_va = qmd_mem.va + 0x2000;
1726 OmegaBlackwellQmdConfig cfg = { .code_va = code_mem.va, .cbank_va = cbank_mem.va, .scratch_va = qmd_mem.va + 0x4000,
1727 .sem_va = sem_va, .qmd0_va = qmd0_va, .qmd1_va = qmd1_va,
1728 .num_elements = (uint32_t)count, .gpr_count = OMEGA_DS_GPR_COUNT };
1729 omega_numeric_launch_shape(count, &cfg.threads_per_block, &cfg.grid_width);
1730 uint32_t qmd0[OMEGA_BW_QMD_WORDS], qmd1[OMEGA_BW_QMD_WORDS];
1731 if (omega_blackwell_build_qmd0(qmd0, qmd0_va, qmd1_va) != 0 || omega_blackwell_build_qmd1(qmd1, &cfg) != 0 ||
1732 omega_ds_check_qmd(qmd1, code_mem.va, err, sizeof(err)) != OMEGA_NUMERIC_OK) {
1733 fprintf(stderr, "omega_ds_gb10_run: %s\n", err);
1734 omega_numeric_native_close(&ctx);
1735 return OMEGA_NUMERIC_ERR_OPERANDS;
1736 }
1737 /* the exact bytes in device memory pass the structural check again */
1738 if (omega_ds_check_kernel(op, code_mem.cpu, code_len, (qmd1[35] >> 8) & 0x1ffu, err, sizeof(err)) != OMEGA_NUMERIC_OK ||
1739 omega_ds_check_digest(op, code_mem.cpu, code_len, err, sizeof(err)) != OMEGA_NUMERIC_OK) {
1740 fprintf(stderr, "omega_ds_gb10_run: %s\n", err);
1741 omega_numeric_native_close(&ctx);
1742 return OMEGA_NUMERIC_ERR_OPERANDS;
1743 }
1744 memcpy(qmd_mem.cpu, qmd0, sizeof(qmd0));
1745 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1, sizeof(qmd1));
1746 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000);
1747 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu;
1748 *hsem = 0;
1749 *hmarker = 0;
1750 *(volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10) = 0;
1751 __asm__ volatile("dsb sy" ::: "memory");
1752
1753 static uint32_t pb[1024];
1754 size_t n = 0;
1755 memcpy(&pb[n], SETUP, sizeof(SETUP)); n += 18;
1756 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(cbank_mem.va >> 32); pb[n++] = (uint32_t)cbank_mem.va;
1757 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001;
1758 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
1759 pb[n++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1760 memcpy(&pb[n], cbank_data, 224 * 4); n += 224;
1761 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); pb[n++] = (uint32_t)(cbank_mem.va + 0x380);
1762 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000028; pb[n++] = 0x00000001;
1763 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
1764 pb[n++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1765 memcpy(&pb[n], args, 10 * 4); n += 10;
1766 pb[n++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
1767 pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff);
1768 pb[n++] = (uint32_t)(qmd0_va >> 8);
1769 memcpy(&pb[n], qmd0, 96 * 4); n += 96;
1770 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va;
1771 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001;
1772 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
1773 pb[n++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
1774 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
1775 pb[n++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
1776 pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff);
1777 pb[n++] = (uint32_t)(qmd1_va >> 8);
1778 memcpy(&pb[n], qmd1, 96 * 4); n += 96;
1779 pb[n++] = nvrm_mthd(0, 0x005c, 5);
1780 pb[n++] = (uint32_t)marker_mem.va; pb[n++] = (uint32_t)(marker_mem.va >> 32);
1781 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
1782 /* C3 FIX A: after the WFI marker, flush GPU L2 dirty lines to memory
1783 * (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27 OPERATION = L2_FLUSH_DIRTY 0x10;
1784 * third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc/class/clc96f.h:36-73),
1785 * then a second WFI marker that the host waits for before the readback. */
1786 pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27);
1787 pb[n++] = nvrm_mthd(0, 0x005c, 5);
1788 pb[n++] = (uint32_t)(marker_mem.va + 0x10); pb[n++] = (uint32_t)((marker_mem.va + 0x10) >> 32);
1789 pb[n++] = 0x46464646u; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
1790
1791 if ((drc_ = m16_native_submit_methods(&ctx, pb, n)) != 0) return GB10_FAIL("submit", 1, drc_);
1792 /* A timeout is uncertain completion, even after a long wait. */
1793 if ((drc_ = omega_numeric_native_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, 600000)) != 0) {
1794 return GB10_FAIL_WAIT("marker_wait", drc_, 600000, hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD);
1795 }
1796 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10);
1797 if ((drc_ = omega_numeric_native_wait(hmarker2, 0x46464646u, 600000)) != 0) {
1798 return GB10_FAIL_WAIT("marker2_wait", drc_, 600000, hmarker2, 0x46464646u);
1799 }
1800 /* The host marker can land before the last CTAs' stores are visible: the
1801 * first chip run (receipt 88930d2f...) read the 0x55 fill pattern for
1802 * 49,359,680 SQRT outputs. Read only after the QMD's own release
1803 * semaphore (written after the grid completes, with its membar) is 6. */
1804 if ((drc_ = omega_numeric_native_wait(hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE, 600000)) != 0) {
1805 return GB10_FAIL_WAIT("sem_wait", drc_, 600000, hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE);
1806 }
1807 __asm__ volatile("dsb sy" ::: "memory");
1808 memcpy(out, out_mem.cpu, count * 4);
1809 if (omega_numeric_native_close(&ctx) != 0) return OMEGA_NUMERIC_ERR_DEVICE;
1810 return OMEGA_NUMERIC_OK;
1811#endif
1812}