interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / polyglot / omx_common.om

1059 lines · 23596 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note src/polyglot/omx_common.om -- pure seeded OM-2 POLYGLOT-0 shared library:
5note constants, SplitMix64 RNG, SHA-256, contract digest, ELF64 LE text sizing,
6note formatting & JSON string escaping, statistics (heapsort, median, MAD),
7note and Omega-X ternary matvec oracle + sdot/crumb pack & run kernels.
8om2
9
10const OMX_MAX_N ulong 16777215
11const OMX_BUF_MAX ulong 16777216
12const OMX_Y_MAX ulong 65536
13const OMX_OK int 0
14const OMX_E_ARG int -1
15const OMX_E_TRIT int -2
16const OMX_E_NOMEM int -3
17const OMX_E_OVERFLOW int -4
18const OMX_ULONG_MAX ulong 18446744073709551615
19
20const OMX_CAND_PLAIN ulong 0
21const OMX_CAND_SDOT ulong 1
22const OMX_CAND_CRUMB ulong 2
23const OMX_CAND_ENC_SDOT ulong 3
24const OMX_CAND_ENC_CRUMB ulong 4
25
26note ---- shared global buffers (zero-initialized, program mode) ----
27global g_omx_w byte 16777216
28global g_omx_x byte 16777216
29global g_omx_pack uchar 16777216
30global g_omx_y int 65536
31global g_omx_yref int 65536
32
33note plan fields: [0]=m [1]=n [2]=weight_bytes [3]=footprint_bytes [4]=nnz [5]=valid [6]=kind
34global g_omx_plan ulong 8
35
36note print & formatting scratch
37global g_omx_pbuf uchar 256
38global g_omx_path uchar 768
39global g_omx_fbuf uchar 262144
40global g_omx_stat f64 1024
41global g_omx_stmp f64 1024
42
43note SHA-256 state
44global g_omx_shk uint 64
45global g_omx_shh uint 8
46global g_omx_shv uint 8
47global g_omx_shw uint 64
48global g_omx_shb uchar 64
49global g_omx_shmeta ulong 4
50global g_omx_shdig uchar 32
51global g_omx_shhex uchar 64
52
53note ---- basic I/O & formatting helpers ----
54
55fn omx_write_all fd int buf view uchar * off ulong n ulong to long
56 sent = ulong 0
57 while lt sent n bound 16777216
58 r = write fd buf add off sent sub n sent
59 if le r 0
60 ret r
61 end
62 sent = add sent ulong r
63 end
64 ret long sent
65end
66
67fn omx_puts s view uchar * to void
68 _r = omx_write_all 1 s 0 len s
69end
70
71fn omx_eputs s view uchar * to void
72 _r = omx_write_all 2 s 0 len s
73end
74
75fn omx_putc ch uchar to void
76 put g_omx_pbuf 0 ch
77 _r = write 1 g_omx_pbuf 0 1
78end
79
80fn omx_eputc ch uchar to void
81 put g_omx_pbuf 0 ch
82 _r = write 2 g_omx_pbuf 0 1
83end
84
85fn omx_nl to void
86 omx_putc uchar 10
87end
88
89fn omx_enl to void
90 omx_eputc uchar 10
91end
92
93fn omx_putu_fd fd int v ulong to void
94 if eq v 0
95 put g_omx_pbuf 0 uchar 48
96 _r = write fd g_omx_pbuf 0 1
97 ret
98 end
99 pos = ulong 32
100 cur = v
101 while gt cur 0 bound 32
102 pos = sub pos 1
103 d = rem cur 10
104 put g_omx_pbuf pos uchar add 48 d
105 cur = div cur 10
106 end
107 _r = write fd g_omx_pbuf pos sub 32 pos
108end
109
110fn omx_putu v ulong to void
111 omx_putu_fd 1 v
112end
113
114fn omx_eputu v ulong to void
115 omx_putu_fd 2 v
116end
117
118fn omx_puti_fd fd int v long to void
119 if lt v 0
120 put g_omx_pbuf 0 uchar 45
121 _r = write fd g_omx_pbuf 0 1
122 if eq v -9223372036854775808
123 omx_putu_fd fd ulong 9223372036854775808
124 ret
125 end
126 omx_putu_fd fd ulong neg v
127 ret
128 end
129 omx_putu_fd fd ulong v
130end
131
132fn omx_puti v long to void
133 omx_puti_fd 1 v
134end
135
136fn omx_eputi v long to void
137 omx_puti_fd 2 v
138end
139
140fn omx_hex_nib v ulong to uchar
141 if lt v 10
142 ret uchar add 48 v
143 end
144 ret uchar add 87 v
145end
146
147fn omx_putx32_fd fd int v uint to void
148 u = ulong v
149 for i ulong 8
150 sh = mul sub 7 i 4
151 nib = and shr u sh 15
152 put g_omx_pbuf i omx_hex_nib nib
153 end
154 _r = write fd g_omx_pbuf 0 8
155end
156
157fn omx_putx32 v uint to void
158 omx_putx32_fd 1 v
159end
160
161fn omx_putx64_fd fd int v ulong to void
162 for i ulong 16
163 sh = mul sub 15 i 4
164 nib = and shr v sh 15
165 put g_omx_pbuf i omx_hex_nib nib
166 end
167 _r = write fd g_omx_pbuf 0 16
168end
169
170fn omx_put_frac_fd fd int frac ulong prec ulong to void
171 put g_omx_pbuf 0 uchar 46
172 _r = write fd g_omx_pbuf 0 1
173 pos = prec
174 cur = frac
175 for i ulong prec bound 10
176 pos = sub pos 1
177 d = rem cur 10
178 put g_omx_pbuf pos uchar add 48 d
179 cur = div cur 10
180 end
181 _r2 = write fd g_omx_pbuf 0 prec
182end
183
184fn omx_putf_fd fd int v f64 prec ulong to void
185 x = v
186 if lt x 0.0f64
187 put g_omx_pbuf 0 uchar 45
188 _r = write fd g_omx_pbuf 0 1
189 x = sub 0.0f64 x
190 end
191 scale = f64 1.0f64
192 iscale = ulong 1
193 for i ulong prec bound 10
194 scale = mul scale 10.0f64
195 iscale = mul iscale 10
196 end
197 scaled = ulong add mul x scale 0.5f64
198 omx_putu_fd fd div scaled iscale
199 if gt prec 0
200 omx_put_frac_fd fd rem scaled iscale prec
201 end
202end
203
204fn omx_putf v f64 prec ulong to void
205 omx_putf_fd 1 v prec
206end
207
208fn omx_json_str_fd fd int s view uchar * n ulong to void
209 put g_omx_pbuf 0 uchar 34
210 _r = write fd g_omx_pbuf 0 1
211 for i ulong n bound 65536
212 c = get s i
213 if eq c 34
214 put g_omx_pbuf 0 uchar 92
215 put g_omx_pbuf 1 uchar 34
216 _r2 = write fd g_omx_pbuf 0 2
217 else
218 if eq c 92
219 put g_omx_pbuf 0 uchar 92
220 put g_omx_pbuf 1 uchar 92
221 _r2 = write fd g_omx_pbuf 0 2
222 else
223 if eq c 10
224 put g_omx_pbuf 0 uchar 92
225 put g_omx_pbuf 1 uchar 110
226 _r2 = write fd g_omx_pbuf 0 2
227 else
228 if eq c 9
229 put g_omx_pbuf 0 uchar 92
230 put g_omx_pbuf 1 uchar 116
231 _r2 = write fd g_omx_pbuf 0 2
232 else
233 put g_omx_pbuf 0 c
234 _r2 = write fd g_omx_pbuf 0 1
235 end
236 end
237 end
238 end
239 end
240 put g_omx_pbuf 0 uchar 34
241 _r3 = write fd g_omx_pbuf 0 1
242end
243
244note ---- SplitMix64 RNG (matches src/polyglot/omx_core.om) ----
245
246fn omx_rng_next st edit ulong 1 to ulong
247 z = add get st 0 0x9e3779b97f4a7c15
248 put st 0 z
249 z = mul xor z shr z 30 0xbf58476d1ce4e5b9
250 z = mul xor z shr z 27 0x94d049bb133111eb
251 ret xor z shr z 31
252end
253
254fn omx_rng_01 st edit ulong 1 to f64
255 v = omx_rng_next st
256 q = shr v 11
257 inv = bitcast f64 ulong 0x3ca0000000000000
258 ret mul f64 q inv
259end
260
261note ---- Statistics: heapsort, median, MAD on f64 (stored as ulong bits or f64 array) ----
262
263fn omx_sift_f64 v edit f64 * root ulong n ulong to void
264 r = root
265 while true bound 65536
266 child = add mul r 2 1
267 if ge child n
268 ret
269 end
270 pick = r
271 if gt get v child get v pick
272 pick = child
273 end
274 c2 = add child 1
275 if lt c2 n
276 if gt get v c2 get v pick
277 pick = c2
278 end
279 end
280 if eq pick r
281 ret
282 end
283 tmp = get v r
284 put v r get v pick
285 put v pick tmp
286 r = pick
287 end
288end
289
290fn omx_sort_f64 v edit f64 * n ulong to void
291 if lt n 2
292 ret
293 end
294 i = div n 2
295 while gt i 0 bound 65536
296 i = sub i 1
297 omx_sift_f64 v i n
298 end
299 i = sub n 1
300 while gt i 0 bound 65536
301 t = get v 0
302 put v 0 get v i
303 put v i t
304 omx_sift_f64 v 0 i
305 i = sub i 1
306 end
307end
308
309fn omx_median_f64 v view f64 * n ulong to f64
310 if eq n 0
311 ret 0.0f64
312 end
313 mid = div n 2
314 if eq and n 1 1
315 ret get v mid
316 end
317 ret mul 0.5f64 add get v sub mid 1 get v mid
318end
319
320fn omx_mad_f64 v view f64 * n ulong med f64 tmp edit f64 * to f64
321 if eq n 0
322 ret 0.0f64
323 end
324 for i ulong n bound 65536
325 x = get v i
326 if gt x med
327 put tmp i sub x med
328 else
329 put tmp i sub med x
330 end
331 end
332 omx_sort_f64 tmp n
333 ret omx_median_f64 tmp n
334end
335
336note ---- Pure OM-2 SHA-256 ----
337
338fn omx_sha_init_k to void
339 put g_omx_shk 0 uint 0x428a2f98
340 put g_omx_shk 1 uint 0x71374491
341 put g_omx_shk 2 uint 0xb5c0fbcf
342 put g_omx_shk 3 uint 0xe9b5dba5
343 put g_omx_shk 4 uint 0x3956c25b
344 put g_omx_shk 5 uint 0x59f111f1
345 put g_omx_shk 6 uint 0x923f82a4
346 put g_omx_shk 7 uint 0xab1c5ed5
347 put g_omx_shk 8 uint 0xd807aa98
348 put g_omx_shk 9 uint 0x12835b01
349 put g_omx_shk 10 uint 0x243185be
350 put g_omx_shk 11 uint 0x550c7dc3
351 put g_omx_shk 12 uint 0x72be5d74
352 put g_omx_shk 13 uint 0x80deb1fe
353 put g_omx_shk 14 uint 0x9bdc06a7
354 put g_omx_shk 15 uint 0xc19bf174
355 put g_omx_shk 16 uint 0xe49b69c1
356 put g_omx_shk 17 uint 0xefbe4786
357 put g_omx_shk 18 uint 0x0fc19dc6
358 put g_omx_shk 19 uint 0x240ca1cc
359 put g_omx_shk 20 uint 0x2de92c6f
360 put g_omx_shk 21 uint 0x4a7484aa
361 put g_omx_shk 22 uint 0x5cb0a9dc
362 put g_omx_shk 23 uint 0x76f988da
363 put g_omx_shk 24 uint 0x983e5152
364 put g_omx_shk 25 uint 0xa831c66d
365 put g_omx_shk 26 uint 0xb00327c8
366 put g_omx_shk 27 uint 0xbf597fc7
367 put g_omx_shk 28 uint 0xc6e00bf3
368 put g_omx_shk 29 uint 0xd5a79147
369 put g_omx_shk 30 uint 0x06ca6351
370 put g_omx_shk 31 uint 0x14292967
371 put g_omx_shk 32 uint 0x27b70a85
372 put g_omx_shk 33 uint 0x2e1b2138
373 put g_omx_shk 34 uint 0x4d2c6dfc
374 put g_omx_shk 35 uint 0x53380d13
375 put g_omx_shk 36 uint 0x650a7354
376 put g_omx_shk 37 uint 0x766a0abb
377 put g_omx_shk 38 uint 0x81c2c92e
378 put g_omx_shk 39 uint 0x92722c85
379 put g_omx_shk 40 uint 0xa2bfe8a1
380 put g_omx_shk 41 uint 0xa81a664b
381 put g_omx_shk 42 uint 0xc24b8b70
382 put g_omx_shk 43 uint 0xc76c51a3
383 put g_omx_shk 44 uint 0xd192e819
384 put g_omx_shk 45 uint 0xd6990624
385 put g_omx_shk 46 uint 0xf40e3585
386 put g_omx_shk 47 uint 0x106aa070
387 put g_omx_shk 48 uint 0x19a4c116
388 put g_omx_shk 49 uint 0x1e376c08
389 put g_omx_shk 50 uint 0x2748774c
390 put g_omx_shk 51 uint 0x34b0bcb5
391 put g_omx_shk 52 uint 0x391c0cb3
392 put g_omx_shk 53 uint 0x4ed8aa4a
393 put g_omx_shk 54 uint 0x5b9cca4f
394 put g_omx_shk 55 uint 0x682e6ff3
395 put g_omx_shk 56 uint 0x748f82ee
396 put g_omx_shk 57 uint 0x78a5636f
397 put g_omx_shk 58 uint 0x84c87814
398 put g_omx_shk 59 uint 0x8cc70208
399 put g_omx_shk 60 uint 0x90befffa
400 put g_omx_shk 61 uint 0xa4506ceb
401 put g_omx_shk 62 uint 0xbef9a3f7
402 put g_omx_shk 63 uint 0xc67178f2
403end
404
405fn omx_sha_init to void
406 omx_sha_init_k
407 put g_omx_shh 0 uint 0x6a09e667
408 put g_omx_shh 1 uint 0xbb67ae85
409 put g_omx_shh 2 uint 0x3c6ef372
410 put g_omx_shh 3 uint 0xa54ff53a
411 put g_omx_shh 4 uint 0x510e527f
412 put g_omx_shh 5 uint 0x9b05688c
413 put g_omx_shh 6 uint 0x1f83d9ab
414 put g_omx_shh 7 uint 0x5be0cd19
415 put g_omx_shmeta 0 ulong 0
416 put g_omx_shmeta 1 ulong 0
417end
418
419fn omx_sha_expand to void
420 for i ulong 16
421 off = mul i 4
422 w = shl uint get g_omx_shb off 24
423 w = or w shl uint get g_omx_shb add off 1 16
424 w = or w shl uint get g_omx_shb add off 2 8
425 w = or w uint get g_omx_shb add off 3
426 put g_omx_shw i w
427 end
428 for i ulong 48
429 k = add i 16
430 x = get g_omx_shw sub k 15
431 s0 = xor xor rotr x 7 rotr x 18 shr x 3
432 y = get g_omx_shw sub k 2
433 s1 = xor xor rotr y 17 rotr y 19 shr y 10
434 w = add add get g_omx_shw sub k 16 s0 add get g_omx_shw sub k 7 s1
435 put g_omx_shw k w
436 end
437end
438
439fn omx_sha_step i ulong to void
440 e = get g_omx_shv 4
441 s1 = xor xor rotr e 6 rotr e 11 rotr e 25
442 ch = xor and e get g_omx_shv 5 and not e get g_omx_shv 6
443 t1 = add add get g_omx_shv 7 s1 ch
444 t1 = add t1 add get g_omx_shk i get g_omx_shw i
445 a = get g_omx_shv 0
446 b = get g_omx_shv 1
447 c = get g_omx_shv 2
448 s0 = xor xor rotr a 2 rotr a 13 rotr a 22
449 maj = xor xor and a b and a c and b c
450 t2 = add s0 maj
451 put g_omx_shv 7 get g_omx_shv 6
452 put g_omx_shv 6 get g_omx_shv 5
453 put g_omx_shv 5 e
454 put g_omx_shv 4 add get g_omx_shv 3 t1
455 put g_omx_shv 3 c
456 put g_omx_shv 2 b
457 put g_omx_shv 1 a
458 put g_omx_shv 0 add t1 t2
459end
460
461fn omx_sha_block to void
462 omx_sha_expand
463 for i ulong 8
464 put g_omx_shv i get g_omx_shh i
465 end
466 for i ulong 64
467 omx_sha_step i
468 end
469 for i ulong 8
470 put g_omx_shh i add get g_omx_shh i get g_omx_shv i
471 end
472end
473
474fn omx_sha_update_byte b uchar to void
475 blen = get g_omx_shmeta 0
476 tot = get g_omx_shmeta 1
477 put g_omx_shb blen b
478 blen = add blen 1
479 put g_omx_shmeta 1 add tot 1
480 if eq blen 64
481 omx_sha_block
482 blen = ulong 0
483 end
484 put g_omx_shmeta 0 blen
485end
486
487fn omx_sha_update_buf buf view uchar * off ulong n ulong to void
488 for i ulong n bound 16777216
489 omx_sha_update_byte get buf add off i
490 end
491end
492
493fn omx_sha_final to void
494 tot = get g_omx_shmeta 1
495 bits = mul tot 8
496 omx_sha_update_byte uchar 0x80
497 while ne get g_omx_shmeta 0 56 bound 64
498 omx_sha_update_byte uchar 0
499 end
500 for i ulong 8
501 sh = mul sub 7 i 8
502 omx_sha_update_byte uchar and shr bits sh 255
503 end
504 for i ulong 8
505 w = get g_omx_shh i
506 put g_omx_shdig mul i 4 uchar and shr w 24 255
507 put g_omx_shdig add mul i 4 1 uchar and shr w 16 255
508 put g_omx_shdig add mul i 4 2 uchar and shr w 8 255
509 put g_omx_shdig add mul i 4 3 uchar and w 255
510 end
511 for i ulong 32
512 b = ulong get g_omx_shdig i
513 put g_omx_shhex mul i 2 omx_hex_nib shr b 4
514 put g_omx_shhex add mul i 2 1 omx_hex_nib and b 15
515 end
516end
517
518note Read file named by g_omx_path[0..plen) into g_omx_fbuf. Returns length >= 0, or -1 on error.
519fn omx_read_gpath plen ulong to long
520 fd = fopen g_omx_path 0 plen 0 0
521 if lt fd 0
522 ret -1
523 end
524 tot = ulong 0
525 while lt tot 262144 bound 262144
526 r = read int fd g_omx_fbuf tot sub 262144 tot
527 if lt r 0
528 _c = fclose int fd
529 ret -1
530 end
531 if eq r 0
532 break
533 end
534 tot = add tot ulong r
535 end
536 _c = fclose int fd
537 ret long tot
538end
539
540note Compute contract digest of spec section 1 ("## 1. " up to next "## ") from g_omx_path[0..plen) into g_omx_shhex.
541note Returns 0 on success, -1 on error.
542fn omx_contract_digest_gpath plen ulong to int
543 flen = omx_read_gpath plen
544 if lt flen 0
545 ret -1
546 end
547 n = ulong flen
548 has_s = ulong 0
549 start = ulong 0
550 stop = n
551 i = ulong 0
552 while lt i n bound 262144
553 if eq has_s 0
554 if ge sub n i 6
555 if eq get g_omx_fbuf i 35
556 if eq get g_omx_fbuf add i 1 35
557 if eq get g_omx_fbuf add i 2 32
558 if eq get g_omx_fbuf add i 3 49
559 if eq get g_omx_fbuf add i 4 46
560 if eq get g_omx_fbuf add i 5 32
561 has_s = 1
562 start = i
563 end
564 end
565 end
566 end
567 end
568 end
569 end
570 else
571 if ge sub n i 3
572 if eq get g_omx_fbuf i 35
573 if eq get g_omx_fbuf add i 1 35
574 if eq get g_omx_fbuf add i 2 32
575 stop = i
576 break
577 end
578 end
579 end
580 end
581 end
582 while lt i n bound 262144
583 c = get g_omx_fbuf i
584 i = add i 1
585 if eq c 10
586 break
587 end
588 end
589 end
590 if eq has_s 0
591 ret -1
592 end
593 omx_sha_init
594 j = start
595 while lt j stop bound 262144
596 omx_sha_update_byte get g_omx_fbuf j
597 j = add j 1
598 end
599 omx_sha_final
600 ret 0
601end
602
603note Global stats helpers operating on g_omx_stat[0..n) and g_omx_stmp[0..n)
604fn omx_gsift root ulong n ulong to void
605 r = root
606 while true bound 65536
607 child = add mul r 2 1
608 if ge child n
609 ret
610 end
611 pick = r
612 if gt get g_omx_stat child get g_omx_stat pick
613 pick = child
614 end
615 c2 = add child 1
616 if lt c2 n
617 if gt get g_omx_stat c2 get g_omx_stat pick
618 pick = c2
619 end
620 end
621 if eq pick r
622 ret
623 end
624 tmp = get g_omx_stat r
625 put g_omx_stat r get g_omx_stat pick
626 put g_omx_stat pick tmp
627 r = pick
628 end
629end
630
631fn omx_gsort n ulong to void
632 if lt n 2
633 ret
634 end
635 i = div n 2
636 while gt i 0 bound 65536
637 i = sub i 1
638 omx_gsift i n
639 end
640 i = sub n 1
641 while gt i 0 bound 65536
642 t = get g_omx_stat 0
643 put g_omx_stat 0 get g_omx_stat i
644 put g_omx_stat i t
645 omx_gsift 0 i
646 i = sub i 1
647 end
648end
649
650fn omx_gmedian n ulong to f64
651 if eq n 0
652 ret 0.0f64
653 end
654 mid = div n 2
655 if eq and n 1 1
656 ret get g_omx_stat mid
657 end
658 ret mul 0.5f64 add get g_omx_stat sub mid 1 get g_omx_stat mid
659end
660
661fn omx_gmad n ulong med f64 to f64
662 if eq n 0
663 ret 0.0f64
664 end
665 for i ulong n bound 1024
666 x = get g_omx_stat i
667 if gt x med
668 put g_omx_stmp i sub x med
669 else
670 put g_omx_stmp i sub med x
671 end
672 end
673 for i ulong n bound 1024
674 put g_omx_stat i get g_omx_stmp i
675 end
676 omx_gsort n
677 ret omx_gmedian n
678end
679
680note ---- ELF64 LE helpers ----
681
682fn omx_u16_le_fbuf off ulong to ulong
683 b0 = ulong get g_omx_fbuf off
684 b1 = ulong get g_omx_fbuf add off 1
685 ret or b0 shl b1 8
686end
687
688fn omx_u32_le_fbuf off ulong to ulong
689 b0 = ulong get g_omx_fbuf off
690 b1 = ulong get g_omx_fbuf add off 1
691 b2 = ulong get g_omx_fbuf add off 2
692 b3 = ulong get g_omx_fbuf add off 3
693 ret or or b0 shl b1 8 or shl b2 16 shl b3 24
694end
695
696fn omx_u64_le_fbuf off ulong to ulong
697 lo = omx_u32_le_fbuf off
698 hi = omx_u32_le_fbuf add off 4
699 ret or lo shl hi 32
700end
701
702fn omx_elf64_le_ok_fbuf slen ulong to ulong
703 if lt slen 64
704 ret 0
705 end
706 if ne ulong get g_omx_fbuf 0 0x7f
707 ret 0
708 end
709 if ne ulong get g_omx_fbuf 1 69
710 ret 0
711 end
712 if ne ulong get g_omx_fbuf 2 76
713 ret 0
714 end
715 if ne ulong get g_omx_fbuf 3 70
716 ret 0
717 end
718 if ne ulong get g_omx_fbuf 4 2
719 ret 0
720 end
721 if ne ulong get g_omx_fbuf 5 1
722 ret 0
723 end
724 ret 1
725end
726
727fn omx_elf_sum_text shoff ulong shnum ulong to ulong
728 tot = ulong 0
729 for i ulong shnum bound 4096
730 base = add shoff mul i 64
731 if eq omx_u32_le_fbuf add base 4 1
732 if ne and omx_u64_le_fbuf add base 8 4 0
733 tot = add tot omx_u64_le_fbuf add base 32
734 end
735 end
736 end
737 ret tot
738end
739
740fn omx_elf_text_bytes_gpath plen ulong to long
741 flen = omx_read_gpath plen
742 if lt flen 64
743 ret -1
744 end
745 n = ulong flen
746 if eq omx_elf64_le_ok_fbuf n 0
747 ret -1
748 end
749 shoff = omx_u64_le_fbuf 40
750 if eq shoff 0
751 ret -1
752 end
753 if ne omx_u16_le_fbuf 58 64
754 ret -1
755 end
756 if gt shoff n
757 ret -1
758 end
759 shnum = omx_u16_le_fbuf 60
760 if lt div sub n shoff 64 shnum
761 ret -1
762 end
763 ret long omx_elf_sum_text shoff shnum
764end
765
766note ---- Omega-X ternary matvec: shape, validation, oracle, sdot & crumb pack/run ----
767
768fn omx_plan_zero to void
769 for i ulong 8
770 put g_omx_plan i 0
771 end
772end
773
774fn omx_plan_is_zero to ulong
775 for i ulong 7
776 if ne get g_omx_plan i 0
777 ret 0
778 end
779 end
780 ret 1
781end
782
783fn omx_check_shape m ulong n ulong max_n ulong to int
784 if eq m 0
785 ret OMX_E_ARG
786 end
787 if eq n 0
788 ret OMX_E_ARG
789 end
790 if gt n max_n
791 ret OMX_E_ARG
792 end
793 if gt m div OMX_ULONG_MAX n
794 ret OMX_E_OVERFLOW
795 end
796 ret OMX_OK
797end
798
799fn omx_validate_w m ulong n ulong to int
800 tot = mul m n
801 if gt tot OMX_BUF_MAX
802 ret OMX_E_NOMEM
803 end
804 for i ulong tot bound 16777216
805 v = int get g_omx_w i
806 if lt v -1
807 ret OMX_E_TRIT
808 end
809 if gt v 1
810 ret OMX_E_TRIT
811 end
812 end
813 ret OMX_OK
814end
815
816note Naive exact int32 oracle: yref[0..m) = W[0..m*n) . x[xoff..xoff+n)
817fn omx_oracle m ulong n ulong xoff ulong to int
818 rc = omx_check_shape m n OMX_MAX_N
819 if ne rc 0
820 ret rc
821 end
822 if gt m OMX_Y_MAX
823 ret OMX_E_NOMEM
824 end
825 for i ulong m bound 65536
826 acc = int 0
827 row = mul i n
828 for j ulong n bound 16777216
829 w = int get g_omx_w add row j
830 xv = int get g_omx_x add xoff j
831 acc = add acc mul w xv
832 end
833 put g_omx_yref i acc
834 end
835 ret OMX_OK
836end
837
838note Pack sdot (row-major int8 in g_omx_pack as two's complement uchar)
839fn omx_pack_sdot m ulong n ulong has_w ulong to int
840 omx_plan_zero
841 rc = omx_check_shape m n OMX_MAX_N
842 if ne rc 0
843 ret rc
844 end
845 if eq has_w 0
846 ret OMX_E_ARG
847 end
848 rc2 = omx_validate_w m n
849 if ne rc2 0
850 ret rc2
851 end
852 tot = mul m n
853 nnz = ulong 0
854 for i ulong tot bound 16777216
855 wb = get g_omx_w i
856 wi = int wb
857 if ne wi 0
858 nnz = add nnz 1
859 end
860 put g_omx_pack i uchar and wi 255
861 end
862 put g_omx_plan 0 m
863 put g_omx_plan 1 n
864 put g_omx_plan 2 tot
865 put g_omx_plan 3 tot
866 put g_omx_plan 4 nnz
867 put g_omx_plan 5 1
868 put g_omx_plan 6 OMX_CAND_SDOT
869 ret OMX_OK
870end
871
872fn omx_sdot_row row ulong n ulong xoff ulong to int
873 acc = int 0
874 for j ulong n bound 16777216
875 xv = int get g_omx_x add xoff j
876 u = int get g_omx_pack add row j
877 if ge u 128
878 u = sub u 256
879 end
880 acc = add acc mul u xv
881 end
882 ret acc
883end
884
885note Run sdot: reads packed int8 weights from g_omx_pack, x from g_omx_x[xoff..], writes g_omx_y[yoff..]
886fn omx_run_sdot xoff ulong yoff ulong has_x ulong has_y ulong to int
887 if eq has_x 0
888 ret OMX_E_ARG
889 end
890 if eq has_y 0
891 ret OMX_E_ARG
892 end
893 if eq get g_omx_plan 5 0
894 ret OMX_E_ARG
895 end
896 m = get g_omx_plan 0
897 n = get g_omx_plan 1
898 if eq m 0
899 ret OMX_E_ARG
900 end
901 if eq n 0
902 ret OMX_E_ARG
903 end
904 for i ulong m bound 65536
905 put g_omx_y add yoff i omx_sdot_row mul i n n xoff
906 end
907 ret OMX_OK
908end
909
910fn omx_pack_crumb_chunk row ulong dbase ulong n ulong cbase ulong to ulong
911 nnz = ulong 0
912 for j ulong 16
913 bv = ulong 0
914 for q ulong 4
915 col = add add cbase mul q 16 j
916 if lt col n
917 v = int get g_omx_w add row col
918 if ne v 0
919 nnz = add nnz 1
920 end
921 bv = or bv shl ulong and v 3 mul q 2
922 end
923 end
924 put g_omx_pack add dbase j uchar bv
925 end
926 ret nnz
927end
928
929fn omx_pack_crumb_row row ulong dst ulong n ulong chunks ulong to ulong
930 nnz = ulong 0
931 for c ulong chunks bound 262144
932 nnz = add nnz omx_pack_crumb_chunk row add dst mul c 16 n mul c 64
933 end
934 ret nnz
935end
936
937note Pack crumb: 2-bit crumbs in 64-weight chunks of 16 bytes (exact B1/R2c_crumb layout)
938fn omx_pack_crumb m ulong n ulong has_w ulong to int
939 omx_plan_zero
940 rc = omx_check_shape m n OMX_MAX_N
941 if ne rc 0
942 ret rc
943 end
944 if eq has_w 0
945 ret OMX_E_ARG
946 end
947 rc2 = omx_validate_w m n
948 if ne rc2 0
949 ret rc2
950 end
951 chunks = div add n 63 64
952 rbytes = mul chunks 16
953 if ne rbytes 0
954 if gt m div OMX_ULONG_MAX rbytes
955 ret OMX_E_OVERFLOW
956 end
957 end
958 wbytes = mul m rbytes
959 if gt wbytes OMX_BUF_MAX
960 ret OMX_E_NOMEM
961 end
962 nnz = ulong 0
963 for i ulong m bound 65536
964 nnz = add nnz omx_pack_crumb_row mul i n mul i rbytes n chunks
965 end
966 put g_omx_plan 0 m
967 put g_omx_plan 1 n
968 put g_omx_plan 2 wbytes
969 put g_omx_plan 3 wbytes
970 put g_omx_plan 4 nnz
971 put g_omx_plan 5 1
972 put g_omx_plan 6 OMX_CAND_CRUMB
973 ret OMX_OK
974end
975
976fn omx_crumb_sext2 v ulong to int
977 b = and v 3
978 if eq b 3
979 ret -1
980 end
981 if eq b 1
982 ret 1
983 end
984 ret 0
985end
986
987fn omx_run_crumb_chunk sbase ulong n ulong cbase ulong xoff ulong to int
988 acc = int 0
989 for j ulong 16
990 bv = ulong get g_omx_pack add sbase j
991 col = add cbase j
992 if lt col n
993 acc = add acc mul omx_crumb_sext2 bv int get g_omx_x add xoff col
994 end
995 col = add col 16
996 if lt col n
997 acc = add acc mul omx_crumb_sext2 shr bv 2 int get g_omx_x add xoff col
998 end
999 col = add col 16
1000 if lt col n
1001 acc = add acc mul omx_crumb_sext2 shr bv 4 int get g_omx_x add xoff col
1002 end
1003 col = add col 16
1004 if lt col n
1005 acc = add acc mul omx_crumb_sext2 shr bv 6 int get g_omx_x add xoff col
1006 end
1007 end
1008 ret acc
1009end
1010
1011fn omx_run_crumb_row src ulong n ulong chunks ulong xoff ulong to int
1012 acc = int 0
1013 for c ulong chunks bound 262144
1014 acc = add acc omx_run_crumb_chunk add src mul c 16 n mul c 64 xoff
1015 end
1016 ret acc
1017end
1018
1019note Run crumb: decodes 2-bit crumbs from g_omx_pack and computes exact dot product with g_omx_x[xoff..]
1020fn omx_run_crumb xoff ulong yoff ulong has_x ulong has_y ulong to int
1021 if eq has_x 0
1022 ret OMX_E_ARG
1023 end
1024 if eq has_y 0
1025 ret OMX_E_ARG
1026 end
1027 if eq get g_omx_plan 5 0
1028 ret OMX_E_ARG
1029 end
1030 m = get g_omx_plan 0
1031 n = get g_omx_plan 1
1032 if eq m 0
1033 ret OMX_E_ARG
1034 end
1035 if eq n 0
1036 ret OMX_E_ARG
1037 end
1038 chunks = div add n 63 64
1039 rbytes = mul chunks 16
1040 for i ulong m bound 65536
1041 put g_omx_y add yoff i omx_run_crumb_row mul i rbytes n chunks xoff
1042 end
1043 ret OMX_OK
1044end
1045
1046note Dispatch pack/run by candidate kind (0=plain, 1=sdot, 2=crumb, 3=enc_sdot, 4=enc_crumb)
1047fn omx_pack_cand kind ulong m ulong n ulong has_w ulong to int
1048 if or eq kind OMX_CAND_CRUMB eq kind OMX_CAND_ENC_CRUMB
1049 ret omx_pack_crumb m n has_w
1050 end
1051 ret omx_pack_sdot m n has_w
1052end
1053
1054fn omx_run_cand kind ulong xoff ulong yoff ulong has_x ulong has_y ulong to int
1055 if or eq kind OMX_CAND_CRUMB eq kind OMX_CAND_ENC_CRUMB
1056 ret omx_run_crumb xoff yoff has_x has_y
1057 end
1058 ret omx_run_sdot xoff yoff has_x has_y
1059end