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.
The 2 pick needs to be over to work properly.
we have n, exp on the stack. The 3 cases we have can be summarized as
sign of
n exp limit of n
+ +- n=min(n, 1024-exp)
- - n=0
- + n=max(n, 1-e)
Lets now compile our different implementations
: A1
over 0 >= if
1024 over - rot min
else
dup 0<= if
nip 0
else
1 over - rot max
then
then
swap ;
: P1
over 0 >= if
swap over 1024 swap - min swap else \ limit positive n
dup 0<= if nip 0 swap else \ limit n when e negative
1 over - rot max swap then then ; \ limit negative n
seea A1
0x4284E0 48837D0000 cmp qword ptr [rbp], 0x0
0x4284E5 7C1E jl 0x428505
0x4284E7 48C7C000040000 mov rax, 0x400
0x4284EE 4829D8 sub rax, rbx
0x4284F1 488B4D00 mov rcx, qword ptr [rbp]
0x4284F5 4839C8 cmp rax, rcx
0x4284F8 7D02 jge 0x4284FC
0x4284FA 4891 xchg rcx, rax
0x4284FC 48895D00 mov qword ptr [rbp], rbx
0x428500 4889CB mov rbx, rcx
0x428503 EB2F jmp 0x428534
0x428505 4883FB00 cmp rbx, 0x0
0x428509 7F0D jg 0x428518
0x42850B 48895D00 mov qword ptr [rbp], rbx
0x42850F 48C7C300000000 mov rbx, 0x0
0x428516 EB1C jmp 0x428534
0x428518 48C7C001000000 mov rax, 0x1
0x42851F 4829D8 sub rax, rbx
0x428522 488B4D00 mov rcx, qword ptr [rbp]
0x428526 4839C8 cmp rax, rcx
0x428529 7D02 jge 0x42852D
0x42852B 4891 xchg rcx, rax
0x42852D 48895D00 mov qword ptr [rbp], rbx
0x428531 4889C3 mov rbx, rax
0x428534 488B4500 mov rax, qword ptr [rbp]
0x428538 48895D00 mov qword ptr [rbp], rbx
0x42853C 4889C3 mov rbx, rax
0x42853F C3 ret
96 bytes, 28 instructions
seea p1
0x428440 48837D0000 cmp qword ptr [rbp], 0x0
0x428445 7C1B jl 0x428462
0x428447 48C7C000040000 mov rax, 0x400
0x42844E 4829D8 sub rax, rbx
0x428451 488B4D00 mov rcx, qword ptr [rbp]
0x428455 4839C1 cmp rcx, rax
0x428458 7D02 jge 0x42845C
0x42845A 4891 xchg rcx, rax
0x42845C 48894500 mov qword ptr [rbp], rax
0x428460 EB29 jmp 0x42848B
0x428462 4883FB00 cmp rbx, 0x0
0x428466 7F0A jg 0x428472
0x428468 48C7450000000000 mov qword ptr [rbp], 0x0
0x428470 EB19 jmp 0x42848B
0x428472 48C7C001000000 mov rax, 0x1
0x428479 4829D8 sub rax, rbx
0x42847C 488B4D00 mov rcx, qword ptr [rbp]
0x428480 4839C8 cmp rax, rcx
0x428483 7D02 jge 0x428487
0x428485 4891 xchg rcx, rax
0x428487 48894500 mov qword ptr [rbp], rax
0x42848B C3 ret
76 bytes, 22 instructions
by moving out the swap and reducing from 3 to 1 occurrences we get
20 bytes more code! 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.
In A1 the exp is second on stack and needs to be moved from rbx to memory
after each block of the code.
Note also that max and min are implemented to not create a block
boundary. Generally stack movements only create code at block boundaries
BR
Peter
> Or, using CASE with ?OF:
>
> case
> dup 0 >= ?of 1024 2 pick - rot min endof
> dup 0<= ?of nip 0 endof
> 1 over - rot max
> 0 endcase
> swap
>
> - anton