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 / realize.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 MA-2 realizations (all 10 exact Omega-X realizations) + MA-2 selector. |
| 3 | note Concatenated after src/algebra/oma_lib.om. |
| 4 | |
| 5 | const OMA_RZ_MAX_N ulong 16777215 |
| 6 | const OMA_RZ_COUNT ulong 10 |
| 7 | const OMA_RZ_OK int 0 |
| 8 | const OMA_RZ_E_ARG int -1 |
| 9 | const OMA_RZ_E_TRIT int -2 |
| 10 | const OMA_RZ_E_OVERFLOW int -4 |
| 11 | |
| 12 | const RZ_R1_PLAIN ulong 0 |
| 13 | const RZ_R1_SDOT ulong 1 |
| 14 | const RZ_R1_SDOT_IL ulong 2 |
| 15 | const RZ_R1_SMMLA ulong 3 |
| 16 | const RZ_R2_BITPLANE ulong 4 |
| 17 | const RZ_R2B_LUT ulong 5 |
| 18 | const RZ_R2C_CRUMB ulong 6 |
| 19 | const RZ_R3_SPARSE ulong 7 |
| 20 | const RZ_R4_RNS ulong 8 |
| 21 | const RZ_R5_DENSE5 ulong 9 |
| 22 | |
| 23 | const RZ_SP_MAX_N ulong 65536 |
| 24 | const RZ_RNS_MAX_N ulong 64512 |
| 25 | const RZ_RNS_M int 16515840 |
| 26 | |
| 27 | global rz_w byte 262144 |
| 28 | global rz_x byte 131072 |
| 29 | global rz_yref int 1024 |
| 30 | global rz_y int 1024 |
| 31 | global rz_mem uchar 524288 |
| 32 | global rz_idx ushort 262144 |
| 33 | global rz_off uint 2048 |
| 34 | global rz_rns_x byte 262144 |
| 35 | global rz_plan ulong 8 |
| 36 | |
| 37 | fn oma_rz_max_n rid ulong to ulong |
| 38 | if eq rid RZ_R3_SPARSE |
| 39 | ret RZ_SP_MAX_N |
| 40 | end |
| 41 | if eq rid RZ_R4_RNS |
| 42 | ret RZ_RNS_MAX_N |
| 43 | end |
| 44 | ret OMA_RZ_MAX_N |
| 45 | end |
| 46 | |
| 47 | fn oma_rz_check_shape m ulong n ulong max_n ulong to int |
| 48 | if or eq m 0 eq n 0 |
| 49 | ret OMA_RZ_E_ARG |
| 50 | end |
| 51 | if or gt n max_n gt n OMA_RZ_MAX_N |
| 52 | ret OMA_RZ_E_ARG |
| 53 | end |
| 54 | ret OMA_RZ_OK |
| 55 | end |
| 56 | |
| 57 | fn oma_rz_validate_g m ulong n ulong to int |
| 58 | tot = mul m n |
| 59 | for i ulong tot bound 300000 |
| 60 | v = int get rz_w i |
| 61 | if or lt v -1 gt v 1 |
| 62 | ret OMA_RZ_E_TRIT |
| 63 | end |
| 64 | end |
| 65 | ret OMA_RZ_OK |
| 66 | end |
| 67 | |
| 68 | fn oma_rz_oracle_row base ulong n ulong to int |
| 69 | acc = long 0 |
| 70 | for j ulong n bound 200000 |
| 71 | wval = long get rz_w add base j |
| 72 | xval = long get rz_x j |
| 73 | acc = add acc mul wval xval |
| 74 | end |
| 75 | ret int acc |
| 76 | end |
| 77 | |
| 78 | fn oma_rz_oracle_g m ulong n ulong to int |
| 79 | rc = oma_rz_check_shape m n OMA_RZ_MAX_N |
| 80 | if ne rc 0 |
| 81 | ret rc |
| 82 | end |
| 83 | for i ulong m bound 1024 |
| 84 | put rz_yref i oma_rz_oracle_row mul i n n |
| 85 | end |
| 86 | ret OMA_RZ_OK |
| 87 | end |
| 88 | |
| 89 | note ---- R1 binary (plain, sdot, sdot_il, smmla) ---- |
| 90 | |
| 91 | fn rz_pack_r1_row base_s ulong base_d ulong n ulong np ulong to ulong |
| 92 | nnz = ulong 0 |
| 93 | for j ulong np bound 200000 |
| 94 | v = int 0 |
| 95 | if lt j n |
| 96 | v = int get rz_w add base_s j |
| 97 | end |
| 98 | if ne v 0 |
| 99 | nnz = add nnz 1 |
| 100 | end |
| 101 | put rz_mem add base_d j uchar and v 255 |
| 102 | end |
| 103 | ret nnz |
| 104 | end |
| 105 | |
| 106 | fn rz_pack_r1 rid ulong m ulong n ulong to int |
| 107 | np = mul div add n 15 16 16 |
| 108 | nnz = ulong 0 |
| 109 | for i ulong m bound 1024 |
| 110 | nnz = add nnz rz_pack_r1_row mul i n mul i np n np |
| 111 | end |
| 112 | wb = mul m np |
| 113 | put rz_plan 0 m |
| 114 | put rz_plan 1 n |
| 115 | put rz_plan 2 wb |
| 116 | put rz_plan 3 wb |
| 117 | put rz_plan 4 nnz |
| 118 | put rz_plan 5 1 |
| 119 | put rz_plan 6 rid |
| 120 | ret OMA_RZ_OK |
| 121 | end |
| 122 | |
| 123 | fn rz_run_r1_row base ulong n ulong to int |
| 124 | acc = int 0 |
| 125 | for j ulong n bound 200000 |
| 126 | wb = int get rz_mem add base j |
| 127 | if ge wb 128 |
| 128 | wb = sub wb 256 |
| 129 | end |
| 130 | acc = add acc mul wb int get rz_x j |
| 131 | end |
| 132 | ret acc |
| 133 | end |
| 134 | |
| 135 | fn rz_run_r1 to int |
| 136 | m = get rz_plan 0 |
| 137 | n = get rz_plan 1 |
| 138 | np = mul div add n 15 16 16 |
| 139 | for i ulong m bound 1024 |
| 140 | put rz_y i rz_run_r1_row mul i np n |
| 141 | end |
| 142 | ret OMA_RZ_OK |
| 143 | end |
| 144 | |
| 145 | note ---- R2_bitplane: chunks of 128 cols -> 32 bytes (16 pos + 16 neg) ---- |
| 146 | |
| 147 | fn rz_pack_bp_lane base_w ulong base_d ulong cbase ulong n ulong j ulong to ulong |
| 148 | nnz = ulong 0 |
| 149 | pb = ulong 0 |
| 150 | nb = ulong 0 |
| 151 | for k ulong 8 |
| 152 | col = add add cbase mul 16 k j |
| 153 | if lt col n |
| 154 | v = int get rz_w add base_w col |
| 155 | if eq v 1 |
| 156 | pb = or pb shl ulong 1 k |
| 157 | nnz = add nnz 1 |
| 158 | else |
| 159 | if eq v -1 |
| 160 | nb = or nb shl ulong 1 k |
| 161 | nnz = add nnz 1 |
| 162 | end |
| 163 | end |
| 164 | end |
| 165 | end |
| 166 | put rz_mem add base_d j uchar pb |
| 167 | put rz_mem add add base_d 16 j uchar nb |
| 168 | ret nnz |
| 169 | end |
| 170 | |
| 171 | fn rz_pack_bp_chunk i ulong c ulong n ulong rb ulong to ulong |
| 172 | nnz = ulong 0 |
| 173 | base_d = add mul i rb mul c 32 |
| 174 | base_w = mul i n |
| 175 | cbase = mul c 128 |
| 176 | for j ulong 16 |
| 177 | nnz = add nnz rz_pack_bp_lane base_w base_d cbase n j |
| 178 | end |
| 179 | ret nnz |
| 180 | end |
| 181 | |
| 182 | fn rz_pack_bitplane m ulong n ulong to int |
| 183 | chunks = div add n 127 128 |
| 184 | rb = mul chunks 32 |
| 185 | nnz = ulong 0 |
| 186 | for i ulong m bound 1024 |
| 187 | for c ulong chunks bound 2048 |
| 188 | nnz = add nnz rz_pack_bp_chunk i c n rb |
| 189 | end |
| 190 | end |
| 191 | wb = mul m rb |
| 192 | put rz_plan 0 m |
| 193 | put rz_plan 1 n |
| 194 | put rz_plan 2 wb |
| 195 | put rz_plan 3 wb |
| 196 | put rz_plan 4 nnz |
| 197 | put rz_plan 5 1 |
| 198 | put rz_plan 6 RZ_R2_BITPLANE |
| 199 | ret OMA_RZ_OK |
| 200 | end |
| 201 | |
| 202 | fn rz_run_bp_lane base_d ulong cbase ulong n ulong j ulong to int |
| 203 | acc = int 0 |
| 204 | pb = ulong get rz_mem add base_d j |
| 205 | nb = ulong get rz_mem add add base_d 16 j |
| 206 | for k ulong 8 |
| 207 | col = add add cbase mul 16 k j |
| 208 | if lt col n |
| 209 | xv = int get rz_x col |
| 210 | if ne and shr pb k 1 0 |
| 211 | acc = add acc xv |
| 212 | else |
| 213 | if ne and shr nb k 1 0 |
| 214 | acc = sub acc xv |
| 215 | end |
| 216 | end |
| 217 | end |
| 218 | end |
| 219 | ret acc |
| 220 | end |
| 221 | |
| 222 | fn rz_run_bp_row i ulong n ulong chunks ulong rb ulong to int |
| 223 | acc = int 0 |
| 224 | base_r = mul i rb |
| 225 | for c ulong chunks bound 2048 |
| 226 | base_d = add base_r mul c 32 |
| 227 | cbase = mul c 128 |
| 228 | for j ulong 16 |
| 229 | acc = add acc rz_run_bp_lane base_d cbase n j |
| 230 | end |
| 231 | end |
| 232 | ret acc |
| 233 | end |
| 234 | |
| 235 | fn rz_run_bitplane to int |
| 236 | m = get rz_plan 0 |
| 237 | n = get rz_plan 1 |
| 238 | chunks = div add n 127 128 |
| 239 | rb = mul chunks 32 |
| 240 | for i ulong m bound 1024 |
| 241 | put rz_y i rz_run_bp_row i n chunks rb |
| 242 | end |
| 243 | ret OMA_RZ_OK |
| 244 | end |
| 245 | |
| 246 | note ---- R2b_lut: 16-row blocks x 16-col super-groups, 64 bytes each ---- |
| 247 | |
| 248 | fn rz_pack_lut_byte row ulong col0 ulong m ulong n ulong dst_off ulong to ulong |
| 249 | nnz = ulong 0 |
| 250 | pos_n = ulong 0 |
| 251 | neg_n = ulong 0 |
| 252 | for q ulong 4 |
| 253 | col = add col0 q |
| 254 | if lt row m |
| 255 | if lt col n |
| 256 | v = int get rz_w add mul row n col |
| 257 | if eq v 1 |
| 258 | pos_n = or pos_n shl ulong 1 q |
| 259 | nnz = add nnz 1 |
| 260 | else |
| 261 | if eq v -1 |
| 262 | neg_n = or neg_n shl ulong 1 q |
| 263 | nnz = add nnz 1 |
| 264 | end |
| 265 | end |
| 266 | end |
| 267 | end |
| 268 | end |
| 269 | put rz_mem dst_off uchar or pos_n shl neg_n 4 |
| 270 | ret nnz |
| 271 | end |
| 272 | |
| 273 | fn rz_pack_lut_row row ulong g ulong m ulong n ulong base_off ulong to ulong |
| 274 | nnz = ulong 0 |
| 275 | for j ulong 4 |
| 276 | col0 = add mul g 16 mul 4 j |
| 277 | nnz = add nnz rz_pack_lut_byte row col0 m n add base_off j |
| 278 | end |
| 279 | ret nnz |
| 280 | end |
| 281 | |
| 282 | fn rz_lut_ro r ulong to ulong |
| 283 | ret add mul div r 4 16 mul rem r 4 4 |
| 284 | end |
| 285 | |
| 286 | fn rz_pack_lut_blk bl ulong g ulong m ulong n ulong sg ulong to ulong |
| 287 | nnz = ulong 0 |
| 288 | blk_off = mul add mul bl sg g 64 |
| 289 | for r ulong 16 |
| 290 | row = add mul bl 16 r |
| 291 | nnz = add nnz rz_pack_lut_row row g m n add blk_off rz_lut_ro r |
| 292 | end |
| 293 | ret nnz |
| 294 | end |
| 295 | |
| 296 | fn rz_pack_lut m ulong n ulong to int |
| 297 | sg = div add n 15 16 |
| 298 | mb = div add m 15 16 |
| 299 | nnz = ulong 0 |
| 300 | for bl ulong mb bound 128 |
| 301 | for g ulong sg bound 8192 |
| 302 | nnz = add nnz rz_pack_lut_blk bl g m n sg |
| 303 | end |
| 304 | end |
| 305 | wb = mul mul mb sg 64 |
| 306 | put rz_plan 0 m |
| 307 | put rz_plan 1 n |
| 308 | put rz_plan 2 wb |
| 309 | put rz_plan 3 wb |
| 310 | put rz_plan 4 nnz |
| 311 | put rz_plan 5 1 |
| 312 | put rz_plan 6 RZ_R2B_LUT |
| 313 | ret OMA_RZ_OK |
| 314 | end |
| 315 | |
| 316 | fn rz_run_lut_byte bv ulong col0 ulong n ulong to int |
| 317 | acc = int 0 |
| 318 | for q ulong 4 |
| 319 | col = add col0 q |
| 320 | if lt col n |
| 321 | xv = int get rz_x col |
| 322 | if ne and shr bv q 1 0 |
| 323 | acc = add acc xv |
| 324 | else |
| 325 | if ne and shr bv add 4 q 1 0 |
| 326 | acc = sub acc xv |
| 327 | end |
| 328 | end |
| 329 | end |
| 330 | end |
| 331 | ret acc |
| 332 | end |
| 333 | |
| 334 | fn rz_run_lut_row row ulong g ulong n ulong base_off ulong to void |
| 335 | acc = get rz_y row |
| 336 | for j ulong 4 |
| 337 | bv = ulong get rz_mem add base_off j |
| 338 | col0 = add mul g 16 mul 4 j |
| 339 | acc = add acc rz_run_lut_byte bv col0 n |
| 340 | end |
| 341 | put rz_y row acc |
| 342 | end |
| 343 | |
| 344 | fn rz_run_lut_blk bl ulong g ulong m ulong n ulong sg ulong to void |
| 345 | blk_off = mul add mul bl sg g 64 |
| 346 | for r ulong 16 |
| 347 | row = add mul bl 16 r |
| 348 | if lt row m |
| 349 | rz_run_lut_row row g n add blk_off rz_lut_ro r |
| 350 | end |
| 351 | end |
| 352 | end |
| 353 | |
| 354 | fn rz_run_lut to int |
| 355 | m = get rz_plan 0 |
| 356 | n = get rz_plan 1 |
| 357 | sg = div add n 15 16 |
| 358 | mb = div add m 15 16 |
| 359 | for i ulong m bound 1024 |
| 360 | put rz_y i 0 |
| 361 | end |
| 362 | for bl ulong mb bound 128 |
| 363 | for g ulong sg bound 8192 |
| 364 | rz_run_lut_blk bl g m n sg |
| 365 | end |
| 366 | end |
| 367 | ret OMA_RZ_OK |
| 368 | end |
| 369 | |
| 370 | note ---- R2c_crumb: chunks of 64 cols -> 16 bytes, 00=0, 01=+1, 11=-1, 10=bottom ---- |
| 371 | |
| 372 | fn rz_pack_crumb_lane base_w ulong base_d ulong cbase ulong n ulong j ulong to ulong |
| 373 | nnz = ulong 0 |
| 374 | bv = ulong 0 |
| 375 | for k ulong 4 |
| 376 | col = add add cbase mul 16 k j |
| 377 | if lt col n |
| 378 | v = int get rz_w add base_w col |
| 379 | if eq v 1 |
| 380 | bv = or bv shl ulong 1 mul 2 k |
| 381 | nnz = add nnz 1 |
| 382 | else |
| 383 | if eq v -1 |
| 384 | bv = or bv shl ulong 3 mul 2 k |
| 385 | nnz = add nnz 1 |
| 386 | end |
| 387 | end |
| 388 | end |
| 389 | end |
| 390 | put rz_mem add base_d j uchar bv |
| 391 | ret nnz |
| 392 | end |
| 393 | |
| 394 | fn rz_pack_crumb_chunk i ulong c ulong n ulong rb ulong to ulong |
| 395 | nnz = ulong 0 |
| 396 | base_d = add mul i rb mul c 16 |
| 397 | base_w = mul i n |
| 398 | cbase = mul c 64 |
| 399 | for j ulong 16 |
| 400 | nnz = add nnz rz_pack_crumb_lane base_w base_d cbase n j |
| 401 | end |
| 402 | ret nnz |
| 403 | end |
| 404 | |
| 405 | fn rz_pack_crumb m ulong n ulong to int |
| 406 | chunks = div add n 63 64 |
| 407 | rb = mul chunks 16 |
| 408 | nnz = ulong 0 |
| 409 | for i ulong m bound 1024 |
| 410 | for c ulong chunks bound 4096 |
| 411 | nnz = add nnz rz_pack_crumb_chunk i c n rb |
| 412 | end |
| 413 | end |
| 414 | wb = mul m rb |
| 415 | put rz_plan 0 m |
| 416 | put rz_plan 1 n |
| 417 | put rz_plan 2 wb |
| 418 | put rz_plan 3 wb |
| 419 | put rz_plan 4 nnz |
| 420 | put rz_plan 5 1 |
| 421 | put rz_plan 6 RZ_R2C_CRUMB |
| 422 | ret OMA_RZ_OK |
| 423 | end |
| 424 | |
| 425 | fn rz_run_crumb_lane bv ulong cbase ulong n ulong j ulong to int |
| 426 | acc = int 0 |
| 427 | for k ulong 4 |
| 428 | col = add add cbase mul 16 k j |
| 429 | if lt col n |
| 430 | cr = and shr bv mul 2 k 3 |
| 431 | if eq cr 1 |
| 432 | acc = add acc int get rz_x col |
| 433 | else |
| 434 | if eq cr 3 |
| 435 | acc = sub acc int get rz_x col |
| 436 | end |
| 437 | end |
| 438 | end |
| 439 | end |
| 440 | ret acc |
| 441 | end |
| 442 | |
| 443 | fn rz_run_crumb_row i ulong n ulong chunks ulong rb ulong to int |
| 444 | acc = int 0 |
| 445 | base_r = mul i rb |
| 446 | for c ulong chunks bound 4096 |
| 447 | base_d = add base_r mul c 16 |
| 448 | cbase = mul c 64 |
| 449 | for j ulong 16 |
| 450 | acc = add acc rz_run_crumb_lane ulong get rz_mem add base_d j cbase n j |
| 451 | end |
| 452 | end |
| 453 | ret acc |
| 454 | end |
| 455 | |
| 456 | fn rz_run_crumb to int |
| 457 | m = get rz_plan 0 |
| 458 | n = get rz_plan 1 |
| 459 | chunks = div add n 63 64 |
| 460 | rb = mul chunks 16 |
| 461 | for i ulong m bound 1024 |
| 462 | put rz_y i rz_run_crumb_row i n chunks rb |
| 463 | end |
| 464 | ret OMA_RZ_OK |
| 465 | end |
| 466 | |
| 467 | note ---- R3_sparse: uint16 +1/-1 index lists + uint32 row offsets ---- |
| 468 | |
| 469 | fn rz_pack_sparse m ulong n ulong to int |
| 470 | k = uint 0 |
| 471 | for i ulong m bound 1024 |
| 472 | base = mul i n |
| 473 | put rz_off mul 2 i k |
| 474 | for j ulong n bound 70000 |
| 475 | if gt int get rz_w add base j 0 |
| 476 | put rz_idx ulong k ushort j |
| 477 | k = add k 1 |
| 478 | end |
| 479 | end |
| 480 | put rz_off add mul 2 i 1 k |
| 481 | for j ulong n bound 70000 |
| 482 | if lt int get rz_w add base j 0 |
| 483 | put rz_idx ulong k ushort j |
| 484 | k = add k 1 |
| 485 | end |
| 486 | end |
| 487 | end |
| 488 | put rz_off mul 2 m k |
| 489 | wb = add mul ulong k 2 mul add mul 2 m 1 4 |
| 490 | put rz_plan 0 m |
| 491 | put rz_plan 1 n |
| 492 | put rz_plan 2 wb |
| 493 | put rz_plan 3 wb |
| 494 | put rz_plan 4 ulong k |
| 495 | put rz_plan 5 1 |
| 496 | put rz_plan 6 RZ_R3_SPARSE |
| 497 | ret OMA_RZ_OK |
| 498 | end |
| 499 | |
| 500 | fn rz_run_sparse to int |
| 501 | m = get rz_plan 0 |
| 502 | for i ulong m bound 1024 |
| 503 | a = ulong get rz_off mul 2 i |
| 504 | b = ulong get rz_off add mul 2 i 1 |
| 505 | c = ulong get rz_off add mul 2 i 2 |
| 506 | acc = int 0 |
| 507 | k = a |
| 508 | while lt k b bound 70000 |
| 509 | acc = add acc int get rz_x ulong get rz_idx k |
| 510 | k = add k 1 |
| 511 | end |
| 512 | k = b |
| 513 | while lt k c bound 70000 |
| 514 | acc = sub acc int get rz_x ulong get rz_idx k |
| 515 | k = add k 1 |
| 516 | end |
| 517 | put rz_y i acc |
| 518 | end |
| 519 | ret OMA_RZ_OK |
| 520 | end |
| 521 | |
| 522 | note ---- R4_rns: moduli {256, 255, 253} + mixed-radix CRT ---- |
| 523 | |
| 524 | fn rz_modp v int p int to int |
| 525 | r = rem v p |
| 526 | if lt r 0 |
| 527 | ret add r p |
| 528 | end |
| 529 | ret r |
| 530 | end |
| 531 | |
| 532 | fn rz_pack_rns m ulong n ulong to int |
| 533 | np = mul div add n 15 16 16 |
| 534 | nnz = ulong 0 |
| 535 | for i ulong m bound 1024 |
| 536 | nnz = add nnz rz_pack_r1_row mul i n mul i np n np |
| 537 | end |
| 538 | wb = mul m np |
| 539 | put rz_plan 0 m |
| 540 | put rz_plan 1 n |
| 541 | put rz_plan 2 wb |
| 542 | put rz_plan 3 add wb mul 3 np |
| 543 | put rz_plan 4 nnz |
| 544 | put rz_plan 5 1 |
| 545 | put rz_plan 6 RZ_R4_RNS |
| 546 | ret OMA_RZ_OK |
| 547 | end |
| 548 | |
| 549 | fn rz_rns_crt c0 int c1 int c2 int to int |
| 550 | a0 = and c0 255 |
| 551 | a1 = rz_modp sub rz_modp c1 255 a0 255 |
| 552 | t2 = rz_modp sub sub rz_modp c2 253 a0 mul 256 a1 253 |
| 553 | a2 = rz_modp rem mul t2 211 253 253 |
| 554 | yv = add a0 add mul 256 a1 mul 65280 a2 |
| 555 | if ge yv div RZ_RNS_M 2 |
| 556 | ret sub yv RZ_RNS_M |
| 557 | end |
| 558 | ret yv |
| 559 | end |
| 560 | |
| 561 | fn rz_rns_dot base ulong xoff ulong n ulong to int |
| 562 | acc = int 0 |
| 563 | for j ulong n bound 70000 |
| 564 | wv = int get rz_mem add base j |
| 565 | if ge wv 128 |
| 566 | wv = sub wv 256 |
| 567 | end |
| 568 | acc = add acc mul wv int get rz_rns_x add xoff j |
| 569 | end |
| 570 | ret acc |
| 571 | end |
| 572 | |
| 573 | fn rz_run_rns_row i ulong n ulong np ulong to int |
| 574 | base = mul i np |
| 575 | c0 = rz_rns_dot base 0 n |
| 576 | c1 = rz_rns_dot base np n |
| 577 | c2 = rz_rns_dot base mul 2 np n |
| 578 | ret rz_rns_crt c0 c1 c2 |
| 579 | end |
| 580 | |
| 581 | fn rz_run_rns to int |
| 582 | m = get rz_plan 0 |
| 583 | n = get rz_plan 1 |
| 584 | np = mul div add n 15 16 16 |
| 585 | for j ulong n bound 70000 |
| 586 | v = int get rz_x j |
| 587 | put rz_rns_x j byte v |
| 588 | if eq v -128 |
| 589 | put rz_rns_x add np j byte 127 |
| 590 | else |
| 591 | put rz_rns_x add np j byte v |
| 592 | end |
| 593 | v253 = v |
| 594 | if gt v 126 |
| 595 | v253 = sub v 253 |
| 596 | else |
| 597 | if lt v -126 |
| 598 | v253 = add v 253 |
| 599 | end |
| 600 | end |
| 601 | put rz_rns_x add mul 2 np j byte v253 |
| 602 | end |
| 603 | for i ulong m bound 1024 |
| 604 | put rz_y i rz_run_rns_row i n np |
| 605 | end |
| 606 | ret OMA_RZ_OK |
| 607 | end |
| 608 | |
| 609 | note ---- R5_dense5: strided 5 trits/byte (chunks of 80 cols -> 16 bytes) ---- |
| 610 | |
| 611 | fn rz_pack_d5_lane base_w ulong base_d ulong cbase ulong n ulong j ulong to ulong |
| 612 | nnz = ulong 0 |
| 613 | bv = ulong 0 |
| 614 | pw = ulong 1 |
| 615 | for i ulong 5 |
| 616 | col = add add cbase mul 16 i j |
| 617 | v = int 0 |
| 618 | if lt col n |
| 619 | v = int get rz_w add base_w col |
| 620 | end |
| 621 | if ne v 0 |
| 622 | nnz = add nnz 1 |
| 623 | end |
| 624 | bv = add bv mul ulong add v 1 pw |
| 625 | pw = mul pw 3 |
| 626 | end |
| 627 | put rz_mem add base_d j uchar bv |
| 628 | ret nnz |
| 629 | end |
| 630 | |
| 631 | fn rz_pack_d5_chunk r ulong c ulong n ulong rb ulong to ulong |
| 632 | nnz = ulong 0 |
| 633 | base_d = add mul r rb mul c 16 |
| 634 | base_w = mul r n |
| 635 | cbase = mul c 80 |
| 636 | for j ulong 16 |
| 637 | nnz = add nnz rz_pack_d5_lane base_w base_d cbase n j |
| 638 | end |
| 639 | ret nnz |
| 640 | end |
| 641 | |
| 642 | fn rz_pack_dense5 m ulong n ulong to int |
| 643 | chunks = div add n 79 80 |
| 644 | rb = mul chunks 16 |
| 645 | nnz = ulong 0 |
| 646 | for r ulong m bound 1024 |
| 647 | for c ulong chunks bound 2048 |
| 648 | nnz = add nnz rz_pack_d5_chunk r c n rb |
| 649 | end |
| 650 | end |
| 651 | wb = mul m rb |
| 652 | put rz_plan 0 m |
| 653 | put rz_plan 1 n |
| 654 | put rz_plan 2 wb |
| 655 | put rz_plan 3 wb |
| 656 | put rz_plan 4 nnz |
| 657 | put rz_plan 5 1 |
| 658 | put rz_plan 6 RZ_R5_DENSE5 |
| 659 | ret OMA_RZ_OK |
| 660 | end |
| 661 | |
| 662 | fn rz_run_d5_lane bv ulong cbase ulong n ulong j ulong to int |
| 663 | acc = int 0 |
| 664 | v = bv |
| 665 | for i ulong 5 |
| 666 | d = int rem v 3 |
| 667 | v = div v 3 |
| 668 | col = add add cbase mul 16 i j |
| 669 | if lt col n |
| 670 | acc = add acc mul d int get rz_x col |
| 671 | end |
| 672 | end |
| 673 | ret acc |
| 674 | end |
| 675 | |
| 676 | fn rz_run_d5_row r ulong n ulong chunks ulong rb ulong sumx int to int |
| 677 | acc = int 0 |
| 678 | base_r = mul r rb |
| 679 | for c ulong chunks bound 2048 |
| 680 | base_d = add base_r mul c 16 |
| 681 | cbase = mul c 80 |
| 682 | for j ulong 16 |
| 683 | acc = add acc rz_run_d5_lane ulong get rz_mem add base_d j cbase n j |
| 684 | end |
| 685 | end |
| 686 | ret sub acc sumx |
| 687 | end |
| 688 | |
| 689 | fn rz_run_dense5 to int |
| 690 | m = get rz_plan 0 |
| 691 | n = get rz_plan 1 |
| 692 | chunks = div add n 79 80 |
| 693 | rb = mul chunks 16 |
| 694 | sumx = int 0 |
| 695 | for j ulong n bound 200000 |
| 696 | sumx = add sumx int get rz_x j |
| 697 | end |
| 698 | for r ulong m bound 1024 |
| 699 | put rz_y r rz_run_d5_row r n chunks rb sumx |
| 700 | end |
| 701 | ret OMA_RZ_OK |
| 702 | end |
| 703 | |
| 704 | note ---- dispatch ---- |
| 705 | |
| 706 | fn oma_rz_pack_g rid ulong m ulong n ulong to int |
| 707 | put rz_plan 5 0 |
| 708 | rc = oma_rz_check_shape m n oma_rz_max_n rid |
| 709 | if ne rc 0 |
| 710 | ret rc |
| 711 | end |
| 712 | rc = oma_rz_validate_g m n |
| 713 | if ne rc 0 |
| 714 | ret rc |
| 715 | end |
| 716 | if le rid RZ_R1_SMMLA |
| 717 | ret rz_pack_r1 rid m n |
| 718 | end |
| 719 | if eq rid RZ_R2_BITPLANE |
| 720 | ret rz_pack_bitplane m n |
| 721 | end |
| 722 | if eq rid RZ_R2B_LUT |
| 723 | ret rz_pack_lut m n |
| 724 | end |
| 725 | if eq rid RZ_R2C_CRUMB |
| 726 | ret rz_pack_crumb m n |
| 727 | end |
| 728 | if eq rid RZ_R3_SPARSE |
| 729 | ret rz_pack_sparse m n |
| 730 | end |
| 731 | if eq rid RZ_R4_RNS |
| 732 | ret rz_pack_rns m n |
| 733 | end |
| 734 | if eq rid RZ_R5_DENSE5 |
| 735 | ret rz_pack_dense5 m n |
| 736 | end |
| 737 | ret OMA_RZ_E_ARG |
| 738 | end |
| 739 | |
| 740 | fn oma_rz_run_g to int |
| 741 | if eq get rz_plan 5 0 |
| 742 | ret OMA_RZ_E_ARG |
| 743 | end |
| 744 | rid = get rz_plan 6 |
| 745 | if le rid RZ_R1_SMMLA |
| 746 | ret rz_run_r1 |
| 747 | end |
| 748 | if eq rid RZ_R2_BITPLANE |
| 749 | ret rz_run_bitplane |
| 750 | end |
| 751 | if eq rid RZ_R2B_LUT |
| 752 | ret rz_run_lut |
| 753 | end |
| 754 | if eq rid RZ_R2C_CRUMB |
| 755 | ret rz_run_crumb |
| 756 | end |
| 757 | if eq rid RZ_R3_SPARSE |
| 758 | ret rz_run_sparse |
| 759 | end |
| 760 | if eq rid RZ_R4_RNS |
| 761 | ret rz_run_rns |
| 762 | end |
| 763 | if eq rid RZ_R5_DENSE5 |
| 764 | ret rz_run_dense5 |
| 765 | end |
| 766 | ret OMA_RZ_E_ARG |
| 767 | end |