CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / om / scan / scan_ir.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note scan_ir.om: the CUDA++ scan and radix-sort kernels (row C3 of docs/OMEGA.md) for the GB10 (sm_121), emitted as |
| 3 | note BlackwellIR records. Kinds 0..4 reproduce the C generator gen() of src/omega_gpu_scan.c record for record |
| 4 | note (the C stays as the migration-pending differential oracle, CUDAPP_MIGRATION_PENDING_C_SCAN): |
| 5 | note kind 0/1/2 one reduction level (sum/max/min, f32), 3 one f32 tile-scan level, 4 the f32 carry pass, |
| 6 | note 5 one u32 tile-scan level, 6 the u32 carry pass, 7 radix-sort digit histogram, 8 radix-sort scatter of keys, |
| 7 | note 9 radix-sort scatter of key-value pairs (values in the second half of the buffer). |
| 8 | note Arguments at constant bank 0x380: in u64, out u64, tot u64, len, tiles, warps, shift (kinds 7..9). |
| 9 | note Record (8 ulong): op, dst, src1, src2, src3 (vreg or 2^64-1 for none), ureg, imm | control << 32, |
| 10 | note uniform | p0 << 1. Control words follow src/omega_bw_em.h (emit kinds, region, tail; no pair schedule). |
| 11 | note Meta: 0 records, 1 vregs, 2 uvregs, 3 pending wait mask, 4 error, 5 closed loops, 6 open loops, |
| 12 | note 8..11 open loop starts, 16 + 2L / 17 + 2L closed loop L start / end, 64 + v vreg kind, 320 + u uvreg kind. |
| 13 | note Mutants (red checks, argument a): 1 the reduction skips d = 1, 2 the tile scans skip d = 16, 3 the carry adds |
| 14 | note S[tile] instead of S[tile - 1], 4 the scatter rank counts the element itself, 5 the scatter ranks in lane |
| 15 | note order instead of position order (unstable), 6 the histogram scan skips d = 16. |
| 16 | |
| 17 | const NONE ulong 65535 |
| 18 | const ALL ulong 18446744073709551615 |
| 19 | const LO32 ulong 4294967295 |
| 20 | const YIELD ulong 8192 |
| 21 | const NOPC ulong 1032192 |
| 22 | const PADSUM ulong 2147483648 |
| 23 | const PADMM ulong 2143289344 |
| 24 | const KFIX ulong 0 |
| 25 | const KPRED ulong 1 |
| 26 | const KVAR ulong 2 |
| 27 | const KVARM ulong 3 |
| 28 | const KUVAR ulong 4 |
| 29 | const KSTORE ulong 5 |
| 30 | const KEXIT ulong 6 |
| 31 | const KBRA ulong 7 |
| 32 | const KBSSY ulong 8 |
| 33 | const KBSYNC ulong 9 |
| 34 | const KRAW ulong 10 |
| 35 | const MOVRZ ulong 1 |
| 36 | const MOVI ulong 2 |
| 37 | const S2R ulong 3 |
| 38 | const LDC ulong 4 |
| 39 | const LDC64 ulong 5 |
| 40 | const LDCU64 ulong 7 |
| 41 | const IMAD ulong 8 |
| 42 | const IMADW ulong 9 |
| 43 | const LDG ulong 11 |
| 44 | const STG ulong 12 |
| 45 | const IADD3 ulong 14 |
| 46 | const SHFR ulong 17 |
| 47 | const LAND ulong 18 |
| 48 | const ISGEU ulong 28 |
| 49 | const FADD ulong 30 |
| 50 | const FMIN ulong 36 |
| 51 | const FMAX ulong 37 |
| 52 | const SHFLD ulong 42 |
| 53 | const EXIT ulong 45 |
| 54 | const BRA ulong 46 |
| 55 | const LUT ulong 51 |
| 56 | const SHFLU ulong 52 |
| 57 | const BSSY ulong 54 |
| 58 | const BSYNC ulong 55 |
| 59 | |
| 60 | note virtual registers, in the allocation order of gen() (uvreg 0 is the global memory descriptor) |
| 61 | const UD ulong 0 |
| 62 | const PIN ulong 0 |
| 63 | const POUT ulong 1 |
| 64 | const PTOT ulong 2 |
| 65 | const LEN ulong 3 |
| 66 | const TILES ulong 4 |
| 67 | const WARPS ulong 5 |
| 68 | const TID ulong 6 |
| 69 | const CTA ulong 7 |
| 70 | const LANE ulong 8 |
| 71 | const GW ulong 9 |
| 72 | const ONE ulong 10 |
| 73 | const TILE ulong 11 |
| 74 | const EL ulong 12 |
| 75 | const V ulong 13 |
| 76 | const T ulong 14 |
| 77 | const A64 ulong 15 |
| 78 | const B64 ulong 16 |
| 79 | const RL ulong 17 |
| 80 | const NZ ulong 18 |
| 81 | const C31 ulong 19 |
| 82 | const M1 ulong 20 |
| 83 | const OFF ulong 21 |
| 84 | const TM1 ulong 22 |
| 85 | const MASK ulong 23 |
| 86 | note the sort kernels' further registers (after the five masks) |
| 87 | const SH ulong 28 |
| 88 | const CJ1 ulong 29 |
| 89 | const CJ2 ulong 30 |
| 90 | const C3 ulong 31 |
| 91 | const KEY ulong 32 |
| 92 | const DIG ulong 33 |
| 93 | const D3 ulong 34 |
| 94 | const VAL ulong 35 |
| 95 | const Q ulong 36 |
| 96 | const X ulong 37 |
| 97 | const R0 ulong 38 |
| 98 | const MQ0 ulong 42 |
| 99 | const ACC ulong 46 |
| 100 | const VV ulong 47 |
| 101 | const DST ulong 48 |
| 102 | |
| 103 | note ---- record building |
| 104 | |
| 105 | fn cw st ulong wb ulong rb ulong wt ulong to ulong |
| 106 | ret or or shl st 9 shl wb 14 or shl rb 17 shl wt 20 |
| 107 | end |
| 108 | |
| 109 | fn fld r ulong to ulong |
| 110 | if eq r NONE |
| 111 | ret ALL |
| 112 | end |
| 113 | ret r |
| 114 | end |
| 115 | |
| 116 | fn r4 d ulong a ulong b ulong c ulong to ulong |
| 117 | ret or or d shl a 16 or shl b 32 shl c 48 |
| 118 | end |
| 119 | |
| 120 | fn okk op ulong k ulong fl ulong ur ulong to ulong |
| 121 | ret or or op shl k 8 or shl fl 16 shl ur 40 |
| 122 | end |
| 123 | |
| 124 | note control word of one record kind (src/omega_bw_em.h emit): returns control | next pending mask << 32 |
| 125 | fn kc k ulong fl ulong pend ulong imm ulong to ulong |
| 126 | if eq k KPRED |
| 127 | ret cw 14 7 7 pend |
| 128 | end |
| 129 | if eq k KVAR |
| 130 | ret or or cw 4 0 1 pend YIELD shl ulong 3 32 |
| 131 | end |
| 132 | if eq k KVARM |
| 133 | ret or or cw 4 0 1 and pend 2 YIELD shl or pend 3 32 |
| 134 | end |
| 135 | if eq k KUVAR |
| 136 | ret or or cw 4 2 7 pend YIELD shl ulong 4 32 |
| 137 | end |
| 138 | if eq k KSTORE |
| 139 | ret or or cw 4 7 1 pend YIELD shl ulong 2 32 |
| 140 | end |
| 141 | if or eq k KEXIT eq k KBRA |
| 142 | if ne and fl 2 0 |
| 143 | ret or cw 5 7 7 pend shl pend 32 |
| 144 | end |
| 145 | ret cw 5 7 7 pend |
| 146 | end |
| 147 | if eq k KBSSY |
| 148 | ret or cw 1 7 7 pend YIELD |
| 149 | end |
| 150 | if eq k KBSYNC |
| 151 | ret or cw 5 7 7 pend YIELD |
| 152 | end |
| 153 | if eq k KRAW |
| 154 | ret or shr imm 32 shl pend 32 |
| 155 | end |
| 156 | ret cw 6 7 7 pend |
| 157 | end |
| 158 | |
| 159 | note one record: ok = op | kind << 8 | flags << 16 | ureg << 40; rg = r4 operands; imm low 32 bits |
| 160 | note (a raw record carries its control word in imm bits 32..63) |
| 161 | note record words 0..5 at b: op and the operand fields of rg, ureg from ok |
| 162 | fn wops ins edit ulong 8192 b ulong ok ulong rg ulong to void |
| 163 | put ins b and ok 255 |
| 164 | put ins add b 1 fld and rg 65535 |
| 165 | put ins add b 2 fld and shr rg 16 65535 |
| 166 | put ins add b 3 fld and shr rg 32 65535 |
| 167 | put ins add b 4 fld shr rg 48 |
| 168 | put ins add b 5 fld and shr ok 40 65535 |
| 169 | end |
| 170 | |
| 171 | fn emit ins edit ulong 8192 m edit ulong 512 ok ulong rg ulong imm ulong to void |
| 172 | n = get m 0 |
| 173 | if ge n 1024 |
| 174 | put m 4 1 |
| 175 | ret |
| 176 | end |
| 177 | x = and shr ok 8 255 |
| 178 | if gt x KRAW |
| 179 | put m 4 2 |
| 180 | ret |
| 181 | end |
| 182 | x = kc x and shr ok 16 255 get m 3 imm |
| 183 | put m 0 add n 1 |
| 184 | n = mul n 8 |
| 185 | wops ins n ok rg |
| 186 | put ins add n 6 or and imm LO32 shl and x LO32 32 |
| 187 | put ins add n 7 and shr ok 16 7 |
| 188 | put m 3 shr x 32 |
| 189 | end |
| 190 | |
| 191 | fn nv m edit ulong 512 kind ulong to void |
| 192 | v = get m 1 |
| 193 | if ge v 256 |
| 194 | put m 4 3 |
| 195 | ret |
| 196 | end |
| 197 | put m add 64 v kind |
| 198 | put m 1 add v 1 |
| 199 | end |
| 200 | |
| 201 | note ---- instruction helpers (the names of src/omega_bw_em.h) |
| 202 | |
| 203 | fn fix ins edit ulong 8192 m edit ulong 512 op ulong rg ulong imm ulong to void |
| 204 | emit ins m okk op KFIX 0 NONE rg imm |
| 205 | end |
| 206 | |
| 207 | fn movi ins edit ulong 8192 m edit ulong 512 d ulong imm ulong to void |
| 208 | fix ins m MOVI r4 d NONE NONE NONE imm |
| 209 | end |
| 210 | |
| 211 | fn movrz ins edit ulong 8192 m edit ulong 512 d ulong to void |
| 212 | fix ins m MOVRZ r4 d NONE NONE NONE 0 |
| 213 | end |
| 214 | |
| 215 | note d = a * imm + c |
| 216 | fn imadi ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong c ulong to void |
| 217 | fix ins m IMAD r4 d a NONE c imm |
| 218 | end |
| 219 | |
| 220 | note d = a * b + c |
| 221 | fn imadr ins edit ulong 8192 m edit ulong 512 d ulong a ulong b ulong c ulong to void |
| 222 | fix ins m IMAD r4 d a b c 0 |
| 223 | end |
| 224 | |
| 225 | note d:d+1 = a * imm + c:c+1 |
| 226 | fn imadw ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong c ulong to void |
| 227 | fix ins m IMADW r4 d a NONE c imm |
| 228 | end |
| 229 | |
| 230 | fn shri ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong to void |
| 231 | fix ins m SHFR r4 d a NONE NONE imm |
| 232 | end |
| 233 | |
| 234 | fn andi ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong to void |
| 235 | fix ins m LAND r4 d a NONE NONE imm |
| 236 | end |
| 237 | |
| 238 | note d = a << b (b a register below 32) |
| 239 | fn shlr ins edit ulong 8192 m edit ulong 512 d ulong a ulong b ulong to void |
| 240 | fix ins m SHFLU r4 d a b NONE 0 |
| 241 | end |
| 242 | |
| 243 | note LOP3(a, b, c, lut) with a = 0xF0, b = 0xCC, c = 0xAA; rg = r4 d a b c (a function takes at most 6 parameters) |
| 244 | fn lop3 ins edit ulong 8192 m edit ulong 512 rg ulong f ulong to void |
| 245 | fix ins m LUT rg f |
| 246 | end |
| 247 | |
| 248 | note d = a op b for the fixed two-operand ops (IADD3, FADD, FMNMX) |
| 249 | fn two ins edit ulong 8192 m edit ulong 512 op ulong d ulong a ulong b ulong to void |
| 250 | fix ins m op r4 d a b NONE 0 |
| 251 | end |
| 252 | |
| 253 | note the variable-latency reads: op LDC, LDC64, S2R (imm offset or special register) |
| 254 | fn vimm ins edit ulong 8192 m edit ulong 512 op ulong d ulong imm ulong to void |
| 255 | emit ins m okk op KVAR 0 NONE r4 d NONE NONE NONE imm |
| 256 | end |
| 257 | |
| 258 | fn ldcu64 ins edit ulong 8192 m edit ulong 512 imm ulong to void |
| 259 | emit ins m okk LDCU64 KUVAR 1 NONE r4 UD NONE NONE NONE imm |
| 260 | end |
| 261 | |
| 262 | note d = global u32 at a:a+1; first 1 starts a load group, 0 joins it |
| 263 | fn ldg32 ins edit ulong 8192 m edit ulong 512 d ulong a ulong first ulong to void |
| 264 | k = KVARM |
| 265 | if ne first 0 |
| 266 | k = KVAR |
| 267 | end |
| 268 | emit ins m okk LDG k 0 UD r4 d a NONE NONE 0 |
| 269 | end |
| 270 | |
| 271 | fn stg ins edit ulong 8192 m edit ulong 512 a ulong imm ulong v ulong to void |
| 272 | emit ins m okk STG KSTORE 0 UD r4 NONE a v NONE imm |
| 273 | end |
| 274 | |
| 275 | fn shfl ins edit ulong 8192 m edit ulong 512 d ulong a ulong off ulong to void |
| 276 | emit ins m okk SHFLD KVAR 0 NONE r4 d a NONE NONE off |
| 277 | end |
| 278 | |
| 279 | note P0 = a >= b (unsigned) |
| 280 | fn isetp ins edit ulong 8192 m edit ulong 512 a ulong b ulong to void |
| 281 | emit ins m okk ISGEU KPRED 0 NONE r4 NONE a b NONE 0 |
| 282 | end |
| 283 | |
| 284 | note ---- branches, regions and loops |
| 285 | |
| 286 | note @P0 BRA forward; returns its record index for patch |
| 287 | fn bfwd ins edit ulong 8192 m edit ulong 512 to ulong |
| 288 | at = get m 0 |
| 289 | emit ins m okk BRA KBRA 2 NONE r4 NONE NONE NONE NONE 1 |
| 290 | ret at |
| 291 | end |
| 292 | |
| 293 | fn patch ins edit ulong 8192 at ulong tgt ulong to void |
| 294 | b = add mul at 8 6 |
| 295 | x = get ins b |
| 296 | put ins b or and x 18446744069414584320 and sub tgt at LO32 |
| 297 | end |
| 298 | |
| 299 | note a region the lanes with P0 set skip (BSSY, @P0 BRA); returns bssy | bra << 16 |
| 300 | fn rbeg ins edit ulong 8192 m edit ulong 512 to ulong |
| 301 | at = get m 0 |
| 302 | emit ins m okk BSSY KBSSY 0 NONE r4 NONE NONE NONE NONE 2 |
| 303 | b = bfwd ins m |
| 304 | ret or at shl b 16 |
| 305 | end |
| 306 | |
| 307 | fn rend ins edit ulong 8192 m edit ulong 512 r ulong to void |
| 308 | at = get m 0 |
| 309 | emit ins m okk BSYNC KBSYNC 0 NONE r4 NONE NONE NONE NONE 0 |
| 310 | patch ins shr r 16 at |
| 311 | patch ins and r 65535 add at 1 |
| 312 | end |
| 313 | |
| 314 | fn bra ins edit ulong 8192 m edit ulong 512 top ulong to void |
| 315 | emit ins m okk BRA KBRA 0 NONE r4 NONE NONE NONE NONE and sub top get m 0 LO32 |
| 316 | end |
| 317 | |
| 318 | fn tail ins edit ulong 8192 m edit ulong 512 to void |
| 319 | emit ins m okk EXIT KEXIT 0 NONE r4 NONE NONE NONE NONE 0 |
| 320 | emit ins m okk BRA KRAW 0 NONE r4 NONE NONE NONE NONE shl NOPC 32 |
| 321 | end |
| 322 | |
| 323 | fn lbeg m edit ulong 512 to void |
| 324 | d = get m 6 |
| 325 | if ge d 4 |
| 326 | put m 4 4 |
| 327 | ret |
| 328 | end |
| 329 | put m add 8 d get m 0 |
| 330 | put m 6 add d 1 |
| 331 | end |
| 332 | |
| 333 | fn lend m edit ulong 512 to void |
| 334 | d = get m 6 |
| 335 | nl = get m 5 |
| 336 | if or eq d 0 ge nl 24 |
| 337 | put m 4 5 |
| 338 | ret |
| 339 | end |
| 340 | d1 = sub d 1 |
| 341 | put m add 16 mul nl 2 get m add 8 d1 |
| 342 | put m add 17 mul nl 2 sub get m 0 1 |
| 343 | put m 5 add nl 1 |
| 344 | put m 6 d1 |
| 345 | end |
| 346 | |
| 347 | note ---- the kernels |
| 348 | |
| 349 | fn istile k ulong to bool |
| 350 | ret or eq k 3 eq k 5 |
| 351 | end |
| 352 | |
| 353 | fn issort k ulong to bool |
| 354 | ret ge k 7 |
| 355 | end |
| 356 | |
| 357 | note the registers of gen(), then (sort) SH .. DST |
| 358 | fn regs m edit ulong 512 k ulong to void |
| 359 | u = get m 2 |
| 360 | put m add 320 u 2 |
| 361 | put m 2 add u 1 |
| 362 | nv m 2 |
| 363 | nv m 2 |
| 364 | nv m 2 |
| 365 | for i ulong 12 |
| 366 | nv m 1 |
| 367 | end |
| 368 | nv m 2 |
| 369 | nv m 2 |
| 370 | for i ulong 6 |
| 371 | nv m 1 |
| 372 | end |
| 373 | if or istile k issort k |
| 374 | for i ulong 5 |
| 375 | nv m 1 |
| 376 | end |
| 377 | end |
| 378 | if issort k |
| 379 | for i ulong 21 |
| 380 | nv m 1 |
| 381 | end |
| 382 | end |
| 383 | end |
| 384 | |
| 385 | fn prolog ins edit ulong 8192 m edit ulong 512 k ulong to void |
| 386 | ldcu64 ins m 856 |
| 387 | vimm ins m LDC64 PIN 896 |
| 388 | if ne k 7 |
| 389 | vimm ins m LDC64 POUT 904 |
| 390 | end |
| 391 | if or istile k issort k |
| 392 | vimm ins m LDC64 PTOT 912 |
| 393 | end |
| 394 | vimm ins m LDC LEN 920 |
| 395 | vimm ins m LDC TILES 924 |
| 396 | vimm ins m LDC WARPS 928 |
| 397 | if issort k |
| 398 | vimm ins m LDC SH 932 |
| 399 | end |
| 400 | vimm ins m S2R TID 33 |
| 401 | vimm ins m S2R CTA 37 |
| 402 | movi ins m ONE 1 |
| 403 | movi ins m M1 LO32 |
| 404 | if or eq k 5 eq k 6 |
| 405 | movi ins m NZ 0 |
| 406 | else |
| 407 | movi ins m NZ PADSUM |
| 408 | end |
| 409 | andi ins m LANE TID 31 |
| 410 | shri ins m T TID 5 |
| 411 | imadi ins m GW CTA 8 T |
| 412 | if or eq k 4 eq k 6 |
| 413 | imadi ins m TILE GW 1 ONE |
| 414 | else |
| 415 | imadi ins m TILE GW 1 NONE |
| 416 | end |
| 417 | if or istile k issort k |
| 418 | movi ins m C31 31 |
| 419 | lop3 ins m r4 RL LANE C31 NONE 60 |
| 420 | for s ulong 5 |
| 421 | movi ins m T shl 1 s |
| 422 | two ins m IADD3 add MASK s LANE T |
| 423 | shri ins m add MASK s add MASK s 5 |
| 424 | imadi ins m add MASK s add MASK s 1 M1 |
| 425 | end |
| 426 | end |
| 427 | if issort k |
| 428 | movi ins m CJ1 1 |
| 429 | movi ins m CJ2 2 |
| 430 | movi ins m C3 3 |
| 431 | end |
| 432 | end |
| 433 | |
| 434 | fn red ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void |
| 435 | imadi ins m EL TILE 32 LANE |
| 436 | if eq k 0 |
| 437 | movi ins m V PADSUM |
| 438 | else |
| 439 | movi ins m V PADMM |
| 440 | end |
| 441 | isetp ins m EL LEN |
| 442 | r = rbeg ins m |
| 443 | imadw ins m A64 EL 4 PIN |
| 444 | ldg32 ins m V A64 1 |
| 445 | rend ins m r |
| 446 | op = FADD |
| 447 | if eq k 1 |
| 448 | op = FMAX |
| 449 | end |
| 450 | if eq k 2 |
| 451 | op = FMIN |
| 452 | end |
| 453 | d = ulong 16 |
| 454 | for i ulong 5 |
| 455 | if or ne mut 1 ne d 1 |
| 456 | shfl ins m T V d |
| 457 | two ins m op V V T |
| 458 | end |
| 459 | d = shr d 1 |
| 460 | end |
| 461 | isetp ins m LANE ONE |
| 462 | r = rbeg ins m |
| 463 | imadw ins m A64 TILE 4 POUT |
| 464 | stg ins m A64 0 V |
| 465 | rend ins m r |
| 466 | end |
| 467 | |
| 468 | fn tilek ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void |
| 469 | op = FADD |
| 470 | pad = PADSUM |
| 471 | if eq k 5 |
| 472 | op = IADD3 |
| 473 | pad = ulong 0 |
| 474 | end |
| 475 | imadi ins m EL TILE 32 RL |
| 476 | movi ins m V pad |
| 477 | isetp ins m EL LEN |
| 478 | r = rbeg ins m |
| 479 | imadw ins m A64 EL 4 PIN |
| 480 | ldg32 ins m V A64 1 |
| 481 | rend ins m r |
| 482 | for s ulong 5 |
| 483 | if or ne mut 2 ne s 4 |
| 484 | shfl ins m T V shl 1 s |
| 485 | lop3 ins m r4 T T add MASK s NZ 226 |
| 486 | two ins m op V T V |
| 487 | end |
| 488 | end |
| 489 | isetp ins m EL LEN |
| 490 | r = rbeg ins m |
| 491 | imadw ins m A64 EL 4 POUT |
| 492 | stg ins m A64 0 V |
| 493 | rend ins m r |
| 494 | isetp ins m LANE ONE |
| 495 | r = rbeg ins m |
| 496 | imadw ins m A64 TILE 4 PTOT |
| 497 | stg ins m A64 0 V |
| 498 | rend ins m r |
| 499 | end |
| 500 | |
| 501 | fn addk ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void |
| 502 | op = FADD |
| 503 | if eq k 6 |
| 504 | op = IADD3 |
| 505 | end |
| 506 | imadi ins m EL TILE 32 LANE |
| 507 | isetp ins m EL LEN |
| 508 | r = rbeg ins m |
| 509 | if eq mut 3 |
| 510 | imadi ins m TM1 TILE 1 NONE |
| 511 | else |
| 512 | imadi ins m TM1 TILE 1 M1 |
| 513 | end |
| 514 | imadw ins m B64 TM1 4 POUT |
| 515 | imadw ins m A64 EL 4 PIN |
| 516 | ldg32 ins m OFF B64 1 |
| 517 | ldg32 ins m V A64 0 |
| 518 | two ins m op V OFF V |
| 519 | stg ins m A64 0 V |
| 520 | rend ins m r |
| 521 | end |
| 522 | |
| 523 | note the digit of KEY (bits shift..shift+3 counted from the top: (KEY << SH) >> 28), its byte one-hot VAL in the |
| 524 | note counter word Q = digit >> 2, and MQj = all ones when Q == j: (((Q ^ j) + 3) >> 2) - 1 |
| 525 | fn digit ins edit ulong 8192 m edit ulong 512 to void |
| 526 | shlr ins m X KEY SH |
| 527 | shri ins m DIG X 28 |
| 528 | andi ins m D3 DIG 3 |
| 529 | imadi ins m X D3 8 NONE |
| 530 | shlr ins m VAL ONE X |
| 531 | shri ins m Q DIG 2 |
| 532 | for j ulong 4 |
| 533 | if eq j 0 |
| 534 | two ins m IADD3 X Q C3 |
| 535 | else |
| 536 | lop3 ins m r4 X Q add CJ1 sub j 1 NONE 60 |
| 537 | two ins m IADD3 X X C3 |
| 538 | end |
| 539 | shri ins m X X 2 |
| 540 | imadi ins m add MQ0 j X 1 M1 |
| 541 | lop3 ins m r4 add R0 j VAL add MQ0 j NONE 192 |
| 542 | end |
| 543 | end |
| 544 | |
| 545 | note per lane: the four counter words R0..R3 (16 digit counters of 8 bits) summed over the tile positions up to |
| 546 | note this lane's (an inclusive scan in reversed lanes, as the tile scan); lane 0 ends with the tile's totals |
| 547 | fn count ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void |
| 548 | if and ne k 7 eq mut 5 |
| 549 | imadi ins m EL TILE 32 LANE |
| 550 | else |
| 551 | imadi ins m EL TILE 32 RL |
| 552 | end |
| 553 | for j ulong 4 |
| 554 | movrz ins m add R0 j |
| 555 | end |
| 556 | isetp ins m EL LEN |
| 557 | r = rbeg ins m |
| 558 | imadw ins m A64 EL 4 PIN |
| 559 | ldg32 ins m KEY A64 1 |
| 560 | digit ins m |
| 561 | rend ins m r |
| 562 | for s ulong 5 |
| 563 | if or or ne k 7 ne mut 6 ne s 4 |
| 564 | for j ulong 4 |
| 565 | shfl ins m T add R0 j shl 1 s |
| 566 | lop3 ins m r4 T T add MASK s NONE 192 |
| 567 | two ins m IADD3 add R0 j T add R0 j |
| 568 | end |
| 569 | end |
| 570 | end |
| 571 | end |
| 572 | |
| 573 | note lane 0 stores the 16 tile counts digit-major: cnt[1 + dig * tiles + tile] (cnt = PTOT), and cnt[0] = 0 |
| 574 | note (every tile's lane 0 stores the same zero, so the exclusive prefix the scatter reads starts at 0) |
| 575 | fn hist ins edit ulong 8192 m edit ulong 512 to void |
| 576 | isetp ins m LANE ONE |
| 577 | r = rbeg ins m |
| 578 | for dg ulong 16 |
| 579 | src = add R0 shr dg 2 |
| 580 | b = mul and dg 3 8 |
| 581 | if eq b 0 |
| 582 | andi ins m X src 255 |
| 583 | else |
| 584 | shri ins m X src b |
| 585 | andi ins m X X 255 |
| 586 | end |
| 587 | imadi ins m DST TILES dg TILE |
| 588 | imadw ins m A64 DST 4 PTOT |
| 589 | stg ins m A64 4 X |
| 590 | end |
| 591 | movrz ins m X |
| 592 | stg ins m PTOT 0 X |
| 593 | rend ins m r |
| 594 | end |
| 595 | |
| 596 | note out[cnt[dig * tiles + tile] + rank] = key, rank = the earlier keys of the tile with the same digit |
| 597 | fn scat ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void |
| 598 | isetp ins m EL LEN |
| 599 | r = rbeg ins m |
| 600 | lop3 ins m r4 ACC R0 MQ0 NONE 192 |
| 601 | for j ulong 3 |
| 602 | lop3 ins m r4 ACC add R0 add j 1 add MQ0 add j 1 ACC 234 |
| 603 | end |
| 604 | lop3 ins m r4 X D3 C3 NONE 60 |
| 605 | imadi ins m X X 8 NONE |
| 606 | shlr ins m X ACC X |
| 607 | shri ins m X X 24 |
| 608 | if eq mut 4 |
| 609 | imadi ins m X X 1 NONE |
| 610 | else |
| 611 | imadi ins m X X 1 M1 |
| 612 | end |
| 613 | imadr ins m DST DIG TILES TILE |
| 614 | imadw ins m A64 DST 4 PTOT |
| 615 | if eq k 9 |
| 616 | two ins m IADD3 T EL LEN |
| 617 | imadw ins m B64 T 4 PIN |
| 618 | end |
| 619 | ldg32 ins m DST A64 1 |
| 620 | if eq k 9 |
| 621 | ldg32 ins m VV B64 0 |
| 622 | end |
| 623 | two ins m IADD3 DST DST X |
| 624 | imadw ins m A64 DST 4 POUT |
| 625 | stg ins m A64 0 KEY |
| 626 | if eq k 9 |
| 627 | two ins m IADD3 T DST LEN |
| 628 | imadw ins m B64 T 4 POUT |
| 629 | stg ins m B64 0 VV |
| 630 | end |
| 631 | rend ins m r |
| 632 | end |
| 633 | |
| 634 | fn kern ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void |
| 635 | regs m k |
| 636 | prolog ins m k |
| 637 | top = get m 0 |
| 638 | lbeg m |
| 639 | isetp ins m TILE TILES |
| 640 | done = bfwd ins m |
| 641 | if le k 2 |
| 642 | red ins m k mut |
| 643 | end |
| 644 | if istile k |
| 645 | tilek ins m k mut |
| 646 | end |
| 647 | if or eq k 4 eq k 6 |
| 648 | addk ins m k mut |
| 649 | end |
| 650 | if issort k |
| 651 | count ins m k mut |
| 652 | if eq k 7 |
| 653 | hist ins m |
| 654 | else |
| 655 | scat ins m k mut |
| 656 | end |
| 657 | end |
| 658 | two ins m IADD3 TILE TILE WARPS |
| 659 | bra ins m top |
| 660 | lend m |
| 661 | patch ins done get m 0 |
| 662 | tail ins m |
| 663 | end |
| 664 | |
| 665 | note ---- entry: kind 0..9, a mutant 0..6 (b, c 0). Returns 0 built, 1 refused arguments, 2 emitter error |
| 666 | note (meta 4 holds the code). |
| 667 | fn scan_ir kind ulong a ulong b ulong c ulong ins edit ulong 8192 meta edit ulong 512 to ulong |
| 668 | for i ulong 512 |
| 669 | put meta i 0 |
| 670 | end |
| 671 | if or or gt kind 9 gt a 6 or ne b 0 ne c 0 |
| 672 | ret 1 |
| 673 | end |
| 674 | kern ins meta kind a |
| 675 | if ne get meta 4 0 |
| 676 | ret 2 |
| 677 | end |
| 678 | if ne get meta 6 0 |
| 679 | ret 2 |
| 680 | end |
| 681 | ret 0 |
| 682 | end |