[PATCH] riscv: avoid vrgather in RVV memcmp mismatch path

Pincheng Wang <[email protected]> Fri, 29 May 2026 09:29:33 +0800
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
vfirst.m already returns the byte offset of the first mismatch in the
current vector chunk. Use that offset to reload the two differing bytes
with lbu instead of extracting them with vrgather.vx and vmv.x.s.

The vector gather path can be more expensive on some implementations and
also increases vector register pressure. This keeps the mismatch path
shorter while preserving the memcmp result.

Signed-off-by: Pincheng Wang <[email protected]>
---
 newlib/libc/machine/riscv/memcmp-asm.S | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/newlib/libc/machine/riscv/memcmp-asm.S b/newlib/libc/machine/riscv/memcmp-asm.S
index b05df9521..1cf1680e2 100644
--- a/newlib/libc/machine/riscv/memcmp-asm.S
+++ b/newlib/libc/machine/riscv/memcmp-asm.S
@@ -28,12 +28,10 @@ memcmp:
   li a0, 0
   ret
 .Lfound:
-  vrgather.vx v16, v0, a4
-  vrgather.vx v24, v8, a4
-  vmv.x.s a0, v16
-  vmv.x.s a4, v24
-  andi a0, a0, 0xff
-  andi a4, a4, 0xff
+  add a0, a0, a4
+  add a1, a1, a4
+  lbu a0, 0(a0)
+  lbu a4, 0(a1)
   sub a0, a0, a4
   ret
 .size memcmp, .-memcmp
-- 
2.39.5