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 / om / gpumath / gpumath.om

1244 lines · 24773 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gpumath.om: seeded Omega port of src/omega_gpu_hostmath.c, src/omega_gpu_fp8_pack.c and
3note src/omega_gpu_fmt.c (aien-dev/omega-mv#726). Parity is on bit patterns against the C as the
4note product builds it (gcc -std=gnu11 -O2, the Makefile CFLAGS, aarch64). Where the C does float
5note arithmetic this file does the same IEEE operation in the same operand order (the operand order
6note decides which NaN payload survives). gcc contracts some of the C's a*b+c into one fused
7note FMADD/FMSUB (gnu11 implies -ffp-contract=fast); Omega has no fused operation, so fma64 below
8note computes the fused result exactly in integer arithmetic.
9note
10note Exported functions (omega-bridge native call ABI v1: ulong/long scalars, fixed-length
11note ulong/uchar arrays, ulong/long/void result). A double or float travels as its bit pattern.
12note hmlog x omega_hostmath_log, f64 bits in and out
13note hmexp y omega_hostmath_exp
14note hmpow b e omega_hostmath_pow
15note hmsincos a o omega_hostmath_sincos: o 0 sin bits, o 1 cos bits
16note hmpow2f e omega_hostmath_pow2f: f32 bits
17note e4m3ref b omega_fp8_e4m3_ref: f32 bits (b 0..255, else trap 5)
18note e4m3kbits b omega_fp8_e4m3_kernel_bits
19note e4m3isnan b omega_fp8_e4m3_is_nan: 1 or 0
20note f8scannan w n base st omega_fp8_pack_scan_nan over a 4096-byte piece (see the function)
21note f8packscales i n o nf omega_fp8_pack_scales over a 4096-element piece
22note f8packxf32 i n o omega_fp8_pack_x_f32 over a 4096-element piece
23note f8packxbf16 i n o omega_fp8_pack_x_bf16 over a 4096-element piece
24note f8gemvblock w x sb acc one 128-column block of one row of omega_fp8_gemv_oracle
25note gpufmt o cap f args pl omega_gpu_fmt with the varargs as 64-bit words (see the function)
26note gpufmtkinds f kinds the va_arg type of every conversion gpufmt consumes
27note Arrays carry little-endian element bytes exactly as the C arrays hold them in memory.
28
29note ---- 128-bit helpers and the exact fused multiply-add ----
30
31note index of the highest set bit of x (x > 0)
32fn msb64 x ulong to ulong
33n = ulong 0
34if ge x 4294967296
35x = shr x 32
36n = add n 32
37end
38if ge x 65536
39x = shr x 16
40n = add n 16
41end
42if ge x 256
43x = shr x 8
44n = add n 8
45end
46if ge x 16
47x = shr x 4
48n = add n 4
49end
50if ge x 4
51x = shr x 2
52n = add n 2
53end
54if ge x 2
55n = add n 1
56end
57ret n
58end
59
60fn isnan64 x ulong to bool
61ret gt and x 9223372036854775807 9218868437227405312
62end
63
64fn issnan64 x ulong to bool
65ret and isnan64 x eq and x 2251799813685248 0
66end
67
68fn isinf64 x ulong to bool
69ret eq and x 9223372036854775807 9218868437227405312
70end
71
72fn iszero64 x ulong to bool
73ret eq and x 9223372036854775807 0
74end
75
76note significand of a finite nonzero double, shifted so that bit 52 is its top bit
77fn mantn x ulong to ulong
78ex = and shr x 52 2047
79m = and x 4503599627370495
80if gt ex 0
81ret or m 4503599627370496
82end
83ret shl m sub 52 msb64 m
84end
85
86note exponent matching mantn: value = mantn x * 2^expn x
87fn expn x ulong to long
88ex = and shr x 52 2047
89if gt ex 0
90ret sub long ex 1075
91end
92m = and x 4503599627370495
93ret sub -1074 long sub 52 msb64 m
94end
95
96note t 0, t 1 = high, low words of a * b (a, b < 2^53)
97fn mul128 a ulong b ulong t edit ulong 8 to void
98al = and a 4294967295
99ah = shr a 32
100bl = and b 4294967295
101bh = shr b 32
102ll = mul al bl
103mid = add add mul al bh mul ah bl shr ll 32
104put t 1 or shl mid 32 and ll 4294967295
105put t 0 add mul ah bh shr mid 32
106end
107
108note shift the 128-bit value t k (high), t k+1 (low) right by n, OR-ing every lost bit into bit 0
109fn srj t edit ulong 8 k ulong n ulong to void
110if eq n 0
111ret
112end
113hi = get t k
114lo = get t add k 1
115if ge n 128
116put t k 0
117if eq or hi lo 0
118put t add k 1 0
119else
120put t add k 1 1
121end
122ret
123end
124st = ulong 0
125if ge n 64
126m = sub n 64
127nlo = hi
128if gt m 0
129nlo = shr hi m
130if ne shl hi sub 64 m 0
131st = 1
132end
133end
134if ne lo 0
135st = 1
136end
137put t k 0
138put t add k 1 or nlo st
139ret
140end
141if ne shl lo sub 64 n 0
142st = 1
143end
144put t add k 1 or or shr lo n shl hi sub 64 n st
145put t k shr hi n
146end
147
148note t 4, t 5 = (t 0, t 1) + (t 2, t 3)
149fn add128 t edit ulong 8 to void
150lo = add get t 1 get t 3
151cy = ulong 0
152if lt lo get t 1
153cy = 1
154end
155put t 5 lo
156put t 4 add add get t 0 get t 2 cy
157end
158
159note t 4, t 5 = (t a, t a+1) - (t b, t b+1), the first not below the second
160fn sub128 t edit ulong 8 a ulong b ulong to void
161l1 = get t add a 1
162l2 = get t add b 1
163bw = ulong 0
164if lt l1 l2
165bw = 1
166end
167put t 5 sub l1 l2
168put t 4 sub sub get t a get t b bw
169end
170
171fn ge128 t edit ulong 8 to bool
172if ne get t 0 get t 2
173ret gt get t 0 get t 2
174end
175ret ge get t 1 get t 3
176end
177
178note bit k of the 128-bit value hi, lo
179fn bit128 hi ulong lo ulong k ulong to ulong
180if ge k 64
181ret and shr hi sub k 64 1
182end
183ret and shr lo k 1
184end
185
186note any bit of hi, lo below bit k
187fn low128 hi ulong lo ulong k ulong to bool
188if ge k 64
189ret or ne lo 0 ne and hi sub shl ulong 1 sub k 64 1 0
190end
191ret ne and lo sub shl ulong 1 k 1 0
192end
193
194note round S = (t 4, t 5) * 2^x (S > 0) to nearest even as a double with sign sg; subnormal
195note results round once at 2^-1074, overflow gives infinity (ARM FPRound, FPCR at its Linux default)
196fn fround t edit ulong 8 x long sg ulong to ulong
197hi = get t 4
198lo = get t 5
199h = msb64 lo
200if ne hi 0
201h = add msb64 hi 64
202end
203s = sub long h 52
204mn = sub -1074 x
205if lt s mn
206s = mn
207end
208sgn = shl sg 63
209if ge s 128
210ret sgn
211end
212q = ulong 0
213if gt s 0
214us = ulong s
215if ge us 64
216q = shr hi sub us 64
217else
218q = or shr lo us shl hi sub 64 us
219end
220if eq bit128 hi lo sub us 1 1
221if or low128 hi lo sub us 1 eq and q 1 1
222q = add q 1
223end
224end
225else
226q = shl lo ulong neg s
227end
228bits = add shl ulong add add x s 1074 52 q
229if ge bits 9218868437227405312
230ret or sgn 9218868437227405312
231end
232ret or sgn bits
233end
234
235note the NaN result of c + a * b when an operand is NaN (FPProcessNaNs3, addend first; a quiet NaN
236note addend with infinity times zero gives the default NaN)
237fn fmanan a ulong b ulong c ulong to ulong
238if issnan64 c
239ret or c 2251799813685248
240end
241if issnan64 a
242ret or a 2251799813685248
243end
244if issnan64 b
245ret or b 2251799813685248
246end
247if isnan64 c
248if or and isinf64 a iszero64 b and iszero64 a isinf64 b
249ret 9221120237041090560
250end
251ret c
252end
253if isnan64 a
254ret a
255end
256ret b
257end
258
259note c + a * b with infinities or zeros among the operands (no NaN operand)
260fn fmaspec a ulong b ulong c ulong to ulong
261sp = xor shr a 63 shr b 63
262sc = shr c 63
263infp = or isinf64 a isinf64 b
264infc = isinf64 c
265if or and isinf64 a iszero64 b and iszero64 a isinf64 b
266ret 9221120237041090560
267end
268if and and infc infp ne sc sp
269ret 9221120237041090560
270end
271if or and infc eq sc 0 and infp eq sp 0
272ret 9218868437227405312
273end
274if or infc infp
275ret 18442240474082181120
276end
277if iszero64 c
278if eq sc sp
279ret c
280end
281ret 0
282end
283ret c
284end
285
286note the exact product a * b (a, b finite nonzero) and the addend c placed in t 0..1 and t 2..3,
287note both with their top bit at bit 124 or 125, the smaller shifted right with a sticky bit to the
288note larger's scale; returns that scale x (value = t * 2^x)
289fn fmaalign a ulong b ulong c ulong t edit ulong 8 to long
290mul128 mantn a mantn b t
291ph = get t 0
292pl = get t 1
293put t 0 or shl ph 20 shr pl 44
294put t 1 shl pl 20
295xp = sub add expn a expn b 20
296if iszero64 c
297ret xp
298end
299put t 2 shl mantn c 9
300xc = sub expn c 73
301d = sub xc xp
302if gt d 0
303srj t 0 ulong d
304ret xc
305end
306srj t 2 ulong neg d
307ret xp
308end
309
310note c + a * b, finite, a and b nonzero: the exact sum rounded once
311fn fmafin a ulong b ulong c ulong to ulong
312t = new ulong 8 0
313x = fmaalign a b c t
314sp = xor shr a 63 shr b 63
315sc = shr c 63
316sg = sp
317if or iszero64 c eq sp sc
318add128 t
319else
320if ge128 t
321sub128 t 0 2
322else
323sub128 t 2 0
324sg = sc
325end
326end
327if eq or get t 4 get t 5 0
328ret 0
329end
330ret fround t x sg
331end
332
333note AArch64 FMADD d = c + a * b (one rounding, ARM NaN rules); FMSUB d = c - a * b is
334note fma64 with the sign of a flipped
335fn fma64 a ulong b ulong c ulong to ulong
336if or or isnan64 a isnan64 b isnan64 c
337ret fmanan a b c
338end
339if or or or isinf64 a isinf64 b isinf64 c or iszero64 a iszero64 b
340ret fmaspec a b c
341end
342ret fmafin a b c
343end
344
345note ---- omega_gpu_hostmath.c ----
346
347note pow2i: 2^k as a double, -1074 <= k <= 1023, 0 below, +inf above
348fn pow2i k long to f64
349if gt k 1023
350ret bitcast f64 ulong 9218868437227405312
351end
352if lt k -1074
353ret 0.0f64
354end
355if ge k -1022
356ret bitcast f64 shl ulong add k 1023 52
357end
358ret mul bitcast f64 ulong 4503599627370496 bitcast f64 shl ulong add k 2045 52
359end
360
361note log(x): the s * t2 + 1/k step and the two final steps are fused in the C object
362fn hmlog xb ulong to ulong
363x = bitcast f64 xb
364if or not gt x 0.0f64 gt x bitcast f64 ulong 9218868437227405311
365ret 9221120237041090560
366end
367e = long 0
368if lt x bitcast f64 ulong 4503599627370496
369x = mul x bitcast f64 ulong 4850376798678024192
370e = -54
371end
372u = bitcast ulong x
373e = add e sub long and shr u 52 2047 1023
374m = bitcast f64 or and u 4503599627370495 4607182418800017408
375if gt m bitcast f64 ulong 4609047870845172685
376m = mul m 0.5f64
377e = add e 1
378end
379t = div sub m 1.0f64 add m 1.0f64
380t2 = bitcast ulong mul t t
381s = ulong 0
382k = long 25
383while ge k 1 bound 13
384s = fma64 s t2 bitcast ulong div 1.0f64 f64 k
385k = sub k 2
386end
387ed = f64 e
388lo = bitcast ulong mul ed bitcast f64 ulong 4461442080421002358
389lo = fma64 s bitcast ulong add t t lo
390ret fma64 bitcast ulong ed 4604418534311723008 lo
391end
392
393note exp(y): r = (y - k * ln2hi) - k * ln2lo is two FMSUBs in the C object
394fn hmexp yb ulong to ulong
395y = bitcast f64 yb
396if ne y y
397ret yb
398end
399if gt y bitcast f64 ulong 4649454506724284170
400ret 9218868437227405312
401end
402if lt y bitcast f64 ulong 13873138101193906586
403ret 0
404end
405fk = mul y bitcast f64 ulong 4609176140021203710
406h = -0.5f64
407if ge fk 0.0f64
408h = 0.5f64
409end
410k = long add fk h
411nk = bitcast ulong neg f64 k
412r = fma64 nk 4604418534311723008 yb
413r = fma64 nk 4461442080421002358 r
414rd = bitcast f64 r
415p = 1.0f64
416n = long 17
417while ge n 1 bound 17
418p = add div mul rd p f64 n 1.0f64
419n = sub n 1
420end
421if lt k -1020
422ret bitcast ulong mul mul p pow2i -1020 pow2i add k 1020
423end
424ret bitcast ulong mul p pow2i k
425end
426
427fn hmpow bb ulong eb ulong to ulong
428b = bitcast f64 bb
429e = bitcast f64 eb
430if or eq b 1.0f64 eq e 0.0f64
431ret 4607182418800017408
432end
433ret hmexp bitcast ulong mul bitcast f64 hmlog bb e
434end
435
436note sin r = r * ps, cos r = pc (w = r * r), the two series of the C
437fn sinser w f64 to f64
438ps = 1.0f64
439k = long 15
440while ge k 3 bound 7
441ps = sub 1.0f64 div mul w ps f64 mul k sub k 1
442k = sub k 2
443end
444ret ps
445end
446
447fn cosser w f64 to f64
448pc = 1.0f64
449k = long 16
450while ge k 2 bound 8
451pc = sub 1.0f64 div mul w pc f64 mul k sub k 1
452k = sub k 2
453end
454ret pc
455end
456
457note sincos(a): o 0 = sin bits, o 1 = cos bits; the four reduction steps are FMSUBs in the C object
458fn hmsincos ab ulong o edit ulong 2 to void
459a = bitcast f64 ab
460fq = mul a bitcast f64 ulong 4603909380684499075
461h = -0.5f64
462if ge fq 0.0f64
463h = 0.5f64
464end
465n = long add fq h
466nn = bitcast ulong neg f64 n
467r = fma64 nn 4609753056924401664 ab
468r = fma64 nn 4454258360616747008 r
469r = fma64 nn 4297306550709518336 r
470r = fma64 nn 4142048980368378305 r
471rd = bitcast f64 r
472w = mul rd rd
473sr = mul rd sinser w
474cr = cosser w
475q = and n 3
476if eq q 0
477put o 0 bitcast ulong sr
478put o 1 bitcast ulong cr
479ret
480end
481if eq q 1
482put o 0 bitcast ulong cr
483put o 1 bitcast ulong neg sr
484ret
485end
486if eq q 2
487put o 0 bitcast ulong neg sr
488put o 1 bitcast ulong neg cr
489ret
490end
491put o 0 bitcast ulong neg cr
492put o 1 bitcast ulong sr
493end
494
495note 2^e as f32 bits, exact for -149 <= e <= 127; 0 below, +inf above
496fn hmpow2f e long to ulong
497if gt e 127
498ret 2139095040
499end
500if ge e -126
501ret shl ulong add e 127 23
502end
503if ge e -149
504ret shl ulong 1 add e 149
505end
506ret 0
507end
508
509note ---- omega_gpu_fp8_pack.c ----
510
511note f32 helpers return the bit pattern (omc refuses an f32 result type, error 5)
512fn p2f e long to uint
513u = hmpow2f e
514ret uint u
515end
516
517fn e4m3ref bb ulong to ulong
518b = uchar bb
519ex = and shr b 3 15
520mf = f32 and b 7
521sg = 1.0f32
522if ne and b 128 0
523sg = -1.0f32
524end
525if and eq ex 15 eq and b 7 7
526ret 2143289344
527end
528if eq ex 0
529ret ulong bitcast uint mul mul sg mf bitcast f32 p2f -9
530end
531v = mul add 1.0f32 div mf 8.0f32 sg
532ret ulong bitcast uint mul v bitcast f32 p2f sub long ex 7
533end
534
535fn e4m3kbits bb ulong to ulong
536b = ulong uchar bb
537ret or shl and b 127 20 shl and b 128 24
538end
539
540fn e4m3isnan bb ulong to ulong
541b = uchar bb
542if eq and b 127 127
543ret 1
544end
545ret 0
546end
547
548note one piece of omega_fp8_pack_scan_nan(w, count, first): scans w 0 .. n-1 (n <= 4096), the
549note piece starting at element base of the whole array. st 0 is the NaN count so far (0 before the
550note first piece), st 1 the index of the first NaN, written only when the count was 0 (as the C
551note writes *first). Returns st 0.
552fn f8scannan w view uchar 4096 n ulong base ulong st edit ulong 2 to ulong
553for i ulong n
554if eq and get w i 127 127
555if eq get st 0 0
556put st 1 add base i
557end
558put st 0 add get st 0 1
559end
560end
561ret get st 0
562end
563
564fn ldbf16 v view uchar 8192 i ulong to uint
565lo = uint get v mul i 2
566hi = uint get v add mul i 2 1
567ret shl or lo shl hi 8 16
568end
569
570fn ldf32 v view uchar 16384 i ulong to uint
571o = mul i 4
572x = uint get v add o 3
573x = or shl x 8 uint get v add o 2
574x = or shl x 8 uint get v add o 1
575x = or shl x 8 uint get v o
576ret x
577end
578
579fn stf32 v edit uchar 16384 i ulong f f32 to void
580o = mul i 4
581x = bitcast uint f
582put v o uchar and x 255
583put v add o 1 uchar and shr x 8 255
584put v add o 2 uchar and shr x 16 255
585put v add o 3 uchar shr x 24
586end
587
588note one piece of omega_fp8_pack_scales: in holds n bf16 (n <= 4096), out gets n f32; nofold
589note nonzero is the C's mutant_no_fold. 0, or -1 at the first NaN/inf scale or fold overflow with
590note the earlier outputs written, as the C.
591fn f8packscales in view uchar 8192 n ulong out edit uchar 16384 nofold ulong to long
592fold = 1.0f32
593if eq nofold 0
594fold = bitcast f32 p2f 56
595end
596lim = bitcast f32 uint 2137108966
597for i ulong n
598s = bitcast f32 ldbf16 in i
599if not le fabs s lim
600ret -1
601end
602f = mul fold s
603if not le fabs f lim
604ret -1
605end
606stf32 out i f
607end
608ret 0
609end
610
611note one piece of omega_fp8_pack_x_f32: out = in * 2^64, n <= 4096 f32
612fn f8packxf32 in view uchar 16384 n ulong out edit uchar 16384 to void
613fold = bitcast f32 p2f 64
614for i ulong n
615stf32 out i mul bitcast f32 ldf32 in i fold
616end
617end
618
619note one piece of omega_fp8_pack_x_bf16: out = bf16 in * 2^64, n <= 4096
620fn f8packxbf16 in view uchar 8192 n ulong out edit uchar 16384 to void
621fold = bitcast f32 p2f 64
622for i ulong n
623stf32 out i mul bitcast f32 ldbf16 in i fold
624end
625end
626
627fn ldx v view uchar 512 i ulong to uint
628o = mul i 4
629x = uint get v add o 3
630x = or shl x 8 uint get v add o 2
631x = or shl x 8 uint get v add o 1
632x = or shl x 8 uint get v o
633ret x
634end
635
636note one 128-column block of one row of omega_fp8_gemv_oracle: w the row's 128 e4m3 bytes, x the
637note 128 f32 of x, sb the bf16 scale of the block, acc 0 / acc 1 the row's acc / mag (f64 bits,
638note 0 before the row's first block). The caller runs the blocks of a row in order, as the C.
639fn f8gemvblock w view uchar 128 x view uchar 512 sb ulong acc edit ulong 2 to void
640s = f64 bitcast f32 shl uint ushort sb 16
641a = bitcast f64 get acc 0
642mg = bitcast f64 get acc 1
643for c ulong 128
644rf = f64 bitcast f32 uint e4m3ref ulong get w c
645p = mul mul f64 bitcast f32 ldx x c rf s
646a = add a p
647mg = add mg fabs p
648end
649put acc 0 bitcast ulong a
650put acc 1 bitcast ulong mg
651end
652
653note ---- omega_gpu_fmt.c ----
654
655note the sink: sn 0 = characters written, sn 1 = cap
656fn fput out edit uchar 1024 sn edit ulong 2 c uchar to void
657n = get sn 0
658if lt add n 1 get sn 1
659put out n c
660put sn 0 add n 1
661end
662end
663
664note a %s argument: an offset into the string pool pl (NUL-terminated there), or 2^64-1 for NULL
665fn fputs out edit uchar 1024 sn edit ulong 2 pl view uchar 4096 off ulong to void
666if eq off 18446744073709551615
667fput out sn 40
668fput out sn 110
669fput out sn 117
670fput out sn 108
671fput out sn 108
672fput out sn 41
673ret
674end
675i = off
676while ne get pl i 0 bound 4096
677fput out sn get pl i
678i = add i 1
679end
680end
681
682fn fputu out edit uchar 1024 sn edit ulong 2 v ulong base ulong to void
683tmp = new uchar 24 0
684i = ulong 0
685while true bound 24
686d = rem v base
687if lt d 10
688put tmp i uchar add d 48
689else
690put tmp i uchar add d 87
691end
692i = add i 1
693v = div v base
694if eq v 0
695break
696end
697end
698while gt i 0 bound 24
699i = sub i 1
700fput out sn get tmp i
701end
702end
703
704fn fputd out edit uchar 1024 sn edit ulong 2 v long to void
705if lt v 0
706fput out sn 45
707fputu out sn add ulong neg add v 1 1 10
708ret
709end
710fputu out sn ulong v 10
711end
712
713note the low 32 bits of w as a signed int
714fn sx32 w ulong to long
715v = and w 4294967295
716if ge v 2147483648
717ret sub long v 4294967296
718end
719ret long v
720end
721
722note the conversion at fmt p (fmt p is '%'): r 0 = index of the conversion byte, r 1 = the va_arg
723note kind it consumes (0 none; 1 int, 2 long, 3 long long, 4 ptrdiff_t, 5 unsigned int,
724note 6 unsigned long, 7 unsigned long long, 8 size_t, 9 const char *)
725fn fscan fmt view uchar 1024 p ulong r edit ulong 4 to void
726q = add p 1
727z = false
728l = ulong 0
729if eq get fmt q 122
730z = true
731q = add q 1
732else
733while and eq get fmt q 108 lt l 2 bound 3
734l = add l 1
735q = add q 1
736end
737end
738ch = get fmt q
739k = ulong 0
740if eq ch 115
741k = 9
742end
743if eq ch 99
744k = 1
745end
746if eq ch 100
747k = 1
748if eq l 1
749k = 2
750end
751if eq l 2
752k = 3
753end
754if z
755k = 4
756end
757end
758if or eq ch 117 eq ch 120
759k = 5
760if eq l 1
761k = 6
762end
763if eq l 2
764k = 7
765end
766if z
767k = 8
768end
769end
770put r 0 q
771put r 1 k
772end
773
774note one consuming conversion: argument word w of kind k, conversion byte cc
775fn fconv out edit uchar 1024 sn edit ulong 2 w ulong k ulong cc uchar pl view uchar 4096 to void
776if eq k 9
777fputs out sn pl w
778ret
779end
780if eq cc 99
781fput out sn uchar and w 255
782ret
783end
784if eq k 1
785fputd out sn sx32 w
786ret
787end
788if lt k 5
789fputd out sn bitcast long w
790ret
791end
792base = ulong 10
793if eq cc 120
794base = 16
795end
796if eq k 5
797fputu out sn and w 4294967295 base
798ret
799end
800fputu out sn w base
801end
802
803note one '%' conversion (r 0, r 1 from fscan, r 2 the next argument, r 3 the '%' index)
804fn fone out edit uchar 1024 sn edit ulong 2 fmt view uchar 1024 args view ulong 16 pl view uchar 4096 r edit ulong 4 to void
805q = get r 0
806k = get r 1
807cc = get fmt q
808if gt k 0
809fconv out sn get args get r 2 k cc pl
810put r 2 add get r 2 1
811ret
812end
813if eq cc 37
814fput out sn 37
815ret
816end
817note unsupported conversion: copied literally, nothing consumed
818j = get r 3
819while le j q bound 5
820if eq get fmt j 0
821break
822end
823fput out sn get fmt j
824j = add j 1
825end
826end
827
828note omega_gpu_fmt(out, cap, fmt, ...): fmt NUL-terminated inside its 1024 bytes; the i-th consuming
829note conversion takes args i (16 at most): an int-sized kind uses the low 32 bits (the AAPCS64
830note va_arg of an int), %c the low 8; %s takes a pool offset (2^64-1 = NULL, printed "(null)").
831note Writes at most cap - 1 characters and the NUL (cap <= 1024, else out-of-range trap 3 once
832note the output reaches 1024); cap 0 writes nothing. Returns the characters written.
833fn gpufmt out edit uchar 1024 cap ulong fmt view uchar 1024 args view ulong 16 pl view uchar 4096 to ulong
834if eq cap 0
835ret 0
836end
837sn = new ulong 2 0
838r = new ulong 4 0
839put sn 1 cap
840p = ulong 0
841while ne get fmt p 0 bound 1024
842if ne get fmt p 37
843fput out sn get fmt p
844p = add p 1
845continue
846end
847fscan fmt p r
848put r 3 p
849fone out sn fmt args pl r
850q = get r 0
851p = add q 1
852if eq get fmt q 0
853p = q
854end
855end
856put out get sn 0 0
857ret get sn 0
858end
859
860note the va_arg kinds (fscan codes) of fmt's consuming conversions, in order; returns their count
861fn gpufmtkinds fmt view uchar 1024 kinds edit ulong 64 to ulong
862r = new ulong 4 0
863p = ulong 0
864nk = ulong 0
865while ne get fmt p 0 bound 1024
866if ne get fmt p 37
867p = add p 1
868continue
869end
870fscan fmt p r
871q = get r 0
872k = get r 1
873if gt k 0
874put kinds nk k
875nk = add nk 1
876end
877p = add q 1
878if eq get fmt q 0
879p = q
880end
881end
882ret nk
883end
884
885note ==== parity harness: program mode only; everything above this line is the library ====
886note Reads corpus records on stdin (om/gpumath/parity/corpus.py), writes one result line per record.
887note Numbers are lowercase hex, byte strings one hex token ("-" when empty).
888
889global ib uchar 1048576
890global ist ulong 2
891global ob uchar 1048576
892global obn ulong 1
893global tb uchar 262144
894global gw uchar 262144
895global gsc uchar 8192
896global gx uchar 131072
897
898fn inb to ulong
899p = get ist 0
900if ge p get ist 1
901r = read 0 ib 0 1048576
902if le r 0
903ret 256
904end
905put ist 1 ulong r
906p = 0
907end
908put ist 0 add p 1
909ret ulong get ib p
910end
911
912fn obflush to void
913n = get obn 0
914if gt n 0
915r = write 1 ob 0 n
916end
917put obn 0 0
918end
919
920fn ob1 c ulong to void
921n = get obn 0
922if ge n 1048576
923obflush
924n = 0
925end
926put ob n uchar c
927put obn 0 add n 1
928end
929
930fn hexdig v ulong to ulong
931if lt v 10
932ret add v 48
933end
934ret add v 87
935end
936
937fn ohex v ulong to void
938ob1 32
939for i ulong 16
940ob1 hexdig and shr v mul sub 15 i 4 15
941end
942end
943
944fn ohexb b ulong to void
945ob1 hexdig shr b 4
946ob1 hexdig and b 15
947end
948
949fn hexval c ulong to ulong
950if and ge c 48 le c 57
951ret sub c 48
952end
953if and ge c 97 le c 102
954ret sub c 87
955end
956ret 16
957end
958
959fn rdcode to ulong
960c = inb
961while or eq c 10 eq c 32 bound 999999
962c = inb
963end
964ret c
965end
966
967fn rdhex to ulong
968c = inb
969while eq c 32 bound 999999
970c = inb
971end
972v = ulong 0
973while lt hexval c 16 bound 17
974v = or shl v 4 hexval c
975c = inb
976end
977ret v
978end
979
980note one hex byte-string token into tb; returns the byte count ("-" is empty)
981fn rdbytes to ulong
982c = inb
983while eq c 32 bound 999999
984c = inb
985end
986if eq c 45
987c = inb
988ret 0
989end
990n = ulong 0
991while lt hexval c 16 bound 999999
992d = inb
993put tb n uchar or shl hexval c 4 hexval d
994n = add n 1
995c = inb
996end
997ret n
998end
999
1000fn sgnl v ulong to long
1001if ge v 9223372036854775808
1002ret neg long add not v 1
1003end
1004ret long v
1005end
1006
1007fn u32at v view uchar 16384 i ulong to ulong
1008o = mul i 4
1009x = ulong get v add o 3
1010x = or shl x 8 ulong get v add o 2
1011x = or shl x 8 ulong get v add o 1
1012ret or shl x 8 ulong get v o
1013end
1014
1015fn doz to void
1016a = rdhex
1017b = rdhex
1018c = rdhex
1019ob1 90
1020ohex fma64 a b c
1021end
1022
1023fn dosc to void
1024o = new ulong 2 0
1025a = rdhex
1026hmsincos a o
1027ob1 68
1028ohex get o 0
1029ohex get o 1
1030end
1031
1032fn dof to void
1033b = rdhex
1034ob1 70
1035ohex e4m3ref b
1036ohex e4m3kbits b
1037ohex e4m3isnan b
1038end
1039
1040note G nofold bf16-bytes / H f32-bytes / I bf16-bytes: one piece of the pack functions
1041fn dopack c ulong to void
1042in = new uchar 8192 0
1043inf = new uchar 16384 0
1044out = new uchar 16384 170
1045nf = ulong 0
1046if eq c 71
1047nf = rdhex
1048end
1049nb = rdbytes
1050n = div nb 2
1051if eq c 72
1052n = div nb 4
1053end
1054for i ulong nb
1055if eq c 72
1056put inf i get tb i
1057else
1058put in i get tb i
1059end
1060end
1061ob1 c
1062if eq c 71
1063ohex bitcast ulong f8packscales in n out nf
1064end
1065if eq c 72
1066f8packxf32 inf n out
1067end
1068if eq c 73
1069f8packxbf16 in n out
1070end
1071for i ulong n
1072ohex u32at out i
1073end
1074end
1075
1076note N bytes: omega_fp8_pack_scan_nan over the whole array, in 4096-byte pieces
1077fn doscan to void
1078w = new uchar 4096 0
1079st = new ulong 2 0
1080put st 1 12297829382473034410
1081nb = rdbytes
1082base = ulong 0
1083while lt base nb bound 64
1084m = sub nb base
1085if gt m 4096
1086m = 4096
1087end
1088for i ulong m
1089put w i get tb add base i
1090end
1091r = f8scannan w m base st
1092base = add base m
1093end
1094ob1 78
1095ohex get st 0
1096ohex get st 1
1097end
1098
1099fn ldblk wb edit uchar 128 xb edit uchar 512 wo ulong xo ulong to void
1100for c ulong 128
1101put wb c get gw add wo c
1102end
1103for c ulong 512
1104put xb c get gx add xo c
1105end
1106end
1107
1108fn gsb i ulong to ulong
1109si = mul i 2
1110ret or ulong get gsc si shl ulong get gsc add si 1 8
1111end
1112
1113note one row r of the gemv held in gw, gsc, gx: the adapter's block loop (acc and mag start at 0)
1114fn gemvrow wb edit uchar 128 xb edit uchar 512 acc edit ulong 2 r ulong k ulong to void
1115kb = div k 128
1116so = mul div r 128 kb
1117put acc 0 0
1118put acc 1 0
1119for b ulong kb
1120ldblk wb xb add mul r k mul b 128 mul b 512
1121f8gemvblock wb xb gsb add so b acc
1122end
1123end
1124
1125note Q n k w-bytes scale-bytes x-bytes: omega_fp8_gemv_oracle row by row, block by block
1126fn dogemv to void
1127wb = new uchar 128 0
1128xb = new uchar 512 0
1129acc = new ulong 2 0
1130n = rdhex
1131k = rdhex
1132nb = rdbytes
1133for i ulong nb
1134put gw i get tb i
1135end
1136nb = rdbytes
1137for i ulong nb
1138put gsc i get tb i
1139end
1140nb = rdbytes
1141for i ulong nb
1142put gx i get tb i
1143end
1144ob1 81
1145for r ulong n
1146gemvrow wb xb acc r k
1147ohex get acc 0
1148ohex get acc 1
1149end
1150end
1151
1152note M cap fmt-bytes nargs args... pool-bytes: omega_gpu_fmt and its va_arg kinds
1153fn dofmt to void
1154out = new uchar 1024 170
1155fm = new uchar 1024 0
1156args = new ulong 16 0
1157pl = new uchar 4096 0
1158kd = new ulong 64 0
1159cap = rdhex
1160nb = rdbytes
1161for i ulong nb
1162put fm i get tb i
1163end
1164na = rdhex
1165for i ulong na
1166put args i rdhex
1167end
1168nb = rdbytes
1169for i ulong nb
1170put pl i get tb i
1171end
1172n = gpufmt out cap fm args pl
1173nk = gpufmtkinds fm kd
1174ob1 77
1175ohex n
1176ob1 32
1177if eq nk 0
1178ob1 45
1179end
1180for i ulong nk
1181ob1 hexdig get kd i
1182end
1183ob1 32
1184m = add n 2
1185if gt m cap
1186m = cap
1187end
1188if eq m 0
1189ob1 45
1190end
1191for i ulong m
1192ohexb ulong get out i
1193end
1194end
1195
1196fn main to int
1197while true bound 999999
1198c = rdcode
1199if eq c 256
1200break
1201end
1202if eq c 90
1203doz
1204end
1205if eq c 65
1206ob1 65
1207ohex hmlog rdhex
1208end
1209if eq c 66
1210ob1 66
1211ohex hmexp rdhex
1212end
1213if eq c 67
1214ob1 67
1215b = rdhex
1216ohex hmpow b rdhex
1217end
1218if eq c 68
1219dosc
1220end
1221if eq c 69
1222ob1 69
1223ohex hmpow2f sgnl rdhex
1224end
1225if eq c 70
1226dof
1227end
1228if and ge c 71 le c 73
1229dopack c
1230end
1231if eq c 78
1232doscan
1233end
1234if eq c 81
1235dogemv
1236end
1237if eq c 77
1238dofmt
1239end
1240ob1 10
1241end
1242obflush
1243ret 0
1244end