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 / nvidia / om / api / attn_math.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note Preserve src/omega_gpu_hostmath.c operation order and its filed fdlibm constants. |
| 4 | fn av_pow2 k long to f64 |
| 5 | if gt k 1023 |
| 6 | ret bitcast f64 ulong 9218868437227405312 |
| 7 | end |
| 8 | if lt k -1074 |
| 9 | ret bitcast f64 ulong 0 |
| 10 | end |
| 11 | if ge k -1022 |
| 12 | ret bitcast f64 shl ulong add k 1023 52 |
| 13 | end |
| 14 | ret mul bitcast f64 shl ulong 1 52 bitcast f64 shl ulong add k 2045 52 |
| 15 | end |
| 16 | fn av_log x f64 to f64 |
| 17 | if or not gt x bitcast f64 ulong 0 gt x bitcast f64 ulong 9218868437227405311 |
| 18 | ret bitcast f64 ulong 9221120237041090560 |
| 19 | end |
| 20 | e = long 0 |
| 21 | if lt x bitcast f64 ulong 4503599627370496 |
| 22 | x = mul x bitcast f64 ulong 4850376798678024192 |
| 23 | e = long -54 |
| 24 | end |
| 25 | u = bitcast ulong x |
| 26 | e = add e sub long and shr u 52 2047 1023 |
| 27 | m = bitcast f64 or and u 4503599627370495 4607182418800017408 |
| 28 | if gt m bitcast f64 ulong 4609047870845172685 |
| 29 | m = mul m bitcast f64 ulong 4602678819172646912 |
| 30 | e = add e 1 |
| 31 | end |
| 32 | t = div sub m bitcast f64 ulong 4607182418800017408 add m bitcast f64 ulong 4607182418800017408 |
| 33 | t2 = mul t t |
| 34 | s = bitcast f64 ulong 0 |
| 35 | k = long 25 |
| 36 | while ge k 1 |
| 37 | s = add mul s t2 div bitcast f64 ulong 4607182418800017408 f64 k |
| 38 | k = sub k 2 |
| 39 | end |
| 40 | x = mul f64 e bitcast f64 ulong 4461442080421002358 |
| 41 | s = mul mul bitcast f64 ulong 4611686018427387904 t s |
| 42 | s = add s x |
| 43 | x = mul f64 e bitcast f64 ulong 4604418534311723008 |
| 44 | ret add x s |
| 45 | end |
| 46 | fn av_exp y f64 to f64 |
| 47 | if ne y y |
| 48 | ret y |
| 49 | end |
| 50 | if gt y bitcast f64 ulong 4649454506724284170 |
| 51 | ret bitcast f64 ulong 9218868437227405312 |
| 52 | end |
| 53 | if lt y bitcast f64 ulong 13873138101193906586 |
| 54 | ret bitcast f64 ulong 0 |
| 55 | end |
| 56 | fk = mul y bitcast f64 ulong 4609176140021203710 |
| 57 | round = bitcast f64 ulong 13826050856027422720 |
| 58 | if ge fk bitcast f64 ulong 0 |
| 59 | round = bitcast f64 ulong 4602678819172646912 |
| 60 | end |
| 61 | k = long add fk round |
| 62 | kd = f64 k |
| 63 | r = sub sub y mul kd bitcast f64 ulong 4604418534311723008 mul kd bitcast f64 ulong 4461442080421002358 |
| 64 | p = bitcast f64 ulong 4607182418800017408 |
| 65 | n = long 17 |
| 66 | while ge n 1 |
| 67 | p = add bitcast f64 ulong 4607182418800017408 div mul r p f64 n |
| 68 | n = sub n 1 |
| 69 | end |
| 70 | if lt k -1020 |
| 71 | ret mul mul p av_pow2 long -1020 av_pow2 add k 1020 |
| 72 | end |
| 73 | ret mul p av_pow2 k |
| 74 | end |
| 75 | fn av_pow base f64 e f64 to f64 |
| 76 | if or eq base bitcast f64 ulong 4607182418800017408 eq e bitcast f64 ulong 0 |
| 77 | ret bitcast f64 ulong 4607182418800017408 |
| 78 | end |
| 79 | ret av_exp mul e av_log base |
| 80 | end |
| 81 | fn av_sinc a f64 dest edit ulong 2 to void |
| 82 | fnn = mul a bitcast f64 ulong 4603909380684499075 |
| 83 | round = bitcast f64 ulong 13826050856027422720 |
| 84 | if ge fnn bitcast f64 ulong 0 |
| 85 | round = bitcast f64 ulong 4602678819172646912 |
| 86 | end |
| 87 | n = long add fnn round |
| 88 | nd = f64 n |
| 89 | r = sub a mul nd bitcast f64 ulong 4609753056924401664 |
| 90 | r = sub r mul nd bitcast f64 ulong 4454258360616747008 |
| 91 | r = sub r mul nd bitcast f64 ulong 4297306550709518336 |
| 92 | r = sub r mul nd bitcast f64 ulong 4142048980368378305 |
| 93 | w = mul r r |
| 94 | ps = bitcast f64 ulong 4607182418800017408 |
| 95 | k = long 15 |
| 96 | while ge k 3 |
| 97 | nd = f64 mul k sub k 1 |
| 98 | ps = mul w ps |
| 99 | ps = div ps nd |
| 100 | ps = sub bitcast f64 ulong 4607182418800017408 ps |
| 101 | k = sub k 2 |
| 102 | end |
| 103 | pc = bitcast f64 ulong 4607182418800017408 |
| 104 | k = long 16 |
| 105 | while ge k 2 |
| 106 | nd = f64 mul k sub k 1 |
| 107 | pc = mul w pc |
| 108 | pc = div pc nd |
| 109 | pc = sub bitcast f64 ulong 4607182418800017408 pc |
| 110 | k = sub k 2 |
| 111 | end |
| 112 | sr = mul r ps |
| 113 | q = and ulong n 3 |
| 114 | if eq q 0 |
| 115 | put dest 0 bitcast ulong sr |
| 116 | put dest 1 bitcast ulong pc |
| 117 | else |
| 118 | if eq q 1 |
| 119 | put dest 0 bitcast ulong pc |
| 120 | put dest 1 bitcast ulong neg sr |
| 121 | else |
| 122 | if eq q 2 |
| 123 | put dest 0 bitcast ulong neg sr |
| 124 | put dest 1 bitcast ulong neg pc |
| 125 | else |
| 126 | put dest 0 bitcast ulong neg pc |
| 127 | put dest 1 bitcast ulong sr |
| 128 | end |
| 129 | end |
| 130 | end |
| 131 | end |
| 132 | fn av_rrow d edit f32 * q view ulong 16 off ulong pos ulong to void |
| 133 | sc = new ulong 2 0 |
| 134 | half = div get q 3 2 |
| 135 | for i ulong half |
| 136 | idx = add off i |
| 137 | if lt i div get q 2 2 |
| 138 | inv = f64 mul i 2 |
| 139 | inv = neg inv |
| 140 | inv = div inv f64 get q 2 |
| 141 | inv = av_pow bitcast f64 get q 1 inv |
| 142 | inv = mul f64 pos inv |
| 143 | av_sinc inv sc |
| 144 | put d idx f32 bitcast f64 get sc 1 |
| 145 | idx = add idx half |
| 146 | put d idx f32 bitcast f64 get sc 0 |
| 147 | else |
| 148 | put d idx f32 1.0 |
| 149 | idx = add idx half |
| 150 | put d idx f32 0.0 |
| 151 | end |
| 152 | end |
| 153 | end |
| 154 | fn vc_rope d edit f32 * q view ulong 16 to ulong |
| 155 | for j ulong get q 7 |
| 156 | row = add get q 6 j |
| 157 | off = mul row get q 3 |
| 158 | pos = add get q 0 row |
| 159 | av_rrow d q off pos |
| 160 | end |
| 161 | ret 0 |
| 162 | end |
| 163 | fn av_attnrope d edit f32 * q view ulong 16 to ulong |
| 164 | z = new ulong 16 0 |
| 165 | for j ulong 16 |
| 166 | put z j get q j |
| 167 | end |
| 168 | for bb ulong add div get q 4 1000000 1 |
| 169 | put z 6 mul bb 1000000 |
| 170 | cnt = sub get q 4 get z 6 |
| 171 | if gt cnt 1000000 |
| 172 | cnt = ulong 1000000 |
| 173 | end |
| 174 | put z 7 cnt |
| 175 | rc = vc_rope d z |
| 176 | if ne rc 0 |
| 177 | ret rc |
| 178 | end |
| 179 | end |
| 180 | ret 0 |
| 181 | end |