[PATCH] riscv: Use page-bounded loads in RVV strlen

Chengrong Dai daichengrong@iscas.ac.cn
Mon Nov 23 08:41:09 GMT 2026


From: daichengrong <daichengrong@iscas.ac.cn>

Replace fault-only-first vector loads with ordinary byte loads bounded
by the current aligned 4096-byte region.  Linux RISC-V page boundaries
are aligned to this granule, so loads do not cross into an unreadable
page before the terminating NUL is found.

Pass the remaining region budget to vsetvli and use the actual returned
VL to advance the pointer and consume that budget.  Recompute the budget
at each region boundary.  An ordinary load does not shorten VL, so the
post-load read of the vl CSR is no longer needed.

Keep the existing e8/m2 configuration and the v2 mask register unchanged.
This avoids combining the loading change with an LMUL change.

On SG2044(VLEN=128) , the unchanged glibc bench-strlen covers 106 ordered length and
alignment cases (lengths 0 to 7680).  Run each library ten times, alternating
order on the same CPU, and compare per-case arithmetic mean times for
__strlen_vector.  All 106 cases were faster in this run.  The per-case
improvement medians, using (old_time/new_time - 1) * 100, were:

  Length       Cases   Median improvement
  0-16            27              118.55%
  17-128          16              284.89%
  129-1024        32              408.00%
  1025-7680       31              607.21%

Co-authored-by: Yuansheng <yuansheng@isrc.iscas.ac.cn>
Signed-off-by: daichengrong <daichengrong@iscas.ac.cn>
---
 sysdeps/riscv/rvv/strlen.S | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/sysdeps/riscv/rvv/strlen.S b/sysdeps/riscv/rvv/strlen.S
index 0a3b18eb0cfad66aafe80d64f5479b8f94465d7a..3b5fa2d1176ee6dcdc629dc8ae1946f3cbafd3c9 100644
--- a/sysdeps/riscv/rvv/strlen.S
+++ b/sysdeps/riscv/rvv/strlen.S
@@ -27,8 +27,10 @@
 #define str a0
 #define copy_str a1
 #define ivl a2
-#define cur_vl a2
 #define end_offset a3
+#define page_mask a4
+#define page_size a5
+#define remaining a6
 
 #define ELEM_LMUL_SETTING m2
 #define vstr v0
@@ -38,18 +40,32 @@ ENTRY (STRLEN)
 .option push
 .option arch, +v
     mv copy_str, str
+    li page_mask, 4095
+    li page_size, 4096
+
+    /* Linux RISC-V pages are at least 4096 bytes.  Restrict ordinary
+       vector loads to the current aligned 4096-byte region, so a NUL
+       before an unreadable page is always found without crossing it.  */
+L(page):
+    and remaining, copy_str, page_mask
+    sub remaining, page_size, remaining
 L(loop):
-    vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma
-    vle8ff.v vstr, (copy_str)
-    csrr cur_vl, vl
+    /* Use the actual VL returned by vsetvli.  An ordinary load does not
+       change VL, so no CSR readback is needed.  */
+    vsetvli ivl, remaining, e8, ELEM_LMUL_SETTING, ta, ma
+    vle8.v vstr, (copy_str)
     vmseq.vi vmask_end, vstr, 0
     vfirst.m end_offset, vmask_end
-    add copy_str, copy_str, cur_vl
-    bltz end_offset, L(loop)
+    bgez end_offset, L(found)
+
+    add copy_str, copy_str, ivl
+    sub remaining, remaining, ivl
+    bnez remaining, L(loop)
+    j L(page)
 
-    add str, str, cur_vl
-    add copy_str, copy_str, end_offset
+L(found):
     sub result, copy_str, result
+    add result, result, end_offset
 
     ret
 .option pop

base-commit: 30f5c72a422b271c6065eeecb941f6fa1b388e62
-- 
2.54.0 (Apple Git-157)



More information about the Libc-alpha mailing list