Skip to content

kernel/riscv64: fold the complex MAC into the accumulators in CTRMM - #6052

Open
6eanut wants to merge 1 commit into
OpenMathLib:developfrom
6eanut:riscv64-ctrmm-fold-mac-ln
Open

6eanut wants to merge 1 commit into
OpenMathLib:developfrom
6eanut:riscv64-ctrmm-fold-mac-ln

Conversation

@6eanut

@6eanut 6eanut commented Sep 17, 2026

Copy link
Copy Markdown

The CTRMM micro-kernel for ZVL256B built every complex product into a temporary and then added it to the accumulator, for each of the eight columns:

tmp = vfmul(Ai, Bi); tmp = VFMACC(tmp, Br, Ar); ACC = vfadd(ACC, tmp)

That is three vector instructions per accumulator and k-step. The two FMAs write the same destination, so applying them straight to ACC yields the same per-k-step sum for two instructions instead:

ACCr = VFMACC_RR( ACCr, Bi, Ai);
ACCr = VFMACC_RR( ACCr, Br, Ar);
ACCi = FOLD_OPA( ACCi, Br, Ai);
ACCi = FOLD_OPB( ACCi, Bi, Ar);

VFMACC_RR/VFMACC_RI already encode the +/- conjugation of the TRMM variant being compiled (LN/LT/RN/RT x UTRANSA/UCONJ), but the imaginary fold needs an op sequence with no existing macro, so it is spelled out per variant as FOLD_OPA/FOLD_OPB next to them.

Two FMAs compose to a plain accumulation because vfmsac(x,u,v)=u*v-x and vfmsac(vfmsac(a,u,v),w,y)=a+w*y-u*v, and likewise for vfmacc/vfnmsac. The value added per k-step is therefore unchanged and the per-column accumulation order is preserved. Only the FP rounding order differs, by a few ULP; measured against an exact double reference the patched kernel has the same error as the unpatched one.

Performance

Measured on a SpaceMiT X100 (2.2 GHz, single thread, ZVL256B), median of three runs: ctrmm +15.8% at 256, +8.8% at 512, +2.8% at 1024.

Testing

make tests returns 0; 125/125 utest and 1473/1473 extension tests pass; the CBLAS L1/L2/L3 suites show no new failures.

The CTRMM micro-kernel built every complex product into a temporary and
then added it to the accumulator, for each of the eight columns:

    tmp = vfmul(Ai, Bi); tmp = VFMACC(tmp, Br, Ar); ACC = vfadd(ACC, tmp)

Three vector instructions per accumulator and k-step.  The two FMAs
write the same destination, so applying them straight to ACC yields the
same per-k-step sum for two instructions instead:

    ACCr = VFMACC_RR( ACCr, Bi, Ai);
    ACCr = VFMACC_RR( ACCr, Br, Ar);
    ACCi = FOLD_OPA( ACCi, Br, Ai);
    ACCi = FOLD_OPB( ACCi, Bi, Ar);

VFMACC_RR/VFMACC_RI already encode the +/- conjugation of the TRMM
variant being compiled (LN/LT/RN/RT x UTRANSA/UCONJ), but the imaginary
fold needs an op sequence with no existing macro, so it is spelled out
per variant as FOLD_OPA/FOLD_OPB next to them.

Two FMAs compose to a plain accumulation because vfmsac(x,u,v)=u*v-x and
vfmsac(vfmsac(a,u,v),w,y)=a+w*y-u*v, and likewise for vfmacc/vfnmsac.
The value added per k-step is therefore unchanged and the per-column
accumulation order is preserved.  Only the FP rounding order differs, by
a few ULP; measured against an exact double reference the patched kernel
has the same error as the unpatched one.

Measured on a SpaceMiT X100 (2.2 GHz, single thread, ZVL256B), median of
three runs: ctrmm +15.8% at 256, +8.8% at 512, +2.8% at 1024.
Tests: make tests returns 0; 125/125 utest and 1473/1473 extension tests
pass; the CBLAS L1/L2/L3 suites show no new failures.

Co-authored-by: Yuansheng <yuansheng@isrc.iscas.ac.cn>
Co-authored-by: Ning Tian <tianning24@iscas.ac.cn>
Signed-off-by: jiakai xu <xujiakai2025@iscas.ac.cn>
@martin-frbg martin-frbg added this to the 0.3.35 milestone Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants