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
omega-mv / gpu / nvidia / om / gemmex / num.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note The number formats of the general GEMM (src/omega_gpu_gemm_ex.h, CUDApp#144), shared by every om/gemmex unit. |
| 3 | note Values travel as raw IEEE bits (f32 as uint, f64 as ulong); the f32/f64 arithmetic is the seeded float set: |
| 4 | note one rounding per operator, round to nearest even, no fused operations. |
| 5 | note Bit constants (Wolfram FromDigits): 0x7f800000 2139095040, 0x7fc00000 2143289344, 0x7fffff 8388607, |
| 6 | note 0x7fffffff 2147483647, 0x7fff 32767, 0x8000 32768, 0x7c00 31744, 0x200 512, 0x477ff000 1199566848, |
| 7 | note 0x38800000 947912704, 0x33000000 855638016, 0x800000 8388608, 0xfff 4095, 0x3ff 1023, 0xffff0000 4294901760, |
| 8 | note 0x7f80 32640, 0x33800000 864026624 (f32 2^-24), 0x3b000000 989855744 (f32 2^-9). |
| 9 | |
| 10 | note finite f32 bits: the exponent field is not all ones |
| 11 | fn gx_fin u uint to bool |
| 12 | ret ne and u 2139095040 2139095040 |
| 13 | end |
| 14 | |
| 15 | note binary16 bits to f32, exact |
| 16 | fn gx_h2f h uint to f32 |
| 17 | s = shl shr h 15 31 |
| 18 | e = and shr h 10 31 |
| 19 | m = and h 1023 |
| 20 | if eq e 31 |
| 21 | ret bitcast f32 or or s 2139095040 shl m 13 |
| 22 | end |
| 23 | if eq e 0 |
| 24 | v = mul f32 m bitcast f32 uint 864026624 |
| 25 | if ne s 0 |
| 26 | ret neg v |
| 27 | end |
| 28 | ret v |
| 29 | end |
| 30 | ret bitcast f32 or or s shl add e 112 23 shl m 13 |
| 31 | end |
| 32 | |
| 33 | note e4m3 byte to f32, exact; 0x7f and 0xff are NaN (0x7fc00000), there is no infinity |
| 34 | fn gx_e4m3 b uint to f32 |
| 35 | e = and shr b 3 15 |
| 36 | m = and b 7 |
| 37 | if and eq e 15 eq m 7 |
| 38 | ret bitcast f32 uint 2143289344 |
| 39 | end |
| 40 | v = bitcast f32 or shl add e 120 23 shl m 20 |
| 41 | if eq e 0 |
| 42 | v = mul f32 m bitcast f32 uint 989855744 |
| 43 | end |
| 44 | if ne and b 128 0 |
| 45 | ret neg v |
| 46 | end |
| 47 | ret v |
| 48 | end |
| 49 | |
| 50 | note a stored element (raw bits u of type t: 0 f32, 1 bf16, 2 f16, 3 e4m3, 4 e5m2) to f32; an e5m2 byte is the top |
| 51 | note byte of an f16 |
| 52 | fn gx_ldf t ulong u uint to f32 |
| 53 | if eq t 0 |
| 54 | ret bitcast f32 u |
| 55 | end |
| 56 | if eq t 1 |
| 57 | ret bitcast f32 shl u 16 |
| 58 | end |
| 59 | if eq t 2 |
| 60 | ret gx_h2f u |
| 61 | end |
| 62 | if eq t 3 |
| 63 | ret gx_e4m3 u |
| 64 | end |
| 65 | ret gx_h2f shl u 8 |
| 66 | end |
| 67 | |
| 68 | note a stored element is non-finite (inf, NaN; e4m3: NaN only) when its exponent field has every bit set |
| 69 | fn gx_bad t ulong u uint to bool |
| 70 | if eq t 0 |
| 71 | ret eq and u 2139095040 2139095040 |
| 72 | end |
| 73 | if eq t 1 |
| 74 | ret eq and u 32640 32640 |
| 75 | end |
| 76 | if eq t 2 |
| 77 | ret eq and u 31744 31744 |
| 78 | end |
| 79 | if eq t 3 |
| 80 | ret eq and u 127 127 |
| 81 | end |
| 82 | ret eq and u 124 124 |
| 83 | end |
| 84 | |
| 85 | note f32 bits to bf16 bits, round to nearest even; a NaN stays a NaN (quiet bit set when the payload is cut) |
| 86 | fn gx_f2bf u uint to uint |
| 87 | if not gx_fin u |
| 88 | if ne and u 8388607 0 |
| 89 | ret or shr u 16 64 |
| 90 | end |
| 91 | ret shr u 16 |
| 92 | end |
| 93 | ret shr add add u 32767 and shr u 16 1 16 |
| 94 | end |
| 95 | |
| 96 | note f32 bits to f16 bits, round to nearest even (subnormals rounded, overflow to infinity, NaN quieted) |
| 97 | fn gx_f2h u uint to uint |
| 98 | s = and shr u 16 32768 |
| 99 | a = and u 2147483647 |
| 100 | if ge a 2139095040 |
| 101 | if gt a 2139095040 |
| 102 | ret or or s 31744 512 |
| 103 | end |
| 104 | ret or s 31744 |
| 105 | end |
| 106 | if ge a 1199566848 |
| 107 | ret or s 31744 |
| 108 | end |
| 109 | if lt a 947912704 |
| 110 | if lt a 855638016 |
| 111 | ret s |
| 112 | end |
| 113 | mt = or and a 8388607 8388608 |
| 114 | sh = sub 126 shr a 23 |
| 115 | q = shr mt sh |
| 116 | rm = and mt sub shl uint 1 sh 1 |
| 117 | hf = shl uint 1 sub sh 1 |
| 118 | if gt rm hf |
| 119 | q = add q 1 |
| 120 | end |
| 121 | if and eq rm hf ne and q 1 0 |
| 122 | q = add q 1 |
| 123 | end |
| 124 | ret or s q |
| 125 | end |
| 126 | r = add add a 4095 and shr a 13 1 |
| 127 | ret or s or shl sub shr r 23 112 10 and shr r 13 1023 |
| 128 | end |
| 129 | |
| 130 | note f64 to f32 rounding to odd: one more rounding to bf16 or f16 is then the correct rounding of v. A NaN or |
| 131 | note overflowing v gives a non-finite f, returned as is; g, f widened back, serves the exact test and the direction. |
| 132 | fn gx_d2fo v f64 to f32 |
| 133 | f = f32 v |
| 134 | u = bitcast uint f |
| 135 | if not gx_fin u |
| 136 | ret f |
| 137 | end |
| 138 | g = f64 f |
| 139 | if eq g v |
| 140 | ret f |
| 141 | end |
| 142 | if ne and u 1 0 |
| 143 | ret f |
| 144 | end |
| 145 | if gt fabs v fabs g |
| 146 | ret bitcast f32 add u 1 |
| 147 | end |
| 148 | ret bitcast f32 sub u 1 |
| 149 | end |
| 150 | |
| 151 | note exact split of a finite f32 into three bf16 planes by truncation (hi | mid << 16 | lo << 32); non-finite: 0 |
| 152 | fn gx_split u uint to ulong |
| 153 | if not gx_fin u |
| 154 | ret 0 |
| 155 | end |
| 156 | r1 = sub bitcast f32 u bitcast f32 and u 4294901760 |
| 157 | u1 = bitcast uint r1 |
| 158 | u2 = bitcast uint sub r1 bitcast f32 and u1 4294901760 |
| 159 | p = or ulong shr u 16 shl ulong shr u1 16 16 |
| 160 | ret or p shl ulong shr u2 16 32 |
| 161 | end |
| 162 | |
| 163 | note the byte size of a stored element of type t |
| 164 | fn gx_es t ulong to ulong |
| 165 | if eq t 0 |
| 166 | ret 4 |
| 167 | end |
| 168 | if le t 2 |
| 169 | ret 2 |
| 170 | end |
| 171 | ret 1 |
| 172 | end |
| 173 | |
| 174 | note element i (little-endian, es bytes) of a byte window |
| 175 | fn gx_get s view uchar 65536 i ulong es ulong to uint |
| 176 | p = mul i es |
| 177 | v = uint get s p |
| 178 | if gt es 1 |
| 179 | v = or v shl uint get s add p 1 8 |
| 180 | end |
| 181 | if gt es 2 |
| 182 | v = or v shl uint get s add p 2 16 |
| 183 | v = or v shl uint get s add p 3 24 |
| 184 | end |
| 185 | ret v |
| 186 | end |
| 187 | |
| 188 | note element i (es bytes, little-endian) of the words of w |
| 189 | fn gx_wel w view ulong 8192 i ulong es ulong to uint |
| 190 | b = mul i es |
| 191 | v = shr get w shr b 3 mul and b 7 8 |
| 192 | ret uint and v sub shl ulong 1 mul es 8 1 |
| 193 | end |
| 194 | |
| 195 | note store element i (little-endian, es bytes) into a byte window |
| 196 | fn gx_put d edit uchar 65536 i ulong es ulong v uint to void |
| 197 | p = mul i es |
| 198 | put d p uchar and v 255 |
| 199 | if gt es 1 |
| 200 | put d add p 1 uchar and shr v 8 255 |
| 201 | end |
| 202 | if gt es 2 |
| 203 | put d add p 2 uchar and shr v 16 255 |
| 204 | put d add p 3 uchar shr v 24 |
| 205 | end |
| 206 | end |