interplane.Get Omega
Read in your language

omega-feel

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-feel / feel.om

717 lines · 14445 bytes Raw Blame History
1note feel: the decision core of SPEC.md. Per class of work it decides which arm (way of running a
2note kernel) runs next. Rules: SPEC.md section 3. Program form (trace in, events out): section 4.
3note
4note What an engine calls (SPEC.md section 4 is the contract). Every entry checks its arguments and
5note the order of calls before it changes anything. A refusal changes nothing and is a result, not a
6note trap: setup, verdict and observe return 0 or an error code, choose returns ERR plus the code.
7note setup c K declare class c with K arms, once. K from 2 to KMAX.
8note r = choose c once per launch of class c, and only when the last launch of c is settled.
9note The arm to run is (and r 65535). (and shr r 32 3) says what is owed:
10note 0 nothing; 1 verdict c equal for arm (and shr r 16 65535), run on the same
11note input into scratch memory; 2 observe c t, the time of this launch.
12note verdict c equal equal is 1 when that arm's output equals the reference bit for bit, else 0.
13note observe c t t is the time the chosen arm took, below TMAX.
14note Every number is a non-negative ulong. A time is below 10^15, so that 256 pairs and the
15note multiplication by 64 in the mean rule stay below 2^64.
16note The constants of section 3.
17const H ulong 12
18const NMIN ulong 32
19const NMAX ulong 256
20const V ulong 3
21const WARM ulong 64
22const REST0 ulong 1024
23const RESTMAX ulong 65536
24note Adopt by mean needs MDEN * challenger below MNUM * incumbent, in both halves of the duel.
25const MNUM ulong 63
26const MDEN ulong 64
27note The order generator of 3.1: z = (z * LCGA + LCGC) mod LCGM.
28const LCGA ulong 1103515245
29const LCGC ulong 12345
30const LCGM ulong 2147483648
31note The limits of the interface. Each is a width of the machine word, not a size of a table: no
32note table here has a fixed size, every one grows with the classes and arms that are declared.
33note TMAX a time is below 10^15 (256 pairs times 64 times a time stays below 2^64).
34note KMAX arm numbers travel in 16 bits of the word choose returns.
35note CTOP conservative class input bound. One table of 2^52 ulong entries would occupy 32 PiB.
36note KTRACE the trace form writes the equal-output mask as a decimal below 10^15, which has room
37note for 49 arms (2^49 - 1 is below 10^15).
38note ERR choose returns ERR plus an error code when it refuses; a result is below 2^63.
39const TMAX ulong 1000000000000000
40const KMAX ulong 65535
41const CTOP ulong 4503599627370496
42const KTRACE ulong 49
43const ERR ulong 9223372036854775808
44note Error codes: 1 class not declared, 2 call out of order, 3 argument out of range, 4 class
45note already declared, 5 memory refused by the host.
46const E_CLASS ulong 1
47const E_STATE ulong 2
48const E_ARG ulong 3
49const E_DUP ulong 4
50const E_MEM ulong 5
51note
52note State of one class, index c, in run-time pools that grow when a class is declared. s_K is 0
53note until the class is declared. Modes: 0 rest, 1 shadow, 2 duel. s_owed: what the last choose
54note left owing (0 nothing, 1 a verdict, 2 a time). Per arm, in s_status and s_counts at index
55note s_base c + arm: status (0 untested, 1 admitted, 2 banned), count of launches.
56global s_K ulong *
57global s_inc ulong *
58global s_mode ulong *
59global s_rest ulong *
60global s_restlen ulong *
61global s_next ulong *
62global s_ch ulong *
63global s_vcount ulong *
64global s_k ulong *
65global s_w ulong *
66global s_l ulong *
67global s_n ulong *
68global s_sI ulong *
69global s_sC ulong *
70global s_hI ulong *
71global s_hC ulong *
72global s_tfirst ulong *
73global s_o ulong *
74global s_z ulong *
75global s_i ulong *
76global s_owed ulong *
77global s_base ulong *
78global s_status ulong *
79global s_counts ulong *
80global g_arms ulong 1
81note
82note The trace reader's own memory: input and output buffers, the times of one line, the byte
83note that ended the last number, the line number.
84global g_ib uchar 65536
85global g_ip ulong 1
86global g_il ulong 1
87global g_ob uchar 65536
88global g_op ulong 1
89global g_tm ulong *
90global g_term ulong 1
91global g_line ulong 1
92text t_bad "feel: bad input, line "
93text t_io "feel: cannot read standard input\n"
94text t_mem "feel: out of memory, line "
95note
96note Output: bytes gather in g_ob; oflush writes them all to fd. A failed write ends with status 3.
97fn oflush fd int to void
98n = get g_op 0
99off = ulong 0
100while lt off n bound 9223372036854775807
101r = write fd g_ob off sub n off
102if le r 0
103exit 3
104end
105off = add off ulong r
106end
107put g_op 0 0
108end
109fn ochr b ulong to void
110p = get g_op 0
111if ge p 65536
112oflush 1
113p = 0
114end
115put g_ob p uchar b
116put g_op 0 add p 1
117end
118note onum writes v in decimal; osn writes a space, then v.
119fn onum v ulong to void
120d = new uchar 20 0
121x = v
122m = ulong 0
123put d 0 uchar add 48 rem x 10
124x = div x 10
125while gt x 0 bound 20
126m = add m 1
127put d m uchar add 48 rem x 10
128x = div x 10
129end
130for j ulong add m 1
131ochr ulong get d sub m j
132end
133end
134fn osn v ulong to void
135ochr 32
136onum v
137end
138note
139note A duel starts (3.1): the counters of the pairs go to zero.
140fn start c ulong to void
141put s_mode c 2
142put s_k c 0
143put s_w c 0
144put s_l c 0
145put s_sI c 0
146put s_sC c 0
147put s_hI c 0
148put s_hC c 0
149put s_n c 0
150end
151note Growing the tables. growby n makes every per-class pool hold n classes: 0, or E_MEM when the host
152note refuses. s_K goes last, so a class number below len s_K has every pool behind it. Growing by the
153note one class asked for is enough: the run-time pool allocates ahead of its length, so a long run of
154note declarations is not quadratic. tests/run.sh has an alarm on the memory a long run needs.
155fn growby n ulong to ulong
156r = enlarge s_inc n
157if ne r 0
158ret E_MEM
159end
160r = enlarge s_mode n
161if ne r 0
162ret E_MEM
163end
164r = enlarge s_rest n
165if ne r 0
166ret E_MEM
167end
168r = enlarge s_restlen n
169if ne r 0
170ret E_MEM
171end
172r = enlarge s_next n
173if ne r 0
174ret E_MEM
175end
176r = enlarge s_ch n
177if ne r 0
178ret E_MEM
179end
180r = enlarge s_vcount n
181if ne r 0
182ret E_MEM
183end
184r = enlarge s_k n
185if ne r 0
186ret E_MEM
187end
188r = enlarge s_w n
189if ne r 0
190ret E_MEM
191end
192r = enlarge s_l n
193if ne r 0
194ret E_MEM
195end
196r = enlarge s_n n
197if ne r 0
198ret E_MEM
199end
200r = enlarge s_sI n
201if ne r 0
202ret E_MEM
203end
204r = enlarge s_sC n
205if ne r 0
206ret E_MEM
207end
208r = enlarge s_hI n
209if ne r 0
210ret E_MEM
211end
212r = enlarge s_hC n
213if ne r 0
214ret E_MEM
215end
216r = enlarge s_tfirst n
217if ne r 0
218ret E_MEM
219end
220r = enlarge s_o n
221if ne r 0
222ret E_MEM
223end
224r = enlarge s_z n
225if ne r 0
226ret E_MEM
227end
228r = enlarge s_i n
229if ne r 0
230ret E_MEM
231end
232r = enlarge s_owed n
233if ne r 0
234ret E_MEM
235end
236r = enlarge s_base n
237if ne r 0
238ret E_MEM
239end
240r = enlarge s_K n
241if ne r 0
242ret E_MEM
243end
244ret 0
245end
246fn growto n ulong to ulong
247if ge len s_K n
248ret 0
249end
250ret growby n
251end
252note The same for the arm pools: they must hold n arms.
253fn growarms n ulong to ulong
254if ge len s_status n
255ret 0
256end
257r = enlarge s_status n
258if ne r 0
259ret E_MEM
260end
261r = enlarge s_counts n
262if ne r 0
263ret E_MEM
264end
265ret 0
266end
267note known c is 1 when class c has been declared, else 0.
268fn known c ulong to ulong
269if ge c len s_K
270ret 0
271end
272if eq get s_K c 0
273ret 0
274end
275ret 1
276end
277note ix c a is the place of arm a of class c in s_status and s_counts.
278fn ix c ulong a ulong to ulong
279ret add get s_base c a
280end
281note A new class: arm 0 is the admitted reference and the incumbent. Every refusal leaves the class
282note tables as they were: nothing is written before the memory is there.
283fn setup c ulong kk ulong to ulong
284if or ge c CTOP or lt kk 2 gt kk KMAX
285ret E_ARG
286end
287if lt c len s_K
288if ne get s_K c 0
289ret E_DUP
290end
291end
292m = growto add c 1
293if ne m 0
294ret E_MEM
295end
296base = get g_arms 0
297m = growarms add base kk
298if ne m 0
299ret E_MEM
300end
301put g_arms 0 add base kk
302put s_base c base
303put s_status base 1
304put s_rest c WARM
305put s_restlen c REST0
306put s_z c 1
307put s_K c kk
308ret 0
309end
310note
311note Events. evshadow: CLASS B|M ARM I. evduel: CLASS A|R|Am|D ARM I N W L SI SC.
312fn evshadow c ulong letter ulong to void
313onum c
314ochr 32
315ochr letter
316osn get s_ch c
317osn get s_i c
318ochr 10
319end
320fn evduel c ulong out ulong to void
321onum c
322ochr 32
323if eq out 1
324ochr 65
325end
326if eq out 2
327ochr 82
328end
329if eq out 3
330ochr 65
331ochr 109
332end
333if eq out 4
334ochr 68
335end
336osn get s_ch c
337osn get s_i c
338osn get s_n c
339osn get s_w c
340osn get s_l c
341osn get s_sI c
342osn get s_sC c
343ochr 10
344end
345note
346note 3.1 Choose. pick is the round-robin: the first arm after s_next that is not the incumbent
347note and not banned, or K when there is none.
348fn pick c ulong to ulong
349kk = get s_K c
350inc = get s_inc c
351nx = get s_next c
352for s ulong kk
353a = rem add add nx s 1 kk
354if ne a inc
355if ne get s_status ix c a 2
356ret a
357end
358end
359end
360ret kk
361end
362fn choose c ulong to ulong
363if eq known c 0
364ret add ERR E_CLASS
365end
366if ne get s_owed c 0
367ret add ERR E_STATE
368end
369put s_i c add get s_i c 1
370inc = get s_inc c
371arm = inc
372if eq get s_mode c 0
373if gt get s_rest c 0
374put s_rest c sub get s_rest c 1
375else
376kk = get s_K c
377found = pick c
378if eq found kk
379put s_rest c get s_restlen c
380else
381put s_ch c found
382put s_next c found
383if eq get s_status ix c found 0
384put s_mode c 1
385put s_vcount c 0
386else
387start c
388end
389end
390end
391end
392kind = get s_mode c
393if eq kind 2
394k = get s_k c
395if eq and k 1 0
396z = rem add mul get s_z c LCGA LCGC LCGM
397put s_z c z
398put s_o c and shr z 16 1
399end
400if ne and k 1 get s_o c
401arm = get s_ch c
402end
403end
404ai = ix c arm
405put s_counts ai add get s_counts ai 1
406put s_owed c kind
407ret or arm or shl kind 32 shl get s_ch c 16
408end
409note
410note 3.2 Verdict.
411fn verdict c ulong equal ulong to ulong
412if eq known c 0
413ret E_CLASS
414end
415if ne get s_owed c 1
416ret E_STATE
417end
418if gt equal 1
419ret E_ARG
420end
421put s_owed c 0
422ci = ix c get s_ch c
423if eq equal 0
424put s_status ci 2
425evshadow c 66
426put s_mode c 0
427put s_rest c get s_restlen c
428else
429vc = add get s_vcount c 1
430put s_vcount c vc
431if eq vc V
432put s_status ci 1
433evshadow c 77
434start c
435end
436end
437ret 0
438end
439note
440note 3.3 Observe. Rule 2, at NMAX pairs; nothing is adopted before it. The first half of the
441note duel is (hI, hC), the second (rI, rC).
442note mean: 3 adopt by mean when the challenger's sum is below MNUM / MDEN of the incumbent's in
443note both halves, else 4 draw.
444fn mean c ulong to ulong
445hI = get s_hI c
446hC = get s_hC c
447rI = sub get s_sI c hI
448rC = sub get s_sC c hC
449if and lt mul MDEN hC mul MNUM hI lt mul MDEN rC mul MNUM rI
450ret 3
451end
452ret 4
453end
454note full: 1 adopt when the challenger's sum is lower in both halves and it leads the count by H;
455note otherwise what mean says.
456fn full c ulong to ulong
457hI = get s_hI c
458hC = get s_hC c
459rI = sub get s_sI c hI
460rC = sub get s_sC c hC
461w = get s_w c
462l = get s_l c
463if and lt hC hI and lt rC rI ge w add l H
464ret 1
465end
466ret mean c
467end
468note pair adds one finished pair (tI the incumbent's time, tC the challenger's) and gives how
469note the duel ends: 0 not yet, 1 adopt, 2 reject, 3 adopt by mean, 4 draw. Rule 1, the early
470note reject: the incumbent leads the count by H after NMIN pairs or more and the sums agree.
471fn pair c ulong tI ulong tC ulong to ulong
472sI = add get s_sI c tI
473sC = add get s_sC c tC
474put s_sI c sI
475put s_sC c sC
476w = get s_w c
477l = get s_l c
478if lt tC tI
479w = add w 1
480end
481if gt tC tI
482l = add l 1
483end
484put s_w c w
485put s_l c l
486n = add get s_n c 1
487put s_n c n
488if eq n div NMAX 2
489put s_hI c sI
490put s_hC c sC
491end
492if and ge n NMIN and ge l add w H ge sC sI
493ret 2
494end
495if ge n NMAX
496ret full c
497end
498ret 0
499end
500note observe: the first launch of a pair is kept; the second completes the pair. In the second
501note launch (k odd) the incumbent ran when the order bit is 1, the challenger when it is 0.
502fn observe c ulong t ulong to ulong
503if eq known c 0
504ret E_CLASS
505end
506if ne get s_owed c 2
507ret E_STATE
508end
509if ge t TMAX
510ret E_ARG
511end
512put s_owed c 0
513k = get s_k c
514if eq and k 1 0
515put s_tfirst c t
516else
517out = ulong 0
518if eq get s_o c 1
519out = pair c t get s_tfirst c
520else
521out = pair c get s_tfirst c t
522end
523if ne out 0
524evduel c out
525if or eq out 1 eq out 3
526put s_inc c get s_ch c
527put s_restlen c REST0
528else
529rl = mul get s_restlen c 2
530if gt rl RESTMAX
531rl = RESTMAX
532end
533put s_restlen c rl
534end
535put s_rest c get s_restlen c
536put s_mode c 0
537end
538end
539put s_k c add k 1
540ret 0
541end
542note
543note One launch, as an engine would run it: choose, then the verdict or the time that is owed.
544fn step c ulong mask ulong to void
545r = choose c
546if ge r ERR
547trap 8
548end
549what = and shr r 32 3
550e = ulong 0
551if eq what 1
552e = verdict c and shr mask and shr r 16 65535 1
553end
554if eq what 2
555e = observe c get g_tm and r 65535
556end
557if ne e 0
558trap 8
559end
560end
561note
562note The trace reader (section 4). Bad input: the events so far, one line on standard error, status 2.
563fn bad to void
564oflush 1
565for j ulong len t_bad
566ochr ulong get t_bad j
567end
568onum get g_line 0
569ochr 10
570oflush 2
571exit 2
572end
573note nomem: the host refused memory for the tables. The events so far, one line on standard error,
574note status 4.
575fn nomem to void
576oflush 1
577for j ulong len t_mem
578ochr ulong get t_mem j
579end
580onum get g_line 0
581ochr 10
582oflush 2
583exit 4
584end
585note getc gives the next input byte, or 256 at the end of input.
586fn getc to ulong
587p = get g_ip 0
588if ge p get g_il 0
589r = read 0 g_ib 0 65536
590if lt r 0
591oflush 1
592for j ulong len t_io
593ochr ulong get t_io j
594end
595oflush 2
596exit 3
597end
598if eq r 0
599ret 256
600end
601put g_il 0 ulong r
602p = 0
603end
604put g_ip 0 add p 1
605ret ulong get g_ib p
606end
607note num reads a decimal number of 1 to 15 digits and leaves the byte after it in g_term.
608fn num to ulong
609v = ulong 0
610nd = ulong 0
611b = getc
612while and ge b 48 le b 57 bound 9223372036854775807
613if ge nd 15
614bad
615end
616v = add mul v 10 sub b 48
617nd = add nd 1
618b = getc
619end
620if eq nd 0
621bad
622end
623put g_term 0 b
624ret v
625end
626note sep refuses the line unless the last number ended with byte want (32 space, 10 newline).
627fn sep want ulong to void
628if ne get g_term 0 want
629bad
630end
631end
632note c CLASS K
633fn cline to void
634if ne getc 32
635bad
636end
637c = num
638sep 32
639kk = num
640sep 10
641if or lt kk 2 gt kk KTRACE
642bad
643end
644r = setup c kk
645if eq r E_MEM
646nomem
647end
648if ne r 0
649bad
650end
651m = enlarge g_tm kk
652if ne m 0
653nomem
654end
655end
656note t CLASS t0 .. t(K-1) OKMASK
657fn tline to void
658if ne getc 32
659bad
660end
661c = num
662sep 32
663if eq known c 0
664bad
665end
666kk = get s_K c
667for j ulong kk
668put g_tm j num
669sep 32
670end
671mask = num
672sep 10
673if ge mask shl 1 kk
674bad
675end
676step c mask
677end
678note At end of input: CLASS F INC RESTLEN COUNTS.. STATUS.. for each declared class.
679fn finals to void
680nc = len s_K
681for c ulong nc bound 9223372036854775807
682kk = get s_K c
683if ne kk 0
684onum c
685ochr 32
686ochr 70
687osn get s_inc c
688osn get s_restlen c
689for a ulong kk
690osn get s_counts ix c a
691end
692for e ulong kk
693osn get s_status ix c e
694end
695ochr 10
696end
697end
698end
699fn main to int
700b = getc
701while ne b 256 bound 9223372036854775807
702put g_line 0 add get g_line 0 1
703if eq b 99
704cline
705else
706if eq b 116
707tline
708else
709bad
710end
711end
712b = getc
713end
714finals
715oflush 1
716ret 0
717end