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
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note fmt: exact printf("%.Pf") and printf("%.Pg") of an f64, as glibc prints them (the exact binary value |
| 3 | note rounded once, ties to even; inf, -inf, nan, -nan; the sign of -0). Shared by the om/tools units that |
| 4 | note format floats; a unit spec concatenates this file before the unit (om/units.sh name=fmt.om,unit.om). |
| 5 | note fm_dig writes the exact decimal digits of |x|: integer digits d 0..ip-1 then fraction digits |
| 6 | note d ip..ip+fl-1, and returns ip * 65536 + fl. Limbs are base 1e9, little-endian, 130 of them; the |
| 7 | note longest value is 2^53 * 5^1074 (767 digits) and the longest digit string 16 + 1074. |
| 8 | const FMDN ulong 1200 |
| 9 | const FMON ulong 1600 |
| 10 | const FMLN ulong 130 |
| 11 | const FMB ulong 1000000000 |
| 12 | fn fm_mul l edit ulong 130 n ulong f ulong to ulong |
| 13 | c = ulong 0 |
| 14 | for i ulong n |
| 15 | v = add mul get l i f c |
| 16 | put l i rem v FMB |
| 17 | c = div v FMB |
| 18 | end |
| 19 | k = n |
| 20 | while gt c 0 bound 130 |
| 21 | put l k rem c FMB |
| 22 | c = div c FMB |
| 23 | k = add k 1 |
| 24 | end |
| 25 | ret k |
| 26 | end |
| 27 | fn fm_ndig v ulong to ulong |
| 28 | n = ulong 1 |
| 29 | x = div v 10 |
| 30 | while gt x 0 bound 20 |
| 31 | n = add n 1 |
| 32 | x = div x 10 |
| 33 | end |
| 34 | ret n |
| 35 | end |
| 36 | note fm_tod writes the limbs l 0..n-1 as decimal digits (the top limb without leading zeros) at d p. |
| 37 | note fm_wd writes the w low decimal digits of x at d p..p+w-1, most significant first. |
| 38 | fn fm_wd d edit uchar 1200 p ulong w ulong x ulong to ulong |
| 39 | k = add p w |
| 40 | v = x |
| 41 | for i ulong w |
| 42 | k = sub k 1 |
| 43 | put d k uchar add 48 rem v 10 |
| 44 | v = div v 10 |
| 45 | end |
| 46 | ret add p w |
| 47 | end |
| 48 | fn fm_tod l edit ulong 130 n ulong d edit uchar 1200 p ulong to ulong |
| 49 | x = get l sub n 1 |
| 50 | q = fm_wd d p fm_ndig x x |
| 51 | j = sub n 1 |
| 52 | while gt j 0 bound 130 |
| 53 | j = sub j 1 |
| 54 | q = fm_wd d q 9 get l j |
| 55 | end |
| 56 | ret q |
| 57 | end |
| 58 | note fm_big: |x| = m 2^k as the integer m 2^k (k >= 0) or m 5^s with s fraction digits (k = -s); |
| 59 | note the limbs go to l 0..n-1, s to l 129; returns n with no zero top limb above limb 0. |
| 60 | fn fm_big l edit ulong 130 b ulong to ulong |
| 61 | e = and shr b 52 2047 |
| 62 | m = and b 4503599627370495 |
| 63 | s = ulong 1074 |
| 64 | if ne e 0 |
| 65 | m = or m 4503599627370496 |
| 66 | s = sub ulong 1075 e |
| 67 | end |
| 68 | put l 0 rem m FMB |
| 69 | put l 1 rem div m FMB FMB |
| 70 | n = ulong 2 |
| 71 | if ge e 1075 |
| 72 | k = sub e 1075 |
| 73 | s = ulong 0 |
| 74 | while ge k 29 bound 40 |
| 75 | n = fm_mul l n 536870912 |
| 76 | k = sub k 29 |
| 77 | end |
| 78 | n = fm_mul l n shl ulong 1 k |
| 79 | end |
| 80 | put l 129 s |
| 81 | k = s |
| 82 | while ge k 13 bound 90 |
| 83 | n = fm_mul l n 1220703125 |
| 84 | k = sub k 13 |
| 85 | end |
| 86 | f = ulong 1 |
| 87 | for i ulong k |
| 88 | f = mul f 5 |
| 89 | end |
| 90 | n = fm_mul l n f |
| 91 | while gt n 1 bound 130 |
| 92 | if ne get l sub n 1 0 |
| 93 | break |
| 94 | end |
| 95 | n = sub n 1 |
| 96 | end |
| 97 | ret n |
| 98 | end |
| 99 | fn fm_dig x f64 d edit uchar 1200 to ulong |
| 100 | l = new ulong 130 0 |
| 101 | n = fm_big l bitcast ulong x |
| 102 | s = get l 129 |
| 103 | t = add fm_ndig get l sub n 1 mul 9 sub n 1 |
| 104 | if eq s 0 |
| 105 | q = fm_tod l n d 0 |
| 106 | ret shl q 16 |
| 107 | end |
| 108 | if gt t s |
| 109 | q = fm_tod l n d 0 |
| 110 | ret or shl sub q s 16 s |
| 111 | end |
| 112 | put d 0 uchar 48 |
| 113 | z = sub s t |
| 114 | for i ulong z |
| 115 | put d add i 1 uchar 48 |
| 116 | end |
| 117 | q = fm_tod l n d add z 1 |
| 118 | ret or shl sub q s 16 s |
| 119 | end |
| 120 | note fm_at: digit i of a string of n digits, '0' past its end. |
| 121 | fn fm_at d edit uchar 1200 n ulong i ulong to ulong |
| 122 | if lt i n |
| 123 | ret ulong get d i |
| 124 | end |
| 125 | ret ulong 48 |
| 126 | end |
| 127 | note fm_rup: 1 when keeping digits 0..q-1 of the n digits rounds up (ties to even). |
| 128 | fn fm_rup d edit uchar 1200 n ulong q ulong to ulong |
| 129 | if ge q n |
| 130 | ret ulong 0 |
| 131 | end |
| 132 | c = ulong get d q |
| 133 | if gt c 53 |
| 134 | ret ulong 1 |
| 135 | end |
| 136 | if lt c 53 |
| 137 | ret ulong 0 |
| 138 | end |
| 139 | j = add q 1 |
| 140 | while lt j n bound 1200 |
| 141 | if ne get d j uchar 48 |
| 142 | ret ulong 1 |
| 143 | end |
| 144 | j = add j 1 |
| 145 | end |
| 146 | if eq q 0 |
| 147 | ret ulong 0 |
| 148 | end |
| 149 | ret and sub ulong get d sub q 1 48 1 |
| 150 | end |
| 151 | note fm_inc adds one at digit q-1; 1 when the carry leaves digit 0. |
| 152 | fn fm_inc d edit uchar 1200 q ulong to ulong |
| 153 | j = q |
| 154 | while gt j 0 bound 1200 |
| 155 | j = sub j 1 |
| 156 | c = get d j |
| 157 | if eq c uchar 57 |
| 158 | put d j uchar 48 |
| 159 | else |
| 160 | put d j uchar add c 1 |
| 161 | ret ulong 0 |
| 162 | end |
| 163 | end |
| 164 | ret ulong 1 |
| 165 | end |
| 166 | note fm_sp: inf/nan text; returns the new length, or 0 when x is finite. Sign printed for -inf and -nan. |
| 167 | fn fm_sp x f64 o edit uchar 1600 to ulong |
| 168 | b = bitcast ulong x |
| 169 | if ne and shr b 52 2047 2047 |
| 170 | ret ulong 0 |
| 171 | end |
| 172 | p = ulong 0 |
| 173 | if ne shr b 63 0 |
| 174 | put o 0 uchar 45 |
| 175 | p = ulong 1 |
| 176 | end |
| 177 | if eq and b 4503599627370495 0 |
| 178 | put o p uchar 105 |
| 179 | put o add p 1 uchar 110 |
| 180 | put o add p 2 uchar 102 |
| 181 | ret add p 3 |
| 182 | end |
| 183 | put o p uchar 110 |
| 184 | put o add p 1 uchar 97 |
| 185 | put o add p 2 uchar 110 |
| 186 | ret add p 3 |
| 187 | end |
| 188 | note fm_f: printf("%.Pf", x) into o; returns the length. |
| 189 | fn fm_f x f64 pr ulong o edit uchar 1600 to ulong |
| 190 | r = fm_sp x o |
| 191 | if ne r 0 |
| 192 | ret r |
| 193 | end |
| 194 | d = new uchar 1200 0 |
| 195 | r = fm_dig x d |
| 196 | ip = shr r 16 |
| 197 | n = add ip and r 65535 |
| 198 | q = add ip pr |
| 199 | p = ulong 0 |
| 200 | if ne shr bitcast ulong x 63 0 |
| 201 | put o 0 uchar 45 |
| 202 | p = ulong 1 |
| 203 | end |
| 204 | if ne fm_rup d n q 0 |
| 205 | if ne fm_inc d q 0 |
| 206 | put o p uchar 49 |
| 207 | p = add p 1 |
| 208 | end |
| 209 | end |
| 210 | for i ulong ip |
| 211 | put o p get d i |
| 212 | p = add p 1 |
| 213 | end |
| 214 | if eq pr 0 |
| 215 | ret p |
| 216 | end |
| 217 | put o p uchar 46 |
| 218 | p = add p 1 |
| 219 | for i ulong pr |
| 220 | put o p uchar fm_at d n add ip i |
| 221 | p = add p 1 |
| 222 | end |
| 223 | ret p |
| 224 | end |
| 225 | note fm_frst: index of the first nonzero digit of 0..n-1, n when there is none. |
| 226 | fn fm_frst d edit uchar 1200 n ulong to ulong |
| 227 | z = ulong 0 |
| 228 | while lt z n bound 1200 |
| 229 | if ne get d z uchar 48 |
| 230 | ret z |
| 231 | end |
| 232 | z = add z 1 |
| 233 | end |
| 234 | ret n |
| 235 | end |
| 236 | note fm_e2: the exponent of the e style: sign and at least two digits of |X|. xn is 1 when X < 0. |
| 237 | fn fm_e2 o edit uchar 1600 p ulong xn ulong xa ulong to ulong |
| 238 | put o p uchar 101 |
| 239 | q = add p 1 |
| 240 | if ne xn 0 |
| 241 | put o q uchar 45 |
| 242 | else |
| 243 | put o q uchar 43 |
| 244 | end |
| 245 | q = add q 1 |
| 246 | if lt xa 10 |
| 247 | put o q uchar 48 |
| 248 | q = add q 1 |
| 249 | end |
| 250 | w = fm_ndig xa |
| 251 | k = add q w |
| 252 | x = xa |
| 253 | for i ulong w |
| 254 | k = sub k 1 |
| 255 | put o k uchar add 48 rem x 10 |
| 256 | x = div x 10 |
| 257 | end |
| 258 | ret add q w |
| 259 | end |
| 260 | note fm_gd: R digit i of %g (digits d z.. of n); after a carry out of digit 0, 1 then zeros. |
| 261 | fn fm_gd d edit uchar 1200 n ulong z ulong c ulong i ulong to ulong |
| 262 | if ne c 0 |
| 263 | if eq i 0 |
| 264 | ret ulong 49 |
| 265 | end |
| 266 | ret ulong 48 |
| 267 | end |
| 268 | ret fm_at d n add z i |
| 269 | end |
| 270 | note fm_rd moves the pr digits R of %g to d 0..pr-1 (reads d z+i at or after the write at i). |
| 271 | fn fm_rd d edit uchar 1200 n ulong z ulong c ulong pr ulong to ulong |
| 272 | for i ulong pr |
| 273 | put d i uchar fm_gd d n z c i |
| 274 | end |
| 275 | ret pr |
| 276 | end |
| 277 | note fm_ges: e style of the k kept digits d 0..k-1 with exponent xs (see fm_ge) at o p. |
| 278 | fn fm_ges o edit uchar 1600 p ulong d edit uchar 1200 k ulong xs ulong to ulong |
| 279 | put o p get d 0 |
| 280 | q = add p 1 |
| 281 | if gt k 1 |
| 282 | put o q uchar 46 |
| 283 | q = add q 1 |
| 284 | for i ulong sub k 1 |
| 285 | put o q get d add i 1 |
| 286 | q = add q 1 |
| 287 | end |
| 288 | end |
| 289 | ret fm_e2 o q and xs 1 shr xs 1 |
| 290 | end |
| 291 | note fm_gfs: f style of the k kept digits d 0..k-1 with exponent xs (see fm_ge) at o p. |
| 292 | fn fm_gfs o edit uchar 1600 p ulong d edit uchar 1200 k ulong xs ulong to ulong |
| 293 | xa = shr xs 1 |
| 294 | q = p |
| 295 | if ne and xs 1 0 |
| 296 | put o q uchar 48 |
| 297 | put o add q 1 uchar 46 |
| 298 | q = add q 2 |
| 299 | for i ulong sub xa 1 |
| 300 | put o q uchar 48 |
| 301 | q = add q 1 |
| 302 | end |
| 303 | for i ulong k |
| 304 | put o q get d i |
| 305 | q = add q 1 |
| 306 | end |
| 307 | ret q |
| 308 | end |
| 309 | for i ulong add xa 1 |
| 310 | put o q get d i |
| 311 | q = add q 1 |
| 312 | end |
| 313 | if gt k add xa 1 |
| 314 | put o q uchar 46 |
| 315 | q = add q 1 |
| 316 | for i ulong sub sub k xa 1 |
| 317 | put o q get d add add xa 1 i |
| 318 | q = add q 1 |
| 319 | end |
| 320 | end |
| 321 | ret q |
| 322 | end |
| 323 | note fm_ge emits R = d 0..pr-1 with exponent X (xs = 2 |X| + 1 when X < 0, else 2 X) at o p. |
| 324 | note e style when X < -4 or X >= pr; trailing fraction zeros removed; returns the length. |
| 325 | fn fm_ge o edit uchar 1600 p ulong d edit uchar 1200 pr ulong xs ulong to ulong |
| 326 | xa = shr xs 1 |
| 327 | el = ulong 0 |
| 328 | if ne and xs 1 0 |
| 329 | if gt xa 4 |
| 330 | el = ulong 1 |
| 331 | end |
| 332 | else |
| 333 | if ge xa pr |
| 334 | el = ulong 1 |
| 335 | end |
| 336 | end |
| 337 | k = pr |
| 338 | while gt k 1 bound 1200 |
| 339 | if ne get d sub k 1 uchar 48 |
| 340 | break |
| 341 | end |
| 342 | k = sub k 1 |
| 343 | end |
| 344 | note k is the count of R digits that survive trailing-zero removal (at least 1). |
| 345 | if ne el 0 |
| 346 | ret fm_ges o p d k xs |
| 347 | end |
| 348 | ret fm_gfs o p d k xs |
| 349 | end |
| 350 | note fm_g: printf("%.Pg", x) into o (P 0 counts as 1); returns the length. The P significant digits are |
| 351 | note rounded at the first nonzero digit z; the exponent X = ip - 1 - z after rounding picks the style. |
| 352 | fn fm_g x f64 pr ulong o edit uchar 1600 to ulong |
| 353 | r = fm_sp x o |
| 354 | if ne r 0 |
| 355 | ret r |
| 356 | end |
| 357 | if eq pr 0 |
| 358 | pr = ulong 1 |
| 359 | end |
| 360 | d = new uchar 1200 0 |
| 361 | r = fm_dig x d |
| 362 | ip = shr r 16 |
| 363 | n = add ip and r 65535 |
| 364 | p = ulong 0 |
| 365 | if ne shr bitcast ulong x 63 0 |
| 366 | put o 0 uchar 45 |
| 367 | p = ulong 1 |
| 368 | end |
| 369 | z = fm_frst d n |
| 370 | if eq z n |
| 371 | put o p uchar 48 |
| 372 | ret add p 1 |
| 373 | end |
| 374 | q = add z pr |
| 375 | c = ulong 0 |
| 376 | if ne fm_rup d n q 0 |
| 377 | c = fm_inc d q |
| 378 | end |
| 379 | note after a carry out of digit 0 the digits are 1 0..0 one place up: the leading 1 sits at index -1, |
| 380 | note so X = ip. Otherwise X = ip - 1 - z for the first nonzero rounded digit z. |
| 381 | xs = shl ip 1 |
| 382 | if eq c 0 |
| 383 | z = fm_frst d q |
| 384 | if lt z ip |
| 385 | xs = shl sub sub ip 1 z 1 |
| 386 | else |
| 387 | xs = or shl add sub z ip 1 1 1 |
| 388 | end |
| 389 | end |
| 390 | r = fm_rd d n z c pr |
| 391 | ret fm_ge o p d r xs |
| 392 | end |