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
CUDApp / backend / amd / gfx942 / exec / fma.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Exact binary32 fused multiply-add, RN-even, gradual underflow, canonical NaN. |
| 3 | note Finite x = mantissa * 2^(effective_exponent-150). Scale exact sum by 2^298. |
| 4 | note Products and addends then are integers; their sum needs at most 554 bits, hence nine u64 limbs. |
| 5 | note This is a format-derived bound, not an input cap. No intermediate floating-point rounding. |
| 6 | global fm ulong 9 |
| 7 | global fc ulong 9 |
| 8 | fn fmbool value bool to ulong |
| 9 | if value |
| 10 | ret 1 |
| 11 | end |
| 12 | ret 0 |
| 13 | end |
| 14 | fn fmexp x ulong to ulong |
| 15 | e = and shr x 23 255 |
| 16 | if eq e 0 |
| 17 | ret 1 |
| 18 | end |
| 19 | ret e |
| 20 | end |
| 21 | fn fmmant x ulong to ulong |
| 22 | v = and x 0x7fffff |
| 23 | if ne and x 0x7f800000 0 |
| 24 | v = or v 0x800000 |
| 25 | end |
| 26 | ret v |
| 27 | end |
| 28 | fn fmput other bool i ulong v ulong to void |
| 29 | if other |
| 30 | put fc i v |
| 31 | else |
| 32 | put fm i v |
| 33 | end |
| 34 | end |
| 35 | fn fmplace other bool v ulong shift ulong to void |
| 36 | i = div shift 64 |
| 37 | s = and shift 63 |
| 38 | fmput other i shl v s |
| 39 | if and ne s 0 lt add i 1 len fm |
| 40 | fmput other add i 1 shr v sub 64 s |
| 41 | end |
| 42 | end |
| 43 | fn fmmag to int |
| 44 | for j ulong 9 |
| 45 | i = sub 8 j |
| 46 | if gt get fm i get fc i |
| 47 | ret 1 |
| 48 | end |
| 49 | if lt get fm i get fc i |
| 50 | ret -1 |
| 51 | end |
| 52 | end |
| 53 | ret 0 |
| 54 | end |
| 55 | fn fmadd to void |
| 56 | cy = ulong 0 |
| 57 | for i ulong 9 |
| 58 | a = get fm i |
| 59 | b = get fc i |
| 60 | v = add a b |
| 61 | c = lt v a |
| 62 | w = add v cy |
| 63 | cy = fmbool or c lt w v |
| 64 | put fm i w |
| 65 | end |
| 66 | end |
| 67 | fn fmsub swap bool to void |
| 68 | borrow = ulong 0 |
| 69 | for i ulong 9 |
| 70 | a = get fm i |
| 71 | b = get fc i |
| 72 | if swap |
| 73 | t = a |
| 74 | a = b |
| 75 | b = t |
| 76 | end |
| 77 | v = sub a b |
| 78 | c = lt a b |
| 79 | w = sub v borrow |
| 80 | borrow = fmbool or c lt v borrow |
| 81 | put fm i w |
| 82 | end |
| 83 | end |
| 84 | fn fmget shift ulong to ulong |
| 85 | i = div shift 64 |
| 86 | s = and shift 63 |
| 87 | v = shr get fm i s |
| 88 | if and ne s 0 lt add i 1 len fm |
| 89 | v = or v shl get fm add i 1 sub 64 s |
| 90 | end |
| 91 | ret v |
| 92 | end |
| 93 | fn fmsticky limit ulong to bool |
| 94 | full = div limit 64 |
| 95 | for i ulong full |
| 96 | if ne get fm i 0 |
| 97 | ret true |
| 98 | end |
| 99 | end |
| 100 | bits = and limit 63 |
| 101 | if ne bits 0 |
| 102 | if ne and get fm full sub shl ulong 1 bits 1 0 |
| 103 | ret true |
| 104 | end |
| 105 | end |
| 106 | ret false |
| 107 | end |
| 108 | fn fmround sg ulong to ulong |
| 109 | n = ulong 0 |
| 110 | for j ulong 9 |
| 111 | i = sub 8 j |
| 112 | v = get fm i |
| 113 | if ne v 0 |
| 114 | n = mul i 64 |
| 115 | while gt v 1 bound 999999 |
| 116 | v = shr v 1 |
| 117 | n = add n 1 |
| 118 | end |
| 119 | break |
| 120 | end |
| 121 | end |
| 122 | shift = ulong 149 |
| 123 | if gt n 172 |
| 124 | shift = sub n 23 |
| 125 | end |
| 126 | m = and fmget shift 0xffffff |
| 127 | half = ne and fmget sub shift 1 1 0 |
| 128 | if and half or fmsticky sub shift 1 ne and m 1 0 |
| 129 | m = add m 1 |
| 130 | end |
| 131 | if ge m 0x1000000 |
| 132 | m = shr m 1 |
| 133 | n = add n 1 |
| 134 | end |
| 135 | if ge n 426 |
| 136 | ret or sg 0x7f800000 |
| 137 | end |
| 138 | e = ulong 0 |
| 139 | if ge m 0x800000 |
| 140 | e = 1 |
| 141 | if gt n 172 |
| 142 | e = sub n 171 |
| 143 | end |
| 144 | end |
| 145 | ret or sg or shl e 23 and m 0x7fffff |
| 146 | end |
| 147 | fn fmfinite a ulong b ulong c ulong to ulong |
| 148 | for i ulong 9 |
| 149 | put fm i 0 |
| 150 | put fc i 0 |
| 151 | end |
| 152 | p = mul fmmant a fmmant b |
| 153 | shift = sub add fmexp a fmexp b 2 |
| 154 | fmplace false p shift |
| 155 | fmplace true fmmant c add fmexp c 148 |
| 156 | sp = and xor a b 0x80000000 |
| 157 | sc = and c 0x80000000 |
| 158 | sg = sp |
| 159 | if eq sp sc |
| 160 | fmadd |
| 161 | else |
| 162 | cmp = fmmag |
| 163 | if eq cmp 0 |
| 164 | ret 0 |
| 165 | end |
| 166 | if lt cmp 0 |
| 167 | sg = sc |
| 168 | end |
| 169 | fmsub lt cmp 0 |
| 170 | end |
| 171 | ret fmround sg |
| 172 | end |
| 173 | fn fpfma a ulong b ulong c ulong to ulong |
| 174 | ax = and a 0x7fffffff |
| 175 | bx = and b 0x7fffffff |
| 176 | cx = and c 0x7fffffff |
| 177 | if or or gt ax 0x7f800000 gt bx 0x7f800000 gt cx 0x7f800000 |
| 178 | ret 0x7fc00000 |
| 179 | end |
| 180 | sp = and xor a b 0x80000000 |
| 181 | if or eq ax 0x7f800000 eq bx 0x7f800000 |
| 182 | if or eq ax 0 eq bx 0 |
| 183 | ret 0x7fc00000 |
| 184 | end |
| 185 | if and eq cx 0x7f800000 ne sp and c 0x80000000 |
| 186 | ret 0x7fc00000 |
| 187 | end |
| 188 | ret or sp 0x7f800000 |
| 189 | end |
| 190 | if eq cx 0x7f800000 |
| 191 | ret c |
| 192 | end |
| 193 | if and or eq ax 0 eq bx 0 eq cx 0 |
| 194 | ret and sp and c 0x80000000 |
| 195 | end |
| 196 | ret fmfinite a b c |
| 197 | end |