• Pairwise dot product

    From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Thu Oct 8 15:55:13 2026
    From Newsgroup: comp.lang.forth

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.forth on Thu Oct 8 13:40:12 2026
    From Newsgroup: comp.lang.forth

    anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
    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.

    1. I wonder if FMA is faster on Intel chips.

    2. A version using AVX intrinsics would be interesting to compare.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From peter@peter.noreply@tin.it to comp.lang.forth on Thu Oct 8 23:23:36 2026
    From Newsgroup: comp.lang.forth

    On Thu, 08 Oct 2026 15:55:13 GMT
    anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

    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

    I tested this on lxf64 running on a ryzen 9950x

    I got
    cycles instructions
    pairwise 789,294,432 3,628,952,994
    naive 2,510,536,091 6,512,423,204

    First run with naive I got 35,476,814,233 cycles and 54,012,383,854 instr.
    I saw that you checked for F*+ . I have that as a library call, obviously
    clang does not use a fma instruction for it but instead compiles a call
    to a libray function. Redefining it as f* f+ gave the above result.

    I would guess that lxf is limited by the use of the x87 fpus small stack
    But I need to confirm that

    BR
    Peter

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Thu Oct 8 21:37:25 2026
    From Newsgroup: comp.lang.forth

    peter <peter.noreply@tin.it> writes:
    I tested this on lxf64 running on a ryzen 9950x

    I got
    cycles instructions
    pairwise 789,294,432 3,628,952,994
    naive 2,510,536,091 6,512,423,204

    Nice!

    First run with naive I got 35,476,814,233 cycles and 54,012,383,854 instr.
    I saw that you checked for F*+ . I have that as a library call, obviously >clang does not use a fma instruction for it but instead compiles a call
    to a libray function.

    I expect that with the right flags (maybe -march=x86-64-v3 will work)
    it will use an fma instruction. Because these instructions were only
    added in ~2010, they are not generated normally; and because C's fma() guarantees that rounding only happens at the end (not between the f*
    and the f+), it has to do an expensive library function if there is no
    fma instruction available.

    I would guess that lxf is limited by the use of the x87 fpus small stack
    But I need to confirm that

    For the version I used in benchmarking, I saw lots of code for the
    data stack stuff, so either the FP stuff or working with ]] ... [[
    seems to throw the usually perfect code generation of lxf for
    straight-line code off track.

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Thu Oct 8 21:52:14 2026
    From Newsgroup: comp.lang.forth

    anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
    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

    I looked at the lxf and sf64 pairwise words, and found that they
    contained calls to some replacement words for primitives in Gforth.
    So I changed these replacements into macros. The result is as follows:

    gforth-fast gforth-fast-v3 lxf sf64 vfx64
    cycles:u
    1_539_448_712 2_039_584_263 v*
    6_089_465_716 6_543_141_374 3_219_098_562 4_712_813_408 3_424_493_885 st
    4_381_413_861 4_373_619_348 4_365_848_521 11_983_572_054 1_204_079_565 pw

    instructions:u
    3_618_136_380 3_117_823_273 v* 11_618_336_625 11_618_023_393 9_509_945_380 16_011_408_567 6_072_662_430 st 10_164_466_634 10_164_153_289 8_266_865_885 15_963_359_138 3_821_722_609 pw

    These changes help lxf pairwise a lot, but for SwiftForth they reduce
    the executed instructions, but increase the cycles.

    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.

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Thu Oct 8 22:28:56 2026
    From Newsgroup: comp.lang.forth

    Paul Rubin <no.email@nospam.invalid> writes:
    anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
    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.

    1. I wonder if FMA is faster on Intel chips.

    Faster than on Zen4? Possibly (see below). Faster than FP addition
    alone? Unlikely, given that FMA does everything FP addition does, and
    some more.

    Here are results from a Core Ultra 225 (Arrow Lake)

    gforth-fast gforth-fast-v3 lxf sf64 vfx64
    P-Core cycles:u
    1_039_649_686 1_039_520_612 v*
    5_039_839_015 5_540_303_178 4_063_130_987 5_031_130_010 2_523_537_177 st
    3_479_430_650 3_478_100_387 3_478_021_063 10_679_996_938 1_743_806_482 pw

    E-Core cycles:u
    1_041_859_303 1_041_516_357 v*
    5_041_787_120 5_041_877_231 2_052_327_326 3_535_626_061 2_033_871_643 st
    2_424_544_211 2_356_400_354 1_921_908_492 8_906_205_259 1_046_854_134 pw

    instructions:u
    3_611_438_372 3_610_952_144 9_510_897_187 16_011_061_008 6_072_681_908 v* 11_611_688_441 11_611_192_419 9_510_888_746 16_011_051_331 6_072_663_745 st 10_157_798_459 10_141_702_338 8_267_809_187 15_963_001_737 3_821_722_537 pw

    The lxf version used on this machine is 1.7-172-983.

    So on these cores FMA and FP addition take only two cycles of latency,
    which is very good. The E-Core beats the P-Core in cycles in
    relatively many cases; and even if you take the higher clock rate of
    the P-Core into account, the E-Core still beats it in time in a number
    of cases.

    2. A version using AVX intrinsics would be interesting to compare.

    Not sure what you mean with "AVX intrinsics" in the context of Forth;
    in the x86-64-v3 version of gforth-fast, every FP instruction is an
    AVX instruction. E.g., v*1 looks as follows:

    <v*1> 2dup 1->2
    $7FAA410750C0: mov %r8,0x0(%r13)
    $7FAA410750C4: sub $0x8,%r13
    $7FAA410750C8: mov 0x10(%r13),%r8
    $7FAA410750CC: mov 0x8(%r13),%r9
    <v*1+$8> f@ 2->1
    $7FAA410750D0: vmovsd %xmm15,(%r12)
    $7FAA410750D6: vmovsd (%r9),%xmm15
    $7FAA410750DB: sub $0x8,%r12
    <v*1+$10> f@ f* 1->1
    <v*1+$18> f* 1->1
    $7FAA410750DF: vmulsd (%r8),%xmm15,%xmm15
    $7FAA410750E4: mov 0x8(%r13),%r8
    $7FAA410750E8: add $0x8,%r13
    <v*1+$20> fourth 1->2
    $7FAA410750EC: mov 0x18(%r13),%r9
    <v*1+$28> under+ 2->1
    $7FAA410750F0: add %r9,0x8(%r13)
    <v*1+$30> third 1->2
    $7FAA410750F4: mov 0x10(%r13),%r9
    <v*1+$38> + 2->1
    $7FAA410750F8: add %r9,%r8
    <v*1+$40> ;s 1->1
    $7FAA410750FB: mov (%r14),%rbx
    $7FAA410750FE: add $0x8,%r14
    $7FAA41075102: mov (%rbx),%rax
    $7FAA41075105: jmp *%rax

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Fri Oct 9 01:51:41 2026
    From Newsgroup: comp.lang.forth

    On 10/8/2026 5:55 PM, Anton Ertl wrote:
    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

    [..]

    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

    [..]


    - anton

    For iForth on AMD Ryzen 7 5800X 8-Core Processor
    \ V* : 2,523,563,242 clock ticks
    \ naive-V* : 2,532,810,048 clock ticks
    \ pairwise-V* : 3,251,776,774 clock ticks

    Using recursion to effect compilation inside an interpreted definition
    ( gen-v* ) is a no-no for iForth because it perceives it as nested
    compilation (which is not supported yet). It is interesting that it
    works in so many other Forths.

    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.forth on Thu Oct 8 20:15:35 2026
    From Newsgroup: comp.lang.forth

    anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
    The E-Core beats the P-Core in cycles in relatively many cases; and
    even if you take the higher clock rate of the P-Core into account the
    E-Core still beats it in time in a number of cases.

    Interesting, heh.

    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.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Fri Oct 9 05:32:40 2026
    From Newsgroup: comp.lang.forth

    marcel hendrix <mhx@iae.nl> writes:
    For iForth on AMD Ryzen 7 5800X 8-Core Processor
    \ V* : 2,523,563,242 clock ticks
    \ naive-V* : 2,532,810,048 clock ticks
    \ pairwise-V* : 3,251,776,774 clock ticks

    Note that V* is just a call to NAIVE-V* on systems that do not have a
    builtin V*.

    Using recursion to effect compilation inside an interpreted definition
    ( gen-v* ) is a no-no for iForth because it perceives it as nested >compilation (which is not supported yet).

    It does not nest definitions, so it's unclear why it should be
    perceived as nested definitions.

    Are you sure it is that problem? The same technique is used in
    GEN-SUM in

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

    and that works on iForth (at least on the 5.1-mini version I have).
    For pairwise-fdot.4th iForth gave me some error that I did not
    investigate further; looking at it again, both

    iforth 'create testthis include /home/anton/gforth/compat/xlocals.fs include /home/anton/forth/pairwise-fdot.4th test bye'

    and

    iforth "create benchthis include /home/anton/gforth/compat/xlocals.fs include /home/anton/forth/pairwise-fdot.4th ' pairwise-v* bench bye"

    work, so maybe the change that I made in the meantime (some words
    generated by GEN-V* used to be normal colon definitions and are now
    macros) makes the difference.

    It is interesting that it
    works in so many other Forths.

    It does not surprise me at all. I see no reason why this should not
    work. Or, in other words, if it does not work on iForth, it is
    probably something that should be fixed in iForth.

    Here are the results with iForth, which works with the current
    version, on Zen4 (Ryzen 8700G):

    gf-f gf-f-v3 iForth lxf sf64 vfx64
    k cycles:u
    1_543_608 2_043_449 v*
    6_090_505 6_544_524 3_399_183 3_219_799 4_705_232 3_425_833 naive-v*
    4_355_047 4_360_980 3_850_466 4_368_398 11_998_171 1_208_370 pairwise-v*

    k instructions:u
    3_618_216 3_117_902 v*
    11_618_416 11_618_103 6_272_607 9_510_019 16_011_448 6_072_706 naive-v* 10_164_546 10_164_233 9_943_466 8_266_940 15_963_399 3_821_771 pairwise-v*

    Looking at iForth's code for VG*2, I see quite a bit of pushing and
    popping going on, so apparently something prevents the register
    allocator from working as well as it does on some other 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Fri Oct 9 06:32:42 2026
    From Newsgroup: comp.lang.forth

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Fri Oct 9 09:12:29 2026
    From Newsgroup: comp.lang.forth

    On 10/9/2026 7:32 AM, Anton Ertl wrote:
    [..]> It does not nest definitions, so it's unclear why it should be
    perceived as nested definitions.

    The latest iForth checks [ and ] for (not) increasing the code space
    in a way that night impact doing nested compilation. It gives a stack
    error that looks like a problem with CASE's branch generator . I will investigate further when I have time. Thanks for reporting.

    Are you sure it is that problem? The same technique is used in
    GEN-SUM in

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

    and that works on iForth (at least on the 5.1-mini version I have).
    For pairwise-fdot.4th iForth gave me some error that I did not
    investigate further; looking at it again, both

    iforth 'create testthis include /home/anton/gforth/compat/xlocals.fs include /home/anton/forth/pairwise-fdot.4th test bye'

    and

    iforth "create benchthis include /home/anton/gforth/compat/xlocals.fs include /home/anton/forth/pairwise-fdot.4th ' pairwise-v* bench bye"

    work, so maybe the change that I made in the meantime (some words
    generated by GEN-V* used to be normal colon definitions and are now
    macros) makes the difference.

    I made a note and will recheck that code with the newest iForth.
    Here are the results with iForth, which works with the current
    version, on Zen4 (Ryzen 8700G):

    gf-f gf-f-v3 iForth lxf sf64 vfx64
    k cycles:u
    1_543_608 2_043_449 v*
    6_090_505 6_544_524 3_399_183 3_219_799 4_705_232 3_425_833 naive-v*
    4_355_047 4_360_980 3_850_466 4_368_398 11_998_171 1_208_370 pairwise-v*

    The most interesting result is the very good result of Gforth's native
    v* and vfx64's surprising sprint in pairwise-v*. I will recheck when my
    9950 is operational.

    Looking at iForth's code for VG*2, I see quite a bit of pushing and
    popping going on, so apparently something prevents the register
    allocator from working as well as it does on some other code.
    I don't see any "VG*2" in https://www.complang.tuwien.ac.at/forth/programs/pairwise-fdot.4th ?

    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Fri Oct 9 07:20:16 2026
    From Newsgroup: comp.lang.forth

    marcel hendrix <mhx@iae.nl> writes:
    On 10/9/2026 7:32 AM, Anton Ertl wrote:
    [..]> It does not nest definitions, so it's unclear why it should be
    perceived as nested definitions.

    The latest iForth checks [ and ] for (not) increasing the code space
    in a way that night impact doing nested compilation. It gives a stack
    error that looks like a problem with CASE's branch generator . I will >investigate further when I have time. Thanks for reporting.

    There's a theory that generating code in interpretation state is
    non-standard. I think that this is a poor proxy for checking whether
    code is generated inside a colon definition. But anyway, if your
    system implements some check against that, one way to work around that
    is to make gen-v* immediate and replace the sequences "gen-v* ]" with
    "] gen-v*".

    gf-f gf-f-v3 iForth lxf sf64 vfx64
    k cycles:u
    1_543_608 2_043_449 v*
    6_090_505 6_544_524 3_399_183 3_219_799 4_705_232 3_425_833 naive-v*
    4_355_047 4_360_980 3_850_466 4_368_398 11_998_171 1_208_370 pairwise-v*

    The most interesting result is the very good result of Gforth's native
    v*

    The inner loop of that is:

    $563ABF4BD990: movsd (%rdi),%xmm0
    $563ABF4BD994: mulsd (%rdx),%xmm0
    $563ABF4BD998: add %rsi,%rdi
    $563ABF4BD99B: add %rcx,%rdx
    $563ABF4BD99E: addsd %xmm0,%xmm1
    $563ABF4BD9A2: sub $0x1,%r8
    $563ABF4BD9A6: jne 0x563abf4bd990

    Ideally, a Forth system compile NAIVE-V* to code that has a similar
    inner loop.

    and vfx64's surprising sprint in pairwise-v*.

    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 )

    Just to give you an idea of the "source code" after code generation,
    this is how Gforth decompiles v*2:

    : v*2
    2dup f@ f@ f* fourth under+ third +
    2dup f@ f@ f* fourth under+ third + f+ ;

    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.

    Looking at iForth's code for VG*2, I see quite a bit of pushing and
    popping going on, so apparently something prevents the register
    allocator from working as well as it does on some other code.
    I don't see any "VG*2" in >https://www.complang.tuwien.ac.at/forth/programs/pairwise-fdot.4th ?

    I meant V*2, sorry.

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Fri Oct 9 10:46:21 2026
    From Newsgroup: comp.lang.forth

    Strange. I changed fourth and third to what I suppose is worse code, but
    \ V* : 2,598,357,958 clock ticks
    \ naive-V* : 2,596,436,412 clock ticks
    \ pairwise-V* : 2,286,386,056 clock ticks

    Now pairwise-V* wins.

    fourth and third became:

    : fourth ( a b c d -- a b c d a ) >S >S OVER S> S> ROT ;
    : third ( a b c -- a b c a ) >S OVER S> SWAP ;

    ... which looks bad, but which generates

    FORTH> ' v*2 idis
    $01344740 : v*2
    $0134474A pop rbx
    $0134474B pop rdi
    $0134474C fld [rbx] qword
    $0134474E fld [rdi] qword
    $01344750 fmulp ST(1), ST
    $01344752 pop rax
    $01344753 pop rdx
    $01344754 fld [rbx rax*1] qword
    $01344757 fld [rdx rdi*1] qword
    $0134475A fmulp ST(1), ST
    $0134475C faddp ST(1), ST
    $0134475E fpush,
    $01344768 push rdx
    $01344769 push rax
    $0134476A lea rdi, [rdi rdx*2] qword
    $0134476E push rdi
    $0134476F lea rbx, [rbx rax*2] qword
    $01344773 push rbx
    $01344774 ;

    instead of

    $01341F80 : v*2
    $01341F8A pop rbx
    $01341F8B pop rdi
    $01341F8C fld [rbx] qword
    $01341F8E fld [rdi] qword
    $01341F90 fmulp ST(1), ST
    $01341F92 push rdi
    $01341F93 mov rcx, rbx
    $01341F96 mov rbx, [rsp #16 +] qword
    $01341F9B pop rdi
    $01341F9C lea rbx, [rbx rdi*1] qword
    $01341FA0 push rbx
    $01341FA1 push rcx
    $01341FA2 mov rbx, [rsp #16 +] qword
    $01341FA7 pop rdi
    $01341FA8 pop rax
    $01341FA9 fld [rdi rbx*1] qword
    $01341FAC fld [rax] qword
    $01341FAE fmulp ST(1), ST
    $01341FB0 push rax
    $01341FB1 lea rbx, [rdi rbx*1] qword
    $01341FB5 mov rcx, rbx
    $01341FB8 mov rbx, [rsp #16 +] qword
    $01341FBD pop rdi
    $01341FBE lea rbx, [rbx rdi*1] qword
    $01341FC2 push rbx
    $01341FC3 push rcx
    $01341FC4 mov rbx, [rsp #16 +] qword
    $01341FC9 pop rdi
    $01341FCA faddp ST(1), ST
    $01341FCC fpush,
    $01341FD6 lea rbx, [rdi rbx*1] qword
    $01341FDA push rbx
    $01341FDB ;

    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Fri Oct 9 10:37:52 2026
    From Newsgroup: comp.lang.forth

    marcel hendrix <mhx@iae.nl> writes:
    Strange. I changed fourth and third to what I suppose is worse code, but
    \ V* : 2,598,357,958 clock ticks
    \ naive-V* : 2,596,436,412 clock ticks
    \ pairwise-V* : 2,286,386,056 clock ticks

    Now pairwise-V* wins.

    fourth and third became:

    : fourth ( a b c d -- a b c d a ) >S >S OVER S> S> ROT ;
    : third ( a b c -- a b c a ) >S OVER S> SWAP ;

    ... which looks bad, but which generates

    FORTH> ' v*2 idis
    $01344740 : v*2
    $0134474A pop rbx
    $0134474B pop rdi
    $0134474C fld [rbx] qword
    $0134474E fld [rdi] qword
    $01344750 fmulp ST(1), ST
    $01344752 pop rax
    $01344753 pop rdx
    $01344754 fld [rbx rax*1] qword
    $01344757 fld [rdx rdi*1] qword
    $0134475A fmulp ST(1), ST
    $0134475C faddp ST(1), ST
    $0134475E fpush,
    $01344768 push rdx
    $01344769 push rax
    $0134476A lea rdi, [rdi rdx*2] qword
    $0134476E push rdi
    $0134476F lea rbx, [rbx rax*2] qword
    $01344773 push rbx
    $01344774 ;

    Nice. I tried these definitions of FOURTH and THIRD on the iforth I
    have installed, but the resulting code does not work, so I did not
    measure iforth on my system with these definitions of FOURTH and
    THIRD.

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From peter@peter.noreply@tin.it to comp.lang.forth on Fri Oct 9 21:15:31 2026
    From Newsgroup: comp.lang.forth

    On Thu, 08 Oct 2026 21:37:25 GMT
    anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

    peter <peter.noreply@tin.it> writes:
    I tested this on lxf64 running on a ryzen 9950x

    I got
    cycles instructions
    pairwise 789,294,432 3,628,952,994
    naive 2,510,536,091 6,512,423,204

    Nice!

    First run with naive I got 35,476,814,233 cycles and 54,012,383,854 instr. >I saw that you checked for F*+ . I have that as a library call, obviously >clang does not use a fma instruction for it but instead compiles a call
    to a libray function.

    I expect that with the right flags (maybe -march=x86-64-v3 will work)
    it will use an fma instruction. Because these instructions were only
    added in ~2010, they are not generated normally; and because C's fma() guarantees that rounding only happens at the end (not between the f*
    and the f+), it has to do an expensive library function if there is no
    fma instruction available.

    clang was actually generating the correct fma instruction. All the cycles
    were spent in calling the library function. There are 5 calls to reach
    the lone instruction. I need to find a more efficient setup to call
    external libraries. For io that was the main interest for the library
    this is not a problem, neither for long math functions. I will add a fma
    opcode to the VM. Doing a preliminary test does not show a speed increase, contrary it is a bit slower. The benefit would be in the higher precision.

    The results are
    cycles instructions
    fma 3,008,490,491 6,013,315,970
    f* f+ 2,510,275,518 6,512,423,192

    The inner loops become almost identical

    fma
    0x428FF1 C5FB100B vmovsd xmm1, qword ptr [rbx]
    0x428FF5 488B4500 mov rax, qword ptr [rbp]
    0x428FF9 C5FB1010 vmovsd xmm2, qword ptr [rax]
    0x428FFD C4E2F1A9D0 vfmadd213sd xmm2, xmm1, xmm0
    0x429002 498B442418 mov rax, qword ptr [r12+0x18]
    0x429007 48034500 add rax, qword ptr [rbp]
    0x42900B 49035C2408 add rbx, qword ptr [r12+0x8]
    0x429010 C5EB10C2 vmovsd xmm0, xmm2, xmm2
    0x429014 48894500 mov qword ptr [rbp], rax

    f* f+
    0x428D01 C5FB100B vmovsd xmm1, qword ptr [rbx]
    0x428D05 488B4500 mov rax, qword ptr [rbp]
    0x428D09 C5FB1010 vmovsd xmm2, qword ptr [rax]
    0x428D0D C5EB59D1 vmulsd xmm2, xmm2, xmm1
    0x428D11 C5EB58D0 vaddsd xmm2, xmm2, xmm0
    0x428D15 498B442418 mov rax, qword ptr [r12+0x18]
    0x428D1A 48034500 add rax, qword ptr [rbp]
    0x428D1E 49035C2408 add rbx, qword ptr [r12+0x8]
    0x428D23 C5EB10C2 vmovsd xmm0, xmm2, xmm2
    0x428D27 48894500 mov qword ptr [rbp], rax

    BR
    Peter


    I would guess that lxf is limited by the use of the x87 fpus small stack >But I need to confirm that

    For the version I used in benchmarking, I saw lots of code for the
    data stack stuff, so either the FP stuff or working with ]] ... [[
    seems to throw the usually perfect code generation of lxf for
    straight-line code off track.

    - anton


    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.forth on Fri Oct 9 15:33:11 2026
    From Newsgroup: comp.lang.forth

    anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
    You mean AVX "p"arrallel instructions rather than "s"calar

    Yes, maybe I had too narrow a view of what AVX does. I thought of it
    mostly as a vector extension, per its name.

    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.

    Maybe it would also benefit at stride=2 or (especially in single
    precision) stride=4. I didn't realize stride!=1 was commonly used for dot-product-like operations.

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

    Yes, even better, if there's a suitable FFI. Next thing after that
    would be CUDA...
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Sat Oct 10 15:42:19 2026
    From Newsgroup: comp.lang.forth

    On 10/9/2026 12:37 PM, Anton Ertl wrote:
    marcel hendrix <mhx@iae.nl> writes:
    Strange. I changed fourth and third to what I suppose is worse code, but
    \ V* : 2,598,357,958 clock ticks
    \ naive-V* : 2,596,436,412 clock ticks
    \ pairwise-V* : 2,286,386,056 clock ticks

    Now pairwise-V* wins.

    fourth and third became:

    : fourth ( a b c d -- a b c d a ) >S >S OVER S> S> ROT ;
    : third ( a b c -- a b c a ) >S OVER S> SWAP ;

    ... which looks bad, but which generates

    FORTH> ' v*2 idis
    $01344740 : v*2
    $0134474A pop rbx
    $0134474B pop rdi
    $0134474C fld [rbx] qword
    $0134474E fld [rdi] qword
    $01344750 fmulp ST(1), ST
    $01344752 pop rax
    $01344753 pop rdx
    $01344754 fld [rbx rax*1] qword
    $01344757 fld [rdx rdi*1] qword
    $0134475A fmulp ST(1), ST
    $0134475C faddp ST(1), ST
    $0134475E fpush,
    $01344768 push rdx
    $01344769 push rax
    $0134476A lea rdi, [rdi rdx*2] qword
    $0134476E push rdi
    $0134476F lea rbx, [rbx rax*2] qword
    $01344773 push rbx
    $01344774 ;

    [..]

    I tried it specialized to a stride of (fixed) 1, which is the stride I normally need. There is both good and bad news:

    FORTH> ' v*2 idis
    $013446C0 : v*2
    $013446CA pop rbx
    $013446CB pop rdi
    $013446CC fld [rdi] qword
    $013446CE fld [rbx] qword
    $013446D0 fmulp ST(1), ST
    $013446D2 fld [rdi 8 +] qword
    $013446D5 fld [rbx 8 +] qword
    $013446D8 fmulp ST(1), ST
    $013446DA faddp ST(1), ST
    $013446DC fpush,
    $013446E6 lea rdi, [rdi #16 +] qword
    $013446EA push rdi
    $013446EB lea rbx, [rbx #16 +] qword
    $013446EF push rbx
    $013446F0 ;

    Somewhat nicer code.

    FORTH> BENCH2
    \ V* : 2,626,824,105 clock ticks
    \ naive-V* : 2,616,912,602 clock ticks
    \ pairwise-V* : 2,275,658,158 clock ticks ok

    ( As V* and naive-V* should give the same result, there is about 0.5% of
    clock jitter going on. I did let the caches warm up for a few seconds now. )

    Compared to the previous results, with variable stride,

    \ V* : 2,598,357,958 clock ticks
    \ naive-V* : 2,596,436,412 clock ticks
    \ pairwise-V* : 2,286,386,056 clock ticks

    It appears the V* code is about 1% slower (!) but pair-wise-V* has
    become 1% faster. Decreasing the stack pressure does not help, the pain
    is somewhere else.

    It will be interesting to test this on the next generation of CPUs.

    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sat Oct 10 16:08:35 2026
    From Newsgroup: comp.lang.forth

    marcel hendrix <mhx@iae.nl> writes:
    FORTH> BENCH2
    \ V* : 2,626,824,105 clock ticks
    \ naive-V* : 2,616,912,602 clock ticks
    \ pairwise-V* : 2,275,658,158 clock ticks ok

    ( As V* and naive-V* should give the same result, there is about 0.5% of >clock jitter going on. I did let the caches warm up for a few seconds now. )

    Compared to the previous results, with variable stride,

    \ V* : 2,598,357,958 clock ticks
    \ naive-V* : 2,596,436,412 clock ticks
    \ pairwise-V* : 2,286,386,056 clock ticks

    It appears the V* code is about 1% slower (!) but pair-wise-V* has
    become 1% faster. Decreasing the stack pressure does not help, the pain
    is somewhere else.

    For NAIVE-V* the bottleneck is probably the latency of the faddp,
    which appears to be around 5 cycles on the processor you use (the
    precision of computation may also play a role).

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Sat Oct 10 21:07:50 2026
    From Newsgroup: comp.lang.forth

    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.)
    With VFX64 5.43, was FPSYSTEM value == 1?

    - anton

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sun Oct 11 08:04:14 2026
    From Newsgroup: comp.lang.forth

    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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sun Oct 11 15:01:30 2026
    From Newsgroup: comp.lang.forth

    Paul Rubin <no.email@nospam.invalid> writes:
    anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
    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.

    Maybe it would also benefit at stride=2 or (especially in single
    precision) stride=4.

    Probably not with AVX, but AVX512 has control over which lanes are
    active, and special-casing for other strides may be profitable for
    that, too.

    I didn't realize stride!=1 was commonly used for
    dot-product-like operations.

    In the "normal" matrix multiplication algorithm that uses the
    dot-product as component, one of the vectors strides across rows and
    the other across columns. So one of them is certainly not 1. But
    nobody in his right mind uses that algorithm directly.

    - 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
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From peter@peter.noreply@tin.it to comp.lang.forth on Mon Oct 12 11:11:48 2026
    From Newsgroup: comp.lang.forth

    On Fri, 09 Oct 2026 06:32:42 GMT
    anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

    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

    I remembered that when you compared pairwise sum some time back that
    I tested some avx-512 variants with great results. I got interested
    again and did some avx-512 variants of fdot. I had to change the
    example to use stride=1 and 2 separate vectors instead of 2 interleaved
    as in your examples. The calling interface got also simpler
    ( v1-addr v2-addr v-len -- r: fdot)

    The result was that using avx-512 got almost the expected 8 times
    improvement. Further unrolling did not show significant gains.
    All tests run on a zen5 ryzen 9950x

    Here are the results, same test data, same number of iterations.

    fdot1 fdot8 fdot16 fdot32
    cycles 1,004,957,412 155,618,582 138,800,039 148,334,303 instructions 3,504,229,271 441,949,102 317,079,085 254,819,091

    fdot1 uses xmm registers 1 double at a time
    fdot8 uses zmm registers 8 doubles at a time
    fdot16 uses zmm unrolled 2 times
    fdot32 uses zmm unrolled 4 times

    The actual vector multiplication uses them in sequence
    : V*1 fdot1 ;
    : V*8 fdot8 fdot1 f+ ;
    : V*16 fdot16 v*8 f+ ;
    : V*32 fdot32 v*16 f+ ;

    I run the benchmark in wsl under windows 11
    as they run very quick, around 30ms, the timings are not that
    stable and the CPU reaches only 4.5Ghz
    Increasing the number of 10x gives more stable output
    and a CPU that reaches 5.3Ghz.

    I found this blog post that explains the zen5 improvements interesting https://www.numberworld.org/blogs/2024_8_7_zen5_avx512_teardown/

    Below is the disassembly of the fdot16. As lxf64 do not generate avx-512
    code I coded them in assembler directly.

    seea fdot16
    0x428B10 C4C17B1145F8 vmovsd qword ptr [r13-0x8], xmm0
    0x428B16 4D8D6DF8 lea r13, [r13-0x8]
    0x428B1A 62F1FD4857C0 vxorpd zmm0, zmm0, zmm0
    0x428B20 488B4D00 mov rcx, qword ptr [rbp]
    0x428B24 488B5508 mov rdx, qword ptr [rbp+0x8]
    0x428B28 4809DB or rbx, rbx
    0x428B2B 7464 jz 0x428B91
    0x428B2D 4889D8 mov rax, rbx
    0x428B30 4883E0F0 and rax, 0xFFFFFFFFFFFFFFF0
    0x428B34 4829C3 sub rbx, rax
    0x428B37 62F1FD481009 vmovupd zmm1, zmmword ptr [rcx]
    0x428B3D 62F1FD48105101 vmovupd zmm2, zmmword ptr [rcx+0x40]
    0x428B44 62F1F548590A vmulpd zmm1, zmm1, zmmword ptr [rdx]
    0x428B4A 62F1ED48595201 vmulpd zmm2, zmm2, zmmword ptr [rdx+0x40]
    0x428B51 4881C180000000 add rcx, 0x80
    0x428B58 4881C280000000 add rdx, 0x80
    0x428B5F 62F1F54858CA vaddpd zmm1, zmm1, zmm2
    0x428B65 62F1FD4858C1 vaddpd zmm0, zmm0, zmm1
    0x428B6B 4883E810 sub rax, 0x10
    0x428B6F 75C6 jnz 0x428B37
    0x428B71 62F3FD481BC101 vextractf64x4 ymm1, zmm0, 0x1
    0x428B78 C5F558D0 vaddpd ymm2, ymm1, ymm0
    0x428B7C 62F3FD2819D301 vextractf64x2 xmm3, ymm2, 0x1
    0x428B83 C5E558E2 vaddpd ymm4, ymm3, ymm2
    0x428B87 C4E37905EC01 vpermilpd xmm5, xmm4, 0x1
    0x428B8D C5DB58C5 vaddsd xmm0, xmm4, xmm5
    0x428B91 48894D00 mov qword ptr [rbp], rcx
    0x428B95 48895508 mov qword ptr [rbp+0x8], rdx
    0x428B99 C5F877 vzeroupper
    0x428B9C C3 ret
    141 bytes, 30 instructions

    BR
    Peter

    --- Synchronet 3.22a-Linux NewsLink 1.2