🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #43

Re: Pairwise dot product

#43
From: Anton Ertl
Date: Fri, 09 Oct 2026 06:32
50 lines
3690 bytes
Paul Rubin <no.email@nospam.invalid> writes:
>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>> Not sure what you mean with "AVX intrinsics" in the context of Forth;
>
>Well, your v* primitive is written in C, so it could potentially use AVX
>vector operations.  _mm512_fmadd_pd to do pointwise multiply-adds into a
>YMM register and _mm512_reduce_add_pd to sum the pointwise results at
>the end, if I have it right.

You mean AVX "p"arrallel instructions rather than "s"calar
instructions used by the generated code, in order to vectorize the code.

Actually, if V* used unit stride and was compiled with -ffast-math
(which allows the compiler to pretend that the associative law holds
for FP addition and multiplication, among other things), gcc would
vectorize V* and, with -march=x86-64-v3 use the AVX instructions
corresponding to these intrinsics, so one gets the result without
using intrinsics.

But given that the stride can be anything, vectorizing V* is not
profitable, so gcc does not do it.  One could special-case unit stride
to get at least that case vectorized, but the benchmark I used does
not use unit stride.

Even in the vectorized case, the dependency between the FP additions
would be a problem; the compiler could unroll the loop and (thanks to
-ffast-math) reassociate the additions from

... f* f+ ... f* f+ ... f* f+

where every F+ depends on the one before to

... f* ... f* f+ ... f* f+ f+

i.e., only the last f+ depends on the previous iteration, while the
other two are in a new dependence chain.  And if unrolling by a factor
of 3 is not enough, use more unrolling until the loop execution time
is dominated by resource consumption rather than latency, but my
impression of the gcc- and clang-generated code is that the versions I
looked at are not doing that.

You can call the BLAS routine DDOT of libopenblas or some other
well-optimized BLAS library and probably get close to optimal code.

- 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: <2026Oct9.083242@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer04.iad!feed-me.highwinds-media.com!peer05.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <2026Oct8.175513@mips.complang.tuwien.ac.at> <87ld882afn.fsf@nightsong.com> <2026Oct9.002856@mips.complang.tuwien.ac.at> <87h5iv36p4.fsf@nightsong.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer04.iad!feed-me.highwinds-media.com!peer05.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!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: Fri, 09 Oct 2026 06:32:42 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 50
Message-ID: <2026Oct9.083242@mips.complang.tuwien.ac.at>
References: <2026Oct8.175513@mips.complang.tuwien.ac.at> <87ld882afn.fsf@nightsong.com> <2026Oct9.002856@mips.complang.tuwien.ac.at> <87h5iv36p4.fsf@nightsong.com>
Injection-Date: Fri, 09 Oct 2026 06:59:16 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="572498"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+sdXsiIS7iji4Qx7PfSz/s"; posting-host="e8d347a9092f3e963a34721c3f889ca2"
Cancel-Lock: sha1:LaXGetFwCS0I7ItDCBrfPvSFe+E= sha256:WLhKS4kSvecZmodE32uhkjfKDH50hfsp2umzP+FbbAM=
	sha1:f5f656DzCbRKeNuWYVmF0oGJXRA= sha256:VEM+LKohs/BqNUaxV6a+0IalnAV6ROLoTCUe3Td0aMg=
X-newsreader: xrn 10.11
X-Received-Bytes: 3490