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 / om / core / hostmath.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Copyright 2026 Drake Stapleton |
| 3 | note Source constants rounded to binary64 with Wolfram N[decimal80,MachinePrecision]; receipt records the exact query. |
| 4 | fn LNHI to f64 |
| 5 | ret bitcast f64 ulong 4604418534311723008 |
| 6 | end |
| 7 | fn LNLO to f64 |
| 8 | ret bitcast f64 ulong 4461442080421002358 |
| 9 | end |
| 10 | fn ILN2 to f64 |
| 11 | ret bitcast f64 ulong 4609176140021203710 |
| 12 | end |
| 13 | fn SQRT2 to f64 |
| 14 | ret bitcast f64 ulong 4609047870845172685 |
| 15 | end |
| 16 | fn hpow2 k long to f64 |
| 17 | if gt k 1023 |
| 18 | ret bitcast f64 shl ulong 2047 52 |
| 19 | end |
| 20 | if lt k -1074 |
| 21 | ret 0.0f64 |
| 22 | end |
| 23 | if ge k -1022 |
| 24 | ret bitcast f64 shl ulong add k 1023 52 |
| 25 | end |
| 26 | ret mul bitcast f64 shl ulong 1 52 bitcast f64 shl ulong add k 2045 52 |
| 27 | end |
| 28 | fn hlogsum e long t f64 s f64 to f64 |
| 29 | ret add mul f64 e LNHI add mul mul 2.0f64 t s mul f64 e LNLO |
| 30 | end |
| 31 | fn hlog x f64 to f64 |
| 32 | if or not gt x 0.0f64 gt x bitcast f64 sub shl ulong 2047 52 1 |
| 33 | ret bitcast f64 or shl ulong 2047 52 shl ulong 1 51 |
| 34 | end |
| 35 | e = long 0 |
| 36 | if lt x bitcast f64 shl ulong 1 52 |
| 37 | x = mul x 18014398509481984.0f64 |
| 38 | e = -54 |
| 39 | end |
| 40 | u = bitcast ulong x |
| 41 | e = sub add e long and shr u 52 2047 1023 |
| 42 | m = bitcast f64 or and u sub shl ulong 1 52 1 shl ulong 1023 52 |
| 43 | if gt m SQRT2 |
| 44 | m = mul m 0.5f64 |
| 45 | e = add e 1 |
| 46 | end |
| 47 | t = div sub m 1.0f64 add m 1.0f64 |
| 48 | t2 = mul t t |
| 49 | s = f64 0.0 |
| 50 | k = long 25 |
| 51 | while ge k 1 bound 13 |
| 52 | s = add mul s t2 div 1.0f64 f64 k |
| 53 | k = sub k 2 |
| 54 | end |
| 55 | ret hlogsum e t s |
| 56 | end |
| 57 | fn hexp y f64 to f64 |
| 58 | if ne y y |
| 59 | ret y |
| 60 | end |
| 61 | if gt y bitcast f64 ulong 4649454506724284170 |
| 62 | ret bitcast f64 shl ulong 2047 52 |
| 63 | end |
| 64 | if lt y bitcast f64 ulong 13873138101193906586 |
| 65 | ret 0.0f64 |
| 66 | end |
| 67 | fk = mul y ILN2 |
| 68 | adj = f64 0.5 |
| 69 | if lt fk 0.0f64 |
| 70 | adj = -0.5f64 |
| 71 | end |
| 72 | k = long add fk adj |
| 73 | kd = f64 k |
| 74 | r = sub sub y mul kd LNHI mul kd LNLO |
| 75 | p = f64 1.0 |
| 76 | n = long 17 |
| 77 | while ge n 1 bound 17 |
| 78 | p = add 1.0f64 div mul r p f64 n |
| 79 | n = sub n 1 |
| 80 | end |
| 81 | if lt k -1020 |
| 82 | ret mul mul p hpow2 -1020 hpow2 add k 1020 |
| 83 | end |
| 84 | ret mul p hpow2 k |
| 85 | end |
| 86 | fn hround x f64 to long |
| 87 | if lt x 0.0f64 |
| 88 | ret long sub x 0.5f64 |
| 89 | end |
| 90 | ret long add x 0.5f64 |
| 91 | end |
| 92 | fn hpoly w f64 k long to f64 |
| 93 | p = f64 1.0 |
| 94 | while ge k 2 bound 8 |
| 95 | p = sub 1.0f64 div mul w p f64 mul k sub k 1 |
| 96 | k = sub k 2 |
| 97 | end |
| 98 | ret p |
| 99 | end |
| 100 | fn hsin a f64 o edit ulong 2 to void |
| 101 | n = hround mul a bitcast f64 ulong 4603909380684499075 |
| 102 | nd = f64 n |
| 103 | r = sub a mul nd bitcast f64 ulong 4609753056924401664 |
| 104 | r = sub r mul nd bitcast f64 ulong 4454258360616747008 |
| 105 | r = sub r mul nd bitcast f64 ulong 4297306550709518336 |
| 106 | r = sub r mul nd bitcast f64 ulong 4142048980368378305 |
| 107 | w = mul r r |
| 108 | sr = mul r hpoly w 15 |
| 109 | cr = hpoly w 16 |
| 110 | quad = ulong and n 3 |
| 111 | if eq quad 0 |
| 112 | put o 0 bitcast ulong sr |
| 113 | put o 1 bitcast ulong cr |
| 114 | end |
| 115 | if eq quad 1 |
| 116 | put o 0 bitcast ulong cr |
| 117 | put o 1 bitcast ulong neg sr |
| 118 | end |
| 119 | if eq quad 2 |
| 120 | put o 0 bitcast ulong neg sr |
| 121 | put o 1 bitcast ulong neg cr |
| 122 | end |
| 123 | if eq quad 3 |
| 124 | put o 0 bitcast ulong neg cr |
| 125 | put o 1 bitcast ulong sr |
| 126 | end |
| 127 | end |
| 128 | fn omhost mode ulong abits ulong bbits ulong o edit ulong 2 to void |
| 129 | a = bitcast f64 abits |
| 130 | b = bitcast f64 bbits |
| 131 | if eq mode 0 |
| 132 | put o 0 bitcast ulong hlog a |
| 133 | ret |
| 134 | end |
| 135 | if eq mode 1 |
| 136 | put o 0 bitcast ulong hexp a |
| 137 | ret |
| 138 | end |
| 139 | if eq mode 2 |
| 140 | if or eq a 1.0f64 eq b 0.0f64 |
| 141 | put o 0 bitcast ulong 1.0f64 |
| 142 | else |
| 143 | put o 0 bitcast ulong hexp mul b hlog a |
| 144 | end |
| 145 | ret |
| 146 | end |
| 147 | if eq mode 3 |
| 148 | hsin a o |
| 149 | ret |
| 150 | end |
| 151 | e = long a |
| 152 | x = ulong 0 |
| 153 | if gt e 127 |
| 154 | x = shl ulong 255 23 |
| 155 | else |
| 156 | if ge e -126 |
| 157 | x = shl ulong add e 127 23 |
| 158 | else |
| 159 | if ge e -149 |
| 160 | x = shl ulong 1 ulong add e 149 |
| 161 | end |
| 162 | end |
| 163 | end |
| 164 | put o 0 x |
| 165 | end |