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 / amd / gfx942 / exec / fp.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note gfx942 host executor. ADD is integer-only; bounded EXP/LOG/RCP/RSQ representatives use native binary64. |
| 4 | note Binary32 addition: magnitude-order operands, align with guard/round/sticky, normalize, |
| 5 | note round nearest ties even, gradual underflow. Exact cancellation is +0; -0 + -0 is -0. |
| 6 | note NaNs and invalid infinity addition produce canonical quiet NaN 0x7fc00000. |
| 7 | note NaN payload/sign propagation is not specified by IEEE arithmetic; this is the model policy. |
| 8 | note tests/amd-exec/oracle.py checks bit patterns against independently rounded Python float/struct. |
| 9 | fn jam x ulong n ulong to ulong |
| 10 | if eq n 0 |
| 11 | ret x |
| 12 | end |
| 13 | if ge n 64 |
| 14 | if ne x 0 |
| 15 | ret 1 |
| 16 | end |
| 17 | ret 0 |
| 18 | end |
| 19 | r = shr x n |
| 20 | if ne and x sub shl ulong 1 n 1 0 |
| 21 | r = or r 1 |
| 22 | end |
| 23 | ret r |
| 24 | end |
| 25 | fn fpround am ulong ae ulong sg ulong to ulong |
| 26 | lo = and am 7 |
| 27 | am = shr am 3 |
| 28 | if or gt lo 4 and eq lo 4 ne and am 1 0 |
| 29 | am = add am 1 |
| 30 | end |
| 31 | if ge am 0x1000000 |
| 32 | am = shr am 1 |
| 33 | ae = add ae 1 |
| 34 | end |
| 35 | if ge ae 255 |
| 36 | ret or sg 0x7f800000 |
| 37 | end |
| 38 | if lt am 0x800000 |
| 39 | ae = 0 |
| 40 | end |
| 41 | ret or or sg shl ae 23 and am 0x7fffff |
| 42 | end |
| 43 | fn fpadd a ulong b ulong to ulong |
| 44 | ax = and a 0x7fffffff |
| 45 | bx = and b 0x7fffffff |
| 46 | if or gt ax 0x7f800000 gt bx 0x7f800000 |
| 47 | ret 0x7fc00000 |
| 48 | end |
| 49 | if eq ax 0x7f800000 |
| 50 | if and eq bx ax ne shr a 31 shr b 31 |
| 51 | ret 0x7fc00000 |
| 52 | end |
| 53 | ret a |
| 54 | end |
| 55 | if eq bx 0x7f800000 |
| 56 | ret b |
| 57 | end |
| 58 | if lt ax bx |
| 59 | t = a |
| 60 | a = b |
| 61 | b = t |
| 62 | t = ax |
| 63 | ax = bx |
| 64 | bx = t |
| 65 | end |
| 66 | if eq ax 0 |
| 67 | ret and and a b 0x80000000 |
| 68 | end |
| 69 | sg = and a 0x80000000 |
| 70 | ae = shr ax 23 |
| 71 | be = shr bx 23 |
| 72 | am = and ax 0x7fffff |
| 73 | bm = and bx 0x7fffff |
| 74 | if ne ae 0 |
| 75 | am = or am 0x800000 |
| 76 | else |
| 77 | ae = 1 |
| 78 | end |
| 79 | if ne be 0 |
| 80 | bm = or bm 0x800000 |
| 81 | else |
| 82 | be = 1 |
| 83 | end |
| 84 | am = shl am 3 |
| 85 | bm = jam shl bm 3 sub ae be |
| 86 | if eq and xor a b 0x80000000 0 |
| 87 | am = add am bm |
| 88 | if ge am 0x8000000 |
| 89 | am = jam am 1 |
| 90 | ae = add ae 1 |
| 91 | end |
| 92 | else |
| 93 | am = sub am bm |
| 94 | if eq am 0 |
| 95 | ret 0 |
| 96 | end |
| 97 | while and lt am 0x4000000 gt ae 1 bound 999999 |
| 98 | am = shl am 1 |
| 99 | ae = sub ae 1 |
| 100 | end |
| 101 | end |
| 102 | ret fpround am ae sg |
| 103 | end |
| 104 | note CDNA3 PDF p172, IEEE mode: signaling NaNs quiet before numeric max; prefer +0. |
| 105 | fn fpmax a ulong b ulong to ulong |
| 106 | ax = and a 0x7fffffff |
| 107 | bx = and b 0x7fffffff |
| 108 | if and gt ax 0x7f800000 eq and ax 0x400000 0 |
| 109 | ret or a 0x400000 |
| 110 | end |
| 111 | if and gt bx 0x7f800000 eq and bx 0x400000 0 |
| 112 | ret or b 0x400000 |
| 113 | end |
| 114 | if gt ax 0x7f800000 |
| 115 | ret b |
| 116 | end |
| 117 | if gt bx 0x7f800000 |
| 118 | ret a |
| 119 | end |
| 120 | if and eq ax 0 eq bx 0 |
| 121 | ret and a b |
| 122 | end |
| 123 | if ge bitcast f32 uint a bitcast f32 uint b |
| 124 | ret a |
| 125 | end |
| 126 | ret b |
| 127 | end |
| 128 | note Transcendental representative, not a claim of device bit identity. PDF pp195-196: 1ULP, FTZ. |
| 129 | note Native binary64 arithmetic only here. Decimal oracle bounds each result independently. |
| 130 | fn fpftz a ulong to ulong |
| 131 | if lt and a 0x7fffffff 0x800000 |
| 132 | ret and a 0x80000000 |
| 133 | end |
| 134 | ret a |
| 135 | end |
| 136 | fn fpexp a ulong to ulong |
| 137 | if gt and a 0x7fffffff 0x7f800000 |
| 138 | ret 0x7fc00000 |
| 139 | end |
| 140 | x = f64 bitcast f32 uint fpftz a |
| 141 | if ge x 128.0f64 |
| 142 | ret 0x7f800000 |
| 143 | end |
| 144 | if lt x -126.0f64 |
| 145 | ret 0 |
| 146 | end |
| 147 | n = int x |
| 148 | if lt x f64 n |
| 149 | n = sub n 1 |
| 150 | end |
| 151 | r = mul sub x f64 n 0.6931471805599453f64 |
| 152 | t = 1.0f64 |
| 153 | y = 1.0f64 |
| 154 | for j ulong 18 |
| 155 | t = div mul t r f64 add j 1 |
| 156 | y = add y t |
| 157 | end |
| 158 | scale = bitcast f64 shl ulong add n 1023 52 |
| 159 | ret fpftz ulong bitcast uint f32 mul y scale |
| 160 | end |
| 161 | note CDNA3 V_LOG_F32 opcode33: base2 log, 1ULP and input/output FTZ. |
| 162 | note Reduce to mantissa [1,2); log(m)=2*(z+z^3/3+...), z=(m-1)/(m+1). |
| 163 | note Thirty terms at |z|<=1/3 put truncation far below binary32 rounding. |
| 164 | fn fplog a ulong to ulong |
| 165 | a = fpftz a |
| 166 | ax = and a 0x7fffffff |
| 167 | if gt ax 0x7f800000 |
| 168 | ret or and a 0x80000000 0x7fc00000 |
| 169 | end |
| 170 | if eq ax 0 |
| 171 | ret 0xff800000 |
| 172 | end |
| 173 | if ne and a 0x80000000 0 |
| 174 | ret 0xffc00000 |
| 175 | end |
| 176 | if eq ax 0x7f800000 |
| 177 | ret a |
| 178 | end |
| 179 | n = sub int shr ax 23 127 |
| 180 | m = f64 bitcast f32 uint or and ax 0x7fffff 0x3f800000 |
| 181 | z = div sub m 1.0f64 add m 1.0f64 |
| 182 | zz = mul z z |
| 183 | t = z |
| 184 | y = z |
| 185 | for j ulong 29 |
| 186 | t = mul t zz |
| 187 | y = add y div t f64 add mul add j 1 2 1 |
| 188 | end |
| 189 | ret fpftz ulong bitcast uint f32 add f64 n div mul y 2.0f64 0.6931471805599453f64 |
| 190 | end |
| 191 | fn fprcp a ulong to ulong |
| 192 | a = fpftz a |
| 193 | if gt and a 0x7fffffff 0x7f800000 |
| 194 | ret 0x7fc00000 |
| 195 | end |
| 196 | if eq and a 0x7fffffff 0 |
| 197 | ret or and a 0x80000000 0x7f800000 |
| 198 | end |
| 199 | x = f64 bitcast f32 uint a |
| 200 | ret fpftz ulong bitcast uint f32 div 1.0f64 x |
| 201 | end |
| 202 | note CDNA3 PDF p197: reciprocal square root, 1ULP and FTZ; preserve the signed-zero examples. |
| 203 | fn fprsq a ulong to ulong |
| 204 | a = fpftz a |
| 205 | if gt and a 0x7fffffff 0x7f800000 |
| 206 | ret or and a 0x80000000 0x7fc00000 |
| 207 | end |
| 208 | if eq and a 0x7fffffff 0 |
| 209 | ret or and a 0x80000000 0x7f800000 |
| 210 | end |
| 211 | if ne and a 0x80000000 0 |
| 212 | ret 0xffc00000 |
| 213 | end |
| 214 | x = f64 bitcast f32 uint a |
| 215 | ret fpftz ulong bitcast uint f32 div 1.0f64 fsqrt x |
| 216 | end |