ckrause / loda

LODA is an assembly language, a computational model and a tool for mining integer sequence programs.
Apache License 2.0
20 stars 5 forks source link

Suggested new instructions: MAX ("Take maximum value of the operands"). #54

Closed karttu closed 3 years ago

karttu commented 3 years ago

Suggested new instructions: max a,b What it would do: select the maximum of a and b and store it into a: a:=max(a,b). Rationale: Although this can be implemented with trn and some other instructions, having it packed into a single instruction would facilitate coding many of the "take maximum of something" kind of loops that are a reasonably common pattern also in many sequences. And would also increase the chances that the miner will find such meaningful subroutines by itself. (NB: code like https://github.com/ckrause/loda/blob/master/programs/oeis/008/A008833.asm would probably shorten considerably). PS. As what comes to min a, b I think it might be good idea to do that (or have another opcode called something like umin) comparison a bit diffrently, as with unsigned integers, with signed value -1 reserved in that case (only) for a value like +infinity. That is, doing mov $1,-1 and then in the loop umin $1,$2 would always at the first iteration overwrite $1 with whatever value of $2 (interpreted as unsigned integer), and etc. PPS. Forget min for a moment, or do it the standard way. In any case, max is much more used in seq. definitions.

ckrause commented 3 years ago

I think max definitely makes sense. I would actually like to introduce and drop trn (it can be migrated to sub,max).

Regarding min: I think it also makes sense it as dual operation to max (but using the normal semantics)

karttu commented 3 years ago

Yes. If you remove trn, then could you make it available (for backwards compatility) as a "macro" of some sort, as it can be implemented without an extra register. That is, trn a,b = sub a,b followed by max a,0. (As you mentioned above!) Yes, min with ordinary semantics (with signed integers) is the best. I was thinking of a version that would be suitable for fold-loops. Say, if you have to collect minimum-value from the ensemble of natural numbers > 0, then you can use 0 itself as a starting value. Then an operation like miz a,b with semantics a := b if a=0, and otherwise a := min(a,b) would work very well. But this can be implemented with min and some other instruction(s), I'm almost sure.

ckrause commented 3 years ago

done. we leave trn for now.