interplane.Get Omega
Read in your language

omega-mv

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

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / nvidia / om / mathlibs / mathlibs_ir.om

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