Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/gpu/nvidia/om/apiviews/e4m3.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / gpu / nvidia / om / apiviews / e4m3.om` 81 lines ยท 1688 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 note f32 to e4m3, round to nearest, ties to even, saturate at 448 (code 0x7e). 3 note Subnormals are units of 2^-9. 448.0f32 is `1138753536`, 512.0f32 is `1140850688`, 4 note 0.5f32 is `1056964608`. WolframLanguageEvaluator: FromDigits of those Real32 5 note bit lists -> `1138753536`, `1140850688`, `1056964608`. 6 note Truncation of a*512 (always in [0, 8)) is a bit extract, not a cast. 7 note One value. The row quantizer keeps its own copy so this unit stays one export. 8 fn av_u2f n uint to f32 9 if eq n 0 10 ret 0.0f32 11 end 12 p = uint 0 13 if ge n 2 14 p = uint 1 15 end 16 if ge n 4 17 p = uint 2 18 end 19 if ge n 8 20 p = uint 3 21 end 22 mant = and shl n sub 23 p `8388607` 23 ret bitcast f32 or shl add 127 p 23 mant 24 end 25 fn av_trunc8 x f32 to uint 26 u = bitcast uint x 27 e = shr u 23 28 if lt e 127 29 ret 0 30 end 31 m = or and u `8388607` `8388608` 32 ret shr m sub 150 e 33 end 34 fn av_qsub a f32 to uint 35 x = mul f32 a bitcast f32 uint `1140850688` 36 xi = av_trunc8 x 37 fr = sub x av_u2f xi 38 hf = bitcast f32 uint `1056964608` 39 if gt fr hf 40 ret add xi 1 41 end 42 if and eq fr hf ne and xi 1 0 43 ret add xi 1 44 end 45 ret xi 46 end 47 fn av_qnorm ab uint to uint 48 m = and ab `8388607` 49 rb = and shr m 19 1 50 st = and m 524287 51 q = shr m 20 52 e = sub add shr ab 23 7 127 53 if and ne rb 0 or ne st 0 ne and q 1 0 54 q = add q 1 55 end 56 if eq q 8 57 q = uint 0 58 e = add e 1 59 end 60 code = or shl e 3 q 61 if gt code 126 62 ret uint 126 63 end 64 ret code 65 end 66 fn qe4m3 v f32 to uint 67 u = bitcast uint v 68 s = and shr u 24 128 69 ab = and u `2147483647` 70 a = bitcast f32 ab 71 if not lt a bitcast f32 uint `1138753536` 72 ret or s 126 73 end 74 if lt shr ab 23 121 75 ret or s av_qsub a 76 end 77 ret or s av_qnorm ab 78 end 79 fn av_e4m3 bits ulong to ulong 80 ret ulong qe4m3 bitcast f32 uint and bits `4294967295` 81 end