[01/17] riscv: add vectorized strlen
Commit Message
The vector implementation processes the string in vector-length chunks
using fault-only-first loads (vle8ff.v), which keeps a vector load from
ever crossing into an unmapped page, providing significant performance
improvements on RVV-capable hardware. Use conditional compilation to
fall back to the scalar implementation when __riscv_vector is not
available, maintaining compatibility with non-vector RISC-V systems.
Signed-off-by: Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn>
---
newlib/libc/machine/riscv/Makefile.inc | 1 +
newlib/libc/machine/riscv/strlen-asm.S | 25 +++++++++++++++++++++++++
newlib/libc/machine/riscv/strlen.c | 5 +++++
3 files changed, 31 insertions(+)
create mode 100644 newlib/libc/machine/riscv/strlen-asm.S
Comments
Hi Pincheng:
Could you use ENTRY/END macro to simplify the whole patch set?
https://sourceware.org/git/?p=newlib-cygwin.git;a=commit;h=a78440e35fddcf329740b5b290e503a34221fcaf
> +.option push
> +.option arch, +v
^^^ Drop this since we already guarded with __riscv_vector
> +.option pop
^^^ Drop this as well
Hi Kito,
On 2026/8/3 14:45, Kito Cheng wrote:
> Could you use ENTRY/END macro to simplify the whole patch set?
>
> https://sourceware.org/git/?p=newlib-cygwin.git;a=commit;h=a78440e35fddcf329740b5b290e503a34221fcaf
>
>> +.option push
>> +.option arch, +v
>
> ^^^ Drop this since we already guarded with __riscv_vector
>
>> +.option pop
>
> ^^^ Drop this as well
Thanks for the review, will apply these changes to the whole series and
submit v2 soon.
@@ -22,4 +22,5 @@ libc_a_SOURCES += \
%D%/stpcpy.c \
%D%/strcmp.S \
%D%/strcpy.c \
+ %D%/strlen-asm.S \
%D%/strlen.c
new file mode 100644
@@ -0,0 +1,25 @@
+#if defined(__riscv_vector) && __riscv_xlen == 64 && !defined(__OPTIMIZE_SIZE__) && !defined(PREFER_SIZE_OVER_SPEED)
+.text
+.option push
+.option arch, +v
+.global strlen
+.type strlen, @function
+strlen:
+ mv a1, a0
+.Lloop:
+ vsetvli a2, zero, e8, m2, ta, ma
+ vle8ff.v v0, (a1)
+ csrr a2, vl
+ vmseq.vi v2, v0, 0
+ vfirst.m a3, v2
+ add a1, a1, a2
+ bltz a3, .Lloop
+
+ add a0, a0, a2
+ add a1, a1, a3
+ sub a0, a1, a0
+
+ ret
+.size strlen, .-strlen
+.option pop
+#endif
@@ -9,6 +9,10 @@
http://www.opensource.org/licenses.
*/
+#if defined(__riscv_vector) && __riscv_xlen == 64 && !defined(__OPTIMIZE_SIZE__) && !defined(PREFER_SIZE_OVER_SPEED)
+/* strlen defined in strlen-asm.S */
+#else
+
#include <sys/types.h>
#include <string.h>
#include <stdint.h>
@@ -66,3 +70,4 @@ size_t strlen(const char *str)
#endif
#endif /* not PREFER_SIZE_OVER_SPEED */
}
+#endif /* not __riscv_vector */