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 / rlvr_breadth.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 |
| 3 | note seed/helix/ollm_factory/rlvr_breadth.om: |
| 4 | note Pure seeded OM-2 Mechanical RLVR Breadth Factory covering all 10 coding skill |
| 5 | note families (arith, branch, bits, digits, loop, fold, select, count, scan, rewrite) |
| 6 | note with multi-phrasing natural-language prompts, NUL (\0) EOS delimiters, and a |
| 7 | note self-checking `oracle` mode compiled identically across Strand A and Strand B. |
| 8 | note |
| 9 | note Modes (argv[1]): |
| 10 | note corpus [variant 1..9] -> NUL-delimited verified OM-2 training records |
| 11 | note oracle [variant 1..9] -> self-checking om2 program executing 100% of records |
| 12 | |
| 13 | global rbMode ulong 1 |
| 14 | global rbVar ulong 1 |
| 15 | global rbId ulong 1 |
| 16 | |
| 17 | text rbOm2 "om2\n" |
| 18 | text rbNote "note " |
| 19 | text rbFnU1 "\nfn solve a ulong to ulong\n" |
| 20 | text rbFnU2 "\nfn solve a ulong b ulong to ulong\n" |
| 21 | text rbFnU3 "\nfn solve a ulong b ulong c ulong to ulong\n" |
| 22 | text rbFnAr "\nfn solve a view ulong 8 n ulong to ulong\n" |
| 23 | text rbFnAw "\nfn solve a edit ulong 8 n ulong to void\n" |
| 24 | text rbEnd "\nend\n" |
| 25 | text rbOrPfx "fn g_" |
| 26 | text rbOrU1 " a ulong to ulong\n" |
| 27 | text rbOrU2 " a ulong b ulong to ulong\n" |
| 28 | text rbOrU3 " a ulong b ulong c ulong to ulong\n" |
| 29 | text rbOrAr " a view ulong 8 n ulong to ulong\n" |
| 30 | text rbOrAw " a edit ulong 8 n ulong to void\n" |
| 31 | text rbMainH "fn main to int\narr = new ulong 8 0\nput arr 0 3\nput arr 1 11\nput arr 2 0\nput arr 3 7\nput arr 4 19\nput arr 5 4\nput arr 6 2\nput arr 7 15\n" |
| 32 | text rbMainT "ret 0\nend\n" |
| 33 | text rbReset "put arr 0 3\nput arr 1 11\nput arr 2 0\nput arr 3 7\nput arr 4 19\nput arr 5 4\nput arr 6 2\nput arr 7 15\n" |
| 34 | text rbChkA "if ne g_" |
| 35 | text rbChkB " " |
| 36 | text rbChkC "\nexit 1\nend\n" |
| 37 | text rbCallG "g_" |
| 38 | text rbAwChk " arr 8\nif ne add get arr 0 add get arr 3 get arr 7 " |
| 39 | text rbSpc " " |
| 40 | text rbNl "\n" |
| 41 | text rbOrArrArg "arr 8" |
| 42 | |
| 43 | text rbV0 "return " |
| 44 | text rbV1 "compute " |
| 45 | text rbV2 "find " |
| 46 | text rbV3 "calculate " |
| 47 | text rbV4 "output " |
| 48 | text rbV5 "give " |
| 49 | |
| 50 | text rbArgOracle "oracle" |
| 51 | |
| 52 | fn rbWrt s view uchar * to void |
| 53 | r = write 1 s 0 len s |
| 54 | end |
| 55 | |
| 56 | fn rbNul to void |
| 57 | b = new uchar 1 0 |
| 58 | r = write 1 b 0 1 |
| 59 | end |
| 60 | |
| 61 | fn rbNum v ulong to void |
| 62 | b = new uchar 24 0 |
| 63 | p = ulong 24 |
| 64 | x = v |
| 65 | while true bound 24 |
| 66 | p = sub p 1 |
| 67 | put b p uchar add 48 rem x 10 |
| 68 | x = div x 10 |
| 69 | if eq x 0 |
| 70 | break |
| 71 | end |
| 72 | end |
| 73 | r = write 1 b p sub 24 p |
| 74 | end |
| 75 | |
| 76 | fn rbVerb k ulong to void |
| 77 | m = rem k 6 |
| 78 | if eq m 0 |
| 79 | rbWrt rbV0 |
| 80 | ret |
| 81 | end |
| 82 | if eq m 1 |
| 83 | rbWrt rbV1 |
| 84 | ret |
| 85 | end |
| 86 | if eq m 2 |
| 87 | rbWrt rbV2 |
| 88 | ret |
| 89 | end |
| 90 | if eq m 3 |
| 91 | rbWrt rbV3 |
| 92 | ret |
| 93 | end |
| 94 | if eq m 4 |
| 95 | rbWrt rbV4 |
| 96 | ret |
| 97 | end |
| 98 | rbWrt rbV5 |
| 99 | end |
| 100 | |
| 101 | fn rbNextId to ulong |
| 102 | id = add get rbId 0 1 |
| 103 | put rbId 0 id |
| 104 | ret id |
| 105 | end |
| 106 | |
| 107 | fn rbBeginFn shape ulong desc view uchar * to ulong |
| 108 | id = rbNextId |
| 109 | if eq get rbMode 0 0 |
| 110 | rbWrt rbNote |
| 111 | rbVerb add id get rbVar 0 |
| 112 | rbWrt desc |
| 113 | if eq shape 1 |
| 114 | rbWrt rbFnU1 |
| 115 | end |
| 116 | if eq shape 2 |
| 117 | rbWrt rbFnU2 |
| 118 | end |
| 119 | if eq shape 3 |
| 120 | rbWrt rbFnU3 |
| 121 | end |
| 122 | if eq shape 4 |
| 123 | rbWrt rbFnAr |
| 124 | end |
| 125 | if eq shape 5 |
| 126 | rbWrt rbFnAw |
| 127 | end |
| 128 | else |
| 129 | rbWrt rbOrPfx |
| 130 | rbNum id |
| 131 | if eq shape 1 |
| 132 | rbWrt rbOrU1 |
| 133 | end |
| 134 | if eq shape 2 |
| 135 | rbWrt rbOrU2 |
| 136 | end |
| 137 | if eq shape 3 |
| 138 | rbWrt rbOrU3 |
| 139 | end |
| 140 | if eq shape 4 |
| 141 | rbWrt rbOrAr |
| 142 | end |
| 143 | if eq shape 5 |
| 144 | rbWrt rbOrAw |
| 145 | end |
| 146 | end |
| 147 | ret id |
| 148 | end |
| 149 | |
| 150 | fn rbEndFn to void |
| 151 | rbWrt rbEnd |
| 152 | if eq get rbMode 0 0 |
| 153 | rbNul |
| 154 | else |
| 155 | rbWrt rbNl |
| 156 | end |
| 157 | end |
| 158 | |
| 159 | fn rbChkU1 id ulong x ulong exp ulong to void |
| 160 | rbWrt rbChkA |
| 161 | rbNum id |
| 162 | rbWrt rbChkB |
| 163 | rbNum x |
| 164 | rbWrt rbChkB |
| 165 | rbNum exp |
| 166 | rbWrt rbChkC |
| 167 | end |
| 168 | |
| 169 | fn rbChkU2 id ulong x ulong y ulong exp ulong to void |
| 170 | rbWrt rbChkA |
| 171 | rbNum id |
| 172 | rbWrt rbChkB |
| 173 | rbNum x |
| 174 | rbWrt rbChkB |
| 175 | rbNum y |
| 176 | rbWrt rbChkB |
| 177 | rbNum exp |
| 178 | rbWrt rbChkC |
| 179 | end |
| 180 | |
| 181 | fn rbChkU3 id ulong x ulong y ulong z ulong exp ulong to void |
| 182 | rbWrt rbChkA |
| 183 | rbNum id |
| 184 | rbWrt rbChkB |
| 185 | rbNum x |
| 186 | rbWrt rbChkB |
| 187 | rbNum y |
| 188 | rbWrt rbChkB |
| 189 | rbNum z |
| 190 | rbWrt rbChkB |
| 191 | rbNum exp |
| 192 | rbWrt rbChkC |
| 193 | end |
| 194 | |
| 195 | fn rbChkAr id ulong exp ulong to void |
| 196 | rbWrt rbChkA |
| 197 | rbNum id |
| 198 | rbWrt rbSpc |
| 199 | rbWrt rbOrArrArg |
| 200 | rbWrt rbSpc |
| 201 | rbNum exp |
| 202 | rbWrt rbChkC |
| 203 | end |
| 204 | |
| 205 | fn rbChkAw id ulong exp ulong to void |
| 206 | rbWrt rbReset |
| 207 | rbWrt rbCallG |
| 208 | rbNum id |
| 209 | rbWrt rbAwChk |
| 210 | rbNum exp |
| 211 | rbWrt rbChkC |
| 212 | end |
| 213 | |
| 214 | note ==================== 1. BRANCH & ARITH ==================== |
| 215 | text dSatSubA "a minus " |
| 216 | text dSatSubB " clamped at zero" |
| 217 | text bSatSub0 "if lt a " |
| 218 | text bSatSub1 "\nret 0\nend\nret sub a " |
| 219 | |
| 220 | text dStepA "value " |
| 221 | text dStepB " if a is less than " |
| 222 | text dStepC " else " |
| 223 | text bStep0 "if lt a " |
| 224 | text bStep1 "\nret " |
| 225 | text bStep2 "\nend\nret " |
| 226 | |
| 227 | text dRndUpA "a rounded up to a multiple of " |
| 228 | text bRndUp0 "r = rem a " |
| 229 | text bRndUp1 "\nif eq r 0\nret a\nend\nret add a sub " |
| 230 | text bRndUp2 " r" |
| 231 | |
| 232 | text dAbsDiff "absolute difference between unsigned a and b" |
| 233 | text bAbsDiff "if ge a b\nret sub a b\nend\nret sub b a" |
| 234 | |
| 235 | text dSatSub2 "saturating difference a minus b or zero when a is smaller" |
| 236 | text bSatSub2 "if lt a b\nret 0\nend\nret sub a b" |
| 237 | |
| 238 | text dMin2 "the smaller of a and b" |
| 239 | text bMin2 "if lt a b\nret a\nend\nret b" |
| 240 | |
| 241 | text dMax2 "the larger of a and b" |
| 242 | text bMax2 "if gt a b\nret a\nend\nret b" |
| 243 | |
| 244 | text dMed3 "the median of three unsigned numbers a b c" |
| 245 | text bMed3 "if lt a b\nif lt b c\nret b\nend\nif lt a c\nret c\nend\nret a\nend\nif lt a c\nret a\nend\nif lt b c\nret c\nend\nret b" |
| 246 | |
| 247 | text dMin3 "the smallest of a b and c" |
| 248 | text bMin3 "m = a\nif lt b m\nm = b\nend\nif lt c m\nm = c\nend\nret m" |
| 249 | |
| 250 | text dMax3 "the largest of a b and c" |
| 251 | text bMax3 "m = a\nif gt b m\nm = b\nend\nif gt c m\nm = c\nend\nret m" |
| 252 | |
| 253 | text dInRng3 "1 if a is between b and c inclusive else 0" |
| 254 | text bInRng3 "if lt a b\nret 0\nend\nif gt a c\nret 0\nend\nret 1" |
| 255 | |
| 256 | text dClp3 "a clamped between b and c" |
| 257 | text bClp3 "if lt a b\nret b\nend\nif gt a c\nret c\nend\nret a" |
| 258 | |
| 259 | fn rbEmitBranch isMain bool to void |
| 260 | v = get rbVar 0 |
| 261 | note 1a. 10 saturating sub a - k |
| 262 | for i ulong 10 |
| 263 | k = add 5 mul add i v 7 |
| 264 | id = rbNextId |
| 265 | if not isMain |
| 266 | if eq get rbMode 0 0 |
| 267 | rbWrt rbNote |
| 268 | rbVerb add id v |
| 269 | rbWrt dSatSubA |
| 270 | rbNum k |
| 271 | rbWrt dSatSubB |
| 272 | rbWrt rbFnU1 |
| 273 | else |
| 274 | rbWrt rbOrPfx |
| 275 | rbNum id |
| 276 | rbWrt rbOrU1 |
| 277 | end |
| 278 | rbWrt bSatSub0 |
| 279 | rbNum k |
| 280 | rbWrt bSatSub1 |
| 281 | rbNum k |
| 282 | rbEndFn |
| 283 | else |
| 284 | rbChkU1 id add k 11 11 |
| 285 | rbChkU1 id sub k 1 0 |
| 286 | end |
| 287 | end |
| 288 | note 1b. 10 step functions |
| 289 | for i ulong 10 |
| 290 | k = add 8 mul add i v 5 |
| 291 | v0 = add 2 mul i 3 |
| 292 | v1 = add 50 mul i 4 |
| 293 | id = rbNextId |
| 294 | if not isMain |
| 295 | if eq get rbMode 0 0 |
| 296 | rbWrt rbNote |
| 297 | rbVerb add id v |
| 298 | rbWrt dStepA |
| 299 | rbNum v0 |
| 300 | rbWrt dStepB |
| 301 | rbNum k |
| 302 | rbWrt dStepC |
| 303 | rbNum v1 |
| 304 | rbWrt rbFnU1 |
| 305 | else |
| 306 | rbWrt rbOrPfx |
| 307 | rbNum id |
| 308 | rbWrt rbOrU1 |
| 309 | end |
| 310 | rbWrt bStep0 |
| 311 | rbNum k |
| 312 | rbWrt bStep1 |
| 313 | rbNum v0 |
| 314 | rbWrt bStep2 |
| 315 | rbNum v1 |
| 316 | rbEndFn |
| 317 | else |
| 318 | rbChkU1 id sub k 1 v0 |
| 319 | rbChkU1 id k v1 |
| 320 | end |
| 321 | end |
| 322 | note 1c. 8 round up to multiple of m |
| 323 | for i ulong 8 |
| 324 | m = add 3 mul add i v 2 |
| 325 | id = rbNextId |
| 326 | if not isMain |
| 327 | if eq get rbMode 0 0 |
| 328 | rbWrt rbNote |
| 329 | rbVerb add id v |
| 330 | rbWrt dRndUpA |
| 331 | rbNum m |
| 332 | rbWrt rbFnU1 |
| 333 | else |
| 334 | rbWrt rbOrPfx |
| 335 | rbNum id |
| 336 | rbWrt rbOrU1 |
| 337 | end |
| 338 | rbWrt bRndUp0 |
| 339 | rbNum m |
| 340 | rbWrt bRndUp1 |
| 341 | rbNum m |
| 342 | rbWrt bRndUp2 |
| 343 | rbEndFn |
| 344 | else |
| 345 | rbChkU1 id add mul m 2 1 mul m 3 |
| 346 | rbChkU1 id mul m 2 mul m 2 |
| 347 | end |
| 348 | end |
| 349 | note 1d. U2 & U3 core branch idioms |
| 350 | if not isMain |
| 351 | id = rbBeginFn 2 dAbsDiff |
| 352 | rbWrt bAbsDiff |
| 353 | rbEndFn |
| 354 | id = rbBeginFn 2 dSatSub2 |
| 355 | rbWrt bSatSub2 |
| 356 | rbEndFn |
| 357 | id = rbBeginFn 2 dMin2 |
| 358 | rbWrt bMin2 |
| 359 | rbEndFn |
| 360 | id = rbBeginFn 2 dMax2 |
| 361 | rbWrt bMax2 |
| 362 | rbEndFn |
| 363 | id = rbBeginFn 3 dMed3 |
| 364 | rbWrt bMed3 |
| 365 | rbEndFn |
| 366 | id = rbBeginFn 3 dInRng3 |
| 367 | rbWrt bInRng3 |
| 368 | rbEndFn |
| 369 | id = rbBeginFn 3 dClp3 |
| 370 | rbWrt bClp3 |
| 371 | rbEndFn |
| 372 | else |
| 373 | rbChkU2 rbNextId 10 3 7 |
| 374 | rbChkU2 rbNextId 10 3 7 |
| 375 | rbChkU2 rbNextId 10 3 3 |
| 376 | rbChkU2 rbNextId 10 3 10 |
| 377 | rbChkU3 rbNextId 9 2 5 5 |
| 378 | rbChkU3 rbNextId 5 2 9 1 |
| 379 | rbChkU3 rbNextId 12 2 9 9 |
| 380 | end |
| 381 | end |
| 382 | |
| 383 | note ==================== 2. BITS & DIGITS & LOOP ==================== |
| 384 | text dClrBitA "a with bit " |
| 385 | text dClrBitB " cleared to zero" |
| 386 | text bClrBit0 "ret and a xor ulong 18446744073709551615 shl ulong 1 " |
| 387 | |
| 388 | text dLowMskA "the low " |
| 389 | text dLowMskB " bits of a" |
| 390 | text bLowMsk0 "ret and a sub shl ulong 1 " |
| 391 | text bLowMsk1 " 1" |
| 392 | |
| 393 | text dParity "parity 1 if a has an odd number of 1 bits else 0" |
| 394 | text bParity "x = a\np = ulong 0\nwhile gt x 0 bound 64\np = xor p and x 1\nx = shr x 1\nend\nret p" |
| 395 | |
| 396 | text dPow2 "1 if a is a power of two else 0" |
| 397 | text bPow2 "if eq a 0\nret 0\nend\nif eq and a sub a 1 0\nret 1\nend\nret 0" |
| 398 | |
| 399 | text dBitLen "number of bits needed to represent unsigned a" |
| 400 | text bBitLen "x = a\nc = ulong 0\nwhile gt x 0 bound 64\nc = add c 1\nx = shr x 1\nend\nret c" |
| 401 | |
| 402 | text dRevDigA "reverse the digits of a in base " |
| 403 | text bRevDig0 "x = a\nr = ulong 0\nwhile gt x 0 bound 64\nr = add mul r " |
| 404 | text bRevDig1 " rem x " |
| 405 | text bRevDig2 "\nx = div x " |
| 406 | text bRevDig3 "\nend\nret r" |
| 407 | |
| 408 | text dGcd "greatest common divisor of a and b" |
| 409 | text bGcd "x = a\ny = b\nwhile gt y 0 bound 128\nt = rem x y\nx = y\ny = t\nend\nret x" |
| 410 | |
| 411 | text dFib "fibonacci number of a mod 25" |
| 412 | text bFib "n = rem a 25\nx = ulong 0\ny = ulong 1\nfor i ulong n bound 25\nt = add x y\nx = y\ny = t\nend\nret x" |
| 413 | |
| 414 | text dPow "a raised to the power b mod 10" |
| 415 | text bPow "e = rem b 10\nr = ulong 1\nfor i ulong e bound 10\nr = mul r a\nend\nret r" |
| 416 | |
| 417 | text dSumN "sum of integers from 1 to a mod 32" |
| 418 | text bSumN "n = rem a 32\ns = ulong 0\nfor i ulong n bound 32\ns = add s add i 1\nend\nret s" |
| 419 | |
| 420 | fn rbEmitBitsLoop isMain bool to void |
| 421 | v = get rbVar 0 |
| 422 | for i ulong 8 |
| 423 | bit = add 1 rem add i v 12 |
| 424 | id = rbNextId |
| 425 | if not isMain |
| 426 | if eq get rbMode 0 0 |
| 427 | rbWrt rbNote |
| 428 | rbVerb add id v |
| 429 | rbWrt dClrBitA |
| 430 | rbNum bit |
| 431 | rbWrt dClrBitB |
| 432 | rbWrt rbFnU1 |
| 433 | else |
| 434 | rbWrt rbOrPfx |
| 435 | rbNum id |
| 436 | rbWrt rbOrU1 |
| 437 | end |
| 438 | rbWrt bClrBit0 |
| 439 | rbNum bit |
| 440 | rbEndFn |
| 441 | else |
| 442 | rbChkU1 id add shl ulong 1 bit 1 1 |
| 443 | end |
| 444 | end |
| 445 | for i ulong 8 |
| 446 | k = add 2 rem add i v 10 |
| 447 | id = rbNextId |
| 448 | if not isMain |
| 449 | if eq get rbMode 0 0 |
| 450 | rbWrt rbNote |
| 451 | rbVerb add id v |
| 452 | rbWrt dLowMskA |
| 453 | rbNum k |
| 454 | rbWrt dLowMskB |
| 455 | rbWrt rbFnU1 |
| 456 | else |
| 457 | rbWrt rbOrPfx |
| 458 | rbNum id |
| 459 | rbWrt rbOrU1 |
| 460 | end |
| 461 | rbWrt bLowMsk0 |
| 462 | rbNum k |
| 463 | rbWrt bLowMsk1 |
| 464 | rbEndFn |
| 465 | else |
| 466 | rbChkU1 id sub shl ulong 1 k 1 sub shl ulong 1 k 1 |
| 467 | end |
| 468 | end |
| 469 | for i ulong 3 |
| 470 | base = ulong 10 |
| 471 | if eq i 1 |
| 472 | base = 8 |
| 473 | end |
| 474 | if eq i 2 |
| 475 | base = 16 |
| 476 | end |
| 477 | id = rbNextId |
| 478 | if not isMain |
| 479 | if eq get rbMode 0 0 |
| 480 | rbWrt rbNote |
| 481 | rbVerb add id v |
| 482 | rbWrt dRevDigA |
| 483 | rbNum base |
| 484 | rbWrt rbFnU1 |
| 485 | else |
| 486 | rbWrt rbOrPfx |
| 487 | rbNum id |
| 488 | rbWrt rbOrU1 |
| 489 | end |
| 490 | rbWrt bRevDig0 |
| 491 | rbNum base |
| 492 | rbWrt bRevDig1 |
| 493 | rbNum base |
| 494 | rbWrt bRevDig2 |
| 495 | rbNum base |
| 496 | rbWrt bRevDig3 |
| 497 | rbEndFn |
| 498 | else |
| 499 | if eq base 10 |
| 500 | rbChkU1 id 1234 4321 |
| 501 | end |
| 502 | if eq base 8 |
| 503 | rbChkU1 id 83 209 |
| 504 | end |
| 505 | if eq base 16 |
| 506 | rbChkU1 id 291 801 |
| 507 | end |
| 508 | end |
| 509 | end |
| 510 | if not isMain |
| 511 | id = rbBeginFn 1 dParity |
| 512 | rbWrt bParity |
| 513 | rbEndFn |
| 514 | id = rbBeginFn 1 dBitLen |
| 515 | rbWrt bBitLen |
| 516 | rbEndFn |
| 517 | id = rbBeginFn 2 dGcd |
| 518 | rbWrt bGcd |
| 519 | rbEndFn |
| 520 | id = rbBeginFn 1 dFib |
| 521 | rbWrt bFib |
| 522 | rbEndFn |
| 523 | id = rbBeginFn 2 dPow |
| 524 | rbWrt bPow |
| 525 | rbEndFn |
| 526 | id = rbBeginFn 1 dSumN |
| 527 | rbWrt bSumN |
| 528 | rbEndFn |
| 529 | else |
| 530 | rbChkU1 rbNextId 7 1 |
| 531 | rbChkU1 rbNextId 13 4 |
| 532 | rbChkU2 rbNextId 48 18 6 |
| 533 | rbChkU1 rbNextId 7 13 |
| 534 | rbChkU2 rbNextId 2 5 32 |
| 535 | rbChkU1 rbNextId 5 15 |
| 536 | end |
| 537 | end |
| 538 | |
| 539 | note ==================== 3. FOLD, SELECT, COUNT, SCAN ==================== |
| 540 | text dSumArr "sum of first n elements of array a" |
| 541 | text bSumArr "s = ulong 0\nfor i ulong n bound 8\ns = add s get a i\nend\nret s" |
| 542 | |
| 543 | text dMaxArr "maximum of first n elements of array a or 0 if empty" |
| 544 | text bMaxArr "if eq n 0\nret 0\nend\nm = get a 0\nfor i ulong n bound 8\nv = get a i\nif gt v m\nm = v\nend\nend\nret m" |
| 545 | |
| 546 | text dMinArr "minimum of first n elements of array a or 0 if empty" |
| 547 | text bMinArr "if eq n 0\nret 0\nend\nm = get a 0\nfor i ulong n bound 8\nv = get a i\nif lt v m\nm = v\nend\nend\nret m" |
| 548 | |
| 549 | text dXorArr "bitwise XOR of first n elements of array a" |
| 550 | text bXorArr "s = ulong 0\nfor i ulong n bound 8\ns = xor s get a i\nend\nret s" |
| 551 | |
| 552 | text dArgMax "index of maximum element in first n elements of array a" |
| 553 | text bArgMax "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" |
| 554 | |
| 555 | text dArgMin "index of minimum element in first n elements of array a" |
| 556 | text bArgMin "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" |
| 557 | |
| 558 | text dCntEv "count of even elements in first n elements of array a" |
| 559 | text bCntEv "c = ulong 0\nfor i ulong n bound 8\nif eq rem get a i 2 0\nc = add c 1\nend\nend\nret c" |
| 560 | |
| 561 | text dCntOd "count of odd elements in first n elements of array a" |
| 562 | text bCntOd "c = ulong 0\nfor i ulong n bound 8\nif ne rem get a i 2 0\nc = add c 1\nend\nend\nret c" |
| 563 | |
| 564 | text dSumEv "sum of even elements in first n elements of array a" |
| 565 | text bSumEv "s = ulong 0\nfor i ulong n bound 8\nv = get a i\nif eq rem v 2 0\ns = add s v\nend\nend\nret s" |
| 566 | |
| 567 | text dSumOd "sum of odd elements in first n elements of array a" |
| 568 | text bSumOd "s = ulong 0\nfor i ulong n bound 8\nv = get a i\nif ne rem v 2 0\ns = add s v\nend\nend\nret s" |
| 569 | |
| 570 | text dRngArr "difference between max and min of first n elements of array a" |
| 571 | text bRngArr "if eq n 0\nret 0\nend\nlo = get a 0\nhi = lo\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" |
| 572 | |
| 573 | text dIsSort "1 if first n elements of array a are non-decreasing else 0" |
| 574 | text bIsSort "if le n 1\nret 1\nend\nfor i ulong sub n 1 bound 8\nif gt get a i get a add i 1\nret 0\nend\nend\nret 1" |
| 575 | |
| 576 | text dCntGtA "count of elements strictly greater than " |
| 577 | text dCntGtB " in first n elements of array a" |
| 578 | text bCntGt0 "c = ulong 0\nfor i ulong n bound 8\nif gt get a i " |
| 579 | text bCntGt1 "\nc = add c 1\nend\nend\nret c" |
| 580 | |
| 581 | text dFndEqA "first index equal to " |
| 582 | text dFndEqB " in first n elements of array a or n if absent" |
| 583 | text bFndEq0 "for i ulong n bound 8\nif eq get a i " |
| 584 | text bFndEq1 "\nret i\nend\nend\nret n" |
| 585 | |
| 586 | fn rbEmitArrayRead isMain bool to void |
| 587 | v = get rbVar 0 |
| 588 | if not isMain |
| 589 | id = rbBeginFn 4 dSumArr |
| 590 | rbWrt bSumArr |
| 591 | rbEndFn |
| 592 | id = rbBeginFn 4 dMaxArr |
| 593 | rbWrt bMaxArr |
| 594 | rbEndFn |
| 595 | id = rbBeginFn 4 dMinArr |
| 596 | rbWrt bMinArr |
| 597 | rbEndFn |
| 598 | id = rbBeginFn 4 dXorArr |
| 599 | rbWrt bXorArr |
| 600 | rbEndFn |
| 601 | id = rbBeginFn 4 dArgMax |
| 602 | rbWrt bArgMax |
| 603 | rbEndFn |
| 604 | id = rbBeginFn 4 dArgMin |
| 605 | rbWrt bArgMin |
| 606 | rbEndFn |
| 607 | id = rbBeginFn 4 dCntEv |
| 608 | rbWrt bCntEv |
| 609 | rbEndFn |
| 610 | id = rbBeginFn 4 dCntOd |
| 611 | rbWrt bCntOd |
| 612 | rbEndFn |
| 613 | id = rbBeginFn 4 dSumEv |
| 614 | rbWrt bSumEv |
| 615 | rbEndFn |
| 616 | id = rbBeginFn 4 dSumOd |
| 617 | rbWrt bSumOd |
| 618 | rbEndFn |
| 619 | id = rbBeginFn 4 dRngArr |
| 620 | rbWrt bRngArr |
| 621 | rbEndFn |
| 622 | id = rbBeginFn 4 dIsSort |
| 623 | rbWrt bIsSort |
| 624 | rbEndFn |
| 625 | else |
| 626 | rbChkAr rbNextId 61 |
| 627 | rbChkAr rbNextId 19 |
| 628 | rbChkAr rbNextId 0 |
| 629 | rbChkAr rbNextId 21 |
| 630 | rbChkAr rbNextId 4 |
| 631 | rbChkAr rbNextId 2 |
| 632 | rbChkAr rbNextId 3 |
| 633 | rbChkAr rbNextId 5 |
| 634 | rbChkAr rbNextId 6 |
| 635 | rbChkAr rbNextId 55 |
| 636 | rbChkAr rbNextId 19 |
| 637 | rbChkAr rbNextId 0 |
| 638 | end |
| 639 | for i ulong 8 |
| 640 | thr = add 1 mul i 2 |
| 641 | id = rbNextId |
| 642 | if not isMain |
| 643 | if eq get rbMode 0 0 |
| 644 | rbWrt rbNote |
| 645 | rbVerb add id v |
| 646 | rbWrt dCntGtA |
| 647 | rbNum thr |
| 648 | rbWrt dCntGtB |
| 649 | rbWrt rbFnAr |
| 650 | else |
| 651 | rbWrt rbOrPfx |
| 652 | rbNum id |
| 653 | rbWrt rbOrAr |
| 654 | end |
| 655 | rbWrt bCntGt0 |
| 656 | rbNum thr |
| 657 | rbWrt bCntGt1 |
| 658 | rbEndFn |
| 659 | else |
| 660 | cnt = ulong 0 |
| 661 | if gt 3 thr |
| 662 | cnt = add cnt 1 |
| 663 | end |
| 664 | if gt 11 thr |
| 665 | cnt = add cnt 1 |
| 666 | end |
| 667 | if gt 7 thr |
| 668 | cnt = add cnt 1 |
| 669 | end |
| 670 | if gt 19 thr |
| 671 | cnt = add cnt 1 |
| 672 | end |
| 673 | if gt 4 thr |
| 674 | cnt = add cnt 1 |
| 675 | end |
| 676 | if gt 2 thr |
| 677 | cnt = add cnt 1 |
| 678 | end |
| 679 | if gt 15 thr |
| 680 | cnt = add cnt 1 |
| 681 | end |
| 682 | rbChkAr id cnt |
| 683 | end |
| 684 | end |
| 685 | for i ulong 4 |
| 686 | tgt = ulong 7 |
| 687 | exp = ulong 3 |
| 688 | if eq i 1 |
| 689 | tgt = 19 |
| 690 | exp = 4 |
| 691 | end |
| 692 | if eq i 2 |
| 693 | tgt = 15 |
| 694 | exp = 7 |
| 695 | end |
| 696 | if eq i 3 |
| 697 | tgt = 99 |
| 698 | exp = 8 |
| 699 | end |
| 700 | id = rbNextId |
| 701 | if not isMain |
| 702 | if eq get rbMode 0 0 |
| 703 | rbWrt rbNote |
| 704 | rbVerb add id v |
| 705 | rbWrt dFndEqA |
| 706 | rbNum tgt |
| 707 | rbWrt dFndEqB |
| 708 | rbWrt rbFnAr |
| 709 | else |
| 710 | rbWrt rbOrPfx |
| 711 | rbNum id |
| 712 | rbWrt rbOrAr |
| 713 | end |
| 714 | rbWrt bFndEq0 |
| 715 | rbNum tgt |
| 716 | rbWrt bFndEq1 |
| 717 | rbEndFn |
| 718 | else |
| 719 | rbChkAr id exp |
| 720 | end |
| 721 | end |
| 722 | end |
| 723 | |
| 724 | note ==================== 4. REWRITE (IN-PLACE ARRAY TRANSFORMS) ==================== |
| 725 | text dRwAddA "add " |
| 726 | text dRwAddB " to each of first n elements of array a in place" |
| 727 | text bRwAdd0 "for i ulong n bound 8\nput a i add get a i " |
| 728 | text bRwAdd1 "\nend" |
| 729 | |
| 730 | text dRwCapA "cap each of first n elements of array a at " |
| 731 | text dRwCapB " in place" |
| 732 | text bRwCap0 "for i ulong n bound 8\nif gt get a i " |
| 733 | text bRwCap1 "\nput a i " |
| 734 | text bRwCap2 "\nend\nend" |
| 735 | |
| 736 | text dRwFlrA "raise any element below " |
| 737 | text dRwFlrB " in first n elements of array a to " |
| 738 | text bRwFlr0 "for i ulong n bound 8\nif lt get a i " |
| 739 | text bRwFlr1 "\nput a i " |
| 740 | text bRwFlr2 "\nend\nend" |
| 741 | |
| 742 | text dRwRev "reverse the first n elements of array a in place" |
| 743 | text bRwRev "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" |
| 744 | |
| 745 | text dRwPfx "replace first n elements of array a with running prefix sums in place" |
| 746 | text bRwPfx "s = ulong 0\nfor i ulong n bound 8\ns = add s get a i\nput a i s\nend" |
| 747 | |
| 748 | text dRwZOd "zero out all odd elements in first n elements of array a in place" |
| 749 | text bRwZOd "for i ulong n bound 8\nif ne rem get a i 2 0\nput a i 0\nend\nend" |
| 750 | |
| 751 | text dRwDOd "double all odd elements in first n elements of array a in place" |
| 752 | text bRwDOd "for i ulong n bound 8\nv = get a i\nif ne rem v 2 0\nput a i mul v 2\nend\nend" |
| 753 | |
| 754 | fn rbEmitRewrite isMain bool to void |
| 755 | v = get rbVar 0 |
| 756 | for i ulong 6 |
| 757 | k = add 1 add i v |
| 758 | id = rbNextId |
| 759 | if not isMain |
| 760 | if eq get rbMode 0 0 |
| 761 | rbWrt rbNote |
| 762 | rbVerb add id v |
| 763 | rbWrt dRwAddA |
| 764 | rbNum k |
| 765 | rbWrt dRwAddB |
| 766 | rbWrt rbFnAw |
| 767 | else |
| 768 | rbWrt rbOrPfx |
| 769 | rbNum id |
| 770 | rbWrt rbOrAw |
| 771 | end |
| 772 | rbWrt bRwAdd0 |
| 773 | rbNum k |
| 774 | rbWrt bRwAdd1 |
| 775 | rbEndFn |
| 776 | else |
| 777 | rbChkAw id add 25 mul 3 k |
| 778 | end |
| 779 | end |
| 780 | for i ulong 5 |
| 781 | cap = add 5 mul i 3 |
| 782 | id = rbNextId |
| 783 | if not isMain |
| 784 | if eq get rbMode 0 0 |
| 785 | rbWrt rbNote |
| 786 | rbVerb add id v |
| 787 | rbWrt dRwCapA |
| 788 | rbNum cap |
| 789 | rbWrt dRwCapB |
| 790 | rbWrt rbFnAw |
| 791 | else |
| 792 | rbWrt rbOrPfx |
| 793 | rbNum id |
| 794 | rbWrt rbOrAw |
| 795 | end |
| 796 | rbWrt bRwCap0 |
| 797 | rbNum cap |
| 798 | rbWrt bRwCap1 |
| 799 | rbNum cap |
| 800 | rbWrt bRwCap2 |
| 801 | rbEndFn |
| 802 | else |
| 803 | e0 = ulong 3 |
| 804 | if gt e0 cap |
| 805 | e0 = cap |
| 806 | end |
| 807 | e3 = ulong 7 |
| 808 | if gt e3 cap |
| 809 | e3 = cap |
| 810 | end |
| 811 | e7 = ulong 15 |
| 812 | if gt e7 cap |
| 813 | e7 = cap |
| 814 | end |
| 815 | rbChkAw id add e0 add e3 e7 |
| 816 | end |
| 817 | end |
| 818 | for i ulong 5 |
| 819 | flr = add 4 mul i 3 |
| 820 | id = rbNextId |
| 821 | if not isMain |
| 822 | if eq get rbMode 0 0 |
| 823 | rbWrt rbNote |
| 824 | rbVerb add id v |
| 825 | rbWrt dRwFlrA |
| 826 | rbNum flr |
| 827 | rbWrt dRwFlrB |
| 828 | rbNum flr |
| 829 | rbWrt rbFnAw |
| 830 | else |
| 831 | rbWrt rbOrPfx |
| 832 | rbNum id |
| 833 | rbWrt rbOrAw |
| 834 | end |
| 835 | rbWrt bRwFlr0 |
| 836 | rbNum flr |
| 837 | rbWrt bRwFlr1 |
| 838 | rbNum flr |
| 839 | rbWrt bRwFlr2 |
| 840 | rbEndFn |
| 841 | else |
| 842 | e0 = ulong 3 |
| 843 | if lt e0 flr |
| 844 | e0 = flr |
| 845 | end |
| 846 | e3 = ulong 7 |
| 847 | if lt e3 flr |
| 848 | e3 = flr |
| 849 | end |
| 850 | e7 = ulong 15 |
| 851 | if lt e7 flr |
| 852 | e7 = flr |
| 853 | end |
| 854 | rbChkAw id add e0 add e3 e7 |
| 855 | end |
| 856 | end |
| 857 | if not isMain |
| 858 | id = rbBeginFn 5 dRwRev |
| 859 | rbWrt bRwRev |
| 860 | rbEndFn |
| 861 | id = rbBeginFn 5 dRwPfx |
| 862 | rbWrt bRwPfx |
| 863 | rbEndFn |
| 864 | id = rbBeginFn 5 dRwZOd |
| 865 | rbWrt bRwZOd |
| 866 | rbEndFn |
| 867 | id = rbBeginFn 5 dRwDOd |
| 868 | rbWrt bRwDOd |
| 869 | rbEndFn |
| 870 | else |
| 871 | rbChkAw rbNextId 37 |
| 872 | rbChkAw rbNextId 85 |
| 873 | rbChkAw rbNextId 0 |
| 874 | rbChkAw rbNextId 50 |
| 875 | end |
| 876 | end |
| 877 | |
| 878 | fn rbEqArg a view uchar * al ulong b view uchar * to bool |
| 879 | if ne al len b |
| 880 | ret false |
| 881 | end |
| 882 | for i ulong al bound 32 |
| 883 | if ne get a i get b i |
| 884 | ret false |
| 885 | end |
| 886 | end |
| 887 | ret true |
| 888 | end |
| 889 | |
| 890 | fn main to int |
| 891 | ab = new uchar 32 0 |
| 892 | vb = new uchar 16 0 |
| 893 | mode = ulong 0 |
| 894 | varn = ulong 1 |
| 895 | if ge argcnt 2 |
| 896 | lr = argread 1 ab 0 32 |
| 897 | if gt lr 0 |
| 898 | if rbEqArg ab ulong lr rbArgOracle |
| 899 | mode = 1 |
| 900 | end |
| 901 | end |
| 902 | end |
| 903 | if ge argcnt 3 |
| 904 | vr = argread 2 vb 0 16 |
| 905 | if gt vr 0 |
| 906 | ch = ulong get vb 0 |
| 907 | if and ge ch 49 le ch 57 |
| 908 | varn = sub ch 48 |
| 909 | end |
| 910 | end |
| 911 | end |
| 912 | put rbMode 0 mode |
| 913 | put rbVar 0 varn |
| 914 | if eq mode 1 |
| 915 | rbWrt rbOm2 |
| 916 | put rbId 0 0 |
| 917 | rbEmitBranch false |
| 918 | rbEmitBitsLoop false |
| 919 | rbEmitArrayRead false |
| 920 | rbEmitRewrite false |
| 921 | rbWrt rbMainH |
| 922 | put rbId 0 0 |
| 923 | rbEmitBranch true |
| 924 | rbEmitBitsLoop true |
| 925 | rbEmitArrayRead true |
| 926 | rbEmitRewrite true |
| 927 | rbWrt rbMainT |
| 928 | ret 0 |
| 929 | end |
| 930 | put rbId 0 0 |
| 931 | rbEmitBranch false |
| 932 | rbEmitBitsLoop false |
| 933 | rbEmitArrayRead false |
| 934 | rbEmitRewrite false |
| 935 | ret 0 |
| 936 | end |