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 / tools / ty_energy_reduce.om

3611 lines · 72469 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note tools/ty_energy_reduce.om -- TURING Yield TY-4/TY-5 reducer in pure seeded OM-2.
5note Replaces tools/ty_energy_reduce.c.
6note Composed after src/turing/ty_energy.om.
7
8const ER_MAXW ulong 512
9const ER_MAXN ulong 4096
10const ER_RESAMP ulong 10000
11
12const JT_NULL ulong 0
13const JT_BOOL ulong 1
14const JT_NUM ulong 2
15const JT_STR ulong 3
16const JT_ARR ulong 4
17const JT_OBJ ulong 5
18
19text t_er_usg "usage: ty_energy_reduce --check DIR | --stage1 DIR OUT.json | --stage2 DIR OUT.json RECS.jsonl\n"
20text t_er_badarg "bad arguments\n"
21text t_er_cant "cannot read "
22text t_er_nl "\n"
23text t_er_sp " "
24text t_er_sl "/"
25text t_er_col ":"
26text t_er_com ","
27text t_er_qt "\""
28text t_er_lb "{"
29text t_er_rb "}"
30text t_er_lk "["
31text t_er_rk "]"
32text t_er_true "true"
33text t_er_false "false"
34text t_er_null "null"
35text t_er_nan "nan"
36text t_er_inf "inf"
37text t_er_ninf "-inf"
38text t_er_dash "-"
39text t_er_chk "--check"
40text t_er_st1 "--stage1"
41text t_er_st2 "--stage2"
42text t_er_jsonl ".jsonl"
43text t_er_notobj ": not a JSON object\n"
44text t_er_disag ": r15_reduce and ty_energy disagree on the pkg rule\n"
45text t_er_ovl "OVERLAP "
46
47text t_jk_run "run"
48text t_jk_cfg "config"
49text t_jk_lvl "level"
50text t_jk_sa "spec_a"
51text t_jk_sb "spec_b"
52text t_jk_rnd "round"
53text t_jk_trl "trial"
54text t_jk_pl "planned_ns"
55text t_jk_e0 "e0"
56text t_jk_e1 "e1"
57text t_jk_tcpu "tool_cpu"
58text t_jk_mok "meter_ok"
59text t_jk_sok "spbm_ok"
60text t_jk_tns "t_ns"
61text t_jk_euj "energy_uj"
62text t_jk_ovf "overflow"
63text t_jk_tel "tel"
64text t_jk_ok "ok"
65text t_jk_parts "participants"
66text t_jk_res "result"
67text t_jk_tag "tag"
68text t_jk_rz "rz"
69text t_jk_cpus "cpus"
70text t_jk_pin "pinned"
71text t_jk_ook "oracle_ok"
72text t_jk_go "go_ns"
73text t_jk_t0 "t0_ns"
74text t_jk_t1 "t1_ns"
75text t_jk_dn "done_ns"
76text t_jk_cl "calls"
77text t_jk_pmu "pmu"
78text t_jk_sum "sum"
79text t_jk_pop "pmu_open"
80text t_jk_cyc "cpu_cycles"
81text t_jk_ins "inst_retired"
82text t_jk_l2 "l2d_cache_refill"
83text t_jk_val "value"
84text t_jk_en "enabled"
85text t_jk_ru "running"
86text t_jk_s0 "s0"
87text t_jk_s1 "s1"
88text t_jk_bj "busy_jiffies"
89text t_jk_tm "temp_mc"
90
91text t_c_idle "IDLE"
92text t_c_a "A"
93text t_c_b "B"
94text t_c_ab "AB"
95text t_ch_pkg "pkg"
96text t_ch_ce "cpu_e"
97text t_ch_cp "cpu_p"
98text t_ch_rs "rest"
99text t_m_solo "solo_counterfactual.v0"
100text t_m_mod "counter_model.v0"
101text t_w_pref "WINDOW "
102text t_w_r " r"
103text t_w_pkg " pkg_uj="
104text t_w_smp " samples="
105text t_w_gap " gap_ms="
106text t_w_fb " foreign_busy_ms="
107text t_w_dig " digest="
108text t_r_pref "ROUND "
109text t_r_ip " I_pkg_J="
110text t_r_b95 " bound95_J="
111text t_a_pref "ATTR "
112text t_a_ms " measured="
113text t_a_al " allocated="
114text t_a_un " unattributed="
115text t_a_tl " tol="
116text t_a_ov " overall="
117
118text t_s1_proto "docs/turing/TURING_YIELD_ENERGY_PROTOCOL_V0.md section 5"
119text t_s2_proto "docs/turing/TURING_YIELD_ENERGY_PROTOCOL_V0.md sections 9-12"
120text t_s1_rule "max score is the contention case; REAL iff score >= 0.05 and every round >= 0.02; control = min |score|"
121text t_s2_wrap "32-bit mJ counters, about 4.29 MJ; overflow_raw flags refused"
122text t_s2_acc "not claimed: no external calibration; 1 mJ step is resolution only"
123text t_s2_fi0 "1, cpu_cycles/1e9, inst_retired/1e9"
124text t_s2_fi1 "1, cpu_cycles/1e9, l2d_cache_refill/1e9"
125text t_s2_na "E_A + E_B - E_0; held-out error = I (see check_a)"
126text t_ok_str "OK"
127text t_ref_str "REFUSED"
128text t_inst_s "inst"
129text t_l2_s "l2"
130
131text t_ok_proto "protocol"
132text t_ok_cfgs "configs"
133text t_ok_cfg "config"
134text t_ok_rnds "rounds"
135text t_ok_rnd "round"
136text t_ok_st "status"
137text t_ok_sla "slowdown_A"
138text t_ok_slb "slowdown_B"
139text t_ok_sc "score"
140text t_ok_ipj "I_pkg_J"
141text t_ok_icj "I_cpu_p_J"
142text t_ok_irj "I_rest_J"
143text t_ok_vr "valid_rounds"
144text t_ok_ipm "I_pkg_mean_J"
145text t_ok_iplo "I_pkg_t95_lo_J"
146text t_ok_iphi "I_pkg_t95_hi_J"
147text t_ok_sa "spec_a"
148text t_ok_sb "spec_b"
149text t_ok_sel "selection"
150text t_ok_rule "rule"
151text t_ok_cont "contention"
152text t_ok_rcont "real_contention"
153text t_ok_ctrl "control"
154text t_ok_mtr "meter"
155text t_ok_resuj "resolution_uj"
156text t_ok_spns "sample_period_ns"
157text t_ok_wrp "wrap"
158text t_ok_acc "accuracy"
159text t_ok_cnds "conditions"
160text t_ok_vw "valid_windows"
161text t_ok_pmj "pkg_mean_J"
162text t_ok_psj "pkg_sd_J"
163text t_ok_cha "check_a"
164text t_ok_imj "I_mean_J"
165text t_ok_itlo "I_t95_lo_J"
166text t_ok_ithi "I_t95_hi_J"
167text t_ok_iblo "I_boot95_lo_J"
168text t_ok_ibhi "I_boot95_hi_J"
169text t_ok_dfz "distinguishable_from_zero"
170text t_ok_eab "E_AB_pkg_mean_J"
171text t_ok_phw "pkg_halfwidth_over_E_AB"
172text t_ok_aeb "analytic_edge_bound95_per_round_J"
173text t_ok_rtot "rounds_total"
174text t_ok_pass "pass"
175text t_ok_ipdfz "I_pkg_distinguishable_from_zero"
176text t_ok_chb "check_b"
177text t_ok_pci "pkg_cycles_inst"
178text t_ok_rng "reported_not_gated"
179text t_ok_cpci "cpu_p_cycles_inst"
180text t_ok_pcl2 "pkg_cycles_l2refill"
181text t_ok_na "naive_additive"
182text t_ok_tgt "target"
183text t_ok_feat "features"
184text t_ok_fw "fit_windows"
185text t_ok_fok "fit_ok"
186text t_ok_p0t "p0_T_J"
187text t_ok_acyc "a_J_per_1e9_cycles"
188text t_ok_bins "b_J_per_1e9_inst"
189text t_ok_bl2 "b_J_per_1e9_l2refill"
190text t_ok_rsd "residual_sd_J"
191text t_ok_cns "condition_number_scaled"
192text t_ok_deg "degenerate"
193text t_ok_hw "heldout_windows"
194text t_ok_hme "heldout_mean_error_J"
195text t_ok_hlo "heldout_mean_error_t95_lo_J"
196text t_ok_hhi "heldout_mean_error_t95_hi_J"
197text t_ok_hrms "heldout_rms_error_J"
198text t_ok_hrel "heldout_mean_relative_error"
199text t_ok_h95 "heldout_inside_95pi"
200text t_ok_attr "attribution"
201text t_ok_scc "solo_counterfactual_conserved"
202text t_ok_sct "solo_counterfactual_total"
203text t_ok_cmc "counter_model_conserved"
204text t_ok_cmt "counter_model_total"
205text t_ok_tsd "tolerance_sd_I_pkg_J"
206
207text t_p_cfg "CONFIG "
208text t_p_sc " score="
209text t_p_rds " rounds="
210text t_p_all " all_rounds>=0.02="
211text t_p_ipk " I_pkg="
212text t_p_jb " J ["
213text t_p_cs ", "
214text t_p_ra "] A="
215text t_p_b " B="
216text t_p_sel "SELECT contention="
217text t_p_rl " real="
218text t_p_ct " control="
219text t_p_fit "FIT "
220text t_p_n " n="
221text t_p_cd " cond="
222text t_p_s " s="
223text t_p_ho " J | heldout n="
224text t_p_me " mean_err="
225text t_p_in "] inside="
226text t_p_rm " rms="
227text t_p_re " rel="
228text t_p_ps " pass="
229text t_p_i "I "
230text t_p_mn " mean="
231text t_p_t95 " J t95=["
232text t_p_b95 "] boot95=["
233text t_p_ca "CHECK_A "
234text t_p_di " distinguishable="
235text t_p_cb "CHECK_B "
236
237text t_rj_w1 "{\"record\":\""
238text t_rj_w2 "\",\"digest\":\""
239text t_rj_w3 "\",\"file\":\""
240text t_rj_w4 "\",\"config\":\""
241text t_rj_w5 "\",\"cond\":\""
242text t_rj_w6 "\",\"round\":"
243text t_rj_w7 ",\"pos\":"
244text t_rj_w8 ",\"status\":\""
245text t_rj_w9 "\",\"confidence\":\"DIRECT\",\"forge\":"
246text t_rj_w10 ",\"e_uj\":{\"pkg\":"
247text t_rj_w11 ",\"cpu_e\":"
248text t_rj_w12 ",\"cpu_p\":"
249text t_rj_w13 "},\"samples\":"
250text t_rj_w14 ",\"max_gap_ns\":"
251text t_rj_w15 ",\"foreign_busy_ms\":"
252text t_rj_w16 ",\"max_temp_mc\":"
253text t_rj_w17 "}\n"
254
255text t_rj_a4 ",\"method\":\""
256text t_rj_a5 "\",\"measured_interval\":\""
257text t_rj_a6 "\",\"verdict\":\""
258text t_rj_a7 "\",\"measured_uj\":"
259text t_rj_a8 ",\"allocated_uj\":"
260text t_rj_a9 ",\"unattributed_uj\":"
261text t_rj_a10 ",\"tolerance_uj\":"
262text t_rj_a11 ",\"overall\":\""
263text t_rj_a12 "\",\"forge\":"
264text t_rj_a13 ",\"shares\":["
265text t_rj_s1 "{\"who\":\""
266text t_rj_s2 "\",\"uj\":"
267text t_rj_s3 ",\"conf\":\""
268text t_rj_s4 "\",\"forge\":"
269text t_rj_a14 "]}\n"
270
271note ---- Buffers & CLI args ----
272global g_ea1 uchar 256
273global g_ea2 uchar 1024
274global g_ea3 uchar 1024
275global g_ea4 uchar 1024
276global g_eal ulong 5
277global g_epath uchar 1536
278global g_edent uchar 16384
279global g_jbuf uchar 262144
280global g_jlen ulong 1
281global g_jpos ulong 1
282global g_ebuf uchar 8192
283global g_elen ulong 1
284global g_obuf uchar 8192
285global g_olen ulong 1
286global g_ofd long 1
287global g_rbuf uchar 8192
288global g_rlen ulong 1
289global g_rfd long 1
290global g_fstack ulong 32
291global g_fdepth ulong 1
292global g_numbuf uchar 128
293
294note ---- JSON DOM pool (reset per file) ----
295global g_jn_n ulong 1
296global g_jn_t ulong 4096
297global g_jn_d f64 4096
298global g_jn_u ulong 4096
299global g_jn_iu ulong 4096
300global g_jn_so ulong 4096
301global g_jn_sl ulong 4096
302global g_jn_ko ulong 4096
303global g_jn_kl ulong 4096
304global g_jn_fc ulong 4096
305global g_jn_lc ulong 4096
306global g_jn_nx ulong 4096
307global g_jn_cnt ulong 4096
308global g_jstr uchar 65536
309global g_jslen ulong 1
310
311note ---- Window extra fields (parallel to g_tw_*) ----
312global g_nw ulong 1
313global g_wf uchar 131072
314global g_wfl ulong 512
315global g_wsa uchar 32768
316global g_wsb uchar 32768
317global g_wst ulong 512
318global g_we long 2048
319global g_wcyc f64 512
320global g_wins f64 512
321global g_wl2 f64 512
322global g_used ulong 64
323
324note ---- Directory filenames (sorted) ----
325global g_fn_n ulong 1
326global g_fn_b uchar 131072
327global g_fn_l ulong 512
328global g_fn_o ulong 512
329
330note ---- Rounds (up to 512) ----
331global g_nr ulong 1
332global g_rcfg uchar 8192
333global g_rrnd ulong 512
334global g_rc long 2048
335global g_rst ulong 512
336global g_ri f64 2048
337global g_rb95 f64 512
338
339note ---- Configs (up to 32) ----
340global g_ncfg ulong 1
341global g_cfgs uchar 512
342
343note ---- Stats / Fit / Holdout scratch ----
344global g_rng ulong 1
345global g_boot f64 10000
346global g_sv f64 512
347global g_sv2 f64 512
348global g_tq f64 32
349global g_ms f64 4
350global g_is f64 6
351global g_isn ulong 1
352global g_fxtx f64 9
353global g_fxty f64 3
354global g_fnrm f64 3
355global g_fsc f64 9
356global g_fm f64 9
357global g_fev f64 3
358global g_fbeta f64 6
359global g_finv f64 18
360global g_fs f64 2
361global g_fcond f64 2
362global g_fok ulong 2
363global g_fdeg ulong 2
364global g_fn ulong 2
365global g_hn ulong 1
366global g_hins ulong 1
367global g_hpass ulong 2
368global g_hmerr f64 1
369global g_hlo f64 1
370global g_hhi f64 1
371global g_hrms f64 1
372global g_hmrel f64 1
373
374note ---- Low-level buffered I/O (ch: 0=stdout, 1=out, 2=g_rec) ----
375fn er_flush ch ulong to void
376 if eq ch 0
377 n = get g_elen 0
378 if gt n 0
379 _w = write 1 g_ebuf 0 n
380 put g_elen 0 0
381 end
382 else
383 if eq ch 1
384 n = get g_olen 0
385 fd = get g_ofd 0
386 if and gt n 0 ge fd 0
387 _w = write int fd g_obuf 0 n
388 put g_olen 0 0
389 end
390 else
391 n = get g_rlen 0
392 fd = get g_rfd 0
393 if and gt n 0 ge fd 0
394 _w = write int fd g_rbuf 0 n
395 put g_rlen 0 0
396 end
397 end
398 end
399end
400
401fn er_putc ch ulong b uchar to void
402 if eq ch 0
403 n = get g_elen 0
404 if ge n 8192
405 er_flush 0
406 n = 0
407 end
408 put g_ebuf n b
409 put g_elen 0 add n 1
410 else
411 if eq ch 1
412 n = get g_olen 0
413 if ge n 8192
414 er_flush 1
415 n = 0
416 end
417 put g_obuf n b
418 put g_olen 0 add n 1
419 else
420 n = get g_rlen 0
421 if ge n 8192
422 er_flush 2
423 n = 0
424 end
425 put g_rbuf n b
426 put g_rlen 0 add n 1
427 end
428 end
429end
430
431fn er_puts ch ulong s view uchar * to void
432 n = len s
433 for i ulong n
434 er_putc ch get s i
435 end
436end
437
438fn er_stderr s view uchar * to void
439 n = len s
440 for i ulong n
441 put g_numbuf i get s i
442 end
443 _w = write 2 g_numbuf 0 n
444end
445
446fn er_put_u64 ch ulong v ulong to void
447 if eq v 0
448 er_putc ch uchar 48
449 ret
450 end
451 n = ulong 0
452 x = v
453 while gt x 0
454 put g_numbuf n uchar add 48 rem x 10
455 x = div x 10
456 n = add n 1
457 end
458 i = n
459 while gt i 0
460 i = sub i 1
461 er_putc ch get g_numbuf i
462 end
463end
464
465fn er_put_i64 ch ulong v long to void
466 if lt v 0
467 er_putc ch uchar 45
468 uv = sub ulong 0 bitcast ulong v
469 er_put_u64 ch uv
470 ret
471 end
472 er_put_u64 ch ulong v
473end
474
475fn er_is_nan x f64 to bool
476 ret ne x x
477end
478
479fn er_is_inf x f64 to bool
480 if ne x x
481 ret false
482 end
483 ax = x
484 if lt ax f64 0.0
485 ax = sub f64 0.0 ax
486 end
487 ret ne sub x x f64 0.0
488end
489
490fn er_fabs x f64 to f64
491 if lt x f64 0.0
492 ret sub f64 0.0 x
493 end
494 ret x
495end
496
497fn er_llround x f64 to long
498 if lt x f64 0.0
499 ret long sub x f64 0.5
500 end
501 ret long add x f64 0.5
502end
503
504fn er_put_fix_frac ch ulong fr ulong sc ulong to void
505 er_putc ch uchar 46
506 d = div sc 10
507 while gt d 0
508 dig = rem div fr d 10
509 er_putc ch uchar add 48 dig
510 d = div d 10
511 end
512end
513
514fn er_put_fix ch ulong x f64 dp ulong to void
515 if er_is_nan x
516 er_puts ch t_er_nan
517 ret
518 end
519 if er_is_inf x
520 if lt x f64 0.0
521 er_puts ch t_er_ninf
522 else
523 er_puts ch t_er_inf
524 end
525 ret
526 end
527 ax = x
528 if lt ax f64 0.0
529 er_putc ch uchar 45
530 ax = sub f64 0.0 ax
531 end
532 sc = ulong 1
533 for i ulong dp
534 sc = mul sc 10
535 end
536 scaled = ulong add mul ax f64 sc f64 0.5
537 er_put_u64 ch div scaled sc
538 if gt dp 0
539 er_put_fix_frac ch rem scaled sc sc
540 end
541end
542
543fn er_put_g9_emit ch ulong exp long last ulong to void
544 if and ge exp -4 lt exp 9
545 if lt exp 0
546 er_putc ch uchar 48
547 er_putc ch uchar 46
548 zs = ulong sub -1 exp
549 for i ulong zs
550 er_putc ch uchar 48
551 end
552 for i ulong last
553 er_putc ch get g_numbuf i
554 end
555 else
556 ip = add ulong exp 1
557 for i ulong ip
558 if lt i last
559 er_putc ch get g_numbuf i
560 else
561 er_putc ch uchar 48
562 end
563 end
564 if gt last ip
565 er_putc ch uchar 46
566 i = ip
567 while lt i last
568 er_putc ch get g_numbuf i
569 i = add i 1
570 end
571 end
572 end
573 else
574 er_putc ch get g_numbuf 0
575 if gt last 1
576 er_putc ch uchar 46
577 i = ulong 1
578 while lt i last
579 er_putc ch get g_numbuf i
580 i = add i 1
581 end
582 end
583 er_putc ch uchar 101
584 if lt exp 0
585 er_putc ch uchar 45
586 ae = ulong sub 0 exp
587 if lt ae 10
588 er_putc ch uchar 48
589 end
590 er_put_u64 ch ae
591 else
592 er_putc ch uchar 43
593 ae = ulong exp
594 if lt ae 10
595 er_putc ch uchar 48
596 end
597 er_put_u64 ch ae
598 end
599 end
600end
601
602fn er_put_g9 ch ulong x f64 to void
603 if or er_is_nan x er_is_inf x
604 er_puts ch t_er_null
605 ret
606 end
607 ax = er_fabs x
608 if and lt ax f64 9000000000000000.0 eq f64 long x x
609 er_put_i64 ch long x
610 ret
611 end
612 if lt x f64 0.0
613 er_putc ch uchar 45
614 end
615 if eq ax f64 0.0
616 er_putc ch uchar 48
617 ret
618 end
619 exp = long 0
620 m = ax
621 while ge m f64 10.0
622 m = div m f64 10.0
623 exp = add exp 1
624 end
625 while lt m f64 1.0
626 m = mul m f64 10.0
627 exp = sub exp 1
628 end
629 digs = ulong add mul m f64 100000000.0 f64 0.5
630 if ge digs 1000000000
631 digs = div digs 10
632 exp = add exp 1
633 end
634 for i ulong 9
635 put g_numbuf sub 8 i uchar add 48 rem digs 10
636 digs = div digs 10
637 end
638 last = ulong 9
639 while and gt last 1 eq get g_numbuf sub last 1 uchar 48
640 last = sub last 1
641 end
642 er_put_g9_emit ch exp last
643end
644
645note ---- C-string helpers on global tables ----
646note tbl: 0=g_wf(256), 1=g_tw_cfg(16), 2=g_tw_cnd(8), 3=g_wsa(64), 4=g_wsb(64),
647note 5=g_rcfg(16), 6=g_cfgs(16), 7=g_ta_mth(32), 8=g_ts_who(8), 9=g_tp_tag(8)
648fn er_tb tbl ulong idx ulong to uchar
649 if eq tbl 0
650 ret get g_wf idx
651 end
652 if eq tbl 1
653 ret get g_tw_cfg idx
654 end
655 if eq tbl 2
656 ret get g_tw_cnd idx
657 end
658 if eq tbl 3
659 ret get g_wsa idx
660 end
661 if eq tbl 4
662 ret get g_wsb idx
663 end
664 if eq tbl 5
665 ret get g_rcfg idx
666 end
667 if eq tbl 6
668 ret get g_cfgs idx
669 end
670 if eq tbl 7
671 ret get g_ta_mth idx
672 end
673 if eq tbl 8
674 ret get g_ts_who idx
675 end
676 ret get g_tp_tag idx
677end
678
679fn er_put_cs ch ulong tbl ulong off ulong cap ulong to void
680 for i ulong cap
681 c = er_tb tbl add off i
682 if eq c 0
683 ret
684 end
685 er_putc ch c
686 end
687end
688
689fn er_eq_t tbl ulong off ulong cap ulong s view uchar * to bool
690 n = len s
691 for i ulong cap
692 c = er_tb tbl add off i
693 if eq c 0
694 ret eq i n
695 end
696 if ge i n
697 ret false
698 end
699 if ne c get s i
700 ret false
701 end
702 end
703 ret eq cap n
704end
705
706fn er_eq_tt t1 ulong o1 ulong t2 ulong o2 ulong cap ulong to bool
707 for i ulong cap
708 a = er_tb t1 add o1 i
709 b = er_tb t2 add o2 i
710 if ne a b
711 ret false
712 end
713 if eq a 0
714 ret true
715 end
716 end
717 ret true
718end
719
720fn er_put_hex ch ulong sl ulong lim ulong to void
721 bo = mul sl 32
722 for i ulong lim
723 b = ulong get g_tdig add bo div i 2
724 d = and b 15
725 if eq rem i 2 0
726 d = shr b 4
727 end
728 c = add 48 d
729 if ge d 10
730 c = add 87 d
731 end
732 er_putc ch uchar c
733 end
734end
735
736fn er_put_status ch ulong s ulong to void
737 if eq s 0
738 er_puts ch t_ty_s0
739 ret
740 end
741 if eq s 1
742 er_puts ch t_ty_s1
743 ret
744 end
745 if eq s 2
746 er_puts ch t_ty_s2
747 ret
748 end
749 if eq s 3
750 er_puts ch t_ty_s3
751 ret
752 end
753 if eq s 4
754 er_puts ch t_ty_s4
755 ret
756 end
757 if eq s 5
758 er_puts ch t_ty_s5
759 ret
760 end
761 if eq s 6
762 er_puts ch t_ty_s6
763 ret
764 end
765 if eq s 7
766 er_puts ch t_ty_s7
767 ret
768 end
769 if eq s 8
770 er_puts ch t_ty_s8
771 ret
772 end
773 if eq s 9
774 er_puts ch t_ty_s9
775 ret
776 end
777 if eq s 10
778 er_puts ch t_ty_s10
779 ret
780 end
781 if eq s 11
782 er_puts ch t_ty_s11
783 ret
784 end
785 if eq s 12
786 er_puts ch t_ty_s12
787 ret
788 end
789 if eq s 13
790 er_puts ch t_ty_s13
791 ret
792 end
793 if eq s 14
794 er_puts ch t_ty_s14
795 ret
796 end
797 if eq s 15
798 er_puts ch t_ty_s15
799 ret
800 end
801 if eq s 16
802 er_puts ch t_ty_s16
803 ret
804 end
805 er_puts ch t_ty_inv
806end
807
808fn er_put_conf ch ulong c ulong to void
809 if eq c 0
810 er_puts ch t_ty_c0
811 ret
812 end
813 if eq c 1
814 er_puts ch t_ty_c1
815 ret
816 end
817 if eq c 2
818 er_puts ch t_ty_c2
819 ret
820 end
821 if eq c 3
822 er_puts ch t_ty_c3
823 ret
824 end
825 if eq c 4
826 er_puts ch t_ty_c4
827 ret
828 end
829 if eq c 5
830 er_puts ch t_ty_c5
831 ret
832 end
833 er_puts ch t_ty_inv
834end
835
836note ---- JSON writer (channel 1) ----
837fn o_sep to void
838 d = get g_fdepth 0
839 if eq get g_fstack d 0
840 er_putc 1 uchar 44
841 end
842 put g_fstack d 0
843end
844
845fn o_key k view uchar * to void
846 o_sep
847 if gt len k 0
848 er_putc 1 uchar 34
849 er_puts 1 k
850 er_putc 1 uchar 34
851 er_putc 1 uchar 58
852 end
853end
854
855fn o_open k view uchar * c uchar to void
856 o_key k
857 er_putc 1 c
858 d = add get g_fdepth 0 1
859 put g_fdepth 0 d
860 put g_fstack d 1
861end
862
863fn o_open_anon c uchar to void
864 o_sep
865 er_putc 1 c
866 d = add get g_fdepth 0 1
867 put g_fdepth 0 d
868 put g_fstack d 1
869end
870
871fn o_close c uchar to void
872 er_putc 1 c
873 put g_fdepth 0 sub get g_fdepth 0 1
874end
875
876fn o_num k view uchar * x f64 to void
877 o_key k
878 er_put_g9 1 x
879end
880
881fn o_u k view uchar * x ulong to void
882 o_key k
883 er_put_u64 1 x
884end
885
886fn o_str k view uchar * s view uchar * to void
887 o_key k
888 er_putc 1 uchar 34
889 er_puts 1 s
890 er_putc 1 uchar 34
891end
892
893fn o_str_cs k view uchar * tbl ulong off ulong cap ulong to void
894 o_key k
895 er_putc 1 uchar 34
896 er_put_cs 1 tbl off cap
897 er_putc 1 uchar 34
898end
899
900fn o_bool k view uchar * b bool to void
901 o_key k
902 if b
903 er_puts 1 t_er_true
904 else
905 er_puts 1 t_er_false
906 end
907end
908
909note ---- JSON parser ----
910fn jp_ws to void
911 n = get g_jlen 0
912 p = get g_jpos 0
913 while lt p n
914 c = get g_jbuf p
915 if or eq c 32 or eq c 9 or eq c 10 eq c 13
916 p = add p 1
917 else
918 put g_jpos 0 p
919 ret
920 end
921 end
922 put g_jpos 0 p
923end
924
925fn jp_new t ulong to long
926 idx = get g_jn_n 0
927 if ge idx ER_MAXN
928 ret -1
929 end
930 put g_jn_n 0 add idx 1
931 put g_jn_t idx t
932 put g_jn_d idx f64 0.0
933 put g_jn_u idx 0
934 put g_jn_iu idx 0
935 put g_jn_so idx 0
936 put g_jn_sl idx 0
937 put g_jn_ko idx 0
938 put g_jn_kl idx 0
939 put g_jn_fc idx 0
940 put g_jn_lc idx 0
941 put g_jn_nx idx 0
942 put g_jn_cnt idx 0
943 ret long idx
944end
945
946fn jp_str_raw to long
947 n = get g_jlen 0
948 p = get g_jpos 0
949 if or ge p n ne get g_jbuf p uchar 34
950 ret -1
951 end
952 p = add p 1
953 so = get g_jslen 0
954 sl = ulong 0
955 while lt p n
956 c = get g_jbuf p
957 if eq c uchar 34
958 put g_jpos 0 add p 1
959 put g_jslen 0 add so sl
960 put g_tidx 0 so
961 put g_tidx 1 sl
962 ret 0
963 end
964 p = add p 1
965 if and eq c uchar 92 lt p n
966 c = get g_jbuf p
967 p = add p 1
968 end
969 if lt add so sl 65536
970 put g_jstr add so sl c
971 sl = add sl 1
972 end
973 end
974 ret -1
975end
976
977fn jp_match s view uchar * to bool
978 n = get g_jlen 0
979 p = get g_jpos 0
980 m = len s
981 if gt add p m n
982 ret false
983 end
984 for i ulong m
985 if ne get g_jbuf add p i get s i
986 ret false
987 end
988 end
989 put g_jpos 0 add p m
990 ret true
991end
992
993fn jp_is_dig p ulong n ulong to bool
994 if ge p n
995 ret false
996 end
997 c = get g_jbuf p
998 ret and ge c uchar 48 le c uchar 57
999end
1000
1001fn jp_parse_exp fl f64 to f64
1002 n = get g_jlen 0
1003 p = get g_jpos 0
1004 eneg = false
1005 if lt p n
1006 ec = get g_jbuf p
1007 if eq ec uchar 45
1008 eneg = true
1009 p = add p 1
1010 else
1011 if eq ec uchar 43
1012 p = add p 1
1013 end
1014 end
1015 end
1016 ev = ulong 0
1017 while jp_is_dig p n
1018 ev = add mul ev 10 ulong sub get g_jbuf p uchar 48
1019 p = add p 1
1020 end
1021 put g_jpos 0 p
1022 res = fl
1023 for i ulong ev
1024 if eneg
1025 res = div res f64 10.0
1026 else
1027 res = mul res f64 10.0
1028 end
1029 end
1030 ret res
1031end
1032
1033fn jp_parse_frac fl f64 to f64
1034 n = get g_jlen 0
1035 p = get g_jpos 0
1036 sc = f64 0.1
1037 res = fl
1038 while jp_is_dig p n
1039 d = ulong sub get g_jbuf p uchar 48
1040 res = add res mul f64 d sc
1041 sc = mul sc f64 0.1
1042 p = add p 1
1043 end
1044 put g_jpos 0 p
1045 ret res
1046end
1047
1048fn jp_parse_num to long
1049 n = get g_jlen 0
1050 p = get g_jpos 0
1051 if ge p n
1052 ret -1
1053 end
1054 negv = false
1055 if eq get g_jbuf p uchar 45
1056 negv = true
1057 p = add p 1
1058 end
1059 if not jp_is_dig p n
1060 ret -1
1061 end
1062 u = ulong 0
1063 fl = f64 0.0
1064 is_u = true
1065 if negv
1066 is_u = false
1067 end
1068 while jp_is_dig p n
1069 d = ulong sub get g_jbuf p uchar 48
1070 if or gt u 1844674407370955161 and eq u 1844674407370955161 gt d 5
1071 is_u = false
1072 else
1073 u = add mul u 10 d
1074 end
1075 fl = add mul fl f64 10.0 f64 d
1076 p = add p 1
1077 end
1078 put g_jpos 0 p
1079 if and lt p n eq get g_jbuf p uchar 46
1080 is_u = false
1081 put g_jpos 0 add p 1
1082 fl = jp_parse_frac fl
1083 p = get g_jpos 0
1084 end
1085 if lt p n
1086 ec = get g_jbuf p
1087 if or eq ec uchar 101 eq ec uchar 69
1088 is_u = false
1089 put g_jpos 0 add p 1
1090 fl = jp_parse_exp fl
1091 end
1092 end
1093 nd = jp_new JT_NUM
1094 if lt nd 0
1095 ret -1
1096 end
1097 ni = ulong nd
1098 if is_u
1099 put g_jn_u ni u
1100 put g_jn_iu ni 1
1101 put g_jn_d ni f64 u
1102 else
1103 if negv
1104 fl = sub f64 0.0 fl
1105 end
1106 put g_jn_d ni fl
1107 end
1108 ret nd
1109end
1110
1111fn jp_push par ulong ch ulong to void
1112 fc = get g_jn_fc par
1113 if eq fc 0
1114 put g_jn_fc par ch
1115 put g_jn_lc par ch
1116 else
1117 lc = get g_jn_lc par
1118 put g_jn_nx lc ch
1119 put g_jn_lc par ch
1120 end
1121 put g_jn_cnt par add get g_jn_cnt par 1
1122end
1123
1124fn jp_parse to long
1125 jp_ws
1126 n = get g_jlen 0
1127 p = get g_jpos 0
1128 if ge p n
1129 ret -1
1130 end
1131 c = get g_jbuf p
1132 if eq c uchar 123
1133 put g_jpos 0 add p 1
1134 nd = jp_new JT_OBJ
1135 if lt nd 0
1136 ret -1
1137 end
1138 obj = ulong nd
1139 jp_ws
1140 p2 = get g_jpos 0
1141 if and lt p2 n eq get g_jbuf p2 uchar 125
1142 put g_jpos 0 add p2 1
1143 ret nd
1144 end
1145 while true
1146 jp_ws
1147 if lt jp_str_raw 0
1148 ret -1
1149 end
1150 ko = get g_tidx 0
1151 kl = get g_tidx 1
1152 jp_ws
1153 p3 = get g_jpos 0
1154 if or ge p3 n ne get g_jbuf p3 uchar 58
1155 ret -1
1156 end
1157 put g_jpos 0 add p3 1
1158 val = jp_parse
1159 if lt val 0
1160 ret -1
1161 end
1162 vi = ulong val
1163 put g_jn_ko vi ko
1164 put g_jn_kl vi kl
1165 jp_push obj vi
1166 jp_ws
1167 p4 = get g_jpos 0
1168 if ge p4 n
1169 ret -1
1170 end
1171 c4 = get g_jbuf p4
1172 if eq c4 uchar 44
1173 put g_jpos 0 add p4 1
1174 else
1175 if eq c4 uchar 125
1176 put g_jpos 0 add p4 1
1177 ret nd
1178 else
1179 ret -1
1180 end
1181 end
1182 end
1183 end
1184 if eq c uchar 91
1185 put g_jpos 0 add p 1
1186 nd = jp_new JT_ARR
1187 if lt nd 0
1188 ret -1
1189 end
1190 arr = ulong nd
1191 jp_ws
1192 p2 = get g_jpos 0
1193 if and lt p2 n eq get g_jbuf p2 uchar 93
1194 put g_jpos 0 add p2 1
1195 ret nd
1196 end
1197 while true
1198 val = jp_parse
1199 if lt val 0
1200 ret -1
1201 end
1202 jp_push arr ulong val
1203 jp_ws
1204 p4 = get g_jpos 0
1205 if ge p4 n
1206 ret -1
1207 end
1208 c4 = get g_jbuf p4
1209 if eq c4 uchar 44
1210 put g_jpos 0 add p4 1
1211 else
1212 if eq c4 uchar 93
1213 put g_jpos 0 add p4 1
1214 ret nd
1215 else
1216 ret -1
1217 end
1218 end
1219 end
1220 end
1221 if eq c uchar 34
1222 if lt jp_str_raw 0
1223 ret -1
1224 end
1225 nd = jp_new JT_STR
1226 if lt nd 0
1227 ret -1
1228 end
1229 put g_jn_so ulong nd get g_tidx 0
1230 put g_jn_sl ulong nd get g_tidx 1
1231 ret nd
1232 end
1233 if jp_match t_er_true
1234 nd = jp_new JT_BOOL
1235 if ge nd 0
1236 put g_jn_d ulong nd f64 1.0
1237 end
1238 ret nd
1239 end
1240 if jp_match t_er_false
1241 ret jp_new JT_BOOL
1242 end
1243 if jp_match t_er_null
1244 ret jp_new JT_NULL
1245 end
1246 ret jp_parse_num
1247end
1248
1249fn jget o long k view uchar * to long
1250 if lt o 0
1251 ret -1
1252 end
1253 if ne get g_jn_t ulong o JT_OBJ
1254 ret -1
1255 end
1256 kl = len k
1257 cur = get g_jn_fc ulong o
1258 while gt cur 0
1259 if eq get g_jn_kl cur kl
1260 ko = get g_jn_ko cur
1261 ok = true
1262 for i ulong kl
1263 if ne get g_jstr add ko i get k i
1264 ok = false
1265 end
1266 end
1267 if ok
1268 ret long cur
1269 end
1270 end
1271 cur = get g_jn_nx cur
1272 end
1273 ret -1
1274end
1275
1276fn jarr_at a long idx ulong to long
1277 if lt a 0
1278 ret -1
1279 end
1280 if ne get g_jn_t ulong a JT_ARR
1281 ret -1
1282 end
1283 if ge idx get g_jn_cnt ulong a
1284 ret -1
1285 end
1286 cur = get g_jn_fc ulong a
1287 for i ulong idx
1288 cur = get g_jn_nx cur
1289 end
1290 ret long cur
1291end
1292
1293fn jd o long k view uchar * to f64
1294 v = jget o k
1295 if lt v 0
1296 ret div f64 0.0 f64 0.0
1297 end
1298 if ne get g_jn_t ulong v JT_NUM
1299 ret div f64 0.0 f64 0.0
1300 end
1301 ret get g_jn_d ulong v
1302end
1303
1304fn ju o long k view uchar * to ulong
1305 v = jget o k
1306 if lt v 0
1307 ret 0
1308 end
1309 vi = ulong v
1310 if ne get g_jn_t vi JT_NUM
1311 ret 0
1312 end
1313 if ne get g_jn_iu vi 0
1314 ret get g_jn_u vi
1315 end
1316 d = get g_jn_d vi
1317 if lt d f64 0.0
1318 ret 0
1319 end
1320 ret ulong d
1321end
1322
1323fn jau a long idx ulong to ulong
1324 v = jarr_at a idx
1325 if lt v 0
1326 ret 0
1327 end
1328 ret get g_jn_u ulong v
1329end
1330
1331fn jstr_len o long k view uchar * to ulong
1332 v = jget o k
1333 if lt v 0
1334 ret 0
1335 end
1336 if ne get g_jn_t ulong v JT_STR
1337 ret 0
1338 end
1339 ret get g_jn_sl ulong v
1340end
1341
1342fn jcopy_str o long k view uchar * dst ulong off ulong cap ulong to void
1343 v = jget o k
1344 so = ulong 0
1345 sl = ulong 0
1346 if ge v 0
1347 if eq get g_jn_t ulong v JT_STR
1348 so = get g_jn_so ulong v
1349 sl = get g_jn_sl ulong v
1350 end
1351 end
1352 lim = sub cap 1
1353 if lt sl lim
1354 lim = sl
1355 end
1356 for i ulong cap
1357 c = uchar 0
1358 if lt i lim
1359 c = get g_jstr add so i
1360 end
1361 if eq dst 0
1362 put g_tw_run add off i c
1363 else
1364 if eq dst 1
1365 put g_tw_cfg add off i c
1366 else
1367 if eq dst 2
1368 put g_tw_cnd add off i c
1369 else
1370 if eq dst 3
1371 put g_wsa add off i c
1372 else
1373 if eq dst 4
1374 put g_wsb add off i c
1375 else
1376 if eq dst 5
1377 put g_tp_tag add off i c
1378 else
1379 if eq dst 6
1380 put g_tp_rz add off i c
1381 else
1382 put g_tp_cpu add off i c
1383 end
1384 end
1385 end
1386 end
1387 end
1388 end
1389 end
1390 end
1391end
1392
1393note ---- edge_shape / energy_ok / win_pkg_uj / cpu_mask ----
1394fn j_is_u x long to bool
1395 if lt x 0
1396 ret false
1397 end
1398 ux = ulong x
1399 if ne get g_jn_t ux JT_NUM
1400 ret false
1401 end
1402 ret ne get g_jn_iu ux 0
1403end
1404
1405fn j_is_arr x long to bool
1406 if lt x 0
1407 ret false
1408 end
1409 ret eq get g_jn_t ulong x JT_ARR
1410end
1411
1412fn j_is_obj x long to bool
1413 if lt x 0
1414 ret false
1415 end
1416 ret eq get g_jn_t ulong x JT_OBJ
1417end
1418
1419fn arr_all_u a ulong to bool
1420 cur = get g_jn_fc a
1421 while gt cur 0
1422 if ne get g_jn_t cur JT_NUM
1423 ret false
1424 end
1425 if eq get g_jn_iu cur 0
1426 ret false
1427 end
1428 cur = get g_jn_nx cur
1429 end
1430 ret true
1431end
1432
1433fn edge_shape e long to bool
1434 if lt e 0
1435 ret false
1436 end
1437 fl = jget e t_jk_sok
1438 if not j_is_u fl
1439 ret false
1440 end
1441 if gt get g_jn_u ulong fl 1
1442 ret false
1443 end
1444 tm = jget e t_jk_tns
1445 if not j_is_u tm
1446 ret false
1447 end
1448 en = jget e t_jk_euj
1449 if not j_is_arr en
1450 ret false
1451 end
1452 if lt get g_jn_cnt ulong en TY_NCH
1453 ret false
1454 end
1455 ov = jget e t_jk_ovf
1456 if not j_is_arr ov
1457 ret false
1458 end
1459 if lt get g_jn_cnt ulong ov TY_NCH
1460 ret false
1461 end
1462 if not arr_all_u ulong en
1463 ret false
1464 end
1465 ret arr_all_u ulong ov
1466end
1467
1468fn energy_ok e long to bool
1469 if lt e 0
1470 ret false
1471 end
1472 d = jd e t_jk_sok
1473 if er_is_nan d
1474 ret false
1475 end
1476 if eq long d 0
1477 ret false
1478 end
1479 ov = jget e t_jk_ovf
1480 if j_is_arr ov
1481 cur = get g_jn_fc ulong ov
1482 while gt cur 0
1483 if ne get g_jn_u cur 0
1484 ret false
1485 end
1486 cur = get g_jn_nx cur
1487 end
1488 end
1489 ret true
1490end
1491
1492fn tp_cpu_dig bo ulong i ulong to bool
1493 if ge i 32
1494 ret false
1495 end
1496 c = get g_tp_cpu add bo i
1497 ret and ge c uchar 48 le c uchar 57
1498end
1499
1500fn cpu_mask_tp p ulong to void
1501 bo = mul p 32
1502 i = ulong 0
1503 while lt i 32
1504 c = get g_tp_cpu add bo i
1505 if eq c 0
1506 ret
1507 end
1508 if and ge c uchar 48 le c uchar 57
1509 v = ulong 0
1510 while tp_cpu_dig bo i
1511 v = add mul v 10 ulong sub get g_tp_cpu add bo i uchar 48
1512 i = add i 1
1513 end
1514 if lt v 64
1515 put g_used v 1
1516 end
1517 if lt i 32
1518 if eq get g_tp_cpu add bo i uchar 44
1519 i = add i 1
1520 end
1521 end
1522 else
1523 ret
1524 end
1525 end
1526end
1527
1528fn fill_clear w ulong to void
1529 for i ulong 64
1530 put g_tw_run add mul w 64 i uchar 0
1531 put g_wsa add mul w 64 i uchar 0
1532 put g_wsb add mul w 64 i uchar 0
1533 end
1534 for i ulong 16
1535 put g_tw_cfg add mul w 16 i uchar 0
1536 end
1537 for i ulong 8
1538 put g_tw_cnd add mul w 8 i uchar 0
1539 end
1540 put g_tw_rnd w 0
1541 put g_tw_pos w 0
1542 put g_tw_t0 w 0
1543 put g_tw_t1 w 0
1544 put g_tw_pl w 0
1545 put g_tw_ok0 w 0
1546 put g_tw_ok1 w 0
1547 put g_tw_ovf w 0
1548 put g_tw_ns w 0
1549 put g_tw_mg w 0
1550 put g_tw_fb w 0
1551 put g_tw_mt w 0
1552 put g_tw_np w 0
1553 for c ulong 3
1554 put g_tw_e0 add mul w 3 c 0
1555 put g_tw_e1 add mul w 3 c 0
1556 end
1557 for c ulong 4
1558 put g_we add mul w 4 c 0
1559 end
1560 put g_wcyc w f64 0.0
1561 put g_wins w f64 0.0
1562 put g_wl2 w f64 0.0
1563end
1564
1565fn fill_pmu_mux ps ulong ls long to void
1566 if not j_is_arr ls
1567 ret
1568 end
1569 lcur = get g_jn_fc ulong ls
1570 while gt lcur 0
1571 va = jget long lcur t_jk_val
1572 en = jget long lcur t_jk_en
1573 ru = jget long lcur t_jk_ru
1574 any = ulong 0
1575 for e ulong 6
1576 any = or any jau va e
1577 end
1578 if ne any 0
1579 for e ulong 6
1580 mul128 jau ru e 1000 0
1581 mul128 jau en e 999 2
1582 h0 = get g_m128 1
1583 h1 = get g_m128 3
1584 if lt h0 h1
1585 put g_tp_mx ps 1
1586 else
1587 if eq h0 h1
1588 l0 = get g_m128 0
1589 l1 = get g_m128 2
1590 if lt l0 l1
1591 put g_tp_mx ps 1
1592 end
1593 end
1594 end
1595 end
1596 end
1597 lcur = get g_jn_nx lcur
1598 end
1599end
1600
1601fn fill_part_one w ulong ps ulong pcur ulong to void
1602 for j ulong 8
1603 put g_tp_tag add mul ps 8 j uchar 0
1604 end
1605 for j ulong 32
1606 put g_tp_rz add mul ps 32 j uchar 0
1607 put g_tp_cpu add mul ps 32 j uchar 0
1608 end
1609 put g_tp_go ps 0
1610 put g_tp_t0 ps 0
1611 put g_tp_t1 ps 0
1612 put g_tp_dn ps 0
1613 put g_tp_pl ps 0
1614 put g_tp_cl ps 0
1615 put g_tp_cy ps 0
1616 put g_tp_in ps 0
1617 put g_tp_l2 ps 0
1618 put g_tp_pn ps 0
1619 put g_tp_or ps 0
1620 put g_tp_ex ps 0
1621 put g_tp_pm ps 0
1622 put g_tp_mx ps 0
1623 if eq jd long pcur t_jk_ok f64 1.0
1624 put g_tp_ex ps 1
1625 end
1626 res = jget long pcur t_jk_res
1627 if not j_is_obj res
1628 ret
1629 end
1630 jcopy_str res t_jk_tag 5 mul ps 8 8
1631 jcopy_str res t_jk_rz 6 mul ps 32 32
1632 jcopy_str res t_jk_cpus 7 mul ps 32 32
1633 cpu_mask_tp ps
1634 if eq jd res t_jk_pin f64 1.0
1635 put g_tp_pn ps 1
1636 end
1637 if eq jd res t_jk_ook f64 1.0
1638 put g_tp_or ps 1
1639 end
1640 put g_tp_pl ps ju res t_jk_pl
1641 put g_tp_go ps ju res t_jk_go
1642 put g_tp_t0 ps ju res t_jk_t0
1643 put g_tp_t1 ps ju res t_jk_t1
1644 put g_tp_dn ps ju res t_jk_dn
1645 put g_tp_cl ps ju res t_jk_cl
1646 pm = jget res t_jk_pmu
1647 sm = jget pm t_jk_sum
1648 if and eq jd res t_jk_pop f64 1.0 eq jd pm t_jk_ok f64 1.0
1649 put g_tp_pm ps 1
1650 end
1651 cy = ju sm t_jk_cyc
1652 ins = ju sm t_jk_ins
1653 l2 = ju sm t_jk_l2
1654 put g_tp_cy ps cy
1655 put g_tp_in ps ins
1656 put g_tp_l2 ps l2
1657 fill_pmu_mux ps jget pm t_jk_pmu
1658 put g_wcyc w add get g_wcyc w f64 cy
1659 put g_wins w add get g_wins w f64 ins
1660 put g_wl2 w add get g_wl2 w f64 l2
1661end
1662
1663fn fill_part w ulong r long to void
1664 parts = jget r t_jk_parts
1665 if not j_is_arr parts
1666 ret
1667 end
1668 pcur = get g_jn_fc ulong parts
1669 while and gt pcur 0 lt get g_tw_np w TY_MAX_PART
1670 pi = get g_tw_np w
1671 put g_tw_np w add pi 1
1672 fill_part_one w add mul w 4 pi pcur
1673 pcur = get g_jn_nx pcur
1674 end
1675end
1676
1677fn fill_temp tz long mt ulong to ulong
1678 res = mt
1679 if not j_is_arr tz
1680 ret res
1681 end
1682 cur = get g_jn_fc ulong tz
1683 while gt cur 0
1684 if j_is_u long cur
1685 uv = get g_jn_u cur
1686 if le uv 4294967295
1687 if gt uv res
1688 res = uv
1689 end
1690 end
1691 end
1692 cur = get g_jn_nx cur
1693 end
1694 ret res
1695end
1696
1697fn fill_sys w ulong r long to void
1698 s0 = jget r t_jk_s0
1699 s1 = jget r t_jk_s1
1700 b0 = jget s0 t_jk_bj
1701 b1 = jget s1 t_jk_bj
1702 busy = ulong 0
1703 if and j_is_arr b0 j_is_arr b1
1704 nb = get g_jn_cnt ulong b0
1705 if gt nb 64
1706 nb = 64
1707 end
1708 for c ulong nb
1709 if eq get g_used c 0
1710 v0 = jau b0 c
1711 v1 = jau b1 c
1712 if ge v1 v0
1713 busy = add busy sub v1 v0
1714 end
1715 end
1716 end
1717 end
1718 put g_tw_fb w mul busy 10
1719 mt = fill_temp jget s0 t_jk_tm 0
1720 mt = fill_temp jget s1 t_jk_tm mt
1721 put g_tw_mt w mt
1722end
1723
1724fn tc_ok tc f64 to bool
1725 if or er_is_nan tc er_is_inf tc
1726 ret false
1727 end
1728 if or lt tc f64 0.0 ge tc f64 64.0
1729 ret false
1730 end
1731 ret eq f64 long tc tc
1732end
1733
1734fn fill_tel w ulong r long to void
1735 t0 = get g_tw_t0 w
1736 t1 = get g_tw_t1 w
1737 tel = jget r t_jk_tel
1738 tt = jget tel t_jk_tns
1739 tok = jget tel t_jk_ok
1740 prev = t0
1741 gap = ulong 0
1742 ns = ulong 0
1743 if j_is_arr tt
1744 nt = get g_jn_cnt ulong tt
1745 for i ulong nt
1746 if ne jau tok i 0
1747 tv = jau tt i
1748 if gt tv prev
1749 dg = sub tv prev
1750 if gt dg gap
1751 gap = dg
1752 end
1753 prev = tv
1754 end
1755 ns = add ns 1
1756 end
1757 end
1758 end
1759 if gt t1 prev
1760 dg = sub t1 prev
1761 if gt dg gap
1762 gap = dg
1763 end
1764 end
1765 put g_tw_ns w ns
1766 put g_tw_mg w gap
1767end
1768
1769fn fill_win_euj w ulong e0 long e1 long to void
1770 ea0 = jget e0 t_jk_euj
1771 ea1 = jget e1 t_jk_euj
1772 bo = mul w 3
1773 for c ulong TY_NCH
1774 put g_tw_e0 add bo c jau ea0 c
1775 put g_tw_e1 add bo c jau ea1 c
1776 end
1777end
1778
1779fn fill_win_edges w ulong r long to bool
1780 e0 = jget r t_jk_e0
1781 e1 = jget r t_jk_e1
1782 tc = jd r t_jk_tcpu
1783 if or not edge_shape e0 or not edge_shape e1 not tc_ok tc
1784 put g_tw_pl w 0
1785 put g_wst w TYE_E_ARG
1786 ret false
1787 end
1788 meter = eq jd r t_jk_mok f64 1.0
1789 put g_tw_t0 w ju e0 t_jk_tns
1790 put g_tw_t1 w ju e1 t_jk_tns
1791 s0ok = and meter eq jd e0 t_jk_sok f64 1.0
1792 s1ok = and meter eq jd e1 t_jk_sok f64 1.0
1793 if s0ok
1794 put g_tw_ok0 w 1
1795 if not energy_ok e0
1796 put g_tw_ovf w 1
1797 end
1798 end
1799 if s1ok
1800 put g_tw_ok1 w 1
1801 if not energy_ok e1
1802 put g_tw_ovf w 1
1803 end
1804 end
1805 fill_win_euj w e0 e1
1806 for i ulong 64
1807 put g_used i 0
1808 end
1809 put g_used ulong long tc 1
1810 ret true
1811end
1812
1813fn fill_win w ulong r long to void
1814 fill_clear w
1815 rp = jget r t_jk_parts
1816 if ge rp 0
1817 if or ne get g_jn_t ulong rp JT_ARR gt get g_jn_cnt ulong rp TY_MAX_PART
1818 put g_wst w TYE_E_ARG
1819 ret
1820 end
1821 end
1822 if or ge jstr_len r t_jk_run 64 or ge jstr_len r t_jk_cfg 16 ge jstr_len r t_jk_lvl 8
1823 put g_wst w TYE_E_ARG
1824 ret
1825 end
1826 if or gt ju r t_jk_rnd 4294967295 gt ju r t_jk_trl 4294967295
1827 put g_wst w TYE_E_ARG
1828 ret
1829 end
1830 jcopy_str r t_jk_run 0 mul w 64 64
1831 jcopy_str r t_jk_cfg 1 mul w 16 16
1832 jcopy_str r t_jk_lvl 2 mul w 8 8
1833 jcopy_str r t_jk_sa 3 mul w 64 64
1834 jcopy_str r t_jk_sb 4 mul w 64 64
1835 put g_tw_rnd w ju r t_jk_rnd
1836 put g_tw_pos w ju r t_jk_trl
1837 put g_tw_pl w ju r t_jk_pl
1838 if not fill_win_edges w r
1839 ret
1840 end
1841 fill_tel w r
1842 fill_part w r
1843 fill_sys w r
1844 put g_wst w ty_interval_check w
1845 _d = ty_interval_digest w w
1846 for c ulong TY_NCH
1847 if eq ty_interval_energy w long c TYE_OK
1848 put g_we add mul w 4 c get g_tret 0
1849 else
1850 put g_we add mul w 4 c 0
1851 end
1852 end
1853 e_pkg = get g_we mul w 4
1854 e_ce = get g_we add mul w 4 1
1855 e_cp = get g_we add mul w 4 2
1856 put g_we add mul w 4 3 sub sub e_pkg e_ce e_cp
1857end
1858
1859note ---- Directory loading ----
1860fn fn_is_jsonl off ulong nl ulong to bool
1861 if lt nl 7
1862 ret false
1863 end
1864 for i ulong 6
1865 if ne get g_fn_b add off add sub nl 6 i get t_er_jsonl i
1866 ret false
1867 end
1868 end
1869 ret true
1870end
1871
1872fn fn_lt a ulong b ulong to bool
1873 la = get g_fn_l a
1874 lb = get g_fn_l b
1875 oa = mul a 256
1876 ob = mul b 256
1877 lim = la
1878 if lt lb lim
1879 lim = lb
1880 end
1881 for i ulong lim
1882 ca = get g_fn_b add oa i
1883 cb = get g_fn_b add ob i
1884 if ne ca cb
1885 ret lt ca cb
1886 end
1887 end
1888 ret lt la lb
1889end
1890
1891fn dent_add pos ulong rl ulong to void
1892 ns = add pos 19
1893 nl = ulong 0
1894 lim = add pos rl
1895 while lt add ns nl lim
1896 if eq get g_edent add ns nl uchar 0
1897 break
1898 end
1899 nl = add nl 1
1900 end
1901 if or ge nl 255 ge get g_fn_n 0 ER_MAXW
1902 ret
1903 end
1904 idx = get g_fn_n 0
1905 ob = mul idx 256
1906 for i ulong nl
1907 put g_fn_b add ob i get g_edent add ns i
1908 end
1909 put g_fn_b add ob nl uchar 0
1910 if fn_is_jsonl ob nl
1911 put g_fn_l idx nl
1912 put g_fn_o idx idx
1913 put g_fn_n 0 add idx 1
1914 end
1915end
1916
1917fn scan_dir to long
1918 dl = get g_eal 2
1919 fd = fopen g_ea2 0 dl 0 0
1920 if lt fd 0
1921 ret -1
1922 end
1923 put g_fn_n 0 0
1924 while true
1925 n = getdnts int fd g_edent 0 16384
1926 if le n 0
1927 break
1928 end
1929 pos = ulong 0
1930 un = ulong n
1931 while lt pos un
1932 b0 = ulong get g_edent add pos 16
1933 b1 = ulong get g_edent add pos 17
1934 rl = or b0 shl b1 8
1935 if eq rl 0
1936 break
1937 end
1938 dent_add pos rl
1939 pos = add pos rl
1940 end
1941 end
1942 _fc = fclose int fd
1943 if eq get g_fn_n 0 0
1944 ret -1
1945 end
1946 ret 0
1947end
1948
1949fn sort_dir to void
1950 nf = get g_fn_n 0
1951 i = ulong 1
1952 while lt i nf
1953 k = get g_fn_o i
1954 j = i
1955 while gt j 0
1956 pv = get g_fn_o sub j 1
1957 if not fn_lt k pv
1958 break
1959 end
1960 put g_fn_o j pv
1961 j = sub j 1
1962 end
1963 put g_fn_o j k
1964 i = add i 1
1965 end
1966end
1967
1968fn prep_path oi ulong w ulong to ulong
1969 dl = get g_eal 2
1970 nl = get g_fn_l oi
1971 ob = mul oi 256
1972 for k ulong dl
1973 put g_epath k get g_ea2 k
1974 end
1975 put g_epath dl uchar 47
1976 for k ulong nl
1977 put g_epath add add dl 1 k get g_fn_b add ob k
1978 end
1979 put g_epath add add dl 1 nl uchar 0
1980 wo = mul w 256
1981 for k ulong nl
1982 put g_wf add wo k get g_fn_b add ob k
1983 end
1984 put g_wf add wo nl uchar 0
1985 put g_wfl w nl
1986 ret add add dl 1 nl
1987end
1988
1989fn load_one fi ulong to bool
1990 oi = get g_fn_o fi
1991 w = get g_nw 0
1992 plen = prep_path oi w
1993 ffd = fopen g_epath 0 plen 0 0
1994 if lt ffd 0
1995 ret false
1996 end
1997 tot = ulong 0
1998 while lt tot 262140
1999 rd = read int ffd g_jbuf tot sub 262140 tot
2000 if le rd 0
2001 break
2002 end
2003 tot = add tot ulong rd
2004 end
2005 _fc2 = fclose int ffd
2006 put g_jbuf tot uchar 0
2007 put g_jlen 0 tot
2008 put g_jpos 0 0
2009 put g_jn_n 0 1
2010 put g_jslen 0 0
2011 root = jp_parse
2012 jp_ws
2013 if or lt root 0 or not j_is_obj root ne get g_jpos 0 tot
2014 er_put_cs 0 0 mul w 256 256
2015 er_flush 0
2016 er_stderr t_er_notobj
2017 ret false
2018 end
2019 fill_win w root
2020 put g_nw 0 add w 1
2021 ret true
2022end
2023
2024fn load_dir to long
2025 if lt scan_dir 0
2026 ret -1
2027 end
2028 sort_dir
2029 inv = false
2030 put g_nw 0 0
2031 nf = get g_fn_n 0
2032 for fi ulong nf
2033 if not load_one fi
2034 inv = true
2035 end
2036 end
2037 if inv
2038 ret -1
2039 end
2040 nw = get g_nw 0
2041 for i ulong nw
2042 j = add i 1
2043 while lt j nw
2044 put g_tidx 0 i
2045 put g_tidx 1 j
2046 if eq ty_intervals_disjoint 2 TYE_E_OVERLAP
2047 put g_wst i TYE_E_OVERLAP
2048 put g_wst j TYE_E_OVERLAP
2049 er_puts 0 t_er_ovl
2050 er_put_cs 0 0 mul i 256 256
2051 er_putc 0 uchar 32
2052 er_put_cs 0 0 mul j 256 256
2053 er_putc 0 uchar 10
2054 end
2055 j = add j 1
2056 end
2057 end
2058 ret 0
2059end
2060
2061note ---- Statistics ----
2062fn init_tq to void
2063 put g_tq 0 div f64 0.0 f64 0.0
2064 put g_tq 1 f64 12.706
2065 put g_tq 2 f64 4.303
2066 put g_tq 3 f64 3.182
2067 put g_tq 4 f64 2.776
2068 put g_tq 5 f64 2.571
2069 put g_tq 6 f64 2.447
2070 put g_tq 7 f64 2.365
2071 put g_tq 8 f64 2.306
2072 put g_tq 9 f64 2.262
2073 put g_tq 10 f64 2.228
2074 put g_tq 11 f64 2.201
2075 put g_tq 12 f64 2.179
2076 put g_tq 13 f64 2.160
2077 put g_tq 14 f64 2.145
2078 put g_tq 15 f64 2.131
2079 put g_tq 16 f64 2.120
2080 put g_tq 17 f64 2.110
2081 put g_tq 18 f64 2.101
2082 put g_tq 19 f64 2.093
2083 put g_tq 20 f64 2.086
2084 put g_tq 21 f64 2.080
2085 put g_tq 22 f64 2.074
2086 put g_tq 23 f64 2.069
2087 put g_tq 24 f64 2.064
2088 put g_tq 25 f64 2.060
2089 put g_tq 26 f64 2.056
2090 put g_tq 27 f64 2.052
2091 put g_tq 28 f64 2.048
2092 put g_tq 29 f64 2.045
2093 put g_tq 30 f64 2.042
2094end
2095
2096fn tq975 df long to f64
2097 if lt df 1
2098 ret div f64 0.0 f64 0.0
2099 end
2100 if le df 30
2101 ret get g_tq ulong df
2102 end
2103 if le df 60
2104 ret f64 2.042
2105 end
2106 if le df 120
2107 ret f64 2.000
2108 end
2109 ret f64 1.96
2110end
2111
2112fn splitmix to ulong
2113 z = add get g_rng 0 ulong 0x9e3779b97f4a7c15
2114 put g_rng 0 z
2115 z = mul xor z shr z 30 ulong 0xbf58476d1ce4e5b9
2116 z = mul xor z shr z 27 ulong 0x94d049bb133111eb
2117 ret xor z shr z 31
2118end
2119
2120fn mean_sd_sv n ulong which ulong to void
2121 nan = div f64 0.0 f64 0.0
2122 if eq n 0
2123 put g_ms 0 nan
2124 put g_ms 1 nan
2125 ret
2126 end
2127 s = f64 0.0
2128 for i ulong n
2129 if eq which 0
2130 s = add s get g_sv i
2131 else
2132 s = add s get g_sv2 i
2133 end
2134 end
2135 m = div s f64 n
2136 put g_ms 0 m
2137 if le n 1
2138 put g_ms 1 nan
2139 ret
2140 end
2141 q = f64 0.0
2142 for i ulong n
2143 v = f64 0.0
2144 if eq which 0
2145 v = get g_sv i
2146 else
2147 v = get g_sv2 i
2148 end
2149 d = sub v m
2150 q = add q mul d d
2151 end
2152 put g_ms 1 fsqrt div q f64 sub n 1
2153end
2154
2155fn heap_sift start ulong endi ulong to void
2156 r = start
2157 while le add mul r 2 1 endi
2158 ch = add mul r 2 1
2159 sw = r
2160 if lt get g_boot sw get g_boot ch
2161 sw = ch
2162 end
2163 if le add ch 1 endi
2164 c2 = add ch 1
2165 if lt get g_boot sw get g_boot c2
2166 sw = c2
2167 end
2168 end
2169 if eq sw r
2170 ret
2171 end
2172 tmp = get g_boot r
2173 put g_boot r get g_boot sw
2174 put g_boot sw tmp
2175 r = sw
2176 end
2177end
2178
2179fn sort_boot n ulong to void
2180 if le n 1
2181 ret
2182 end
2183 i = div n 2
2184 while gt i 0
2185 i = sub i 1
2186 heap_sift i sub n 1
2187 end
2188 ei = sub n 1
2189 while gt ei 0
2190 tmp = get g_boot ei
2191 put g_boot ei get g_boot 0
2192 put g_boot 0 tmp
2193 ei = sub ei 1
2194 heap_sift 0 ei
2195 end
2196end
2197
2198fn boot_mean_sv n ulong to void
2199 nan = div f64 0.0 f64 0.0
2200 put g_ms 2 nan
2201 put g_ms 3 nan
2202 if lt n 2
2203 ret
2204 end
2205 put g_rng 0 21
2206 for b ulong ER_RESAMP
2207 s = f64 0.0
2208 for i ulong n
2209 idx = rem splitmix n
2210 s = add s get g_sv idx
2211 end
2212 put g_boot b div s f64 n
2213 end
2214 sort_boot ER_RESAMP
2215 put g_ms 2 get g_boot 249
2216 put g_ms 3 get g_boot 9749
2217end
2218
2219note ---- Rounds ----
2220fn cond_idx w ulong to long
2221 bo = mul w 8
2222 if er_eq_t 2 bo 8 t_c_idle
2223 ret 0
2224 end
2225 if er_eq_t 2 bo 8 t_c_a
2226 ret 1
2227 end
2228 if er_eq_t 2 bo 8 t_c_b
2229 ret 2
2230 end
2231 if er_eq_t 2 bo 8 t_c_ab
2232 ret 3
2233 end
2234 ret -1
2235end
2236
2237fn build_round_iv k ulong to void
2238 u0 = ulong get g_rc mul k 4
2239 u1 = ulong get g_rc add mul k 4 1
2240 u2 = ulong get g_rc add mul k 4 2
2241 u3 = ulong get g_rc add mul k 4 3
2242 for c ulong 4
2243 iv = get g_we add mul u3 4 c
2244 iv = sub iv get g_we add mul u1 4 c
2245 iv = sub iv get g_we add mul u2 4 c
2246 iv = add iv get g_we add mul u0 4 c
2247 put g_ri add mul k 4 c div f64 iv f64 1000000.0
2248 end
2249 vr = f64 0.0
2250 s3 = fsqrt f64 3.0
2251 for c ulong 4
2252 cw = ulong get g_rc add mul k 4 c
2253 ej = div f64 get g_we mul cw 4 f64 1000000.0
2254 ts = div f64 get g_tw_pl cw f64 1000000000.0
2255 pw = div ej ts
2256 sde = div mul pw f64 0.1 s3
2257 vr = add vr mul f64 2.0 mul sde sde
2258 end
2259 put g_rb95 k mul f64 1.96 fsqrt vr
2260end
2261
2262fn build_round_eval k ulong to void
2263 nan = div f64 0.0 f64 0.0
2264 for c ulong 4
2265 put g_ri add mul k 4 c nan
2266 end
2267 put g_rb95 k nan
2268 if ne get g_rst k TYE_OK
2269 ret
2270 end
2271 w0 = get g_rc mul k 4
2272 w1 = get g_rc add mul k 4 1
2273 w2 = get g_rc add mul k 4 2
2274 w3 = get g_rc add mul k 4 3
2275 if or lt w1 0 or lt w2 0 lt w3 0
2276 put g_rst k TYE_E_CONDITION
2277 ret
2278 end
2279 if lt w0 0
2280 put g_rst k TYE_E_MISSING_IDLE
2281 ret
2282 end
2283 st = ty_interaction ulong w0 ulong w1 ulong w2 ulong w3 long TY_CH_PKG
2284 if eq st TYE_OK
2285 for c ulong 4
2286 cw = ulong get g_rc add mul k 4 c
2287 if ne get g_wst cw TYE_OK
2288 st = get g_wst cw
2289 end
2290 end
2291 end
2292 put g_rst k st
2293 if eq st TYE_OK
2294 build_round_iv k
2295 end
2296end
2297
2298fn round_add_win i ulong to void
2299 rnd = get g_tw_rnd i
2300 nr = get g_nr 0
2301 found = long -1
2302 for k ulong nr
2303 if lt found 0
2304 if and er_eq_tt 5 mul k 16 1 mul i 16 16 eq get g_rrnd k rnd
2305 found = long k
2306 end
2307 end
2308 end
2309 if lt found 0
2310 found = long nr
2311 put g_nr 0 add nr 1
2312 ro = mul nr 16
2313 wo = mul i 16
2314 for j ulong 16
2315 ch = get g_tw_cfg add wo j
2316 put g_rcfg add ro j ch
2317 end
2318 put g_rrnd nr rnd
2319 put g_rst nr TYE_OK
2320 for c ulong 4
2321 put g_rc add mul nr 4 c -1
2322 end
2323 end
2324 ri = ulong found
2325 ci = cond_idx i
2326 if lt ci 0
2327 put g_rst ri TYE_E_CONDITION
2328 ret
2329 end
2330 slot = add mul ri 4 ulong ci
2331 if ge get g_rc slot 0
2332 put g_rst ri TYE_E_DOUBLE_COUNT
2333 else
2334 put g_rc slot long i
2335 end
2336end
2337
2338fn build_rounds to void
2339 put g_nr 0 0
2340 nw = get g_nw 0
2341 for i ulong nw
2342 round_add_win i
2343 end
2344 nr = get g_nr 0
2345 for k ulong nr
2346 build_round_eval k
2347 end
2348end
2349
2350note ---- Linear fit (3x3 OLS + Jacobi eigenvalues) ----
2351fn m2 i0 ulong i1 ulong i2 ulong i3 ulong to f64
2352 ret sub mul get g_fxtx i0 get g_fxtx i1 mul get g_fxtx i2 get g_fxtx i3
2353end
2354
2355fn inv3_to slot ulong to bool
2356 t0 = mul get g_fxtx 0 m2 4 8 5 7
2357 t1 = mul get g_fxtx 1 m2 3 8 5 6
2358 t2 = mul get g_fxtx 2 m2 3 7 4 6
2359 d = add sub t0 t1 t2
2360 if lt er_fabs d bitcast f64 or shl ulong 0x01ae1025 32 ulong 0x648836b8
2361 ret false
2362 end
2363 bo = mul slot 9
2364 put g_finv bo div m2 4 8 5 7 d
2365 put g_finv add bo 1 div m2 2 7 1 8 d
2366 put g_finv add bo 2 div m2 1 5 2 4 d
2367 put g_finv add bo 3 div m2 5 6 3 8 d
2368 put g_finv add bo 4 div m2 0 8 2 6 d
2369 put g_finv add bo 5 div m2 2 3 0 5 d
2370 put g_finv add bo 6 div m2 3 7 4 6 d
2371 put g_finv add bo 7 div m2 1 6 0 7 d
2372 put g_finv add bo 8 div m2 0 4 1 3 d
2373 ret true
2374end
2375
2376fn eig3_apply p ulong q ulong c f64 s f64 to void
2377 for k ulong 3
2378 ip = add mul k 3 p
2379 iq = add mul k 3 q
2380 mkp = get g_fm ip
2381 mkq = get g_fm iq
2382 put g_fm ip sub mul c mkp mul s mkq
2383 put g_fm iq add mul s mkp mul c mkq
2384 end
2385 for k ulong 3
2386 ip = add mul p 3 k
2387 iq = add mul q 3 k
2388 mpk = get g_fm ip
2389 mqk = get g_fm iq
2390 put g_fm ip sub mul c mpk mul s mqk
2391 put g_fm iq add mul s mpk mul c mqk
2392 end
2393end
2394
2395fn eig3_rot p ulong q ulong to void
2396 mpq = get g_fm add mul p 3 q
2397 if lt er_fabs mpq bitcast f64 or shl ulong 0x01ae1025 32 ulong 0x648836b8
2398 ret
2399 end
2400 mqq = get g_fm add mul q 3 q
2401 mpp = get g_fm add mul p 3 p
2402 th = div sub mqq mpp mul f64 2.0 mpq
2403 sgn = f64 1.0
2404 if lt th f64 0.0
2405 sgn = f64 -1.0
2406 end
2407 t = div sgn add er_fabs th fsqrt add mul th th f64 1.0
2408 c = div f64 1.0 fsqrt add mul t t f64 1.0
2409 eig3_apply p q c mul t c
2410end
2411
2412fn eig3_jacobi to void
2413 for i ulong 9
2414 put g_fm i get g_fsc i
2415 end
2416 for it ulong 60
2417 for p ulong 3
2418 q = add p 1
2419 while lt q 3
2420 eig3_rot p q
2421 q = add q 1
2422 end
2423 end
2424 end
2425 put g_fev 0 get g_fm 0
2426 put g_fev 1 get g_fm 4
2427 put g_fev 2 get g_fm 8
2428end
2429
2430fn feat_v w ulong f2 ulong k ulong to f64
2431 if eq k 0
2432 ret f64 1.0
2433 end
2434 if eq k 1
2435 ret div get g_wcyc w f64 1000000000.0
2436 end
2437 if ne f2 0
2438 ret div get g_wl2 w f64 1000000000.0
2439 end
2440 ret div get g_wins w f64 1000000000.0
2441end
2442
2443fn fit_accum i ulong ch ulong f2 ulong to void
2444 y = div f64 get g_we add mul i 4 ch f64 1000000.0
2445 for a ulong 3
2446 va = feat_v i f2 a
2447 put g_fnrm a add get g_fnrm a mul va va
2448 put g_fxty a add get g_fxty a mul va y
2449 for b ulong 3
2450 vb = feat_v i f2 b
2451 idx = add mul a 3 b
2452 put g_fxtx idx add get g_fxtx idx mul va vb
2453 end
2454 end
2455end
2456
2457fn win_is_train i ulong ci ulong to bool
2458 if ne get g_wst i TYE_OK
2459 ret false
2460 end
2461 if not er_eq_tt 1 mul i 16 6 mul ci 16 16
2462 ret false
2463 end
2464 ret not er_eq_t 2 mul i 8 8 t_c_ab
2465end
2466
2467fn win_is_ab i ulong ci ulong to bool
2468 if ne get g_wst i TYE_OK
2469 ret false
2470 end
2471 if not er_eq_tt 1 mul i 16 6 mul ci 16 16
2472 ret false
2473 end
2474 ret er_eq_t 2 mul i 8 8 t_c_ab
2475end
2476
2477fn fit_rss slot ulong ci ulong ch ulong f2 ulong to f64
2478 bo = mul slot 3
2479 nw = get g_nw 0
2480 rss = f64 0.0
2481 for i ulong nw
2482 if win_is_train i ci
2483 y = div f64 get g_we add mul i 4 ch f64 1000000.0
2484 p = f64 0.0
2485 for a ulong 3
2486 bv = get g_fbeta add bo a
2487 fv = feat_v i f2 a
2488 p = add p mul bv fv
2489 end
2490 df = sub y p
2491 rss = add rss mul df df
2492 end
2493 end
2494 ret rss
2495end
2496
2497fn fit_solve slot ulong to bool
2498 for a ulong 3
2499 for b ulong 3
2500 na = get g_fnrm a
2501 nb = get g_fnrm b
2502 sv = f64 0.0
2503 if and gt na f64 0.0 gt nb f64 0.0
2504 sv = div get g_fxtx add mul a 3 b fsqrt mul na nb
2505 end
2506 put g_fsc add mul a 3 b sv
2507 end
2508 end
2509 eig3_jacobi
2510 lo = get g_fev 0
2511 hi = get g_fev 0
2512 for k ulong 3
2513 ek = get g_fev k
2514 if lt ek lo
2515 lo = ek
2516 end
2517 if gt ek hi
2518 hi = ek
2519 end
2520 end
2521 cnd = div f64 1.0 f64 0.0
2522 if gt lo f64 0.0
2523 cnd = fsqrt div hi lo
2524 end
2525 put g_fcond slot cnd
2526 if not le cnd f64 10000.0
2527 put g_fdeg slot 1
2528 end
2529 if not inv3_to slot
2530 put g_fdeg slot 1
2531 ret false
2532 end
2533 bo = mul slot 3
2534 io = mul slot 9
2535 for a ulong 3
2536 s = f64 0.0
2537 for b ulong 3
2538 iv = get g_finv add io add mul a 3 b
2539 xy = get g_fxty b
2540 s = add s mul iv xy
2541 end
2542 put g_fbeta add bo a s
2543 end
2544 ret true
2545end
2546
2547fn run_fit slot ulong ci ulong ch ulong f2 ulong to void
2548 put g_fok slot 0
2549 put g_fdeg slot 0
2550 put g_fn slot 0
2551 put g_fs slot f64 0.0
2552 put g_fcond slot f64 0.0
2553 for a ulong 3
2554 put g_fbeta add mul slot 3 a f64 0.0
2555 put g_fxty a f64 0.0
2556 put g_fnrm a f64 0.0
2557 for b ulong 3
2558 put g_fxtx add mul a 3 b f64 0.0
2559 put g_finv add mul slot 9 add mul a 3 b f64 0.0
2560 end
2561 end
2562 nw = get g_nw 0
2563 cnt = ulong 0
2564 for i ulong nw
2565 if win_is_train i ci
2566 fit_accum i ch f2
2567 cnt = add cnt 1
2568 end
2569 end
2570 put g_fn slot cnt
2571 if le cnt 3
2572 ret
2573 end
2574 if not fit_solve slot
2575 ret
2576 end
2577 rss = fit_rss slot ci ch f2
2578 put g_fs slot fsqrt div rss f64 sub cnt 3
2579 put g_fok slot 1
2580end
2581
2582fn holdout_one slot ulong i ulong f2 ulong to f64
2583 p = f64 0.0
2584 h = f64 0.0
2585 for a ulong 3
2586 va = feat_v i f2 a
2587 bv = get g_fbeta add mul slot 3 a
2588 p = add p mul bv va
2589 for b ulong 3
2590 vb = feat_v i f2 b
2591 idx = add mul slot 9 add mul a 3 b
2592 iv = get g_finv idx
2593 h = add h mul va mul iv vb
2594 end
2595 end
2596 put g_ms 2 p
2597 ret h
2598end
2599
2600fn holdout_finish slot ulong hn ulong ins ulong to void
2601 mean_sd_sv hn 0
2602 me = get g_ms 0
2603 sd = get g_ms 1
2604 put g_hmerr 0 me
2605 mean_sd_sv hn 1
2606 put g_hmrel 0 get g_ms 0
2607 hf = div mul tq975 sub long hn 1 sd fsqrt f64 hn
2608 lo = sub me hf
2609 hi = add me hf
2610 put g_hlo 0 lo
2611 put g_hhi 0 hi
2612 q = f64 0.0
2613 for i ulong hn
2614 e = get g_sv i
2615 q = add q mul e e
2616 end
2617 put g_hrms 0 fsqrt div q f64 hn
2618 if eq get g_fdeg slot 0
2619 if and le lo f64 0.0 ge hi f64 0.0
2620 if ge mul ins 5 mul hn 4
2621 put g_hpass slot 1
2622 end
2623 end
2624 end
2625end
2626
2627fn holdout_step slot ulong i ulong ch ulong f2 ulong hn ulong to bool
2628 ew = get g_we add mul i 4 ch
2629 y = div f64 ew f64 1000000.0
2630 h = holdout_one slot i f2
2631 tp = tq975 sub long get g_fn slot 3
2632 hf = mul tp mul get g_fs slot fsqrt add f64 1.0 h
2633 er = sub y get g_ms 2
2634 put g_sv hn er
2635 put g_sv2 hn div er y
2636 ret le er_fabs er hf
2637end
2638
2639fn run_holdout slot ulong ci ulong ch ulong f2 ulong to void
2640 nan = div f64 0.0 f64 0.0
2641 put g_hn 0 0
2642 put g_hins 0 0
2643 put g_hpass slot 0
2644 put g_hmerr 0 nan
2645 put g_hlo 0 nan
2646 put g_hhi 0 nan
2647 put g_hrms 0 nan
2648 put g_hmrel 0 f64 0.0
2649 if eq get g_fok slot 0
2650 ret
2651 end
2652 nw = get g_nw 0
2653 hn = ulong 0
2654 ins = ulong 0
2655 for i ulong nw
2656 if and lt hn 256 win_is_ab i ci
2657 if holdout_step slot i ch f2 hn
2658 ins = add ins 1
2659 end
2660 hn = add hn 1
2661 end
2662 end
2663 put g_hn 0 hn
2664 put g_hins 0 ins
2665 if lt hn 2
2666 ret
2667 end
2668 holdout_finish slot hn ins
2669end
2670
2671note ---- Attribution ----
2672fn set_at_str dst ulong off ulong cap ulong s view uchar * to void
2673 n = len s
2674 for i ulong cap
2675 c = uchar 0
2676 if lt i n
2677 c = get s i
2678 end
2679 if eq dst 0
2680 put g_ta_mth add off i c
2681 else
2682 put g_ts_who add off i c
2683 end
2684 end
2685end
2686
2687fn add_share who view uchar * uj long cf ulong to void
2688 i = get g_ta_nsh 0
2689 put g_ta_nsh 0 add i 1
2690 set_at_str 1 mul i 8 8 who
2691 put g_ts_uj i uj
2692 put g_ts_cf i cf
2693end
2694
2695fn add_share_tp ps ulong uj long cf ulong to void
2696 i = get g_ta_nsh 0
2697 put g_ta_nsh 0 add i 1
2698 for j ulong 8
2699 put g_ts_who add mul i 8 j get g_tp_tag add mul ps 8 j
2700 end
2701 put g_ts_uj i uj
2702 put g_ts_cf i cf
2703end
2704
2705fn emit_attr r ulong to void
2706 _d = ty_attribution_digest 0 560
2707 er_puts 0 t_a_pref
2708 er_put_cs 0 5 mul r 16 16
2709 er_puts 0 t_w_r
2710 er_put_u64 0 get g_rrnd r
2711 er_putc 0 uchar 32
2712 er_put_cs 0 7 0 32
2713 er_putc 0 uchar 32
2714 er_put_status 0 get g_ta_vd 0
2715 er_puts 0 t_a_ms
2716 er_put_i64 0 get g_ta_muj 0
2717 er_puts 0 t_a_al
2718 er_put_i64 0 get g_ta_auj 0
2719 er_puts 0 t_a_un
2720 er_put_i64 0 get g_ta_ruj 0
2721 er_puts 0 t_a_tl
2722 er_put_u64 0 get g_ta_tol 0
2723 er_puts 0 t_a_ov
2724 ovc = get g_ta_ov 0
2725 if ne get g_ta_au 0 0
2726 ovc = TY_UNATTRIBUTED
2727 end
2728 er_put_conf 0 ovc
2729 er_putc 0 uchar 10
2730 if lt get g_rfd 0 0
2731 ret
2732 end
2733 er_puts 2 t_rj_w1
2734 er_puts 2 t_ty_dom_att
2735 er_puts 2 t_rj_w2
2736 er_put_hex 2 560 64
2737 er_puts 2 t_rj_w4
2738 er_put_cs 2 5 mul r 16 16
2739 er_puts 2 t_rj_w6
2740 er_put_u64 2 get g_rrnd r
2741 er_puts 2 t_rj_a4
2742 er_put_cs 2 7 0 32
2743 er_puts 2 t_rj_a5
2744 er_put_hex 2 528 64
2745 er_puts 2 t_rj_a6
2746 er_put_status 2 get g_ta_vd 0
2747 er_puts 2 t_rj_a7
2748 er_put_i64 2 get g_ta_muj 0
2749 er_puts 2 t_rj_a8
2750 er_put_i64 2 get g_ta_auj 0
2751 er_puts 2 t_rj_a9
2752 er_put_i64 2 get g_ta_ruj 0
2753 er_puts 2 t_rj_a10
2754 er_put_u64 2 get g_ta_tol 0
2755 er_puts 2 t_rj_a11
2756 er_put_conf 2 get g_ta_ov 0
2757 er_puts 2 t_rj_a12
2758 er_put_u64 2 ty_conf_forge get g_ta_ov 0
2759 er_puts 2 t_rj_a13
2760 nsh = get g_ta_nsh 0
2761 for i ulong nsh
2762 if gt i 0
2763 er_putc 2 uchar 44
2764 end
2765 er_puts 2 t_rj_s1
2766 er_put_cs 2 8 mul i 8 8
2767 er_puts 2 t_rj_s2
2768 er_put_i64 2 get g_ts_uj i
2769 er_puts 2 t_rj_s3
2770 er_put_conf 2 get g_ts_cf i
2771 er_puts 2 t_rj_s4
2772 er_put_u64 2 ty_conf_forge get g_ts_cf i
2773 er_putc 2 uchar 125
2774 end
2775 er_puts 2 t_rj_a14
2776end
2777
2778fn tol_uj r ulong sdi f64 to ulong
2779 t = get g_rb95 r
2780 if and not er_is_nan sdi gt mul f64 1.96 sdi t
2781 t = mul f64 1.96 sdi
2782 end
2783 if er_is_nan t
2784 ret 0
2785 end
2786 v = er_llround mul t f64 1000000.0
2787 if lt v 0
2788 ret 0
2789 end
2790 ret ulong v
2791end
2792
2793fn clear_attr0 to void
2794 for i ulong 32
2795 put g_ta_mth i uchar 0
2796 end
2797 put g_ta_ch 0 0
2798 put g_ta_tol 0 0
2799 put g_ta_au 0 0
2800 put g_ta_nsh 0 0
2801 put g_ta_nsr 0 0
2802 put g_ta_muj 0 0
2803 put g_ta_auj 0 0
2804 put g_ta_ruj 0 0
2805 put g_ta_vd 0 0
2806 put g_ta_ov 0 0
2807end
2808
2809fn attr_solo r ulong sdi f64 to ulong
2810 w3 = get g_rc add mul r 4 3
2811 if lt w3 0
2812 ret TYE_E_CONDITION
2813 end
2814 clear_attr0
2815 set_at_str 0 0 32 t_m_solo
2816 put g_ta_ch 0 long TY_CH_PKG
2817 put g_ta_tol 0 tol_uj r sdi
2818 w0 = get g_rc mul r 4
2819 w1 = get g_rc add mul r 4 1
2820 w2 = get g_rc add mul r 4 2
2821 e0 = long 0
2822 if ge w0 0
2823 e0 = get g_we mul ulong w0 4
2824 add_share t_c_idle e0 TY_COUNTERFACTUAL
2825 nsr = get g_ta_nsr 0
2826 tdg_cp add 512 nsr ulong w0
2827 put g_ta_nsr 0 add nsr 1
2828 end
2829 if ge w1 0
2830 add_share t_c_a sub get g_we mul ulong w1 4 e0 TY_COUNTERFACTUAL
2831 nsr = get g_ta_nsr 0
2832 tdg_cp add 512 nsr ulong w1
2833 put g_ta_nsr 0 add nsr 1
2834 end
2835 if ge w2 0
2836 add_share t_c_b sub get g_we mul ulong w2 4 e0 TY_COUNTERFACTUAL
2837 nsr = get g_ta_nsr 0
2838 tdg_cp add 512 nsr ulong w2
2839 put g_ta_nsr 0 add nsr 1
2840 end
2841 _st = ty_attribution_check 0 ulong w3
2842 if and ne get g_rst r TYE_OK eq get g_ta_vd 0 TYE_OK
2843 put g_ta_vd 0 get g_rst r
2844 end
2845 emit_attr r
2846 ret get g_ta_vd 0
2847end
2848
2849fn attr_model r ulong sdi f64 to ulong
2850 w3 = get g_rc add mul r 4 3
2851 if or lt w3 0 eq get g_fok 0 0
2852 ret TYE_E_ARG
2853 end
2854 clear_attr0
2855 set_at_str 0 0 32 t_m_mod
2856 put g_ta_ch 0 long TY_CH_PKG
2857 put g_ta_tol 0 tol_uj r sdi
2858 add_share t_c_idle er_llround mul get g_fbeta 0 f64 1000000.0 TY_MODELLED
2859 uw = ulong w3
2860 np = get g_tw_np uw
2861 for i ulong np
2862 ps = add mul uw 4 i
2863 t1 = mul get g_fbeta 1 div f64 get g_tp_cy ps f64 1000000000.0
2864 t2 = mul get g_fbeta 2 div f64 get g_tp_in ps f64 1000000000.0
2865 add_share_tp ps er_llround mul add t1 t2 f64 1000000.0 TY_COUNTER_DERIVED
2866 end
2867 for c ulong 3
2868 wc = get g_rc add mul r 4 c
2869 if ge wc 0
2870 nsr = get g_ta_nsr 0
2871 tdg_cp add 512 nsr ulong wc
2872 put g_ta_nsr 0 add nsr 1
2873 end
2874 end
2875 _st = ty_attribution_check 0 uw
2876 emit_attr r
2877 ret get g_ta_vd 0
2878end
2879
2880note ---- Modes & helpers ----
2881fn print_windows to void
2882 nw = get g_nw 0
2883 for i ulong nw
2884 er_puts 0 t_w_pref
2885 er_put_cs 0 0 mul i 256 256
2886 er_putc 0 uchar 32
2887 er_put_cs 0 1 mul i 16 16
2888 er_putc 0 uchar 32
2889 er_put_cs 0 2 mul i 8 8
2890 er_puts 0 t_w_r
2891 er_put_u64 0 get g_tw_rnd i
2892 er_putc 0 uchar 32
2893 er_put_status 0 get g_wst i
2894 er_puts 0 t_w_pkg
2895 er_put_i64 0 get g_we mul i 4
2896 er_puts 0 t_w_smp
2897 er_put_u64 0 get g_tw_ns i
2898 er_puts 0 t_w_gap
2899 er_put_u64 0 div get g_tw_mg i 1000000
2900 er_puts 0 t_w_fb
2901 er_put_u64 0 get g_tw_fb i
2902 er_puts 0 t_w_dig
2903 er_put_hex 0 i 16
2904 er_putc 0 uchar 10
2905 if ge get g_rfd 0 0
2906 er_puts 2 t_rj_w1
2907 er_puts 2 t_ty_dom_int
2908 er_puts 2 t_rj_w2
2909 er_put_hex 2 i 64
2910 er_puts 2 t_rj_w3
2911 er_put_cs 2 0 mul i 256 256
2912 er_puts 2 t_rj_w4
2913 er_put_cs 2 1 mul i 16 16
2914 er_puts 2 t_rj_w5
2915 er_put_cs 2 2 mul i 8 8
2916 er_puts 2 t_rj_w6
2917 er_put_u64 2 get g_tw_rnd i
2918 er_puts 2 t_rj_w7
2919 er_put_u64 2 get g_tw_pos i
2920 er_puts 2 t_rj_w8
2921 er_put_status 2 get g_wst i
2922 er_puts 2 t_rj_w9
2923 er_put_u64 2 ty_conf_forge TY_DIRECT
2924 er_puts 2 t_rj_w10
2925 er_put_i64 2 get g_we mul i 4
2926 er_puts 2 t_rj_w11
2927 er_put_i64 2 get g_we add mul i 4 1
2928 er_puts 2 t_rj_w12
2929 er_put_i64 2 get g_we add mul i 4 2
2930 er_puts 2 t_rj_w13
2931 er_put_u64 2 get g_tw_ns i
2932 er_puts 2 t_rj_w14
2933 er_put_u64 2 get g_tw_mg i
2934 er_puts 2 t_rj_w15
2935 er_put_u64 2 get g_tw_fb i
2936 er_puts 2 t_rj_w16
2937 er_put_u64 2 get g_tw_mt i
2938 er_puts 2 t_rj_w17
2939 end
2940 end
2941end
2942
2943fn collect_configs to void
2944 put g_ncfg 0 0
2945 nw = get g_nw 0
2946 for i ulong nw
2947 nc = get g_ncfg 0
2948 seen = false
2949 for k ulong nc
2950 if er_eq_tt 6 mul k 16 1 mul i 16 16
2951 seen = true
2952 end
2953 end
2954 if and not seen lt nc 32
2955 for j ulong 16
2956 put g_cfgs add mul nc 16 j get g_tw_cfg add mul i 16 j
2957 end
2958 put g_ncfg 0 add nc 1
2959 end
2960 end
2961end
2962
2963fn sd_i_pkg_tbl tbl ulong off ulong to f64
2964 nr = get g_nr 0
2965 n = ulong 0
2966 for k ulong nr
2967 if and lt n 512 and er_eq_tt 5 mul k 16 tbl off 16 eq get g_rst k TYE_OK
2968 put g_sv n get g_ri mul k 4
2969 n = add n 1
2970 end
2971 end
2972 mean_sd_sv n 0
2973 ret get g_ms 1
2974end
2975
2976fn calls_of w ulong tag view uchar * to ulong
2977 np = get g_tw_np w
2978 for i ulong np
2979 ps = add mul w 4 i
2980 if er_eq_t 9 mul ps 8 8 tag
2981 ret get g_tp_cl ps
2982 end
2983 end
2984 ret 0
2985end
2986
2987fn i_stats ci ulong ch ulong to void
2988 nr = get g_nr 0
2989 n = ulong 0
2990 for k ulong nr
2991 if and lt n 512 and er_eq_tt 5 mul k 16 6 mul ci 16 16 eq get g_rst k TYE_OK
2992 put g_sv n get g_ri add mul k 4 ch
2993 n = add n 1
2994 end
2995 end
2996 put g_isn 0 n
2997 mean_sd_sv n 0
2998 m = get g_ms 0
2999 sd = get g_ms 1
3000 hf = div f64 0.0 f64 0.0
3001 if gt n 1
3002 hf = div mul tq975 sub long n 1 sd fsqrt f64 n
3003 end
3004 put g_is 0 m
3005 put g_is 1 sub m hf
3006 put g_is 2 add m hf
3007 boot_mean_sv n
3008 put g_is 3 get g_ms 2
3009 put g_is 4 get g_ms 3
3010end
3011
3012fn mode_check to int
3013 print_windows
3014 nr = get g_nr 0
3015 for k ulong nr
3016 er_puts 0 t_r_pref
3017 er_put_cs 0 5 mul k 16 16
3018 er_puts 0 t_w_r
3019 er_put_u64 0 get g_rrnd k
3020 er_putc 0 uchar 32
3021 er_put_status 0 get g_rst k
3022 er_puts 0 t_r_ip
3023 er_put_fix 0 get g_ri mul k 4 3
3024 er_puts 0 t_r_b95
3025 er_put_fix 0 get g_rb95 k 3
3026 er_putc 0 uchar 10
3027 end
3028 for k ulong nr
3029 _st = attr_solo k sd_i_pkg_tbl 5 mul k 16
3030 end
3031 er_flush 0
3032 ret 0
3033end
3034
3035fn stage1_round k ulong ns ulong to ulong
3036 w1 = get g_rc add mul k 4 1
3037 w2 = get g_rc add mul k 4 2
3038 w3 = get g_rc add mul k 4 3
3039 ok = false
3040 if and ge w1 0 and ge w2 0 ge w3 0
3041 if eq get g_wst ulong w1 TYE_OK
3042 if eq get g_wst ulong w2 TYE_OK
3043 if eq get g_wst ulong w3 TYE_OK
3044 ok = true
3045 end
3046 end
3047 end
3048 end
3049 o_open_anon uchar 123
3050 o_u t_ok_rnd get g_rrnd k
3051 if ok
3052 o_str t_ok_st t_ok_str
3053 sa = sub f64 1.0 div f64 calls_of ulong w3 t_c_a f64 calls_of ulong w1 t_c_a
3054 sb = sub f64 1.0 div f64 calls_of ulong w3 t_c_b f64 calls_of ulong w2 t_c_b
3055 sc = div add sa sb f64 2.0
3056 o_num t_ok_sla sa
3057 o_num t_ok_slb sb
3058 o_num t_ok_sc sc
3059 o_num t_ok_ipj get g_ri mul k 4
3060 o_num t_ok_icj get g_ri add mul k 4 2
3061 o_num t_ok_irj get g_ri add mul k 4 3
3062 put g_sv ns sc
3063 if lt sc f64 0.02
3064 put g_tidx 1 0
3065 end
3066 o_close uchar 125
3067 ret add ns 1
3068 end
3069 o_str t_ok_st t_ref_str
3070 o_close uchar 125
3071 ret ns
3072end
3073
3074fn stage1_cfg c ulong to void
3075 ns = ulong 0
3076 put g_tidx 1 1
3077 o_open_anon uchar 123
3078 o_str_cs t_ok_cfg 6 mul c 16 16
3079 o_open t_ok_rnds uchar 91
3080 nr = get g_nr 0
3081 for k ulong nr
3082 if and lt ns 64 er_eq_tt 5 mul k 16 6 mul c 16 16
3083 ns = stage1_round k ns
3084 end
3085 end
3086 o_close uchar 93
3087 mean_sd_sv ns 0
3088 m = get g_ms 0
3089 o_num t_ok_sc m
3090 o_u t_ok_vr ns
3091 i_stats c 0
3092 o_num t_ok_ipm get g_is 0
3093 o_num t_ok_iplo get g_is 1
3094 o_num t_ok_iphi get g_is 2
3095 nw = get g_nw 0
3096 any = long -1
3097 for i ulong nw
3098 if and lt any 0 er_eq_tt 1 mul i 16 6 mul c 16 16
3099 any = long i
3100 end
3101 end
3102 if ge any 0
3103 o_str_cs t_ok_sa 3 mul ulong any 64 64
3104 o_str_cs t_ok_sb 4 mul ulong any 64 64
3105 else
3106 o_str t_ok_sa t_er_dash
3107 o_str t_ok_sb t_er_dash
3108 end
3109 o_close uchar 125
3110 er_puts 0 t_p_cfg
3111 er_put_cs 0 6 mul c 16 16
3112 er_puts 0 t_p_sc
3113 er_put_fix 0 m 4
3114 er_puts 0 t_p_rds
3115 er_put_u64 0 ns
3116 er_puts 0 t_p_all
3117 er_put_u64 0 get g_tidx 1
3118 er_puts 0 t_p_ipk
3119 er_put_fix 0 get g_is 0 2
3120 er_puts 0 t_p_jb
3121 er_put_fix 0 get g_is 1 2
3122 er_puts 0 t_p_cs
3123 er_put_fix 0 get g_is 2 2
3124 er_puts 0 t_p_ra
3125 if ge any 0
3126 er_put_cs 0 3 mul ulong any 64 64
3127 end
3128 er_puts 0 t_p_b
3129 if ge any 0
3130 er_put_cs 0 4 mul ulong any 64 64
3131 end
3132 er_putc 0 uchar 10
3133 put g_tidx 0 ns
3134 put g_ms 2 m
3135end
3136
3137fn mode_stage1 to int
3138 print_windows
3139 collect_configs
3140 fd = fopen g_ea3 0 get g_eal 3 577 420
3141 if lt fd 0
3142 ret 2
3143 end
3144 put g_ofd 0 fd
3145 put g_fstack 0 1
3146 put g_fdepth 0 0
3147 o_open_anon uchar 123
3148 o_str t_ok_proto t_s1_proto
3149 o_open t_ok_cfgs uchar 91
3150 best = div f64 -1.0 f64 0.0
3151 least = div f64 1.0 f64 0.0
3152 ibest = long -1
3153 ileast = long -1
3154 breal = false
3155 nc = get g_ncfg 0
3156 for c ulong nc
3157 stage1_cfg c
3158 ns = get g_tidx 0
3159 m = get g_ms 2
3160 if and gt ns 0 gt m best
3161 best = m
3162 ibest = long c
3163 breal = and ge m f64 0.05 ne get g_tidx 1 0
3164 end
3165 if and gt ns 0 lt er_fabs m least
3166 least = er_fabs m
3167 ileast = long c
3168 end
3169 end
3170 o_close uchar 93
3171 o_open t_ok_sel uchar 123
3172 o_str t_ok_rule t_s1_rule
3173 if ge ibest 0
3174 o_str_cs t_ok_cont 6 mul ulong ibest 16 16
3175 else
3176 o_str_cs t_ok_cont 6 0 0
3177 end
3178 o_bool t_ok_rcont breal
3179 if ge ileast 0
3180 o_str_cs t_ok_ctrl 6 mul ulong ileast 16 16
3181 else
3182 o_str_cs t_ok_ctrl 6 0 0
3183 end
3184 o_close uchar 125
3185 o_close uchar 125
3186 er_putc 1 uchar 10
3187 er_flush 1
3188 _fc = fclose int fd
3189 put g_ofd 0 -1
3190 er_puts 0 t_p_sel
3191 if ge ibest 0
3192 er_put_cs 0 6 mul ulong ibest 16 16
3193 else
3194 er_puts 0 t_er_dash
3195 end
3196 er_puts 0 t_p_rl
3197 if breal
3198 er_putc 0 uchar 49
3199 else
3200 er_putc 0 uchar 48
3201 end
3202 er_puts 0 t_p_ct
3203 if ge ileast 0
3204 er_put_cs 0 6 mul ulong ileast 16 16
3205 else
3206 er_puts 0 t_er_dash
3207 end
3208 er_putc 0 uchar 10
3209 er_flush 0
3210 ret 0
3211end
3212
3213fn put_ch_name ch ulong chx ulong to void
3214 if eq chx 0
3215 er_puts ch t_ch_pkg
3216 else
3217 if eq chx 1
3218 er_puts ch t_ch_ce
3219 else
3220 if eq chx 2
3221 er_puts ch t_ch_cp
3222 else
3223 er_puts ch t_ch_rs
3224 end
3225 end
3226 end
3227end
3228
3229fn fit_block key view uchar * slot ulong ci ulong ch ulong f2 ulong to void
3230 run_fit slot ci ch f2
3231 run_holdout slot ci ch f2
3232 o_open key uchar 123
3233 if eq ch 0
3234 o_str t_ok_tgt t_ch_pkg
3235 else
3236 if eq ch 1
3237 o_str t_ok_tgt t_ch_ce
3238 else
3239 if eq ch 2
3240 o_str t_ok_tgt t_ch_cp
3241 else
3242 o_str t_ok_tgt t_ch_rs
3243 end
3244 end
3245 end
3246 if ne f2 0
3247 o_str t_ok_feat t_s2_fi1
3248 else
3249 o_str t_ok_feat t_s2_fi0
3250 end
3251 bo = mul slot 3
3252 o_u t_ok_fw get g_fn slot
3253 o_bool t_ok_fok ne get g_fok slot 0
3254 o_num t_ok_p0t get g_fbeta bo
3255 o_num t_ok_acyc get g_fbeta add bo 1
3256 if ne f2 0
3257 o_num t_ok_bl2 get g_fbeta add bo 2
3258 else
3259 o_num t_ok_bins get g_fbeta add bo 2
3260 end
3261 o_num t_ok_rsd get g_fs slot
3262 o_num t_ok_cns get g_fcond slot
3263 o_bool t_ok_deg ne get g_fdeg slot 0
3264 o_u t_ok_hw get g_hn 0
3265 o_num t_ok_hme get g_hmerr 0
3266 o_num t_ok_hlo get g_hlo 0
3267 o_num t_ok_hhi get g_hhi 0
3268 o_num t_ok_hrms get g_hrms 0
3269 o_num t_ok_hrel get g_hmrel 0
3270 o_u t_ok_h95 get g_hins 0
3271 o_bool t_ok_pass ne get g_hpass slot 0
3272 o_close uchar 125
3273 er_puts 0 t_p_fit
3274 er_put_cs 0 6 mul ci 16 16
3275 er_putc 0 uchar 32
3276 put_ch_name 0 ch
3277 er_putc 0 uchar 32
3278 if ne f2 0
3279 er_puts 0 t_l2_s
3280 else
3281 er_puts 0 t_inst_s
3282 end
3283 er_puts 0 t_p_n
3284 er_put_u64 0 get g_fn slot
3285 er_puts 0 t_p_cd
3286 er_put_fix 0 get g_fcond slot 1
3287 er_puts 0 t_p_s
3288 er_put_fix 0 get g_fs slot 3
3289 er_puts 0 t_p_ho
3290 er_put_u64 0 get g_hn 0
3291 er_puts 0 t_p_me
3292 er_put_fix 0 get g_hmerr 0 3
3293 er_puts 0 t_p_jb
3294 er_put_fix 0 get g_hlo 0 3
3295 er_puts 0 t_p_cs
3296 er_put_fix 0 get g_hhi 0 3
3297 er_puts 0 t_p_in
3298 er_put_u64 0 get g_hins 0
3299 er_putc 0 uchar 47
3300 er_put_u64 0 get g_hn 0
3301 er_puts 0 t_p_rm
3302 er_put_fix 0 get g_hrms 0 3
3303 er_puts 0 t_p_re
3304 er_put_fix 0 get g_hmrel 0 4
3305 er_puts 0 t_p_ps
3306 er_put_u64 0 get g_hpass slot
3307 er_putc 0 uchar 10
3308end
3309
3310fn stage2_conds c ulong to void
3311 nw = get g_nw 0
3312 o_open t_ok_cnds uchar 123
3313 for k ulong 4
3314 n = ulong 0
3315 for i ulong nw
3316 if and lt n 512 and er_eq_tt 1 mul i 16 6 mul c 16 16 eq get g_wst i TYE_OK
3317 if eq cond_idx i long k
3318 put g_sv n div f64 get g_we mul i 4 f64 1000000.0
3319 n = add n 1
3320 end
3321 end
3322 end
3323 mean_sd_sv n 0
3324 if eq k 0
3325 o_open t_c_idle uchar 123
3326 else
3327 if eq k 1
3328 o_open t_c_a uchar 123
3329 else
3330 if eq k 2
3331 o_open t_c_b uchar 123
3332 else
3333 o_open t_c_ab uchar 123
3334 end
3335 end
3336 end
3337 o_u t_ok_vw n
3338 o_num t_ok_pmj get g_ms 0
3339 o_num t_ok_psj get g_ms 1
3340 o_close uchar 125
3341 end
3342 o_close uchar 125
3343end
3344
3345fn stage2_ch_pkg c ulong n ulong lo f64 hi f64 tot_r ulong to void
3346 nw = get g_nw 0
3347 k2 = ulong 0
3348 for i ulong nw
3349 if and lt k2 512 win_is_ab i c
3350 ew = get g_we mul i 4
3351 put g_sv k2 div f64 ew f64 1000000.0
3352 k2 = add k2 1
3353 end
3354 end
3355 mean_sd_sv k2 0
3356 eab = get g_ms 0
3357 hf = div sub hi lo f64 2.0
3358 pa = ulong 0
3359 if ge n 8
3360 if ge mul n 10 mul tot_r 8
3361 if le hf mul f64 0.02 eab
3362 pa = 1
3363 end
3364 end
3365 end
3366 ds = ulong 0
3367 if or gt lo f64 0.0 lt hi f64 0.0
3368 ds = 1
3369 end
3370 put g_tidx 0 pa
3371 put g_tidx 1 ds
3372 o_num t_ok_eab eab
3373 o_num t_ok_phw div hf eab
3374end
3375
3376fn stage2_ch c ulong ch ulong tot_r ulong to void
3377 i_stats c ch
3378 m = get g_is 0
3379 lo = get g_is 1
3380 hi = get g_is 2
3381 n = get g_isn 0
3382 if eq ch 0
3383 o_open t_ch_pkg uchar 123
3384 else
3385 if eq ch 1
3386 o_open t_ch_ce uchar 123
3387 else
3388 if eq ch 2
3389 o_open t_ch_cp uchar 123
3390 else
3391 o_open t_ch_rs uchar 123
3392 end
3393 end
3394 end
3395 o_u t_ok_vr n
3396 o_num t_ok_imj m
3397 o_num t_ok_itlo lo
3398 o_num t_ok_ithi hi
3399 o_num t_ok_iblo get g_is 3
3400 o_num t_ok_ibhi get g_is 4
3401 o_bool t_ok_dfz or gt lo f64 0.0 lt hi f64 0.0
3402 o_close uchar 125
3403 er_puts 0 t_p_i
3404 er_put_cs 0 6 mul c 16 16
3405 er_putc 0 uchar 32
3406 put_ch_name 0 ch
3407 er_puts 0 t_p_n
3408 er_put_u64 0 n
3409 er_puts 0 t_p_mn
3410 er_put_fix 0 m 3
3411 er_puts 0 t_p_t95
3412 er_put_fix 0 lo 3
3413 er_puts 0 t_p_cs
3414 er_put_fix 0 hi 3
3415 er_puts 0 t_p_b95
3416 er_put_fix 0 get g_is 3 3
3417 er_puts 0 t_p_cs
3418 er_put_fix 0 get g_is 4 3
3419 er_putc 0 uchar 93
3420 er_putc 0 uchar 10
3421 if eq ch 0
3422 stage2_ch_pkg c n lo hi tot_r
3423 end
3424end
3425
3426fn stage2_attr c ulong to void
3427 nr = get g_nr 0
3428 sdi = sd_i_pkg_tbl 6 mul c 16
3429 sok = ulong 0
3430 sn = ulong 0
3431 mok = ulong 0
3432 mn = ulong 0
3433 for k ulong nr
3434 if and er_eq_tt 5 mul k 16 6 mul c 16 16 eq get g_rst k TYE_OK
3435 sn = add sn 1
3436 if eq attr_solo k sdi TYE_OK
3437 sok = add sok 1
3438 end
3439 if ne get g_hpass 0 0
3440 mn = add mn 1
3441 if eq attr_model k sdi TYE_OK
3442 mok = add mok 1
3443 end
3444 end
3445 end
3446 end
3447 o_open t_ok_attr uchar 123
3448 o_u t_ok_scc sok
3449 o_u t_ok_sct sn
3450 o_u t_ok_cmc mok
3451 o_u t_ok_cmt mn
3452 o_num t_ok_tsd sdi
3453 o_close uchar 125
3454end
3455
3456fn stage2_cfg c ulong to void
3457 nr = get g_nr 0
3458 o_open_anon uchar 123
3459 o_str_cs t_ok_cfg 6 mul c 16 16
3460 stage2_conds c
3461 tot_r = ulong 0
3462 for k ulong nr
3463 if er_eq_tt 5 mul k 16 6 mul c 16 16
3464 tot_r = add tot_r 1
3465 end
3466 end
3467 o_open t_ok_cha uchar 123
3468 put g_tidx 0 0
3469 put g_tidx 1 0
3470 for ch ulong 4
3471 stage2_ch c ch tot_r
3472 end
3473 bsum = f64 0.0
3474 bn = ulong 0
3475 for k ulong nr
3476 if and er_eq_tt 5 mul k 16 6 mul c 16 16 eq get g_rst k TYE_OK
3477 bsum = add bsum get g_rb95 k
3478 bn = add bn 1
3479 end
3480 end
3481 if gt bn 0
3482 o_num t_ok_aeb div bsum f64 bn
3483 else
3484 o_num t_ok_aeb div f64 0.0 f64 0.0
3485 end
3486 o_u t_ok_rtot tot_r
3487 o_bool t_ok_pass ne get g_tidx 0 0
3488 o_bool t_ok_ipdfz ne get g_tidx 1 0
3489 o_close uchar 125
3490 er_puts 0 t_p_ca
3491 er_put_cs 0 6 mul c 16 16
3492 er_puts 0 t_p_ps
3493 er_put_u64 0 get g_tidx 0
3494 er_puts 0 t_p_di
3495 er_put_u64 0 get g_tidx 1
3496 er_puts 0 t_p_rds
3497 er_put_u64 0 tot_r
3498 er_putc 0 uchar 10
3499 o_open t_ok_chb uchar 123
3500 fit_block t_ok_pci 0 c 0 0
3501 o_bool t_ok_pass ne get g_hpass 0 0
3502 o_open t_ok_rng uchar 123
3503 fit_block t_ok_cpci 1 c 2 0
3504 fit_block t_ok_pcl2 1 c 0 1
3505 o_str t_ok_na t_s2_na
3506 o_close uchar 125
3507 o_close uchar 125
3508 er_puts 0 t_p_cb
3509 er_put_cs 0 6 mul c 16 16
3510 er_puts 0 t_p_ps
3511 er_put_u64 0 get g_hpass 0
3512 er_putc 0 uchar 10
3513 stage2_attr c
3514 o_close uchar 125
3515end
3516
3517fn mode_stage2 to int
3518 rfd = fopen g_ea4 0 get g_eal 4 577 420
3519 if lt rfd 0
3520 ret 2
3521 end
3522 put g_rfd 0 rfd
3523 print_windows
3524 collect_configs
3525 fd = fopen g_ea3 0 get g_eal 3 577 420
3526 if lt fd 0
3527 _fc = fclose int rfd
3528 ret 2
3529 end
3530 put g_ofd 0 fd
3531 put g_fstack 0 1
3532 put g_fdepth 0 0
3533 o_open_anon uchar 123
3534 o_str t_ok_proto t_s2_proto
3535 o_open t_ok_mtr uchar 123
3536 o_u t_ok_resuj TY_RESOLUTION_UJ
3537 o_u t_ok_spns TY_SAMPLE_PERIOD_NS
3538 o_str t_ok_wrp t_s2_wrap
3539 o_str t_ok_acc t_s2_acc
3540 o_close uchar 125
3541 o_open t_ok_cfgs uchar 91
3542 nc = get g_ncfg 0
3543 for c ulong nc
3544 stage2_cfg c
3545 end
3546 o_close uchar 93
3547 o_close uchar 125
3548 er_putc 1 uchar 10
3549 er_flush 1
3550 er_flush 2
3551 er_flush 0
3552 _fc1 = fclose int fd
3553 _fc2 = fclose int rfd
3554 put g_ofd 0 -1
3555 put g_rfd 0 -1
3556 ret 0
3557end
3558
3559fn eq_arg1 s view uchar * to bool
3560 n = get g_eal 1
3561 if ne n len s
3562 ret false
3563 end
3564 for i ulong n
3565 if ne get g_ea1 i get s i
3566 ret false
3567 end
3568 end
3569 ret true
3570end
3571
3572fn main to int
3573 init_tq
3574 put g_ofd 0 -1
3575 put g_rfd 0 -1
3576 ac = argcnt
3577 if lt ac 3
3578 er_stderr t_er_usg
3579 ret 64
3580 end
3581 l1 = argread 1 g_ea1 0 255
3582 put g_eal 1 ulong l1
3583 l2 = argread 2 g_ea2 0 1023
3584 put g_eal 2 ulong l2
3585 if ge ac 4
3586 l3 = argread 3 g_ea3 0 1023
3587 put g_eal 3 ulong l3
3588 end
3589 if ge ac 5
3590 l4 = argread 4 g_ea4 0 1023
3591 put g_eal 4 ulong l4
3592 end
3593 if lt load_dir 0
3594 er_stderr t_er_cant
3595 _w = write 2 g_ea2 0 get g_eal 2
3596 er_stderr t_er_nl
3597 ret 2
3598 end
3599 build_rounds
3600 if eq_arg1 t_er_chk
3601 ret mode_check
3602 end
3603 if and eq_arg1 t_er_st1 eq ac 4
3604 ret mode_stage1
3605 end
3606 if and eq_arg1 t_er_st2 eq ac 5
3607 ret mode_stage2
3608 end
3609 er_stderr t_er_badarg
3610 ret 64
3611end