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 / components / cm_math_extra.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Double-double Taylor exponential, exact power-of-two transport and subnormal rounding. |
| 3 | fn cm_dd_div_scalar a edit ulong 2 b f64 to void |
| 4 | cm_div bitcast f64 get a 0 bitcast f64 get a 1 b f64 0.0 a |
| 5 | end |
| 6 | fn cm_exp_series r view ulong 2 out edit ulong 2 to void |
| 7 | term = new ulong 2 0 |
| 8 | put term 0 4607182418800017408 |
| 9 | put out 0 4607182418800017408 |
| 10 | put out 1 0 |
| 11 | for i ulong 40 |
| 12 | cm_dd_mul term r |
| 13 | cm_dd_div_scalar term f64 add i 1 |
| 14 | cm_dd_add out term |
| 15 | end |
| 16 | end |
| 17 | fn cm_exp_reduce ah f64 al f64 base2 bool out edit ulong 2 to long |
| 18 | r = new ulong 2 0 |
| 19 | v = ah |
| 20 | k = long 0 |
| 21 | if not base2 |
| 22 | v = mul ah bitcast f64 ulong 4609176140021203710 |
| 23 | end |
| 24 | if ge v f64 0.0 |
| 25 | k = long add v f64 0.5 |
| 26 | else |
| 27 | k = long sub v f64 0.5 |
| 28 | end |
| 29 | if base2 |
| 30 | cm_add ah al neg f64 k f64 0.0 r |
| 31 | cm_mul bitcast f64 get r 0 bitcast f64 get r 1 bitcast f64 ulong 4604418534313441775 bitcast f64 ulong 4358002977218854975 r |
| 32 | else |
| 33 | cm_mul neg f64 k f64 0.0 bitcast f64 ulong 4604418534313441775 bitcast f64 ulong 4358002977218854975 r |
| 34 | cm_add ah al bitcast f64 get r 0 bitcast f64 get r 1 r |
| 35 | end |
| 36 | cm_exp_series r out |
| 37 | ret k |
| 38 | end |
| 39 | fn cm_scale_bits a edit ulong 2 k long to ulong |
| 40 | if lt k -1000 |
| 41 | scale = bitcast f64 shl ulong add k 2097 52 |
| 42 | cm_dd_scale a scale |
| 43 | h = bitcast f64 get a 0 |
| 44 | l = bitcast f64 get a 1 |
| 45 | if le h f64 4503599627370496 |
| 46 | n = ulong h |
| 47 | frac = add sub h f64 n l |
| 48 | if gt frac f64 0.5 |
| 49 | n = add n 1 |
| 50 | else |
| 51 | if lt frac f64 -0.5 |
| 52 | n = sub n 1 |
| 53 | else |
| 54 | if and eq frac f64 0.5 ne and n 1 0 |
| 55 | n = add n 1 |
| 56 | end |
| 57 | if and eq frac f64 -0.5 ne and n 1 0 |
| 58 | n = sub n 1 |
| 59 | end |
| 60 | end |
| 61 | end |
| 62 | ret n |
| 63 | end |
| 64 | normal = mul add h l bitcast f64 ulong 4503599627370496 |
| 65 | ret bitcast ulong mul normal bitcast f64 ulong 4372995238176751616 |
| 66 | end |
| 67 | if eq k 1024 |
| 68 | cm_dd_scale a f64 2.0 |
| 69 | k = 1023 |
| 70 | end |
| 71 | scale = bitcast f64 shl ulong add k 1023 52 |
| 72 | ret bitcast ulong mul add bitcast f64 get a 0 bitcast f64 get a 1 scale |
| 73 | end |
| 74 | fn cm_exp bits ulong base2 bool to ulong |
| 75 | x = bitcast f64 bits |
| 76 | if ne x x |
| 77 | ret bitcast ulong add x x |
| 78 | end |
| 79 | if base2 |
| 80 | if ge x f64 1024.0 |
| 81 | ret 9218868437227405312 |
| 82 | end |
| 83 | if le x f64 -1075.0 |
| 84 | ret 0 |
| 85 | end |
| 86 | else |
| 87 | if gt x f64 710.0 |
| 88 | ret 9218868437227405312 |
| 89 | end |
| 90 | if lt x f64 -746.0 |
| 91 | ret 0 |
| 92 | end |
| 93 | end |
| 94 | out = new ulong 2 0 |
| 95 | k = cm_exp_reduce x f64 0.0 base2 out |
| 96 | if gt k 1024 |
| 97 | ret 9218868437227405312 |
| 98 | end |
| 99 | if lt k -1075 |
| 100 | ret 0 |
| 101 | end |
| 102 | ret cm_scale_bits out k |
| 103 | end |