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 / bridge / ggml / kernels / math.om
| 1 | note math.om: private f64/f16 helpers prepended to the ggml-omega kernel units that need them |
| 2 | note (exp, log, pow, sin, cos, f16 <-> f32). Constants are binary64 bit patterns checked in Wolfram |
| 3 | note Language (ImportByteArray of the bits, difference to Log[2], 1/Log[2], Pi/2, 2/Pi, Sqrt[2]): |
| 4 | note ln2 hi+lo -1.2e-26, pio2 1+2+3 -8.5e-32; the hi parts end in >= 21 zero bits, so k*hi is exact for |k| < 2^21. |
| 5 | const cln2h ulong 4604418534311723008 |
| 6 | const cln2l ulong 4461442080421002358 |
| 7 | const civln2 ulong 4609176140021203710 |
| 8 | const cpio21 ulong 4609753056924401664 |
| 9 | const cpio22 ulong 4454258360616747008 |
| 10 | const cpio23 ulong 4297306550709518336 |
| 11 | const civpio2 ulong 4603909380684499075 |
| 12 | const csqrt2 ulong 4609047870845172685 |
| 13 | note rnd: x rounded to the nearest whole number (halves away from zero), as long. |
| 14 | fn rnd x f64 to long |
| 15 | if lt x 0.0 |
| 16 | ret long sub x 0.5 |
| 17 | end |
| 18 | ret long add x 0.5 |
| 19 | end |
| 20 | note ex64: e^x. Inputs below -200 give 0 (every f32 result there is 0), above 709 are clamped (f32 inf). |
| 21 | fn ex64 x f64 to f64 |
| 22 | if ne x x |
| 23 | ret x |
| 24 | end |
| 25 | if lt x -200.0 |
| 26 | ret 0.0 |
| 27 | end |
| 28 | y = x |
| 29 | if gt y 709.0 |
| 30 | y = 709.0 |
| 31 | end |
| 32 | k = rnd mul y bitcast f64 civln2 |
| 33 | kf = f64 k |
| 34 | r = sub sub y mul kf bitcast f64 cln2h mul kf bitcast f64 cln2l |
| 35 | p = f64 1.0 |
| 36 | for i ulong 13 |
| 37 | n = f64 sub 13 i |
| 38 | p = add 1.0 div mul r p n |
| 39 | end |
| 40 | sc = bitcast f64 shl ulong add k 1023 52 |
| 41 | ret mul p sc |
| 42 | end |
| 43 | note ln64: natural log of a positive normal x. |
| 44 | fn ln64 x f64 to f64 |
| 45 | b = bitcast ulong x |
| 46 | e = sub long shr b 52 1023 |
| 47 | m = bitcast f64 or and b 4503599627370495 shl ulong 1023 52 |
| 48 | if gt m bitcast f64 csqrt2 |
| 49 | m = div m 2.0 |
| 50 | e = add e 1 |
| 51 | end |
| 52 | s = div sub m 1.0 add m 1.0 |
| 53 | s2 = mul s s |
| 54 | t = f64 0.0 |
| 55 | for i ulong 12 |
| 56 | n = f64 add mul 2 sub 11 i 1 |
| 57 | t = add div 1.0 n mul s2 t |
| 58 | end |
| 59 | ef = f64 e |
| 60 | ret add mul ef bitcast f64 cln2h add mul ef bitcast f64 cln2l mul mul 2.0 s t |
| 61 | end |
| 62 | note sinr / cosr: Taylor series on |r| <= pi/4. |
| 63 | fn sinr r f64 to f64 |
| 64 | r2 = mul r r |
| 65 | t = f64 1.0 |
| 66 | for i ulong 9 |
| 67 | n = f64 sub 9 i |
| 68 | t = sub 1.0 div mul r2 t mul mul 2.0 n add mul 2.0 n 1.0 |
| 69 | end |
| 70 | ret mul r t |
| 71 | end |
| 72 | fn cosr r f64 to f64 |
| 73 | r2 = mul r r |
| 74 | t = f64 1.0 |
| 75 | for i ulong 10 |
| 76 | n = f64 sub 10 i |
| 77 | t = sub 1.0 div mul r2 t mul sub mul 2.0 n 1.0 mul 2.0 n |
| 78 | end |
| 79 | ret t |
| 80 | end |
| 81 | note sc64: sin (c 0) or cos (c 1) of x, reduced by k * pi/2 in three parts. |
| 82 | fn sc64 x f64 c ulong to f64 |
| 83 | if ne x x |
| 84 | ret x |
| 85 | end |
| 86 | k = rnd mul x bitcast f64 civpio2 |
| 87 | kf = f64 k |
| 88 | r = sub sub sub x mul kf bitcast f64 cpio21 mul kf bitcast f64 cpio22 mul kf bitcast f64 cpio23 |
| 89 | q = add ulong and k 3 c |
| 90 | q = and q 3 |
| 91 | if eq q 0 |
| 92 | ret sinr r |
| 93 | end |
| 94 | if eq q 1 |
| 95 | ret cosr r |
| 96 | end |
| 97 | if eq q 2 |
| 98 | ret neg sinr r |
| 99 | end |
| 100 | ret neg cosr r |
| 101 | end |
| 102 | note h2f: binary16 bits to f32. |
| 103 | fn h2f h ushort to f32 |
| 104 | v = uint h |
| 105 | s = shl and shr v 15 1 31 |
| 106 | e = and shr v 10 31 |
| 107 | m = and v 1023 |
| 108 | if eq e 0 |
| 109 | z = mul f32 m 0.000000059604644775390625f32 |
| 110 | if eq s 0 |
| 111 | ret z |
| 112 | end |
| 113 | ret neg z |
| 114 | end |
| 115 | if eq e 31 |
| 116 | ret bitcast f32 or or s 2139095040 shl m 13 |
| 117 | end |
| 118 | ret bitcast f32 or or s shl add e 112 23 shl m 13 |
| 119 | end |
| 120 | note rne: v >> sh (sh 1 .. 31) rounded to nearest, ties to even. |
| 121 | fn rne v uint sh uint to uint |
| 122 | r = shr v sh |
| 123 | hf = shl uint 1 sub sh 1 |
| 124 | rm = and v sub shl hf 1 1 |
| 125 | up = gt rm hf |
| 126 | if and eq rm hf eq and r 1 1 |
| 127 | up = true |
| 128 | end |
| 129 | if up |
| 130 | ret add r 1 |
| 131 | end |
| 132 | ret r |
| 133 | end |
| 134 | note f2h: f32 to binary16 bits, round to nearest even (as FCVT). |
| 135 | fn f2h x f32 to ushort |
| 136 | b = bitcast uint x |
| 137 | s = and shr b 16 32768 |
| 138 | e = long and shr b 23 255 |
| 139 | m = and b 8388607 |
| 140 | if eq e 255 |
| 141 | if eq m 0 |
| 142 | ret ushort or s 31744 |
| 143 | end |
| 144 | ret ushort or or s 32256 shr m 13 |
| 145 | end |
| 146 | ee = sub e 112 |
| 147 | if ge ee 31 |
| 148 | ret ushort or s 31744 |
| 149 | end |
| 150 | if le ee 0 |
| 151 | if lt ee -10 |
| 152 | ret ushort s |
| 153 | end |
| 154 | ret ushort or s rne or m 8388608 uint sub 14 ee |
| 155 | end |
| 156 | ret ushort or s rne or shl uint ee 23 m 13 |
| 157 | end |