🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #109

Re: F.N floating point format word

#109
From: peter
Date: Sat, 17 Oct 2026 13:41
137 lines
6678 bytes
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.
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

Technical Details
Message-ID: <20261017154100.0000475b@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-mail
References: <119oin2$26n99$1@dont-email.me> <11a01mr$sqrm$1@dont-email.me> <20261005150222.00006cf5@tin.it> <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>

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 15:41:00 +0200
Organization: A noiseless patient Spider
Lines: 137
Message-ID: <20261017154100.0000475b@tin.it>
References: <119oin2$26n99$1@dont-email.me>
	<11a01mr$sqrm$1@dont-email.me>
	<20261005150222.00006cf5@tin.it>
	<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>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Oct 2026 13:41:13 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="184653"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1954R3vcNCBi6HtXHDXtOaGXVq6BVZldyk="; posting-host="507d3bef6a0b8698f82fc557b52a9e01"
Cancel-Lock: sha1:bHK2O8XO/m/CwY6H3L4Fvtvcpjc= sha256:q9K3jAKYwdFO5oGYBVdeiPu12vX3XClAfM6tfJblbvM=
	sha1:pDsdPLlTgNEkAIFGEX4tParDOw0= sha256:m3GZnMAnwEaNrz/JAgMF2HyEKnXbzdjCo16Y1U50RiY=
X-Newsreader: Claws Mail 4.4.0 (GTK 3.24.51; x86_64-w64-mingw32)