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 / dual_experiment / applic.om

1648 lines · 41236 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note DUAL applicability experiment (ARCH-0031 / ADR 0031, offline evidence lane):
5note pure seeded OM-2 simulator, aggregate (macroscopic) predictor, heterogeneity
6note measurements, campaign driver, canonical formatting, SHA-256 digests, and
7note Pearson correlation. Zero heap, zero syscalls, zero host words.
8om2
9
10note ---- pre-registered experiment constants (DUAL-APPLIC-V1) ----
11const AP_WINDOW ulong 20
12const AP_WINDOWS ulong 100
13const AP_STEPS ulong 2000
14const AP_WARMUP_WINDOWS ulong 5
15
16const AP_SLOTS ulong 32
17const AP_MEMORY ulong 1024
18const AP_MAX_QUEUE ulong 256
19const AP_CLASSES ulong 4
20const AP_MAX_ITEMS ulong 32768
21
22const AP_HOM_DEMAND_MIN ulong 11
23const AP_HOM_DEMAND_MAX ulong 13
24const AP_HOM_MEM_MIN ulong 16
25const AP_HOM_MEM_MAX ulong 24
26const AP_HOM_SLOTS ulong 1
27
28const AP_HET_DEMAND_CAP ulong 400
29const AP_HET_MEM_BASE ulong 8
30
31const AP_SQUARE_HALF_PERIOD ulong 100
32const AP_BURST_MIN ulong 15
33const AP_BURST_MAX ulong 35
34const AP_CAP_DROP_SLOTS ulong 20
35const AP_SWITCH_STEP ulong 1000
36
37const AP_WL_COUNT ulong 8
38const AP_SEEDS ulong 24
39const AP_NROWS ulong 192
40const AP_SEED_BASE ulong 0x4455414c2d415031
41
42note ---- slot-indexed state (2 sim slots, 2 row-table slots) ----
43note sim_u layout (stride 20 per slot):
44note 0:n_items 1:queue_len 2:running_len 3:used_slots 4:used_mem
45note 5:slots_capacity 6:arrived 7:completed 8:rejected 9:pool_exhausted
46note 10:n_completed_stats 11:n_windows 12:rng_s 13:wl 14:step
47note 15:w_arrivals 16:w_completions 17:w_blocked 18:w_rejected
48global ap_sim_u ulong 40
49
50note sim_f layout (stride 5 per slot):
51note 0:sum_demand 1:sum_slots 2:sum_mem 3:het_sigma 4:sweep_rate
52global ap_sim_f f64 10
53
54note items arrays (stride 32768 per slot)
55global ap_it_arr uint 65536
56global ap_it_dem uint 65536
57global ap_it_rem uint 65536
58global ap_it_mem uint 65536
59global ap_it_slt uint 65536
60global ap_it_cls uint 65536
61
62note queue (stride 256 per slot) and running (stride 32 per slot)
63global ap_queue uint 512
64global ap_run uint 64
65
66note windows (stride 100 windows per slot):
67note win_f stride 6: 0:used_slots_frac 1:used_mem_frac 2:queue_density 3:mean_demand 4:mean_slots 5:mean_mem
68note win_u stride 8: 0:arrivals 1:completions 2:blocked_steps 3:rejected 4:in_system 5:queue_len 6:running 7:slots_capacity
69global ap_win_f f64 1200
70global ap_win_u ulong 1600
71
72note scratch index array for heapsort in ap_measure
73global ap_idx uint 32768
74
75note campaign rows (2 slots x 192 rows = 384 rows):
76note row_u stride 7: 0:wl 1:seed_index 2:seed 3:arrived 4:completed 5:rejected 6:windows
77note row_f stride 19:
78note 0..3: real (completions_nrmse, queue_rmse, blocked_rmse, composite)
79note 4..7: known_arrivals (completions_nrmse, queue_rmse, blocked_rmse, composite)
80note 8..11: shuffled (completions_nrmse, queue_rmse, blocked_rmse, composite)
81note 12..18: hetero (cv_demand, top_share, max_item_share, class_entropy, arrival_cv, broken_const, broken_noise)
82global ap_row_u ulong 2688
83global ap_row_f f64 7296
84
85note temporary single-window/prediction/error/hetero/pearson buffers
86global ap_pred_f f64 3
87global ap_err_f f64 4
88global ap_err_w ulong 1
89global ap_se_acc f64 4
90global ap_het_f f64 7
91global ap_pearson_f f64 2
92
93note streaming SHA-256 state
94global ap_sha_h ulong 8
95global ap_sha_w ulong 64
96global ap_sha_k ulong 64
97global ap_sha_blk uchar 64
98global ap_sha_st ulong 3
99
100note scratch formatting buffers
101global ap_line_buf uchar 512
102global ap_pbuf uchar 2048
103
104note ---- canonical text literals ----
105text ap_t_hex "0123456789abcdef"
106text ap_t_wl0 "a_homogeneous"
107text ap_t_wl1 "b_heterogeneous"
108text ap_t_wl2 "c_step"
109text ap_t_wl3 "d_square_wave"
110text ap_t_wl4 "e_bursty"
111text ap_t_wl5 "f_capacity_drop"
112text ap_t_wl6 "g_regime_change"
113text ap_t_wl7 "h_hetero_sweep"
114text ap_t_wlq "?"
115
116text ap_t_hdr0 "workload,seed_index,seed,arrived,completed,rejected,windows,"
117text ap_t_hdr1 "err_completions_nrmse,err_queue_rmse,err_blocked_rmse,err_composite,"
118text ap_t_hdr2 "known_completions_nrmse,known_queue_rmse,known_blocked_rmse,known_composite,"
119text ap_t_hdr3 "shuf_completions_nrmse,shuf_queue_rmse,shuf_blocked_rmse,shuf_composite,"
120text ap_t_hdr4 "cv_demand,top5_share,max_item_share,class_entropy,arrival_cv,broken_const,broken_noise"
121
122text ap_t_par0 "experiment=DUAL-APPLIC-V1\nwindow=20\nwindows=100\nwarmup_windows=5\nslots=32\nmemory=1024\nmax_queue=256\nclasses=4\nmax_items=32768\n"
123text ap_t_par1 "hom_demand=11..13\nhom_mem=16..24\nhom_slots=1\n"
124text ap_t_par2 "het_ln_median=1.791759469228055\nhet_sigma=1.2\nhet_demand_cap=400\nhet_slots_p=0.700000,0.900000\nhet_mem_base=8\nhet_mem_span=120.000000\nhet_class_p=0.550000,0.800000,0.950000\n"
125text ap_t_par3 "rate_hom=2.200000\nrate_het=1.200000\nstep_rate=1.000000,3.000000\nsquare_rate=0.800000,3.200000\nsquare_half_period=100\n"
126text ap_t_par4 "burst_p=0.080000\nburst_size=15..35\nburst_background=0.400000\ncap_drop_slots=20\nswitch_step=1000\n"
127text ap_t_par5 "sweep_sigma=0.000000..1.500000\nsweep_load=0.800000\nhet_mean_slots=1.700000\n"
128text ap_t_par6 "prior_demand=12.000000\nprior_slots=1.000000\nprior_mem=20.000000\n"
129text ap_t_par7 "seeds=24\nseed_base=4455414c2d415031\ntop_share_fraction=0.050000\n"
130text ap_t_par8 "grid_cv=0.400000,0.800000,1.200000\ngrid_top=0.100000,0.200000,0.300000\ngrid_arrival_cv=0.500000\nbroken_const=0.500000\n"
131text ap_t_par9 "blocked_ramp_start=0.750000\npredictor=flow_balance_v2\nerror=completions_nrmse,queue_rmse,blocked_rmse,composite_mean\nprimary_measure=completions_nrmse\n"
132
133note ---- IEEE-754 math helpers (pure f64) ----
134fn ap_isfinite x f64 to bool
135 b = bitcast ulong x
136 eb = and shr b 52 2047
137 ret lt eb 2047
138end
139
140fn ap_clamp01 x f64 to f64
141 if lt x 0.0f64
142 ret 0.0f64
143 end
144 if gt x 1.0f64
145 ret 1.0f64
146 end
147 ret x
148end
149
150fn ap_exp x f64 to f64
151 if ne x x
152 ret x
153 end
154 if gt x 709.782712893384f64
155 ret bitcast f64 ulong 9218868437227405312
156 end
157 if lt x -745.1332191019412f64
158 ret 0.0f64
159 end
160 kf = mul x 1.4426950408889634f64
161 if ge kf 0.0f64
162 kf = add kf 0.5f64
163 else
164 kf = sub kf 0.5f64
165 end
166 k = long kf
167 fk = f64 k
168 r = sub sub x mul fk 0.6931471803691238f64 mul fk 1.9082149292705877e-10f64
169 p = add mul r 1.6059043836821613e-10f64 2.08767569878681e-9f64
170 p = add mul p r 2.505210838544172e-8f64
171 p = add mul p r 2.755731922398589e-7f64
172 p = add mul p r 2.7557319223985893e-6f64
173 p = add mul p r 0.0000248015873015873f64
174 p = add mul p r 0.0001984126984126984f64
175 p = add mul p r 0.001388888888888889f64
176 p = add mul p r 0.008333333333333333f64
177 p = add mul p r 0.041666666666666664f64
178 p = add mul p r 0.16666666666666666f64
179 p = add mul p r 0.5f64
180 p = add mul p r 1.0f64
181 p = add mul p r 1.0f64
182 k1 = div k 2
183 k2 = sub k k1
184 s1 = bitcast f64 shl ulong add k1 1023 52
185 s2 = bitcast f64 shl ulong add k2 1023 52
186 ret mul mul p s1 s2
187end
188
189fn ap_log x f64 to f64
190 if ne x x
191 ret x
192 end
193 if lt x 0.0f64
194 ret bitcast f64 ulong 9221120237041090560
195 end
196 if eq x 0.0f64
197 ret bitcast f64 ulong 18442240474082181120
198 end
199 b = bitcast ulong x
200 if eq b 9218868437227405312
201 ret x
202 end
203 e = long 0
204 if lt b 4503599627370496
205 b = bitcast ulong mul x 18014398509481984.0f64
206 e = long -54
207 end
208 e = add e sub long shr b 52 1023
209 m = bitcast f64 or and b 4503599627370495 4607182418800017408
210 if gt m 1.4142135623730951f64
211 m = mul m 0.5f64
212 e = add e 1
213 end
214 f = sub m 1.0f64
215 s = div f add f 2.0f64
216 z = mul s s
217 q = add mul z 0.043478260869565216f64 0.047619047619047616f64
218 q = add mul q z 0.05263157894736842f64
219 q = add mul q z 0.058823529411764705f64
220 q = add mul q z 0.06666666666666667f64
221 q = add mul q z 0.07692307692307693f64
222 q = add mul q z 0.09090909090909091f64
223 q = add mul q z 0.1111111111111111f64
224 q = add mul q z 0.14285714285714285f64
225 q = add mul q z 0.2f64
226 q = add mul q z 0.3333333333333333f64
227 q = add mul q z 1.0f64
228 lm = mul add s s q
229 fe = f64 e
230 ret add mul fe 0.6931471803691238f64 add lm mul fe 1.9082149292705877e-10f64
231end
232
233fn ap_cos_poly r f64 to f64
234 z = mul r r
235 p = add mul z -1.1470745597729725e-11f64 2.08767569878681e-9f64
236 p = add mul p z -2.7557319223985893e-7f64
237 p = add mul p z 0.0000248015873015873f64
238 p = add mul p z -0.001388888888888889f64
239 p = add mul p z 0.041666666666666664f64
240 p = add mul p z -0.5f64
241 ret add mul p z 1.0f64
242end
243
244fn ap_sin_poly r f64 to f64
245 z = mul r r
246 p = add mul z 1.6059043836821613e-10f64 -2.505210838544172e-8f64
247 p = add mul p z 2.7557319223985893e-6f64
248 p = add mul p z -0.0001984126984126984f64
249 p = add mul p z 0.008333333333333333f64
250 p = add mul p z -0.16666666666666666f64
251 p = add mul p z 1.0f64
252 ret mul r p
253end
254
255fn ap_cos x f64 to f64
256 ax = fabs x
257 kf = add mul ax 0.6366197723675814f64 0.5f64
258 k = ulong kf
259 fk = f64 k
260 r = sub sub ax mul fk 1.5707963267948966f64 mul fk 6.123233995736766e-17f64
261 q = and k 3
262 if eq q 0
263 ret ap_cos_poly r
264 end
265 if eq q 1
266 ret neg ap_sin_poly r
267 end
268 if eq q 2
269 ret neg ap_cos_poly r
270 end
271 ret ap_sin_poly r
272end
273
274fn ap_ceil_u x f64 to ulong
275 if le x 0.0f64
276 ret ulong 0
277 end
278 i = ulong x
279 if gt x f64 i
280 i = add i 1
281 end
282 ret i
283end
284
285note ---- xorshift64* RNG ----
286fn ap_rng_next_val s ulong to ulong
287 v = xor s shr s 12
288 v = xor v shl v 25
289 v = xor v shr v 27
290 ret v
291end
292
293fn ap_rng_step_raw st edit ulong 1 to ulong
294 s = ap_rng_next_val get st 0
295 put st 0 s
296 ret mul s 0x2545f4914f6cdd1d
297end
298
299fn ap_rng_unit_raw st edit ulong 1 to f64
300 v = ap_rng_step_raw st
301 q = shr v 11
302 scale = bitcast f64 ulong 0x4340000000000000
303 ret div f64 q scale
304end
305
306fn ap_sim_rng_next slot ulong to ulong
307 off = add mul slot 20 12
308 s = ap_rng_next_val get ap_sim_u off
309 put ap_sim_u off s
310 ret mul s 0x2545f4914f6cdd1d
311end
312
313fn ap_sim_rng_unit slot ulong to f64
314 v = ap_sim_rng_next slot
315 q = shr v 11
316 scale = bitcast f64 ulong 0x4340000000000000
317 ret div f64 q scale
318end
319
320fn ap_sim_rng_range slot ulong lo ulong hi ulong to ulong
321 v = ap_sim_rng_next slot
322 span = add sub hi lo 1
323 ret add lo rem v span
324end
325
326fn ap_sim_rng_normal slot ulong to f64
327 u1 = ap_sim_rng_unit slot
328 u2 = ap_sim_rng_unit slot
329 if lt u1 1.0e-300f64
330 u1 = 1.0e-300f64
331 end
332 mag = fsqrt mul -2.0f64 ap_log u1
333 ang = mul 6.283185307179586f64 u2
334 ret mul mag ap_cos ang
335end
336
337fn ap_sim_rng_poisson slot ulong lam f64 to ulong
338 if le lam 0.0f64
339 ret ulong 0
340 end
341 lim = ap_exp neg lam
342 p = 1.0f64
343 k = ulong 0
344 while true bound 1000
345 k = add k 1
346 p = mul p ap_sim_rng_unit slot
347 if le p lim
348 ret sub k 1
349 end
350 end
351 ret k
352end
353
354note ---- seed and workload regime helpers ----
355fn ap_seed wl ulong si ulong to ulong
356 a = mul wl 0x9e3779b97f4a7c15
357 b = mul add si 1 0xd1b54a32d192ed03
358 s = xor xor AP_SEED_BASE a b
359 if eq s 0
360 ret ulong 1
361 end
362 ret s
363end
364
365fn ap_sweep_sigma si ulong to f64
366 ret mul 1.5f64 div f64 si 23.0f64
367end
368
369fn ap_family_is_het wl ulong step ulong to bool
370 if or eq wl 1 eq wl 7
371 ret true
372 end
373 if and eq wl 6 ge step AP_SWITCH_STEP
374 ret true
375 end
376 ret false
377end
378
379fn ap_capacity_this_step wl ulong step ulong to ulong
380 if and eq wl 5 ge step AP_SWITCH_STEP
381 ret AP_CAP_DROP_SLOTS
382 end
383 ret AP_SLOTS
384end
385
386fn ap_arrivals_this_step slot ulong wl ulong step ulong srate f64 to ulong
387 if or eq wl 0 eq wl 5
388 ret ap_sim_rng_poisson slot 2.2f64
389 end
390 if eq wl 1
391 ret ap_sim_rng_poisson slot 1.2f64
392 end
393 if eq wl 2
394 if lt step AP_SWITCH_STEP
395 ret ap_sim_rng_poisson slot 1.0f64
396 end
397 ret ap_sim_rng_poisson slot 3.0f64
398 end
399 if eq wl 3
400 hp = div step AP_SQUARE_HALF_PERIOD
401 if ne and hp 1 0
402 ret ap_sim_rng_poisson slot 3.2f64
403 end
404 ret ap_sim_rng_poisson slot 0.8f64
405 end
406 if eq wl 4
407 if lt ap_sim_rng_unit slot 0.08f64
408 ret ap_sim_rng_range slot AP_BURST_MIN AP_BURST_MAX
409 end
410 ret ap_sim_rng_poisson slot 0.4f64
411 end
412 if eq wl 6
413 if lt step AP_SWITCH_STEP
414 ret ap_sim_rng_poisson slot 2.2f64
415 end
416 ret ap_sim_rng_poisson slot 1.2f64
417 end
418 if eq wl 7
419 ret ap_sim_rng_poisson slot srate
420 end
421 ret ulong 0
422end
423
424note ---- simulator init, ledger, step, run ----
425fn ap_sim_init slot ulong wl ulong seed ulong to void
426 ub = mul slot 20
427 for i ulong 20
428 put ap_sim_u add ub i 0
429 end
430 fb = mul slot 5
431 for i ulong 5
432 put ap_sim_f add fb i 0.0f64
433 end
434 wb_u = mul slot 800
435 for i ulong 800
436 put ap_win_u add wb_u i 0
437 end
438 wb_f = mul slot 600
439 for i ulong 600
440 put ap_win_f add wb_f i 0.0f64
441 end
442 put ap_sim_u add ub 13 wl
443 rs = seed
444 if eq rs 0
445 rs = ulong 1
446 end
447 put ap_sim_u add ub 12 rs
448 put ap_sim_u add ub 5 AP_SLOTS
449 put ap_sim_f add fb 3 1.2f64
450 put ap_sim_f add fb 4 0.0f64
451end
452
453fn ap_sim_init_indexed slot ulong wl ulong seed ulong si ulong to void
454 ap_sim_init slot wl seed
455 if eq wl 7
456 sigma = ap_sweep_sigma si
457 md = ap_exp add 1.7917594692280550f64 mul 0.5f64 mul sigma sigma
458 fb = mul slot 5
459 put ap_sim_f add fb 3 sigma
460 put ap_sim_f add fb 4 div mul 0.80f64 32.0f64 mul md 1.7f64
461 end
462end
463
464fn ap_sim_ledger_ok slot ulong to bool
465 ub = mul slot 20
466 arr = get ap_sim_u add ub 6
467 comp = get ap_sim_u add ub 7
468 ql = get ap_sim_u add ub 1
469 rl = get ap_sim_u add ub 2
470 rej = get ap_sim_u add ub 8
471 sum = add add add comp ql rl rej
472 ret eq arr sum
473end
474
475fn ap_gen_het_attrs slot ulong pos ulong to void
476 u = ap_sim_rng_unit slot
477 v = ulong 4
478 if lt u 0.70f64
479 v = ulong 1
480 else
481 if lt u 0.90f64
482 v = ulong 2
483 end
484 end
485 put ap_it_slt pos uint v
486 u = ap_sim_rng_unit slot
487 mspan = mul mul mul 120.0f64 u u u
488 v = add AP_HET_MEM_BASE ulong add mspan 0.5f64
489 put ap_it_mem pos uint v
490 u = ap_sim_rng_unit slot
491 v = ulong 3
492 if lt u 0.55f64
493 v = ulong 0
494 else
495 if lt u 0.80f64
496 v = ulong 1
497 else
498 if lt u 0.95f64
499 v = ulong 2
500 end
501 end
502 end
503 put ap_it_cls pos uint v
504end
505
506fn ap_gen_item slot ulong pos ulong is_het bool sigma f64 to void
507 if not is_het
508 dem = ap_sim_rng_range slot AP_HOM_DEMAND_MIN AP_HOM_DEMAND_MAX
509 mem = ap_sim_rng_range slot AP_HOM_MEM_MIN AP_HOM_MEM_MAX
510 cls = ap_sim_rng_range slot 0 3
511 put ap_it_dem pos uint dem
512 put ap_it_rem pos uint dem
513 put ap_it_mem pos uint mem
514 put ap_it_slt pos uint 1
515 put ap_it_cls pos uint cls
516 ret
517 end
518 z = ap_sim_rng_normal slot
519 d = ap_exp add 1.7917594692280550f64 mul sigma z
520 if lt d 1.0f64
521 d = 1.0f64
522 end
523 if gt d 400.0f64
524 d = 400.0f64
525 end
526 dem = ulong add d 0.5f64
527 put ap_it_dem pos uint dem
528 put ap_it_rem pos uint dem
529 ap_gen_het_attrs slot pos
530end
531
532fn ap_arrive_one slot ulong step ulong is_het bool sigma f64 to ulong
533 ub = mul slot 20
534 ni = get ap_sim_u ub
535 if ge ni AP_MAX_ITEMS
536 put ap_sim_u add ub 9 add get ap_sim_u add ub 9 1
537 ret ulong 2
538 end
539 pos = add mul slot AP_MAX_ITEMS ni
540 ap_gen_item slot pos is_het sigma
541 put ap_it_arr pos uint step
542 put ap_sim_u add ub 6 add get ap_sim_u add ub 6 1
543 put ap_sim_u add ub 15 add get ap_sim_u add ub 15 1
544 ql = get ap_sim_u add ub 1
545 if ge ql AP_MAX_QUEUE
546 put ap_sim_u add ub 8 add get ap_sim_u add ub 8 1
547 put ap_sim_u add ub 18 add get ap_sim_u add ub 18 1
548 else
549 put ap_queue add mul slot AP_MAX_QUEUE ql uint ni
550 put ap_sim_u add ub 1 add ql 1
551 put ap_sim_u ub add ni 1
552 end
553 ret ulong 0
554end
555
556fn ap_step_arrivals slot ulong step ulong to ulong
557 ub = mul slot 20
558 wl = get ap_sim_u add ub 13
559 n = ap_arrivals_this_step slot wl step get ap_sim_f add mul slot 5 4
560 is_het = ap_family_is_het wl step
561 sigma = get ap_sim_f add mul slot 5 3
562 for i ulong n bound 1024
563 rc = ap_arrive_one slot step is_het sigma
564 if ne rc 0
565 ret rc
566 end
567 end
568 ret ulong 0
569end
570
571fn ap_queue_remove slot ulong idx ulong to void
572 ub = mul slot 20
573 ql = get ap_sim_u add ub 1
574 qbase = mul slot AP_MAX_QUEUE
575 j = idx
576 lim = sub ql 1
577 while lt j lim bound 256
578 put ap_queue add qbase j get ap_queue add qbase add j 1
579 j = add j 1
580 end
581 put ap_sim_u add ub 1 lim
582end
583
584fn ap_try_admit slot ulong i ulong c ulong to bool
585 ub = mul slot 20
586 qid = ulong get ap_queue add mul slot AP_MAX_QUEUE i
587 pos = add mul slot AP_MAX_ITEMS qid
588 if ne ulong get ap_it_cls pos c
589 ret false
590 end
591 us = add get ap_sim_u add ub 3 ulong get ap_it_slt pos
592 um = add get ap_sim_u add ub 4 ulong get ap_it_mem pos
593 rl = get ap_sim_u add ub 2
594 if and le us get ap_sim_u add ub 5 and le um AP_MEMORY lt rl AP_SLOTS
595 put ap_run add mul slot AP_SLOTS rl uint qid
596 put ap_sim_u add ub 2 add rl 1
597 put ap_sim_u add ub 3 us
598 put ap_sim_u add ub 4 um
599 ret true
600 end
601 ret false
602end
603
604fn ap_step_admit_class slot ulong c ulong to void
605 ub = mul slot 20
606 i = ulong 0
607 while lt i get ap_sim_u add ub 1 bound 512
608 if ap_try_admit slot i c
609 ap_queue_remove slot i
610 else
611 i = add i 1
612 end
613 end
614end
615
616fn ap_complete_item slot ulong i ulong pos ulong to void
617 ub = mul slot 20
618 fb = mul slot 5
619 slt = ulong get ap_it_slt pos
620 mem = ulong get ap_it_mem pos
621 dem = ulong get ap_it_dem pos
622 put ap_sim_u add ub 3 sub get ap_sim_u add ub 3 slt
623 put ap_sim_u add ub 4 sub get ap_sim_u add ub 4 mem
624 put ap_sim_u add ub 7 add get ap_sim_u add ub 7 1
625 put ap_sim_u add ub 16 add get ap_sim_u add ub 16 1
626 put ap_sim_f fb add get ap_sim_f fb f64 dem
627 put ap_sim_f add fb 1 add get ap_sim_f add fb 1 f64 slt
628 put ap_sim_f add fb 2 add get ap_sim_f add fb 2 f64 mem
629 put ap_sim_u add ub 10 add get ap_sim_u add ub 10 1
630 nrl = sub get ap_sim_u add ub 2 1
631 put ap_sim_u add ub 2 nrl
632 rbase = mul slot AP_SLOTS
633 put ap_run add rbase i get ap_run add rbase nrl
634end
635
636fn ap_step_service slot ulong to void
637 ub = mul slot 20
638 ibase = mul slot AP_MAX_ITEMS
639 rbase = mul slot AP_SLOTS
640 i = ulong 0
641 while lt i get ap_sim_u add ub 2 bound 64
642 rid = ulong get ap_run add rbase i
643 pos = add ibase rid
644 remv = ulong get ap_it_rem pos
645 if gt remv 0
646 remv = sub remv 1
647 put ap_it_rem pos uint remv
648 end
649 if eq remv 0
650 ap_complete_item slot i pos
651 else
652 i = add i 1
653 end
654 end
655end
656
657fn ap_close_window_means slot ulong wfb ulong to void
658 ub = mul slot 20
659 fb = mul slot 5
660 ncs = get ap_sim_u add ub 10
661 if gt ncs 0
662 nf = f64 ncs
663 put ap_win_f add wfb 3 div get ap_sim_f fb nf
664 put ap_win_f add wfb 4 div get ap_sim_f add fb 1 nf
665 put ap_win_f add wfb 5 div get ap_sim_f add fb 2 nf
666 else
667 put ap_win_f add wfb 3 12.0f64
668 put ap_win_f add wfb 4 1.0f64
669 put ap_win_f add wfb 5 20.0f64
670 end
671end
672
673fn ap_close_window slot ulong to void
674 ub = mul slot 20
675 nw = get ap_sim_u add ub 11
676 put ap_sim_u add ub 11 add nw 1
677 wi = add mul slot AP_WINDOWS nw
678 wub = mul wi 8
679 wfb = mul wi 6
680 scap = get ap_sim_u add ub 5
681 put ap_win_u add wub 7 scap
682 put ap_win_f wfb div f64 get ap_sim_u add ub 3 f64 scap
683 put ap_win_f add wfb 1 div f64 get ap_sim_u add ub 4 1024.0f64
684 ql = get ap_sim_u add ub 1
685 rl = get ap_sim_u add ub 2
686 put ap_win_f add wfb 2 div f64 ql 256.0f64
687 put ap_win_u wub get ap_sim_u add ub 15
688 put ap_win_u add wub 1 get ap_sim_u add ub 16
689 put ap_win_u add wub 2 get ap_sim_u add ub 17
690 put ap_win_u add wub 3 get ap_sim_u add ub 18
691 put ap_win_u add wub 5 ql
692 put ap_win_u add wub 6 rl
693 put ap_win_u add wub 4 add ql rl
694 ap_close_window_means slot wfb
695 put ap_sim_u add ub 15 0
696 put ap_sim_u add ub 16 0
697 put ap_sim_u add ub 17 0
698 put ap_sim_u add ub 18 0
699end
700
701fn ap_sim_step slot ulong to ulong
702 ub = mul slot 20
703 step = get ap_sim_u add ub 14
704 if ge step AP_STEPS
705 ret ulong 0
706 end
707 wl = get ap_sim_u add ub 13
708 put ap_sim_u add ub 5 ap_capacity_this_step wl step
709 rc = ap_step_arrivals slot step
710 if ne rc 0
711 ret rc
712 end
713 for c ulong AP_CLASSES
714 ap_step_admit_class slot c
715 end
716 if gt get ap_sim_u add ub 1 0
717 put ap_sim_u add ub 17 add get ap_sim_u add ub 17 1
718 end
719 ap_step_service slot
720 step = add step 1
721 put ap_sim_u add ub 14 step
722 if and eq rem step AP_WINDOW 0 lt get ap_sim_u add ub 11 AP_WINDOWS
723 ap_close_window slot
724 end
725 if ap_sim_ledger_ok slot
726 ret ulong 0
727 end
728 ret ulong 1
729end
730
731fn ap_sim_run slot ulong to ulong
732 ub = mul slot 20
733 rc = ulong 0
734 while lt get ap_sim_u add ub 14 AP_STEPS bound 2048
735 r = ap_sim_step slot
736 if and ne r 0 eq rc 0
737 rc = r
738 end
739 if eq r 2
740 break
741 end
742 end
743 ret rc
744end
745
746note ---- aggregate predictor (flow balance v2) ----
747fn ap_pred_rcap scap ulong mean_s f64 mean_m f64 to f64
748 ms = mean_s
749 if le ms 0.0f64
750 ms = 1.0f64
751 end
752 mm = mean_m
753 if le mm 0.0f64
754 mm = 20.0f64
755 end
756 by_s = div f64 scap ms
757 by_m = div 1024.0f64 mm
758 if lt by_m by_s
759 ret by_m
760 end
761 ret by_s
762end
763
764fn ap_predict_core in_sys ulong rcap f64 mean_d f64 comp f64 arr f64 to void
765 in_next = sub add f64 in_sys arr comp
766 if lt in_next 0.0f64
767 in_next = 0.0f64
768 end
769 qdens = ap_clamp01 div sub in_next rcap 256.0f64
770 occ = 1.0f64
771 if gt rcap 0.0f64
772 occ = div in_next rcap
773 end
774 blk = ap_clamp01 div sub occ 0.75f64 0.25f64
775 put ap_pred_f 0 comp
776 put ap_pred_f 1 qdens
777 put ap_pred_f 2 blk
778end
779
780fn ap_pred_flow run_u ulong ql_u ulong mean_d f64 arr f64 to f64
781 wlen = 20.0f64
782 frac = div wlen mean_d
783 if gt frac 1.0f64
784 frac = 1.0f64
785 end
786 late = 0.0f64
787 if gt sub wlen mean_d 0.0f64
788 late = div sub wlen mean_d wlen
789 end
790 ret add add mul f64 run_u frac mul f64 ql_u frac mul arr late
791end
792
793fn ap_predict_win slot ulong w ulong arr f64 to void
794 wi = add mul slot AP_WINDOWS w
795 wub = mul wi 8
796 wfb = mul wi 6
797 rcap = ap_pred_rcap get ap_win_u add wub 7 get ap_win_f add wfb 4 get ap_win_f add wfb 5
798 mean_d = get ap_win_f add wfb 3
799 if le mean_d 0.0f64
800 mean_d = 12.0f64
801 end
802 cap_w = div mul rcap 20.0f64 mean_d
803 comp = ap_pred_flow get ap_win_u add wub 6 get ap_win_u add wub 5 mean_d arr
804 if lt cap_w comp
805 comp = cap_w
806 end
807 ap_predict_core get ap_win_u add wub 4 rcap mean_d comp arr
808end
809
810fn ap_predict_win_default slot ulong w ulong to void
811 wi = add mul slot AP_WINDOWS w
812 wub = mul wi 8
813 arr = f64 get ap_win_u wub
814 ap_predict_win slot w arr
815end
816
817fn ap_error_finalize se_c f64 se_q f64 se_b f64 sum_c f64 scored ulong to void
818 put ap_err_w 0 scored
819 if eq scored 0
820 for i ulong 4
821 put ap_err_f i 0.0f64
822 end
823 ret
824 end
825 nf = f64 scored
826 mean_c = div sum_c nf
827 rc = fsqrt div se_c nf
828 if gt mean_c 0.0f64
829 rc = div rc mean_c
830 end
831 rq = fsqrt div se_q nf
832 rb = fsqrt div se_b nf
833 comp = div add add rc rq rb 3.0f64
834 put ap_err_f 0 rc
835 put ap_err_f 1 rq
836 put ap_err_f 2 rb
837 put ap_err_f 3 comp
838end
839
840fn ap_score_accum aw ulong to void
841 awu = mul aw 8
842 awf = mul aw 6
843 ac = f64 get ap_win_u add awu 1
844 ab = div f64 get ap_win_u add awu 2 20.0f64
845 d = sub get ap_pred_f 0 ac
846 put ap_se_acc 0 add get ap_se_acc 0 mul d d
847 d = sub get ap_pred_f 1 get ap_win_f add awf 2
848 put ap_se_acc 1 add get ap_se_acc 1 mul d d
849 d = sub get ap_pred_f 2 ab
850 put ap_se_acc 2 add get ap_se_acc 2 mul d d
851 put ap_se_acc 3 add get ap_se_acc 3 ac
852end
853
854fn ap_score_step slot ulong t ulong mode ulong offset ulong scored ulong to void
855 aw = add mul slot AP_WINDOWS add t 1
856 if eq mode 1
857 ap_predict_win slot t f64 get ap_win_u mul aw 8
858 else
859 src = t
860 if eq mode 2
861 src = add AP_WARMUP_WINDOWS rem add sub t AP_WARMUP_WINDOWS offset scored
862 end
863 ap_predict_win_default slot src
864 end
865 ap_score_accum aw
866end
867
868fn ap_score slot ulong mode ulong offset ulong to void
869 nw = get ap_sim_u add mul slot 20 11
870 last = ulong 0
871 if gt nw 0
872 last = sub nw 1
873 end
874 scored = ulong 0
875 if gt last AP_WARMUP_WINDOWS
876 scored = sub last AP_WARMUP_WINDOWS
877 end
878 if eq scored 0
879 ap_error_finalize 0.0f64 0.0f64 0.0f64 0.0f64 0
880 ret
881 end
882 for i ulong 4
883 put ap_se_acc i 0.0f64
884 end
885 t = AP_WARMUP_WINDOWS
886 while lt t last bound 128
887 ap_score_step slot t mode offset scored
888 t = add t 1
889 end
890 ap_error_finalize get ap_se_acc 0 get ap_se_acc 1 get ap_se_acc 2 get ap_se_acc 3 scored
891end
892
893fn ap_error_real slot ulong to void
894 ap_score slot 0 0
895end
896
897fn ap_error_known_arrivals slot ulong to void
898 ap_score slot 1 0
899end
900
901fn ap_error_shuffled slot ulong offset ulong to void
902 ub = mul slot 20
903 nw = get ap_sim_u add ub 11
904 first = AP_WARMUP_WINDOWS
905 last = ulong 0
906 if gt nw 0
907 last = sub nw 1
908 end
909 scored = ulong 0
910 if gt last first
911 scored = sub last first
912 end
913 if lt scored 2
914 ap_score slot 0 0
915 ret
916 end
917 off = add 1 rem offset sub scored 1
918 ap_score slot 2 off
919end
920
921note ---- deterministic load heapsort and heterogeneity metrics ----
922fn ap_item_load ibase ulong i ulong to ulong
923 pos = add ibase i
924 ret mul ulong get ap_it_dem pos ulong get ap_it_slt pos
925end
926
927fn ap_load_before ibase ulong a ulong b ulong to bool
928 la = ap_item_load ibase a
929 lb = ap_item_load ibase b
930 if ne la lb
931 ret gt la lb
932 end
933 ret lt a b
934end
935
936fn ap_sift ibase ulong root ulong n ulong to void
937 r = root
938 while true bound 64
939 child = add mul r 2 1
940 if ge child n
941 ret
942 end
943 pick = r
944 ci = ulong get ap_idx child
945 pi = ulong get ap_idx pick
946 if not ap_load_before ibase ci pi
947 pick = child
948 pi = ci
949 end
950 c2 = add child 1
951 if lt c2 n
952 c2i = ulong get ap_idx c2
953 if not ap_load_before ibase c2i pi
954 pick = c2
955 end
956 end
957 if eq pick r
958 ret
959 end
960 tmp = get ap_idx r
961 put ap_idx r get ap_idx pick
962 put ap_idx pick tmp
963 r = pick
964 end
965end
966
967fn ap_sort_desc ibase ulong n ulong to void
968 if lt n 2
969 ret
970 end
971 i = div n 2
972 while gt i 0 bound 65536
973 i = sub i 1
974 ap_sift ibase i n
975 end
976 i = sub n 1
977 while gt i 0 bound 65536
978 t = get ap_idx 0
979 put ap_idx 0 get ap_idx i
980 put ap_idx i t
981 ap_sift ibase 0 i
982 i = sub i 1
983 end
984end
985
986fn ap_measure_arr_cv slot ulong to f64
987 ub = mul slot 20
988 nw = get ap_sim_u add ub 11
989 if eq nw 0
990 ret 0.0f64
991 end
992 a = 0.0f64
993 a2 = 0.0f64
994 for i ulong nw bound 128
995 wi = add mul slot AP_WINDOWS i
996 x = f64 get ap_win_u mul wi 8
997 a = add a x
998 a2 = add a2 mul x x
999 end
1000 m = div a f64 nw
1001 v = sub div a2 f64 nw mul m m
1002 if lt v 0.0f64
1003 v = 0.0f64
1004 end
1005 if gt m 0.0f64
1006 ret div fsqrt v m
1007 end
1008 ret 0.0f64
1009end
1010
1011fn ap_measure_scan ibase ulong n ulong cls_cnt edit ulong 4 to f64
1012 sum = 0.0f64
1013 sum2 = 0.0f64
1014 tload = 0.0f64
1015 for i ulong n bound 65536
1016 pos = add ibase i
1017 d = f64 get ap_it_dem pos
1018 sum = add sum d
1019 sum2 = add sum2 mul d d
1020 tload = add tload mul d f64 get ap_it_slt pos
1021 c = ulong get ap_it_cls pos
1022 put cls_cnt c add get cls_cnt c 1
1023 put ap_idx i uint i
1024 end
1025 nf = f64 n
1026 mean = div sum nf
1027 var = sub div sum2 nf mul mean mean
1028 if lt var 0.0f64
1029 var = 0.0f64
1030 end
1031 if gt mean 0.0f64
1032 put ap_het_f 0 div fsqrt var mean
1033 end
1034 ret tload
1035end
1036
1037fn ap_measure_top ibase ulong n ulong tload f64 to void
1038 ap_sort_desc ibase n
1039 k = ap_ceil_u mul 0.05f64 f64 n
1040 if lt k 1
1041 k = ulong 1
1042 end
1043 if gt k n
1044 k = n
1045 end
1046 top = 0.0f64
1047 for i ulong k bound 65536
1048 idx_i = ulong get ap_idx i
1049 top = add top f64 ap_item_load ibase idx_i
1050 end
1051 if gt tload 0.0f64
1052 put ap_het_f 1 div top tload
1053 top0 = ulong get ap_idx 0
1054 put ap_het_f 2 div f64 ap_item_load ibase top0 tload
1055 end
1056end
1057
1058fn ap_measure_ent cls_cnt view ulong 4 n ulong to void
1059 ent = 0.0f64
1060 nf = f64 n
1061 ln2 = 0.6931471805599453f64
1062 for i ulong AP_CLASSES
1063 p = div f64 get cls_cnt i nf
1064 if gt p 0.0f64
1065 ent = sub ent mul p div ap_log p ln2
1066 end
1067 end
1068 put ap_het_f 3 div ent 2.0f64
1069end
1070
1071fn ap_measure slot ulong noise_st edit ulong 1 to void
1072 for i ulong 7
1073 put ap_het_f i 0.0f64
1074 end
1075 put ap_het_f 5 0.5f64
1076 put ap_het_f 6 ap_rng_unit_raw noise_st
1077 n = get ap_sim_u mul slot 20
1078 if eq n 0
1079 ret
1080 end
1081 ibase = mul slot AP_MAX_ITEMS
1082 cls_cnt = new ulong 4 0
1083 tload = ap_measure_scan ibase n cls_cnt
1084 ap_measure_top ibase n tload
1085 ap_measure_ent cls_cnt n
1086 put ap_het_f 4 ap_measure_arr_cv slot
1087end
1088
1089note ---- campaign runner ----
1090fn ap_store_row_scores ridx ulong sim_slot ulong sd ulong to void
1091 rub = mul ridx 7
1092 rfb = mul ridx 19
1093 ap_error_real sim_slot
1094 put ap_row_u add rub 6 get ap_err_w 0
1095 for i ulong 4
1096 put ap_row_f add rfb i get ap_err_f i
1097 end
1098 ap_error_known_arrivals sim_slot
1099 for i ulong 4
1100 put ap_row_f add rfb add 4 i get ap_err_f i
1101 end
1102 ap_error_shuffled sim_slot and shr sd 7 0xffffffff
1103 for i ulong 4
1104 put ap_row_f add rfb add 8 i get ap_err_f i
1105 end
1106 nst = new ulong 1 0
1107 ns = xor sd 0xa5a5a5a5a5a5a5a5
1108 if eq ns 0
1109 ns = ulong 1
1110 end
1111 put nst 0 ns
1112 ap_measure sim_slot nst
1113 for i ulong 7
1114 put ap_row_f add rfb add 12 i get ap_het_f i
1115 end
1116end
1117
1118fn ap_store_row rslot ulong idx ulong sim_slot ulong wl ulong si ulong sd ulong to void
1119 ridx = add mul rslot AP_NROWS idx
1120 rub = mul ridx 7
1121 ub = mul sim_slot 20
1122 put ap_row_u rub wl
1123 put ap_row_u add rub 1 si
1124 put ap_row_u add rub 2 sd
1125 put ap_row_u add rub 3 get ap_sim_u add ub 6
1126 put ap_row_u add rub 4 get ap_sim_u add ub 7
1127 put ap_row_u add rub 5 get ap_sim_u add ub 8
1128 ap_store_row_scores ridx sim_slot sd
1129end
1130
1131fn ap_campaign sim_slot ulong rslot ulong to ulong
1132 n = ulong 0
1133 for wl ulong AP_WL_COUNT
1134 for si ulong AP_SEEDS
1135 sd = ap_seed wl si
1136 ap_sim_init_indexed sim_slot wl sd si
1137 if ne ap_sim_run sim_slot 0
1138 ret ulong 2
1139 end
1140 ap_store_row rslot n sim_slot wl si sd
1141 n = add n 1
1142 end
1143 end
1144 ret ulong 0
1145end
1146
1147note ---- Pearson correlation ----
1148fn ap_mean_vec a view f64 * n ulong to f64
1149 s = 0.0f64
1150 for i ulong n bound 1024
1151 s = add s get a i
1152 end
1153 ret div s f64 n
1154end
1155
1156fn ap_ss_vec a view f64 * n ulong m f64 to f64
1157 s = 0.0f64
1158 for i ulong n bound 1024
1159 d = sub get a i m
1160 s = add s mul d d
1161 end
1162 ret s
1163end
1164
1165fn ap_sxy_vec x view f64 * y view f64 * mx f64 my f64 to f64
1166 n = len x
1167 s = 0.0f64
1168 for i ulong n bound 1024
1169 dx = sub get x i mx
1170 dy = sub get y i my
1171 s = add s mul dx dy
1172 end
1173 ret s
1174end
1175
1176fn ap_pearson x view f64 * y view f64 * n ulong to f64
1177 put ap_pearson_f 0 0.0f64
1178 put ap_pearson_f 1 0.0f64
1179 if lt n 2
1180 ret 0.0f64
1181 end
1182 mx = ap_mean_vec x n
1183 my = ap_mean_vec y n
1184 sxx = ap_ss_vec x n mx
1185 syy = ap_ss_vec y n my
1186 sxy = ap_sxy_vec x y mx my
1187 put ap_pearson_f 1 div sxy f64 sub n 1
1188 if or le sxx 0.0f64 le syy 0.0f64
1189 ret 0.0f64
1190 end
1191 r = div sxy fsqrt mul sxx syy
1192 put ap_pearson_f 0 r
1193 ret r
1194end
1195
1196note ---- streaming SHA-256 (FIPS 180-4) ----
1197fn ap_sha_mask x ulong to ulong
1198 ret and x 4294967295
1199end
1200
1201fn ap_sha_ror x ulong n ulong to ulong
1202 ret ap_sha_mask or shr x n shl x sub 32 n
1203end
1204
1205fn ap_sha_ch e ulong f ulong g ulong to ulong
1206 ret xor and e f and xor e 4294967295 g
1207end
1208
1209fn ap_sha_maj a ulong b ulong c ulong to ulong
1210 ret xor and a b xor and a c and b c
1211end
1212
1213fn ap_sha_bs0 x ulong to ulong
1214 ret xor ap_sha_ror x 2 xor ap_sha_ror x 13 ap_sha_ror x 22
1215end
1216
1217fn ap_sha_bs1 x ulong to ulong
1218 ret xor ap_sha_ror x 6 xor ap_sha_ror x 11 ap_sha_ror x 25
1219end
1220
1221fn ap_sha_ss0 x ulong to ulong
1222 ret xor ap_sha_ror x 7 xor ap_sha_ror x 18 shr x 3
1223end
1224
1225fn ap_sha_ss1 x ulong to ulong
1226 ret xor ap_sha_ror x 17 xor ap_sha_ror x 19 shr x 10
1227end
1228
1229fn ap_sha_kinit to void
1230 if ne get ap_sha_st 0 0
1231 ret
1232 end
1233 put ap_sha_k 0 ulong 1116352408
1234 put ap_sha_k 1 ulong 1899447441
1235 put ap_sha_k 2 ulong 3049323471
1236 put ap_sha_k 3 ulong 3921009573
1237 put ap_sha_k 4 ulong 961987163
1238 put ap_sha_k 5 ulong 1508970993
1239 put ap_sha_k 6 ulong 2453635748
1240 put ap_sha_k 7 ulong 2870763221
1241 put ap_sha_k 8 ulong 3624381080
1242 put ap_sha_k 9 ulong 310598401
1243 put ap_sha_k 10 ulong 607225278
1244 put ap_sha_k 11 ulong 1426881987
1245 put ap_sha_k 12 ulong 1925078388
1246 put ap_sha_k 13 ulong 2162078206
1247 put ap_sha_k 14 ulong 2614888103
1248 put ap_sha_k 15 ulong 3248222580
1249 put ap_sha_k 16 ulong 3835390401
1250 put ap_sha_k 17 ulong 4022224774
1251 put ap_sha_k 18 ulong 264347078
1252 put ap_sha_k 19 ulong 604807628
1253 put ap_sha_k 20 ulong 770255983
1254 put ap_sha_k 21 ulong 1249150122
1255 put ap_sha_k 22 ulong 1555081692
1256 put ap_sha_k 23 ulong 1996064986
1257 put ap_sha_k 24 ulong 2554220882
1258 put ap_sha_k 25 ulong 2821834349
1259 put ap_sha_k 26 ulong 2952996808
1260 put ap_sha_k 27 ulong 3210313671
1261 put ap_sha_k 28 ulong 3336571891
1262 put ap_sha_k 29 ulong 3584528711
1263 put ap_sha_k 30 ulong 113926993
1264 put ap_sha_k 31 ulong 338241895
1265 put ap_sha_k 32 ulong 666307205
1266 put ap_sha_k 33 ulong 773529912
1267 put ap_sha_k 34 ulong 1294757372
1268 put ap_sha_k 35 ulong 1396182291
1269 put ap_sha_k 36 ulong 1695183700
1270 put ap_sha_k 37 ulong 1986661051
1271 put ap_sha_k 38 ulong 2177026350
1272 put ap_sha_k 39 ulong 2456956037
1273 put ap_sha_k 40 ulong 2730485921
1274 put ap_sha_k 41 ulong 2820302411
1275 put ap_sha_k 42 ulong 3259730800
1276 put ap_sha_k 43 ulong 3345764771
1277 put ap_sha_k 44 ulong 3516065817
1278 put ap_sha_k 45 ulong 3600352804
1279 put ap_sha_k 46 ulong 4094571909
1280 put ap_sha_k 47 ulong 275423344
1281 put ap_sha_k 48 ulong 430227734
1282 put ap_sha_k 49 ulong 506948616
1283 put ap_sha_k 50 ulong 659060556
1284 put ap_sha_k 51 ulong 883997877
1285 put ap_sha_k 52 ulong 958139571
1286 put ap_sha_k 53 ulong 1322822218
1287 put ap_sha_k 54 ulong 1537002063
1288 put ap_sha_k 55 ulong 1747873779
1289 put ap_sha_k 56 ulong 1955562222
1290 put ap_sha_k 57 ulong 2024104815
1291 put ap_sha_k 58 ulong 2227730452
1292 put ap_sha_k 59 ulong 2361852424
1293 put ap_sha_k 60 ulong 2428436474
1294 put ap_sha_k 61 ulong 2756734187
1295 put ap_sha_k 62 ulong 3204031479
1296 put ap_sha_k 63 ulong 3329325298
1297 put ap_sha_st 0 1
1298end
1299
1300fn ap_sha_compress to void
1301 ap_sha_kinit
1302 for i ulong 16
1303 v = ulong 0
1304 for j ulong 4
1305 v = or shl v 8 ulong get ap_sha_blk add mul i 4 j
1306 end
1307 put ap_sha_w i v
1308 end
1309 for i ulong 48
1310 j = add i 16
1311 v = add ap_sha_ss1 get ap_sha_w sub j 2 add ap_sha_ss0 get ap_sha_w sub j 15 add get ap_sha_w sub j 7 get ap_sha_w sub j 16
1312 put ap_sha_w j ap_sha_mask v
1313 end
1314 a = get ap_sha_h 0
1315 b = get ap_sha_h 1
1316 c = get ap_sha_h 2
1317 d = get ap_sha_h 3
1318 e = get ap_sha_h 4
1319 f = get ap_sha_h 5
1320 g = get ap_sha_h 6
1321 hh = get ap_sha_h 7
1322 for i ulong 64
1323 x = ap_sha_ch e f g
1324 x = add x get ap_sha_k i
1325 x = add x get ap_sha_w i
1326 x = add x ap_sha_bs1 e
1327 x = add x hh
1328 t1 = ap_sha_mask x
1329 x = ap_sha_maj a b c
1330 x = add ap_sha_bs0 a x
1331 hh = g
1332 g = f
1333 f = e
1334 e = ap_sha_mask add d t1
1335 d = c
1336 c = b
1337 b = a
1338 a = ap_sha_mask add t1 x
1339 end
1340 put ap_sha_h 0 ap_sha_mask add get ap_sha_h 0 a
1341 put ap_sha_h 1 ap_sha_mask add get ap_sha_h 1 b
1342 put ap_sha_h 2 ap_sha_mask add get ap_sha_h 2 c
1343 put ap_sha_h 3 ap_sha_mask add get ap_sha_h 3 d
1344 put ap_sha_h 4 ap_sha_mask add get ap_sha_h 4 e
1345 put ap_sha_h 5 ap_sha_mask add get ap_sha_h 5 f
1346 put ap_sha_h 6 ap_sha_mask add get ap_sha_h 6 g
1347 put ap_sha_h 7 ap_sha_mask add get ap_sha_h 7 hh
1348end
1349
1350fn ap_sha_init to void
1351 put ap_sha_h 0 ulong 1779033703
1352 put ap_sha_h 1 ulong 3144134277
1353 put ap_sha_h 2 ulong 1013904242
1354 put ap_sha_h 3 ulong 2773480762
1355 put ap_sha_h 4 ulong 1359893119
1356 put ap_sha_h 5 ulong 2600822924
1357 put ap_sha_h 6 ulong 528734635
1358 put ap_sha_h 7 ulong 1541459225
1359 put ap_sha_st 1 0
1360 put ap_sha_st 2 0
1361end
1362
1363fn ap_sha_push_byte b uchar to void
1364 bl = get ap_sha_st 1
1365 put ap_sha_blk bl b
1366 bl = add bl 1
1367 put ap_sha_st 2 add get ap_sha_st 2 1
1368 if eq bl 64
1369 ap_sha_compress
1370 bl = ulong 0
1371 end
1372 put ap_sha_st 1 bl
1373end
1374
1375fn ap_sha_update data view uchar * n ulong to void
1376 for i ulong n bound 65536
1377 ap_sha_push_byte get data i
1378 end
1379end
1380
1381fn ap_sha_final dst edit uchar 32 to void
1382 tot = get ap_sha_st 2
1383 bl = get ap_sha_st 1
1384 put ap_sha_blk bl uchar 128
1385 bl = add bl 1
1386 if gt bl 56
1387 while lt bl 64 bound 64
1388 put ap_sha_blk bl uchar 0
1389 bl = add bl 1
1390 end
1391 ap_sha_compress
1392 bl = ulong 0
1393 end
1394 while lt bl 56 bound 64
1395 put ap_sha_blk bl uchar 0
1396 bl = add bl 1
1397 end
1398 bits = mul tot 8
1399 for i ulong 8
1400 sh = mul sub 7 i 8
1401 put ap_sha_blk add 56 i uchar and shr bits sh 255
1402 end
1403 ap_sha_compress
1404 for i ulong 8
1405 v = get ap_sha_h i
1406 for j ulong 4
1407 sh = mul sub 3 j 8
1408 put dst add mul i 4 j uchar and shr v sh 255
1409 end
1410 end
1411end
1412
1413note ---- byte buffer formatting helpers ----
1414fn ap_buf_copy dst edit uchar * pos ulong src view uchar * to ulong
1415 n = len src
1416 for i ulong n bound 4096
1417 put dst add pos i get src i
1418 end
1419 ret add pos n
1420end
1421
1422fn ap_buf_byte dst edit uchar * pos ulong b uchar to ulong
1423 put dst pos b
1424 ret add pos 1
1425end
1426
1427fn ap_buf_u64 dst edit uchar * pos ulong v ulong to ulong
1428 if eq v 0
1429 put dst pos uchar 48
1430 ret add pos 1
1431 end
1432 dig = new uchar 24 0
1433 nd = ulong 0
1434 x = v
1435 while gt x 0 bound 24
1436 put dig nd uchar add 48 rem x 10
1437 x = div x 10
1438 nd = add nd 1
1439 end
1440 p = pos
1441 for i ulong nd bound 24
1442 put dst p get dig sub sub nd 1 i
1443 p = add p 1
1444 end
1445 ret p
1446end
1447
1448fn ap_buf_hex64 dst edit uchar * pos ulong v ulong to ulong
1449 for i ulong 16
1450 sh = mul sub 15 i 4
1451 nib = and shr v sh 15
1452 put dst add pos i get ap_t_hex nib
1453 end
1454 ret add pos 16
1455end
1456
1457fn ap_buf_hex32 dst edit uchar * pos ulong d view uchar 32 to ulong
1458 for i ulong 32
1459 b = ulong get d i
1460 put dst add pos mul i 2 get ap_t_hex shr b 4
1461 put dst add pos add mul i 2 1 get ap_t_hex and b 15
1462 end
1463 ret add pos 64
1464end
1465
1466fn ap_pow10_u prec ulong to ulong
1467 r = ulong 1
1468 for i ulong prec bound 12
1469 r = mul r 10
1470 end
1471 ret r
1472end
1473
1474fn ap_buf_frac dst edit uchar * pos ulong fp ulong prec ulong to ulong
1475 put dst pos uchar 46
1476 k = fp
1477 for i ulong prec bound 12
1478 put dst sub add pos prec i uchar add 48 rem k 10
1479 k = div k 10
1480 end
1481 ret add add pos 1 prec
1482end
1483
1484fn ap_round_scaled v f64 su ulong to ulong
1485 sc = mul v f64 su
1486 fl = ulong sc
1487 rf = sub sc f64 fl
1488 if gt rf 0.5f64
1489 ret add fl 1
1490 end
1491 if and eq rf 0.5f64 eq and fl 1 1
1492 ret add fl 1
1493 end
1494 ret fl
1495end
1496
1497fn ap_buf_f64 dst edit uchar * pos ulong x f64 prec ulong plus_sign bool to ulong
1498 p = pos
1499 v = x
1500 if lt v 0.0f64
1501 put dst p uchar 45
1502 p = add p 1
1503 v = neg v
1504 else
1505 if plus_sign
1506 put dst p uchar 43
1507 p = add p 1
1508 end
1509 end
1510 su = ap_pow10_u prec
1511 fl = ap_round_scaled v su
1512 p = ap_buf_u64 dst p div fl su
1513 if gt prec 0
1514 p = ap_buf_frac dst p rem fl su prec
1515 end
1516 ret p
1517end
1518
1519fn ap_buf_wl_name dst edit uchar * pos ulong wl ulong to ulong
1520 if eq wl 0
1521 ret ap_buf_copy dst pos ap_t_wl0
1522 end
1523 if eq wl 1
1524 ret ap_buf_copy dst pos ap_t_wl1
1525 end
1526 if eq wl 2
1527 ret ap_buf_copy dst pos ap_t_wl2
1528 end
1529 if eq wl 3
1530 ret ap_buf_copy dst pos ap_t_wl3
1531 end
1532 if eq wl 4
1533 ret ap_buf_copy dst pos ap_t_wl4
1534 end
1535 if eq wl 5
1536 ret ap_buf_copy dst pos ap_t_wl5
1537 end
1538 if eq wl 6
1539 ret ap_buf_copy dst pos ap_t_wl6
1540 end
1541 if eq wl 7
1542 ret ap_buf_copy dst pos ap_t_wl7
1543 end
1544 ret ap_buf_copy dst pos ap_t_wlq
1545end
1546
1547note ---- canonical params text, CSV header, CSV row, digests ----
1548fn ap_pbuf_copy pos ulong src view uchar * to ulong
1549 n = len src
1550 for i ulong n bound 2048
1551 put ap_pbuf add pos i get src i
1552 end
1553 ret add pos n
1554end
1555
1556fn ap_params_text_to_pbuf to ulong
1557 p = ulong 0
1558 p = ap_pbuf_copy p ap_t_par0
1559 p = ap_pbuf_copy p ap_t_par1
1560 p = ap_pbuf_copy p ap_t_par2
1561 p = ap_pbuf_copy p ap_t_par3
1562 p = ap_pbuf_copy p ap_t_par4
1563 p = ap_pbuf_copy p ap_t_par5
1564 p = ap_pbuf_copy p ap_t_par6
1565 p = ap_pbuf_copy p ap_t_par7
1566 p = ap_pbuf_copy p ap_t_par8
1567 p = ap_pbuf_copy p ap_t_par9
1568 ret p
1569end
1570
1571fn ap_params_digest dst edit uchar 32 to void
1572 n = ap_params_text_to_pbuf
1573 ap_sha_init
1574 for i ulong n bound 2048
1575 ap_sha_push_byte get ap_pbuf i
1576 end
1577 ap_sha_final dst
1578end
1579
1580fn ap_row_header dst edit uchar 512 to ulong
1581 p = ulong 0
1582 p = ap_buf_copy dst p ap_t_hdr0
1583 p = ap_buf_copy dst p ap_t_hdr1
1584 p = ap_buf_copy dst p ap_t_hdr2
1585 p = ap_buf_copy dst p ap_t_hdr3
1586 p = ap_buf_copy dst p ap_t_hdr4
1587 ret p
1588end
1589
1590fn ap_row_format dst edit uchar 512 rslot ulong idx ulong to ulong
1591 ridx = add mul rslot AP_NROWS idx
1592 rub = mul ridx 7
1593 rfb = mul ridx 19
1594 p = ulong 0
1595 p = ap_buf_wl_name dst p get ap_row_u rub
1596 p = ap_buf_byte dst p uchar 44
1597 p = ap_buf_u64 dst p get ap_row_u add rub 1
1598 p = ap_buf_byte dst p uchar 44
1599 p = ap_buf_hex64 dst p get ap_row_u add rub 2
1600 p = ap_buf_byte dst p uchar 44
1601 p = ap_buf_u64 dst p get ap_row_u add rub 3
1602 p = ap_buf_byte dst p uchar 44
1603 p = ap_buf_u64 dst p get ap_row_u add rub 4
1604 p = ap_buf_byte dst p uchar 44
1605 p = ap_buf_u64 dst p get ap_row_u add rub 5
1606 p = ap_buf_byte dst p uchar 44
1607 p = ap_buf_u64 dst p get ap_row_u add rub 6
1608 for i ulong 19
1609 p = ap_buf_byte dst p uchar 44
1610 p = ap_buf_f64 dst p get ap_row_f add rfb i 6 false
1611 end
1612 ret p
1613end
1614
1615fn ap_row_header_to_line to ulong
1616 buf = new uchar 512 0
1617 n = ap_row_header buf
1618 for i ulong n bound 512
1619 put ap_line_buf i get buf i
1620 end
1621 ret n
1622end
1623
1624fn ap_row_format_to_line rslot ulong idx ulong to ulong
1625 buf = new uchar 512 0
1626 n = ap_row_format buf rslot idx
1627 for i ulong n bound 512
1628 put ap_line_buf i get buf i
1629 end
1630 ret n
1631end
1632
1633fn ap_rows_digest rslot ulong n ulong dst edit uchar 32 to void
1634 ap_sha_init
1635 hn = ap_row_header_to_line
1636 for i ulong hn bound 512
1637 ap_sha_push_byte get ap_line_buf i
1638 end
1639 ap_sha_push_byte uchar 10
1640 for r ulong n bound 256
1641 ln = ap_row_format_to_line rslot r
1642 for i ulong ln bound 512
1643 ap_sha_push_byte get ap_line_buf i
1644 end
1645 ap_sha_push_byte uchar 10
1646 end
1647 ap_sha_final dst
1648end