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 / apiviews / e4m3.om

81 lines · 1688 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note f32 to e4m3, round to nearest, ties to even, saturate at 448 (code 0x7e).
3note Subnormals are units of 2^-9. 448.0f32 is 1138753536, 512.0f32 is 1140850688,
4note 0.5f32 is 1056964608. WolframLanguageEvaluator: FromDigits of those Real32
5note bit lists -> 1138753536, 1140850688, 1056964608.
6note Truncation of a*512 (always in [0, 8)) is a bit extract, not a cast.
7note One value. The row quantizer keeps its own copy so this unit stays one export.
8fn av_u2f n uint to f32
9if eq n 0
10ret 0.0f32
11end
12p = uint 0
13if ge n 2
14p = uint 1
15end
16if ge n 4
17p = uint 2
18end
19if ge n 8
20p = uint 3
21end
22mant = and shl n sub 23 p 8388607
23ret bitcast f32 or shl add 127 p 23 mant
24end
25fn av_trunc8 x f32 to uint
26u = bitcast uint x
27e = shr u 23
28if lt e 127
29ret 0
30end
31m = or and u 8388607 8388608
32ret shr m sub 150 e
33end
34fn av_qsub a f32 to uint
35x = mul f32 a bitcast f32 uint 1140850688
36xi = av_trunc8 x
37fr = sub x av_u2f xi
38hf = bitcast f32 uint 1056964608
39if gt fr hf
40ret add xi 1
41end
42if and eq fr hf ne and xi 1 0
43ret add xi 1
44end
45ret xi
46end
47fn av_qnorm ab uint to uint
48m = and ab 8388607
49rb = and shr m 19 1
50st = and m 524287
51q = shr m 20
52e = sub add shr ab 23 7 127
53if and ne rb 0 or ne st 0 ne and q 1 0
54q = add q 1
55end
56if eq q 8
57q = uint 0
58e = add e 1
59end
60code = or shl e 3 q
61if gt code 126
62ret uint 126
63end
64ret code
65end
66fn qe4m3 v f32 to uint
67u = bitcast uint v
68s = and shr u 24 128
69ab = and u 2147483647
70a = bitcast f32 ab
71if not lt a bitcast f32 uint 1138753536
72ret or s 126
73end
74if lt shr ab 23 121
75ret or s av_qsub a
76end
77ret or s av_qnorm ab
78end
79fn av_e4m3 bits ulong to ulong
80ret ulong qe4m3 bitcast f32 uint and bits 4294967295
81end