🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #112

Re: F.N floating point format word

#112
From: Anton Ertl
Date: Sat, 17 Oct 2026 13:59
111 lines
5283 bytes
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> ;

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
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Technical Details
Message-ID: <2026Oct17.155902@mips.complang.tuwien.ac.at>
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
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>

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: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: F.N floating point format word
Date: Sat, 17 Oct 2026 13:59:02 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 111
Message-ID: <2026Oct17.155902@mips.complang.tuwien.ac.at>
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>
Injection-Date: Sat, 17 Oct 2026 15:00:11 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="322951"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19YN9T9mzHXXYxVQi0Gsmip"; posting-host="16c33216d85d56b66611c21098e8c6f9"
Cancel-Lock: sha1:2dZpHZqW15V4jNGKf5Fu53CWEY0= sha256:iwWgQfL7nNTmg5Z1JQru0ZRHxWTkPDf/vv0YubiydEA=
	sha1:wdugole7beO9fq4sNsR6uB4oXQI= sha256:iO96fPWabiqZW2yVVIkQbu/ajBj2CqP9qEYjkPjLxVM=
X-newsreader: xrn 10.11