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 / src / algebra / oma_core.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 core logic backing src/algebra/ (oma_trit.c, oma_z3.c, |
| 3 | note oma_pack.c): balanced-ternary and Z3 64-lane bitplane encode/decode, |
| 4 | note plane add/mul/dot (SWAR popcount64), 16-byte LE block serialize/deserialize, |
| 5 | note 5-trit base-3 dense byte pack/unpack, and int64 <-> balanced ternary. |
| 6 | om2 |
| 7 | |
| 8 | fn oma_popcount64 x ulong to ulong |
| 9 | y = sub x and shr x 1 0x5555555555555555 |
| 10 | y = add and y 0x3333333333333333 and shr y 2 0x3333333333333333 |
| 11 | y = and add y shr y 4 0x0f0f0f0f0f0f0f0f |
| 12 | ret shr mul y 0x0101010101010101 56 |
| 13 | end |
| 14 | |
| 15 | export fn oma_om_block_encode src view uchar 64 dst edit ulong 2 to ulong |
| 16 | p = ulong 0 |
| 17 | n = ulong 0 |
| 18 | for i ulong 64 |
| 19 | b = ulong get src i |
| 20 | if eq b 1 |
| 21 | p = or p shl ulong 1 i |
| 22 | else |
| 23 | if eq b 255 |
| 24 | n = or n shl ulong 1 i |
| 25 | else |
| 26 | if ne b 0 |
| 27 | ret 1 |
| 28 | end |
| 29 | end |
| 30 | end |
| 31 | end |
| 32 | put dst 0 p |
| 33 | put dst 1 n |
| 34 | ret 0 |
| 35 | end |
| 36 | |
| 37 | export fn oma_om_block_decode pos ulong neg_p ulong dst edit uchar 64 to ulong |
| 38 | if ne and pos neg_p 0 |
| 39 | ret 3 |
| 40 | end |
| 41 | for i ulong 64 |
| 42 | pb = and shr pos i 1 |
| 43 | nb = and shr neg_p i 1 |
| 44 | if eq pb 1 |
| 45 | put dst i uchar 1 |
| 46 | else |
| 47 | if eq nb 1 |
| 48 | put dst i uchar 255 |
| 49 | else |
| 50 | put dst i uchar 0 |
| 51 | end |
| 52 | end |
| 53 | end |
| 54 | ret 0 |
| 55 | end |
| 56 | |
| 57 | export fn oma_om_block_add a view ulong 2 b view ulong 2 dst edit ulong 4 to ulong |
| 58 | ap = get a 0 |
| 59 | an = get a 1 |
| 60 | bp = get b 0 |
| 61 | bn = get b 1 |
| 62 | if or ne and ap an 0 ne and bp bn 0 |
| 63 | ret 3 |
| 64 | end |
| 65 | all1 = ulong 0xffffffffffffffff |
| 66 | az = xor all1 or ap an |
| 67 | bz = xor all1 or bp bn |
| 68 | sp = or or and ap bz and az bp and an bn |
| 69 | sn = or or and an bz and az bn and ap bp |
| 70 | put dst 0 sp |
| 71 | put dst 1 sn |
| 72 | put dst 2 and ap bp |
| 73 | put dst 3 and an bn |
| 74 | ret 0 |
| 75 | end |
| 76 | |
| 77 | export fn oma_om_block_mul a view ulong 2 b view ulong 2 dst edit ulong 2 to ulong |
| 78 | ap = get a 0 |
| 79 | an = get a 1 |
| 80 | bp = get b 0 |
| 81 | bn = get b 1 |
| 82 | if or ne and ap an 0 ne and bp bn 0 |
| 83 | ret 3 |
| 84 | end |
| 85 | put dst 0 or and ap bp and an bn |
| 86 | put dst 1 or and ap bn and an bp |
| 87 | ret 0 |
| 88 | end |
| 89 | |
| 90 | export fn oma_om_block_dot a view ulong 2 b view ulong 2 dst edit ulong 1 to ulong |
| 91 | ap = get a 0 |
| 92 | an = get a 1 |
| 93 | bp = get b 0 |
| 94 | bn = get b 1 |
| 95 | if or ne and ap an 0 ne and bp bn 0 |
| 96 | ret 3 |
| 97 | end |
| 98 | pp = or and ap bp and an bn |
| 99 | pn = or and ap bn and an bp |
| 100 | put dst 0 sub oma_popcount64 pp oma_popcount64 pn |
| 101 | ret 0 |
| 102 | end |
| 103 | |
| 104 | export fn oma_om_z3_encode src view uchar 64 dst edit ulong 2 to ulong |
| 105 | o = ulong 0 |
| 106 | t = ulong 0 |
| 107 | for i ulong 64 |
| 108 | b = ulong get src i |
| 109 | if eq b 1 |
| 110 | o = or o shl ulong 1 i |
| 111 | else |
| 112 | if eq b 2 |
| 113 | t = or t shl ulong 1 i |
| 114 | else |
| 115 | if ne b 0 |
| 116 | ret 6 |
| 117 | end |
| 118 | end |
| 119 | end |
| 120 | end |
| 121 | put dst 0 o |
| 122 | put dst 1 t |
| 123 | ret 0 |
| 124 | end |
| 125 | |
| 126 | export fn oma_om_z3_decode one ulong two ulong dst edit uchar 64 to ulong |
| 127 | if ne and one two 0 |
| 128 | ret 3 |
| 129 | end |
| 130 | for i ulong 64 |
| 131 | b0 = and shr one i 1 |
| 132 | b1 = and shr two i 1 |
| 133 | put dst i uchar or b0 shl b1 1 |
| 134 | end |
| 135 | ret 0 |
| 136 | end |
| 137 | |
| 138 | export fn oma_om_block_serialize pos ulong neg_p ulong dst edit uchar 16 to ulong |
| 139 | if ne and pos neg_p 0 |
| 140 | ret 3 |
| 141 | end |
| 142 | for i ulong 8 |
| 143 | put dst i uchar and shr pos mul i 8 255 |
| 144 | put dst add 8 i uchar and shr neg_p mul i 8 255 |
| 145 | end |
| 146 | ret 0 |
| 147 | end |
| 148 | |
| 149 | export fn oma_om_block_deserialize src view uchar 16 dst edit ulong 2 to ulong |
| 150 | p = ulong 0 |
| 151 | n = ulong 0 |
| 152 | for i ulong 8 |
| 153 | bp = ulong get src i |
| 154 | bn = ulong get src add 8 i |
| 155 | p = or p shl bp mul i 8 |
| 156 | n = or n shl bn mul i 8 |
| 157 | end |
| 158 | if ne and p n 0 |
| 159 | ret 3 |
| 160 | end |
| 161 | put dst 0 p |
| 162 | put dst 1 n |
| 163 | ret 0 |
| 164 | end |
| 165 | |
| 166 | export fn oma_om_dense_byte_decode bv ulong dst edit uchar 5 to ulong |
| 167 | if ge bv 243 |
| 168 | ret 5 |
| 169 | end |
| 170 | v = bv |
| 171 | for j ulong 5 |
| 172 | r = rem v 3 |
| 173 | v = div v 3 |
| 174 | if eq r 0 |
| 175 | put dst j uchar 255 |
| 176 | else |
| 177 | put dst j uchar sub r 1 |
| 178 | end |
| 179 | end |
| 180 | ret 0 |
| 181 | end |
| 182 | |
| 183 | export fn oma_om_pack_dense_byte src view uchar * start ulong n ulong to ulong |
| 184 | v = ulong 0 |
| 185 | p = ulong 1 |
| 186 | for j ulong 5 |
| 187 | idx = add start j |
| 188 | d = ulong 1 |
| 189 | if lt idx n |
| 190 | b = ulong get src idx |
| 191 | if eq b 255 |
| 192 | d = 0 |
| 193 | else |
| 194 | if eq b 1 |
| 195 | d = 2 |
| 196 | end |
| 197 | end |
| 198 | end |
| 199 | v = add v mul d p |
| 200 | p = mul p 3 |
| 201 | end |
| 202 | ret v |
| 203 | end |
| 204 | |
| 205 | export fn oma_om_int_to_bt v_bits ulong dst edit uchar 41 n_out edit ulong 1 to void |
| 206 | is_neg = ulong 0 |
| 207 | u = v_bits |
| 208 | if ge v_bits 0x8000000000000000 |
| 209 | is_neg = 1 |
| 210 | u = sub 0 v_bits |
| 211 | end |
| 212 | n = ulong 0 |
| 213 | while gt u 0 |
| 214 | r = rem u 3 |
| 215 | u = div u 3 |
| 216 | d = ulong r |
| 217 | if eq r 2 |
| 218 | d = 255 |
| 219 | u = add u 1 |
| 220 | end |
| 221 | if eq is_neg 1 |
| 222 | if eq d 1 |
| 223 | d = 255 |
| 224 | else |
| 225 | if eq d 255 |
| 226 | d = 1 |
| 227 | end |
| 228 | end |
| 229 | end |
| 230 | put dst n uchar d |
| 231 | n = add n 1 |
| 232 | end |
| 233 | put n_out 0 n |
| 234 | end |
| 235 | |
| 236 | fn oma_add_i64_ok a long b long to ulong |
| 237 | if gt b 0 |
| 238 | if gt a sub 9223372036854775807 b |
| 239 | ret 0 |
| 240 | end |
| 241 | end |
| 242 | if lt b 0 |
| 243 | minv = sub long -9223372036854775807 1 |
| 244 | if lt a sub minv b |
| 245 | ret 0 |
| 246 | end |
| 247 | end |
| 248 | ret 1 |
| 249 | end |
| 250 | |
| 251 | export fn oma_om_bt_to_int digits view uchar * n ulong dst edit ulong 1 to ulong |
| 252 | for i ulong n |
| 253 | b = ulong get digits i |
| 254 | if and ne b 0 and ne b 1 ne b 255 |
| 255 | ret 1 |
| 256 | end |
| 257 | end |
| 258 | acc = long 0 |
| 259 | i = n |
| 260 | while gt i 0 |
| 261 | i = sub i 1 |
| 262 | b = ulong get digits i |
| 263 | d = long 0 |
| 264 | if eq b 1 |
| 265 | d = 1 |
| 266 | else |
| 267 | if eq b 255 |
| 268 | d = -1 |
| 269 | end |
| 270 | end |
| 271 | if eq oma_add_i64_ok acc d 0 |
| 272 | ret 4 |
| 273 | end |
| 274 | t = add acc d |
| 275 | if eq oma_add_i64_ok t acc 0 |
| 276 | ret 4 |
| 277 | end |
| 278 | t = add t acc |
| 279 | if eq oma_add_i64_ok t acc 0 |
| 280 | ret 4 |
| 281 | end |
| 282 | acc = add t acc |
| 283 | end |
| 284 | put dst 0 bitcast ulong acc |
| 285 | ret 0 |
| 286 | end |