🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #59

Re: Pairwise dot product

#59
From: Anton Ertl
Date: Sun, 11 Oct 2026 08:04
100 lines
5813 bytes
marcel hendrix <mhx@iae.nl> writes:
>On 10/9/2026 9:20 AM, Anton Ertl wrote:
>> marcel hendrix <mhx@iae.nl> writes:
>>> On 10/9/2026 7:32 AM, Anton Ertl wrote:
>[..]> VFX generates good code for v*1...v*64, e.g., v*2:
>> 
>> ( 0050AB40    DD03 )                  FLD     DOUBLE 0 [RBX]
>> ( 0050AB42    488B5500 )              MOV     RDX, [RBP]
>> ( 0050AB46    DD02 )                  FLD     DOUBLE 0 [RDX]
>> ( 0050AB48    DEC9 )                  FMULP   ST(1), ST
>> ( 0050AB4A    488B5500 )              MOV     RDX, [RBP]
>> ( 0050AB4E    48035510 )              ADD     RDX, [RBP+10]
>> ( 0050AB52    48035D08 )              ADD     RBX, [RBP+08]
>> ( 0050AB56    DD03 )                  FLD     DOUBLE 0 [RBX]
>> ( 0050AB58    DD02 )                  FLD     DOUBLE 0 [RDX]
>> ( 0050AB5A    DEC9 )                  FMULP   ST(1), ST
>> ( 0050AB5C    48035510 )              ADD     RDX, [RBP+10]
>> ( 0050AB60    48035D08 )              ADD     RBX, [RBP+08]
>> ( 0050AB64    DEC1 )                  FADDP   ST(1), ST
>> ( 0050AB66    48895500 )              MOV     [RBP], RDX
>> ( 0050AB6A    C3 )                    RET/NEXT
>> ( 43 bytes, 15 instructions )
>[..]> VFX compiles 18 Forth words into 15 instructions in this case.  For
>> v*64, it compiles 576 Forth words into 387 instructions.  It may or
>> may not be helpful for performance to load the values that it accesses
>> at [RBP+10] and [RBP+08] into registers, too, to reduce the usage of
>> load ports, which may limit the performance of this code.
>
>VFX is about 3 times faster for paired-v*, which reeks of some magical 
>loophole in the x86 instruction set ... It benefits from (1) not 
>push/popping RBX and RBX (assuming the FADDP doesn't swamp that) and (2) 
>not saving FTOS to memory after the FADDP . I wonder if (2) is by 
>design, or if it simply assumes (or makes sure) the FP-stack will be 
>deep enough for further iterations. (I found "making sure" too difficult 
>without interrupts.)

By default (i.e., unless you use a different FP package) VFX64 indeed
has only an 8-deep FP:

[~:172625] vfx64 's" floating-stack" environment? . . bye'
VFX Forth 64 5.43 [build 0199] 2023-11-09 for Linux x64
© MicroProcessor Engineering Ltd, 1998-2023 
-1 8 

PAIRWISE-V* contains code to work around that limitation.

Concerning 1), modern processor cores like Zen3 and Zen4 have a
hardware optimization that forwards stores (pushes) to loads (pops) in
zero cycles in many cases; it does not always work, so 1) may or may
not be an issue.

Concerning 2), I have not measured store to load forwarding for
DF!...DF@ implemented with th 387 instructions, but my guess is that
it does not perform store-to-load forwarding, because there is not
just forwarding involved, but also the conversion from the 80-bit
format to the 64-bit format and back.  And even with F!...F@ with the
387 instructions, the 80-bit nature of the values involved may or may
not prevent 0-cycle store-to-load forwarding (I would have to
measure).

So my educated guess is that 2) contributes more to the speed
difference than 1).

>With VFX64 5.43, was FPSYSTEM value == 1?

[~:172623] vfx64 "fpsystem . bye"
VFX Forth 64 5.43 [build 0199] 2023-11-09 for Linux x64
© MicroProcessor Engineering Ltd, 1998-2023 
2 


Here's a comparison of the performance of VFX64 with different FP
packs on Zen4:

 Ndpx64.fth Hfpx64,fth FPSSE64S.fth 
           K cycles:u
  3_427_115 12_098_828  9_553_877 naive-v*
  1_217_805 11_230_571  9_921_314 pairwise-v*

           K instructions:u
  6_072_800 16_601_813 16_599_400 naive-v*
  3_821_864 15_421_328 15_637_296 pairwise-v*

Ndpx64.fth:   387 stack only, 8-deep FP stack (default)
Hfpx64,fth:   387 instructions, FP stack in memory
FPSSE64S.fth: SSE2 instructions, FP stack in memory

SEE V*2 crashes with the non-default FP packs in VFX64 5.43 (possibly
fixed since that version), so I am not sure what the code does.  For
VFX64 5.11 the FP words are called (not inlined), and they keep the
FTOS in a register, but perform no other register allocation or
somesuch for FP values; the calls also mean that the integer code is
not analysed as well as it is with Ndpx64.fth.

- 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: <2026Oct11.100414@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: <2026Oct8.175513@mips.complang.tuwien.ac.at> <11a9aah$19g75$1@dont-email.me> <2026Oct9.073240@mips.complang.tuwien.ac.at> <11aa44u$9s7j$1@dont-email.me> <2026Oct9.092016@mips.complang.tuwien.ac.at> <11ae2e7$bv4s$1@dont-email.me>

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: Pairwise dot product
Date: Sun, 11 Oct 2026 08:04:14 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 100
Message-ID: <2026Oct11.100414@mips.complang.tuwien.ac.at>
References: <2026Oct8.175513@mips.complang.tuwien.ac.at> <11a9aah$19g75$1@dont-email.me> <2026Oct9.073240@mips.complang.tuwien.ac.at> <11aa44u$9s7j$1@dont-email.me> <2026Oct9.092016@mips.complang.tuwien.ac.at> <11ae2e7$bv4s$1@dont-email.me>
Injection-Date: Sun, 11 Oct 2026 08:58:22 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2618552"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+SxI0eisSnAI8ERhcUbySw"; posting-host="048ebf825c3e0ead5be8fd534130ebc5"
Cancel-Lock: sha1:OmKh2gcw+VDCPrqKJBU4EZ6dpy8= sha256:eXLjFOoPdivOJyWtqJnCib3zGoWpnGO7qznFYIhBVDQ=
	sha1:0rD4VrCy3cQAcNkIReRpUHJAuLQ= sha256:UI0FTynm9xOFxqDlwHDAVsYvLw6GbFV6aa5gNzniPbU=
X-newsreader: xrn 10.11