From 513c2618ecc8ef613936b516af4568bf6c2afa75 Mon Sep 17 00:00:00 2001 From: Yicheng Kang <3193997905@qq.com> Date: Sun, 25 Oct 2026 11:13:00 -0700 Subject: [PATCH 1/2] RISC-V: return Inf from nrm2_rvv when the input contains an infinity The scaled sum of squares divides by the running maximum. Once an infinite element becomes that maximum, inf/inf = NaN enters the sum and the result is NaN instead of +Inf (OpenBLAS's dnrm2_inf utest only covers an infinity among zeros, where the division is skipped). When the result is NaN, rescan the input: return a NaN element if there is one, +Inf otherwise. Finite inputs do not take this path. --- kernel/riscv64/nrm2_rvv.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kernel/riscv64/nrm2_rvv.c b/kernel/riscv64/nrm2_rvv.c index 472b1148eb..7009630da2 100644 --- a/kernel/riscv64/nrm2_rvv.c +++ b/kernel/riscv64/nrm2_rvv.c @@ -244,7 +244,15 @@ FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x) i += inc_x; } } - return(scale * sqrt(ssq)); + FLOAT norm = scale * sqrt(ssq); + if (inc_x > 0 && isnan(norm)) { + // An infinite element makes the scaled update compute inf/inf = NaN. The norm is +Inf + // then, unless the input itself contains a NaN. + for (i = 0; i < n; i++) + if (isnan(x[i * inc_x])) return x[i * inc_x]; + return INFINITY; + } + return norm; } From da4d081f49cc877a86dc84bff9486c6933bf984b Mon Sep 17 00:00:00 2001 From: Yicheng Kang <3193997905@qq.com> Date: Sun, 25 Oct 2026 11:13:00 -0700 Subject: [PATCH 2/2] RISC-V: use the RVV NRM2 kernel for RISCV64_ZVL256B nrm2_rvv.c (already used by RISCV64_ZVL128B and x280) is 23%/9%/8% faster for SNRM2 and 10%/6%/6% faster for DNRM2 than nrm2_vector.c on the SpacemiT X100 (n = 1024 / 32768 / 4M), and it propagates NaN inputs, which nrm2_vector.c can drop. --- kernel/riscv64/KERNEL.RISCV64_ZVL256B | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/riscv64/KERNEL.RISCV64_ZVL256B b/kernel/riscv64/KERNEL.RISCV64_ZVL256B index c96fac6fa3..6fc31a5fb8 100644 --- a/kernel/riscv64/KERNEL.RISCV64_ZVL256B +++ b/kernel/riscv64/KERNEL.RISCV64_ZVL256B @@ -56,8 +56,8 @@ CDOTKERNEL = zdot_vector.c ZDOTKERNEL = zdot_vector.c DSDOTKERNEL = ../generic/dot.c -SNRM2KERNEL = nrm2_vector.c -DNRM2KERNEL = nrm2_vector.c +SNRM2KERNEL = nrm2_rvv.c +DNRM2KERNEL = nrm2_rvv.c CNRM2KERNEL = znrm2_vector.c ZNRM2KERNEL = znrm2_vector.c