omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / gpu / nvidia / om / mathlibs / mathlibs_ir.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note mathlibs_ir.om: CUDA++ math-library kernels for the GB10 (sm_121), emitted as BlackwellIR records. |
| 3 | note kind 0: xoshiro256** random numbers (variant 0 raw 64-bit, 1 uniform f32, 2 normal f32). |
| 4 | note kind 1: batched complex f32 Stockham FFT, n = 2^a (4..2048), radix 4 plus one radix-2 stage when a is odd. |
| 5 | note kind 2: the FFT twiddle table w^m, m < n, n = 2^a (4..4096), as f32 pairs (re bits | im bits << 32), in f64. |
| 6 | note kind 3: Philox4x32-10 counter-based random numbers, the stream of cuRAND's curandStatePhilox4_32_10_t. |
| 7 | note kind 4: real-FFT split (R2C, after an N/2-point complex FFT) or merge (C2R, before it), N = 2^a (8..4096). |
| 8 | note kind 5: complex f32 transpose [batch][2^a][2^b] -> [batch][2^b][2^a]. |
| 9 | note kind 6: the pass plan (data) of an R2C, C2R or 1-, 2-, 3-D complex transform over kinds 1, 4 and 5. |
| 10 | note kinds 7..13: any-length FFT for cufftPlanMany: mixed-radix (2, 3, 4, 5, 7) Stockham passes, Bluestein steps, |
| 11 | note strided gather / scatter copy, and their data (twiddles of any n, chirp, factor plan, magic divisor). |
| 12 | note The algorithms are the public-domain xoshiro256** and SplitMix64 (Blackman and Vigna), Philox4x32-10 |
| 13 | note (Salmon, Moraes, Dror and Shaw, SC'11) and the Stockham FFT; the xoshiro stream matches aien-math |
| 14 | note src/stats/random.om bit for bit and the Philox stream the Random123 known answers (fixtures in tests/). |
| 15 | note Record (8 ulong): op, dst, src1, src2, src3 (vreg or 2^64-1 for none), ureg, imm | control << 32, |
| 16 | note uniform | p0 << 1 | not << 2 | dsub << 16 | s1sub << 24 | s2sub << 32 | s3sub << 40. |
| 17 | note The records replay through src/omega_gpu_om_ir.c (C register allocator, migration-pending) and |
| 18 | note encode through om/encoder/bw_enc_sm121.om. Control words follow the elementwise emitter |
| 19 | note (src/omega_gpu_elementwise_api.c: emit kinds, ld_group, tail, schedule_fixed_pairs). |
| 20 | note Meta: 0 records, 1 vregs, 2 uvregs, 3 pending wait mask, 4 error, 5 closed loops, 6 open loops, |
| 21 | note 8..11 open loop starts, 16 + 2L / 17 + 2L closed loop L start / end, 64 + v vreg kind, 320 + u uvreg kind. |
| 22 | note An operand reference is vreg | subreg << 10; NONE (65535) is an absent operand. |
| 23 | |
| 24 | const NONE ulong 65535 |
| 25 | const ALL ulong 18446744073709551615 |
| 26 | const LO32 ulong 4294967295 |
| 27 | const M1 ulong 4294967295 |
| 28 | const YIELD ulong 8192 |
| 29 | const RAWC ulong 927744 |
| 30 | const NOPC ulong 1032192 |
| 31 | const KFIX ulong 0 |
| 32 | const KPRED ulong 1 |
| 33 | const KVAR ulong 2 |
| 34 | const KUVAR ulong 3 |
| 35 | const KSTORE ulong 4 |
| 36 | const KSYNC ulong 5 |
| 37 | const KEXIT ulong 6 |
| 38 | const KBRA ulong 7 |
| 39 | const KRAW ulong 8 |
| 40 | const MOVRZ ulong 1 |
| 41 | const MOVI ulong 2 |
| 42 | const S2R ulong 3 |
| 43 | const LDC ulong 4 |
| 44 | const LDC64 ulong 5 |
| 45 | const LDCU64 ulong 7 |
| 46 | const IMAD ulong 8 |
| 47 | const IMADW ulong 9 |
| 48 | const STG ulong 12 |
| 49 | const IADD3 ulong 14 |
| 50 | const SHFR ulong 17 |
| 51 | const LAND ulong 18 |
| 52 | const ISGEU ulong 28 |
| 53 | const LXOR ulong 29 |
| 54 | const FADD ulong 30 |
| 55 | const FSUB ulong 31 |
| 56 | const FMUL ulong 32 |
| 57 | const FFMA ulong 33 |
| 58 | const I2FP ulong 38 |
| 59 | const FMAX ulong 37 |
| 60 | const MRSQ ulong 41 |
| 61 | const EXIT ulong 45 |
| 62 | const BRA ulong 46 |
| 63 | const BAR ulong 48 |
| 64 | const LDS ulong 49 |
| 65 | const STS ulong 50 |
| 66 | const LUT ulong 51 |
| 67 | const SHFL ulong 52 |
| 68 | const IMADHI ulong 53 |
| 69 | const LDG64 ulong 56 |
| 70 | const MLG2 ulong 59 |
| 71 | const C1 ulong 13787848793156543929 |
| 72 | const C2 ulong 10723151780598845931 |
| 73 | const K24 f32 5.9604644775390625e-8 |
| 74 | const K46 f32 1.4210854715202004e-14 |
| 75 | const NLN2 f32 -0.6931471805599453 |
| 76 | const RT2 f32 1.4142135623730951 |
| 77 | const F25 f32 2.5 |
| 78 | const F3 f32 3.0 |
| 79 | const F5 f32 5.0 |
| 80 | const GC0 f32 1.50140941 |
| 81 | const GC1 f32 0.246640727 |
| 82 | const GC2 f32 -0.00417768164 |
| 83 | const GC3 f32 -0.00125372503 |
| 84 | const GC4 f32 0.00021858087 |
| 85 | const GC5 f32 -0.00000439150654 |
| 86 | const GC6 f32 -0.0000035233877 |
| 87 | const GC7 f32 0.000000343273939 |
| 88 | const GC8 f32 0.0000000281022636 |
| 89 | const GCM f32 1.5 |
| 90 | const GT0 f32 2.83297682 |
| 91 | const GT1 f32 1.00167406 |
| 92 | const GT2 f32 0.00943887047 |
| 93 | const GT3 f32 -0.0076224613 |
| 94 | const GT4 f32 0.00573950773 |
| 95 | const GT5 f32 -0.00367342844 |
| 96 | const GT6 f32 0.00134934322 |
| 97 | const GT7 f32 0.000100950558 |
| 98 | const GT8 f32 -0.000200214257 |
| 99 | const HPI f64 1.5707963267948966 |
| 100 | |
| 101 | note ---- record building |
| 102 | |
| 103 | fn fb x f32 to ulong |
| 104 | ret ulong bitcast uint x |
| 105 | end |
| 106 | |
| 107 | fn cw st ulong wb ulong rb ulong wt ulong to ulong |
| 108 | ret or or shl st 9 shl wb 14 or shl rb 17 shl wt 20 |
| 109 | end |
| 110 | |
| 111 | fn fld r ulong to ulong |
| 112 | if eq r NONE |
| 113 | ret ALL |
| 114 | end |
| 115 | ret and r 1023 |
| 116 | end |
| 117 | |
| 118 | fn rsub r ulong to ulong |
| 119 | if eq r NONE |
| 120 | ret 0 |
| 121 | end |
| 122 | ret shr r 10 |
| 123 | end |
| 124 | |
| 125 | fn r4 d ulong a ulong b ulong c ulong to ulong |
| 126 | ret or or d shl a 16 or shl b 32 shl c 48 |
| 127 | end |
| 128 | |
| 129 | fn okk op ulong k ulong fl ulong ur ulong to ulong |
| 130 | ret or or op shl k 8 or shl fl 16 shl ur 40 |
| 131 | end |
| 132 | |
| 133 | fn sb v ulong s ulong to ulong |
| 134 | ret or v shl s 10 |
| 135 | end |
| 136 | |
| 137 | fn p2 lo ulong hi ulong to ulong |
| 138 | ret or lo shl hi 16 |
| 139 | end |
| 140 | |
| 141 | const PW1 ulong 4294967296 |
| 142 | const PW2 ulong 8589934592 |
| 143 | const PW4 ulong 17179869184 |
| 144 | |
| 145 | note control word of one record kind: returns control | next pending wait mask << 32 |
| 146 | fn kc k ulong fl ulong pend ulong imm ulong to ulong |
| 147 | if eq k KPRED |
| 148 | ret cw 14 7 7 pend |
| 149 | end |
| 150 | if eq k KVAR |
| 151 | ret or or cw 4 0 7 pend YIELD PW1 |
| 152 | end |
| 153 | if eq k KUVAR |
| 154 | ret or or cw 4 2 7 pend YIELD PW4 |
| 155 | end |
| 156 | if eq k KSTORE |
| 157 | ret or or cw 4 7 1 pend YIELD PW2 |
| 158 | end |
| 159 | if or eq k KEXIT eq k KBRA |
| 160 | if ne and fl 2 0 |
| 161 | ret or cw 5 7 7 pend shl pend 32 |
| 162 | end |
| 163 | ret cw 5 7 7 pend |
| 164 | end |
| 165 | if eq k KRAW |
| 166 | ret or shr imm 32 shl pend 32 |
| 167 | end |
| 168 | ret cw 6 7 7 pend |
| 169 | end |
| 170 | |
| 171 | note operand fields 1..4 and their subregisters (record word 7 bits 16..47) |
| 172 | fn wops ins edit ulong 8192 b ulong rg ulong to void |
| 173 | d = and rg 65535 |
| 174 | s1 = and shr rg 16 65535 |
| 175 | s2 = and shr rg 32 65535 |
| 176 | s3 = shr rg 48 |
| 177 | put ins add b 1 fld d |
| 178 | put ins add b 2 fld s1 |
| 179 | put ins add b 3 fld s2 |
| 180 | put ins add b 4 fld s3 |
| 181 | f1 = or shl rsub d 16 shl rsub s1 24 |
| 182 | f2 = or shl rsub s2 32 shl rsub s3 40 |
| 183 | put ins add b 7 or f1 f2 |
| 184 | end |
| 185 | |
| 186 | note record words 5..7: ureg, imm | control << 32, flags (x = control | pending << 32) |
| 187 | fn wtail ins edit ulong 8192 b ulong ok ulong imm ulong x ulong to void |
| 188 | ur = and shr ok 40 65535 |
| 189 | uv = ALL |
| 190 | if ne ur NONE |
| 191 | uv = ur |
| 192 | end |
| 193 | put ins add b 5 uv |
| 194 | c = and x LO32 |
| 195 | put ins add b 6 or and imm LO32 shl c 32 |
| 196 | w7 = get ins add b 7 |
| 197 | put ins add b 7 or w7 and shr ok 16 7 |
| 198 | end |
| 199 | |
| 200 | note one record: ok = op | kind << 8 | flags << 16 | ureg << 40; rg = r4 operands; imm low 32 bits |
| 201 | note (a raw record carries its control word in imm bits 32..63) |
| 202 | fn emit ins edit ulong 8192 m edit ulong 512 ok ulong rg ulong imm ulong to void |
| 203 | n = get m 0 |
| 204 | if ge n 1024 |
| 205 | put m 4 1 |
| 206 | ret |
| 207 | end |
| 208 | k = and shr ok 8 255 |
| 209 | if gt k KRAW |
| 210 | put m 4 2 |
| 211 | ret |
| 212 | end |
| 213 | x = kc k and shr ok 16 255 get m 3 imm |
| 214 | b = mul n 8 |
| 215 | put ins b and ok 255 |
| 216 | wops ins b rg |
| 217 | wtail ins b ok imm x |
| 218 | put m 0 add n 1 |
| 219 | put m 3 shr x 32 |
| 220 | end |
| 221 | |
| 222 | fn nv m edit ulong 512 kind ulong to ulong |
| 223 | v = get m 1 |
| 224 | if ge v 256 |
| 225 | put m 4 3 |
| 226 | ret 0 |
| 227 | end |
| 228 | put m add 64 v kind |
| 229 | put m 1 add v 1 |
| 230 | ret v |
| 231 | end |
| 232 | |
| 233 | fn nu m edit ulong 512 kind ulong to ulong |
| 234 | u = get m 2 |
| 235 | if ge u 32 |
| 236 | put m 4 3 |
| 237 | ret 0 |
| 238 | end |
| 239 | put m add 320 u kind |
| 240 | put m 2 add u 1 |
| 241 | ret u |
| 242 | end |
| 243 | |
| 244 | note ---- instruction helpers |
| 245 | |
| 246 | fn ef ins edit ulong 8192 m edit ulong 512 op ulong rg ulong imm ulong to void |
| 247 | emit ins m okk op KFIX 0 NONE rg imm |
| 248 | end |
| 249 | |
| 250 | note d = op a b (fixed latency), a fresh scalar |
| 251 | fn fo ins edit ulong 8192 m edit ulong 512 op ulong a ulong b ulong to ulong |
| 252 | d = nv m 1 |
| 253 | ef ins m op r4 d a b NONE 0 |
| 254 | ret d |
| 255 | end |
| 256 | |
| 257 | note d = a * imm + c (low 32 bits), a fresh scalar |
| 258 | fn imi ins edit ulong 8192 m edit ulong 512 a ulong imm ulong c ulong to ulong |
| 259 | d = nv m 1 |
| 260 | ef ins m IMAD r4 d a NONE c imm |
| 261 | ret d |
| 262 | end |
| 263 | |
| 264 | note d:d+1 = a * imm + c:c+1 (unsigned wide), a fresh pair |
| 265 | fn imw ins edit ulong 8192 m edit ulong 512 a ulong imm ulong c ulong to ulong |
| 266 | d = nv m 2 |
| 267 | ef ins m IMADW r4 d a NONE c imm |
| 268 | ret d |
| 269 | end |
| 270 | |
| 271 | fn sh ins edit ulong 8192 m edit ulong 512 a ulong s ulong to ulong |
| 272 | d = nv m 1 |
| 273 | ef ins m SHFR r4 d a NONE NONE s |
| 274 | ret d |
| 275 | end |
| 276 | |
| 277 | fn mov ins edit ulong 8192 m edit ulong 512 imm ulong to ulong |
| 278 | d = nv m 1 |
| 279 | ef ins m MOVI r4 d NONE NONE NONE imm |
| 280 | ret d |
| 281 | end |
| 282 | |
| 283 | fn ffm ins edit ulong 8192 m edit ulong 512 a ulong b ulong c ulong to ulong |
| 284 | d = nv m 1 |
| 285 | ef ins m FFMA r4 d a b c 0 |
| 286 | ret d |
| 287 | end |
| 288 | |
| 289 | note d = LOP3(a, b, c, lut) with a = 0xF0, b = 0xCC, c = 0xAA |
| 290 | fn lut ins edit ulong 8192 m edit ulong 512 a ulong b ulong c ulong f ulong to ulong |
| 291 | d = nv m 1 |
| 292 | ef ins m LUT r4 d a b c f |
| 293 | ret d |
| 294 | end |
| 295 | |
| 296 | note a variable-latency load into a fresh register of kind kd (1 scalar, 2 pair) |
| 297 | fn vl ins edit ulong 8192 m edit ulong 512 op ulong kd ulong a ulong imm ulong to ulong |
| 298 | d = nv m kd |
| 299 | emit ins m okk op KVAR 0 NONE r4 d a NONE NONE imm |
| 300 | ret d |
| 301 | end |
| 302 | |
| 303 | fn gl ins edit ulong 8192 m edit ulong 512 a ulong imm ulong u ulong to ulong |
| 304 | d = nv m 2 |
| 305 | emit ins m okk LDG64 KVAR 0 u r4 d a NONE NONE imm |
| 306 | ret d |
| 307 | end |
| 308 | |
| 309 | fn stg ins edit ulong 8192 m edit ulong 512 a ulong v ulong imm ulong u ulong to void |
| 310 | emit ins m okk STG KSTORE 0 u r4 NONE a v NONE imm |
| 311 | end |
| 312 | |
| 313 | fn sts ins edit ulong 8192 m edit ulong 512 a ulong v ulong to void |
| 314 | emit ins m okk STS KSTORE 0 NONE r4 NONE a v NONE 0 |
| 315 | end |
| 316 | |
| 317 | fn bar ins edit ulong 8192 m edit ulong 512 to void |
| 318 | emit ins m okk BAR KSYNC 0 NONE r4 NONE NONE NONE NONE 0 |
| 319 | end |
| 320 | |
| 321 | note P0 = a >= b (unsigned) |
| 322 | fn isge ins edit ulong 8192 m edit ulong 512 a ulong b ulong to void |
| 323 | emit ins m okk ISGEU KPRED 0 NONE r4 NONE a b NONE 0 |
| 324 | end |
| 325 | |
| 326 | fn exitp ins edit ulong 8192 m edit ulong 512 to void |
| 327 | emit ins m okk EXIT KEXIT 2 NONE r4 NONE NONE NONE NONE 0 |
| 328 | end |
| 329 | |
| 330 | fn tail ins edit ulong 8192 m edit ulong 512 to void |
| 331 | emit ins m okk EXIT KEXIT 0 NONE r4 NONE NONE NONE NONE 0 |
| 332 | emit ins m okk BRA KRAW 0 NONE r4 NONE NONE NONE NONE shl NOPC 32 |
| 333 | end |
| 334 | |
| 335 | note ---- loops and branches (the replay applies the loop live-range fixup at each closing BRA) |
| 336 | |
| 337 | fn lbeg m edit ulong 512 to void |
| 338 | d = get m 6 |
| 339 | if ge d 4 |
| 340 | put m 4 4 |
| 341 | ret |
| 342 | end |
| 343 | put m add 8 d get m 0 |
| 344 | put m 6 add d 1 |
| 345 | end |
| 346 | |
| 347 | fn lend m edit ulong 512 to void |
| 348 | d = get m 6 |
| 349 | nl = get m 5 |
| 350 | if or eq d 0 ge nl 24 |
| 351 | put m 4 5 |
| 352 | ret |
| 353 | end |
| 354 | d1 = sub d 1 |
| 355 | put m add 16 mul nl 2 get m add 8 d1 |
| 356 | put m add 17 mul nl 2 sub get m 0 1 |
| 357 | put m 5 add nl 1 |
| 358 | put m 6 d1 |
| 359 | end |
| 360 | |
| 361 | note @P0 BRA forward; returns the record index for patch |
| 362 | fn bfwd ins edit ulong 8192 m edit ulong 512 to ulong |
| 363 | at = get m 0 |
| 364 | emit ins m okk BRA KBRA 2 NONE r4 NONE NONE NONE NONE 1 |
| 365 | ret at |
| 366 | end |
| 367 | |
| 368 | fn patch ins edit ulong 8192 m edit ulong 512 at ulong to void |
| 369 | b = add mul at 8 6 |
| 370 | x = get ins b |
| 371 | put ins b or and x 18446744069414584320 sub get m 0 at |
| 372 | end |
| 373 | |
| 374 | note @!P0 BRA back to the innermost open loop start |
| 375 | fn bback ins edit ulong 8192 m edit ulong 512 to void |
| 376 | d = get m 6 |
| 377 | if eq d 0 |
| 378 | put m 4 6 |
| 379 | ret |
| 380 | end |
| 381 | tgt = get m add 7 d |
| 382 | emit ins m okk BRA KBRA 6 NONE r4 NONE NONE NONE NONE and sub tgt get m 0 LO32 |
| 383 | end |
| 384 | |
| 385 | note ---- fixed-latency pair scheduling (schedule_fixed_pairs in the elementwise emitter) |
| 386 | |
| 387 | fn cls op ulong to ulong |
| 388 | if or or eq op MOVRZ eq op MOVI or eq op IADD3 eq op LXOR |
| 389 | ret 1 |
| 390 | end |
| 391 | if or or eq op LUT eq op SHFR eq op SHFL |
| 392 | ret 1 |
| 393 | end |
| 394 | if or or eq op IMAD eq op IMADHI or eq op FADD eq op FSUB |
| 395 | ret 2 |
| 396 | end |
| 397 | if or eq op FMUL eq op FFMA |
| 398 | ret 2 |
| 399 | end |
| 400 | ret 0 |
| 401 | end |
| 402 | |
| 403 | note tighten record i's stall from 6 to 5 when record i + 1 is a fixed op of the same class |
| 404 | fn tpair ins edit ulong 8192 i ulong to void |
| 405 | a = get ins mul i 8 |
| 406 | ca = cls a |
| 407 | if or eq ca 0 ne ca cls get ins mul add i 1 8 |
| 408 | ret |
| 409 | end |
| 410 | if eq a IADD3 |
| 411 | ret |
| 412 | end |
| 413 | if ne and get ins add mul i 8 7 2 0 |
| 414 | ret |
| 415 | end |
| 416 | if ne and get ins add mul add i 1 8 7 2 0 |
| 417 | ret |
| 418 | end |
| 419 | b = add mul i 8 6 |
| 420 | x = get ins b |
| 421 | ct = shr x 32 |
| 422 | if ne and shr ct 9 15 6 |
| 423 | ret |
| 424 | end |
| 425 | put ins b or and x LO32 shl sub ct 512 32 |
| 426 | end |
| 427 | |
| 428 | fn sched ins edit ulong 8192 m edit ulong 512 to void |
| 429 | n = get m 0 |
| 430 | for i ulong n |
| 431 | if lt add i 1 n |
| 432 | tpair ins i |
| 433 | end |
| 434 | end |
| 435 | end |
| 436 | |
| 437 | note ---- xoshiro256** on 64-bit values held as two 32-bit registers (p2 lo hi) |
| 438 | |
| 439 | note x ^ (x >> s), 0 < s < 32, into fresh registers |
| 440 | fn xs ins edit ulong 8192 m edit ulong 512 x ulong s ulong to ulong |
| 441 | lo = and x 65535 |
| 442 | hi = shr x 16 |
| 443 | t = sh ins m lo s |
| 444 | u = imi ins m hi shl 1 sub 32 s t |
| 445 | nl = fo ins m LXOR lo u |
| 446 | v = sh ins m hi s |
| 447 | nh = fo ins m LXOR hi v |
| 448 | ret p2 nl nh |
| 449 | end |
| 450 | |
| 451 | note x * c mod 2^64 |
| 452 | fn mulc ins edit ulong 8192 m edit ulong 512 x ulong c ulong to ulong |
| 453 | lo = and x 65535 |
| 454 | hi = shr x 16 |
| 455 | cl = and c LO32 |
| 456 | ch = shr c 32 |
| 457 | w = imw ins m lo cl NONE |
| 458 | h = imi ins m hi cl sb w 1 |
| 459 | nh = h |
| 460 | if ne ch 0 |
| 461 | nh = imi ins m lo ch h |
| 462 | end |
| 463 | ret p2 sb w 0 nh |
| 464 | end |
| 465 | |
| 466 | note rotl(x, s), 0 < s < 32, into fresh registers |
| 467 | fn rotl ins edit ulong 8192 m edit ulong 512 x ulong s ulong to ulong |
| 468 | lo = and x 65535 |
| 469 | hi = shr x 16 |
| 470 | a = sh ins m hi sub 32 s |
| 471 | b = sh ins m lo sub 32 s |
| 472 | nl = imi ins m lo shl 1 s a |
| 473 | nh = imi ins m hi shl 1 s b |
| 474 | ret p2 nl nh |
| 475 | end |
| 476 | |
| 477 | note SplitMix64 finaliser |
| 478 | fn mix ins edit ulong 8192 m edit ulong 512 x ulong to ulong |
| 479 | a = xs ins m x 30 |
| 480 | b = mulc ins m a C1 |
| 481 | c = xs ins m b 27 |
| 482 | d = mulc ins m c C2 |
| 483 | ret xs ins m d 31 |
| 484 | end |
| 485 | |
| 486 | note d ^= a in place |
| 487 | fn x64 ins edit ulong 8192 m edit ulong 512 d ulong a ulong to void |
| 488 | dl = and d 65535 |
| 489 | dh = shr d 16 |
| 490 | ef ins m LXOR r4 dl dl and a 65535 NONE 0 |
| 491 | ef ins m LXOR r4 dh dh shr a 16 NONE 0 |
| 492 | end |
| 493 | |
| 494 | note x = rotl(x, 45) in place: lo' = hi << 13 | lo >> 19, hi' = lo << 13 | hi >> 19 |
| 495 | fn rot45 ins edit ulong 8192 m edit ulong 512 x ulong to void |
| 496 | lo = and x 65535 |
| 497 | hi = shr x 16 |
| 498 | a = sh ins m lo 19 |
| 499 | b = sh ins m hi 19 |
| 500 | nl = imi ins m hi 8192 a |
| 501 | ef ins m IMAD r4 hi lo NONE b 8192 |
| 502 | ef ins m IMAD r4 lo nl NONE NONE 1 |
| 503 | end |
| 504 | |
| 505 | note one xoshiro256** state advance, in place |
| 506 | fn step ins edit ulong 8192 m edit ulong 512 st view ulong 4 to void |
| 507 | s0 = get st 0 |
| 508 | s1 = get st 1 |
| 509 | s2 = get st 2 |
| 510 | s3 = get st 3 |
| 511 | l1 = and s1 65535 |
| 512 | h1 = shr s1 16 |
| 513 | tl = imi ins m l1 131072 NONE |
| 514 | th = imi ins m h1 131072 sh ins m l1 15 |
| 515 | x64 ins m s2 s0 |
| 516 | x64 ins m s3 s1 |
| 517 | x64 ins m s1 s2 |
| 518 | x64 ins m s0 s3 |
| 519 | x64 ins m s2 p2 tl th |
| 520 | rot45 ins m s3 |
| 521 | end |
| 522 | |
| 523 | note ---- inverse normal: Giles' single-precision erfinv on w = -ln(1 - x^2) |
| 524 | |
| 525 | fn hcen ins edit ulong 8192 m edit ulong 512 t ulong mut ulong to ulong |
| 526 | p = mov ins m fb GC8 |
| 527 | p = ffm ins m p t mov ins m fb GC7 |
| 528 | p = ffm ins m p t mov ins m fb GC6 |
| 529 | p = ffm ins m p t mov ins m fb GC5 |
| 530 | p = ffm ins m p t mov ins m fb GC4 |
| 531 | p = ffm ins m p t mov ins m fb GC3 |
| 532 | p = ffm ins m p t mov ins m fb GC2 |
| 533 | p = ffm ins m p t mov ins m fb GC1 |
| 534 | c0 = fb GC0 |
| 535 | if eq mut 2 |
| 536 | c0 = fb GCM |
| 537 | end |
| 538 | ret ffm ins m p t mov ins m c0 |
| 539 | end |
| 540 | |
| 541 | fn htail ins edit ulong 8192 m edit ulong 512 t ulong to ulong |
| 542 | p = mov ins m fb GT8 |
| 543 | p = ffm ins m p t mov ins m fb GT7 |
| 544 | p = ffm ins m p t mov ins m fb GT6 |
| 545 | p = ffm ins m p t mov ins m fb GT5 |
| 546 | p = ffm ins m p t mov ins m fb GT4 |
| 547 | p = ffm ins m p t mov ins m fb GT3 |
| 548 | p = ffm ins m p t mov ins m fb GT2 |
| 549 | p = ffm ins m p t mov ins m fb GT1 |
| 550 | ret ffm ins m p t mov ins m fb GT0 |
| 551 | end |
| 552 | |
| 553 | note w = -ln(1 - x^2) and xi = 2A - 2^24 for A = top 23 bits * 2 + 1 (x = xi / 2^24, 1 - x^2 = A (2^24 - A) / 2^46); |
| 554 | note returns p2 w xi |
| 555 | fn vnw ins edit ulong 8192 m edit ulong 512 hi ulong to ulong |
| 556 | one = mov ins m 1 |
| 557 | a = sh ins m hi 8 |
| 558 | a = lut ins m a one NONE 252 |
| 559 | b = mov ins m 16777216 |
| 560 | b = imi ins m a M1 b |
| 561 | xi = mov ins m 4278190080 |
| 562 | xi = imi ins m a 2 xi |
| 563 | a = fo ins m I2FP a NONE |
| 564 | b = fo ins m I2FP b NONE |
| 565 | q = fo ins m FMUL a b |
| 566 | q = fo ins m FMUL q mov ins m fb K46 |
| 567 | q = vl ins m MLG2 1 q 0 |
| 568 | q = fo ins m FMUL q mov ins m fb NLN2 |
| 569 | ret p2 q xi |
| 570 | end |
| 571 | |
| 572 | note z = sqrt(2) erfinv(x): central branch for w < 5 (t = w - 2.5), tail branch (t = sqrt(w) - 3) otherwise |
| 573 | fn vnorm ins edit ulong 8192 m edit ulong 512 hi ulong mut ulong to ulong |
| 574 | wx = vnw ins m hi |
| 575 | w = and wx 65535 |
| 576 | t = fo ins m FSUB w mov ins m fb F25 |
| 577 | pc = hcen ins m t mut |
| 578 | t = vl ins m MRSQ 1 w 0 |
| 579 | t = fo ins m FMUL w t |
| 580 | t = fo ins m FSUB t mov ins m fb F3 |
| 581 | pt = htail ins m t |
| 582 | t = fo ins m FSUB w mov ins m fb F5 |
| 583 | t = sh ins m t 31 |
| 584 | t = imi ins m t M1 NONE |
| 585 | pc = lut ins m t pc pt 202 |
| 586 | t = fo ins m I2FP shr wx 16 NONE |
| 587 | t = fo ins m FMUL t mov ins m fb K24 |
| 588 | t = fo ins m FMUL pc t |
| 589 | ret fo ins m FMUL t mov ins m fb RT2 |
| 590 | end |
| 591 | |
| 592 | note ---- the RNG kernel. Arguments (constant bank): 0x380 out, 0x388 + 8 i base_i = seed + stream0 + (i + 1) G |
| 593 | note (i < 4), 0x3a8 skip, 0x3ac per-stream count, 0x3b0 stream count. Thread g writes out[j * ns + g]. |
| 594 | note R: 0 ureg desc, 1 out, 2 skip, 3 count, 4 streams, 5 g, 6 one, 7 zero, 8 element index, 9..12 base_i |
| 595 | |
| 596 | fn rpro ins edit ulong 8192 m edit ulong 512 R edit ulong 16 to void |
| 597 | u = nu m 2 |
| 598 | put R 0 u |
| 599 | emit ins m okk LDCU64 KUVAR 1 NONE r4 u NONE NONE NONE 856 |
| 600 | put R 1 vl ins m LDC64 2 NONE 896 |
| 601 | for i ulong 4 |
| 602 | put R add 9 i vl ins m LDC64 2 NONE add 904 mul i 8 |
| 603 | end |
| 604 | put R 2 vl ins m LDC 1 NONE 936 |
| 605 | put R 3 vl ins m LDC 1 NONE 940 |
| 606 | put R 4 vl ins m LDC 1 NONE 944 |
| 607 | t = vl ins m S2R 1 NONE 33 |
| 608 | c = vl ins m S2R 1 NONE 37 |
| 609 | g = imi ins m c 128 t |
| 610 | put R 5 g |
| 611 | isge ins m g get R 4 |
| 612 | exitp ins m |
| 613 | put R 6 mov ins m 1 |
| 614 | z = nv m 1 |
| 615 | ef ins m MOVRZ r4 z NONE NONE NONE 0 |
| 616 | put R 7 z |
| 617 | end |
| 618 | |
| 619 | note state word i = SplitMix64(base_i + g): stream g seeds aien-math's SplitMix64 with seed + stream0 + g, and the host |
| 620 | note puts seed + stream0 + (i + 1) G in base_i; IMAD.WIDE g * 1 adds g (see omega_gpu_mathlibs_api.c) |
| 621 | fn rinit ins edit ulong 8192 m edit ulong 512 R view ulong 16 st edit ulong 4 to void |
| 622 | for i ulong 4 |
| 623 | x = imw ins m get R 5 1 get R add 9 i |
| 624 | put st i mix ins m p2 sb x 0 sb x 1 |
| 625 | end |
| 626 | end |
| 627 | |
| 628 | note advance every stream by the skip count |
| 629 | fn rskip ins edit ulong 8192 m edit ulong 512 R view ulong 16 st view ulong 4 to void |
| 630 | off = get R 2 |
| 631 | isge ins m get R 7 off |
| 632 | at = bfwd ins m |
| 633 | lbeg m |
| 634 | step ins m st |
| 635 | ef ins m IMAD r4 off get R 6 NONE off M1 |
| 636 | isge ins m get R 7 off |
| 637 | bback ins m |
| 638 | lend m |
| 639 | patch ins m at |
| 640 | end |
| 641 | |
| 642 | note one output: raw 64-bit, uniform f32 top 24 bits / 2^24, or normal f32 |
| 643 | fn rstore ins edit ulong 8192 m edit ulong 512 R view ulong 16 r ulong var ulong mut ulong to void |
| 644 | hi = shr r 16 |
| 645 | if eq var 0 |
| 646 | ad = imw ins m get R 8 8 get R 1 |
| 647 | stg ins m ad and r 65535 0 get R 0 |
| 648 | stg ins m ad hi 4 get R 0 |
| 649 | ret |
| 650 | end |
| 651 | z = ulong 0 |
| 652 | if eq var 1 |
| 653 | z = sh ins m hi 8 |
| 654 | z = fo ins m I2FP z NONE |
| 655 | z = fo ins m FMUL z mov ins m fb K24 |
| 656 | else |
| 657 | z = vnorm ins m hi mut |
| 658 | end |
| 659 | ad = imw ins m get R 8 4 get R 1 |
| 660 | stg ins m ad z 0 get R 0 |
| 661 | end |
| 662 | |
| 663 | fn rout ins edit ulong 8192 m edit ulong 512 R edit ulong 16 st view ulong 4 var ulong mut ulong to void |
| 664 | e = imi ins m get R 5 1 NONE |
| 665 | put R 8 e |
| 666 | rot = ulong 7 |
| 667 | if eq mut 1 |
| 668 | rot = 8 |
| 669 | end |
| 670 | lbeg m |
| 671 | r = mulc ins m get st 1 5 |
| 672 | r = rotl ins m r rot |
| 673 | r = mulc ins m r 9 |
| 674 | rstore ins m R r var mut |
| 675 | step ins m st |
| 676 | ef ins m IMAD r4 e get R 4 NONE e 1 |
| 677 | c = get R 3 |
| 678 | ef ins m IMAD r4 c get R 6 NONE c M1 |
| 679 | isge ins m get R 7 c |
| 680 | bback ins m |
| 681 | lend m |
| 682 | end |
| 683 | |
| 684 | fn rngk ins edit ulong 8192 m edit ulong 512 var ulong mut ulong to void |
| 685 | R = new ulong 16 0 |
| 686 | st = new ulong 4 0 |
| 687 | rpro ins m edit R |
| 688 | rinit ins m view R edit st |
| 689 | rskip ins m view R view st |
| 690 | rout ins m edit R view st var mut |
| 691 | tail ins m |
| 692 | end |
| 693 | |
| 694 | note ---- the FFT kernel: one CTA of n / 4 threads per transform; data interleaved (re, im) f32 pairs. |
| 695 | note Arguments: 0x380 in, 0x388 out, 0x390 twiddles. Shared memory: re plane at 0, im plane at 4n. |
| 696 | note A: 0..7 Y (stage outputs, re/im of elements 0..3), 8..15 T (DFT4 temporaries), 16..23 shared read |
| 697 | note addresses, 24..31 V (stage inputs), 32..34 twiddle pairs, 40 ureg desc, 41 in, 42 out, 43 twiddles, |
| 698 | note 44 tid, 45 one, 46 g = cta n + tid, 47 store address, 48 n, 50 stages, 51 odd, 52 forward, 53 p, |
| 699 | note 54 twiddled stage seen, 55 mutant, 56 i = tid mod p. |
| 700 | |
| 701 | fn pk d ulong a ulong b ulong to ulong |
| 702 | ret or d or shl a 8 shl b 16 |
| 703 | end |
| 704 | |
| 705 | note A[d] = op A[a] A[b] for x = pk d a b |
| 706 | fn fop ins edit ulong 8192 m edit ulong 512 A view ulong 64 op ulong x ulong to void |
| 707 | d = get A and x 255 |
| 708 | a = get A and shr x 8 255 |
| 709 | ef ins m op r4 d a get A shr x 16 NONE 0 |
| 710 | end |
| 711 | |
| 712 | fn fset ins edit ulong 8192 m edit ulong 512 A edit ulong 64 lg ulong dir ulong mut ulong to void |
| 713 | n = shl 1 lg |
| 714 | put A 48 n |
| 715 | put A 51 and lg 1 |
| 716 | put A 50 add and lg 1 shr lg 1 |
| 717 | f = ulong 1 |
| 718 | if eq dir 1 |
| 719 | f = 0 |
| 720 | end |
| 721 | if eq mut 1 |
| 722 | f = sub 1 f |
| 723 | end |
| 724 | put A 52 f |
| 725 | put A 53 1 |
| 726 | put A 55 mut |
| 727 | for j ulong 16 |
| 728 | put A j nv m 1 |
| 729 | end |
| 730 | for j ulong 3 |
| 731 | put A add 32 j nv m 2 |
| 732 | end |
| 733 | u = nu m 2 |
| 734 | put A 40 u |
| 735 | emit ins m okk LDCU64 KUVAR 1 NONE r4 u NONE NONE NONE 856 |
| 736 | put A 41 vl ins m LDC64 2 NONE 896 |
| 737 | put A 42 vl ins m LDC64 2 NONE 904 |
| 738 | put A 43 vl ins m LDC64 2 NONE 912 |
| 739 | t = vl ins m S2R 1 NONE 33 |
| 740 | put A 44 t |
| 741 | c = vl ins m S2R 1 NONE 37 |
| 742 | put A 45 mov ins m 1 |
| 743 | put A 46 imi ins m c n t |
| 744 | put A 47 nv m 1 |
| 745 | if gt get A 50 1 |
| 746 | fra ins m A |
| 747 | end |
| 748 | end |
| 749 | |
| 750 | note shared read addresses: re of element j at 4t + j n, im + 4n |
| 751 | fn fra ins edit ulong 8192 m edit ulong 512 A edit ulong 64 to void |
| 752 | n = get A 48 |
| 753 | one = get A 45 |
| 754 | c = imi ins m get A 44 4 NONE |
| 755 | for j ulong 4 |
| 756 | o = mul j n |
| 757 | put A add 16 mul j 2 imi ins m one o c |
| 758 | put A add 17 mul j 2 imi ins m one add o mul n 4 c |
| 759 | end |
| 760 | end |
| 761 | |
| 762 | note V = this stage's inputs: global loads at stage 0, else the shared reads as one group |
| 763 | note (the first load waits, the rest ride its scoreboard, like the elementwise ld_group) |
| 764 | fn fload ins edit ulong 8192 m edit ulong 512 A edit ulong 64 s ulong to void |
| 765 | if eq s 0 |
| 766 | n = get A 48 |
| 767 | ga = imw ins m get A 46 8 get A 41 |
| 768 | for j ulong 4 |
| 769 | w = gl ins m ga mul j mul n 2 get A 40 |
| 770 | put A add 24 mul j 2 sb w 0 |
| 771 | put A add 25 mul j 2 sb w 1 |
| 772 | end |
| 773 | ret |
| 774 | end |
| 775 | emit ins m okk LDS KVAR 0 NONE r4 get A 0 get A 16 NONE NONE 0 |
| 776 | for j ulong 7 |
| 777 | k = add j 1 |
| 778 | emit ins m okk LDS KRAW 0 NONE r4 get A k get A add 16 k NONE NONE shl RAWC 32 |
| 779 | end |
| 780 | for j ulong 8 |
| 781 | put A add 24 j get A j |
| 782 | end |
| 783 | end |
| 784 | |
| 785 | note element j of V times twiddle pair w, in place |
| 786 | fn cmul ins edit ulong 8192 m edit ulong 512 A view ulong 64 j ulong w ulong to void |
| 787 | a = get A add 24 mul j 2 |
| 788 | b = get A add 25 mul j 2 |
| 789 | t1 = fo ins m FMUL a sb w 0 |
| 790 | t2 = fo ins m FMUL b sb w 1 |
| 791 | t3 = fo ins m FMUL b sb w 0 |
| 792 | ef ins m FFMA r4 b a sb w 1 t3 0 |
| 793 | ef ins m FSUB r4 a t1 t2 NONE 0 |
| 794 | end |
| 795 | |
| 796 | note i = tid mod p and the twiddles w^(j i n / (4p)), j = 1..3 (mutant 2 skips them on the first p > 1 stage) |
| 797 | fn ftw ins edit ulong 8192 m edit ulong 512 A edit ulong 64 to void |
| 798 | p = get A 53 |
| 799 | if eq p 1 |
| 800 | ret |
| 801 | end |
| 802 | i = nv m 1 |
| 803 | put A 56 i |
| 804 | ef ins m LAND r4 i get A 44 NONE NONE sub p 1 |
| 805 | dotw = or ne get A 55 2 ne get A 54 0 |
| 806 | put A 54 1 |
| 807 | if dotw |
| 808 | tws = div get A 48 mul p 4 |
| 809 | for j ulong 3 |
| 810 | ta = imw ins m i mul mul add j 1 tws 8 get A 43 |
| 811 | t = get A add 32 j |
| 812 | emit ins m okk LDG64 KVAR 0 get A 40 r4 t ta NONE NONE 0 |
| 813 | cmul ins m A add j 1 t |
| 814 | end |
| 815 | end |
| 816 | end |
| 817 | |
| 818 | note Y = DFT4(V) through T; forward multiplies u1 - u3 by -i |
| 819 | fn dft4 ins edit ulong 8192 m edit ulong 512 A view ulong 64 to void |
| 820 | fop ins m A FADD pk 8 24 28 |
| 821 | fop ins m A FADD pk 9 25 29 |
| 822 | fop ins m A FSUB pk 10 24 28 |
| 823 | fop ins m A FSUB pk 11 25 29 |
| 824 | fop ins m A FADD pk 12 26 30 |
| 825 | fop ins m A FADD pk 13 27 31 |
| 826 | fop ins m A FSUB pk 14 26 30 |
| 827 | fop ins m A FSUB pk 15 27 31 |
| 828 | fop ins m A FADD pk 0 8 12 |
| 829 | fop ins m A FADD pk 1 9 13 |
| 830 | fop ins m A FSUB pk 4 8 12 |
| 831 | fop ins m A FSUB pk 5 9 13 |
| 832 | k1 = ulong 2 |
| 833 | k3 = ulong 6 |
| 834 | if eq get A 52 0 |
| 835 | k1 = 6 |
| 836 | k3 = 2 |
| 837 | end |
| 838 | fop ins m A FADD pk k1 10 15 |
| 839 | fop ins m A FSUB pk add k1 1 11 14 |
| 840 | fop ins m A FSUB pk k3 10 15 |
| 841 | fop ins m A FADD pk add k3 1 11 14 |
| 842 | end |
| 843 | |
| 844 | note radix 2 on (u0, u2) and (u1, u3): Y = (u0 + u2, u0 - u2, u1 + u3, u1 - u3) to shared 2t, 2t + 1, |
| 845 | note 2t + n/2, 2t + n/2 + 1 |
| 846 | fn fr2 ins edit ulong 8192 m edit ulong 512 A edit ulong 64 to void |
| 847 | fop ins m A FADD pk 0 24 28 |
| 848 | fop ins m A FADD pk 1 25 29 |
| 849 | fop ins m A FSUB pk 2 24 28 |
| 850 | fop ins m A FSUB pk 3 25 29 |
| 851 | fop ins m A FADD pk 4 26 30 |
| 852 | fop ins m A FADD pk 5 27 31 |
| 853 | fop ins m A FSUB pk 6 26 30 |
| 854 | fop ins m A FSUB pk 7 27 31 |
| 855 | n = get A 48 |
| 856 | wb = imi ins m get A 44 8 NONE |
| 857 | sa = get A 47 |
| 858 | for j ulong 4 |
| 859 | o = add mul shr j 1 mul n 2 mul and j 1 4 |
| 860 | for pl ulong 2 |
| 861 | ef ins m IMAD r4 sa get A 45 NONE wb add o mul pl mul n 4 |
| 862 | sts ins m sa get A add mul j 2 pl |
| 863 | end |
| 864 | end |
| 865 | bar ins m |
| 866 | put A 53 2 |
| 867 | end |
| 868 | |
| 869 | note Y to shared at 4k + i + j p (k = t - i): byte 16t - 12i + 4jp, im plane + 4n |
| 870 | fn fsst ins edit ulong 8192 m edit ulong 512 A edit ulong 64 to void |
| 871 | n = get A 48 |
| 872 | p = get A 53 |
| 873 | wb = imi ins m get A 44 16 NONE |
| 874 | if gt p 1 |
| 875 | wb = imi ins m get A 56 4294967284 wb |
| 876 | end |
| 877 | sa = get A 47 |
| 878 | for j ulong 4 |
| 879 | for pl ulong 2 |
| 880 | o = add mul mul j p 4 mul pl mul n 4 |
| 881 | ef ins m IMAD r4 sa get A 45 NONE wb o |
| 882 | sts ins m sa get A add mul j 2 pl |
| 883 | end |
| 884 | end |
| 885 | bar ins m |
| 886 | put A 53 mul p 4 |
| 887 | end |
| 888 | |
| 889 | note the last stage (p = n/4, so i = t): Y element j to out[g + j n/4] |
| 890 | fn fgst ins edit ulong 8192 m edit ulong 512 A view ulong 64 to void |
| 891 | n = get A 48 |
| 892 | go = imw ins m get A 46 8 get A 42 |
| 893 | for j ulong 4 |
| 894 | o = mul j mul n 2 |
| 895 | stg ins m go get A mul j 2 o get A 40 |
| 896 | stg ins m go get A add mul j 2 1 add o 4 get A 40 |
| 897 | end |
| 898 | end |
| 899 | |
| 900 | fn fstage ins edit ulong 8192 m edit ulong 512 A edit ulong 64 s ulong to void |
| 901 | fload ins m A s |
| 902 | if and eq s 0 eq get A 51 1 |
| 903 | fr2 ins m A |
| 904 | ret |
| 905 | end |
| 906 | ftw ins m A |
| 907 | dft4 ins m A |
| 908 | if eq s sub get A 50 1 |
| 909 | fgst ins m A |
| 910 | ret |
| 911 | end |
| 912 | if gt s 0 |
| 913 | bar ins m |
| 914 | end |
| 915 | fsst ins m A |
| 916 | end |
| 917 | |
| 918 | fn fftk ins edit ulong 8192 m edit ulong 512 lg ulong dir ulong mut ulong to void |
| 919 | A = new ulong 64 0 |
| 920 | fset ins m edit A lg dir mut |
| 921 | nst = get A 50 |
| 922 | for s ulong nst |
| 923 | fstage ins m edit A s |
| 924 | end |
| 925 | tail ins m |
| 926 | end |
| 927 | |
| 928 | note ---- twiddles: w^m = exp(-+2 pi i m / n), m < n, by quadrant folding and f64 Taylor series on [0, pi/4] |
| 929 | |
| 930 | fn sinp x f64 to f64 |
| 931 | x2 = mul x x |
| 932 | t = x |
| 933 | s = x |
| 934 | for k ulong 12 |
| 935 | a = f64 add mul k 2 2 |
| 936 | t = div mul t neg x2 mul a add a 1.0f64 |
| 937 | s = add s t |
| 938 | end |
| 939 | ret s |
| 940 | end |
| 941 | |
| 942 | fn cosp x f64 to f64 |
| 943 | x2 = mul x x |
| 944 | t = 1.0f64 |
| 945 | s = 1.0f64 |
| 946 | for k ulong 12 |
| 947 | a = f64 add mul k 2 1 |
| 948 | t = div mul t neg x2 mul a add a 1.0f64 |
| 949 | s = add s t |
| 950 | end |
| 951 | ret s |
| 952 | end |
| 953 | |
| 954 | note (c, s) = (cos, sin) of the folded angle rotated into quadrant q; forward conjugates; f32 bits packed |
| 955 | fn tq q ulong c f64 s f64 dir ulong to ulong |
| 956 | re = c |
| 957 | im = s |
| 958 | if eq q 1 |
| 959 | re = neg s |
| 960 | im = c |
| 961 | end |
| 962 | if eq q 2 |
| 963 | re = neg c |
| 964 | im = neg s |
| 965 | end |
| 966 | if eq q 3 |
| 967 | re = s |
| 968 | im = neg c |
| 969 | end |
| 970 | fr = f32 re |
| 971 | fi = f32 im |
| 972 | if eq dir 0 |
| 973 | fi = neg fi |
| 974 | end |
| 975 | ret or ulong bitcast uint fr shl ulong bitcast uint fi 32 |
| 976 | end |
| 977 | |
| 978 | note w^k = exp(+-2 pi i k / n), any n >= 1, k < n: 4k = q n + r; angle (pi/2) r / n, folded to (pi/2) (n - r) / n |
| 979 | note with cos and sin swapped when r > n/2, so the series only sees [0, pi/4] (for n = 2^lg the same bits as the |
| 980 | note shift form the power-of-two kernels used before kinds 7..13 existed) |
| 981 | fn twg k ulong n ulong dir ulong to ulong |
| 982 | q = div mul k 4 n |
| 983 | r = sub mul k 4 mul q n |
| 984 | sw = gt r shr n 1 |
| 985 | if sw |
| 986 | r = sub n r |
| 987 | end |
| 988 | ph = mul HPI div f64 r f64 n |
| 989 | c = cosp ph |
| 990 | s = sinp ph |
| 991 | if sw |
| 992 | t = c |
| 993 | c = s |
| 994 | s = t |
| 995 | end |
| 996 | ret tq q c s dir |
| 997 | end |
| 998 | |
| 999 | fn twid ins edit ulong 8192 n ulong dir ulong to void |
| 1000 | for k ulong n |
| 1001 | put ins k twg k n dir |
| 1002 | end |
| 1003 | end |
| 1004 | |
| 1005 | note ---- kind 3: Philox4x32-10 (Salmon, Moraes, Dror, Shaw 2011; Random123), the stream of cuRAND's |
| 1006 | note curandStatePhilox4_32_10_t, and cuRAND's distributions on it. a = distribution: 0 raw 32-bit words (curand), |
| 1007 | note 1 uniform f32 (curand_uniform), 2 uniform f64 (curand_uniform_double), 3 uniform f64 from two words |
| 1008 | note (curand_uniform2_double, x then y), 4 normal f32 (curand_normal, Box-Muller, sine first). b = offset mod 4; |
| 1009 | note c = mutant 0..6. Group i of stream g is the stream words offset + 4 i .. offset + 4 i + 3: counter block q0 + i |
| 1010 | note shifted by b (b > 0 joins words b..3 of block q0 + i to words 0..b - 1 of block q0 + i + 1). A group yields G |
| 1011 | note outputs (G 4; 2 for distribution 3), each element 4 bytes (0, 1, 4) or 8 bytes (2, 3). |
| 1012 | note Arguments: 0x380 out, 0x388 key (the 64-bit seed), 0x390 subsequence of stream 0, 0x398 q0 = offset / 4, |
| 1013 | note 0x3a0 groups per stream T, 0x3a4 stream count ns, 0x3a8 dl = r - G mod 2^32 where 0 < r <= G is the output count of |
| 1014 | note the last group (dl 0 when it is full). Thread g is stream g with counter (q0 + i, subseq0 + g) as two 64-bit halves. |
| 1015 | note Layout: output c of group j < T - 1 of stream g is element (j ns + g) G + c; output c < r of the last group is |
| 1016 | note element (T - 1) ns G + g r + c (the last group of every stream packed after the full groups). |
| 1017 | note P: 0 ureg desc, 1 out, 2 key, 3 counter high pair, 4 counter low pair, 5 groups left, 6 streams, 7 element index |
| 1018 | note of the group, 8 one, 9 zero, 10..19 round keys k0 + r W0, 20..29 round keys k1 + r W1, 30 dl, 31 g dl, 32 ns G, |
| 1019 | note 33 G, 34..37 previous block (b > 0), 38 2^32 - 2, 40..47 two round scratch sets (pair X, pair Y, L0, L1), |
| 1020 | note 49..51 the constants 1..3, 52 store limit, 54 mutant, 55 distribution, 56 b, 58..61 the current block words, |
| 1021 | note 64 + i distribution constant i, 128..222 the scratch pool (vreg + 1), 223 the next pool index, 224..243 named |
| 1022 | note intermediates of pdbl / phq / pbm (functions take at |
| 1023 | note most six parameters, so the kernel state is this one array). |
| 1024 | note Mutants: 1 k1 bumped by W0, 2 nine rounds, 3 uniform f32 without the half step, 4 b > 0 ignores the previous |
| 1025 | note block, 5 normal sine and cosine swapped, 6 uniform2_double without round to nearest even. |
| 1026 | const PHM0 ulong 3528531795 |
| 1027 | const PHM1 ulong 3449720151 |
| 1028 | const PHW0 ulong 2654435769 |
| 1029 | const PHW1 ulong 3144134277 |
| 1030 | note f32 bit patterns of the distribution constants (Wolfram-checked; values in the PR evidence README) |
| 1031 | const QK65536 ulong 1199570944 |
| 1032 | const QP2M32 ulong 796917760 |
| 1033 | const QP2M33 ulong 788529152 |
| 1034 | const QC2PI ulong 818483163 |
| 1035 | const QC2PIH ulong 810094555 |
| 1036 | const QLOGADD ulong 4913933 |
| 1037 | const QMASK23 ulong 8388607 |
| 1038 | const QSQ2H ulong 1060439283 |
| 1039 | const QFM1 ulong 3212836864 |
| 1040 | const QFM127 ulong 3271426048 |
| 1041 | const QLC0 ulong 1051372202 |
| 1042 | const QLC1 ulong 3196059644 |
| 1043 | const QLC2 ulong 1045221036 |
| 1044 | const QLC3 ulong 3190468176 |
| 1045 | const QLC4 ulong 1041361343 |
| 1046 | const QLC5 ulong 3187563855 |
| 1047 | const QLC6 ulong 1039082778 |
| 1048 | const QLC7 ulong 3186348470 |
| 1049 | const QLC8 ulong 1032855994 |
| 1050 | const QFMH ulong 3204448256 |
| 1051 | const QLN2H ulong 1060204928 |
| 1052 | const QLN2L ulong 924317650 |
| 1053 | const QFM2 ulong 3221225472 |
| 1054 | const QTINY ulong 8388608 |
| 1055 | const QFH ulong 1056964608 |
| 1056 | const QTWOOPI ulong 1059256707 |
| 1057 | const QMAGIC ulong 1262485504 |
| 1058 | const QMAGICN ulong 3409969152 |
| 1059 | const QNP1 ulong 3217625051 |
| 1060 | const QNP2 ulong 859553070 |
| 1061 | const QNP3 ulong 653733101 |
| 1062 | const QS3 ulong 3190467243 |
| 1063 | const QS5 ulong 1007192201 |
| 1064 | const QS7 ulong 3109031169 |
| 1065 | const QS9 ulong 909700893 |
| 1066 | const QCC4 ulong 1026206379 |
| 1067 | const QCC6 ulong 3132492641 |
| 1068 | const QCC8 ulong 936381697 |
| 1069 | const QCC10 ulong 3029594750 |
| 1070 | const QONE ulong 1065353216 |
| 1071 | |
| 1072 | fn ppro ins edit ulong 8192 m edit ulong 512 P edit ulong 256 G ulong to void |
| 1073 | put P 0 nu m 2 |
| 1074 | emit ins m okk LDCU64 KUVAR 1 NONE r4 get P 0 NONE NONE NONE 856 |
| 1075 | put P 1 vl ins m LDC64 2 NONE 896 |
| 1076 | put P 2 vl ins m LDC64 2 NONE 904 |
| 1077 | s0 = vl ins m LDC64 2 NONE 912 |
| 1078 | put P 4 vl ins m LDC64 2 NONE 920 |
| 1079 | put P 5 vl ins m LDC 1 NONE 928 |
| 1080 | put P 6 vl ins m LDC 1 NONE 932 |
| 1081 | put P 30 vl ins m LDC 1 NONE 936 |
| 1082 | t = vl ins m S2R 1 NONE 33 |
| 1083 | c = vl ins m S2R 1 NONE 37 |
| 1084 | g = imi ins m c 128 t |
| 1085 | isge ins m g get P 6 |
| 1086 | exitp ins m |
| 1087 | put P 3 imw ins m g 1 s0 |
| 1088 | put P 8 mov ins m 1 |
| 1089 | z = nv m 1 |
| 1090 | ef ins m MOVRZ r4 z NONE NONE NONE 0 |
| 1091 | put P 9 z |
| 1092 | put P 7 imi ins m g G z |
| 1093 | put P 32 imi ins m get P 6 G z |
| 1094 | gd = nv m 1 |
| 1095 | ef ins m IMAD r4 gd g get P 30 z 0 |
| 1096 | put P 31 gd |
| 1097 | put P 33 mov ins m G |
| 1098 | put P 38 mov ins m 4294967294 |
| 1099 | put P 49 get P 8 |
| 1100 | put P 50 mov ins m 2 |
| 1101 | put P 51 mov ins m 3 |
| 1102 | end |
| 1103 | |
| 1104 | note round keys (mutant 1 bumps k1 by W0) |
| 1105 | fn pkeys ins edit ulong 8192 m edit ulong 512 P edit ulong 256 to void |
| 1106 | k = get P 2 |
| 1107 | w1 = PHW1 |
| 1108 | if eq get P 54 1 |
| 1109 | w1 = PHW0 |
| 1110 | end |
| 1111 | put P 10 sb k 0 |
| 1112 | put P 20 sb k 1 |
| 1113 | for r ulong 9 |
| 1114 | q = add r 1 |
| 1115 | one = get P 8 |
| 1116 | x = imi ins m one and mul q PHW0 LO32 sb k 0 |
| 1117 | put P add 10 q x |
| 1118 | x = imi ins m one and mul q w1 LO32 sb k 1 |
| 1119 | put P add 20 q x |
| 1120 | end |
| 1121 | end |
| 1122 | |
| 1123 | note constant i of the distribution (P 64 + i) |
| 1124 | fn kq P edit ulong 256 i ulong to ulong |
| 1125 | ret get P add 64 i |
| 1126 | end |
| 1127 | |
| 1128 | fn kput ins edit ulong 8192 m edit ulong 512 P edit ulong 256 i ulong bits ulong to void |
| 1129 | put P add 64 i mov ins m bits |
| 1130 | end |
| 1131 | |
| 1132 | note distribution constants (f32 bits; 37..41 the f64 builder: 158, exponent bias << 20, the pair (2^32 - 1, 0), |
| 1133 | note the pair (1, 0), 2) |
| 1134 | fn pkc ins edit ulong 8192 m edit ulong 512 P edit ulong 256 to void |
| 1135 | a = get P 55 |
| 1136 | if or eq a 1 eq a 4 |
| 1137 | kput ins m P 0 QK65536 |
| 1138 | kput ins m P 1 QP2M32 |
| 1139 | kput ins m P 2 QP2M33 |
| 1140 | end |
| 1141 | if eq a 4 |
| 1142 | kput ins m P 3 QC2PI |
| 1143 | kput ins m P 4 QC2PIH |
| 1144 | kput ins m P 5 QFM1 |
| 1145 | kput ins m P 6 QFM127 |
| 1146 | kput ins m P 7 QLC0 |
| 1147 | kput ins m P 8 QLC1 |
| 1148 | kput ins m P 9 QLC2 |
| 1149 | kput ins m P 10 QLC3 |
| 1150 | kput ins m P 11 QLC4 |
| 1151 | kput ins m P 12 QLC5 |
| 1152 | kput ins m P 13 QLC6 |
| 1153 | kput ins m P 14 QLC7 |
| 1154 | kput ins m P 15 QLC8 |
| 1155 | kput ins m P 16 QFMH |
| 1156 | kput ins m P 17 QLN2H |
| 1157 | kput ins m P 18 QLN2L |
| 1158 | kput ins m P 19 QFM2 |
| 1159 | kput ins m P 20 QTINY |
| 1160 | kput ins m P 21 QFH |
| 1161 | kput ins m P 22 QTWOOPI |
| 1162 | kput ins m P 23 QMAGIC |
| 1163 | kput ins m P 24 QMAGICN |
| 1164 | kput ins m P 25 QNP1 |
| 1165 | kput ins m P 26 QNP2 |
| 1166 | kput ins m P 27 QNP3 |
| 1167 | kput ins m P 28 QS3 |
| 1168 | kput ins m P 29 QS5 |
| 1169 | kput ins m P 30 QS7 |
| 1170 | kput ins m P 31 QS9 |
| 1171 | kput ins m P 32 QCC4 |
| 1172 | kput ins m P 33 QCC6 |
| 1173 | kput ins m P 34 QCC8 |
| 1174 | kput ins m P 35 QCC10 |
| 1175 | kput ins m P 36 QONE |
| 1176 | end |
| 1177 | if or eq a 2 eq a 3 |
| 1178 | kput ins m P 37 158 |
| 1179 | bias = ulong 863 |
| 1180 | if eq a 3 |
| 1181 | bias = 841 |
| 1182 | end |
| 1183 | kput ins m P 38 shl bias 20 |
| 1184 | put P 103 imw ins m mov ins m M1 1 NONE |
| 1185 | put P 104 imw ins m mov ins m 1 1 NONE |
| 1186 | kput ins m P 41 2 |
| 1187 | end |
| 1188 | end |
| 1189 | |
| 1190 | note ---- the scratch pool: a fixed set of scalar vregs reused by every output (the loop body would otherwise |
| 1191 | note exceed 256 vregs); put P 223 0 restarts it |
| 1192 | fn tnew m edit ulong 512 P edit ulong 256 to ulong |
| 1193 | i = get P 223 |
| 1194 | if ge i 95 |
| 1195 | put m 4 3 |
| 1196 | ret 0 |
| 1197 | end |
| 1198 | put P 223 add i 1 |
| 1199 | v = get P add 128 i |
| 1200 | if eq v 0 |
| 1201 | v = add nv m 1 1 |
| 1202 | put P add 128 i v |
| 1203 | end |
| 1204 | ret sub v 1 |
| 1205 | end |
| 1206 | |
| 1207 | fn tfo ins edit ulong 8192 m edit ulong 512 P edit ulong 256 op ulong a ulong b ulong to ulong |
| 1208 | d = tnew m P |
| 1209 | ef ins m op r4 d a b NONE 0 |
| 1210 | ret d |
| 1211 | end |
| 1212 | |
| 1213 | note d = a * b + c (FFMA) |
| 1214 | fn tff ins edit ulong 8192 m edit ulong 512 P edit ulong 256 a ulong b ulong c ulong to ulong |
| 1215 | d = tnew m P |
| 1216 | ef ins m FFMA r4 d a b c 0 |
| 1217 | ret d |
| 1218 | end |
| 1219 | |
| 1220 | note d = a * imm + c (IMAD, low 32 bits) |
| 1221 | fn tim ins edit ulong 8192 m edit ulong 512 P edit ulong 256 a ulong imm ulong c ulong to ulong |
| 1222 | d = tnew m P |
| 1223 | ef ins m IMAD r4 d a NONE c imm |
| 1224 | ret d |
| 1225 | end |
| 1226 | |
| 1227 | note d = a * b + c (IMAD register form, low 32 bits) |
| 1228 | fn tir ins edit ulong 8192 m edit ulong 512 P edit ulong 256 a ulong b ulong c ulong to ulong |
| 1229 | d = tnew m P |
| 1230 | ef ins m IMAD r4 d a b c 0 |
| 1231 | ret d |
| 1232 | end |
| 1233 | |
| 1234 | note d = lut(a, b, RZ) with function f |
| 1235 | fn tlu ins edit ulong 8192 m edit ulong 512 P edit ulong 256 a ulong b ulong f ulong to ulong |
| 1236 | d = tnew m P |
| 1237 | ef ins m LUT r4 d a b NONE f |
| 1238 | ret d |
| 1239 | end |
| 1240 | |
| 1241 | fn tsh ins edit ulong 8192 m edit ulong 512 P edit ulong 256 a ulong s ulong to ulong |
| 1242 | d = tnew m P |
| 1243 | ef ins m SHFR r4 d a NONE NONE s |
| 1244 | ret d |
| 1245 | end |
| 1246 | |
| 1247 | fn tan ins edit ulong 8192 m edit ulong 512 P edit ulong 256 a ulong imm ulong to ulong |
| 1248 | d = tnew m P |
| 1249 | ef ins m LAND r4 d a NONE NONE imm |
| 1250 | ret d |
| 1251 | end |
| 1252 | |
| 1253 | note ---- one Philox round into scratch set o: X = M0 c0, Y = M1 c2 (wide), c = (Y.hi ^ c1 ^ k0, Y.lo, X.hi ^ c3 ^ k1, |
| 1254 | note X.lo) on the block words P 58..61; the sets alternate so a round never overwrites the words it reads |
| 1255 | fn prnd ins edit ulong 8192 m edit ulong 512 P edit ulong 256 k0 ulong k1 ulong o ulong to void |
| 1256 | b = add 40 mul o 4 |
| 1257 | x = get P b |
| 1258 | y = get P add b 1 |
| 1259 | l0 = get P add b 2 |
| 1260 | l1 = get P add b 3 |
| 1261 | ef ins m IMADW r4 x get P 58 NONE NONE PHM0 |
| 1262 | ef ins m IMADW r4 y get P 60 NONE NONE PHM1 |
| 1263 | ef ins m LUT r4 l0 sb y 1 get P 59 k0 150 |
| 1264 | ef ins m LUT r4 l1 sb x 1 get P 61 k1 150 |
| 1265 | put P 58 l0 |
| 1266 | put P 59 sb y 0 |
| 1267 | put P 60 l1 |
| 1268 | put P 61 sb x 0 |
| 1269 | end |
| 1270 | |
| 1271 | note block blk (counter low pair) of this thread's subsequence into P 58..61: nr rounds |
| 1272 | fn pblk ins edit ulong 8192 m edit ulong 512 P edit ulong 256 blk ulong nr ulong to void |
| 1273 | put P 58 sb blk 0 |
| 1274 | put P 59 sb blk 1 |
| 1275 | put P 60 sb get P 3 0 |
| 1276 | put P 61 sb get P 3 1 |
| 1277 | for r ulong nr |
| 1278 | prnd ins m P get P add 10 r get P add 20 r and r 1 |
| 1279 | end |
| 1280 | end |
| 1281 | |
| 1282 | note word i of the group (b > 0: previous block words b..3, then this block's words 0..b - 1; mutant 4 skips it) |
| 1283 | fn pw P edit ulong 256 i ulong to ulong |
| 1284 | b = get P 56 |
| 1285 | if or eq b 0 eq get P 54 4 |
| 1286 | ret get P add 58 i |
| 1287 | end |
| 1288 | j = add i b |
| 1289 | if lt j 4 |
| 1290 | ret get P add 34 j |
| 1291 | end |
| 1292 | ret get P add 58 sub j 4 |
| 1293 | end |
| 1294 | |
| 1295 | note the guard of output c: c = 0 always stores; c > 0 skips its stores when c >= the store limit (last group) |
| 1296 | fn pgrd ins edit ulong 8192 m edit ulong 512 P edit ulong 256 c ulong to ulong |
| 1297 | if eq c 0 |
| 1298 | ret ALL |
| 1299 | end |
| 1300 | isge ins m get P add 48 c get P 52 |
| 1301 | ret bfwd ins m |
| 1302 | end |
| 1303 | |
| 1304 | fn pgend ins edit ulong 8192 m edit ulong 512 at ulong to void |
| 1305 | if ne at ALL |
| 1306 | patch ins m at |
| 1307 | end |
| 1308 | end |
| 1309 | |
| 1310 | note x as the f32 cuRAND builds it: (x >> 16) 65536 + (x & 0xffff), one rounding (FFMA of two exact I2FP) |
| 1311 | fn pfrn ins edit ulong 8192 m edit ulong 512 P edit ulong 256 x ulong to ulong |
| 1312 | a = tfo ins m P I2FP tsh ins m P x 16 NONE |
| 1313 | b = tfo ins m P I2FP tan ins m P x 65535 NONE |
| 1314 | ret tff ins m P a kq P 0 b |
| 1315 | end |
| 1316 | |
| 1317 | note curand_uniform: x 2^-32 + 2^-33 in one FFMA (mutant 3: x 2^-32) |
| 1318 | fn punif ins edit ulong 8192 m edit ulong 512 P edit ulong 256 x ulong to ulong |
| 1319 | f = pfrn ins m P x |
| 1320 | if eq get P 54 3 |
| 1321 | ret tfo ins m P FMUL f kq P 1 |
| 1322 | end |
| 1323 | ret tff ins m P f kq P 1 kq P 2 |
| 1324 | end |
| 1325 | |
| 1326 | note a 64-bit integer hi:lo (nonzero, < 2^54) times 2^-s as f64, exact: normalize the leading one to bit 63 of H:L, |
| 1327 | note then dhi = (H >> 11) + (e - 1) 2^20 and dlo = H 2^21 + (L >> 11). The exponent field is |
| 1328 | note nz 32 + eb + big + bias, eb the f32 exponent of the leading bit of the top nonzero word, bias = 895 - s |
| 1329 | note (863 for s = 32, 841 for s = 54 with big adding one when s = 53). Returns p2 dlo dhi. |
| 1330 | fn pdbl ins edit ulong 8192 m edit ulong 512 P edit ulong 256 hi ulong lo ulong big ulong to ulong |
| 1331 | x = imw ins m hi 1 get P 103 |
| 1332 | put P 224 sb x 1 |
| 1333 | x = tim ins m P get P 224 M1 get P 8 |
| 1334 | put P 225 tir ins m P lo x hi |
| 1335 | put P 226 tir ins m P lo get P 224 NONE |
| 1336 | x = get P 225 |
| 1337 | for i ulong 5 |
| 1338 | x = tlu ins m P x tsh ins m P x shl 1 i 252 |
| 1339 | end |
| 1340 | x = tfo ins m P LXOR x tsh ins m P x 1 |
| 1341 | x = tfo ins m P I2FP x NONE |
| 1342 | put P 227 tan ins m P tsh ins m P x 23 255 |
| 1343 | x = tim ins m P get P 227 M1 kq P 37 |
| 1344 | x = tfo ins m P SHFL get P 8 x |
| 1345 | y = tfo ins m P IMADHI get P 226 x |
| 1346 | put P 228 tir ins m P get P 225 x y |
| 1347 | put P 229 tir ins m P get P 226 x NONE |
| 1348 | x = tim ins m P get P 224 32 get P 227 |
| 1349 | if ne big NONE |
| 1350 | x = tfo ins m P IADD3 x big |
| 1351 | end |
| 1352 | x = tim ins m P x 1048576 kq P 38 |
| 1353 | y = tim ins m P tsh ins m P get P 228 11 1 x |
| 1354 | x = tim ins m P get P 228 2097152 tsh ins m P get P 229 11 |
| 1355 | ret p2 x y |
| 1356 | end |
| 1357 | |
| 1358 | note curand_uniform2_double (x, y): z = x ^ (y << 21) < 2^53, value z 2^-53 + 2^-54 rounded to nearest even: |
| 1359 | note z < 2^52 gives (2 z + 1) 2^-54 exactly; z >= 2^52 (big) gives (z + (z & 1)) 2^-53 (mutant 6: z 2^-53) |
| 1360 | fn phq ins edit ulong 8192 m edit ulong 512 P edit ulong 256 x ulong y ulong to ulong |
| 1361 | put P 224 tsh ins m P y 11 |
| 1362 | put P 225 tfo ins m P LXOR x tim ins m P y 2097152 get P 9 |
| 1363 | put P 226 tsh ins m P get P 224 20 |
| 1364 | put P 227 tim ins m P get P 226 M1 get P 8 |
| 1365 | x = tir ins m P get P 225 get P 227 NONE |
| 1366 | y = imw ins m get P 225 1 NONE |
| 1367 | y = imw ins m x 1 y |
| 1368 | x = get P 227 |
| 1369 | if ne get P 54 6 |
| 1370 | x = tir ins m P get P 226 tan ins m P get P 225 1 get P 227 |
| 1371 | end |
| 1372 | y = imw ins m x 1 y |
| 1373 | x = tir ins m P get P 224 tim ins m P get P 226 M1 kq P 41 sb y 1 |
| 1374 | ret pdbl ins m P x sb y 0 get P 226 |
| 1375 | end |
| 1376 | |
| 1377 | note curand_normal on the words (x, y): u = uniform(x), v = y 2^-32 2 pi + pi 2^-32 (two roundings), |
| 1378 | note s = sqrt(-2 log u), (s sin v, s cos v). log: u = 2^k m, m in [sqrt(2)/2, sqrt(2)), f = m - 1, |
| 1379 | note log1p f = f - f^2/2 + f^3 R(f) (minimax R), k ln 2 in two parts. sqrt: MUFU.RSQ y0, s0 = w y0, one Newton |
| 1380 | note step on the residual. sin / cos: quadrant k = round(2 v / pi) by the 1.5 2^23 trick, three-part Cody-Waite |
| 1381 | note reduction, odd / even polynomials, the quadrant applied as (A, B) = (|q| - 1, q (|q| - 2)), q = k - 2. |
| 1382 | note Returns p2 n0 n1 (mutant 5 swaps sine and cosine). |
| 1383 | fn pbm ins edit ulong 8192 m edit ulong 512 P edit ulong 256 x ulong y ulong to ulong |
| 1384 | put P 230 punif ins m P x |
| 1385 | x = pfrn ins m P y |
| 1386 | put P 231 tfo ins m P FADD tfo ins m P FMUL x kq P 3 kq P 4 |
| 1387 | x = tim ins m P get P 8 QLOGADD get P 230 |
| 1388 | y = tsh ins m P x 23 |
| 1389 | x = tim ins m P get P 8 QSQ2H tan ins m P x QMASK23 |
| 1390 | put P 232 tfo ins m P FADD x kq P 5 |
| 1391 | put P 233 tfo ins m P FADD tfo ins m P I2FP y NONE kq P 6 |
| 1392 | x = kq P 15 |
| 1393 | for i ulong 8 |
| 1394 | x = tff ins m P x get P 232 kq P sub 14 i |
| 1395 | end |
| 1396 | x = tff ins m P get P 232 x kq P 16 |
| 1397 | y = tfo ins m P FMUL get P 232 get P 232 |
| 1398 | x = tff ins m P y x get P 232 |
| 1399 | x = tff ins m P get P 233 kq P 17 tff ins m P get P 233 kq P 18 x |
| 1400 | put P 234 tfo ins m P FMUL x kq P 19 |
| 1401 | x = tfo ins m P FMAX get P 234 kq P 20 |
| 1402 | y = tnew m P |
| 1403 | emit ins m okk MRSQ KVAR 0 NONE r4 y x NONE NONE 0 |
| 1404 | put P 235 y |
| 1405 | x = tfo ins m P FMUL get P 234 y |
| 1406 | y = tff ins m P tfo ins m P FMUL x kq P 5 x get P 234 |
| 1407 | put P 236 tff ins m P y tfo ins m P FMUL get P 235 kq P 21 x |
| 1408 | x = tfo ins m P FADD tff ins m P get P 231 kq P 22 kq P 23 kq P 24 |
| 1409 | put P 237 x |
| 1410 | y = tff ins m P x kq P 25 get P 231 |
| 1411 | y = tff ins m P x kq P 26 y |
| 1412 | y = tff ins m P x kq P 27 y |
| 1413 | put P 238 y |
| 1414 | x = tfo ins m P FMUL y y |
| 1415 | y = tff ins m P x kq P 31 kq P 30 |
| 1416 | y = tff ins m P x y kq P 29 |
| 1417 | y = tff ins m P x y kq P 28 |
| 1418 | put P 240 tff ins m P tfo ins m P FMUL get P 238 x y get P 238 |
| 1419 | y = tff ins m P x kq P 35 kq P 34 |
| 1420 | y = tff ins m P x y kq P 33 |
| 1421 | y = tff ins m P x y kq P 32 |
| 1422 | y = tff ins m P x y kq P 16 |
| 1423 | put P 241 tff ins m P x y kq P 36 |
| 1424 | x = tfo ins m P FADD get P 237 kq P 19 |
| 1425 | y = tfo ins m P FMAX x tfo ins m P FMUL x kq P 5 |
| 1426 | put P 242 tfo ins m P FADD y kq P 5 |
| 1427 | put P 243 tfo ins m P FMUL x tfo ins m P FADD y kq P 19 |
| 1428 | x = tff ins m P get P 240 get P 242 tfo ins m P FMUL get P 241 get P 243 |
| 1429 | y = tff ins m P get P 241 get P 242 tfo ins m P FMUL get P 240 tfo ins m P FMUL get P 243 kq P 5 |
| 1430 | x = tfo ins m P FMUL x get P 236 |
| 1431 | y = tfo ins m P FMUL y get P 236 |
| 1432 | if eq get P 54 5 |
| 1433 | ret p2 y x |
| 1434 | end |
| 1435 | ret p2 x y |
| 1436 | end |
| 1437 | |
| 1438 | note store a 64-bit value (p2 lo hi) as element c of 8 bytes, under its guard |
| 1439 | fn pst8 ins edit ulong 8192 m edit ulong 512 P edit ulong 256 ad ulong c ulong d ulong to void |
| 1440 | at = pgrd ins m P c |
| 1441 | stg ins m ad and d 65535 mul c 8 get P 0 |
| 1442 | stg ins m ad shr d 16 add mul c 8 4 get P 0 |
| 1443 | pgend ins m at |
| 1444 | end |
| 1445 | |
| 1446 | note store a 32-bit value as element c of 4 bytes, under its guard |
| 1447 | fn pst4 ins edit ulong 8192 m edit ulong 512 P edit ulong 256 ad ulong c ulong x ulong to void |
| 1448 | at = pgrd ins m P c |
| 1449 | stg ins m ad x mul c 4 get P 0 |
| 1450 | pgend ins m at |
| 1451 | end |
| 1452 | |
| 1453 | note the outputs of one group, each stored under its guard as soon as it is computed (the pool restarts per output; |
| 1454 | note the second normal pair takes fresh vregs so no scratch register is held across both Box-Muller transforms) |
| 1455 | fn pdist ins edit ulong 8192 m edit ulong 512 P edit ulong 256 ad ulong to void |
| 1456 | a = get P 55 |
| 1457 | if lt a 2 |
| 1458 | for c ulong 4 |
| 1459 | put P 223 0 |
| 1460 | x = pw P c |
| 1461 | if eq a 1 |
| 1462 | x = punif ins m P x |
| 1463 | end |
| 1464 | pst4 ins m P ad c x |
| 1465 | end |
| 1466 | ret |
| 1467 | end |
| 1468 | if eq a 2 |
| 1469 | for c ulong 4 |
| 1470 | put P 223 0 |
| 1471 | x = imw ins m pw P c 1 get P 104 |
| 1472 | pst8 ins m P ad c pdbl ins m P sb x 1 sb x 0 NONE |
| 1473 | end |
| 1474 | ret |
| 1475 | end |
| 1476 | if eq a 3 |
| 1477 | for c ulong 2 |
| 1478 | put P 223 0 |
| 1479 | pst8 ins m P ad c phq ins m P pw P mul c 2 pw P add mul c 2 1 |
| 1480 | end |
| 1481 | ret |
| 1482 | end |
| 1483 | for h ulong 2 |
| 1484 | put P 223 0 |
| 1485 | if eq h 1 |
| 1486 | for i ulong 95 |
| 1487 | put P add 128 i 0 |
| 1488 | end |
| 1489 | end |
| 1490 | c = mul h 2 |
| 1491 | d = pbm ins m P pw P c pw P add c 1 |
| 1492 | pst4 ins m P ad c and d 65535 |
| 1493 | pst4 ins m P ad add c 1 shr d 16 |
| 1494 | end |
| 1495 | end |
| 1496 | |
| 1497 | note copy the block words into the previous block (P 34..37) |
| 1498 | fn pprev ins edit ulong 8192 m edit ulong 512 P edit ulong 256 to void |
| 1499 | for i ulong 4 |
| 1500 | ef ins m IMAD r4 get P add 34 i get P add 58 i NONE NONE 1 |
| 1501 | end |
| 1502 | end |
| 1503 | |
| 1504 | note per group: the block (b > 0: and the previous one), the outputs, counter low += 1, element index += ns G |
| 1505 | fn ploop ins edit ulong 8192 m edit ulong 512 P edit ulong 256 to void |
| 1506 | nr = ulong 10 |
| 1507 | if eq get P 54 2 |
| 1508 | nr = 9 |
| 1509 | end |
| 1510 | esz = ulong 4 |
| 1511 | if or eq get P 55 2 eq get P 55 3 |
| 1512 | esz = 8 |
| 1513 | end |
| 1514 | blk = get P 4 |
| 1515 | if gt get P 56 0 |
| 1516 | pblk ins m P blk nr |
| 1517 | pprev ins m P |
| 1518 | ef ins m IMADW r4 blk get P 8 NONE blk 1 |
| 1519 | end |
| 1520 | lbeg m |
| 1521 | pblk ins m P blk nr |
| 1522 | cnt = get P 5 |
| 1523 | x = sh ins m imi ins m cnt 1 get P 38 31 |
| 1524 | y = nv m 1 |
| 1525 | ef ins m IMAD r4 y x get P 30 get P 33 0 |
| 1526 | put P 52 y |
| 1527 | y = nv m 1 |
| 1528 | ef ins m IMAD r4 y x get P 31 get P 7 0 |
| 1529 | pdist ins m P imw ins m y esz get P 1 |
| 1530 | if gt get P 56 0 |
| 1531 | pprev ins m P |
| 1532 | end |
| 1533 | ef ins m IMADW r4 blk get P 8 NONE blk 1 |
| 1534 | x = get P 7 |
| 1535 | ef ins m IMAD r4 x get P 32 NONE x 1 |
| 1536 | ef ins m IMAD r4 cnt get P 8 NONE cnt M1 |
| 1537 | isge ins m get P 9 cnt |
| 1538 | bback ins m |
| 1539 | lend m |
| 1540 | end |
| 1541 | |
| 1542 | fn phik ins edit ulong 8192 m edit ulong 512 a ulong b ulong mut ulong to void |
| 1543 | P = new ulong 256 0 |
| 1544 | put P 54 mut |
| 1545 | put P 55 a |
| 1546 | put P 56 b |
| 1547 | g = ulong 4 |
| 1548 | if eq a 3 |
| 1549 | g = 2 |
| 1550 | end |
| 1551 | ppro ins m P g |
| 1552 | pkeys ins m P |
| 1553 | pkc ins m P |
| 1554 | for o ulong 2 |
| 1555 | q = add 40 mul o 4 |
| 1556 | put P q nv m 2 |
| 1557 | put P add q 1 nv m 2 |
| 1558 | put P add q 2 nv m 1 |
| 1559 | put P add q 3 nv m 1 |
| 1560 | end |
| 1561 | if gt b 0 |
| 1562 | for i ulong 4 |
| 1563 | put P add 34 i nv m 1 |
| 1564 | end |
| 1565 | end |
| 1566 | ploop ins m P |
| 1567 | tail ins m |
| 1568 | end |
| 1569 | |
| 1570 | note ---- kind 4: the real-FFT post-pass (b 0, split) and pre-pass (b 1, merge) for N = 2^a, n = N / 2. |
| 1571 | note One CTA of n / 2 threads per row; thread t takes the pair (t, n - t), and every thread also writes element n / 2 |
| 1572 | note (identical bits). Split: Z = FFT_n(x read as complex) in rows of n -> X in rows of n + 1 (cuFFT R2C layout): |
| 1573 | note E = (Z_t + conj Z_(n-t)) / 2, O = (Z_t - conj Z_(n-t)) / 2i, P = W^t O, X_t = E + P, X_(n-t) = conj (E - P), |
| 1574 | note X_(n/2) = conj Z_(n/2). Merge: X in rows of n + 1 -> Z' in rows of n, A = X_t + conj X_(n-t), |
| 1575 | note Q = W^-t (X_t - conj X_(n-t)), Z'_t = A + iQ, Z'_(n-t) = conj A + i conj Q, Z'_(n/2) = 2 conj X_(n/2); the |
| 1576 | note unnormalized inverse FFT_n of Z' is the C2R output (cuFFT convention: C2R(R2C(x)) = N x). |
| 1577 | note Arguments: 0x380 in, 0x388 out, 0x390 twiddles of N (forward for split, inverse for merge). |
| 1578 | note Q: 0 ureg desc, 1 in row, 2 out row, 3 twiddles, 4 tid, 5 n - tid, 6 (n - tid) mod n. |
| 1579 | note V: 0 first input pair, 1 partner pair, 2 twiddle pair, 3 / 4 E or A, 5 P or Q (p2 re im). |
| 1580 | note Mutants: 1 drops the twiddle multiply, 2 skips element n / 2. |
| 1581 | const FRH f32 0.5 |
| 1582 | const FRNH f32 -0.5 |
| 1583 | const FRN1 f32 -1.0 |
| 1584 | const FR2 f32 2.0 |
| 1585 | const FRN2 f32 -2.0 |
| 1586 | |
| 1587 | fn andi ins edit ulong 8192 m edit ulong 512 a ulong imm ulong to ulong |
| 1588 | d = nv m 1 |
| 1589 | ef ins m LAND r4 d a NONE NONE imm |
| 1590 | ret d |
| 1591 | end |
| 1592 | |
| 1593 | note (re + i im) times the twiddle pair w: a fresh p2 re im |
| 1594 | fn cmw ins edit ulong 8192 m edit ulong 512 re ulong im ulong w ulong to ulong |
| 1595 | t1 = fo ins m FMUL re sb w 0 |
| 1596 | t2 = fo ins m FMUL im sb w 1 |
| 1597 | t3 = fo ins m FMUL im sb w 0 |
| 1598 | i2 = ffm ins m re sb w 1 t3 |
| 1599 | ret p2 fo ins m FSUB t1 t2 i2 |
| 1600 | end |
| 1601 | |
| 1602 | fn rpro4 ins edit ulong 8192 m edit ulong 512 Q edit ulong 16 lg ulong dir ulong to void |
| 1603 | n = shl 1 sub lg 1 |
| 1604 | put Q 0 nu m 2 |
| 1605 | emit ins m okk LDCU64 KUVAR 1 NONE r4 get Q 0 NONE NONE NONE 856 |
| 1606 | put Q 1 vl ins m LDC64 2 NONE 896 |
| 1607 | put Q 2 vl ins m LDC64 2 NONE 904 |
| 1608 | put Q 3 vl ins m LDC64 2 NONE 912 |
| 1609 | c = vl ins m S2R 1 NONE 37 |
| 1610 | t = vl ins m S2R 1 NONE 33 |
| 1611 | put Q 4 t |
| 1612 | si = add n dir |
| 1613 | so = sub add n 1 dir |
| 1614 | put Q 1 imw ins m c mul si 8 get Q 1 |
| 1615 | put Q 2 imw ins m c mul so 8 get Q 2 |
| 1616 | v = imi ins m t M1 mov ins m n |
| 1617 | put Q 5 v |
| 1618 | put Q 6 andi ins m v sub n 1 |
| 1619 | end |
| 1620 | |
| 1621 | note V 0 = in[t], V 1 = in[(n - t) mod n] (split) or in[n - t] (merge), V 2 = twiddle t |
| 1622 | fn rld4 ins edit ulong 8192 m edit ulong 512 Q view ulong 16 V edit ulong 8 dir ulong to void |
| 1623 | u = get Q 0 |
| 1624 | put V 0 gl ins m imw ins m get Q 4 8 get Q 1 0 u |
| 1625 | put V 1 gl ins m imw ins m get Q sub 6 dir 8 get Q 1 0 u |
| 1626 | put V 2 gl ins m imw ins m get Q 4 8 get Q 3 0 u |
| 1627 | end |
| 1628 | |
| 1629 | fn rsplit ins edit ulong 8192 m edit ulong 512 V edit ulong 8 mut ulong to void |
| 1630 | a = get V 0 |
| 1631 | b = get V 1 |
| 1632 | h = mov ins m fb FRH |
| 1633 | put V 3 fo ins m FMUL fo ins m FADD sb a 0 sb b 0 h |
| 1634 | put V 4 fo ins m FMUL fo ins m FSUB sb a 1 sb b 1 h |
| 1635 | o1 = fo ins m FMUL fo ins m FADD sb a 1 sb b 1 h |
| 1636 | o2 = fo ins m FMUL fo ins m FSUB sb a 0 sb b 0 mov ins m fb FRNH |
| 1637 | p = p2 o1 o2 |
| 1638 | if ne mut 1 |
| 1639 | p = cmw ins m o1 o2 get V 2 |
| 1640 | end |
| 1641 | put V 5 p |
| 1642 | end |
| 1643 | |
| 1644 | note X_t = (Er + Pr, Ei + Pi), X_(n-t) = (Er - Pr, Pi - Ei) |
| 1645 | fn sst4 ins edit ulong 8192 m edit ulong 512 Q view ulong 16 V view ulong 8 to void |
| 1646 | er = get V 3 |
| 1647 | ei = get V 4 |
| 1648 | pr = and get V 5 65535 |
| 1649 | pi = shr get V 5 16 |
| 1650 | u = get Q 0 |
| 1651 | x = imw ins m get Q 4 8 get Q 2 |
| 1652 | stg ins m x fo ins m FADD er pr 0 u |
| 1653 | stg ins m x fo ins m FADD ei pi 4 u |
| 1654 | x = imw ins m get Q 5 8 get Q 2 |
| 1655 | stg ins m x fo ins m FSUB er pr 0 u |
| 1656 | stg ins m x fo ins m FSUB pi ei 4 u |
| 1657 | end |
| 1658 | |
| 1659 | fn rmerge ins edit ulong 8192 m edit ulong 512 V edit ulong 8 mut ulong to void |
| 1660 | a = get V 0 |
| 1661 | b = get V 1 |
| 1662 | put V 3 fo ins m FADD sb a 0 sb b 0 |
| 1663 | put V 4 fo ins m FSUB sb a 1 sb b 1 |
| 1664 | d1 = fo ins m FSUB sb a 0 sb b 0 |
| 1665 | d2 = fo ins m FADD sb a 1 sb b 1 |
| 1666 | q = p2 d1 d2 |
| 1667 | if ne mut 1 |
| 1668 | q = cmw ins m d1 d2 get V 2 |
| 1669 | end |
| 1670 | put V 5 q |
| 1671 | end |
| 1672 | |
| 1673 | note Z'_((n-t) mod n) = (Ar + Qi, Qr - Ai) first, then Z'_t = (Ar - Qi, Ai + Qr) (t = 0 keeps the second) |
| 1674 | fn mst4 ins edit ulong 8192 m edit ulong 512 Q view ulong 16 V view ulong 8 to void |
| 1675 | ar = get V 3 |
| 1676 | ai = get V 4 |
| 1677 | qr = and get V 5 65535 |
| 1678 | qi = shr get V 5 16 |
| 1679 | u = get Q 0 |
| 1680 | x = imw ins m get Q 6 8 get Q 2 |
| 1681 | stg ins m x fo ins m FADD ar qi 0 u |
| 1682 | stg ins m x fo ins m FSUB qr ai 4 u |
| 1683 | x = imw ins m get Q 4 8 get Q 2 |
| 1684 | stg ins m x fo ins m FSUB ar qi 0 u |
| 1685 | stg ins m x fo ins m FADD ai qr 4 u |
| 1686 | end |
| 1687 | |
| 1688 | note element n / 2: split conj Z, merge 2 conj X |
| 1689 | fn rmid ins edit ulong 8192 m edit ulong 512 Q view ulong 16 lg ulong dir ulong to void |
| 1690 | o = shl 1 add lg 1 |
| 1691 | u = get Q 0 |
| 1692 | z = gl ins m get Q 1 o u |
| 1693 | if eq dir 0 |
| 1694 | stg ins m get Q 2 sb z 0 o u |
| 1695 | stg ins m get Q 2 fo ins m FMUL sb z 1 mov ins m fb FRN1 add o 4 u |
| 1696 | ret |
| 1697 | end |
| 1698 | stg ins m get Q 2 fo ins m FMUL sb z 0 mov ins m fb FR2 o u |
| 1699 | stg ins m get Q 2 fo ins m FMUL sb z 1 mov ins m fb FRN2 add o 4 u |
| 1700 | end |
| 1701 | |
| 1702 | fn rk4 ins edit ulong 8192 m edit ulong 512 lg ulong dir ulong mut ulong to void |
| 1703 | Q = new ulong 16 0 |
| 1704 | V = new ulong 8 0 |
| 1705 | rpro4 ins m edit Q lg dir |
| 1706 | rld4 ins m view Q edit V dir |
| 1707 | if eq dir 0 |
| 1708 | rsplit ins m edit V mut |
| 1709 | sst4 ins m view Q view V |
| 1710 | else |
| 1711 | rmerge ins m edit V mut |
| 1712 | mst4 ins m view Q view V |
| 1713 | end |
| 1714 | if ne mut 2 |
| 1715 | rmid ins m view Q lg dir |
| 1716 | end |
| 1717 | tail ins m |
| 1718 | end |
| 1719 | |
| 1720 | note ---- kind 5: complex f32 transpose [batch][R][C] -> [batch][C][R], R = 2^a, C = 2^b; 256 threads, one element |
| 1721 | note each. Arguments: 0x380 in, 0x388 out, 0x390 element count, 0x394 first element of this launch. |
| 1722 | note Element g = (hi, r, c) goes to hi + c R + r. Mutants: 1 copies (identity), 2 drops the batch part hi. |
| 1723 | note T: 0 ureg desc, 1 in, 2 out, 3 g. |
| 1724 | fn trpro ins edit ulong 8192 m edit ulong 512 T edit ulong 8 to void |
| 1725 | put T 0 nu m 2 |
| 1726 | emit ins m okk LDCU64 KUVAR 1 NONE r4 get T 0 NONE NONE NONE 856 |
| 1727 | put T 1 vl ins m LDC64 2 NONE 896 |
| 1728 | put T 2 vl ins m LDC64 2 NONE 904 |
| 1729 | tot = vl ins m LDC 1 NONE 912 |
| 1730 | g0 = vl ins m LDC 1 NONE 916 |
| 1731 | c = vl ins m S2R 1 NONE 37 |
| 1732 | t = vl ins m S2R 1 NONE 33 |
| 1733 | g = fo ins m IADD3 imi ins m c 256 g0 t |
| 1734 | put T 3 g |
| 1735 | isge ins m g tot |
| 1736 | exitp ins m |
| 1737 | end |
| 1738 | |
| 1739 | fn trix ins edit ulong 8192 m edit ulong 512 g ulong lr ulong lc ulong mut ulong to ulong |
| 1740 | if eq mut 1 |
| 1741 | ret g |
| 1742 | end |
| 1743 | cc = andi ins m g sub shl 1 lc 1 |
| 1744 | rr = andi ins m sh ins m g lc sub shl 1 lr 1 |
| 1745 | o = imi ins m cc shl 1 lr rr |
| 1746 | if eq mut 2 |
| 1747 | ret o |
| 1748 | end |
| 1749 | k = shl 1 add lr lc |
| 1750 | k = and sub 0 k LO32 |
| 1751 | hi = andi ins m g k |
| 1752 | ret fo ins m IADD3 o hi |
| 1753 | end |
| 1754 | |
| 1755 | fn trk ins edit ulong 8192 m edit ulong 512 lr ulong lc ulong mut ulong to void |
| 1756 | T = new ulong 8 0 |
| 1757 | trpro ins m edit T |
| 1758 | g = get T 3 |
| 1759 | v = gl ins m imw ins m g 8 get T 1 0 get T 0 |
| 1760 | ad = imw ins m trix ins m g lr lc mut 8 get T 2 |
| 1761 | stg ins m ad sb v 0 0 get T 0 |
| 1762 | stg ins m ad sb v 1 4 get T 0 |
| 1763 | tail ins m |
| 1764 | end |
| 1765 | |
| 1766 | note ---- kind 6: the pass plan (data). a = mode | ndim << 4 (mode 0 complex ndim-D, 1 R2C, 2 C2R); b = log2 of the |
| 1767 | note dims, 4 bits each, dim 0 (outermost) at bits 0..3 (R2C / C2R: log2 N 3..12); c = direction (mode 0 only). |
| 1768 | note Word 0 = steps; word s + 1 = kind | a << 8 | b << 16 | src << 24 | dst << 32 | log2 rows per batch item << 40, |
| 1769 | note buffers 0 in, 1 out, 2 scratch. ndim-D: per round r, the FFT along dim ndim - 1 - r (contiguous) to scratch, |
| 1770 | note then the transpose [R][C] -> [C][R] back to out; after ndim rounds the layout is the input's again. |
| 1771 | fn pstep k ulong a ulong b ulong s ulong d ulong r ulong to ulong |
| 1772 | ret or or or k shl a 8 or shl b 16 shl s 24 or shl d 32 shl r 40 |
| 1773 | end |
| 1774 | |
| 1775 | fn plan1 ins edit ulong 8192 mode ulong lg ulong to ulong |
| 1776 | if or lt lg 3 gt lg 12 |
| 1777 | ret 1 |
| 1778 | end |
| 1779 | h = sub lg 1 |
| 1780 | put ins 0 2 |
| 1781 | if eq mode 1 |
| 1782 | put ins 1 pstep 1 h 0 0 2 0 |
| 1783 | put ins 2 pstep 4 lg 0 2 1 0 |
| 1784 | ret 0 |
| 1785 | end |
| 1786 | put ins 1 pstep 4 lg 1 0 2 0 |
| 1787 | put ins 2 pstep 1 h 1 2 1 0 |
| 1788 | ret 0 |
| 1789 | end |
| 1790 | |
| 1791 | fn plann ins edit ulong 8192 nd ulong b ulong dir ulong to ulong |
| 1792 | if ne shr b mul 4 nd 0 |
| 1793 | ret 1 |
| 1794 | end |
| 1795 | tot = ulong 0 |
| 1796 | for i ulong nd |
| 1797 | l = and shr b mul 4 i 15 |
| 1798 | if or lt l 2 gt l 11 |
| 1799 | ret 1 |
| 1800 | end |
| 1801 | tot = add tot l |
| 1802 | end |
| 1803 | if gt tot 27 |
| 1804 | ret 1 |
| 1805 | end |
| 1806 | if eq nd 1 |
| 1807 | put ins 0 1 |
| 1808 | put ins 1 pstep 1 b dir 0 1 0 |
| 1809 | ret 0 |
| 1810 | end |
| 1811 | put ins 0 mul nd 2 |
| 1812 | for r ulong nd |
| 1813 | l = and shr b mul 4 sub sub nd 1 r 15 |
| 1814 | s = ulong 1 |
| 1815 | if eq r 0 |
| 1816 | s = 0 |
| 1817 | end |
| 1818 | put ins add 1 mul r 2 pstep 1 l dir s 2 sub tot l |
| 1819 | put ins add 2 mul r 2 pstep 5 sub tot l l 2 1 0 |
| 1820 | end |
| 1821 | ret 0 |
| 1822 | end |
| 1823 | |
| 1824 | fn plan ins edit ulong 8192 a ulong b ulong c ulong to ulong |
| 1825 | mode = and a 15 |
| 1826 | nd = shr a 4 |
| 1827 | if gt mode 2 |
| 1828 | ret 1 |
| 1829 | end |
| 1830 | if gt mode 0 |
| 1831 | if or ne nd 1 ne c 0 |
| 1832 | ret 1 |
| 1833 | end |
| 1834 | ret plan1 ins mode b |
| 1835 | end |
| 1836 | if or or lt nd 1 gt nd 3 gt c 1 |
| 1837 | ret 1 |
| 1838 | end |
| 1839 | ret plann ins nd b c |
| 1840 | end |
| 1841 | |
| 1842 | note ---- kinds 7..13: general-length FFT (cufftPlanMany). Kind 7 is one Stockham radix-R pass (R 2, 3, 4, 5, 7), |
| 1843 | note kind 8 the four Bluestein steps (any other length through a power-of-two M >= 2N - 1), kind 9 the strided |
| 1844 | note gather / scatter / rotate copy, kinds 10..13 their data (twiddles of any n, chirp, factor plan, magic divisor). |
| 1845 | note The kernels run 128 threads per CTA, one butterfly or element per thread, chunked by a 'first' word like kind 5. |
| 1846 | note Every launch passes np 64-bit pointers at 0x380 + 8 i, then 32-bit words: word 0 total, word 1 first, ... |
| 1847 | const LDGE ulong 11 |
| 1848 | const SGN ulong 2147483648 |
| 1849 | |
| 1850 | note d = a * b + c (IMAD register form, low 32 bits); c may be NONE (zero) |
| 1851 | fn imr ins edit ulong 8192 m edit ulong 512 a ulong b ulong c ulong to ulong |
| 1852 | d = nv m 1 |
| 1853 | ef ins m IMAD r4 d a b c 0 |
| 1854 | ret d |
| 1855 | end |
| 1856 | |
| 1857 | note 32-bit global load (LDG.E) of [a] |
| 1858 | fn g32 ins edit ulong 8192 m edit ulong 512 a ulong u ulong to ulong |
| 1859 | d = nv m 1 |
| 1860 | emit ins m okk LDGE KVAR 0 u r4 d a NONE NONE 0 |
| 1861 | ret d |
| 1862 | end |
| 1863 | |
| 1864 | note x div d and x mod d for x < 2^32, 1 <= d < 2^31, mg = min(floor(2^32 / d), 2^32 - 1), nd = 2^32 - d: |
| 1865 | note q0 = hi32(x mg) is q - 1 or q; t = x - (q0 + 1) d is r - d < 0 (q0 = q) or r >= 0 (q0 = q - 1), so |
| 1866 | note inc = (~t) >> 31, q = q0 + inc, r = x - q d. Returns p2 q r. |
| 1867 | fn mdiv ins edit ulong 8192 m edit ulong 512 x ulong d ulong mg ulong to ulong |
| 1868 | nd = imi ins m d M1 NONE |
| 1869 | q = fo ins m IMADHI x mg |
| 1870 | t = fo ins m IADD3 imr ins m q nd x nd |
| 1871 | i = sh ins m lut ins m t t NONE 15 31 |
| 1872 | q = fo ins m IADD3 q i |
| 1873 | ret p2 q imr ins m q nd x |
| 1874 | end |
| 1875 | |
| 1876 | note G: 0 ureg desc, 1..3 pointers, 4 + w word w; returns g = ctaid 128 + tid + first after the exit on g >= total |
| 1877 | fn kpro ins edit ulong 8192 m edit ulong 512 G edit ulong 32 np ulong nw ulong to ulong |
| 1878 | u = nu m 2 |
| 1879 | put G 0 u |
| 1880 | emit ins m okk LDCU64 KUVAR 1 NONE r4 u NONE NONE NONE 856 |
| 1881 | for i ulong np |
| 1882 | put G add 1 i vl ins m LDC64 2 NONE add 896 mul i 8 |
| 1883 | end |
| 1884 | b = add 896 mul np 8 |
| 1885 | for j ulong nw |
| 1886 | put G add 4 j vl ins m LDC 1 NONE add b mul j 4 |
| 1887 | end |
| 1888 | c = vl ins m S2R 1 NONE 37 |
| 1889 | t = vl ins m S2R 1 NONE 33 |
| 1890 | g = fo ins m IADD3 imi ins m c 128 get G 5 t |
| 1891 | isge ins m g get G 4 |
| 1892 | exitp ins m |
| 1893 | ret g |
| 1894 | end |
| 1895 | |
| 1896 | note ---- kind 7: one Stockham pass, radix R = a (2, 3, 4, 5, 7), b 0 forward / 1 inverse, c mutant (1 the |
| 1897 | note butterfly runs the other direction, 2 drops the twiddle multiply). Pointers 0x380 in, 0x388 out, 0x390 |
| 1898 | note twiddles of N in the transform's direction. Words: 0 T butterflies, 1 first, 2 NR = N / R, 3 magic NR, 4 Ns, |
| 1899 | note 5 magic Ns, 6 s = N / (Ns R), 7 N. Butterfly g: row, j = g divmod NR; k = j mod Ns; v_r = in[row N + j + r NR] |
| 1900 | note times w^(r k s); X_m = sum_r v_r exp(-+2 pi i r m / R) goes to out[row N + R j - (R - 1) k + m Ns]. |
| 1901 | note G 20 row, 21 j, 22 k. |
| 1902 | note radix constants (Wolfram, f32 round-to-nearest bits): cos and sin of 2 pi j / R; s 0 cos, 1 sin |
| 1903 | fn rcv R ulong j ulong s ulong to ulong |
| 1904 | if eq R 3 |
| 1905 | if eq s 0 |
| 1906 | ret 3204448256 |
| 1907 | end |
| 1908 | ret 1063105495 |
| 1909 | end |
| 1910 | if eq R 5 |
| 1911 | if eq j 1 |
| 1912 | if eq s 0 |
| 1913 | ret 1050556282 |
| 1914 | end |
| 1915 | ret 1064532081 |
| 1916 | end |
| 1917 | if eq s 0 |
| 1918 | ret 3209632701 |
| 1919 | end |
| 1920 | ret 1058437400 |
| 1921 | end |
| 1922 | if eq j 1 |
| 1923 | if eq s 0 |
| 1924 | ret 1059036423 |
| 1925 | end |
| 1926 | ret 1061692956 |
| 1927 | end |
| 1928 | if eq j 2 |
| 1929 | if eq s 0 |
| 1930 | ret 3194215559 |
| 1931 | end |
| 1932 | ret 1064932576 |
| 1933 | end |
| 1934 | if eq s 0 |
| 1935 | ret 3211175397 |
| 1936 | end |
| 1937 | ret 1054746114 |
| 1938 | end |
| 1939 | |
| 1940 | note G 23 = address of in[row N + j], 24 = k s; V 2r, 2r + 1 = the twiddled inputs (operand refs) |
| 1941 | fn rld1 ins edit ulong 8192 m edit ulong 512 G view ulong 32 V edit ulong 16 r ulong mut ulong to void |
| 1942 | a = get G 23 |
| 1943 | if gt r 0 |
| 1944 | a = imw ins m get G 6 mul r 8 a |
| 1945 | end |
| 1946 | w = gl ins m a 0 get G 0 |
| 1947 | re = sb w 0 |
| 1948 | im = sb w 1 |
| 1949 | if gt r 0 |
| 1950 | if ne mut 2 |
| 1951 | a = imw ins m get G 24 mul r 8 get G 3 |
| 1952 | a = gl ins m a 0 get G 0 |
| 1953 | a = cmw ins m re im a |
| 1954 | re = and a 65535 |
| 1955 | im = shr a 16 |
| 1956 | end |
| 1957 | end |
| 1958 | put V mul r 2 re |
| 1959 | put V add 1 mul r 2 im |
| 1960 | end |
| 1961 | |
| 1962 | fn rld7 ins edit ulong 8192 m edit ulong 512 G edit ulong 32 V edit ulong 16 R ulong mut ulong to void |
| 1963 | a = imw ins m imr ins m get G 20 get G 11 get G 21 8 get G 1 |
| 1964 | put G 23 a |
| 1965 | if ne mut 2 |
| 1966 | e = imr ins m get G 22 get G 10 NONE |
| 1967 | put G 24 e |
| 1968 | end |
| 1969 | for r ulong R |
| 1970 | rld1 ins m view G edit V r mut |
| 1971 | end |
| 1972 | end |
| 1973 | |
| 1974 | fn dr2 ins edit ulong 8192 m edit ulong 512 V view ulong 16 Y edit ulong 16 to void |
| 1975 | put Y 0 fo ins m FADD get V 0 get V 2 |
| 1976 | put Y 1 fo ins m FADD get V 1 get V 3 |
| 1977 | put Y 2 fo ins m FSUB get V 0 get V 2 |
| 1978 | put Y 3 fo ins m FSUB get V 1 get V 3 |
| 1979 | end |
| 1980 | |
| 1981 | note X0 = s02 + s13, X2 = s02 - s13; forward X1 = d02 - i d13, X3 = d02 + i d13 (inverse swaps X1, X3) |
| 1982 | fn dr4 ins edit ulong 8192 m edit ulong 512 V view ulong 16 Y edit ulong 16 fw ulong to void |
| 1983 | a0 = fo ins m FADD get V 0 get V 4 |
| 1984 | a1 = fo ins m FADD get V 1 get V 5 |
| 1985 | b0 = fo ins m FADD get V 2 get V 6 |
| 1986 | b1 = fo ins m FADD get V 3 get V 7 |
| 1987 | put Y 0 fo ins m FADD a0 b0 |
| 1988 | put Y 1 fo ins m FADD a1 b1 |
| 1989 | put Y 4 fo ins m FSUB a0 b0 |
| 1990 | put Y 5 fo ins m FSUB a1 b1 |
| 1991 | a0 = fo ins m FSUB get V 0 get V 4 |
| 1992 | a1 = fo ins m FSUB get V 1 get V 5 |
| 1993 | b0 = fo ins m FSUB get V 2 get V 6 |
| 1994 | b1 = fo ins m FSUB get V 3 get V 7 |
| 1995 | k = ulong 2 |
| 1996 | l = ulong 6 |
| 1997 | if eq fw 0 |
| 1998 | k = 6 |
| 1999 | l = 2 |
| 2000 | end |
| 2001 | put Y k fo ins m FADD a0 b1 |
| 2002 | put Y add k 1 fo ins m FSUB a1 b0 |
| 2003 | put Y l fo ins m FSUB a0 b1 |
| 2004 | put Y add l 1 fo ins m FADD a1 b0 |
| 2005 | end |
| 2006 | |
| 2007 | note odd R, h = (R - 1) / 2. Work array W: 0..15 the inputs v (re, im refs); 16 + 2r, 17 + 2r = a_r = v_r + v_(R-r); |
| 2008 | note 24 + 2r, 25 + 2r = b_r = v_r - v_(R-r); 32 + j cos, 36 + j sin, 40 + j -sin of 2 pi j / R (j <= h); |
| 2009 | note 48 R, 49 forward flag, 50..53 the running P re, P im, Q re, Q im. |
| 2010 | fn dpre ins edit ulong 8192 m edit ulong 512 W edit ulong 64 to void |
| 2011 | R = get W 48 |
| 2012 | h = shr sub R 1 1 |
| 2013 | for i ulong h |
| 2014 | r = add i 1 |
| 2015 | p = mul r 2 |
| 2016 | q = mul sub R r 2 |
| 2017 | put W add p 16 fo ins m FADD get W p get W q |
| 2018 | put W add p 17 fo ins m FADD get W add p 1 get W add q 1 |
| 2019 | put W add p 24 fo ins m FSUB get W p get W q |
| 2020 | put W add p 25 fo ins m FSUB get W add p 1 get W add q 1 |
| 2021 | put W add r 32 mov ins m rcv R r 0 |
| 2022 | put W add r 36 mov ins m rcv R r 1 |
| 2023 | put W add r 40 mov ins m add rcv R r 1 SGN |
| 2024 | end |
| 2025 | end |
| 2026 | |
| 2027 | note one term r = i + 1 of output m: P += a_r C(rm), Q += b_r S(rm), with C(j) = C(R - j), S(j) = -S(R - j) for j > h |
| 2028 | fn dterm ins edit ulong 8192 m edit ulong 512 W edit ulong 64 mm ulong i ulong to void |
| 2029 | R = get W 48 |
| 2030 | h = shr sub R 1 1 |
| 2031 | r = mul add i 1 2 |
| 2032 | j = mul add i 1 mm |
| 2033 | j = sub j mul div j R R |
| 2034 | s = ulong 36 |
| 2035 | if gt j h |
| 2036 | j = sub R j |
| 2037 | s = 40 |
| 2038 | end |
| 2039 | put W 50 ffm ins m get W add r 16 get W add j 32 get W 50 |
| 2040 | put W 51 ffm ins m get W add r 17 get W add j 32 get W 51 |
| 2041 | if eq i 0 |
| 2042 | put W 52 fo ins m FMUL get W add r 24 get W add j s |
| 2043 | put W 53 fo ins m FMUL get W add r 25 get W add j s |
| 2044 | ret |
| 2045 | end |
| 2046 | put W 52 ffm ins m get W add r 24 get W add j s get W 52 |
| 2047 | put W 53 ffm ins m get W add r 25 get W add j s get W 53 |
| 2048 | end |
| 2049 | |
| 2050 | note forward X_m = (Pr + Qi, Pi - Qr), X_(R-m) = (Pr - Qi, Pi + Qr); inverse swaps them |
| 2051 | fn dodm ins edit ulong 8192 m edit ulong 512 W edit ulong 64 Y edit ulong 16 mm ulong to void |
| 2052 | R = get W 48 |
| 2053 | put W 50 get W 0 |
| 2054 | put W 51 get W 1 |
| 2055 | for i ulong shr sub R 1 1 |
| 2056 | dterm ins m edit W mm i |
| 2057 | end |
| 2058 | k = mm |
| 2059 | l = sub R mm |
| 2060 | if eq get W 49 0 |
| 2061 | k = sub R mm |
| 2062 | l = mm |
| 2063 | end |
| 2064 | put Y mul k 2 fo ins m FADD get W 50 get W 53 |
| 2065 | put Y add 1 mul k 2 fo ins m FSUB get W 51 get W 52 |
| 2066 | put Y mul l 2 fo ins m FSUB get W 50 get W 53 |
| 2067 | put Y add 1 mul l 2 fo ins m FADD get W 51 get W 52 |
| 2068 | end |
| 2069 | |
| 2070 | note W 54, 55 = X_0 = v_0 + sum a_r |
| 2071 | fn dsum ins edit ulong 8192 m edit ulong 512 W edit ulong 64 to void |
| 2072 | put W 54 get W 0 |
| 2073 | put W 55 get W 1 |
| 2074 | for i ulong shr sub get W 48 1 1 |
| 2075 | r = mul add i 1 2 |
| 2076 | put W 54 fo ins m FADD get W 54 get W add r 16 |
| 2077 | put W 55 fo ins m FADD get W 55 get W add r 17 |
| 2078 | end |
| 2079 | end |
| 2080 | |
| 2081 | fn dodd ins edit ulong 8192 m edit ulong 512 V view ulong 16 Y edit ulong 16 R ulong fw ulong to void |
| 2082 | W = new ulong 64 0 |
| 2083 | for i ulong 16 |
| 2084 | put W i get V i |
| 2085 | end |
| 2086 | put W 48 R |
| 2087 | put W 49 fw |
| 2088 | dpre ins m edit W |
| 2089 | dsum ins m edit W |
| 2090 | put Y 0 get W 54 |
| 2091 | put Y 1 get W 55 |
| 2092 | for i ulong shr sub R 1 1 |
| 2093 | dodm ins m edit W edit Y add i 1 |
| 2094 | end |
| 2095 | end |
| 2096 | |
| 2097 | fn rst7 ins edit ulong 8192 m edit ulong 512 G view ulong 32 Y view ulong 16 R ulong to void |
| 2098 | u = get G 0 |
| 2099 | t = imi ins m get G 22 sub 4294967296 sub R 1 NONE |
| 2100 | t = imi ins m get G 21 R t |
| 2101 | oa = imw ins m imr ins m get G 20 get G 11 t 8 get G 2 |
| 2102 | for k ulong R |
| 2103 | a = oa |
| 2104 | if gt k 0 |
| 2105 | a = imw ins m get G 8 mul k 8 oa |
| 2106 | end |
| 2107 | stg ins m a get Y mul k 2 0 u |
| 2108 | stg ins m a get Y add 1 mul k 2 4 u |
| 2109 | end |
| 2110 | end |
| 2111 | |
| 2112 | fn rpk ins edit ulong 8192 m edit ulong 512 R ulong dir ulong mut ulong to void |
| 2113 | G = new ulong 32 0 |
| 2114 | V = new ulong 16 0 |
| 2115 | Y = new ulong 16 0 |
| 2116 | g = kpro ins m edit G 3 8 |
| 2117 | x = mdiv ins m g get G 6 get G 7 |
| 2118 | put G 20 and x 65535 |
| 2119 | put G 21 shr x 16 |
| 2120 | x = mdiv ins m shr x 16 get G 8 get G 9 |
| 2121 | put G 22 shr x 16 |
| 2122 | rld7 ins m edit G edit V R mut |
| 2123 | fw = eq dir 0 |
| 2124 | if eq mut 1 |
| 2125 | fw = ne dir 0 |
| 2126 | end |
| 2127 | f = ulong 0 |
| 2128 | if fw |
| 2129 | f = 1 |
| 2130 | end |
| 2131 | if eq R 2 |
| 2132 | dr2 ins m view V edit Y |
| 2133 | end |
| 2134 | if eq R 4 |
| 2135 | dr4 ins m view V edit Y f |
| 2136 | end |
| 2137 | if eq and R 1 1 |
| 2138 | dodd ins m view V edit Y R f |
| 2139 | end |
| 2140 | rst7 ins m view G view Y R |
| 2141 | tail ins m |
| 2142 | end |
| 2143 | |
| 2144 | note ---- kind 8: Bluestein steps over rows of M = 2^b (b 5..13); a variant, c mutant. Pointers 0x380 src, |
| 2145 | note 0x388 dst, 0x390 aux; words 0 total, 1 first, 2 N. g = row M + n. |
| 2146 | note a 0 pre: dst[g] = 0, then for n < N dst[g] = src[row N + n] chirp[n] (mutant 1: no chirp); |
| 2147 | note a 1 mul: dst[g] = src[g] B[n] (in place allowed); |
| 2148 | note a 2 post: for n < N dst[row N + n] = src[g] chirp[n] 2^-b (mutant 2: no 2^-b); |
| 2149 | note a 3 B build over g < M: j = min(g, M - g); dst[g] = conj chirp[j] for j < N, else 0. |
| 2150 | note chirp[n] = exp(-+i pi n^2 / N) (kind 11). G 20 n, 21 row. |
| 2151 | fn bpre ins edit ulong 8192 m edit ulong 512 G view ulong 32 g ulong mut ulong to void |
| 2152 | wa = imw ins m g 8 get G 2 |
| 2153 | x = mov ins m 0 |
| 2154 | stg ins m wa x 0 get G 0 |
| 2155 | stg ins m wa x 4 get G 0 |
| 2156 | isge ins m get G 20 get G 6 |
| 2157 | exitp ins m |
| 2158 | x = imr ins m get G 21 get G 6 get G 20 |
| 2159 | x = gl ins m imw ins m x 8 get G 1 0 get G 0 |
| 2160 | p = p2 sb x 0 sb x 1 |
| 2161 | if ne mut 1 |
| 2162 | p = gl ins m imw ins m get G 20 8 get G 3 0 get G 0 |
| 2163 | p = cmw ins m sb x 0 sb x 1 p |
| 2164 | end |
| 2165 | stg ins m wa and p 65535 0 get G 0 |
| 2166 | stg ins m wa shr p 16 4 get G 0 |
| 2167 | end |
| 2168 | |
| 2169 | fn bmul ins edit ulong 8192 m edit ulong 512 G view ulong 32 g ulong to void |
| 2170 | e = gl ins m imw ins m g 8 get G 1 0 get G 0 |
| 2171 | p = gl ins m imw ins m get G 20 8 get G 3 0 get G 0 |
| 2172 | p = cmw ins m sb e 0 sb e 1 p |
| 2173 | e = imw ins m g 8 get G 2 |
| 2174 | stg ins m e and p 65535 0 get G 0 |
| 2175 | stg ins m e shr p 16 4 get G 0 |
| 2176 | end |
| 2177 | |
| 2178 | fn bpost ins edit ulong 8192 m edit ulong 512 G view ulong 32 g ulong lg ulong mut ulong to void |
| 2179 | isge ins m get G 20 get G 6 |
| 2180 | exitp ins m |
| 2181 | e = gl ins m imw ins m g 8 get G 1 0 get G 0 |
| 2182 | p = gl ins m imw ins m get G 20 8 get G 3 0 get G 0 |
| 2183 | p = cmw ins m sb e 0 sb e 1 p |
| 2184 | re = and p 65535 |
| 2185 | im = shr p 16 |
| 2186 | if ne mut 2 |
| 2187 | e = mov ins m shl sub 127 lg 23 |
| 2188 | re = fo ins m FMUL re e |
| 2189 | im = fo ins m FMUL im e |
| 2190 | end |
| 2191 | e = imr ins m get G 21 get G 6 get G 20 |
| 2192 | e = imw ins m e 8 get G 2 |
| 2193 | stg ins m e re 0 get G 0 |
| 2194 | stg ins m e im 4 get G 0 |
| 2195 | end |
| 2196 | |
| 2197 | fn bbld ins edit ulong 8192 m edit ulong 512 G view ulong 32 g ulong lg ulong to void |
| 2198 | wa = imw ins m g 8 get G 2 |
| 2199 | c = mov ins m 0 |
| 2200 | stg ins m wa c 0 get G 0 |
| 2201 | stg ins m wa c 4 get G 0 |
| 2202 | c = imi ins m g M1 mov ins m shl 1 lg |
| 2203 | d = imi ins m c M1 g |
| 2204 | d = imr ins m sh ins m d 31 d c |
| 2205 | isge ins m d get G 6 |
| 2206 | exitp ins m |
| 2207 | c = gl ins m imw ins m d 8 get G 3 0 get G 0 |
| 2208 | stg ins m wa sb c 0 0 get G 0 |
| 2209 | d = mov ins m SGN |
| 2210 | stg ins m wa fo ins m LXOR sb c 1 d 4 get G 0 |
| 2211 | end |
| 2212 | |
| 2213 | fn bsk ins edit ulong 8192 m edit ulong 512 v ulong lg ulong mut ulong to void |
| 2214 | G = new ulong 32 0 |
| 2215 | g = kpro ins m edit G 3 3 |
| 2216 | if eq v 3 |
| 2217 | bbld ins m view G g lg |
| 2218 | tail ins m |
| 2219 | ret |
| 2220 | end |
| 2221 | put G 20 andi ins m g sub shl 1 lg 1 |
| 2222 | put G 21 sh ins m g lg |
| 2223 | if eq v 0 |
| 2224 | bpre ins m view G g mut |
| 2225 | end |
| 2226 | if eq v 1 |
| 2227 | bmul ins m view G g |
| 2228 | end |
| 2229 | if eq v 2 |
| 2230 | bpost ins m view G g lg mut |
| 2231 | end |
| 2232 | tail ins m |
| 2233 | end |
| 2234 | |
| 2235 | note ---- kind 9: strided copy of total elements, a mode (0 complex, 1 real -> complex with im 0, 2 complex -> real |
| 2236 | note part, 3 complex conjugated), c mutant (1 drops the conjugate, 2 drops the source batch stride). Pointers 0x380 |
| 2237 | note src, 0x388 dst. Words: 0 total, 1 first, 2 d0, 3 magic d0, 4 d1, 5 magic d1, 6 d2, 7 magic d2, 8 sbase, 9 sb, |
| 2238 | note 10 s0, 11 s1, 12 s2, 13 dbase, 14 db, 15 t0, 16 t1, 17 t2. Element g = ((b d2 + i2) d1 + i1) d0 + i0 reads |
| 2239 | note src[sbase + b sb + i2 s2 + i1 s1 + i0 s0] and writes dst[dbase + b db + i2 t2 + i1 t1 + i0 t0]; index |
| 2240 | note arithmetic is mod 2^32, so a stride may be negative (2^32 - s) when the final index is in range. |
| 2241 | note G 24 i0, 25 i1, 26 i2, 27 b. |
| 2242 | fn cidx ins edit ulong 8192 m edit ulong 512 G view ulong 32 o ulong mut ulong to ulong |
| 2243 | s = imr ins m get G 24 get G add o 2 get G o |
| 2244 | s = imr ins m get G 25 get G add o 3 s |
| 2245 | s = imr ins m get G 26 get G add o 4 s |
| 2246 | if ne mut 2 |
| 2247 | s = imr ins m get G 27 get G add o 1 s |
| 2248 | end |
| 2249 | ret s |
| 2250 | end |
| 2251 | |
| 2252 | note G 28 source index, 29 destination index |
| 2253 | fn cmv ins edit ulong 8192 m edit ulong 512 G view ulong 32 mode ulong mut ulong to void |
| 2254 | u = get G 0 |
| 2255 | if eq mode 1 |
| 2256 | v = g32 ins m imw ins m get G 28 4 get G 1 u |
| 2257 | da = imw ins m get G 29 8 get G 2 |
| 2258 | stg ins m da v 0 u |
| 2259 | stg ins m da mov ins m 0 4 u |
| 2260 | ret |
| 2261 | end |
| 2262 | w = gl ins m imw ins m get G 28 8 get G 1 0 u |
| 2263 | if eq mode 2 |
| 2264 | stg ins m imw ins m get G 29 4 get G 2 sb w 0 0 u |
| 2265 | ret |
| 2266 | end |
| 2267 | im = sb w 1 |
| 2268 | if eq mode 3 |
| 2269 | if ne mut 1 |
| 2270 | im = fo ins m LXOR im mov ins m SGN |
| 2271 | end |
| 2272 | end |
| 2273 | da = imw ins m get G 29 8 get G 2 |
| 2274 | stg ins m da sb w 0 0 u |
| 2275 | stg ins m da im 4 u |
| 2276 | end |
| 2277 | |
| 2278 | fn cpk ins edit ulong 8192 m edit ulong 512 mode ulong mut ulong to void |
| 2279 | G = new ulong 32 0 |
| 2280 | g = kpro ins m edit G 2 18 |
| 2281 | x = mdiv ins m g get G 6 get G 7 |
| 2282 | put G 24 shr x 16 |
| 2283 | x = mdiv ins m and x 65535 get G 8 get G 9 |
| 2284 | put G 25 shr x 16 |
| 2285 | x = mdiv ins m and x 65535 get G 10 get G 11 |
| 2286 | put G 26 shr x 16 |
| 2287 | put G 27 and x 65535 |
| 2288 | s = cidx ins m view G 12 mut |
| 2289 | put G 28 s |
| 2290 | s = cidx ins m view G 17 0 |
| 2291 | put G 29 s |
| 2292 | cmv ins m view G mode mut |
| 2293 | tail ins m |
| 2294 | end |
| 2295 | |
| 2296 | note ---- kinds 10..13 (data). 10: twiddles w^k, k < a (a 2..8192), b direction. 11: chirp exp(-+i pi n^2 / a), |
| 2297 | note n < a (a 2..4096), b direction, as w^(n^2 mod 2a) of 2a. 12: factor plan of a (2..4096): word 0 the pass length |
| 2298 | note M, 1 Bluestein flag, 2 passes, 3.. radices (4s, one 2, 3s, 5s, 7s); a length with another prime factor runs |
| 2299 | note Bluestein on M = the least power of two >= 2a - 1. 13: word 0 = magic min(floor(2^32 / a), 2^32 - 1), a 1..2^31-1. |
| 2300 | fn chirp ins edit ulong 8192 n ulong dir ulong to void |
| 2301 | t = mul n 2 |
| 2302 | for i ulong n |
| 2303 | s = mul i i |
| 2304 | put ins i twg sub s mul div s t t t dir |
| 2305 | end |
| 2306 | end |
| 2307 | |
| 2308 | fn fpr ins edit ulong 8192 x ulong R ulong lim ulong to ulong |
| 2309 | y = x |
| 2310 | for i ulong lim |
| 2311 | if eq sub y mul div y R R 0 |
| 2312 | k = get ins 2 |
| 2313 | put ins add 3 k R |
| 2314 | put ins 2 add k 1 |
| 2315 | y = div y R |
| 2316 | end |
| 2317 | end |
| 2318 | ret y |
| 2319 | end |
| 2320 | |
| 2321 | fn fpl ins edit ulong 8192 n ulong to ulong |
| 2322 | if or lt n 2 gt n 4096 |
| 2323 | ret 1 |
| 2324 | end |
| 2325 | for i ulong 32 |
| 2326 | put ins i 0 |
| 2327 | end |
| 2328 | put ins 0 n |
| 2329 | x = fpr ins n 4 6 |
| 2330 | x = fpr ins x 2 1 |
| 2331 | x = fpr ins x 3 7 |
| 2332 | x = fpr ins x 5 5 |
| 2333 | x = fpr ins x 7 4 |
| 2334 | if eq x 1 |
| 2335 | ret 0 |
| 2336 | end |
| 2337 | mm = ulong 1 |
| 2338 | for i ulong 14 |
| 2339 | if lt mm sub mul n 2 1 |
| 2340 | mm = mul mm 2 |
| 2341 | end |
| 2342 | end |
| 2343 | for i ulong 32 |
| 2344 | put ins i 0 |
| 2345 | end |
| 2346 | put ins 0 mm |
| 2347 | put ins 1 1 |
| 2348 | x = fpr ins mm 4 7 |
| 2349 | x = fpr ins x 2 1 |
| 2350 | ret 0 |
| 2351 | end |
| 2352 | |
| 2353 | fn gdata ins edit ulong 8192 kind ulong a ulong b ulong c ulong to ulong |
| 2354 | if ne c 0 |
| 2355 | ret 1 |
| 2356 | end |
| 2357 | if eq kind 10 |
| 2358 | if or or lt a 2 gt a 8192 gt b 1 |
| 2359 | ret 1 |
| 2360 | end |
| 2361 | twid ins a b |
| 2362 | ret 0 |
| 2363 | end |
| 2364 | if eq kind 11 |
| 2365 | if or or lt a 2 gt a 4096 gt b 1 |
| 2366 | ret 1 |
| 2367 | end |
| 2368 | chirp ins a b |
| 2369 | ret 0 |
| 2370 | end |
| 2371 | if ne b 0 |
| 2372 | ret 1 |
| 2373 | end |
| 2374 | if gt kind 13 |
| 2375 | ret 1 |
| 2376 | end |
| 2377 | if eq kind 12 |
| 2378 | x = fpl ins a |
| 2379 | ret x |
| 2380 | end |
| 2381 | if or lt a 1 gt a 2147483647 |
| 2382 | ret 1 |
| 2383 | end |
| 2384 | put ins 0 M1 |
| 2385 | if gt a 1 |
| 2386 | put ins 0 div PW1 a |
| 2387 | end |
| 2388 | ret 0 |
| 2389 | end |
| 2390 | |
| 2391 | note ---- entry. kind 0 RNG (a variant 0..2, b 0, c mutant 0..2); kind 1 FFT (a log2 n 2..11, b direction |
| 2392 | note 0 forward / 1 inverse, c mutant 0..2); kind 2 twiddles (a log2 n 2..12, b direction, c 0); kind 3 Philox |
| 2393 | note distributions (a distribution 0..4, b offset mod 4, c mutant 0..6); kind 4 real-FFT split / merge (a log2 N |
| 2394 | note 3..12, b 0 split / 1 merge, c mutant 0..2); |
| 2395 | note kind 5 transpose (a log2 R 2..25, b log2 C 2..11, a + b <= 27, c mutant 0..2); kind 6 pass plan (data); |
| 2396 | note kind 7 Stockham pass (a radix 2, 3, 4, 5, 7, b direction, c mutant 0..2); kind 8 Bluestein step (a variant |
| 2397 | note 0..3, b log2 M 5..13, c mutant 0..2); kind 9 strided copy (a mode 0..3, b 0, c mutant 0..2); kinds 10..13 |
| 2398 | note data (twiddles of any n, chirp, factor plan, magic divisor). |
| 2399 | note Returns 0 built, 1 refused arguments, 2 emitter error (meta 4 holds the code). |
| 2400 | fn mathlibs_ir kind ulong a ulong b ulong c ulong ins edit ulong 8192 meta edit ulong 512 to ulong |
| 2401 | for i ulong 512 |
| 2402 | put meta i 0 |
| 2403 | end |
| 2404 | if eq kind 2 |
| 2405 | if or or lt a 2 gt a 12 or gt b 1 ne c 0 |
| 2406 | ret 1 |
| 2407 | end |
| 2408 | twid ins shl 1 a b |
| 2409 | ret 0 |
| 2410 | end |
| 2411 | if gt kind 9 |
| 2412 | ret gdata ins kind a b c |
| 2413 | end |
| 2414 | if eq kind 6 |
| 2415 | ret plan ins a b c |
| 2416 | end |
| 2417 | if eq kind 0 |
| 2418 | if or or gt a 2 ne b 0 gt c 2 |
| 2419 | ret 1 |
| 2420 | end |
| 2421 | rngk ins meta a c |
| 2422 | end |
| 2423 | if eq kind 1 |
| 2424 | if or or lt a 2 gt a 11 or gt b 1 gt c 2 |
| 2425 | ret 1 |
| 2426 | end |
| 2427 | fftk ins meta a b c |
| 2428 | end |
| 2429 | if eq kind 3 |
| 2430 | if or or gt a 4 gt b 3 gt c 6 |
| 2431 | ret 1 |
| 2432 | end |
| 2433 | phik ins meta a b c |
| 2434 | end |
| 2435 | if eq kind 4 |
| 2436 | if or or lt a 3 gt a 12 or gt b 1 gt c 2 |
| 2437 | ret 1 |
| 2438 | end |
| 2439 | rk4 ins meta a b c |
| 2440 | end |
| 2441 | if eq kind 5 |
| 2442 | if or or lt a 2 gt a 25 or lt b 2 gt b 11 |
| 2443 | ret 1 |
| 2444 | end |
| 2445 | if or gt add a b 27 gt c 2 |
| 2446 | ret 1 |
| 2447 | end |
| 2448 | trk ins meta a b c |
| 2449 | end |
| 2450 | if eq kind 7 |
| 2451 | if or or gt c 2 gt b 1 or or lt a 2 gt a 7 eq a 6 |
| 2452 | ret 1 |
| 2453 | end |
| 2454 | rpk ins meta a b c |
| 2455 | end |
| 2456 | if eq kind 8 |
| 2457 | if or or gt a 3 gt c 2 or lt b 5 gt b 13 |
| 2458 | ret 1 |
| 2459 | end |
| 2460 | bsk ins meta a b c |
| 2461 | end |
| 2462 | if eq kind 9 |
| 2463 | if or or gt a 3 ne b 0 gt c 2 |
| 2464 | ret 1 |
| 2465 | end |
| 2466 | cpk ins meta a c |
| 2467 | end |
| 2468 | if gt kind 9 |
| 2469 | ret 1 |
| 2470 | end |
| 2471 | if ne get meta 4 0 |
| 2472 | ret 2 |
| 2473 | end |
| 2474 | if ne get meta 6 0 |
| 2475 | ret 2 |
| 2476 | end |
| 2477 | sched ins meta |
| 2478 | ret 0 |
| 2479 | end |