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 / turing / ty_energy.om

1673 lines · 34020 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4note
5note src/turing/ty_energy.om -- TURING Yield TY-4/TY-5 records, checks, and
6note OMG0 canonical digests in pure seeded OM-2.
7note Replaces src/turing/ty_energy.{c,h}.
8
9const TY_DIRECT ulong 0
10const TY_COUNTER_DERIVED ulong 1
11const TY_CAUSAL_RUNTIME ulong 2
12const TY_COUNTERFACTUAL ulong 3
13const TY_MODELLED ulong 4
14const TY_UNATTRIBUTED ulong 5
15const TY_CONF_COUNT ulong 6
16
17const TY_FORGE_NOT_MEASURED ulong 0
18const TY_FORGE_MEASURED ulong 1
19const TY_FORGE_ESTIMATED ulong 2
20
21const TYE_OK ulong 0
22const TYE_E_SENSOR ulong 1
23const TYE_E_WRAP ulong 2
24const TYE_E_HEADROOM ulong 3
25const TYE_E_SAMPLING_GAP ulong 4
26const TYE_E_PARTIAL_RUN ulong 5
27const TYE_E_INTERVAL_MISMATCH ulong 6
28const TYE_E_MULTIPLEXED ulong 7
29const TYE_E_OVERLAP ulong 8
30const TYE_E_MISSING_IDLE ulong 9
31const TYE_E_DOUBLE_COUNT ulong 10
32const TYE_E_OVER_ALLOCATED ulong 11
33const TYE_E_NEGATIVE_SHARE ulong 12
34const TYE_E_MISSING_PARTICIPANT ulong 13
35const TYE_E_UNKNOWN_PARTICIPANT ulong 14
36const TYE_E_CONDITION ulong 15
37const TYE_E_ARG ulong 16
38const TYE_STATUS_COUNT ulong 17
39
40const TY_CH_PKG ulong 0
41const TY_CH_CPU_E ulong 1
42const TY_CH_CPU_P ulong 2
43const TY_NCH ulong 3
44const TY_MAX_PART ulong 4
45const TY_MAX_SHARE ulong 8
46const TY_MAX_SRC ulong 8
47const TY_RESOLUTION_UJ ulong 1000
48const TY_SAMPLE_PERIOD_NS ulong 100000000
49const TY_MAX_GAP_NS ulong 300000000
50const TY_WRAP_UJ ulong 4294967296000
51const TY_KIND_EVIDENCE ulong 10
52const TY_REL_DEPENDS_ON ulong 6
53const TY_REL_DERIVED_FROM ulong 7
54
55text t_ty_dom_int "turing.resource_interval.v0"
56text t_ty_dom_att "turing.resource_attribution.v0"
57text t_ty_rec_int "turing.resource_interval"
58text t_ty_rec_att "turing.resource_attribution"
59text t_ty_idle "IDLE"
60text t_ty_c0 "DIRECT"
61text t_ty_c1 "COUNTER_DERIVED"
62text t_ty_c2 "CAUSAL_RUNTIME"
63text t_ty_c3 "COUNTERFACTUAL"
64text t_ty_c4 "MODELLED"
65text t_ty_c5 "UNATTRIBUTED"
66text t_ty_inv "INVALID"
67text t_ty_s0 "OK"
68text t_ty_s1 "SENSOR_UNAVAILABLE"
69text t_ty_s2 "COUNTER_WRAP"
70text t_ty_s3 "WRAP_HEADROOM"
71text t_ty_s4 "SAMPLING_GAP"
72text t_ty_s5 "PARTIAL_RUN"
73text t_ty_s6 "INTERVAL_MISMATCH"
74text t_ty_s7 "PMU_MULTIPLEXED"
75text t_ty_s8 "OVERLAPPING_INTERVALS"
76text t_ty_s9 "MISSING_IDLE_BASELINE"
77text t_ty_s10 "DOUBLE_COUNT"
78text t_ty_s11 "ALLOCATION_EXCEEDS_TOTAL"
79text t_ty_s12 "NEGATIVE_SHARE"
80text t_ty_s13 "MISSING_PARTICIPANT"
81text t_ty_s14 "UNKNOWN_PARTICIPANT"
82text t_ty_s15 "CONDITION_MISMATCH"
83text t_ty_s16 "BAD_ARGUMENT"
84text t_ty_ch0 "pkg"
85text t_ty_ch1 "cpu_e"
86text t_ty_ch2 "cpu_p"
87text t_ty_ch_inv "invalid"
88
89text t_k_record "record"
90text t_k_run "run"
91text t_k_config "config"
92text t_k_cond "cond"
93text t_k_round "round"
94text t_k_pos "pos"
95text t_k_t0 "t0_ns"
96text t_k_t1 "t1_ns"
97text t_k_pl "planned_ns"
98text t_k_sok "sensor_ok0_ok1_overflow"
99text t_k_e0 "e0_uj_pkg_cpue_cpup"
100text t_k_e1 "e1_uj_pkg_cpue_cpup"
101text t_k_ns "n_samples"
102text t_k_mg "max_gap_ns"
103text t_k_res "resolution_uj"
104text t_k_sp "sample_period_ns"
105text t_k_fb "foreign_busy_ms"
106text t_k_mt "max_temp_mc"
107text t_k_p0 "part0"
108text t_k_p1 "part1"
109text t_k_p2 "part2"
110text t_k_p3 "part3"
111text t_k_st "status"
112text t_k_cf "confidence"
113text t_k_fe "forge_energy_source"
114text t_k_meth "method"
115text t_k_chan "channel"
116text t_k_tol "tolerance_uj"
117text t_k_au "all_unattributed"
118text t_k_mu "measured_uj"
119text t_k_al "allocated_uj"
120text t_k_un "unattributed_uj"
121text t_k_sh "shares_who_conf_uj_forge"
122text t_k_vd "verdict"
123text t_k_oc "overall_confidence"
124text t_k_of "overall_forge_energy_source"
125
126note ---- SHA-256 state ----
127global g_tshk uint 72
128global g_tshh uint 8
129global g_tshw uint 64
130global g_tshv uint 8
131global g_tshb uchar 64
132global g_tshm ulong 2
133
134note ---- Unified digest store (1024 slots x 32B = 32768B) ----
135note Slots 0..511: window/test, 512..527: attr.src (2x8), 528..529: attr.measured, 536..551: canon rels
136global g_tdig uchar 32768
137
138note ---- Interval pool (512 slots; each has up to 4 participants -> 2048 participant slots) ----
139global g_tw_run uchar 32768
140global g_tw_cfg uchar 8192
141global g_tw_cnd uchar 4096
142global g_tw_rnd ulong 512
143global g_tw_pos ulong 512
144global g_tw_t0 ulong 512
145global g_tw_t1 ulong 512
146global g_tw_pl ulong 512
147global g_tw_ok0 ulong 512
148global g_tw_ok1 ulong 512
149global g_tw_ovf ulong 512
150global g_tw_e0 ulong 1536
151global g_tw_e1 ulong 1536
152global g_tw_ns ulong 512
153global g_tw_mg ulong 512
154global g_tw_fb ulong 512
155global g_tw_mt ulong 512
156global g_tw_np ulong 512
157
158note Participants (512 * 4 = 2048 slots)
159global g_tp_tag uchar 16384
160global g_tp_rz uchar 65536
161global g_tp_cpu uchar 65536
162global g_tp_go ulong 2048
163global g_tp_t0 ulong 2048
164global g_tp_t1 ulong 2048
165global g_tp_dn ulong 2048
166global g_tp_pl ulong 2048
167global g_tp_cl ulong 2048
168global g_tp_cy ulong 2048
169global g_tp_in ulong 2048
170global g_tp_l2 ulong 2048
171global g_tp_pn ulong 2048
172global g_tp_or ulong 2048
173global g_tp_ex ulong 2048
174global g_tp_pm ulong 2048
175global g_tp_mx ulong 2048
176
177note ---- Attribution pool (2 slots; each has up to 8 shares -> 16 share slots) ----
178global g_ta_mth uchar 64
179global g_ta_ch long 2
180global g_ta_tol ulong 2
181global g_ta_au ulong 2
182global g_ta_nsh ulong 2
183global g_ta_nsr ulong 2
184global g_ta_muj long 2
185global g_ta_auj long 2
186global g_ta_ruj long 2
187global g_ta_vd ulong 2
188global g_ta_ov ulong 2
189
190global g_ts_who uchar 128
191global g_ts_uj long 16
192global g_ts_cf ulong 16
193
194note ---- Canonical OMG0 object builder ----
195global g_ca_n ulong 1
196global g_ca_k uchar 2048
197global g_ca_kl ulong 32
198global g_ca_v uchar 16384
199global g_ca_vl ulong 32
200global g_ca_ord ulong 32
201global g_cr_n ulong 1
202global g_cr_k ulong 16
203global g_cr_ord ulong 16
204global g_cbuf uchar 40000
205global g_clen ulong 1
206global g_tval uchar 512
207global g_tvl ulong 1
208global g_tret long 8
209global g_tidx ulong 512
210
211note ---- FIPS 180-4 SHA-256 ----
212fn tsh_rotr x uint n uint to uint
213 ret or shr x n shl x sub uint 32 n
214end
215
216fn tsh_init_tbl to void
217 if ne get g_tshk 0 0
218 ret
219 end
220 put g_tshk 0 1116352408
221 put g_tshk 1 1899447441
222 put g_tshk 2 3049323471
223 put g_tshk 3 3921009573
224 put g_tshk 4 961987163
225 put g_tshk 5 1508970993
226 put g_tshk 6 2453635748
227 put g_tshk 7 2870763221
228 put g_tshk 8 3624381080
229 put g_tshk 9 310598401
230 put g_tshk 10 607225278
231 put g_tshk 11 1426881987
232 put g_tshk 12 1925078388
233 put g_tshk 13 2162078206
234 put g_tshk 14 2614888103
235 put g_tshk 15 3248222580
236 put g_tshk 16 3835390401
237 put g_tshk 17 4022224774
238 put g_tshk 18 264347078
239 put g_tshk 19 604807628
240 put g_tshk 20 770255983
241 put g_tshk 21 1249150122
242 put g_tshk 22 1555081692
243 put g_tshk 23 1996064986
244 put g_tshk 24 2554220882
245 put g_tshk 25 2821834349
246 put g_tshk 26 2952996808
247 put g_tshk 27 3210313671
248 put g_tshk 28 3336571891
249 put g_tshk 29 3584528711
250 put g_tshk 30 113926993
251 put g_tshk 31 338241895
252 put g_tshk 32 666307205
253 put g_tshk 33 773529912
254 put g_tshk 34 1294757372
255 put g_tshk 35 1396182291
256 put g_tshk 36 1695183700
257 put g_tshk 37 1986661051
258 put g_tshk 38 2177026350
259 put g_tshk 39 2456956037
260 put g_tshk 40 2730485921
261 put g_tshk 41 2820302411
262 put g_tshk 42 3259730800
263 put g_tshk 43 3345764771
264 put g_tshk 44 3516065817
265 put g_tshk 45 3600352804
266 put g_tshk 46 4094571909
267 put g_tshk 47 275423344
268 put g_tshk 48 430227734
269 put g_tshk 49 506948616
270 put g_tshk 50 659060556
271 put g_tshk 51 883997877
272 put g_tshk 52 958139571
273 put g_tshk 53 1322822218
274 put g_tshk 54 1537002063
275 put g_tshk 55 1747873779
276 put g_tshk 56 1955562222
277 put g_tshk 57 2024104815
278 put g_tshk 58 2227730452
279 put g_tshk 59 2361852424
280 put g_tshk 60 2428436474
281 put g_tshk 61 2756734187
282 put g_tshk 62 3204031479
283 put g_tshk 63 3329325298
284 put g_tshk 64 1779033703
285 put g_tshk 65 3144134277
286 put g_tshk 66 1013904242
287 put g_tshk 67 2773480762
288 put g_tshk 68 1359893119
289 put g_tshk 69 2600822924
290 put g_tshk 70 528734635
291 put g_tshk 71 1541459225
292end
293
294fn tsh_load_w to void
295 for i ulong 16
296 b0 = shl uint get g_tshb mul i 4 uint 24
297 b1 = shl uint get g_tshb add mul i 4 1 uint 16
298 b2 = shl uint get g_tshb add mul i 4 2 uint 8
299 b3 = uint get g_tshb add mul i 4 3
300 put g_tshw i or or b0 b1 or b2 b3
301 end
302 i = ulong 16
303 while lt i 64
304 x = get g_tshw sub i 15
305 s0 = xor xor tsh_rotr x uint 7 tsh_rotr x uint 18 shr x uint 3
306 y = get g_tshw sub i 2
307 s1 = xor xor tsh_rotr y uint 17 tsh_rotr y uint 19 shr y uint 10
308 put g_tshw i add add get g_tshw sub i 16 s0 add get g_tshw sub i 7 s1
309 i = add i 1
310 end
311end
312
313fn tsh_round j ulong to void
314 e = get g_tshv 4
315 s1 = xor xor tsh_rotr e uint 6 tsh_rotr e uint 11 tsh_rotr e uint 25
316 ch = xor and e get g_tshv 5 and not e get g_tshv 6
317 t1 = add add add get g_tshv 7 s1 add ch get g_tshk j get g_tshw j
318 a = get g_tshv 0
319 s0 = xor xor tsh_rotr a uint 2 tsh_rotr a uint 13 tsh_rotr a uint 22
320 maj = xor xor and a get g_tshv 1 and a get g_tshv 2 and get g_tshv 1 get g_tshv 2
321 t2 = add s0 maj
322 put g_tshv 7 get g_tshv 6
323 put g_tshv 6 get g_tshv 5
324 put g_tshv 5 e
325 put g_tshv 4 add get g_tshv 3 t1
326 put g_tshv 3 get g_tshv 2
327 put g_tshv 2 get g_tshv 1
328 put g_tshv 1 a
329 put g_tshv 0 add t1 t2
330end
331
332fn tsh_block to void
333 tsh_load_w
334 for j ulong 8
335 put g_tshv j get g_tshh j
336 end
337 for j ulong 64
338 tsh_round j
339 end
340 for j ulong 8
341 put g_tshh j add get g_tshh j get g_tshv j
342 end
343end
344
345fn tsh_init to void
346 tsh_init_tbl
347 for i ulong 8
348 put g_tshh i get g_tshk add i 64
349 end
350 put g_tshm 0 0
351 put g_tshm 1 0
352end
353
354fn tsh_byte b uchar to void
355 bl = get g_tshm 0
356 put g_tshb bl b
357 bl = add bl 1
358 put g_tshm 1 add get g_tshm 1 1
359 if eq bl 64
360 tsh_block
361 bl = 0
362 end
363 put g_tshm 0 bl
364end
365
366fn tsh_txt s view uchar * to void
367 n = len s
368 for i ulong n
369 tsh_byte get s i
370 end
371end
372
373fn tsh_final slot ulong to void
374 tot = get g_tshm 1
375 bits = mul tot 8
376 tsh_byte uchar 128
377 while ne get g_tshm 0 56
378 tsh_byte uchar 0
379 end
380 for i ulong 8
381 sh = mul sub 7 i 8
382 tsh_byte uchar and shr bits sh 255
383 end
384 off = mul slot 32
385 for i ulong 8
386 w = get g_tshh i
387 o = add off mul i 4
388 put g_tdig o uchar and shr w uint 24 uint 255
389 put g_tdig add o 1 uchar and shr w uint 16 uint 255
390 put g_tdig add o 2 uchar and shr w uint 8 uint 255
391 put g_tdig add o 3 uchar and w uint 255
392 end
393end
394
395fn tdg_eq s1 ulong s2 ulong to bool
396 o1 = mul s1 32
397 o2 = mul s2 32
398 for i ulong 32
399 if ne get g_tdig add o1 i get g_tdig add o2 i
400 ret false
401 end
402 end
403 ret true
404end
405
406fn tdg_cp dst ulong src ulong to void
407 o1 = mul dst 32
408 o2 = mul src 32
409 for i ulong 32
410 put g_tdig add o1 i get g_tdig add o2 i
411 end
412end
413
414note ---- Confidence & Status helpers ----
415fn ty_conf_forge c ulong to ulong
416 if or eq c TY_DIRECT eq c TY_COUNTER_DERIVED
417 ret TY_FORGE_MEASURED
418 end
419 if or eq c TY_CAUSAL_RUNTIME or eq c TY_COUNTERFACTUAL eq c TY_MODELLED
420 ret TY_FORGE_ESTIMATED
421 end
422 ret TY_FORGE_NOT_MEASURED
423end
424
425fn tv_clr to void
426 put g_tvl 0 0
427end
428
429fn tv_b c uchar to void
430 n = get g_tvl 0
431 if lt n 512
432 put g_tval n c
433 put g_tvl 0 add n 1
434 end
435end
436
437fn tv_txt s view uchar * to void
438 n = len s
439 for i ulong n
440 tv_b get s i
441 end
442end
443
444fn tv_u64 v ulong to void
445 if eq v 0
446 tv_b uchar 48
447 ret
448 end
449 dig = new uchar 24 0
450 n = ulong 0
451 x = v
452 while gt x 0
453 put dig n uchar add 48 rem x 10
454 x = div x 10
455 n = add n 1
456 end
457 i = n
458 while gt i 0
459 i = sub i 1
460 tv_b get dig i
461 end
462end
463
464fn tv_i64 v long to void
465 if lt v 0
466 tv_b uchar 45
467 note handle INT64_MIN safely
468 u = add not bitcast ulong v 1
469 tv_u64 u
470 ret
471 end
472 tv_u64 ulong v
473end
474
475fn tv_conf c ulong to void
476 if eq c 0
477 tv_txt t_ty_c0
478 ret
479 end
480 if eq c 1
481 tv_txt t_ty_c1
482 ret
483 end
484 if eq c 2
485 tv_txt t_ty_c2
486 ret
487 end
488 if eq c 3
489 tv_txt t_ty_c3
490 ret
491 end
492 if eq c 4
493 tv_txt t_ty_c4
494 ret
495 end
496 if eq c 5
497 tv_txt t_ty_c5
498 ret
499 end
500 tv_txt t_ty_inv
501end
502
503fn tv_status s ulong to void
504 if eq s 0
505 tv_txt t_ty_s0
506 ret
507 end
508 if eq s 1
509 tv_txt t_ty_s1
510 ret
511 end
512 if eq s 2
513 tv_txt t_ty_s2
514 ret
515 end
516 if eq s 3
517 tv_txt t_ty_s3
518 ret
519 end
520 if eq s 4
521 tv_txt t_ty_s4
522 ret
523 end
524 if eq s 5
525 tv_txt t_ty_s5
526 ret
527 end
528 if eq s 6
529 tv_txt t_ty_s6
530 ret
531 end
532 if eq s 7
533 tv_txt t_ty_s7
534 ret
535 end
536 if eq s 8
537 tv_txt t_ty_s8
538 ret
539 end
540 if eq s 9
541 tv_txt t_ty_s9
542 ret
543 end
544 if eq s 10
545 tv_txt t_ty_s10
546 ret
547 end
548 if eq s 11
549 tv_txt t_ty_s11
550 ret
551 end
552 if eq s 12
553 tv_txt t_ty_s12
554 ret
555 end
556 if eq s 13
557 tv_txt t_ty_s13
558 ret
559 end
560 if eq s 14
561 tv_txt t_ty_s14
562 ret
563 end
564 if eq s 15
565 tv_txt t_ty_s15
566 ret
567 end
568 if eq s 16
569 tv_txt t_ty_s16
570 ret
571 end
572 tv_txt t_ty_inv
573end
574
575note ---- C-string helpers in fixed-stride pools ----
576fn tx_ok_at which ulong off ulong cap ulong to bool
577 for i ulong cap
578 c = uchar 0
579 if eq which 0
580 c = get g_tw_run add off i
581 else
582 if eq which 1
583 c = get g_tw_cfg add off i
584 else
585 if eq which 2
586 c = get g_tw_cnd add off i
587 else
588 if eq which 3
589 c = get g_tp_rz add off i
590 else
591 if eq which 4
592 c = get g_tp_cpu add off i
593 else
594 if eq which 5
595 c = get g_ta_mth add off i
596 else
597 c = get g_ts_who add off i
598 end
599 end
600 end
601 end
602 end
603 end
604 if eq c 0
605 ret gt i 0
606 end
607 if eq c 124
608 ret false
609 end
610 end
611 ret false
612end
613
614fn tv_cstr which ulong off ulong cap ulong to void
615 for i ulong cap
616 c = uchar 0
617 if eq which 0
618 c = get g_tw_run add off i
619 else
620 if eq which 1
621 c = get g_tw_cfg add off i
622 else
623 if eq which 2
624 c = get g_tw_cnd add off i
625 else
626 if eq which 3
627 c = get g_tp_rz add off i
628 else
629 if eq which 4
630 c = get g_tp_cpu add off i
631 else
632 if eq which 5
633 c = get g_ta_mth add off i
634 else
635 if eq which 6
636 c = get g_ts_who add off i
637 else
638 c = get g_tp_tag add off i
639 end
640 end
641 end
642 end
643 end
644 end
645 end
646 if eq c 0
647 ret
648 end
649 tv_b c
650 end
651end
652
653fn cnd_len w ulong to ulong
654 off = mul w 8
655 for i ulong 8
656 if eq get g_tw_cnd add off i 0
657 ret i
658 end
659 end
660 ret 8
661end
662
663fn cnd_eq_txt w ulong want view uchar * to bool
664 n = cnd_len w
665 if ne n len want
666 ret false
667 end
668 off = mul w 8
669 for i ulong n
670 if ne get g_tw_cnd add off i get want i
671 ret false
672 end
673 end
674 ret true
675end
676
677fn cstr_eq_pool which ulong o1 ulong o2 ulong cap ulong to bool
678 for i ulong cap
679 a = uchar 0
680 b = uchar 0
681 if eq which 0
682 a = get g_tw_run add o1 i
683 b = get g_tw_run add o2 i
684 else
685 if eq which 1
686 a = get g_tw_cfg add o1 i
687 b = get g_tw_cfg add o2 i
688 else
689 a = get g_ts_who add o1 i
690 b = get g_ts_who add o2 i
691 end
692 end
693 if ne a b
694 ret false
695 end
696 if eq a 0
697 ret true
698 end
699 end
700 ret true
701end
702
703fn who_eq_idle sh ulong to bool
704 off = mul sh 8
705 for i ulong 4
706 if ne get g_ts_who add off i get t_ty_idle i
707 ret false
708 end
709 end
710 ret eq get g_ts_who add off 4 0
711end
712
713fn has_tag_byte w ulong ch uchar to bool
714 np = get g_tw_np w
715 pbo = mul w 4
716 for i ulong np
717 po = mul add pbo i 8
718 c0 = get g_tp_tag po
719 c1 = get g_tp_tag add po 1
720 if and eq c0 ch eq c1 0
721 ret true
722 end
723 end
724 ret false
725end
726
727fn who_matches_part w ulong sh ulong to bool
728 off = mul sh 8
729 if eq get g_ts_who off 0
730 ret false
731 end
732 if ne get g_ts_who add off 1 0
733 ret false
734 end
735 ret has_tag_byte w get g_ts_who off
736end
737
738note ---- Window validity & energy ----
739fn ty_check_part_str pbo ulong i ulong to bool
740 p = add pbo i
741 toff = mul p 8
742 c = get g_tp_tag toff
743 c1 = get g_tp_tag add toff 1
744 if or lt c 65 or gt c 68 ne c1 0
745 ret false
746 end
747 if not tx_ok_at 3 mul p 32 32
748 ret false
749 end
750 if not tx_ok_at 4 mul p 32 32
751 ret false
752 end
753 for j ulong i
754 cj = get g_tp_tag mul add pbo j 8
755 if eq c cj
756 ret false
757 end
758 end
759 ret true
760end
761
762fn ty_shape_ok w ulong to bool
763 np = get g_tw_np w
764 if gt np TY_MAX_PART
765 ret false
766 end
767 if not tx_ok_at 0 mul w 64 64
768 ret false
769 end
770 if not tx_ok_at 1 mul w 16 16
771 ret false
772 end
773 if not tx_ok_at 2 mul w 8 8
774 ret false
775 end
776 pbo = mul w 4
777 for i ulong np
778 if not ty_check_part_str pbo i
779 ret false
780 end
781 end
782 ret true
783end
784
785fn ty_check_cond w ulong to ulong
786 if cnd_eq_txt w t_ty_idle
787 if eq get g_tw_np w 0
788 ret TYE_OK
789 end
790 ret TYE_E_CONDITION
791 end
792 want = cnd_len w
793 if ne want get g_tw_np w
794 ret TYE_E_CONDITION
795 end
796 off = mul w 8
797 for i ulong want
798 c = get g_tw_cnd add off i
799 if or lt c 65 gt c 68
800 ret TYE_E_CONDITION
801 end
802 for j ulong i
803 cj = get g_tw_cnd add off j
804 if eq c cj
805 ret TYE_E_CONDITION
806 end
807 end
808 if not has_tag_byte w c
809 ret TYE_E_CONDITION
810 end
811 end
812 ret TYE_OK
813end
814
815note 128-bit compare a1*a2 < b1*b2 using 64x64->128 split (32-bit halves)
816global g_m128 ulong 8
817
818fn mul128 a ulong b ulong dst ulong to void
819 put g_m128 4 mul and a 0xffffffff and b 0xffffffff
820 put g_m128 5 mul and a 0xffffffff shr b 32
821 put g_m128 6 mul shr a 32 and b 0xffffffff
822 put g_m128 7 mul shr a 32 shr b 32
823 mid = add add shr get g_m128 4 32 and get g_m128 5 0xffffffff and get g_m128 6 0xffffffff
824 put g_m128 dst or shl and mid 0xffffffff 32 and get g_m128 4 0xffffffff
825 hi = add add add get g_m128 7 shr get g_m128 5 32 shr get g_m128 6 32 shr mid 32
826 put g_m128 add dst 1 hi
827end
828
829fn lt128 a1 ulong a2 ulong b1 ulong b2 ulong to bool
830 mul128 a1 a2 0
831 mul128 b1 b2 2
832 h1 = get g_m128 1
833 h2 = get g_m128 3
834 if lt h1 h2
835 ret true
836 end
837 if gt h1 h2
838 ret false
839 end
840 ret lt get g_m128 0 get g_m128 2
841end
842
843fn ty_check_part w ulong p ulong to ulong
844 if eq get g_tp_ex p 0
845 ret TYE_E_PARTIAL_RUN
846 end
847 if eq get g_tp_or p 0
848 ret TYE_E_PARTIAL_RUN
849 end
850 if eq get g_tp_pn p 0
851 ret TYE_E_PARTIAL_RUN
852 end
853 if eq get g_tp_cl p 0
854 ret TYE_E_PARTIAL_RUN
855 end
856 pl = get g_tw_pl w
857 if ne get g_tp_pl p pl
858 ret TYE_E_INTERVAL_MISMATCH
859 end
860 go = get g_tp_go p
861 t0 = get g_tp_t0 p
862 t1 = get g_tp_t1 p
863 dn = get g_tp_dn p
864 if lt go get g_tw_t0 w
865 ret TYE_E_INTERVAL_MISMATCH
866 end
867 if lt t0 go
868 ret TYE_E_INTERVAL_MISMATCH
869 end
870 if lt t1 t0
871 ret TYE_E_INTERVAL_MISMATCH
872 end
873 if lt dn t1
874 ret TYE_E_INTERVAL_MISMATCH
875 end
876 if gt dn get g_tw_t1 w
877 ret TYE_E_INTERVAL_MISMATCH
878 end
879 if lt128 sub t1 t0 50 pl 49
880 ret TYE_E_PARTIAL_RUN
881 end
882 if eq get g_tp_pm p 0
883 ret TYE_E_SENSOR
884 end
885 if ne get g_tp_mx p 0
886 ret TYE_E_MULTIPLEXED
887 end
888 ret TYE_OK
889end
890
891fn ty_interval_check w ulong to ulong
892 if not ty_shape_ok w
893 ret TYE_E_ARG
894 end
895 pl = get g_tw_pl w
896 if or eq pl 0 le get g_tw_t1 w get g_tw_t0 w
897 ret TYE_E_ARG
898 end
899 if or eq get g_tw_ok0 w 0 eq get g_tw_ok1 w 0
900 ret TYE_E_SENSOR
901 end
902 if ne get g_tw_ovf w 0
903 ret TYE_E_WRAP
904 end
905 ebo = mul w 3
906 for c ulong TY_NCH
907 if lt get g_tw_e1 add ebo c get g_tw_e0 add ebo c
908 ret TYE_E_WRAP
909 end
910 end
911 for c ulong TY_NCH
912 e0 = get g_tw_e0 add ebo c
913 e1 = get g_tw_e1 add ebo c
914 if ge e1 TY_WRAP_UJ
915 ret TYE_E_HEADROOM
916 end
917 if le sub TY_WRAP_UJ e1 sub e1 e0
918 ret TYE_E_HEADROOM
919 end
920 end
921 dt = sub get g_tw_t1 w get g_tw_t0 w
922 ns = get g_tw_ns w
923 if gt get g_tw_mg w TY_MAX_GAP_NS
924 ret TYE_E_SAMPLING_GAP
925 end
926 if lt128 ns mul 10 TY_SAMPLE_PERIOD_NS dt 9
927 ret TYE_E_SAMPLING_GAP
928 end
929 if gt ns add div dt TY_SAMPLE_PERIOD_NS 2
930 ret TYE_E_SAMPLING_GAP
931 end
932 sc = ty_check_cond w
933 if ne sc TYE_OK
934 ret sc
935 end
936 if or lt dt pl gt sub dt pl div pl 20
937 ret TYE_E_INTERVAL_MISMATCH
938 end
939 np = get g_tw_np w
940 pbo = mul w 4
941 for i ulong np
942 sp = ty_check_part w add pbo i
943 if ne sp TYE_OK
944 ret sp
945 end
946 end
947 ret TYE_OK
948end
949
950note Compute floor(de * pl_us / dt_us) safely via 128-bit division (shift-subtract)
951fn div128_by_u64 lo ulong hi ulong d ulong to ulong
952 q = ulong 0
953 r = hi
954 for i ulong 64
955 sh = sub 63 i
956 bit = and shr lo sh 1
957 r_hi = shr r 63
958 r = or shl r 1 bit
959 if or ne r_hi 0 ge r d
960 r = sub r d
961 q = or q shl ulong 1 sh
962 end
963 end
964 ret q
965end
966
967fn ty_interval_energy w ulong ch long to ulong
968 if or lt ch 0 ge ch long TY_NCH
969 ret TYE_E_ARG
970 end
971 s = ty_interval_check w
972 if ne s TYE_OK
973 ret s
974 end
975 ebo = mul w 3
976 de = sub get g_tw_e1 add ebo ulong ch get g_tw_e0 add ebo ulong ch
977 dt_us = div sub get g_tw_t1 w get g_tw_t0 w 1000
978 if or eq dt_us 0 gt de 1000000000000
979 ret TYE_E_ARG
980 end
981 pl_us = div get g_tw_pl w 1000
982 mul128 de pl_us 0
983 if ge get g_m128 1 dt_us
984 ret TYE_E_ARG
985 end
986 sc = div128_by_u64 get g_m128 0 get g_m128 1 dt_us
987 if gt sc 0x7fffffffffffffff
988 ret TYE_E_ARG
989 end
990 put g_tret 0 long sc
991 ret TYE_OK
992end
993
994fn ty_intervals_disjoint n ulong to ulong
995 for i ulong n
996 wi = get g_tidx i
997 if le get g_tw_t1 wi get g_tw_t0 wi
998 ret TYE_E_ARG
999 end
1000 end
1001 for i ulong n
1002 wi = get g_tidx i
1003 j = add i 1
1004 while lt j n
1005 wj = get g_tidx j
1006 if lt get g_tw_t0 wi get g_tw_t1 wj
1007 if lt get g_tw_t0 wj get g_tw_t1 wi
1008 ret TYE_E_OVERLAP
1009 end
1010 end
1011 j = add j 1
1012 end
1013 end
1014 ret TYE_OK
1015end
1016
1017text t_ty_cA "A"
1018text t_ty_cB "B"
1019text t_ty_cAB "AB"
1020
1021fn ty_inter_cond k ulong wk ulong to bool
1022 if eq k 0
1023 ret cnd_eq_txt wk t_ty_idle
1024 end
1025 if eq k 1
1026 ret cnd_eq_txt wk t_ty_cA
1027 end
1028 if eq k 2
1029 ret cnd_eq_txt wk t_ty_cB
1030 end
1031 ret cnd_eq_txt wk t_ty_cAB
1032end
1033
1034fn ty_inter_match k ulong wk ulong wab ulong to bool
1035 if not ty_inter_cond k wk
1036 ret false
1037 end
1038 if ne get g_tw_rnd wk get g_tw_rnd wab
1039 ret false
1040 end
1041 if ne get g_tw_pl wk get g_tw_pl wab
1042 ret false
1043 end
1044 if not cstr_eq_pool 1 mul wk 16 mul wab 16 16
1045 ret false
1046 end
1047 ret cstr_eq_pool 0 mul wk 64 mul wab 64 64
1048end
1049
1050fn ty_interaction wi ulong wa ulong wb ulong wab ulong ch long to ulong
1051 put g_tidx 0 wi
1052 put g_tidx 1 wa
1053 put g_tidx 2 wb
1054 put g_tidx 3 wab
1055 for k ulong 4
1056 wk = get g_tidx k
1057 s = ty_interval_energy wk ch
1058 if ne s TYE_OK
1059 ret s
1060 end
1061 put g_tret add 4 k get g_tret 0
1062 if not ty_inter_match k wk wab
1063 if and eq k 0 not cnd_eq_txt wk t_ty_idle
1064 ret TYE_E_MISSING_IDLE
1065 end
1066 ret TYE_E_CONDITION
1067 end
1068 end
1069 sd = ty_intervals_disjoint 4
1070 if ne sd TYE_OK
1071 ret sd
1072 end
1073 e0 = get g_tret 4
1074 e1 = get g_tret 5
1075 e2 = get g_tret 6
1076 e3 = get g_tret 7
1077 put g_tret 0 add sub sub e3 e1 e2 e0
1078 ret TYE_OK
1079end
1080
1081note ---- Canonical OMG0 encoder & digests ----
1082fn ca_reset to void
1083 put g_ca_n 0 0
1084 put g_cr_n 0 0
1085end
1086
1087fn ca_add_attr k view uchar * to void
1088 idx = get g_ca_n 0
1089 kl = len k
1090 put g_ca_kl idx kl
1091 ko = mul idx 64
1092 for i ulong kl
1093 put g_ca_k add ko i get k i
1094 end
1095 vl = get g_tvl 0
1096 put g_ca_vl idx vl
1097 vo = mul idx 512
1098 for i ulong vl
1099 put g_ca_v add vo i get g_tval i
1100 end
1101 put g_ca_n 0 add idx 1
1102end
1103
1104fn ca_add_txt k view uchar * v view uchar * to void
1105 tv_clr
1106 tv_txt v
1107 ca_add_attr k
1108end
1109
1110fn ca_add_u64 k view uchar * v ulong to void
1111 tv_clr
1112 tv_u64 v
1113 ca_add_attr k
1114end
1115
1116fn ca_add_i64 k view uchar * v long to void
1117 tv_clr
1118 tv_i64 v
1119 ca_add_attr k
1120end
1121
1122fn ca_add_rel kind ulong dslot ulong to void
1123 idx = get g_cr_n 0
1124 put g_cr_k idx kind
1125 tdg_cp add 536 idx dslot
1126 put g_cr_n 0 add idx 1
1127end
1128
1129fn ca_key_lt a ulong b ulong to bool
1130 la = get g_ca_kl a
1131 lb = get g_ca_kl b
1132 mn = la
1133 if lt lb mn
1134 mn = lb
1135 end
1136 oa = mul a 64
1137 ob = mul b 64
1138 for i ulong mn
1139 ca = get g_ca_k add oa i
1140 cb = get g_ca_k add ob i
1141 if lt ca cb
1142 ret true
1143 end
1144 if gt ca cb
1145 ret false
1146 end
1147 end
1148 ret lt la lb
1149end
1150
1151fn ca_rel_lt a ulong b ulong to bool
1152 ka = get g_cr_k a
1153 kb = get g_cr_k b
1154 if lt ka kb
1155 ret true
1156 end
1157 if gt ka kb
1158 ret false
1159 end
1160 oa = mul add 536 a 32
1161 ob = mul add 536 b 32
1162 for i ulong 32
1163 ca = get g_tdig add oa i
1164 cb = get g_tdig add ob i
1165 if lt ca cb
1166 ret true
1167 end
1168 if gt ca cb
1169 ret false
1170 end
1171 end
1172 ret false
1173end
1174
1175fn cb_u8 b uchar to void
1176 p = get g_clen 0
1177 put g_cbuf p b
1178 put g_clen 0 add p 1
1179end
1180
1181fn cb_u16be v ulong to void
1182 cb_u8 uchar and shr v 8 255
1183 cb_u8 uchar and v 255
1184end
1185
1186fn cb_u32be v ulong to void
1187 cb_u8 uchar and shr v 24 255
1188 cb_u8 uchar and shr v 16 255
1189 cb_u8 uchar and shr v 8 255
1190 cb_u8 uchar and v 255
1191end
1192
1193fn ca_sort_attrs to void
1194 na = get g_ca_n 0
1195 for i ulong na
1196 put g_ca_ord i i
1197 end
1198 i = ulong 1
1199 while lt i na
1200 key = get g_ca_ord i
1201 j = i
1202 while gt j 0
1203 prev = get g_ca_ord sub j 1
1204 if ca_key_lt key prev
1205 put g_ca_ord j prev
1206 j = sub j 1
1207 else
1208 break
1209 end
1210 end
1211 put g_ca_ord j key
1212 i = add i 1
1213 end
1214end
1215
1216fn ca_sort_rels to void
1217 nr = get g_cr_n 0
1218 for i ulong nr
1219 put g_cr_ord i i
1220 end
1221 i = ulong 1
1222 while lt i nr
1223 key = get g_cr_ord i
1224 j = i
1225 while gt j 0
1226 prev = get g_cr_ord sub j 1
1227 if ca_rel_lt key prev
1228 put g_cr_ord j prev
1229 j = sub j 1
1230 else
1231 break
1232 end
1233 end
1234 put g_cr_ord j key
1235 i = add i 1
1236 end
1237end
1238
1239fn ca_emit_attr ai ulong to void
1240 kl = get g_ca_kl ai
1241 cb_u8 uchar kl
1242 ko = mul ai 64
1243 for j ulong kl
1244 cb_u8 get g_ca_k add ko j
1245 end
1246 vl = get g_ca_vl ai
1247 cb_u16be vl
1248 vo = mul ai 512
1249 for j ulong vl
1250 cb_u8 get g_ca_v add vo j
1251 end
1252end
1253
1254fn ca_emit_rel ri ulong to void
1255 cb_u16be get g_cr_k ri
1256 ro = mul add 536 ri 32
1257 for j ulong 32
1258 cb_u8 get g_tdig add ro j
1259 end
1260end
1261
1262fn ca_finish dom view uchar * dslot ulong to long
1263 ca_sort_attrs
1264 ca_sort_rels
1265 put g_clen 0 0
1266 cb_u8 uchar 79
1267 cb_u8 uchar 77
1268 cb_u8 uchar 71
1269 cb_u8 uchar 48
1270 cb_u8 uchar 1
1271 cb_u8 uchar TY_KIND_EVIDENCE
1272 na = get g_ca_n 0
1273 cb_u16be na
1274 for i ulong na
1275 ca_emit_attr get g_ca_ord i
1276 end
1277 nr = get g_cr_n 0
1278 cb_u16be nr
1279 for i ulong nr
1280 ca_emit_rel get g_cr_ord i
1281 end
1282 cb_u16be 0
1283 cb_u32be 0
1284 tsh_init
1285 tsh_txt dom
1286 tsh_byte uchar 0
1287 n = get g_clen 0
1288 for i ulong n
1289 tsh_byte get g_cbuf i
1290 end
1291 tsh_final dslot
1292 ret 0
1293end
1294
1295fn ty_add_part_attr w ulong i ulong to void
1296 p = add mul w 4 i
1297 tv_clr
1298 tv_cstr 7 mul p 8 8
1299 tv_b uchar 124
1300 tv_cstr 3 mul p 32 32
1301 tv_b uchar 124
1302 tv_cstr 4 mul p 32 32
1303 tv_b uchar 124
1304 tv_u64 get g_tp_go p
1305 tv_b uchar 124
1306 tv_u64 get g_tp_t0 p
1307 tv_b uchar 124
1308 tv_u64 get g_tp_t1 p
1309 tv_b uchar 124
1310 tv_u64 get g_tp_dn p
1311 tv_b uchar 124
1312 tv_u64 get g_tp_pl p
1313 tv_b uchar 124
1314 tv_u64 get g_tp_cl p
1315 tv_b uchar 124
1316 tv_u64 get g_tp_cy p
1317 tv_b uchar 124
1318 tv_u64 get g_tp_in p
1319 tv_b uchar 124
1320 tv_u64 get g_tp_l2 p
1321 tv_b uchar 124
1322 tv_u64 get g_tp_pn p
1323 tv_b uchar 124
1324 tv_u64 get g_tp_or p
1325 tv_b uchar 124
1326 tv_u64 get g_tp_ex p
1327 tv_b uchar 124
1328 tv_u64 get g_tp_pm p
1329 tv_b uchar 124
1330 tv_u64 get g_tp_mx p
1331 if eq i 0
1332 ca_add_attr t_k_p0
1333 ret
1334 end
1335 if eq i 1
1336 ca_add_attr t_k_p1
1337 ret
1338 end
1339 if eq i 2
1340 ca_add_attr t_k_p2
1341 ret
1342 end
1343 ca_add_attr t_k_p3
1344end
1345
1346fn ty_interval_digest w ulong dslot ulong to long
1347 if not ty_shape_ok w
1348 ret -1
1349 end
1350 ca_reset
1351 ca_add_txt t_k_record t_ty_rec_int
1352 tv_clr
1353 tv_cstr 0 mul w 64 64
1354 ca_add_attr t_k_run
1355 tv_clr
1356 tv_cstr 1 mul w 16 16
1357 ca_add_attr t_k_config
1358 tv_clr
1359 tv_cstr 2 mul w 8 8
1360 ca_add_attr t_k_cond
1361 ca_add_u64 t_k_round get g_tw_rnd w
1362 ca_add_u64 t_k_pos get g_tw_pos w
1363 ca_add_u64 t_k_t0 get g_tw_t0 w
1364 ca_add_u64 t_k_t1 get g_tw_t1 w
1365 ca_add_u64 t_k_pl get g_tw_pl w
1366 tv_clr
1367 tv_u64 get g_tw_ok0 w
1368 tv_b uchar 44
1369 tv_u64 get g_tw_ok1 w
1370 tv_b uchar 44
1371 tv_u64 get g_tw_ovf w
1372 ca_add_attr t_k_sok
1373 ebo = mul w 3
1374 tv_clr
1375 tv_u64 get g_tw_e0 ebo
1376 tv_b uchar 44
1377 tv_u64 get g_tw_e0 add ebo 1
1378 tv_b uchar 44
1379 tv_u64 get g_tw_e0 add ebo 2
1380 ca_add_attr t_k_e0
1381 tv_clr
1382 tv_u64 get g_tw_e1 ebo
1383 tv_b uchar 44
1384 tv_u64 get g_tw_e1 add ebo 1
1385 tv_b uchar 44
1386 tv_u64 get g_tw_e1 add ebo 2
1387 ca_add_attr t_k_e1
1388 ca_add_u64 t_k_ns get g_tw_ns w
1389 ca_add_u64 t_k_mg get g_tw_mg w
1390 ca_add_u64 t_k_res TY_RESOLUTION_UJ
1391 ca_add_u64 t_k_sp TY_SAMPLE_PERIOD_NS
1392 ca_add_u64 t_k_fb get g_tw_fb w
1393 ca_add_u64 t_k_mt get g_tw_mt w
1394 for i ulong get g_tw_np w
1395 ty_add_part_attr w i
1396 end
1397 st = ty_interval_check w
1398 cf = TY_UNATTRIBUTED
1399 if eq st TYE_OK
1400 cf = TY_DIRECT
1401 end
1402 tv_clr
1403 tv_status st
1404 ca_add_attr t_k_st
1405 tv_clr
1406 tv_conf cf
1407 ca_add_attr t_k_cf
1408 ca_add_u64 t_k_fe ty_conf_forge cf
1409 ret ca_finish t_ty_dom_int dslot
1410end
1411
1412note Signed 64-bit overflow-checked add/sub
1413fn i64_add_ovf a long b long to bool
1414 if gt b 0
1415 if gt a sub 0x7fffffffffffffff b
1416 ret true
1417 end
1418 end
1419 mn = bitcast long or shl ulong 0x80000000 32 ulong 0
1420 if lt b 0
1421 if lt a sub mn b
1422 ret true
1423 end
1424 end
1425 ret false
1426end
1427
1428fn i64_sub_ovf a long b long to bool
1429 if lt b 0
1430 if gt a add 0x7fffffffffffffff b
1431 ret true
1432 end
1433 end
1434 mn = bitcast long or shl ulong 0x80000000 32 ulong 0
1435 if gt b 0
1436 if lt a add mn b
1437 ret true
1438 end
1439 end
1440 ret false
1441end
1442
1443fn ty_attr_has_dup a ulong to bool
1444 nsh = get g_ta_nsh a
1445 sbo = mul a 8
1446 for i ulong nsh
1447 si = add sbo i
1448 j = add i 1
1449 while lt j nsh
1450 sj = add sbo j
1451 if cstr_eq_pool 2 mul si 8 mul sj 8 8
1452 ret true
1453 end
1454 j = add j 1
1455 end
1456 end
1457 ret false
1458end
1459
1460fn ty_attr_has_tag a ulong ptag uchar to bool
1461 nsh = get g_ta_nsh a
1462 sbo = mul a 8
1463 for j ulong nsh
1464 off = mul add sbo j 8
1465 if eq get g_ts_who off ptag
1466 if eq get g_ts_who add off 1 0
1467 ret true
1468 end
1469 end
1470 end
1471 ret false
1472end
1473
1474fn ty_attr_missing a ulong w ulong to bool
1475 np = get g_tw_np w
1476 pbo = mul w 4
1477 for i ulong np
1478 ptag = get g_tp_tag mul add pbo i 8
1479 if not ty_attr_has_tag a ptag
1480 ret true
1481 end
1482 end
1483 ret false
1484end
1485
1486fn ty_attr_check_shares a ulong w ulong tol long to ulong
1487 if ty_attr_has_dup a
1488 ret TYE_E_DOUBLE_COUNT
1489 end
1490 nsh = get g_ta_nsh a
1491 sbo = mul a 8
1492 idle = ulong 0
1493 wk = TY_DIRECT
1494 alloc = long 0
1495 for i ulong nsh
1496 si = add sbo i
1497 cf = get g_ts_cf si
1498 if ge cf TY_UNATTRIBUTED
1499 ret TYE_E_ARG
1500 end
1501 if who_eq_idle si
1502 idle = 1
1503 else
1504 if not who_matches_part w si
1505 ret TYE_E_UNKNOWN_PARTICIPANT
1506 end
1507 end
1508 uj = get g_ts_uj si
1509 if lt uj sub 0 tol
1510 ret TYE_E_NEGATIVE_SHARE
1511 end
1512 if gt cf wk
1513 wk = cf
1514 end
1515 if i64_add_ovf alloc uj
1516 ret TYE_E_ARG
1517 end
1518 alloc = add alloc uj
1519 end
1520 put g_ta_auj a alloc
1521 if eq idle 0
1522 ret TYE_E_MISSING_IDLE
1523 end
1524 if ty_attr_missing a w
1525 ret TYE_E_MISSING_PARTICIPANT
1526 end
1527 muj = get g_ta_muj a
1528 if i64_sub_ovf muj alloc
1529 ret TYE_E_ARG
1530 end
1531 ruj = sub muj alloc
1532 put g_ta_ruj a ruj
1533 put g_ta_ov a wk
1534 if lt ruj sub 0 tol
1535 ret TYE_E_OVER_ALLOCATED
1536 end
1537 ret TYE_OK
1538end
1539
1540fn ty_attribution_check a ulong w ulong to ulong
1541 put g_ta_vd a TYE_E_ARG
1542 nsh = get g_ta_nsh a
1543 nsr = get g_ta_nsr a
1544 ch = get g_ta_ch a
1545 if or gt nsh TY_MAX_SHARE or gt nsr TY_MAX_SRC or lt ch 0 ge ch long TY_NCH
1546 ret TYE_E_ARG
1547 end
1548 if or not tx_ok_at 5 mul a 32 32 gt get g_ta_tol a 0x7fffffffffffffff
1549 ret TYE_E_ARG
1550 end
1551 sbo = mul a 8
1552 for i ulong nsh
1553 if not tx_ok_at 6 mul add sbo i 8 8
1554 ret TYE_E_ARG
1555 end
1556 end
1557 put g_ta_auj a 0
1558 put g_ta_ruj a 0
1559 put g_ta_muj a 0
1560 put g_ta_ov a TY_UNATTRIBUTED
1561 s = ty_interval_energy w ch
1562 if eq s TYE_OK
1563 put g_ta_muj a get g_tret 0
1564 if ne ty_interval_digest w add 528 a 0
1565 s = TYE_E_ARG
1566 end
1567 end
1568 if ne s TYE_OK
1569 put g_ta_vd a s
1570 ret s
1571 end
1572 srbo = add 512 mul a 8
1573 for i ulong nsr
1574 if tdg_eq add srbo i add 528 a
1575 put g_ta_vd a TYE_E_DOUBLE_COUNT
1576 ret TYE_E_DOUBLE_COUNT
1577 end
1578 j = add i 1
1579 while lt j nsr
1580 if tdg_eq add srbo i add srbo j
1581 put g_ta_vd a TYE_E_DOUBLE_COUNT
1582 ret TYE_E_DOUBLE_COUNT
1583 end
1584 j = add j 1
1585 end
1586 end
1587 if ne get g_ta_au a 0
1588 if ne nsh 0
1589 put g_ta_vd a TYE_E_ARG
1590 ret TYE_E_ARG
1591 end
1592 put g_ta_ruj a get g_ta_muj a
1593 put g_ta_vd a TYE_OK
1594 ret TYE_OK
1595 end
1596 v = ty_attr_check_shares a w long get g_ta_tol a
1597 put g_ta_vd a v
1598 ret v
1599end
1600
1601fn ty_attribution_digest a ulong dslot ulong to long
1602 nsh = get g_ta_nsh a
1603 nsr = get g_ta_nsr a
1604 if or not tx_ok_at 5 mul a 32 32 or gt nsh TY_MAX_SHARE gt nsr TY_MAX_SRC
1605 ret -1
1606 end
1607 sbo = mul a 8
1608 for i ulong nsh
1609 if not tx_ok_at 6 mul add sbo i 8 8
1610 ret -1
1611 end
1612 end
1613 ca_reset
1614 ca_add_txt t_k_record t_ty_rec_att
1615 tv_clr
1616 tv_cstr 5 mul a 32 32
1617 ca_add_attr t_k_meth
1618 ch = get g_ta_ch a
1619 if eq ch 0
1620 ca_add_txt t_k_chan t_ty_ch0
1621 else
1622 if eq ch 1
1623 ca_add_txt t_k_chan t_ty_ch1
1624 else
1625 if eq ch 2
1626 ca_add_txt t_k_chan t_ty_ch2
1627 else
1628 ca_add_txt t_k_chan t_ty_ch_inv
1629 end
1630 end
1631 end
1632 ca_add_u64 t_k_tol get g_ta_tol a
1633 auv = ulong 0
1634 if ne get g_ta_au a 0
1635 auv = 1
1636 end
1637 ca_add_u64 t_k_au auv
1638 ca_add_i64 t_k_mu get g_ta_muj a
1639 ca_add_i64 t_k_al get g_ta_auj a
1640 ca_add_i64 t_k_un get g_ta_ruj a
1641 tv_clr
1642 for i ulong nsh
1643 si = add sbo i
1644 if gt i 0
1645 tv_b uchar 59
1646 end
1647 tv_cstr 6 mul si 8 8
1648 tv_b uchar 58
1649 tv_conf get g_ts_cf si
1650 tv_b uchar 58
1651 tv_i64 get g_ts_uj si
1652 tv_b uchar 58
1653 tv_u64 ty_conf_forge get g_ts_cf si
1654 end
1655 ca_add_attr t_k_sh
1656 tv_clr
1657 tv_status get g_ta_vd a
1658 ca_add_attr t_k_vd
1659 cf = get g_ta_ov a
1660 if or ne get g_ta_au a 0 ne get g_ta_vd a TYE_OK
1661 cf = TY_UNATTRIBUTED
1662 end
1663 tv_clr
1664 tv_conf cf
1665 ca_add_attr t_k_oc
1666 ca_add_u64 t_k_of ty_conf_forge cf
1667 ca_add_rel TY_REL_DERIVED_FROM add 528 a
1668 srbo = add 512 mul a 8
1669 for i ulong nsr
1670 ca_add_rel TY_REL_DEPENDS_ON add srbo i
1671 end
1672 ret ca_finish t_ty_dom_att dslot
1673end