peter <pete…@tin.it> writes:
>On Sat, 17 Oct 2026 10:06:20 GMT >ant…@mips.complang.tuwien.ac.at (Anton Ertl) wrote: >
>> peter <pete…@tin.it> writes:
>> >Here is a version that corrects this. >> > >> >over 0 >= if >> > swap over 1024 swap - min swap else \ limit positive n >> > dup 0<= if nip 0 swap else \ limit negative n when e negative >> > 1 over - rot max swap then then \ limit negative n
>> >> >> Can this be simplified? Here's what I came up with: >> >> over 0 >= if >> 1024 2 pick - rot min >> else >> dup 0<= if >> nip 0 >> else >> 1 over - rot max >> then >> then >> swap
> >So you are basically taken out the swap and putting it last.
Yes, and the change to the stuff after the first IF that I got wrong.
>The 2 pick needs to be over to work properly.
Even nicer.
>Lets now compile our different implementations >by moving out the swap and reducing from 3 to 1 occurrences we get >20 bytes more code!
Yes, with your code generator that usually produces a perfect register allocation for basic blocks, the change to the code after the first if provides no benefit to the generated code. As for the SWAP, your code generator produces the canonical stack representation at every basic block end, so putting the SWAP in a separate basic block costs extra code in that block, and the other blocks don't necessarily get smaller, sometimes even larger. E.g. nip 0 nip 0 swap mov qword ptr [rbp], rbx mov qword ptr [rbp], 0x0 mov rbx, 0x0
>Now this example when we look at the function >standalone instead of inlined we are penalized by moving out the swap >as it need to generate code, it gets a block by itself between >then and ; In P1 we have the exp always as top of stack at all block >boundaries, it is unmodified and resides always in rbx.
Given that you don't change exp, another alternative is to keep it on the return stack: : A2 >r dup 0 >= if 1024 r@ - min else r@ 0<= if drop 0 else 1 r@ - max then then r> ; I leave it to you to determine if any of those results in shorter code (my copy of lxf64 does not generate native code yet). And who knows if I got that right. Apart from inlining, there are two other ways in which the code generation can be improved: 1) Extend the basic blocks by duplicating the code after control-flow joins (in particular for THEN) until some other limit is reached (e.g., the end of the definition); this would undo the move of the SWAP to the end, but also duplicate the EXIT, and make the ELSE jumps dead code, resulting in code as if the source looked like (for A1): : A1B over 0 >= if 1024 over - rot min swap exit then dup 0<= if nip 0 swap exit then 1 over - rot max swap ; That's closer to P1 than A1:-). 2) Register allocation that works across basic block boundaries and does not need to materialize a canonical state there. If the code generator is good enough that it does not make a significant difference in the generated code which of the variants we have written, we can ask ourselves which version is best for understanding by a reader. Looking at the variants again, the return-stack variant looks best to me, but again, who knows if I got it right. - anton