🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #31

Pairwise dot product

#31
From: Anton Ertl
Date: Thu, 08 Oct 2026 15:55
91 lines
4983 bytes
I discussed summing up an array with pairwise addition earlier
<2025Jul16.132504@mips.complang.tuwien.ac.at>.  The benefits compared
to the straightforward algorithm are:

* Better performance, because the straightforward algorithm is limited
  by the dependence chains of the F+ computations.

* Lower rounding errors in the typical case.

The dot-product of two vectors is a primitive in linear algebra, and
also has such a chain of F+ if implemented natively.  Implementing the
dot product with pairwise addition should have similar benefits.

Gforth already has a word for the dot product:

|'v*' ( f-addr1 nstride1 f-addr2 nstride2 ucount - r ) gforth-0.5 "v-star"
|   dot-product: r=v1*v2.  The first element of v1 is at f_addr1, the
|next at f_addr1+nstride1 and so on (similar for v2).  Both vectors have
|ucount elements.

This word is implemented as C function, which allows us to compare the
performance of the various Forth systems to C.

I have implemented variants of v* in Forth, a straightforward one and
one with pairwise addition.  The code can be found in

https://www.complang.tuwien.ac.at/forth/programs/pairwise-fdot.4th

Note that if your Forth system only supports an 8-deep FP stack, you
must not call PAIRWISE-V* unless the FP stack is empty.

I have measured the performance for 10000 dot products of vectors of
50000 elements (so the straightforward algorithm has a minimum
run-time of 0.5G times the latency of F+.

Here are the results (measured on Zen4 (Ryzen 8700G)):

   gforth-fast gforth-fast-v3     lxf            sf64          vfx64
                            cycles:u
 1_543_071_551  2_044_522_195                                             v*
 6_095_056_325  6_544_418_067  3_221_307_358  4_579_918_381 3_425_243_416 st
 4_350_367_901  4_371_526_269 10_527_316_831 10_630_319_712 1_203_870_244 pw

                           instructions:u
 3_618_080_286  3_117_767_051                                             v*
11_618_280_866 11_617_967_567  9_516_121_645 17_010_999_668 6_074_024_083 st
10_164_410_950 10_164_097_601 14_265_222_850 16_962_950_438 3_823_084_163 pw

The Forth versions are:
gforth-fast    current development version
gforth-fast-v3 current development version compiled with -march=x86-64-v3
                   this makes use of fma instructions
lxf 1.6-982-823
sf64 SwiftForth 4.0.0-RC89
vfx64 5.43

The versions are:
tag word        Explanation
v*  v*          Gforth primitive implemented in C
st  naive-v*    Straightforward implementation
pw  pairwise-v*

VFX64 shows that pairwise addition can be faster than even the C
implementation of the straightforward algorithm (v* in gforth-fast).
VFX also executes slightly fewer instructions than V* on Gforth-fast:
V* has loop overhead, whereas PAIRWISE-V* uses base steps in the
recursion for vectors with up to 64 elements, resulting in less
overhead.

With FMA instructions V* executes fewer instructions (6 per iteration
instead of 7), but more cycles (4 per iteration instead of 3).  My
explanation is that the FMA instruction has 4 cycles of latency on
Zen4, while fadd has 3 cycles of latency.  Interestingly, in
gforth-fast-v3 NAIVE-V* takes the same number of instructions and one
cycle per iteration more than in gforth-fast, but I have not looked at
why that is.

In gforth-fast the PAIRWISE-V* is quite a bit faster than NAIVE-V*.
It also takes a little fewer instructions, probably because of the
unfolding.  But V* is still a lot faster, so our code generation
should be improved.

I have not looked at why lxf and sf64 perform word on PAIRWISE-V* than
on NAIVE-V*.

- 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: <2026Oct8.175513@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

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: Pairwise dot product
Date: Thu, 08 Oct 2026 15:55:13 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 91
Message-ID: <2026Oct8.175513@mips.complang.tuwien.ac.at>
Injection-Date: Thu, 08 Oct 2026 17:00:34 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="49868"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18s5wCRMg/NcTL8IRQEnMdF"; posting-host="4440b373708cb595d29900b8ff51a766"
Cancel-Lock: sha1:1peFV9a2nYsFljXJGDsuZPQufVM= sha256:zj8xoukgar10LDoXAmID4rSmt/xYVlzY1xGkdYFEvv4=
	sha1:YZ3shWKX+DXoYIbtjNU1o41ia4Y= sha256:D0PB7OPkKaRfYPI23K7hH6M2l6IPWVN9VtOI2dtoOwM=
X-newsreader: xrn 10.11