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