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 / seed / helix / ollm_factory / om_sketch.om

3094 lines · 52248 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note seed/helix/ollm_factory/om_sketch.om:
4note Pure Seeded OM-2 Recursion-Scheme Sketcher for Stateful Loops, Digits,
5note Bit-Loops, Number Theory, Array Folds/Selects/Counts/Scans (shape 4),
6note and In-Place Array Rewrites (shape 5).
7note
8note Emits 100% self-contained inline OM-2 function bodies compatible with
9note `bench.om` (`fn solve PARAMS ... end`, zero external helper calls,
10note zero syntax errors, zero catastrophic forgetting).
11note
12note Usage:
13note om_sketch solve SPEC_FILE OUT_OM
14note om_sketch selfcheck
15
16global skInA ulong 8
17global skInB ulong 8
18global skInC ulong 8
19global skTgt ulong 8
20global skArr ulong 64
21global skArrN ulong 8
22global skOutArr ulong 64
23global skTmpArr ulong 8
24global skConsts ulong 32
25global skSt ulong 32
26global skPath uchar 4096
27global skInBuf uchar 65536
28global skOut uchar 65536
29
30text mSolve "solve"
31text mSelf "selfcheck"
32text tUsage "usage: om_sketch solve SPEC_FILE OUT_OM | selfcheck\n"
33text tErrIo "om_sketch: I/O error\n"
34text tOk "SKETCH_OK family="
35text tId " id="
36text tParam " k="
37text tFail "SKETCH_FAIL\n"
38text tNl "\n"
39text tSelfOk "OM_SKETCH SELFCHECK PASS scalar_loops=5/5 fold=3/3 rewrite=3/3\n"
40text tSelfFail "OM_SKETCH SELFCHECK FAIL step="
41
42text fScalar "scalar_loop"
43text fBin "binary_loop"
44text fTern "ternary_branch"
45text fFold "fold"
46text fRew "rewrite"
47
48global skipMatches ulong 1
49text kwSkip "skip "
50text kwRewrites "rewrites "
51global rewriteMask ulong 1
52global scaleResult ulong 1
53text kwArity "arity "
54text kwConsts "consts "
55text kwCase "case "
56text kwFold "fold "
57text kwRew "rew "
58
59text tFn1 "fn solve a ulong to ulong\n"
60text tFn2 "fn solve a ulong b ulong to ulong\n"
61text tFn3 "fn solve a ulong b ulong c ulong to ulong\n"
62text tFn4 "fn solve a view ulong 8 n ulong to ulong\n"
63text tFn5 "fn solve a edit ulong 8 n ulong to void\n"
64
65note ======== Self-contained inline OM-2 bodies for Shape 1 scalar loops ========
66text bPopcnt "c = ulong 0\nx = a\nwhile gt x 0 bound 64\nc = add c and x 1\nx = shr x 1\nend\nret c\nend\n"
67text bBitlen "c = ulong 0\nx = a\nwhile gt x 0 bound 64\nc = add c 1\nx = shr x 1\nend\nret c\nend\n"
68text bCtz "if eq a 0\nret 64\nend\nc = ulong 0\nx = a\nwhile eq and x 1 0 bound 64\nc = add c 1\nx = shr x 1\nend\nret c\nend\n"
69text bLowbit "if eq a 0\nret 0\nend\nret and a sub 0 a\nend\n"
70text bHighpow2 "if eq a 0\nret 0\nend\np = ulong 1\nx = a\nwhile gt x 1 bound 64\np = shl p 1\nx = shr x 1\nend\nret p\nend\n"
71text bParity "c = ulong 0\nx = a\nwhile gt x 0 bound 64\nc = xor c and x 1\nx = shr x 1\nend\nret c\nend\n"
72text bIsPow2 "if eq a 0\nret 0\nend\nif eq and a sub a 1 0\nret 1\nend\nret 0\nend\n"
73text bDigSum "s = ulong 0\nx = a\nwhile gt x 0 bound 24\ns = add s rem x 10\nx = div x 10\nend\nret s\nend\n"
74text bDigCnt "if eq a 0\nret 1\nend\nc = ulong 0\nx = a\nwhile gt x 0 bound 24\nc = add c 1\nx = div x 10\nend\nret c\nend\n"
75text bDigRev "r = ulong 0\nx = a\nwhile gt x 0 bound 24\nr = add mul r 10 rem x 10\nx = div x 10\nend\nret r\nend\n"
76text bDigProd "if eq a 0\nret 0\nend\np = ulong 1\nx = a\nwhile gt x 0 bound 24\np = mul p rem x 10\nx = div x 10\nend\nret p\nend\n"
77text bDigMax "m = ulong 0\nx = a\nwhile gt x 0 bound 24\nd = rem x 10\nif gt d m\nm = d\nend\nx = div x 10\nend\nret m\nend\n"
78text bDigMin "if eq a 0\nret 0\nend\nm = ulong 9\nx = a\nwhile gt x 0 bound 24\nd = rem x 10\nif lt d m\nm = d\nend\nx = div x 10\nend\nret m\nend\n"
79text bDigPal "r = ulong 0\nx = a\nwhile gt x 0 bound 24\nr = add mul r 10 rem x 10\nx = div x 10\nend\nif eq r a\nret 1\nend\nret 0\nend\n"
80text bDigRoot "x = a\nwhile ge x 10 bound 16\ns = ulong 0\nwhile gt x 0 bound 24\ns = add s rem x 10\nx = div x 10\nend\nx = s\nend\nret x\nend\n"
81text bIsqrt "if le a 1\nret a\nend\nlo = ulong 1\nhi = ulong 4294967296\nif lt a hi\nhi = a\nend\nans = ulong 1\nwhile le lo hi bound 64\nmid = add lo shr sub hi lo 1\nif le mid div a mid\nans = mid\nlo = add mid 1\nelse\nhi = sub mid 1\nend\nend\nret ans\nend\n"
82text bIsPrime "if lt a 2\nret 0\nend\nif eq a 2\nret 1\nend\nif eq rem a 2 0\nret 0\nend\nd = ulong 3\nwhile le d div a d bound 2000000\nif eq rem a d 0\nret 0\nend\nd = add d 2\nend\nret 1\nend\n"
83text bSmallFac "if le a 1\nret a\nend\nif eq rem a 2 0\nret 2\nend\nd = ulong 3\nwhile le d div a d bound 2000000\nif eq rem a d 0\nret d\nend\nd = add d 2\nend\nret a\nend\n"
84text bFact "if gt a 20\nret 0\nend\nr = ulong 1\ni = ulong 2\nwhile le i a bound 24\nr = mul r i\ni = add i 1\nend\nret r\nend\n"
85text bFib "if gt a 93\nret 0\nend\nif eq a 0\nret 0\nend\np = ulong 0\nc = ulong 1\ni = ulong 1\nwhile lt i a bound 96\nt = add p c\np = c\nc = t\ni = add i 1\nend\nret c\nend\n"
86text bCollatz "if le a 1\nret 0\nend\nx = a\ns = ulong 0\nwhile gt x 1 bound 2000\nif eq and x 1 0\nx = shr x 1\nelse\nx = add mul 3 x 1\nend\ns = add s 1\nend\nret s\nend\n"
87text bBswap16 "lo = and a 255\nhi = and shr a 8 255\nret or shl lo 8 hi\nend\n"
88text bInv16 "ret xor and a 65535 65535\nend\n"
89text bBitRev8 "r = ulong 0\nx = a\nfor i ulong 8 bound 8\nr = or shl r 1 and x 1\nx = shr x 1\nend\nret r\nend\n"
90text bDivCnt "if eq a 0\nret 0\nend\nc = ulong 0\nd = ulong 1\nwhile le d div a d bound 2000000\nif eq rem a d 0\nc = add c 1\nif ne d div a d\nc = add c 1\nend\nend\nd = add d 1\nend\nret c\nend\n"
91text bDivSum "if eq a 0\nret 0\nend\ns = ulong 0\nd = ulong 1\nwhile le d div a d bound 2000000\nif eq rem a d 0\ns = add s d\nq = div a d\nif ne d q\ns = add s q\nend\nend\nd = add d 1\nend\nret s\nend\n"
92text bTrailZ10 "if eq a 0\nret 0\nend\nc = ulong 0\nx = a\nwhile eq rem x 10 0 bound 24\nc = add c 1\nx = div x 10\nend\nret c\nend\n"
93text bOddPart "if eq a 0\nret 0\nend\nx = a\nwhile eq and x 1 0 bound 64\nx = shr x 1\nend\nret x\nend\n"
94text bNextPow2 "if le a 1\nret 1\nend\np = ulong 1\nwhile lt p a bound 64\np = shl p 1\nend\nret p\nend\n"
95text bTriSum "ret div mul a add a 1 2\nend\n"
96text bDigEven "if eq a 0\nret 1\nend\nc = ulong 0\nx = a\nwhile gt x 0 bound 24\nif eq and rem x 10 1 0\nc = add c 1\nend\nx = div x 10\nend\nret c\nend\n"
97text bDigOdd "c = ulong 0\nx = a\nwhile gt x 0 bound 24\nif ne and rem x 10 1 0\nc = add c 1\nend\nx = div x 10\nend\nret c\nend\n"
98
99note ======== Shape 2 binary loop skeletons ========
100text bGcd "x = a\ny = b\nwhile ne y 0 bound 128\nt = rem x y\nx = y\ny = t\nend\nret x\nend\n"
101text bLcm "if or eq a 0 eq b 0\nret 0\nend\nx = a\ny = b\nwhile ne y 0 bound 128\nt = rem x y\nx = y\ny = t\nend\nret mul div a x b\nend\n"
102text bPow "r = ulong 1\nbase = a\ne = b\nwhile gt e 0 bound 64\nif ne and e 1 0\nr = mul r base\nend\nbase = mul base base\ne = shr e 1\nend\nret r\nend\n"
103text bHamm "x = xor a b\nc = ulong 0\nwhile gt x 0 bound 64\nc = add c and x 1\nx = shr x 1\nend\nret c\nend\n"
104text bSafeDiv "if eq b 0\nret 0\nend\nret div a b\nend\n"
105text bSafeRem "if eq b 0\nret 0\nend\nret rem a b\nend\n"
106text bCeilDiv "if eq b 0\nret 0\nend\nq = div a b\nif ne rem a b 0\nq = add q 1\nend\nret q\nend\n"
107text bSatSub "if le a b\nret 0\nend\nret sub a b\nend\n"
108text bRoundDown "if eq b 0\nret 0\nend\nret sub a rem a b\nend\n"
109text bRoundUp "if eq b 0\nret 0\nend\nr = rem a b\nif eq r 0\nret a\nend\nret add a sub b r\nend\n"
110text bDigSum2 "s = ulong 0\nx = a\nwhile gt x 0 bound 24\ns = add s rem x 10\nx = div x 10\nend\nx = b\nwhile gt x 0 bound 24\ns = add s rem x 10\nx = div x 10\nend\nret s\nend\n"
111text bDigCnt2 "c = ulong 0\nif eq a 0\nc = add c 1\nelse\nx = a\nwhile gt x 0 bound 24\nc = add c 1\nx = div x 10\nend\nend\nif eq b 0\nc = add c 1\nelse\nx = b\nwhile gt x 0 bound 24\nc = add c 1\nx = div x 10\nend\nend\nret c\nend\n"
112text bPackBytes "ret or shl and a 255 8 and b 255\nend\n"
113text bTwoDigLast "ret add mul rem a 10 10 rem b 10\nend\n"
114
115note ======== Shape 3 ternary skeletons (a, b, c) ========
116text bClamp "if lt a b\nret b\nend\nif gt a c\nret c\nend\nret a\nend\n"
117text bMed3 "if le a b\nif le b c\nret b\nend\nif le a c\nret c\nend\nret a\nend\nif le a c\nret a\nend\nif le b c\nret c\nend\nret b\nend\n"
118text bMax3 "m = a\nif gt b m\nm = b\nend\nif gt c m\nm = c\nend\nret m\nend\n"
119text bMin3 "m = a\nif lt b m\nm = b\nend\nif lt c m\nm = c\nend\nret m\nend\n"
120text bInRange "if and ge a b le a c\nret 1\nend\nret 0\nend\n"
121text bSum3 "ret add add a b c\nend\n"
122text bProd3 "ret mul mul a b c\nend\n"
123text bMux "if ne a 0\nret b\nend\nret c\nend\n"
124text bOrd3 "if and le a b le b c\nret 1\nend\nret 0\nend\n"
125text bDist3 "c1 = ulong 1\nif ne b a\nc1 = add c1 1\nend\nif and ne c a ne c b\nc1 = add c1 1\nend\nret c1\nend\n"
126text bCntOdd3 "ret add add and a 1 and b 1 and c 1\nend\n"
127text bMajBit3 "ret or or and a b and a c and b c\nend\n"
128text bXor3 "ret xor xor a b c\nend\n"
129text bBitMux3 "ret or and a c sub b and b c\nend\n"
130text bAvg3 "ret div add add a b c 3\nend\n"
131text bGcd3 "x = a\ny = b\nwhile ne y 0 bound 128\nt = rem x y\nx = y\ny = t\nend\ny = c\nwhile ne y 0 bound 128\nt = rem x y\nx = y\ny = t\nend\nret x\nend\n"
132text bDup3 "if or eq a b eq a c\nret a\nend\nif eq b c\nret b\nend\nret 0\nend\n"
133text bOddOut3 "if eq a b\nret c\nend\nif eq a c\nret b\nend\nret a\nend\n"
134text bFma3 "ret add mul a b c\nend\n"
135text bCntEqF3 "r = ulong 1\nif eq b a\nr = add r 1\nend\nif eq c a\nr = add r 1\nend\nret r\nend\n"
136text bMuxC0 "if eq c 0\nret a\nend\nret b\nend\n"
137text bParOp3 "if eq and c 1 0\nret add a b\nend\nret mul a b\nend\n"
138
139note ======== Shape 4 fold/select/count/scan skeletons ========
140text f0Sum "acc = ulong 0\nfor i ulong n bound 8\nacc = add acc get a i\nend\nret acc\nend\n"
141text f1Prod "acc = ulong 1\nfor i ulong n bound 8\nacc = mul acc get a i\nend\nret acc\nend\n"
142text f2Max "acc = ulong 0\nfor i ulong n bound 8\nv = get a i\nif gt v acc\nacc = v\nend\nend\nret acc\nend\n"
143text f3Min "if eq n 0\nret 0\nend\nacc = get a 0\nfor i ulong n bound 8\nv = get a i\nif lt v acc\nacc = v\nend\nend\nret acc\nend\n"
144text f4Xor "acc = ulong 0\nfor i ulong n bound 8\nacc = xor acc get a i\nend\nret acc\nend\n"
145text f5Or "acc = ulong 0\nfor i ulong n bound 8\nacc = or acc get a i\nend\nret acc\nend\n"
146text f6And "if eq n 0\nret 0\nend\nacc = get a 0\nfor i ulong n bound 8\nacc = and acc get a i\nend\nret acc\nend\n"
147text f7CntEvn "acc = ulong 0\nfor i ulong n bound 8\nif eq and get a i 1 0\nacc = add acc 1\nend\nend\nret acc\nend\n"
148text f8CntOdd "acc = ulong 0\nfor i ulong n bound 8\nif ne and get a i 1 0\nacc = add acc 1\nend\nend\nret acc\nend\n"
149text f9CntNz "acc = ulong 0\nfor i ulong n bound 8\nif ne get a i 0\nacc = add acc 1\nend\nend\nret acc\nend\n"
150text f10CntZ "acc = ulong 0\nfor i ulong n bound 8\nif eq get a i 0\nacc = add acc 1\nend\nend\nret acc\nend\n"
151text f11SumSq "acc = ulong 0\nfor i ulong n bound 8\nv = get a i\nacc = add acc mul v v\nend\nret acc\nend\n"
152text f12SumEvn "acc = ulong 0\nfor i ulong n bound 8\nv = get a i\nif eq and v 1 0\nacc = add acc v\nend\nend\nret acc\nend\n"
153text f13SumOdd "acc = ulong 0\nfor i ulong n bound 8\nv = get a i\nif ne and v 1 0\nacc = add acc v\nend\nend\nret acc\nend\n"
154text f14Range "if eq n 0\nret 0\nend\nlo = get a 0\nhi = get a 0\nfor i ulong n bound 8\nv = get a i\nif lt v lo\nlo = v\nend\nif gt v hi\nhi = v\nend\nend\nret sub hi lo\nend\n"
155text f15Argmax "if eq n 0\nret 0\nend\nbi = ulong 0\nbv = get a 0\nfor i ulong n bound 8\nv = get a i\nif gt v bv\nbv = v\nbi = i\nend\nend\nret bi\nend\n"
156text f16Argmin "if eq n 0\nret 0\nend\nbi = ulong 0\nbv = get a 0\nfor i ulong n bound 8\nv = get a i\nif lt v bv\nbv = v\nbi = i\nend\nend\nret bi\nend\n"
157text f17Sorted "if le n 1\nret 1\nend\ni = ulong 1\nwhile lt i n bound 8\nif lt get a i get a sub i 1\nret 0\nend\ni = add i 1\nend\nret 1\nend\n"
158text f18StrictInc "if le n 1\nret 1\nend\ni = ulong 1\nwhile lt i n bound 8\nif le get a i get a sub i 1\nret 0\nend\ni = add i 1\nend\nret 1\nend\n"
159text f19AllEq "if le n 1\nret 1\nend\nv0 = get a 0\nfor i ulong n bound 8\nif ne get a i v0\nret 0\nend\nend\nret 1\nend\n"
160text f20FirstNz "for i ulong n bound 8\nv = get a i\nif ne v 0\nret v\nend\nend\nret 0\nend\n"
161text f21LastNz "r = ulong 0\nfor i ulong n bound 8\nv = get a i\nif ne v 0\nr = v\nend\nend\nret r\nend\n"
162text f22WtSum "acc = ulong 0\nfor i ulong n bound 8\nacc = add acc mul i get a i\nend\nret acc\nend\n"
163text f23SumAdjDiff "if le n 1\nret 0\nend\ns = ulong 0\ni = ulong 1\nwhile lt i n bound 8\nx = get a i\ny = get a sub i 1\nif ge x y\ns = add s sub x y\nelse\ns = add s sub y x\nend\ni = add i 1\nend\nret s\nend\n"
164text f24MaxAdjDiff "if le n 1\nret 0\nend\nm = ulong 0\ni = ulong 1\nwhile lt i n bound 8\nx = get a i\ny = get a sub i 1\nd = ulong 0\nif ge x y\nd = sub x y\nelse\nd = sub y x\nend\nif gt d m\nm = d\nend\ni = add i 1\nend\nret m\nend\n"
165text f25CntInc "if le n 1\nret 0\nend\nc = ulong 0\ni = ulong 1\nwhile lt i n bound 8\nif gt get a i get a sub i 1\nc = add c 1\nend\ni = add i 1\nend\nret c\nend\n"
166text f26PrefixMaxCnt "if eq n 0\nret 0\nend\nc = ulong 1\nm = get a 0\ni = ulong 1\nwhile lt i n bound 8\nv = get a i\nif gt v m\nm = v\nc = add c 1\nend\ni = add i 1\nend\nret c\nend\n"
167text f27SecMax "if le n 1\nret 0\nend\nm1 = ulong 0\nm2 = ulong 0\nfor i ulong n bound 8\nv = get a i\nif gt v m1\nm2 = m1\nm1 = v\nelse\nif and gt v m2 lt v m1\nm2 = v\nend\nend\nend\nret m2\nend\n"
168text f28LongNzRun "best = ulong 0\ncur = ulong 0\nfor i ulong n bound 8\nif ne get a i 0\ncur = add cur 1\nif gt cur best\nbest = cur\nend\nelse\ncur = 0\nend\nend\nret best\nend\n"
169text f29First "if eq n 0\nret 0\nend\nret get a 0\nend\n"
170text f30Last "if eq n 0\nret 0\nend\nret get a sub n 1\nend\n"
171text f31SumEvnIdx "acc = ulong 0\nfor i ulong n bound 8\nif eq and i 1 0\nacc = add acc get a i\nend\nend\nret acc\nend\n"
172text f32AltSum "acc = ulong 0\nfor i ulong n bound 8\nif eq and i 1 0\nacc = add acc get a i\nelse\nacc = sub acc get a i\nend\nend\nret acc\nend\n"
173text f33CntEqF "if eq n 0\nret 0\nend\nv0 = get a 0\nc = ulong 0\nfor i ulong n bound 8\nif eq get a i v0\nc = add c 1\nend\nend\nret c\nend\n"
174text f34CntLtF "if eq n 0\nret 0\nend\nv0 = get a 0\nc = ulong 0\nfor i ulong n bound 8\nif lt get a i v0\nc = add c 1\nend\nend\nret c\nend\n"
175text f35CntEqIdx "c = ulong 0\nfor i ulong n bound 8\nif eq get a i i\nc = add c 1\nend\nend\nret c\nend\n"
176text f36CntDist "c = ulong 0\nfor i ulong n bound 8\nseen = ulong 0\nfor j ulong i bound 8\nif eq get a j get a i\nseen = 1\nend\nend\nif eq seen 0\nc = add c 1\nend\nend\nret c\nend\n"
177text f37HasDup "for i ulong n bound 8\nfor j ulong i bound 8\nif eq get a j get a i\nret 1\nend\nend\nend\nret 0\nend\n"
178text f38LastOddIdx "r = n\nfor i ulong n bound 8\nif ne and get a i 1 0\nr = i\nend\nend\nret r\nend\n"
179text f39LongEqRun "if eq n 0\nret 0\nend\nbest = ulong 1\ncur = ulong 1\ni = ulong 1\nwhile lt i n bound 8\nif eq get a i get a sub i 1\ncur = add cur 1\nif gt cur best\nbest = cur\nend\nelse\ncur = 1\nend\ni = add i 1\nend\nret best\nend\n"
180text f40ArrGcd "g = ulong 0\nfor i ulong n bound 8\nx = g\ny = get a i\nwhile ne y 0 bound 128\nt = rem x y\nx = y\ny = t\nend\ng = x\nend\nret g\nend\n"
181text f41EvnSubOdd "se = ulong 0\nso = ulong 0\nfor i ulong n bound 8\nif eq and i 1 0\nse = add se get a i\nelse\nso = add so get a i\nend\nend\nif lt se so\nret 0\nend\nret sub se so\nend\n"
182
183note ======== Shape 5 in-place array rewrite skeletons ========
184text r0Rev "i = ulong 0\nwhile lt mul i 2 n bound 8\nj = sub sub n 1 i\nt = get a i\nput a i get a j\nput a j t\ni = add i 1\nend\nend\n"
185text r1SortAsc "for i ulong n bound 8\nfor j ulong n bound 8\nif and gt j i lt get a j get a i\nt = get a i\nput a i get a j\nput a j t\nend\nend\nend\nend\n"
186text r2SortDesc "for i ulong n bound 8\nfor j ulong n bound 8\nif and gt j i gt get a j get a i\nt = get a i\nput a i get a j\nput a j t\nend\nend\nend\nend\n"
187text r3PrefSum "s = ulong 0\nfor i ulong n bound 8\ns = add s get a i\nput a i s\nend\nend\n"
188text r4PrefMax "m = ulong 0\nfor i ulong n bound 8\nv = get a i\nif gt v m\nm = v\nend\nput a i m\nend\nend\n"
189text r5Double "for i ulong n bound 8\nput a i mul get a i 2\nend\nend\n"
190text r6Halve "for i ulong n bound 8\nput a i div get a i 2\nend\nend\n"
191text r7Square "for i ulong n bound 8\nv = get a i\nput a i mul v v\nend\nend\n"
192text r8ZeroEvn "for i ulong n bound 8\nif eq and get a i 1 0\nput a i 0\nend\nend\nend\n"
193text r9ZeroOdd "for i ulong n bound 8\nif ne and get a i 1 0\nput a i 0\nend\nend\nend\n"
194text r10RotL "if gt n 1\nf = get a 0\ni = ulong 1\nwhile lt i n bound 8\nput a sub i 1 get a i\ni = add i 1\nend\nput a sub n 1 f\nend\nend\n"
195text r11RotR "if gt n 1\nl = get a sub n 1\ni = sub n 1\nwhile gt i 0 bound 8\nput a i get a sub i 1\ni = sub i 1\nend\nput a 0 l\nend\nend\n"
196text r12ShiftL "if gt n 0\ni = ulong 1\nwhile lt i n bound 8\nput a sub i 1 get a i\ni = add i 1\nend\nput a sub n 1 0\nend\nend\n"
197text r13ShiftR "if gt n 0\ni = sub n 1\nwhile gt i 0 bound 8\nput a i get a sub i 1\ni = sub i 1\nend\nput a 0 0\nend\nend\n"
198text r14SwapAdj "i = ulong 0\nwhile lt add i 1 n bound 8\nt = get a i\nput a i get a add i 1\nput a add i 1 t\ni = add i 2\nend\nend\n"
199text r15MoveZ "w = ulong 0\nfor i ulong n bound 8\nv = get a i\nif ne v 0\nput a w v\nw = add w 1\nend\nend\nwhile lt w n bound 8\nput a w 0\nw = add w 1\nend\nend\n"
200text r16AddIdx "for i ulong n bound 8\nput a i add get a i i\nend\nend\n"
201text r17Zero "for i ulong n bound 8\nput a i 0\nend\nend\n"
202text r18PrefXor "x = ulong 0\nfor i ulong n bound 8\nx = xor x get a i\nput a i x\nend\nend\n"
203text r19Inc1 "for i ulong n bound 8\nput a i add get a i 1\nend\nend\n"
204
205fn wStr fd int s view uchar * to void
206r = write fd s 0 len s
207end
208
209fn wNum fd int v ulong to void
210b = new uchar 24 0
211p = ulong 24
212x = v
213while true bound 24
214p = sub p 1
215put b p uchar add 48 rem x 10
216x = div x 10
217if eq x 0
218break
219end
220end
221r = write fd b p sub 24 p
222end
223
224fn eqTxt a view uchar * al ulong b view uchar * to bool
225if ne al len b
226ret false
227end
228for i ulong al bound 64
229if ne get a i get b i
230ret false
231end
232end
233ret true
234end
235
236fn oClr to void
237put skSt 9 0
238end
239
240fn oNum v ulong to void
241b = new uchar 24 0
242p = ulong 24
243x = v
244while true bound 24
245p = sub p 1
246put b p uchar add 48 rem x 10
247x = div x 10
248if eq x 0
249break
250end
251end
252op = get skSt 9
253while lt p 24 bound 24
254if lt op 65535
255put skOut op get b p
256op = add op 1
257end
258p = add p 1
259end
260put skSt 9 op
261end
262
263fn oTxt s view uchar * to void
264p = get skSt 9
265for i ulong len s bound 4096
266if lt p 65535
267put skOut p get s i
268p = add p 1
269end
270end
271put skSt 9 p
272end
273
274note ======== Evaluators for Shape 1 scalar loops (id 0..31) ========
275fn evalScA id ulong a ulong to ulong
276if eq id 0
277c = ulong 0
278x = a
279while gt x 0 bound 64
280c = add c and x 1
281x = shr x 1
282end
283ret c
284end
285if eq id 1
286c = ulong 0
287x = a
288while gt x 0 bound 64
289c = add c 1
290x = shr x 1
291end
292ret c
293end
294if eq id 2
295if eq a 0
296ret 64
297end
298c = ulong 0
299x = a
300while eq and x 1 0 bound 64
301c = add c 1
302x = shr x 1
303end
304ret c
305end
306if eq id 3
307if eq a 0
308ret 0
309end
310ret and a sub 0 a
311end
312if eq id 4
313if eq a 0
314ret 0
315end
316p = ulong 1
317x = a
318while gt x 1 bound 64
319p = shl p 1
320x = shr x 1
321end
322ret p
323end
324if eq id 5
325c = ulong 0
326x = a
327while gt x 0 bound 64
328c = xor c and x 1
329x = shr x 1
330end
331ret c
332end
333if eq id 6
334if eq a 0
335ret 0
336end
337if eq and a sub a 1 0
338ret 1
339end
340ret 0
341end
342if eq id 7
343s = ulong 0
344x = a
345while gt x 0 bound 24
346s = add s rem x 10
347x = div x 10
348end
349ret s
350end
351if eq id 8
352if eq a 0
353ret 1
354end
355c = ulong 0
356x = a
357while gt x 0 bound 24
358c = add c 1
359x = div x 10
360end
361ret c
362end
363if eq id 9
364r = ulong 0
365x = a
366while gt x 0 bound 24
367r = add mul r 10 rem x 10
368x = div x 10
369end
370ret r
371end
372if eq id 10
373if eq a 0
374ret 0
375end
376p = ulong 1
377x = a
378while gt x 0 bound 24
379p = mul p rem x 10
380x = div x 10
381end
382ret p
383end
384if eq id 11
385m = ulong 0
386x = a
387while gt x 0 bound 24
388d = rem x 10
389if gt d m
390m = d
391end
392x = div x 10
393end
394ret m
395end
396if eq id 12
397if eq a 0
398ret 0
399end
400m = ulong 9
401x = a
402while gt x 0 bound 24
403d = rem x 10
404if lt d m
405m = d
406end
407x = div x 10
408end
409ret m
410end
411if eq id 13
412r = ulong 0
413x = a
414while gt x 0 bound 24
415r = add mul r 10 rem x 10
416x = div x 10
417end
418if eq r a
419ret 1
420end
421ret 0
422end
423if eq id 14
424x = a
425while ge x 10 bound 16
426s = ulong 0
427while gt x 0 bound 24
428s = add s rem x 10
429x = div x 10
430end
431x = s
432end
433ret x
434end
435ret 0
436end
437
438fn evalScB id ulong a ulong to ulong
439if eq id 15
440if le a 1
441ret a
442end
443lo = ulong 1
444hi = ulong 4294967296
445if lt a hi
446hi = a
447end
448ans = ulong 1
449while le lo hi bound 64
450mid = add lo shr sub hi lo 1
451if le mid div a mid
452ans = mid
453lo = add mid 1
454else
455hi = sub mid 1
456end
457end
458ret ans
459end
460if eq id 16
461if lt a 2
462ret 0
463end
464if eq a 2
465ret 1
466end
467if eq rem a 2 0
468ret 0
469end
470d = ulong 3
471while le d div a d bound 2000000
472if eq rem a d 0
473ret 0
474end
475d = add d 2
476end
477ret 1
478end
479if eq id 17
480if le a 1
481ret a
482end
483if eq rem a 2 0
484ret 2
485end
486d = ulong 3
487while le d div a d bound 2000000
488if eq rem a d 0
489ret d
490end
491d = add d 2
492end
493ret a
494end
495if eq id 18
496if gt a 20
497ret 0
498end
499r = ulong 1
500i = ulong 2
501while le i a bound 24
502r = mul r i
503i = add i 1
504end
505ret r
506end
507if eq id 19
508if gt a 93
509ret 0
510end
511if eq a 0
512ret 0
513end
514p = ulong 0
515c = ulong 1
516i = ulong 1
517while lt i a bound 96
518t = add p c
519p = c
520c = t
521i = add i 1
522end
523ret c
524end
525if eq id 20
526if le a 1
527ret 0
528end
529x = a
530s = ulong 0
531while gt x 1 bound 2000
532if eq and x 1 0
533x = shr x 1
534else
535x = add mul 3 x 1
536end
537s = add s 1
538end
539ret s
540end
541if eq id 21
542ret or shl and a 255 8 and shr a 8 255
543end
544if eq id 22
545ret xor and a 65535 65535
546end
547if eq id 23
548r = ulong 0
549x = a
550for i ulong 8 bound 8
551r = or shl r 1 and x 1
552x = shr x 1
553end
554ret r
555end
556if eq id 24
557if eq a 0
558ret 0
559end
560c = ulong 0
561d = ulong 1
562while le d div a d bound 2000000
563if eq rem a d 0
564c = add c 1
565if ne d div a d
566c = add c 1
567end
568end
569d = add d 1
570end
571ret c
572end
573if eq id 25
574if eq a 0
575ret 0
576end
577s = ulong 0
578d = ulong 1
579while le d div a d bound 2000000
580if eq rem a d 0
581s = add s d
582q = div a d
583if ne d q
584s = add s q
585end
586end
587d = add d 1
588end
589ret s
590end
591if eq id 26
592if eq a 0
593ret 0
594end
595c = ulong 0
596x = a
597while eq rem x 10 0 bound 24
598c = add c 1
599x = div x 10
600end
601ret c
602end
603if eq id 27
604if eq a 0
605ret 0
606end
607x = a
608while eq and x 1 0 bound 64
609x = shr x 1
610end
611ret x
612end
613if eq id 28
614if le a 1
615ret 1
616end
617p = ulong 1
618while lt p a bound 64
619p = shl p 1
620end
621ret p
622end
623if eq id 29
624ret div mul a add a 1 2
625end
626if eq id 30
627if eq a 0
628ret 1
629end
630c = ulong 0
631x = a
632while gt x 0 bound 24
633if eq and rem x 10 1 0
634c = add c 1
635end
636x = div x 10
637end
638ret c
639end
640if eq id 31
641c = ulong 0
642x = a
643while gt x 0 bound 24
644if ne and rem x 10 1 0
645c = add c 1
646end
647x = div x 10
648end
649ret c
650end
651ret 0
652end
653
654fn evalSc id ulong a ulong to ulong
655if lt id 15
656ret evalScA id a
657end
658ret evalScB id a
659end
660
661fn emitScA id ulong to void
662if eq id 0
663oTxt bPopcnt
664end
665if eq id 1
666oTxt bBitlen
667end
668if eq id 2
669oTxt bCtz
670end
671if eq id 3
672oTxt bLowbit
673end
674if eq id 4
675oTxt bHighpow2
676end
677if eq id 5
678oTxt bParity
679end
680if eq id 6
681oTxt bIsPow2
682end
683if eq id 7
684oTxt bDigSum
685end
686if eq id 8
687oTxt bDigCnt
688end
689if eq id 9
690oTxt bDigRev
691end
692if eq id 10
693oTxt bDigProd
694end
695if eq id 11
696oTxt bDigMax
697end
698if eq id 12
699oTxt bDigMin
700end
701if eq id 13
702oTxt bDigPal
703end
704if eq id 14
705oTxt bDigRoot
706end
707end
708
709fn emitScB id ulong to void
710if eq id 15
711oTxt bIsqrt
712end
713if eq id 16
714oTxt bIsPrime
715end
716if eq id 17
717oTxt bSmallFac
718end
719if eq id 18
720oTxt bFact
721end
722if eq id 19
723oTxt bFib
724end
725if eq id 20
726oTxt bCollatz
727end
728if eq id 21
729oTxt bBswap16
730end
731if eq id 22
732oTxt bInv16
733end
734if eq id 23
735oTxt bBitRev8
736end
737if eq id 24
738oTxt bDivCnt
739end
740if eq id 25
741oTxt bDivSum
742end
743if eq id 26
744oTxt bTrailZ10
745end
746if eq id 27
747oTxt bOddPart
748end
749if eq id 28
750oTxt bNextPow2
751end
752if eq id 29
753oTxt bTriSum
754end
755if eq id 30
756oTxt bDigEven
757end
758if eq id 31
759oTxt bDigOdd
760end
761end
762
763note An experiment can request a later matching sketch. A normal solve selects the first.
764fn pickMatch matched bool to bool
765if not matched
766ret false
767end
768if eq get skipMatches 0 0
769ret true
770end
771put skipMatches 0 sub get skipMatches 0 1
772ret false
773end
774
775fn solveScalar to ulong
776nc = get skSt 0
777for id ulong 32 bound 32
778ok = true
779for i ulong nc bound 8
780if ne evalSc id get skInA i get skTgt i
781ok = false
782break
783end
784end
785if pickMatch ok
786oClr
787oTxt tFn1
788if lt id 15
789emitScA id
790else
791emitScB id
792end
793ret id
794end
795end
796ret 999
797end
798
799note ======== Binary loops (id 0..9) ========
800fn evalBin id ulong a ulong b ulong to ulong
801if eq id 0
802x = a
803y = b
804while ne y 0 bound 128
805t = rem x y
806x = y
807y = t
808end
809ret x
810end
811if eq id 1
812if or eq a 0 eq b 0
813ret 0
814end
815x = a
816y = b
817while ne y 0 bound 128
818t = rem x y
819x = y
820y = t
821end
822ret mul div a x b
823end
824if eq id 2
825r = ulong 1
826base = a
827e = b
828while gt e 0 bound 64
829if ne and e 1 0
830r = mul r base
831end
832base = mul base base
833e = shr e 1
834end
835ret r
836end
837if eq id 3
838x = xor a b
839c = ulong 0
840while gt x 0 bound 64
841c = add c and x 1
842x = shr x 1
843end
844ret c
845end
846if eq id 4
847if eq b 0
848ret 0
849end
850ret div a b
851end
852if eq id 5
853if eq b 0
854ret 0
855end
856ret rem a b
857end
858if eq id 6
859if eq b 0
860ret 0
861end
862q = div a b
863if ne rem a b 0
864q = add q 1
865end
866ret q
867end
868if eq id 7
869if le a b
870ret 0
871end
872ret sub a b
873end
874if eq id 8
875if eq b 0
876ret 0
877end
878ret sub a rem a b
879end
880if eq id 9
881if eq b 0
882ret 0
883end
884r = rem a b
885if eq r 0
886ret a
887end
888ret add a sub b r
889end
890if eq id 10
891s = ulong 0
892x = a
893while gt x 0 bound 24
894s = add s rem x 10
895x = div x 10
896end
897x = b
898while gt x 0 bound 24
899s = add s rem x 10
900x = div x 10
901end
902ret s
903end
904if eq id 11
905c = ulong 0
906if eq a 0
907c = add c 1
908else
909x = a
910while gt x 0 bound 24
911c = add c 1
912x = div x 10
913end
914end
915if eq b 0
916c = add c 1
917else
918x = b
919while gt x 0 bound 24
920c = add c 1
921x = div x 10
922end
923end
924ret c
925end
926if eq id 12
927ret or shl and a 255 8 and b 255
928end
929if eq id 13
930ret add mul rem a 10 10 rem b 10
931end
932ret 0
933end
934
935fn solveBinary to ulong
936nc = get skSt 0
937for id ulong 14 bound 14
938ok = true
939for i ulong nc bound 8
940if ne evalBin id get skInA i get skInB i get skTgt i
941ok = false
942break
943end
944end
945if pickMatch ok
946oClr
947oTxt tFn2
948if eq id 0
949oTxt bGcd
950end
951if eq id 1
952oTxt bLcm
953end
954if eq id 2
955oTxt bPow
956end
957if eq id 3
958oTxt bHamm
959end
960if eq id 4
961oTxt bSafeDiv
962end
963if eq id 5
964oTxt bSafeRem
965end
966if eq id 6
967oTxt bCeilDiv
968end
969if eq id 7
970oTxt bSatSub
971end
972if eq id 8
973oTxt bRoundDown
974end
975if eq id 9
976oTxt bRoundUp
977end
978if eq id 10
979oTxt bDigSum2
980end
981if eq id 11
982oTxt bDigCnt2
983end
984if eq id 12
985oTxt bPackBytes
986end
987if eq id 13
988oTxt bTwoDigLast
989end
990ret id
991end
992end
993ret 999
994end
995
996note ======== Ternary skeletons (id 0..21) ========
997fn evalTernA id ulong a ulong b ulong c ulong to ulong
998if eq id 0
999if lt a b
1000ret b
1001end
1002if gt a c
1003ret c
1004end
1005ret a
1006end
1007if eq id 1
1008if le a b
1009if le b c
1010ret b
1011end
1012if le a c
1013ret c
1014end
1015ret a
1016end
1017if le a c
1018ret a
1019end
1020if le b c
1021ret c
1022end
1023ret b
1024end
1025if eq id 2
1026m = a
1027if gt b m
1028m = b
1029end
1030if gt c m
1031m = c
1032end
1033ret m
1034end
1035if eq id 3
1036m = a
1037if lt b m
1038m = b
1039end
1040if lt c m
1041m = c
1042end
1043ret m
1044end
1045if eq id 4
1046if and ge a b le a c
1047ret 1
1048end
1049ret 0
1050end
1051if eq id 5
1052ret add add a b c
1053end
1054if eq id 6
1055ret mul mul a b c
1056end
1057if eq id 7
1058if ne a 0
1059ret b
1060end
1061ret c
1062end
1063if eq id 8
1064if and le a b le b c
1065ret 1
1066end
1067ret 0
1068end
1069if eq id 9
1070c1 = ulong 1
1071if ne b a
1072c1 = add c1 1
1073end
1074if and ne c a ne c b
1075c1 = add c1 1
1076end
1077ret c1
1078end
1079if eq id 10
1080ret add add and a 1 and b 1 and c 1
1081end
1082if eq id 11
1083ret or or and a b and a c and b c
1084end
1085ret 0
1086end
1087
1088fn evalTernB id ulong a ulong b ulong c ulong to ulong
1089if eq id 12
1090ret xor xor a b c
1091end
1092if eq id 13
1093ret or and a c sub b and b c
1094end
1095if eq id 14
1096ret div add add a b c 3
1097end
1098if eq id 15
1099x = a
1100y = b
1101while ne y 0 bound 128
1102t = rem x y
1103x = y
1104y = t
1105end
1106y = c
1107while ne y 0 bound 128
1108t = rem x y
1109x = y
1110y = t
1111end
1112ret x
1113end
1114if eq id 16
1115if or eq a b eq a c
1116ret a
1117end
1118if eq b c
1119ret b
1120end
1121ret 0
1122end
1123if eq id 17
1124if eq a b
1125ret c
1126end
1127if eq a c
1128ret b
1129end
1130ret a
1131end
1132if eq id 18
1133ret add mul a b c
1134end
1135if eq id 19
1136r = ulong 1
1137if eq b a
1138r = add r 1
1139end
1140if eq c a
1141r = add r 1
1142end
1143ret r
1144end
1145if eq id 20
1146if eq c 0
1147ret a
1148end
1149ret b
1150end
1151if eq id 21
1152if eq and c 1 0
1153ret add a b
1154end
1155ret mul a b
1156end
1157ret 0
1158end
1159
1160fn evalTern id ulong a ulong b ulong c ulong to ulong
1161if lt id 12
1162ret evalTernA id a b c
1163end
1164ret evalTernB id a b c
1165end
1166
1167fn emitTern id ulong to void
1168if eq id 0
1169oTxt bClamp
1170end
1171if eq id 1
1172oTxt bMed3
1173end
1174if eq id 2
1175oTxt bMax3
1176end
1177if eq id 3
1178oTxt bMin3
1179end
1180if eq id 4
1181oTxt bInRange
1182end
1183if eq id 5
1184oTxt bSum3
1185end
1186if eq id 6
1187oTxt bProd3
1188end
1189if eq id 7
1190oTxt bMux
1191end
1192if eq id 8
1193oTxt bOrd3
1194end
1195if eq id 9
1196oTxt bDist3
1197end
1198if eq id 10
1199oTxt bCntOdd3
1200end
1201if eq id 11
1202oTxt bMajBit3
1203end
1204if eq id 12
1205oTxt bXor3
1206end
1207if eq id 13
1208oTxt bBitMux3
1209end
1210if eq id 14
1211oTxt bAvg3
1212end
1213if eq id 15
1214oTxt bGcd3
1215end
1216if eq id 16
1217oTxt bDup3
1218end
1219if eq id 17
1220oTxt bOddOut3
1221end
1222if eq id 18
1223oTxt bFma3
1224end
1225if eq id 19
1226oTxt bCntEqF3
1227end
1228if eq id 20
1229oTxt bMuxC0
1230end
1231if eq id 21
1232oTxt bParOp3
1233end
1234end
1235
1236fn solveTernary to ulong
1237nc = get skSt 0
1238for id ulong 22 bound 22
1239ok = true
1240for i ulong nc bound 8
1241if ne evalTern id get skInA i get skInB i get skInC i get skTgt i
1242ok = false
1243break
1244end
1245end
1246if pickMatch ok
1247oClr
1248oTxt tFn3
1249emitTern id
1250ret id
1251end
1252end
1253ret 999
1254end
1255
1256note ======== Fold/Select/Count/Scan evaluators (id 0..30) ========
1257fn evalFoldA sk ulong ci ulong to ulong
1258n = get skArrN ci
1259base = mul ci 8
1260if eq sk 0
1261acc = ulong 0
1262for i ulong n bound 8
1263acc = add acc get skArr add base i
1264end
1265ret acc
1266end
1267if eq sk 1
1268acc = ulong 1
1269for i ulong n bound 8
1270acc = mul acc get skArr add base i
1271end
1272ret acc
1273end
1274if eq sk 2
1275acc = ulong 0
1276for i ulong n bound 8
1277v = get skArr add base i
1278if gt v acc
1279acc = v
1280end
1281end
1282ret acc
1283end
1284if eq sk 3
1285if eq n 0
1286ret 0
1287end
1288acc = get skArr base
1289for i ulong n bound 8
1290v = get skArr add base i
1291if lt v acc
1292acc = v
1293end
1294end
1295ret acc
1296end
1297if eq sk 4
1298acc = ulong 0
1299for i ulong n bound 8
1300acc = xor acc get skArr add base i
1301end
1302ret acc
1303end
1304if eq sk 5
1305acc = ulong 0
1306for i ulong n bound 8
1307acc = or acc get skArr add base i
1308end
1309ret acc
1310end
1311if eq sk 6
1312if eq n 0
1313ret 0
1314end
1315acc = get skArr base
1316for i ulong n bound 8
1317acc = and acc get skArr add base i
1318end
1319ret acc
1320end
1321if eq sk 7
1322acc = ulong 0
1323for i ulong n bound 8
1324if eq and get skArr add base i 1 0
1325acc = add acc 1
1326end
1327end
1328ret acc
1329end
1330if eq sk 8
1331acc = ulong 0
1332for i ulong n bound 8
1333if ne and get skArr add base i 1 0
1334acc = add acc 1
1335end
1336end
1337ret acc
1338end
1339if eq sk 9
1340acc = ulong 0
1341for i ulong n bound 8
1342if ne get skArr add base i 0
1343acc = add acc 1
1344end
1345end
1346ret acc
1347end
1348if eq sk 10
1349acc = ulong 0
1350for i ulong n bound 8
1351if eq get skArr add base i 0
1352acc = add acc 1
1353end
1354end
1355ret acc
1356end
1357if eq sk 11
1358acc = ulong 0
1359for i ulong n bound 8
1360v = get skArr add base i
1361acc = add acc mul v v
1362end
1363ret acc
1364end
1365if eq sk 12
1366acc = ulong 0
1367for i ulong n bound 8
1368v = get skArr add base i
1369if eq and v 1 0
1370acc = add acc v
1371end
1372end
1373ret acc
1374end
1375if eq sk 13
1376acc = ulong 0
1377for i ulong n bound 8
1378v = get skArr add base i
1379if ne and v 1 0
1380acc = add acc v
1381end
1382end
1383ret acc
1384end
1385if eq sk 14
1386if eq n 0
1387ret 0
1388end
1389lo = get skArr base
1390hi = lo
1391for i ulong n bound 8
1392v = get skArr add base i
1393if lt v lo
1394lo = v
1395end
1396if gt v hi
1397hi = v
1398end
1399end
1400ret sub hi lo
1401end
1402ret 0
1403end
1404
1405fn evalFoldB sk ulong ci ulong to ulong
1406n = get skArrN ci
1407base = mul ci 8
1408if eq sk 15
1409if eq n 0
1410ret 0
1411end
1412bi = ulong 0
1413bv = get skArr base
1414for i ulong n bound 8
1415v = get skArr add base i
1416if gt v bv
1417bv = v
1418bi = i
1419end
1420end
1421ret bi
1422end
1423if eq sk 16
1424if eq n 0
1425ret 0
1426end
1427bi = ulong 0
1428bv = get skArr base
1429for i ulong n bound 8
1430v = get skArr add base i
1431if lt v bv
1432bv = v
1433bi = i
1434end
1435end
1436ret bi
1437end
1438if eq sk 17
1439if le n 1
1440ret 1
1441end
1442i = ulong 1
1443while lt i n bound 8
1444if lt get skArr add base i get skArr add base sub i 1
1445ret 0
1446end
1447i = add i 1
1448end
1449ret 1
1450end
1451if eq sk 18
1452if le n 1
1453ret 1
1454end
1455i = ulong 1
1456while lt i n bound 8
1457if le get skArr add base i get skArr add base sub i 1
1458ret 0
1459end
1460i = add i 1
1461end
1462ret 1
1463end
1464if eq sk 19
1465if le n 1
1466ret 1
1467end
1468v0 = get skArr base
1469for i ulong n bound 8
1470if ne get skArr add base i v0
1471ret 0
1472end
1473end
1474ret 1
1475end
1476if eq sk 20
1477for i ulong n bound 8
1478v = get skArr add base i
1479if ne v 0
1480ret v
1481end
1482end
1483ret 0
1484end
1485if eq sk 21
1486r = ulong 0
1487for i ulong n bound 8
1488v = get skArr add base i
1489if ne v 0
1490r = v
1491end
1492end
1493ret r
1494end
1495if eq sk 22
1496acc = ulong 0
1497for i ulong n bound 8
1498acc = add acc mul i get skArr add base i
1499end
1500ret acc
1501end
1502if eq sk 23
1503if le n 1
1504ret 0
1505end
1506s = ulong 0
1507i = ulong 1
1508while lt i n bound 8
1509x = get skArr add base i
1510y = get skArr add base sub i 1
1511if ge x y
1512s = add s sub x y
1513else
1514s = add s sub y x
1515end
1516i = add i 1
1517end
1518ret s
1519end
1520if eq sk 24
1521if le n 1
1522ret 0
1523end
1524m = ulong 0
1525i = ulong 1
1526while lt i n bound 8
1527x = get skArr add base i
1528y = get skArr add base sub i 1
1529d = ulong 0
1530if ge x y
1531d = sub x y
1532else
1533d = sub y x
1534end
1535if gt d m
1536m = d
1537end
1538i = add i 1
1539end
1540ret m
1541end
1542if eq sk 25
1543if le n 1
1544ret 0
1545end
1546c = ulong 0
1547i = ulong 1
1548while lt i n bound 8
1549if gt get skArr add base i get skArr add base sub i 1
1550c = add c 1
1551end
1552i = add i 1
1553end
1554ret c
1555end
1556if eq sk 26
1557if eq n 0
1558ret 0
1559end
1560c = ulong 1
1561m = get skArr base
1562i = ulong 1
1563while lt i n bound 8
1564v = get skArr add base i
1565if gt v m
1566m = v
1567c = add c 1
1568end
1569i = add i 1
1570end
1571ret c
1572end
1573if eq sk 27
1574if le n 1
1575ret 0
1576end
1577m1 = ulong 0
1578m2 = ulong 0
1579for i ulong n bound 8
1580v = get skArr add base i
1581if gt v m1
1582m2 = m1
1583m1 = v
1584else
1585if and gt v m2 lt v m1
1586m2 = v
1587end
1588end
1589end
1590ret m2
1591end
1592if eq sk 28
1593best = ulong 0
1594cur = ulong 0
1595for i ulong n bound 8
1596if ne get skArr add base i 0
1597cur = add cur 1
1598if gt cur best
1599best = cur
1600end
1601else
1602cur = 0
1603end
1604end
1605ret best
1606end
1607if eq sk 29
1608if eq n 0
1609ret 0
1610end
1611ret get skArr base
1612end
1613if eq sk 30
1614if eq n 0
1615ret 0
1616end
1617ret get skArr add base sub n 1
1618end
1619ret 0
1620end
1621
1622fn evalFoldC sk ulong ci ulong to ulong
1623n = get skArrN ci
1624base = mul ci 8
1625if eq sk 31
1626acc = ulong 0
1627for i ulong n bound 8
1628if eq and i 1 0
1629acc = add acc get skArr add base i
1630end
1631end
1632ret acc
1633end
1634if eq sk 32
1635acc = ulong 0
1636for i ulong n bound 8
1637if eq and i 1 0
1638acc = add acc get skArr add base i
1639else
1640acc = sub acc get skArr add base i
1641end
1642end
1643ret acc
1644end
1645if eq sk 33
1646if eq n 0
1647ret 0
1648end
1649v0 = get skArr base
1650c = ulong 0
1651for i ulong n bound 8
1652if eq get skArr add base i v0
1653c = add c 1
1654end
1655end
1656ret c
1657end
1658if eq sk 34
1659if eq n 0
1660ret 0
1661end
1662v0 = get skArr base
1663c = ulong 0
1664for i ulong n bound 8
1665if lt get skArr add base i v0
1666c = add c 1
1667end
1668end
1669ret c
1670end
1671if eq sk 35
1672c = ulong 0
1673for i ulong n bound 8
1674if eq get skArr add base i i
1675c = add c 1
1676end
1677end
1678ret c
1679end
1680if eq sk 36
1681c = ulong 0
1682for i ulong n bound 8
1683seen = ulong 0
1684for j ulong i bound 8
1685if eq get skArr add base j get skArr add base i
1686seen = 1
1687end
1688end
1689if eq seen 0
1690c = add c 1
1691end
1692end
1693ret c
1694end
1695if eq sk 37
1696for i ulong n bound 8
1697for j ulong i bound 8
1698if eq get skArr add base j get skArr add base i
1699ret 1
1700end
1701end
1702end
1703ret 0
1704end
1705if eq sk 38
1706r = n
1707for i ulong n bound 8
1708if ne and get skArr add base i 1 0
1709r = i
1710end
1711end
1712ret r
1713end
1714if eq sk 39
1715if eq n 0
1716ret 0
1717end
1718best = ulong 1
1719cur = ulong 1
1720i = ulong 1
1721while lt i n bound 8
1722if eq get skArr add base i get skArr add base sub i 1
1723cur = add cur 1
1724if gt cur best
1725best = cur
1726end
1727else
1728cur = 1
1729end
1730i = add i 1
1731end
1732ret best
1733end
1734if eq sk 40
1735g = ulong 0
1736for i ulong n bound 8
1737x = g
1738y = get skArr add base i
1739while ne y 0 bound 128
1740t = rem x y
1741x = y
1742y = t
1743end
1744g = x
1745end
1746ret g
1747end
1748if eq sk 41
1749se = ulong 0
1750so = ulong 0
1751for i ulong n bound 8
1752if eq and i 1 0
1753se = add se get skArr add base i
1754else
1755so = add so get skArr add base i
1756end
1757end
1758if lt se so
1759ret 0
1760end
1761ret sub se so
1762end
1763ret 0
1764end
1765
1766fn evalFold sk ulong ci ulong to ulong
1767if lt sk 15
1768ret evalFoldA sk ci
1769end
1770if lt sk 31
1771ret evalFoldB sk ci
1772end
1773ret evalFoldC sk ci
1774end
1775
1776fn emitFoldA sk ulong to void
1777if eq sk 0
1778oTxt f0Sum
1779end
1780if eq sk 1
1781oTxt f1Prod
1782end
1783if eq sk 2
1784oTxt f2Max
1785end
1786if eq sk 3
1787oTxt f3Min
1788end
1789if eq sk 4
1790oTxt f4Xor
1791end
1792if eq sk 5
1793oTxt f5Or
1794end
1795if eq sk 6
1796oTxt f6And
1797end
1798if eq sk 7
1799oTxt f7CntEvn
1800end
1801if eq sk 8
1802oTxt f8CntOdd
1803end
1804if eq sk 9
1805oTxt f9CntNz
1806end
1807if eq sk 10
1808oTxt f10CntZ
1809end
1810if eq sk 11
1811oTxt f11SumSq
1812end
1813if eq sk 12
1814oTxt f12SumEvn
1815end
1816if eq sk 13
1817oTxt f13SumOdd
1818end
1819if eq sk 14
1820oTxt f14Range
1821end
1822end
1823
1824fn emitFoldB sk ulong to void
1825if eq sk 15
1826oTxt f15Argmax
1827end
1828if eq sk 16
1829oTxt f16Argmin
1830end
1831if eq sk 17
1832oTxt f17Sorted
1833end
1834if eq sk 18
1835oTxt f18StrictInc
1836end
1837if eq sk 19
1838oTxt f19AllEq
1839end
1840if eq sk 20
1841oTxt f20FirstNz
1842end
1843if eq sk 21
1844oTxt f21LastNz
1845end
1846if eq sk 22
1847oTxt f22WtSum
1848end
1849if eq sk 23
1850oTxt f23SumAdjDiff
1851end
1852if eq sk 24
1853oTxt f24MaxAdjDiff
1854end
1855if eq sk 25
1856oTxt f25CntInc
1857end
1858if eq sk 26
1859oTxt f26PrefixMaxCnt
1860end
1861if eq sk 27
1862oTxt f27SecMax
1863end
1864if eq sk 28
1865oTxt f28LongNzRun
1866end
1867if eq sk 29
1868oTxt f29First
1869end
1870if eq sk 30
1871oTxt f30Last
1872end
1873end
1874
1875fn emitFoldC sk ulong to void
1876if eq sk 31
1877oTxt f31SumEvnIdx
1878end
1879if eq sk 32
1880oTxt f32AltSum
1881end
1882if eq sk 33
1883oTxt f33CntEqF
1884end
1885if eq sk 34
1886oTxt f34CntLtF
1887end
1888if eq sk 35
1889oTxt f35CntEqIdx
1890end
1891if eq sk 36
1892oTxt f36CntDist
1893end
1894if eq sk 37
1895oTxt f37HasDup
1896end
1897if eq sk 38
1898oTxt f38LastOddIdx
1899end
1900if eq sk 39
1901oTxt f39LongEqRun
1902end
1903if eq sk 40
1904oTxt f40ArrGcd
1905end
1906if eq sk 41
1907oTxt f41EvnSubOdd
1908end
1909end
1910
1911text pfPre "acc = ulong 0\nfor i ulong n bound 8\nv = get a i\nif "
1912text pfGt "gt v "
1913text pfLt "lt v "
1914text pfEq "eq v "
1915text pfGe "ge v "
1916text pfDiv1 "eq rem v "
1917text pfDiv2 " 0"
1918text pfInc "\nacc = add acc 1\nend\nend\nret acc\nend\n"
1919text pfAddV "\nacc = add acc v\nend\nend\nret acc\nend\n"
1920text pfIdx1 "for i ulong n bound 8\nv = get a i\nif "
1921text pfIdx2 "\nret i\nend\nend\nret n\nend\n"
1922text pfFstV "\nret v\nend\nend\nret 0\nend\n"
1923
1924fn evalFoldParam pid ulong kv ulong ci ulong to ulong
1925n = get skArrN ci
1926base = mul ci 8
1927if eq pid 0
1928acc = ulong 0
1929for i ulong n bound 8
1930if gt get skArr add base i kv
1931acc = add acc 1
1932end
1933end
1934ret acc
1935end
1936if eq pid 1
1937acc = ulong 0
1938for i ulong n bound 8
1939if lt get skArr add base i kv
1940acc = add acc 1
1941end
1942end
1943ret acc
1944end
1945if eq pid 2
1946acc = ulong 0
1947for i ulong n bound 8
1948if eq get skArr add base i kv
1949acc = add acc 1
1950end
1951end
1952ret acc
1953end
1954if eq pid 3
1955acc = ulong 0
1956for i ulong n bound 8
1957if ge get skArr add base i kv
1958acc = add acc 1
1959end
1960end
1961ret acc
1962end
1963if eq pid 4
1964if eq kv 0
1965ret 999999999
1966end
1967acc = ulong 0
1968for i ulong n bound 8
1969if eq rem get skArr add base i kv 0
1970acc = add acc 1
1971end
1972end
1973ret acc
1974end
1975if eq pid 5
1976acc = ulong 0
1977for i ulong n bound 8
1978v = get skArr add base i
1979if gt v kv
1980acc = add acc v
1981end
1982end
1983ret acc
1984end
1985if eq pid 6
1986for i ulong n bound 8
1987if eq get skArr add base i kv
1988ret i
1989end
1990end
1991ret n
1992end
1993if eq pid 7
1994for i ulong n bound 8
1995if gt get skArr add base i kv
1996ret i
1997end
1998end
1999ret n
2000end
2001if eq pid 8
2002for i ulong n bound 8
2003v = get skArr add base i
2004if gt v kv
2005ret v
2006end
2007end
2008ret 0
2009end
2010ret 0
2011end
2012
2013fn emitFoldParam pid ulong kv ulong to void
2014oClr
2015oTxt tFn4
2016if le pid 5
2017oTxt pfPre
2018if eq pid 0
2019oTxt pfGt
2020oNum kv
2021oTxt pfInc
2022ret
2023end
2024if eq pid 1
2025oTxt pfLt
2026oNum kv
2027oTxt pfInc
2028ret
2029end
2030if eq pid 2
2031oTxt pfEq
2032oNum kv
2033oTxt pfInc
2034ret
2035end
2036if eq pid 3
2037oTxt pfGe
2038oNum kv
2039oTxt pfInc
2040ret
2041end
2042if eq pid 4
2043oTxt pfDiv1
2044oNum kv
2045oTxt pfDiv2
2046oTxt pfInc
2047ret
2048end
2049oTxt pfGt
2050oNum kv
2051oTxt pfAddV
2052ret
2053end
2054oTxt pfIdx1
2055if eq pid 6
2056oTxt pfEq
2057oNum kv
2058oTxt pfIdx2
2059ret
2060end
2061if eq pid 7
2062oTxt pfGt
2063oNum kv
2064oTxt pfIdx2
2065ret
2066end
2067oTxt pfGt
2068oNum kv
2069oTxt pfFstV
2070end
2071
2072fn solveFold to ulong
2073nc = get skSt 13
2074for sk ulong 42 bound 42
2075ok = true
2076for ci ulong nc bound 8
2077if ne evalFold sk ci get skTgt ci
2078ok = false
2079break
2080end
2081end
2082if pickMatch ok
2083oClr
2084oTxt tFn4
2085if lt sk 15
2086emitFoldA sk
2087else
2088if lt sk 31
2089emitFoldB sk
2090else
2091emitFoldC sk
2092end
2093end
2094ret sk
2095end
2096end
2097ncon = get skSt 2
2098for ci2 ulong ncon bound 32
2099kv = get skConsts ci2
2100for pid ulong 9 bound 9
2101ok2 = true
2102for c ulong nc bound 8
2103if ne evalFoldParam pid kv c get skTgt c
2104ok2 = false
2105break
2106end
2107end
2108if pickMatch ok2
2109emitFoldParam pid kv
2110ret add 100 pid
2111end
2112end
2113end
2114ret 999
2115end
2116
2117note ======== In-place array rewrite evaluators (id 0..19) ========
2118fn runRewA sk ulong n ulong to void
2119if eq sk 0
2120i = ulong 0
2121while lt mul i 2 n bound 8
2122j = sub sub n 1 i
2123t = get skTmpArr i
2124put skTmpArr i get skTmpArr j
2125put skTmpArr j t
2126i = add i 1
2127end
2128ret
2129end
2130if eq sk 1
2131for i ulong n bound 8
2132for j ulong n bound 8
2133if and gt j i lt get skTmpArr j get skTmpArr i
2134t = get skTmpArr i
2135put skTmpArr i get skTmpArr j
2136put skTmpArr j t
2137end
2138end
2139end
2140ret
2141end
2142if eq sk 2
2143for i ulong n bound 8
2144for j ulong n bound 8
2145if and gt j i gt get skTmpArr j get skTmpArr i
2146t = get skTmpArr i
2147put skTmpArr i get skTmpArr j
2148put skTmpArr j t
2149end
2150end
2151end
2152ret
2153end
2154if eq sk 3
2155s = ulong 0
2156for i ulong n bound 8
2157s = add s get skTmpArr i
2158put skTmpArr i s
2159end
2160ret
2161end
2162if eq sk 4
2163m = ulong 0
2164for i ulong n bound 8
2165v = get skTmpArr i
2166if gt v m
2167m = v
2168end
2169put skTmpArr i m
2170end
2171ret
2172end
2173if eq sk 5
2174for i ulong n bound 8
2175put skTmpArr i mul get skTmpArr i 2
2176end
2177ret
2178end
2179if eq sk 6
2180for i ulong n bound 8
2181put skTmpArr i div get skTmpArr i 2
2182end
2183ret
2184end
2185if eq sk 7
2186for i ulong n bound 8
2187v = get skTmpArr i
2188put skTmpArr i mul v v
2189end
2190ret
2191end
2192if eq sk 8
2193for i ulong n bound 8
2194if eq and get skTmpArr i 1 0
2195put skTmpArr i 0
2196end
2197end
2198ret
2199end
2200if eq sk 9
2201for i ulong n bound 8
2202if ne and get skTmpArr i 1 0
2203put skTmpArr i 0
2204end
2205end
2206ret
2207end
2208end
2209
2210fn runRewB sk ulong n ulong to void
2211if eq sk 10
2212if gt n 1
2213f = get skTmpArr 0
2214i = ulong 1
2215while lt i n bound 8
2216put skTmpArr sub i 1 get skTmpArr i
2217i = add i 1
2218end
2219put skTmpArr sub n 1 f
2220end
2221ret
2222end
2223if eq sk 11
2224if gt n 1
2225l = get skTmpArr sub n 1
2226i = sub n 1
2227while gt i 0 bound 8
2228put skTmpArr i get skTmpArr sub i 1
2229i = sub i 1
2230end
2231put skTmpArr 0 l
2232end
2233ret
2234end
2235if eq sk 12
2236if gt n 0
2237i = ulong 1
2238while lt i n bound 8
2239put skTmpArr sub i 1 get skTmpArr i
2240i = add i 1
2241end
2242put skTmpArr sub n 1 0
2243end
2244ret
2245end
2246if eq sk 13
2247if gt n 0
2248i = sub n 1
2249while gt i 0 bound 8
2250put skTmpArr i get skTmpArr sub i 1
2251i = sub i 1
2252end
2253put skTmpArr 0 0
2254end
2255ret
2256end
2257if eq sk 14
2258i = ulong 0
2259while lt add i 1 n bound 8
2260t = get skTmpArr i
2261put skTmpArr i get skTmpArr add i 1
2262put skTmpArr add i 1 t
2263i = add i 2
2264end
2265ret
2266end
2267if eq sk 15
2268w = ulong 0
2269for i ulong n bound 8
2270v = get skTmpArr i
2271if ne v 0
2272put skTmpArr w v
2273w = add w 1
2274end
2275end
2276while lt w n bound 8
2277put skTmpArr w 0
2278w = add w 1
2279end
2280ret
2281end
2282if eq sk 16
2283for i ulong n bound 8
2284put skTmpArr i add get skTmpArr i i
2285end
2286ret
2287end
2288if eq sk 17
2289for i ulong n bound 8
2290put skTmpArr i 0
2291end
2292ret
2293end
2294if eq sk 18
2295x = ulong 0
2296for i ulong n bound 8
2297x = xor x get skTmpArr i
2298put skTmpArr i x
2299end
2300ret
2301end
2302if eq sk 19
2303for i ulong n bound 8
2304put skTmpArr i add get skTmpArr i 1
2305end
2306ret
2307end
2308end
2309
2310fn emitRewA sk ulong to void
2311if eq sk 0
2312oTxt r0Rev
2313end
2314if eq sk 1
2315oTxt r1SortAsc
2316end
2317if eq sk 2
2318oTxt r2SortDesc
2319end
2320if eq sk 3
2321oTxt r3PrefSum
2322end
2323if eq sk 4
2324oTxt r4PrefMax
2325end
2326if eq sk 5
2327oTxt r5Double
2328end
2329if eq sk 6
2330oTxt r6Halve
2331end
2332if eq sk 7
2333oTxt r7Square
2334end
2335if eq sk 8
2336oTxt r8ZeroEvn
2337end
2338if eq sk 9
2339oTxt r9ZeroOdd
2340end
2341end
2342
2343fn emitRewB sk ulong to void
2344if eq sk 10
2345oTxt r10RotL
2346end
2347if eq sk 11
2348oTxt r11RotR
2349end
2350if eq sk 12
2351oTxt r12ShiftL
2352end
2353if eq sk 13
2354oTxt r13ShiftR
2355end
2356if eq sk 14
2357oTxt r14SwapAdj
2358end
2359if eq sk 15
2360oTxt r15MoveZ
2361end
2362if eq sk 16
2363oTxt r16AddIdx
2364end
2365if eq sk 17
2366oTxt r17Zero
2367end
2368if eq sk 18
2369oTxt r18PrefXor
2370end
2371if eq sk 19
2372oTxt r19Inc1
2373end
2374end
2375
2376fn checkRewCase sk ulong ci ulong to bool
2377n = get skArrN ci
2378base = mul ci 8
2379for k ulong 8 bound 8
2380put skTmpArr k get skArr add base k
2381end
2382if lt sk 10
2383runRewA sk n
2384else
2385runRewB sk n
2386end
2387for k ulong 8 bound 8
2388if ne get skTmpArr k get skOutArr add base k
2389ret false
2390end
2391end
2392ret true
2393end
2394
2395text prLoop "for i ulong n bound 8\nv = get a i\n"
2396text prEnd "\nend\nend\n"
2397text prAdd "put a i add v "
2398text prSub "if le v "
2399text prSub2 "\nput a i 0\nelse\nput a i sub v "
2400text prSub3 "\nend"
2401text prMul "put a i mul v "
2402text prDiv "put a i div v "
2403text prRem "put a i rem v "
2404text prCapH "if gt v "
2405text prCapH2 "\nput a i "
2406text prCapL "if lt v "
2407text prZLo "\nput a i 0\nend"
2408text prRepZ "if eq v 0\nput a i "
2409text prFill "put a i "
2410text prXor "put a i xor v "
2411text prAnd "put a i and v "
2412text prOr "put a i or v "
2413
2414fn runRewParam pid ulong kv ulong n ulong to bool
2415if and or eq pid 3 eq pid 4 eq kv 0
2416ret false
2417end
2418for i ulong n bound 8
2419v = get skTmpArr i
2420if eq pid 0
2421put skTmpArr i add v kv
2422end
2423if eq pid 1
2424if le v kv
2425put skTmpArr i 0
2426else
2427put skTmpArr i sub v kv
2428end
2429end
2430if eq pid 2
2431put skTmpArr i mul v kv
2432end
2433if eq pid 3
2434put skTmpArr i div v kv
2435end
2436if eq pid 4
2437put skTmpArr i rem v kv
2438end
2439if eq pid 5
2440if gt v kv
2441put skTmpArr i kv
2442end
2443end
2444if eq pid 6
2445if lt v kv
2446put skTmpArr i kv
2447end
2448end
2449if eq pid 7
2450if lt v kv
2451put skTmpArr i 0
2452end
2453end
2454if eq pid 8
2455if gt v kv
2456put skTmpArr i 0
2457end
2458end
2459if eq pid 9
2460if eq v 0
2461put skTmpArr i kv
2462end
2463end
2464if eq pid 10
2465put skTmpArr i kv
2466end
2467if eq pid 11
2468put skTmpArr i xor v kv
2469end
2470if eq pid 12
2471put skTmpArr i and v kv
2472end
2473if eq pid 13
2474put skTmpArr i or v kv
2475end
2476end
2477ret true
2478end
2479
2480fn emitRewParam pid ulong kv ulong to void
2481oClr
2482oTxt tFn5
2483oTxt prLoop
2484if eq pid 0
2485oTxt prAdd
2486oNum kv
2487end
2488if eq pid 1
2489oTxt prSub
2490oNum kv
2491oTxt prSub2
2492oNum kv
2493oTxt prSub3
2494end
2495if eq pid 2
2496oTxt prMul
2497oNum kv
2498end
2499if eq pid 3
2500oTxt prDiv
2501oNum kv
2502end
2503if eq pid 4
2504oTxt prRem
2505oNum kv
2506end
2507if eq pid 5
2508oTxt prCapH
2509oNum kv
2510oTxt prCapH2
2511oNum kv
2512oTxt prSub3
2513end
2514if eq pid 6
2515oTxt prCapL
2516oNum kv
2517oTxt prCapH2
2518oNum kv
2519oTxt prSub3
2520end
2521if eq pid 7
2522oTxt prCapL
2523oNum kv
2524oTxt prZLo
2525end
2526if eq pid 8
2527oTxt prCapH
2528oNum kv
2529oTxt prZLo
2530end
2531if eq pid 9
2532oTxt prRepZ
2533oNum kv
2534oTxt prSub3
2535end
2536if eq pid 10
2537oTxt prFill
2538oNum kv
2539end
2540if eq pid 11
2541oTxt prXor
2542oNum kv
2543end
2544if eq pid 12
2545oTxt prAnd
2546oNum kv
2547end
2548if eq pid 13
2549oTxt prOr
2550oNum kv
2551end
2552oTxt prEnd
2553end
2554
2555fn checkRewParamCase pid ulong kv ulong ci ulong to bool
2556n = get skArrN ci
2557base = mul ci 8
2558for k ulong 8 bound 8
2559put skTmpArr k get skArr add base k
2560end
2561if not runRewParam pid kv n
2562ret false
2563end
2564for k ulong 8 bound 8
2565if ne get skTmpArr k get skOutArr add base k
2566ret false
2567end
2568end
2569ret true
2570end
2571
2572note Two development families: add a multiple of the original next or first item.
2573text scaledNext "for i ulong n bound 8\nj = add i 1\nif lt j n\nput a i add get a i mul get a j "
2574text scaledFirst "if gt n 0\nanchor = get a 0\nfor i ulong n bound 8\nput a i add get a i mul anchor "
2575text scaledEnd "\nend\nend\nend\n"
2576
2577fn scaleFactor family ulong ci ulong i ulong to ulong
2578base = mul ci 8
2579if eq family 1
2580ret get skArr base
2581end
2582j = add i 1
2583if ge j get skArrN ci
2584ret 0
2585end
2586ret get skArr add base j
2587end
2588
2589fn scaleFits family ulong k ulong to bool
2590for ci ulong get skSt 14 bound 8
2591n = get skArrN ci
2592for i ulong 8
2593j = add mul ci 8 i
2594v = get skArr j
2595if lt i n
2596v = add v mul k scaleFactor family ci i
2597end
2598if ne v get skOutArr j
2599ret false
2600end
2601end
2602end
2603ret true
2604end
2605
2606fn scaleMatch family ulong to bool
2607note Try the spec constants first, then integer ratios from informative input/output pairs.
2608note Verification always uses wrapping ulong arithmetic and compares the entire array.
2609for c ulong get skSt 2 bound 32
2610k = get skConsts c
2611if scaleFits family k
2612put scaleResult 0 k
2613ret true
2614end
2615end
2616for ci ulong get skSt 14 bound 8
2617for i ulong get skArrN ci bound 8
2618factor = scaleFactor family ci i
2619if ne factor 0
2620j = add mul ci 8 i
2621delta = sub get skOutArr j get skArr j
2622if eq rem delta factor 0
2623k = div delta factor
2624if scaleFits family k
2625put scaleResult 0 k
2626ret true
2627end
2628end
2629end
2630end
2631end
2632ret false
2633end
2634
2635fn scaledRewrite to ulong
2636for family ulong 2
2637if ne and get rewriteMask 0 shl ulong 1 family 0
2638if pickMatch scaleMatch family
2639oClr
2640oTxt tFn5
2641if eq family 0
2642oTxt scaledNext
2643else
2644oTxt scaledFirst
2645end
2646oNum get scaleResult 0
2647oTxt scaledEnd
2648ret add 200 family
2649end
2650end
2651end
2652ret 999
2653end
2654
2655fn solveRewrite to ulong
2656nc = get skSt 14
2657for sk ulong 20 bound 20
2658ok = true
2659for ci ulong nc bound 8
2660if not checkRewCase sk ci
2661ok = false
2662break
2663end
2664end
2665if pickMatch ok
2666oClr
2667oTxt tFn5
2668if lt sk 10
2669emitRewA sk
2670else
2671emitRewB sk
2672end
2673ret sk
2674end
2675end
2676ncon = get skSt 2
2677for ci2 ulong ncon bound 32
2678kv = get skConsts ci2
2679for pid ulong 14 bound 14
2680ok2 = true
2681for c ulong nc bound 8
2682if not checkRewParamCase pid kv c
2683ok2 = false
2684break
2685end
2686end
2687if pickMatch ok2
2688emitRewParam pid kv
2689ret add 100 pid
2690end
2691end
2692end
2693ret scaledRewrite
2694end
2695
2696note ======== Spec file parser ========
2697fn spStarts o ulong e ulong t view uchar * to bool
2698tl = len t
2699if gt tl sub e o
2700ret false
2701end
2702for i ulong tl bound 32
2703if ne get skInBuf add o i get t i
2704ret false
2705end
2706end
2707ret true
2708end
2709
2710fn spNextU pos ulong e ulong to ulong
2711p = pos
2712while lt p e bound 4096
2713c = get skInBuf p
2714if and ne c 32 and ne c 9 ne c 13
2715break
2716end
2717p = add p 1
2718end
2719v = ulong 0
2720got = ulong 0
2721while lt p e bound 4096
2722c = get skInBuf p
2723if not and ge c 48 le c 57
2724break
2725end
2726v = add mul v 10 ulong sub c 48
2727got = 1
2728p = add p 1
2729end
2730put skSt 20 v
2731put skSt 21 got
2732ret p
2733end
2734
2735fn skParseLine ls ulong lend ulong to void
2736if spStarts ls lend kwRewrites
2737p = spNextU add ls len kwRewrites lend
2738put rewriteMask 0 get skSt 20
2739ret
2740end
2741if spStarts ls lend kwSkip
2742p = spNextU add ls len kwSkip lend
2743put skipMatches 0 get skSt 20
2744ret
2745end
2746if spStarts ls lend kwArity
2747p = spNextU add ls 6 lend
2748if eq get skSt 21 1
2749put skSt 1 get skSt 20
2750end
2751ret
2752end
2753if spStarts ls lend kwConsts
2754p = add ls 7
2755cc = ulong 0
2756while lt p lend bound 32
2757p = spNextU p lend
2758if eq get skSt 21 0
2759break
2760end
2761if lt cc 32
2762put skConsts cc get skSt 20
2763cc = add cc 1
2764end
2765end
2766put skSt 2 cc
2767ret
2768end
2769if spStarts ls lend kwCase
2770nc = get skSt 0
2771if lt nc 8
2772ar = get skSt 1
2773p = spNextU add ls 5 lend
2774put skInA nc get skSt 20
2775if ge ar 2
2776p = spNextU p lend
2777put skInB nc get skSt 20
2778end
2779if ge ar 3
2780p = spNextU p lend
2781put skInC nc get skSt 20
2782end
2783p = spNextU p lend
2784put skTgt nc get skSt 20
2785put skSt 0 add nc 1
2786end
2787ret
2788end
2789if spStarts ls lend kwFold
2790fc = get skSt 13
2791if lt fc 8
2792p = spNextU add ls 5 lend
2793vn = get skSt 20
2794put skArrN fc vn
2795base = mul fc 8
2796for k ulong 8 bound 8
2797put skArr add base k 0
2798end
2799for k ulong vn bound 8
2800p = spNextU p lend
2801put skArr add base k get skSt 20
2802end
2803p = spNextU p lend
2804put skTgt fc get skSt 20
2805put skSt 13 add fc 1
2806end
2807ret
2808end
2809if spStarts ls lend kwRew
2810rc = get skSt 14
2811if lt rc 8
2812p = spNextU add ls 4 lend
2813vn = get skSt 20
2814put skArrN rc vn
2815base = mul rc 8
2816for k ulong 8 bound 8
2817p = spNextU p lend
2818put skArr add base k get skSt 20
2819end
2820for k ulong 8 bound 8
2821p = spNextU p lend
2822put skOutArr add base k get skSt 20
2823end
2824put skSt 14 add rc 1
2825end
2826end
2827end
2828
2829fn skParseSpec blen ulong to bool
2830put rewriteMask 0 3
2831put skipMatches 0 0
2832put skSt 0 0
2833put skSt 1 1
2834put skSt 2 0
2835put skSt 13 0
2836put skSt 14 0
2837pos = ulong 0
2838while lt pos blen bound 65536
2839ls = pos
2840while lt pos blen bound 65536
2841if eq get skInBuf pos 10
2842break
2843end
2844pos = add pos 1
2845end
2846lend = pos
2847if lt pos blen
2848pos = add pos 1
2849end
2850skParseLine ls lend
2851end
2852if gt get skSt 14 0
2853put skSt 1 5
2854ret true
2855end
2856if gt get skSt 13 0
2857put skSt 1 4
2858ret true
2859end
2860ret gt get skSt 0 0
2861end
2862
2863fn skSaveOut argi ulong to bool
2864pl = argread argi skPath 0 4095
2865if le pl 0
2866ret false
2867end
2868fd = fopen skPath 0 ulong pl 577 420
2869if lt fd 0
2870ret false
2871end
2872n = get skSt 9
2873o = ulong 0
2874while lt o n bound 65536
2875k = write int fd skOut o sub n o
2876if le k 0
2877break
2878end
2879o = add o ulong k
2880end
2881z = fclose int fd
2882ret true
2883end
2884
2885fn runSelfcheck to int
2886note 1. Popcount
2887put skSt 0 4
2888put skInA 0 0
2889put skTgt 0 0
2890put skInA 1 7
2891put skTgt 1 3
2892put skInA 2 15
2893put skTgt 2 4
2894put skInA 3 255
2895put skTgt 3 8
2896if ne solveScalar 0
2897wStr 2 tSelfFail
2898wNum 2 1
2899wStr 2 tNl
2900ret 1
2901end
2902note 2. Digit sum
2903put skInA 0 0
2904put skTgt 0 0
2905put skInA 1 123
2906put skTgt 1 6
2907put skInA 2 999
2908put skTgt 2 27
2909put skInA 3 405
2910put skTgt 3 9
2911if ne solveScalar 7
2912wStr 2 tSelfFail
2913wNum 2 2
2914wStr 2 tNl
2915ret 1
2916end
2917note 3. GCD
2918put skInA 0 48
2919put skInB 0 18
2920put skTgt 0 6
2921put skInA 1 25
2922put skInB 1 6
2923put skTgt 1 1
2924put skInA 2 64
2925put skInB 2 64
2926put skTgt 2 64
2927put skInA 3 0
2928put skInB 3 9
2929put skTgt 3 9
2930if ne solveBinary 0
2931wStr 2 tSelfFail
2932wNum 2 3
2933wStr 2 tNl
2934ret 1
2935end
2936note 4. Clamp
2937put skInA 0 5
2938put skInB 0 10
2939put skInC 0 20
2940put skTgt 0 10
2941put skInA 1 15
2942put skInB 1 10
2943put skInC 1 20
2944put skTgt 1 15
2945put skInA 2 25
2946put skInB 2 10
2947put skInC 2 20
2948put skTgt 2 20
2949put skInA 3 10
2950put skInB 3 10
2951put skInC 3 20
2952put skTgt 3 10
2953if ne solveTernary 0
2954wStr 2 tSelfFail
2955wNum 2 4
2956wStr 2 tNl
2957ret 1
2958end
2959note 5. Fold argmax
2960put skSt 13 2
2961put skArrN 0 4
2962put skArr 0 3
2963put skArr 1 9
2964put skArr 2 2
2965put skArr 3 7
2966put skTgt 0 1
2967put skArrN 1 3
2968put skArr 8 5
2969put skArr 9 1
2970put skArr 10 8
2971put skTgt 1 2
2972if ne solveFold 15
2973wStr 2 tSelfFail
2974wNum 2 5
2975wStr 2 tNl
2976ret 1
2977end
2978note 6. Rewrite reverse
2979put skSt 14 1
2980put skArrN 0 4
2981put skArr 0 1
2982put skArr 1 2
2983put skArr 2 3
2984put skArr 3 4
2985put skArr 4 9
2986put skArr 5 9
2987put skArr 6 9
2988put skArr 7 9
2989put skOutArr 0 4
2990put skOutArr 1 3
2991put skOutArr 2 2
2992put skOutArr 3 1
2993put skOutArr 4 9
2994put skOutArr 5 9
2995put skOutArr 6 9
2996put skOutArr 7 9
2997if ne solveRewrite 0
2998wStr 2 tSelfFail
2999wNum 2 6
3000wStr 2 tNl
3001ret 1
3002end
3003wStr 1 tSelfOk
3004ret 0
3005end
3006
3007fn main to int
3008if lt argcnt 2
3009wStr 2 tUsage
3010ret 2
3011end
3012mb = new uchar 64 0
3013ml = argread 1 mb 0 64
3014if le ml 0
3015wStr 2 tUsage
3016ret 2
3017end
3018if eqTxt mb ulong ml mSelf
3019ret runSelfcheck
3020end
3021if not eqTxt mb ulong ml mSolve
3022wStr 2 tUsage
3023ret 2
3024end
3025if lt argcnt 4
3026wStr 2 tUsage
3027ret 2
3028end
3029pl = argread 2 skPath 0 4095
3030if le pl 0
3031wStr 2 tErrIo
3032ret 1
3033end
3034fd = fopen skPath 0 ulong pl 0 0
3035if lt fd 0
3036wStr 2 tErrIo
3037ret 1
3038end
3039kn = read int fd skInBuf 0 65535
3040z = fclose int fd
3041if le kn 0
3042wStr 2 tErrIo
3043ret 1
3044end
3045if not skParseSpec ulong kn
3046wStr 2 tErrIo
3047ret 1
3048end
3049ar = get skSt 1
3050id = ulong 999
3051if eq ar 1
3052id = solveScalar
3053end
3054if eq ar 2
3055id = solveBinary
3056end
3057if eq ar 3
3058id = solveTernary
3059end
3060if eq ar 4
3061id = solveFold
3062end
3063if eq ar 5
3064id = solveRewrite
3065end
3066if eq id 999
3067wStr 1 tFail
3068ret 1
3069end
3070if not skSaveOut 3
3071wStr 2 tErrIo
3072ret 1
3073end
3074wStr 1 tOk
3075if eq ar 1
3076wStr 1 fScalar
3077end
3078if eq ar 2
3079wStr 1 fBin
3080end
3081if eq ar 3
3082wStr 1 fTern
3083end
3084if eq ar 4
3085wStr 1 fFold
3086end
3087if eq ar 5
3088wStr 1 fRew
3089end
3090wStr 1 tId
3091wNum 1 id
3092wStr 1 tNl
3093ret 0
3094end