Article View: comp.lang.forth
Article #116Re: F.N floating point format word
From: peter
Date: Sat, 17 Oct 2026 16:20
Date: Sat, 17 Oct 2026 16:20
190 lines
9334 bytes
9334 bytes
On Sat, 17 Oct 2026 13:59:02 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> peter <peter.noreply@tin.it> writes:
> >On Sat, 17 Oct 2026 10:06:20 GMT
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> >
> >> peter <peter.noreply@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> ;
This is nice and easy to read.
seea a2
0x4285A0 48837D0000 cmp qword ptr [rbp], 0x0
0x4285A5 48895C24F8 mov qword ptr [rsp-0x8], rbx
0x4285AA 488B5D00 mov rbx, qword ptr [rbp]
0x4285AE 488D6D08 lea rbp, [rbp+0x8]
0x4285B2 488D6424F8 lea rsp, [rsp-0x8]
0x4285B7 7C17 jl 0x4285D0
0x4285B9 48C7C000040000 mov rax, 0x400
0x4285C0 482B0424 sub rax, qword ptr [rsp]
0x4285C4 4839C3 cmp rbx, rax
0x4285C7 7D02 jge 0x4285CB
0x4285C9 4893 xchg rbx, rax
0x4285CB 4889C3 mov rbx, rax
0x4285CE EB22 jmp 0x4285F2
0x4285D0 48833C2400 cmp qword ptr [rsp], 0x0
0x4285D5 7F09 jg 0x4285E0
0x4285D7 48C7C300000000 mov rbx, 0x0
0x4285DE EB12 jmp 0x4285F2
0x4285E0 48C7C001000000 mov rax, 0x1
0x4285E7 482B0424 sub rax, qword ptr [rsp]
0x4285EB 4839C3 cmp rbx, rax
0x4285EE 7D02 jge 0x4285F2
0x4285F0 4893 xchg rbx, rax
0x4285F2 48895DF8 mov qword ptr [rbp-0x8], rbx
0x4285F6 488B1C24 mov rbx, qword ptr [rsp]
0x4285FA 488D6DF8 lea rbp, [rbp-0x8]
0x4285FE 488D642408 lea rsp, [rsp+0x8]
0x428603 C3 ret
100 bytes, 27 instructions
> 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 ;
This is how I would have written it if it was a standalone word
seea a1b
0x428550 48837D0000 cmp qword ptr [rbp], 0x0
0x428555 7C1A jl 0x428571
0x428557 48C7C000040000 mov rax, 0x400
0x42855E 4829D8 sub rax, rbx
0x428561 488B4D00 mov rcx, qword ptr [rbp]
0x428565 4839C8 cmp rax, rcx
0x428568 7D02 jge 0x42856C
0x42856A 4891 xchg rcx, rax
0x42856C 48894D00 mov qword ptr [rbp], rcx
0x428570 C3 ret
0x428571 4883FB00 cmp rbx, 0x0
0x428575 7F09 jg 0x428580
0x428577 48C7450000000000 mov qword ptr [rbp], 0x0
0x42857F C3 ret
0x428580 48C7C001000000 mov rax, 0x1
0x428587 4829D8 sub rax, rbx
0x42858A 488B4D00 mov rcx, qword ptr [rbp]
0x42858E 4839C8 cmp rax, rcx
0x428591 7D02 jge 0x428595
0x428593 4891 xchg rcx, rax
0x428595 48894500 mov qword ptr [rbp], rax
0x428599 C3 ret
74 bytes, 22 instructions
> 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.
It tests out right!
I agree that this is the easiest to understand.
When I started the code-generator for lxf, many years ago my first approach
was to push the state to a state stack at a block entry and to pop it of
at the end and there do a fixup to get both branches of an IF to the same
state . It tuned out difficult to implement so I started with the easy
way of returning to a known state at every block. This was good also
for another reason. The longer blocks you have the more registers can be
tied up. In lxf I have 5 registers available to the code generator. It is
very seldom that they get over used and need to spill to memory. In lxf64
I have 9 registers and have not yet written a spill routine. The few times
it has been signaled it has been due to another error, like not releasing
a register. On ARM64 there will be even more registers to play with.
I need to think if I can use them better
BR
Peter
> - anton
Technical Details
Message-ID:
<20261017182005.0000659d@tin.it>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<119oin2$26n99$1@dont-email.me> <11a16nc$1camn$1@dont-email.me> <20261006080347.00002cdf@tin.it> <11a4tj5$2ku7v$1@dont-email.me> <20261007223526.000065ab@tin.it> <6ac7013a$1@news.ausics.net> <20261008121302.00007412@tin.it> <20261008152645.00001682@tin.it> <11a88oh$3t0ti$1@dont-email.me> <20261008220746.0000042f@tin.it> <11autmd$3t6hf$1@dont-email.me> <20261017115221.0000440b@tin.it> <2026Oct17.120620@mips.complang.tuwien.ac.at> <20261017154100.0000475b@tin.it> <2026Oct17.155902@mips.complang.tuwien.ac.at>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: peter <peter.noreply@tin.it> Newsgroups: comp.lang.forth Subject: Re: F.N floating point format word Date: Sat, 17 Oct 2026 18:20:05 +0200 Organization: A noiseless patient Spider Lines: 190 Message-ID: <20261017182005.0000659d@tin.it> References: <119oin2$26n99$1@dont-email.me> <11a16nc$1camn$1@dont-email.me> <20261006080347.00002cdf@tin.it> <11a4tj5$2ku7v$1@dont-email.me> <20261007223526.000065ab@tin.it> <6ac7013a$1@news.ausics.net> <20261008121302.00007412@tin.it> <20261008152645.00001682@tin.it> <11a88oh$3t0ti$1@dont-email.me> <20261008220746.0000042f@tin.it> <11autmd$3t6hf$1@dont-email.me> <20261017115221.0000440b@tin.it> <2026Oct17.120620@mips.complang.tuwien.ac.at> <20261017154100.0000475b@tin.it> <2026Oct17.155902@mips.complang.tuwien.ac.at> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Sat, 17 Oct 2026 16:20:09 +0000 (UTC) Injection-Info: dont-email.me; logging-data="365591"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19My7cI6Z9W7rXXVhHAOe7k/KCBdnbTSY4="; posting-host="507d3bef6a0b8698f82fc557b52a9e01" Cancel-Lock: sha1:x6R/GoZFKx2S19tQhJP8V1VpkuY= sha256:yuY6youvwCuE3o/O8jBD/05Tj5UOXRFOdpY1g5Ii464= sha1:vlklHdQK7UcQGFfjEX0Uebmk64g= sha256:Vlv6wofHVYXeqcDXosUrZC5XRh96rLqPfdrbrPinkCo= X-Newsreader: Claws Mail 4.4.0 (GTK 3.24.51; x86_64-w64-mingw32)