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_lib.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 Omega mixed algebra library (oma_trit, oma_z3, oma_pack, oma_quant). |
| 3 | om2 |
| 4 | |
| 5 | const OMA_OK int 0 |
| 6 | const OMA_E_INVALID_TRIT int -1 |
| 7 | const OMA_E_INVALID_CODE int -2 |
| 8 | const OMA_E_INVALID_PLANES int -3 |
| 9 | const OMA_E_OVERFLOW int -4 |
| 10 | const OMA_E_INVALID_BYTE int -5 |
| 11 | const OMA_E_INVALID_Z3 int -6 |
| 12 | const OMA_E_ARG int -7 |
| 13 | const OMA_E_UNDERFLOW int -8 |
| 14 | const OMA_E_NATIVE int -9 |
| 15 | |
| 16 | const OMA_CODE_ZERO uchar 0 |
| 17 | const OMA_CODE_POS uchar 1 |
| 18 | const OMA_CODE_NEG uchar 2 |
| 19 | const OMA_CODE_INVALID uchar 3 |
| 20 | |
| 21 | const OMA_DOT_I8_MAX_N ulong 16777215 |
| 22 | const OMA_INT64_MAX_DIGITS ulong 41 |
| 23 | const OMA_DENSE_TRITS_PER_BYTE ulong 5 |
| 24 | const OMA_DENSE_BYTE_LIMIT int 243 |
| 25 | const OMA_BLOCK_BYTES ulong 16 |
| 26 | |
| 27 | text oma_nl "\n" |
| 28 | text oma_minus "-" |
| 29 | |
| 30 | fn oma_w_all fd int buf view uchar * n ulong to void |
| 31 | off = ulong 0 |
| 32 | while lt off n bound 1000000 |
| 33 | k = write fd buf off sub n off |
| 34 | if le k 0 |
| 35 | break |
| 36 | end |
| 37 | off = add off ulong k |
| 38 | end |
| 39 | end |
| 40 | |
| 41 | fn oma_put_str s view uchar * to void |
| 42 | oma_w_all 1 s len s |
| 43 | end |
| 44 | |
| 45 | fn oma_put_err s view uchar * to void |
| 46 | oma_w_all 2 s len s |
| 47 | end |
| 48 | |
| 49 | fn oma_put_u64 fd int v ulong to void |
| 50 | buf = new uchar 24 0 |
| 51 | if eq v 0 |
| 52 | put buf 0 uchar 48 |
| 53 | oma_w_all fd buf 1 |
| 54 | ret |
| 55 | end |
| 56 | tmp = new uchar 24 0 |
| 57 | cnt = ulong 0 |
| 58 | x = v |
| 59 | while gt x 0 bound 32 |
| 60 | d = rem x 10 |
| 61 | put tmp cnt uchar add 48 d |
| 62 | cnt = add cnt 1 |
| 63 | x = div x 10 |
| 64 | end |
| 65 | for i ulong cnt bound 32 |
| 66 | put buf i get tmp sub sub cnt 1 i |
| 67 | end |
| 68 | oma_w_all fd buf cnt |
| 69 | end |
| 70 | |
| 71 | fn oma_put_i64 fd int v long to void |
| 72 | if lt v 0 |
| 73 | oma_w_all fd oma_minus 1 |
| 74 | u = sub ulong 0 bitcast ulong v |
| 75 | oma_put_u64 fd u |
| 76 | else |
| 77 | oma_put_u64 fd ulong v |
| 78 | end |
| 79 | end |
| 80 | |
| 81 | note ---- bit helpers ---- |
| 82 | |
| 83 | fn oma_popcount64 x ulong to ulong |
| 84 | y = sub x and shr x 1 0x5555555555555555 |
| 85 | y = add and y 0x3333333333333333 and shr y 2 0x3333333333333333 |
| 86 | y = and add y shr y 4 0x0f0f0f0f0f0f0f0f |
| 87 | ret shr mul y 0x0101010101010101 56 |
| 88 | end |
| 89 | |
| 90 | fn oma_ctz64 x ulong to ulong |
| 91 | if eq x 0 |
| 92 | ret 64 |
| 93 | end |
| 94 | lo = and x sub ulong 0 x |
| 95 | ret oma_popcount64 sub lo 1 |
| 96 | end |
| 97 | |
| 98 | fn oma_add_i64_ok a long b long to ulong |
| 99 | if gt b 0 |
| 100 | if gt a sub 9223372036854775807 b |
| 101 | ret 0 |
| 102 | end |
| 103 | end |
| 104 | if lt b 0 |
| 105 | minv = sub long -9223372036854775807 1 |
| 106 | if lt a sub minv b |
| 107 | ret 0 |
| 108 | end |
| 109 | end |
| 110 | ret 1 |
| 111 | end |
| 112 | |
| 113 | fn oma_f32_isfinite x f32 to bool |
| 114 | bits = and bitcast uint x 0x7fffffff |
| 115 | ret lt bits 0x7f800000 |
| 116 | end |
| 117 | |
| 118 | fn oma_f64_isfinite x f64 to bool |
| 119 | bits = and bitcast ulong x 0x7fffffffffffffff |
| 120 | ret lt bits 0x7ff0000000000000 |
| 121 | end |
| 122 | |
| 123 | fn oma_f64_ldexp x f64 e int to f64 |
| 124 | r = x |
| 125 | k = e |
| 126 | while gt k 0 bound 2048 |
| 127 | r = mul r 2.0f64 |
| 128 | k = sub k 1 |
| 129 | end |
| 130 | while lt k 0 bound 2048 |
| 131 | r = mul r 0.5f64 |
| 132 | k = add k 1 |
| 133 | end |
| 134 | ret r |
| 135 | end |
| 136 | |
| 137 | note ---- scalar trits and codes ---- |
| 138 | |
| 139 | fn oma_trit_make v int dst edit byte 1 to int |
| 140 | if or lt v -1 gt v 1 |
| 141 | ret OMA_E_INVALID_TRIT |
| 142 | end |
| 143 | put dst 0 byte v |
| 144 | ret OMA_OK |
| 145 | end |
| 146 | |
| 147 | fn oma_trit_to_code t int dst edit uchar 1 to int |
| 148 | if eq t 0 |
| 149 | put dst 0 OMA_CODE_ZERO |
| 150 | ret OMA_OK |
| 151 | end |
| 152 | if eq t 1 |
| 153 | put dst 0 OMA_CODE_POS |
| 154 | ret OMA_OK |
| 155 | end |
| 156 | if eq t -1 |
| 157 | put dst 0 OMA_CODE_NEG |
| 158 | ret OMA_OK |
| 159 | end |
| 160 | ret OMA_E_INVALID_TRIT |
| 161 | end |
| 162 | |
| 163 | fn oma_code_to_trit code int dst edit byte 1 to int |
| 164 | if or lt code 0 gt code 2 |
| 165 | ret OMA_E_INVALID_CODE |
| 166 | end |
| 167 | p = and code 1 |
| 168 | n = and shr code 1 1 |
| 169 | put dst 0 byte sub p n |
| 170 | ret OMA_OK |
| 171 | end |
| 172 | |
| 173 | fn oma_code_neg a int dst edit uchar 1 to int |
| 174 | if or lt a 0 gt a 2 |
| 175 | ret OMA_E_INVALID_CODE |
| 176 | end |
| 177 | p = and a 1 |
| 178 | n = and shr a 1 1 |
| 179 | put dst 0 uchar or shl p 1 n |
| 180 | ret OMA_OK |
| 181 | end |
| 182 | |
| 183 | fn oma_code_add a int b int dst edit uchar 2 to int |
| 184 | if or or lt a 0 gt a 2 or lt b 0 gt b 2 |
| 185 | ret OMA_E_INVALID_CODE |
| 186 | end |
| 187 | ap = ulong and a 1 |
| 188 | an = ulong and shr a 1 1 |
| 189 | bp = ulong and b 1 |
| 190 | bn = ulong and shr b 1 1 |
| 191 | az = xor ulong 1 or ap an |
| 192 | bz = xor ulong 1 or bp bn |
| 193 | sp = or or and ap bz and az bp and an bn |
| 194 | sn = or or and an bz and az bn and ap bp |
| 195 | cp = and ap bp |
| 196 | cn = and an bn |
| 197 | put dst 0 uchar or sp shl sn 1 |
| 198 | put dst 1 uchar or cp shl cn 1 |
| 199 | ret OMA_OK |
| 200 | end |
| 201 | |
| 202 | fn oma_code_mul a int b int dst edit uchar 1 to int |
| 203 | if or or lt a 0 gt a 2 or lt b 0 gt b 2 |
| 204 | ret OMA_E_INVALID_CODE |
| 205 | end |
| 206 | ap = ulong and a 1 |
| 207 | an = ulong and shr a 1 1 |
| 208 | bp = ulong and b 1 |
| 209 | bn = ulong and shr b 1 1 |
| 210 | pp = or and ap bp and an bn |
| 211 | pn = or and ap bn and an bp |
| 212 | put dst 0 uchar or pp shl pn 1 |
| 213 | ret OMA_OK |
| 214 | end |
| 215 | |
| 216 | fn oma_trit_neg a int dst edit byte 1 to int |
| 217 | ca = new uchar 1 0 |
| 218 | r = new uchar 1 0 |
| 219 | rc = oma_trit_to_code a ca |
| 220 | if ne rc 0 |
| 221 | ret rc |
| 222 | end |
| 223 | rc = oma_code_neg int get ca 0 r |
| 224 | if ne rc 0 |
| 225 | ret rc |
| 226 | end |
| 227 | ret oma_code_to_trit int get r 0 dst |
| 228 | end |
| 229 | |
| 230 | fn oma_trit_add a int b int dst edit byte 2 to int |
| 231 | ca = new uchar 1 0 |
| 232 | cb = new uchar 1 0 |
| 233 | sc = new uchar 2 0 |
| 234 | t0 = new byte 1 0 |
| 235 | t1 = new byte 1 0 |
| 236 | rc = oma_trit_to_code a ca |
| 237 | if ne rc 0 |
| 238 | ret rc |
| 239 | end |
| 240 | rc = oma_trit_to_code b cb |
| 241 | if ne rc 0 |
| 242 | ret rc |
| 243 | end |
| 244 | rc = oma_code_add int get ca 0 int get cb 0 sc |
| 245 | if ne rc 0 |
| 246 | ret rc |
| 247 | end |
| 248 | rc = oma_code_to_trit int get sc 0 t0 |
| 249 | if ne rc 0 |
| 250 | ret rc |
| 251 | end |
| 252 | rc = oma_code_to_trit int get sc 1 t1 |
| 253 | if ne rc 0 |
| 254 | ret rc |
| 255 | end |
| 256 | put dst 0 get t0 0 |
| 257 | put dst 1 get t1 0 |
| 258 | ret OMA_OK |
| 259 | end |
| 260 | |
| 261 | fn oma_trit_mul a int b int dst edit byte 1 to int |
| 262 | ca = new uchar 1 0 |
| 263 | cb = new uchar 1 0 |
| 264 | r = new uchar 1 0 |
| 265 | rc = oma_trit_to_code a ca |
| 266 | if ne rc 0 |
| 267 | ret rc |
| 268 | end |
| 269 | rc = oma_trit_to_code b cb |
| 270 | if ne rc 0 |
| 271 | ret rc |
| 272 | end |
| 273 | rc = oma_code_mul int get ca 0 int get cb 0 r |
| 274 | if ne rc 0 |
| 275 | ret rc |
| 276 | end |
| 277 | ret oma_code_to_trit int get r 0 dst |
| 278 | end |
| 279 | |
| 280 | note ---- 64-trit blocks (represented as ulong 2: [0]=pos, [1]=neg) ---- |
| 281 | |
| 282 | fn oma_block_validate b view ulong 2 to int |
| 283 | if ne and get b 0 get b 1 0 |
| 284 | ret OMA_E_INVALID_PLANES |
| 285 | end |
| 286 | ret OMA_OK |
| 287 | end |
| 288 | |
| 289 | fn oma_block_encode src view byte 64 dst edit ulong 2 to int |
| 290 | p = ulong 0 |
| 291 | n = ulong 0 |
| 292 | for i ulong 64 |
| 293 | v = int get src i |
| 294 | if eq v 1 |
| 295 | p = or p shl ulong 1 i |
| 296 | else |
| 297 | if eq v -1 |
| 298 | n = or n shl ulong 1 i |
| 299 | else |
| 300 | if ne v 0 |
| 301 | ret OMA_E_INVALID_TRIT |
| 302 | end |
| 303 | end |
| 304 | end |
| 305 | end |
| 306 | put dst 0 p |
| 307 | put dst 1 n |
| 308 | ret OMA_OK |
| 309 | end |
| 310 | |
| 311 | fn oma_block_decode b view ulong 2 dst edit byte 64 to int |
| 312 | p = get b 0 |
| 313 | n = get b 1 |
| 314 | if ne and p n 0 |
| 315 | ret OMA_E_INVALID_PLANES |
| 316 | end |
| 317 | for i ulong 64 |
| 318 | pb = and shr p i 1 |
| 319 | nb = and shr n i 1 |
| 320 | if eq pb 1 |
| 321 | put dst i byte 1 |
| 322 | else |
| 323 | if eq nb 1 |
| 324 | put dst i byte -1 |
| 325 | else |
| 326 | put dst i byte 0 |
| 327 | end |
| 328 | end |
| 329 | end |
| 330 | ret OMA_OK |
| 331 | end |
| 332 | |
| 333 | fn oma_block_neg a view ulong 2 dst edit ulong 2 to int |
| 334 | p = get a 0 |
| 335 | n = get a 1 |
| 336 | if ne and p n 0 |
| 337 | ret OMA_E_INVALID_PLANES |
| 338 | end |
| 339 | put dst 0 n |
| 340 | put dst 1 p |
| 341 | ret OMA_OK |
| 342 | end |
| 343 | |
| 344 | fn oma_block_add a view ulong 2 b view ulong 2 sum edit ulong 2 carry edit ulong 2 to int |
| 345 | ap = get a 0 |
| 346 | an = get a 1 |
| 347 | bp = get b 0 |
| 348 | bn = get b 1 |
| 349 | if or ne and ap an 0 ne and bp bn 0 |
| 350 | ret OMA_E_INVALID_PLANES |
| 351 | end |
| 352 | all1 = ulong 0xffffffffffffffff |
| 353 | az = xor all1 or ap an |
| 354 | bz = xor all1 or bp bn |
| 355 | put sum 0 or or and ap bz and az bp and an bn |
| 356 | put sum 1 or or and an bz and az bn and ap bp |
| 357 | put carry 0 and ap bp |
| 358 | put carry 1 and an bn |
| 359 | ret OMA_OK |
| 360 | end |
| 361 | |
| 362 | fn oma_block_mul a view ulong 2 b view ulong 2 dst edit ulong 2 to int |
| 363 | ap = get a 0 |
| 364 | an = get a 1 |
| 365 | bp = get b 0 |
| 366 | bn = get b 1 |
| 367 | if or ne and ap an 0 ne and bp bn 0 |
| 368 | ret OMA_E_INVALID_PLANES |
| 369 | end |
| 370 | put dst 0 or and ap bp and an bn |
| 371 | put dst 1 or and ap bn and an bp |
| 372 | ret OMA_OK |
| 373 | end |
| 374 | |
| 375 | fn oma_block_dot a view ulong 2 b view ulong 2 dst edit int 1 to int |
| 376 | ap = get a 0 |
| 377 | an = get a 1 |
| 378 | bp = get b 0 |
| 379 | bn = get b 1 |
| 380 | if or ne and ap an 0 ne and bp bn 0 |
| 381 | ret OMA_E_INVALID_PLANES |
| 382 | end |
| 383 | pp = or and ap bp and an bn |
| 384 | pn = or and ap bn and an bp |
| 385 | diff = sub long oma_popcount64 pp long oma_popcount64 pn |
| 386 | put dst 0 int diff |
| 387 | ret OMA_OK |
| 388 | end |
| 389 | |
| 390 | fn oma_bitplane_blocks n ulong to ulong |
| 391 | q = div n 64 |
| 392 | if ne rem n 64 0 |
| 393 | ret add q 1 |
| 394 | end |
| 395 | ret q |
| 396 | end |
| 397 | |
| 398 | fn oma_dense_bytes n ulong to ulong |
| 399 | q = div n 5 |
| 400 | if ne rem n 5 0 |
| 401 | ret add q 1 |
| 402 | end |
| 403 | ret q |
| 404 | end |
| 405 | |
| 406 | fn oma_has_stray p ulong ng ulong lanes ulong to bool |
| 407 | if ge lanes 64 |
| 408 | ret false |
| 409 | end |
| 410 | live = sub shl ulong 1 lanes 1 |
| 411 | dead = xor ulong 0xffffffffffffffff live |
| 412 | ret ne and or p ng dead 0 |
| 413 | end |
| 414 | |
| 415 | fn oma_dot_block_i8 p ulong ng ulong x view byte * base ulong to int |
| 416 | acc = int 0 |
| 417 | mp = p |
| 418 | while ne mp 0 bound 65 |
| 419 | bit = oma_ctz64 mp |
| 420 | acc = add acc int get x add base bit |
| 421 | mp = and mp sub mp 1 |
| 422 | end |
| 423 | mn = ng |
| 424 | while ne mn 0 bound 65 |
| 425 | bit = oma_ctz64 mn |
| 426 | acc = sub acc int get x add base bit |
| 427 | mn = and mn sub mn 1 |
| 428 | end |
| 429 | ret acc |
| 430 | end |
| 431 | |
| 432 | fn oma_dot_tw_i8 w view ulong * x view byte * n ulong dst edit int 1 to int |
| 433 | if gt n OMA_DOT_I8_MAX_N |
| 434 | ret OMA_E_OVERFLOW |
| 435 | end |
| 436 | nb = oma_bitplane_blocks n |
| 437 | if lt len w mul nb 2 |
| 438 | ret OMA_E_ARG |
| 439 | end |
| 440 | if lt len x n |
| 441 | ret OMA_E_ARG |
| 442 | end |
| 443 | acc = int 0 |
| 444 | for k ulong nb bound 300000 |
| 445 | p = get w mul k 2 |
| 446 | ng = get w add mul k 2 1 |
| 447 | if ne and p ng 0 |
| 448 | ret OMA_E_INVALID_PLANES |
| 449 | end |
| 450 | if oma_has_stray p ng sub n mul k 64 |
| 451 | ret OMA_E_ARG |
| 452 | end |
| 453 | acc = add acc oma_dot_block_i8 p ng x mul k 64 |
| 454 | end |
| 455 | put dst 0 acc |
| 456 | ret OMA_OK |
| 457 | end |
| 458 | |
| 459 | note ---- integer <-> balanced ternary ---- |
| 460 | |
| 461 | fn oma_int_to_bt v long dst edit byte * cap ulong ndigits edit ulong 1 to int |
| 462 | tmp = new byte 41 0 |
| 463 | v_bits = bitcast ulong v |
| 464 | is_neg = ulong 0 |
| 465 | u = v_bits |
| 466 | if ge v_bits 0x8000000000000000 |
| 467 | is_neg = 1 |
| 468 | u = sub ulong 0 v_bits |
| 469 | end |
| 470 | n = ulong 0 |
| 471 | while gt u 0 bound 64 |
| 472 | r = rem u 3 |
| 473 | u = div u 3 |
| 474 | d = int r |
| 475 | if eq r 2 |
| 476 | d = -1 |
| 477 | u = add u 1 |
| 478 | end |
| 479 | if eq is_neg 1 |
| 480 | d = sub 0 d |
| 481 | end |
| 482 | put tmp n byte d |
| 483 | n = add n 1 |
| 484 | end |
| 485 | if gt n cap |
| 486 | ret OMA_E_OVERFLOW |
| 487 | end |
| 488 | if lt len dst n |
| 489 | ret OMA_E_OVERFLOW |
| 490 | end |
| 491 | for i ulong n bound 64 |
| 492 | put dst i get tmp i |
| 493 | end |
| 494 | put ndigits 0 n |
| 495 | ret OMA_OK |
| 496 | end |
| 497 | |
| 498 | fn oma_int_to_bt_fixed v long dst edit byte * n ulong to int |
| 499 | nd = new ulong 1 0 |
| 500 | rc = oma_int_to_bt v dst n nd |
| 501 | if ne rc 0 |
| 502 | ret rc |
| 503 | end |
| 504 | used = get nd 0 |
| 505 | i = used |
| 506 | while lt i n bound 64 |
| 507 | put dst i byte 0 |
| 508 | i = add i 1 |
| 509 | end |
| 510 | ret OMA_OK |
| 511 | end |
| 512 | |
| 513 | fn oma_bt_to_int digits view byte * n ulong dst edit long 1 to int |
| 514 | if lt len digits n |
| 515 | ret OMA_E_ARG |
| 516 | end |
| 517 | for i ulong n bound 1024 |
| 518 | v = int get digits i |
| 519 | if or lt v -1 gt v 1 |
| 520 | ret OMA_E_INVALID_TRIT |
| 521 | end |
| 522 | end |
| 523 | acc = long 0 |
| 524 | i = n |
| 525 | while gt i 0 bound 1024 |
| 526 | i = sub i 1 |
| 527 | d = long get digits i |
| 528 | if eq oma_add_i64_ok acc d 0 |
| 529 | ret OMA_E_OVERFLOW |
| 530 | end |
| 531 | t = add acc d |
| 532 | if eq oma_add_i64_ok t acc 0 |
| 533 | ret OMA_E_OVERFLOW |
| 534 | end |
| 535 | t = add t acc |
| 536 | if eq oma_add_i64_ok t acc 0 |
| 537 | ret OMA_E_OVERFLOW |
| 538 | end |
| 539 | acc = add t acc |
| 540 | end |
| 541 | put dst 0 acc |
| 542 | ret OMA_OK |
| 543 | end |
| 544 | |
| 545 | note ---- Z3 ---- |
| 546 | |
| 547 | fn oma_z3_make v int dst edit uchar 1 to int |
| 548 | if or lt v 0 gt v 2 |
| 549 | ret OMA_E_INVALID_Z3 |
| 550 | end |
| 551 | put dst 0 uchar v |
| 552 | ret OMA_OK |
| 553 | end |
| 554 | |
| 555 | fn oma_z3_add a int b int dst edit uchar 1 to int |
| 556 | if or or lt a 0 gt a 2 or lt b 0 gt b 2 |
| 557 | ret OMA_E_INVALID_Z3 |
| 558 | end |
| 559 | put dst 0 uchar rem add a b 3 |
| 560 | ret OMA_OK |
| 561 | end |
| 562 | |
| 563 | fn oma_z3_mul a int b int dst edit uchar 1 to int |
| 564 | if or or lt a 0 gt a 2 or lt b 0 gt b 2 |
| 565 | ret OMA_E_INVALID_Z3 |
| 566 | end |
| 567 | put dst 0 uchar rem mul a b 3 |
| 568 | ret OMA_OK |
| 569 | end |
| 570 | |
| 571 | fn oma_z3_neg a int dst edit uchar 1 to int |
| 572 | if or lt a 0 gt a 2 |
| 573 | ret OMA_E_INVALID_Z3 |
| 574 | end |
| 575 | put dst 0 uchar rem sub 3 a 3 |
| 576 | ret OMA_OK |
| 577 | end |
| 578 | |
| 579 | fn oma_z3_to_trit a int dst edit byte 1 to int |
| 580 | if or lt a 0 gt a 2 |
| 581 | ret OMA_E_INVALID_Z3 |
| 582 | end |
| 583 | if eq a 2 |
| 584 | put dst 0 byte -1 |
| 585 | else |
| 586 | put dst 0 byte a |
| 587 | end |
| 588 | ret OMA_OK |
| 589 | end |
| 590 | |
| 591 | fn oma_trit_to_z3 t int dst edit uchar 1 to int |
| 592 | if or lt t -1 gt t 1 |
| 593 | ret OMA_E_INVALID_TRIT |
| 594 | end |
| 595 | if lt t 0 |
| 596 | put dst 0 uchar 2 |
| 597 | else |
| 598 | put dst 0 uchar t |
| 599 | end |
| 600 | ret OMA_OK |
| 601 | end |
| 602 | |
| 603 | fn oma_z3_block_validate b view ulong 2 to int |
| 604 | if ne and get b 0 get b 1 0 |
| 605 | ret OMA_E_INVALID_PLANES |
| 606 | end |
| 607 | ret OMA_OK |
| 608 | end |
| 609 | |
| 610 | fn oma_z3_block_encode src view uchar 64 dst edit ulong 2 to int |
| 611 | o = ulong 0 |
| 612 | t = ulong 0 |
| 613 | for i ulong 64 |
| 614 | v = ulong get src i |
| 615 | if eq v 1 |
| 616 | o = or o shl ulong 1 i |
| 617 | else |
| 618 | if eq v 2 |
| 619 | t = or t shl ulong 1 i |
| 620 | else |
| 621 | if ne v 0 |
| 622 | ret OMA_E_INVALID_Z3 |
| 623 | end |
| 624 | end |
| 625 | end |
| 626 | end |
| 627 | put dst 0 o |
| 628 | put dst 1 t |
| 629 | ret OMA_OK |
| 630 | end |
| 631 | |
| 632 | fn oma_z3_block_decode b view ulong 2 dst edit uchar 64 to int |
| 633 | o = get b 0 |
| 634 | t = get b 1 |
| 635 | if ne and o t 0 |
| 636 | ret OMA_E_INVALID_PLANES |
| 637 | end |
| 638 | for i ulong 64 |
| 639 | b0 = and shr o i 1 |
| 640 | b1 = and shr t i 1 |
| 641 | put dst i uchar or b0 shl b1 1 |
| 642 | end |
| 643 | ret OMA_OK |
| 644 | end |
| 645 | |
| 646 | fn oma_z3_block_from_trits t view ulong 2 dst edit ulong 2 to int |
| 647 | rc = oma_block_validate t |
| 648 | if ne rc 0 |
| 649 | ret rc |
| 650 | end |
| 651 | put dst 0 get t 0 |
| 652 | put dst 1 get t 1 |
| 653 | ret OMA_OK |
| 654 | end |
| 655 | |
| 656 | fn oma_z3_block_to_trits z view ulong 2 dst edit ulong 2 to int |
| 657 | rc = oma_z3_block_validate z |
| 658 | if ne rc 0 |
| 659 | ret rc |
| 660 | end |
| 661 | put dst 0 get z 0 |
| 662 | put dst 1 get z 1 |
| 663 | ret OMA_OK |
| 664 | end |
| 665 | |
| 666 | fn oma_z3_block_add a view ulong 2 b view ulong 2 dst edit ulong 2 to int |
| 667 | cy = new ulong 2 0 |
| 668 | ret oma_block_add a b dst cy |
| 669 | end |
| 670 | |
| 671 | fn oma_z3_block_mul a view ulong 2 b view ulong 2 dst edit ulong 2 to int |
| 672 | ret oma_block_mul a b dst |
| 673 | end |
| 674 | |
| 675 | fn oma_z3_block_neg a view ulong 2 dst edit ulong 2 to int |
| 676 | ret oma_block_neg a dst |
| 677 | end |
| 678 | |
| 679 | note ---- packing and serialization ---- |
| 680 | |
| 681 | fn oma_block_serialize b view ulong 2 dst edit uchar 16 to int |
| 682 | p = get b 0 |
| 683 | n = get b 1 |
| 684 | if ne and p n 0 |
| 685 | ret OMA_E_INVALID_PLANES |
| 686 | end |
| 687 | for i ulong 8 |
| 688 | put dst i uchar and shr p mul i 8 255 |
| 689 | put dst add 8 i uchar and shr n mul i 8 255 |
| 690 | end |
| 691 | ret OMA_OK |
| 692 | end |
| 693 | |
| 694 | fn oma_block_deserialize src view uchar 16 dst edit ulong 2 to int |
| 695 | p = ulong 0 |
| 696 | n = ulong 0 |
| 697 | for i ulong 8 |
| 698 | bp = ulong get src i |
| 699 | bn = ulong get src add 8 i |
| 700 | p = or p shl bp mul i 8 |
| 701 | n = or n shl bn mul i 8 |
| 702 | end |
| 703 | if ne and p n 0 |
| 704 | ret OMA_E_INVALID_PLANES |
| 705 | end |
| 706 | put dst 0 p |
| 707 | put dst 1 n |
| 708 | ret OMA_OK |
| 709 | end |
| 710 | |
| 711 | fn oma_pack_bp_one t view byte * base ulong m ulong dst edit ulong 2 to void |
| 712 | p = ulong 0 |
| 713 | ng = ulong 0 |
| 714 | for i ulong m bound 64 |
| 715 | v = int get t add base i |
| 716 | if eq v 1 |
| 717 | p = or p shl ulong 1 i |
| 718 | else |
| 719 | if eq v -1 |
| 720 | ng = or ng shl ulong 1 i |
| 721 | end |
| 722 | end |
| 723 | end |
| 724 | put dst 0 p |
| 725 | put dst 1 ng |
| 726 | end |
| 727 | |
| 728 | fn oma_pack_bitplane t view byte * n ulong dst edit ulong * out_blocks ulong to int |
| 729 | nb = oma_bitplane_blocks n |
| 730 | if lt out_blocks nb |
| 731 | ret OMA_E_OVERFLOW |
| 732 | end |
| 733 | if lt len t n |
| 734 | ret OMA_E_ARG |
| 735 | end |
| 736 | if lt len dst mul nb 2 |
| 737 | ret OMA_E_OVERFLOW |
| 738 | end |
| 739 | for i ulong n bound 2000000 |
| 740 | v = int get t i |
| 741 | if or lt v -1 gt v 1 |
| 742 | ret OMA_E_INVALID_TRIT |
| 743 | end |
| 744 | end |
| 745 | tmp = new ulong 2 0 |
| 746 | for k ulong nb bound 100000 |
| 747 | base = mul k 64 |
| 748 | m = sub n base |
| 749 | if gt m 64 |
| 750 | m = 64 |
| 751 | end |
| 752 | oma_pack_bp_one t base m tmp |
| 753 | put dst mul k 2 get tmp 0 |
| 754 | put dst add mul k 2 1 get tmp 1 |
| 755 | end |
| 756 | ret OMA_OK |
| 757 | end |
| 758 | |
| 759 | fn oma_unpack_bp_one p ulong ng ulong dst edit byte * base ulong m ulong to void |
| 760 | for i ulong m bound 64 |
| 761 | pb = and shr p i 1 |
| 762 | nb_bit = and shr ng i 1 |
| 763 | if eq pb 1 |
| 764 | put dst add base i byte 1 |
| 765 | else |
| 766 | if eq nb_bit 1 |
| 767 | put dst add base i byte -1 |
| 768 | else |
| 769 | put dst add base i byte 0 |
| 770 | end |
| 771 | end |
| 772 | end |
| 773 | end |
| 774 | |
| 775 | fn oma_unpack_bitplane src view ulong * in_blocks ulong n ulong dst edit byte * to int |
| 776 | nb = oma_bitplane_blocks n |
| 777 | if lt in_blocks nb |
| 778 | ret OMA_E_ARG |
| 779 | end |
| 780 | if lt len src mul nb 2 |
| 781 | ret OMA_E_ARG |
| 782 | end |
| 783 | if lt len dst n |
| 784 | ret OMA_E_ARG |
| 785 | end |
| 786 | for k ulong nb bound 100000 |
| 787 | p = get src mul k 2 |
| 788 | ng = get src add mul k 2 1 |
| 789 | if ne and p ng 0 |
| 790 | ret OMA_E_INVALID_PLANES |
| 791 | end |
| 792 | if oma_has_stray p ng sub n mul k 64 |
| 793 | ret OMA_E_ARG |
| 794 | end |
| 795 | end |
| 796 | for k ulong nb bound 100000 |
| 797 | p = get src mul k 2 |
| 798 | ng = get src add mul k 2 1 |
| 799 | base = mul k 64 |
| 800 | m = sub n base |
| 801 | if gt m 64 |
| 802 | m = 64 |
| 803 | end |
| 804 | oma_unpack_bp_one p ng dst base m |
| 805 | end |
| 806 | ret OMA_OK |
| 807 | end |
| 808 | |
| 809 | fn oma_dense_byte_decode bv int dst edit byte 5 to int |
| 810 | if or lt bv 0 ge bv OMA_DENSE_BYTE_LIMIT |
| 811 | ret OMA_E_INVALID_BYTE |
| 812 | end |
| 813 | v = ulong bv |
| 814 | for j ulong 5 |
| 815 | r = rem v 3 |
| 816 | v = div v 3 |
| 817 | put dst j byte sub int r 1 |
| 818 | end |
| 819 | ret OMA_OK |
| 820 | end |
| 821 | |
| 822 | fn oma_pack_dense_one t view byte * base ulong n ulong to uchar |
| 823 | bv = ulong 0 |
| 824 | pw = ulong 1 |
| 825 | for j ulong 5 |
| 826 | idx = add base j |
| 827 | d = ulong 1 |
| 828 | if lt idx n |
| 829 | d = ulong add int get t idx 1 |
| 830 | end |
| 831 | bv = add bv mul d pw |
| 832 | pw = mul pw 3 |
| 833 | end |
| 834 | ret uchar bv |
| 835 | end |
| 836 | |
| 837 | fn oma_pack_dense t view byte * n ulong dst edit uchar * out_len edit ulong 1 to int |
| 838 | nbytes = oma_dense_bytes n |
| 839 | if lt len t n |
| 840 | ret OMA_E_ARG |
| 841 | end |
| 842 | if lt len dst nbytes |
| 843 | ret OMA_E_OVERFLOW |
| 844 | end |
| 845 | for i ulong n bound 2000000 |
| 846 | v = int get t i |
| 847 | if or lt v -1 gt v 1 |
| 848 | ret OMA_E_INVALID_TRIT |
| 849 | end |
| 850 | end |
| 851 | for k ulong nbytes bound 400000 |
| 852 | put dst k oma_pack_dense_one t mul k 5 n |
| 853 | end |
| 854 | put out_len 0 nbytes |
| 855 | ret OMA_OK |
| 856 | end |
| 857 | |
| 858 | fn oma_unpack_dense src view uchar * in_len ulong n ulong dst edit byte * to int |
| 859 | nbytes = oma_dense_bytes n |
| 860 | if lt in_len nbytes |
| 861 | ret OMA_E_ARG |
| 862 | end |
| 863 | if lt len src nbytes |
| 864 | ret OMA_E_ARG |
| 865 | end |
| 866 | if lt len dst n |
| 867 | ret OMA_E_ARG |
| 868 | end |
| 869 | d5 = new byte 5 0 |
| 870 | for k ulong nbytes bound 400000 |
| 871 | rc = oma_dense_byte_decode int get src k d5 |
| 872 | if ne rc 0 |
| 873 | ret rc |
| 874 | end |
| 875 | base = mul k 5 |
| 876 | for j ulong 5 |
| 877 | if ge add base j n |
| 878 | if ne int get d5 j 0 |
| 879 | ret OMA_E_INVALID_BYTE |
| 880 | end |
| 881 | end |
| 882 | end |
| 883 | end |
| 884 | for k ulong nbytes bound 400000 |
| 885 | rc = oma_dense_byte_decode int get src k d5 |
| 886 | if ne rc 0 |
| 887 | ret rc |
| 888 | end |
| 889 | base = mul k 5 |
| 890 | for j ulong 5 |
| 891 | idx = add base j |
| 892 | if lt idx n |
| 893 | put dst idx get d5 j |
| 894 | end |
| 895 | end |
| 896 | end |
| 897 | ret OMA_OK |
| 898 | end |
| 899 | |
| 900 | note ---- absmean quantization ---- |
| 901 | |
| 902 | fn oma_quant_absmean w view f32 * n ulong q edit byte * scale edit f32 1 to int |
| 903 | if lt len w n |
| 904 | ret OMA_E_ARG |
| 905 | end |
| 906 | if lt len q n |
| 907 | ret OMA_E_ARG |
| 908 | end |
| 909 | sum = 0.0f64 |
| 910 | for i ulong n bound 2000000 |
| 911 | wi = get w i |
| 912 | if not oma_f32_isfinite wi |
| 913 | ret OMA_E_ARG |
| 914 | end |
| 915 | sum = add sum fabs f64 wi |
| 916 | end |
| 917 | if or eq n 0 eq sum 0.0f64 |
| 918 | for i ulong n bound 2000000 |
| 919 | put q i byte 0 |
| 920 | end |
| 921 | put scale 0 0.0f32 |
| 922 | ret OMA_OK |
| 923 | end |
| 924 | mean = div sum f64 n |
| 925 | flt_min = bitcast f32 uint 0x00800000 |
| 926 | if lt mean f64 flt_min |
| 927 | ret OMA_E_UNDERFLOW |
| 928 | end |
| 929 | s = f32 mean |
| 930 | sd = f64 s |
| 931 | for i ulong n bound 2000000 |
| 932 | r = div f64 get w i sd |
| 933 | if ge r 0.5f64 |
| 934 | put q i byte 1 |
| 935 | else |
| 936 | if le r -0.5f64 |
| 937 | put q i byte -1 |
| 938 | else |
| 939 | put q i byte 0 |
| 940 | end |
| 941 | end |
| 942 | end |
| 943 | put scale 0 s |
| 944 | ret OMA_OK |
| 945 | end |
| 946 | |
| 947 | fn oma_quant_rel_l2_check w view f32 * q view byte * to int |
| 948 | n = len w |
| 949 | if lt len q n |
| 950 | ret OMA_E_ARG |
| 951 | end |
| 952 | for i ulong n bound 2000000 |
| 953 | if not oma_f32_isfinite get w i |
| 954 | ret OMA_E_ARG |
| 955 | end |
| 956 | qi = int get q i |
| 957 | if lt qi -1 |
| 958 | ret OMA_E_INVALID_TRIT |
| 959 | end |
| 960 | if gt qi 1 |
| 961 | ret OMA_E_INVALID_TRIT |
| 962 | end |
| 963 | end |
| 964 | ret OMA_OK |
| 965 | end |
| 966 | |
| 967 | fn oma_quant_rel_l2_num w view f32 * q view byte * scale f32 to f64 |
| 968 | num = 0.0f64 |
| 969 | sd = f64 scale |
| 970 | for i ulong len w bound 2000000 |
| 971 | d = sub f64 get w i mul f64 int get q i sd |
| 972 | num = add num mul d d |
| 973 | end |
| 974 | ret num |
| 975 | end |
| 976 | |
| 977 | fn oma_quant_rel_l2_den w view f32 * to f64 |
| 978 | den = 0.0f64 |
| 979 | for i ulong len w bound 2000000 |
| 980 | wd = f64 get w i |
| 981 | den = add den mul wd wd |
| 982 | end |
| 983 | ret den |
| 984 | end |
| 985 | |
| 986 | fn oma_quant_rel_l2 w view f32 * q view byte * scale f32 err edit f64 1 to int |
| 987 | if not oma_f32_isfinite scale |
| 988 | ret OMA_E_ARG |
| 989 | end |
| 990 | if lt scale 0.0f32 |
| 991 | ret OMA_E_ARG |
| 992 | end |
| 993 | if ne oma_quant_rel_l2_check w q 0 |
| 994 | ret oma_quant_rel_l2_check w q |
| 995 | end |
| 996 | num = oma_quant_rel_l2_num w q scale |
| 997 | den = oma_quant_rel_l2_den w |
| 998 | if eq den 0.0f64 |
| 999 | if ne num 0.0f64 |
| 1000 | ret OMA_E_ARG |
| 1001 | end |
| 1002 | put err 0 0.0f64 |
| 1003 | ret OMA_OK |
| 1004 | end |
| 1005 | put err 0 fsqrt div num den |
| 1006 | ret OMA_OK |
| 1007 | end |