Skip to content

RISC-V: fix the strided loop of sum_vector.c (S/DSUM on RISCV64_ZVL256B) - #6176

Open
HiderX wants to merge 1 commit into
OpenMathLib:developfrom
HiderX:riscv-zvl256b-sum-stride
Open

HiderX wants to merge 1 commit into
OpenMathLib:developfrom
HiderX:riscv-zvl256b-sum-stride

Conversation

@HiderX

@HiderX HiderX commented Oct 26, 2026

Copy link
Copy Markdown

Summary

kernel/riscv64/sum_vector.c is the S/DSUM kernel of RISCV64_ZVL256B. In its unrolled loop for incx > 1 it never
advanced ix, and it tripled inc_xv on every iteration:

                                j += gvl * 2;
-                               inc_xv += inc_xv * 2;
+                               ix += inc_xv * 2;

For incx > 1 and n >= 2*vl, this summed the first block again on every iteration, plus elements further and further
past the end of x:

  • with incx = 2 or 3 at n = 128 and 129, cblas_dsum returned wrong results, e.g. −2.15 instead of 3.55;
  • longer vectors read outside the mapping, and cblas_ssum(1000, x, 2) and cblas_dsum(1000, x, 2) crashed with
    SIGSEGV.

The fix advances ix by two blocks, as zsum_vector.c already does. No other target uses this file: C910V and GENERIC
use ../arm/sum.c, and ZVL128B uses sum_rvv.c.

Testing

All runs were on a SpacemiT K3 (X100 core), GCC 15.2, RISCV64_ZVL256B.

  • OpenBLAS test suite (make TARGET=RISCV64_ZVL256B NO_FORTRAN=1 NO_LAPACK=1 NO_LAPACKE=1 USE_OPENMP=0 NUM_THREADS=8): identical to develop, with utest 141/141, extension tests 1479/1479 and every BLAS/CBLAS test PASSED.

  • A level-1 sweep through CBLAS, for all four precisions. It covers copy, swap, scal, axpy, axpby, rot, dot,
    asum, sum, i?amax and i?amin:

    • n from 0 to 1031, and increment pairs including negative ones;
    • each vector sits in a buffer with guard zones and sentinel gaps, which are checked after every call;
    • results are compared bitwise where exact, otherwise against a binary128 reference.
    crash failures
    develop ?SUM, n = 1000, incx = 2 ?SUM, incx > 1, n ≥ 2·vl
    this PR none 0 of 6486 cases

Disclosure: an AI coding agent (Anthropic Claude) prepared this patch, its validation runs and this description on
behalf of the account owner. All numbers above come from archived runs on the board.

The unrolled strided loop never advanced ix and tripled inc_xv on every
iteration ("inc_xv += inc_xv * 2"), so for incx > 1 and n >= 2*vl it summed
the first block repeatedly plus elements past the end of x. Results were
wrong, and for longer vectors the reads left the mapping (SIGSEGV for
cblas_ssum(1000, x, 2)). Advance ix by two blocks instead, as zsum_vector.c
does.

This branch has not been deployed

No deployments
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.

1 participant