interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / numeric / numeric.om

816 lines · 14192 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note OMEGA-NUMERIC reference tier in Omega: the pure scalar definitions of
5note src/omega_numeric.c (E1 scalar contract, Omega div/sqrt/exp/log, compare
6note classes, launch shape). Every FP32/FP16/BF16 value travels as raw bits in
7note a ulong. The C file keeps the pointer loops, the asm realization tier and
8note the registry tables; CUDAPP_MIGRATION_PENDING_C_NUMERIC selects the old C
9note bodies as the differential oracle. One exported dispatcher, numeric_ref,
10note because a hosted unit exports only its last function.
11const M32 ulong 4294967295
12const SGN ulong 2147483648
13const ABS ulong 2147483647
14const INF ulong 2139095040
15const QNAN ulong 2143289344
16const QBIT ulong 4194304
17note CANON is the one NaN every float-producing op returns: PTX leaves f32 NaN results
18note unspecified and the GPU produces 0x7fffffff, so no host NaN propagation leaks out.
19const CANON ulong 2147483647
20const TOP ulong 9223372036854775808
21fn isnan32 u ulong to bool
22ret gt and u ABS INF
23end
24fn canon u ulong to ulong
25if isnan32 u
26ret CANON
27end
28ret u
29end
30fn issnan u ulong to bool
31ret and isnan32 u eq and u QBIT 0
32end
33fn iszero32 u ulong to bool
34ret eq and u ABS 0
35end
36fn msb64 v ulong to ulong
37note v is nonzero, so the leading one bit is found within 64 passes
38p = ulong 63
39while eq and shr v p 1 0 bound 64
40p = sub p 1
41end
42ret p
43end
44note Round sig * 2^e2 (s <= 0: exact shift up) to the lsb chosen by rndfmt.
45fn rndsh sig ulong s long p long to ulong
46if le s 0
47ret shl sig ulong neg s
48end
49if gt s add p 1
50ret 0
51end
52u = ulong s
53mant = ulong 0
54if lt u 64
55mant = shr sig u
56end
57g = and shr sig sub u 1 1
58r = and sig sub shl ulong 1 sub u 1 1
59if and eq g 1 or ne r 0 eq and mant 1 1
60mant = add mant 1
61end
62ret mant
63end
64note ref_round_small: sig * 2^e2 (exact, sig > 0) to a format with man
65note explicit bits, bias bias and all-ones exponent emaxf; RNE, subnormals
66note kept, overflow to infinity. Returns the bits without the sign. A sticky
67note amount below sig is passed as sig*2+1 at e2-1.
68fn rndfmt sig ulong e2 long man long bias long emaxf ulong to ulong
69p = long msb64 sig
70be = add add p e2 bias
71inf = shl emaxf ulong man
72if ge be long emaxf
73ret inf
74end
75s = sub sub sub 1 bias man e2
76if ge be 1
77s = sub p man
78end
79mant = rndsh sig s p
80bits = mant
81if ge be 1
82bits = add shl ulong sub be 1 ulong man mant
83end
84if ge bits inf
85ret inf
86end
87ret bits
88end
89fn rnd32 sig ulong e2 long to ulong
90ret rndfmt sig e2 23 127 255
91end
92note ref_split: finite FP32 magnitude as m * 2^e, m below 2^24.
93fn mant32 x ulong to ulong
94f = and x 8388607
95if ne and shr x 23 255 0
96ret or f 8388608
97end
98ret f
99end
100fn expo32 x ulong to long
101be = and shr x 23 255
102if ne be 0
103ret sub long be 150
104end
105ret long -149
106end
107note omega_unpack: finite nonzero magnitude as m * 2^e, m in [2^23, 2^24).
108fn unpm x ulong to ulong
109m = mant32 x
110while eq and m 8388608 0 bound 24
111m = shl m 1
112end
113ret m
114end
115fn unpe x ulong to long
116m = mant32 x
117e = expo32 x
118while eq and m 8388608 0 bound 24
119m = shl m 1
120e = sub e 1
121end
122ret e
123end
124note Order key of a non-NaN FP32: -0 and +0 share a key.
125fn okey u ulong to ulong
126if ne and u SGN 0
127ret sub SGN and u ABS
128end
129ret add SGN u
130end
131fn fmin32 a ulong b ulong to ulong
132if isnan32 a
133ret canon b
134end
135if isnan32 b
136ret a
137end
138if and iszero32 a iszero32 b
139ret and or a b SGN
140end
141if lt okey a okey b
142ret a
143end
144ret b
145end
146fn fmax32 a ulong b ulong to ulong
147if isnan32 a
148ret canon b
149end
150if isnan32 b
151ret a
152end
153if and iszero32 a iszero32 b
154ret and and a b SGN
155end
156if gt okey a okey b
157ret a
158end
159ret b
160end
161fn fsetpge a ulong b ulong to ulong
162if or isnan32 a isnan32 b
163ret 0
164end
165if ge okey a okey b
166ret 1
167end
168ret 0
169end
170note ref_order: PL 1, PE 2, PG 4, PU 8.
171fn order a ulong b ulong to ulong
172if or isnan32 a isnan32 b
173ret 8
174end
175ka = okey a
176kb = okey b
177if lt ka kb
178ret 1
179end
180if gt ka kb
181ret 4
182end
183ret 2
184end
185fn fsetpred mask ulong a ulong b ulong to ulong
186if ne and mask order a b 0
187ret 1
188end
189ret 0
190end
191fn fadd32 a ulong b ulong to ulong
192ret canon ulong bitcast uint add bitcast f32 uint a bitcast f32 uint b
193end
194fn fsub32 a ulong b ulong to ulong
195ret canon ulong bitcast uint sub bitcast f32 uint a bitcast f32 uint b
196end
197fn fmul32 a ulong b ulong to ulong
198ret canon ulong bitcast uint mul bitcast f32 uint a bitcast f32 uint b
199end
200fn i2f32 a ulong to ulong
201if eq a 0
202ret 0
203end
204if ne and a SGN 0
205ret or SGN rnd32 sub ulong 4294967296 a 0
206end
207ret rnd32 a 0
208end
209fn u2f32 a ulong to ulong
210if eq a 0
211ret 0
212end
213ret rnd32 a 0
214end
215note Integer part and fraction class of |x| (finite, nonzero): ip*4 + frac,
216note frac 0 none, 1 below half, 2 half, 3 above; TOP when |x| >= 2^40.
217fn ipfrac u ulong to ulong
218m = mant32 u
219e = expo32 u
220if ge e 0
221if gt e 16
222ret TOP
223end
224ret shl shl m ulong e 2
225end
226if ge neg e 40
227ret 1
228end
229sh = ulong neg e
230ip = shr m sh
231rm = and m sub shl ulong 1 sh 1
232h = shl ulong 1 sub sh 1
233fr = ulong 3
234if eq rm 0
235fr = 0
236else
237if lt rm h
238fr = 1
239else
240if eq rm h
241fr = 2
242end
243end
244end
245ret or shl ip 2 fr
246end
247fn satv ng bool uns bool to ulong
248if uns
249if ng
250ret 0
251end
252ret M32
253end
254if ng
255ret SGN
256end
257ret ABS
258end
259fn clampi ng bool mag ulong uns bool to ulong
260if uns
261if ng
262ret 0
263end
264if gt mag M32
265ret M32
266end
267ret mag
268end
269if ng
270if gt mag SGN
271ret SGN
272end
273ret and sub 0 mag M32
274end
275if gt mag ABS
276ret ABS
277end
278ret mag
279end
280note ref_to_int: mode 0 RTZ, 1 floor, 2 ceil, 3 RNE; saturating to int32
281note (uns false) or uint32 (uns true); NaN -> 0. Result: the 32 result bits.
282fn toint u ulong mode ulong uns bool to ulong
283if isnan32 u
284ret 0
285end
286ng = ne and u SGN 0
287x = and u ABS
288if eq x INF
289ret satv ng uns
290end
291if eq x 0
292ret 0
293end
294pf = ipfrac x
295if eq pf TOP
296ret satv ng uns
297end
298ret clampi ng intrnd pf mode ng uns
299end
300note Integer magnitude of ipfrac pf after rounding mode mode (ng: negative).
301fn intrnd pf ulong mode ulong ng bool to ulong
302ip = shr pf 2
303fr = and pf 3
304up = false
305if eq mode 1
306up = and ng ne fr 0
307end
308if eq mode 2
309up = and not ng ne fr 0
310end
311if eq mode 3
312up = or eq fr 3 and eq fr 2 eq and ip 1 1
313end
314if up
315ret add ip 1
316end
317ret ip
318end
319note ref_narrow: FP32 to a 16-bit format (man bits, bias, emaxf).
320fn narrow u ulong man ulong bias long emaxf ulong to ulong
321sign = and shr u 16 32768
322be = and shr u 23 255
323f = and u 8388607
324inf = shl emaxf man
325if eq be 255
326if eq f 0
327ret or sign inf
328end
329ret or or or sign inf shl ulong 1 sub man 1 shr f sub 23 man
330end
331if and eq be 0 eq f 0
332ret sign
333end
334ret or sign rndfmt mant32 u expo32 u long man bias emaxf
335end
336fn f16to32 h ulong to ulong
337sign = shl and h 32768 16
338be = and shr h 10 31
339f = and h 1023
340if eq be 31
341if eq f 0
342ret or sign INF
343end
344ret or or sign INF or QBIT shl f 13
345end
346if eq be 0
347if eq f 0
348ret sign
349end
350note f >= 1 reaches bit 10 within 10 shifts; e is the biased exponent
351e = ulong 113
352while eq and f 1024 0 bound 10
353f = shl f 1
354e = sub e 1
355end
356ret or or sign shl e 23 shl and f 1023 13
357end
358ret or or sign shl add be 112 23 shl f 13
359end
360fn bf16to32 h ulong to ulong
361ret shl and h 65535 16
362end
363note omega_ref_ffma_int tail: X is the term with the leading bit at or
364note above Y's. X is moved to bit 62; lost bits of Y are ORed into bit 0.
365fn fmaord X ulong ex long sx ulong Y ulong ey long sy ulong to ulong
366up = sub 62 long msb64 X
367xs = shl X ulong up
368xe = sub ex up
369d = sub xe ey
370ys = Y
371if lt d 0
372ys = shl Y ulong neg d
373else
374if ge d 64
375ys = 1
376else
377if gt d 0
378ys = shr Y ulong d
379if ne and Y sub shl ulong 1 ulong d 1 0
380ys = or ys 1
381end
382end
383end
384end
385m = add xs ys
386s = sx
387if ne sx sy
388if ge xs ys
389m = sub xs ys
390else
391m = sub ys xs
392s = sy
393end
394end
395if eq m 0
396ret 0
397end
398ret or s rnd32 m xe
399end
400fn fmaadd X ulong ex long sx ulong Y ulong ey long sy ulong to ulong
401if gt add long msb64 Y ey add long msb64 X ex
402ret fmaord Y ey sy X ex sx
403end
404ret fmaord X ex sx Y ey sy
405end
406fn fmafin xa ulong xb ulong xc ulong ps ulong cs ulong to ulong
407mp = mul mant32 xa mant32 xb
408ep = add expo32 xa expo32 xb
409if eq xc 0
410ret or ps rnd32 mp ep
411end
412ret fmaadd mp ep ps mant32 xc expo32 xc cs
413end
414fn ffmaint a ulong b ulong c ulong to ulong
415ps = and xor a b SGN
416cs = and c SGN
417xa = and a ABS
418xb = and b ABS
419xc = and c ABS
420if or or gt xa INF gt xb INF gt xc INF
421ret QNAN
422end
423pinf = or eq xa INF eq xb INF
424pz = or eq xa 0 eq xb 0
425if and pinf pz
426ret QNAN
427end
428if pinf
429if and eq xc INF ne cs ps
430ret QNAN
431end
432ret or ps INF
433end
434if eq xc INF
435ret c
436end
437if pz
438if ne xc 0
439ret c
440end
441ret and ps cs
442end
443ret fmafin xa xb xc ps cs
444end
445note AArch64 FMADD (FPCR DN=0): FPProcessNaNs3(addend c, a, b), signaling
446note NaNs first, then a quiet addend with inf*0 gives the default NaN.
447fn ffmahw a ulong b ulong c ulong to ulong
448if issnan c
449ret or c QBIT
450end
451if issnan a
452ret or a QBIT
453end
454if issnan b
455ret or b QBIT
456end
457if isnan32 c
458xa = and a ABS
459xb = and b ABS
460if or and eq xa INF eq xb 0 and eq xa 0 eq xb INF
461ret QNAN
462end
463ret c
464end
465if isnan32 a
466ret a
467end
468if isnan32 b
469ret b
470end
471ret ffmaint a b c
472end
473fn divfin ax ulong ay ulong sign ulong to ulong
474my = unpm ay
475rm = unpm ax
476q = ulong 0
477for i ulong 27
478q = shl q 1
479if ge rm my
480rm = sub rm my
481q = or q 1
482end
483rm = shl rm 1
484end
485st = ulong 0
486if ne rm 0
487st = 1
488end
489ret or sign rnd32 or shl q 1 st sub sub sub unpe ax unpe ay 26 1
490end
491fn mdiv x ulong y ulong to ulong
492if or isnan32 x isnan32 y
493ret QNAN
494end
495sign = and xor x y SGN
496ax = and x ABS
497ay = and y ABS
498if eq ay 0
499if eq ax 0
500ret QNAN
501end
502ret or sign INF
503end
504if eq ax 0
505ret sign
506end
507if eq ax INF
508if eq ay INF
509ret QNAN
510end
511ret or sign INF
512end
513if eq ay INF
514ret sign
515end
516ret divfin ax ay sign
517end
518fn sqfin mm ulong e long to ulong
519op = shl mm 28
520s = ulong 0
521one = shl ulong 1 62
522while gt one op bound 32
523one = shr one 2
524end
525while ne one 0 bound 32
526t = add s one
527if ge op t
528op = sub op t
529s = add shr s 1 one
530else
531s = shr s 1
532end
533one = shr one 2
534end
535st = ulong 0
536if ne op 0
537st = 1
538end
539ret rnd32 or shl s 1 st sub sub div e 2 14 1
540end
541fn msqrt x ulong to ulong
542if isnan32 x
543ret QNAN
544end
545if iszero32 x
546ret x
547end
548if ne and x SGN 0
549ret QNAN
550end
551if eq x INF
552ret INF
553end
554mm = unpm x
555e = unpe x
556if ne rem e 2 0
557mm = shl mm 1
558e = sub e 1
559end
560ret sqfin mm e
561end
562note AArch64 FSQRT (DN=0): a NaN operand is returned quiet.
563fn ieeesqrt x ulong to ulong
564if isnan32 x
565ret or x QBIT
566end
567ret msqrt x
568end
569fn ieeediv x ulong y ulong to ulong
570ret ulong bitcast uint div bitcast f32 uint x bitcast f32 uint y
571end
572fn f32of u ulong to f32
573ret bitcast f32 uint u
574end
575fn bitsof v f32 to ulong
576ret ulong bitcast uint v
577end
578fn pow2 k long to f32
579ret bitcast f32 uint shl add k 127 23
580end
581fn expscal poly f32 k long to ulong
582if gt k 127
583ret bitsof mul mul poly f32of 2130706432 pow2 sub k 127
584end
585if lt k long -126
586ret bitsof mul mul poly pow2 add k 100 pow2 long -100
587end
588ret bitsof mul poly pow2 k
589end
590fn exppoly x f32 k long to ulong
591fk = f32 k
592r = sub sub x mul fk f32of 1060205056 mul fk f32of 901758606
593p = mul r add f32of 1026206379 mul r f32of 1007192201
594p = mul r add f32of 1042983595 p
595p = mul r add f32of 1056964608 p
596poly = add f32of 1065353216 mul r add f32of 1065353216 p
597ret expscal poly k
598end
599fn mexp u ulong to ulong
600if isnan32 u
601ret QNAN
602end
603x = f32of u
604if lt x f32of 3268411392
605ret 0
606end
607if gt x f32of 1118925336
608ret INF
609end
610prod = mul x f32of 1069066811
611k = int 0
612if ge prod f32of 0
613k = int add prod f32of 1056964608
614else
615k = int sub prod f32of 1056964608
616end
617ret exppoly x long k
618end
619fn logpoly m f32 e long to ulong
620z = f32of mdiv bitsof sub m f32of 1065353216 bitsof add m f32of 1065353216
621z2 = mul z z
622poly = add f32of 1065353216 mul z2 add f32of 1051372203 mul z2 add f32of 1045220557 mul z2 f32of 1041385765
623lnm = mul mul f32of 1073741824 z poly
624ret bitsof add mul f32 e f32of 1060205080 lnm
625end
626fn mlog u ulong to ulong
627if isnan32 u
628ret QNAN
629end
630if lt f32of u f32of 0
631ret QNAN
632end
633if iszero32 u
634ret 4286578688
635end
636if eq u INF
637ret INF
638end
639x = u
640ea = long 0
641if eq and shr u 23 255 0
642x = bitsof mul f32of u f32of 1258291200
643ea = long -23
644end
645e = add sub long and shr x 23 255 127 ea
646m = f32of or and x 8388607 1065353216
647if gt m f32of 1068827891
648m = mul m f32of 1056964608
649e = add e 1
650end
651ret logpoly m e
652end
653fn bitseq a ulong b ulong to ulong
654if and isnan32 a isnan32 b
655ret 1
656end
657if eq a b
658ret 1
659end
660ret 0
661end
662note omega_numeric_compare_equal: mode 0 BIT_EXACT, 1 INT_EXACT,
663note 2 SEED_BOUND, 3 F16_BITS, 4 BF16_BITS.
664fn b2u v bool to ulong
665if v
666ret 1
667end
668ret 0
669end
670fn cmpeq mode ulong ex ulong got ulong to ulong
671if eq mode 0
672ret bitseq ex got
673end
674if eq mode 1
675ret b2u eq ex got
676end
677if or eq mode 3 eq mode 4
678if or ne shr ex 16 0 ne shr got 16 0
679ret 0
680end
681inf = ulong 31744
682if eq mode 4
683inf = 32640
684end
685en = gt and ex 32767 inf
686gn = gt and got 32767 inf
687if or en gn
688ret b2u and en gn
689end
690ret b2u eq ex got
691end
692ret 0
693end
694fn levels n ulong to ulong
695if eq n 0
696ret 0
697end
698l = ulong 1
699cap = ulong 32
700while lt cap n bound 13
701if gt cap 576460752303423487
702cap = sub ulong 0 1
703else
704cap = mul cap 32
705end
706l = add l 1
707end
708ret l
709end
710fn shape cnt ulong to ulong
711g = and div add cnt 63 64 M32
712if eq g 0
713ret 1
714end
715ret g
716end
717note Float constants are written as the bits of the C `f` literals, because
718note the pinned decimal conversion is not always correctly rounded (omega-mv#1024).
719note Dispatcher. Codes are the OMEGA_NUMERIC_OM_* values of src/omega_numeric.c.
720fn numeric_ref op ulong a ulong b ulong c ulong to ulong
721if eq op 0
722ret fadd32 a b
723end
724if eq op 1
725ret fsub32 a b
726end
727if eq op 2
728ret fmul32 a b
729end
730if eq op 3
731ret canon ffmahw a b c
732end
733if eq op 4
734ret canon ffmaint a b c
735end
736if eq op 5
737ret fmin32 a b
738end
739if eq op 6
740ret fmax32 a b
741end
742if eq op 7
743ret fsetpge a b
744end
745if eq op 8
746ret fsetpred c a b
747end
748if eq op 9
749ret i2f32 a
750end
751if eq op 10
752ret u2f32 a
753end
754if eq op 11
755ret toint a 0 false
756end
757if eq op 12
758ret toint a 1 false
759end
760if eq op 13
761ret toint a 2 false
762end
763if eq op 14
764ret toint a 3 false
765end
766if eq op 15
767ret toint a 0 true
768end
769if eq op 16
770ret narrow a 10 15 31
771end
772if eq op 17
773ret narrow a 7 127 255
774end
775if eq op 18
776ret f16to32 a
777end
778if eq op 19
779ret bf16to32 a
780end
781if eq op 20
782ret canon mdiv a b
783end
784if eq op 21
785ret canon msqrt a
786end
787if eq op 22
788ret canon mexp a
789end
790if eq op 23
791ret canon mlog a
792end
793if eq op 24
794ret canon ieeediv a b
795end
796if eq op 25
797ret canon ieeesqrt a
798end
799if eq op 26
800ret and sub 2129859008 a M32
801end
802if eq op 27
803ret bitseq a b
804end
805if eq op 28
806ret cmpeq a b c
807end
808if eq op 29
809ret levels a
810end
811if eq op 30
812ret shape a
813end
814note unknown codes never come from the C wrappers
815ret 0
816end