This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit d73efec9ea740f77e127fefeccbacfc47320296a
Author: ganjing <[email protected]>
AuthorDate: Fri Nov 20 19:48:59 2026 +0800

    libc/machine/arm/armv8-m: add MVE paths to memchr, strlen and strcpy.
    
    Add an __ARM_FEATURE_MVE path as the first branch of the three string
    functions, scanning 16 bytes per iteration with vminv. The existing
    NEON, DSP and word paths are kept for cores without MVE.
    
    memchr initializes the vminv accumulator to 0xff, because vminv returns
    min(Rda, min(Qm)) and a zero accumulator would report a match on every
    block. strlen and strcpy byte-scan the head until the source is 16-byte
    aligned, so a vector load never crosses a page boundary. A block holding
    the NUL is re-scanned byte by byte, so strcpy writes nothing past the
    terminator.
    
    The pointer-authentication prologue is factored into a pac_prologue
    macro shared by all variants, and the MVE returns authenticate with aut
    before returning.
    
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/machine/arm/armv8-m/arch_memchr.S | 125 ++++++++++++++++++++++++----
 libs/libc/machine/arm/armv8-m/arch_strcpy.S |  88 ++++++++++++++++++--
 libs/libc/machine/arm/armv8-m/arch_strlen.S |  82 ++++++++++++++++--
 3 files changed, 266 insertions(+), 29 deletions(-)

diff --git a/libs/libc/machine/arm/armv8-m/arch_memchr.S 
b/libs/libc/machine/arm/armv8-m/arch_memchr.S
index 018c56afb33..a0b940a93c7 100644
--- a/libs/libc/machine/arm/armv8-m/arch_memchr.S
+++ b/libs/libc/machine/arm/armv8-m/arch_memchr.S
@@ -94,8 +94,97 @@
 
 #include "acle-compat.h"
 
+/* Pointer-authentication prologue shared by all variants.  */
+       .macro pac_prologue
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       .endm
+
+#if defined(__ARM_FEATURE_MVE)
+
+/* MVE path: scan 16 bytes per iteration.  XOR each loaded vector with the
+ * replicated search byte (matching bytes become zero), then take the
+ * horizontal minimum.  VMINV computes min(Rda, min(Qm)), so Rda must be
+ * initialized to 0xFF (the largest unsigned byte) before the loop so the
+ * result reflects only the vector contents.
+ */
+
+#define src            r0
+#define ch             r1
+#define n              r2
+#define chunks         r3
+#define data           r4
+#define tail           r5
+
+       .thumb
+       .syntax unified
+       .global ARCH_LIBCFUN(memchr)
+       .thumb_func
+       .type ARCH_LIBCFUN(memchr), %function
+ARCH_LIBCFUN(memchr):
+       .cfi_sections .debug_frame
+       .cfi_startproc
+       pac_prologue
+       push    {r4, r5, lr}
+       .cfi_def_cfa_offset 12
+       .cfi_offset lr, -4
+       .cfi_offset r4, -12
+       .cfi_offset r5, -8
+
+       cbz     n, .Lmve_nf
+       and     ch, ch, #0xff
+       vdup.8  q0, ch
+       lsrs    chunks, n, #4           /* number of 16-byte chunks */
+       beq     .Lmve_tail
+       movs    data, #0xff             /* VMINV starting value: max unsigned 
byte */
+.Lmve_loop:
+       vldrb.8 q1, [src], #16
+       veor    q1, q1, q0              /* matching bytes become zero */
+       vminv.u8 data, q1               /* min(0xff, min(q1)) = 0 iff match */
+       cmp     data, #0
+       beq     .Lmve_found
+       subs    chunks, chunks, #1
+       bne     .Lmve_loop
+.Lmve_tail:
+       ands    tail, n, #15
+       beq     .Lmve_nf
+.Lmve_tail_loop:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       beq     .Lmve_found_ret
+       subs    tail, tail, #1
+       bne     .Lmve_tail_loop
+.Lmve_nf:
+       mov     src, #0
+       pop     {r4, r5, lr}
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       bx      lr
+.Lmve_found:
+       sub     src, src, #16           /* back to the start of the block */
+.Lmve_found_scan:
+       ldrb    data, [src], #1
+       cmp     data, ch
+       bne     .Lmve_found_scan
+.Lmve_found_ret:
+       sub     src, src, #1            /* src was post-incremented */
+       pop     {r4, r5, lr}
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       bx      lr
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(memchr), . - ARCH_LIBCFUN(memchr)
+
+#elif defined (__ARM_NEON__) || defined (__ARM_NEON)
 @ NOTE: This ifdef MUST match the one in memchr-stub.c
-#if defined (__ARM_NEON__) || defined (__ARM_NEON)
 #if __ARM_ARCH >= 8 && __ARM_ARCH_PROFILE == 'R'
        .arch   armv8-r
 #else
@@ -153,13 +242,7 @@
 ARCH_LIBCFUN(memchr):
        .cfi_sections .debug_frame
        .cfi_startproc
-#if __ARM_FEATURE_PAC_DEFAULT
-#  if __ARM_FEATURE_BTI_DEFAULT
-       pacbti  ip, lr, sp
-#  else
-       pac     ip, lr, sp
-#  endif /* __ARM_FEATURE_BTI_DEFAULT */
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
+       pac_prologue
        /* Use a simple loop if there are less than 8 bytes to search.  */
        cmp     cntin, #7
        bhi     .Llargestr
@@ -321,17 +404,13 @@ ARCH_LIBCFUN(memchr):
        .global ARCH_LIBCFUN(memchr)
        .type ARCH_LIBCFUN(memchr),%function
 ARCH_LIBCFUN(memchr):
+       .cfi_sections .debug_frame
+       .cfi_startproc
        @ r0 = start of memory to scan
        @ r1 = character to look for
        @ r2 = length
        @ returns r0 = pointer to character or NULL if not found
-#if __ARM_FEATURE_PAC_DEFAULT
-#  if __ARM_FEATURE_BTI_DEFAULT
-       pacbti  ip, lr, sp
-#  else
-       pac     ip, lr, sp
-#  endif /* __ARM_FEATURE_BTI_DEFAULT */
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
+       pac_prologue
        and     r1,r1,#0xff     @ Don't trust the caller to pass a char
 
        cmp     r2,#16          @ If short don't bother with anything clever
@@ -353,6 +432,11 @@ ARCH_LIBCFUN(memchr):
 10:
        @ We are aligned, we know we have at least 8 bytes to work with
        push    {r4,r5,r6,r7}
+       .cfi_def_cfa_offset 16
+       .cfi_offset r4, -16
+       .cfi_offset r5, -12
+       .cfi_offset r6, -8
+       .cfi_offset r7, -4
        orr     r1, r1, r1, lsl #8      @ expand the match word across all bytes
        orr     r1, r1, r1, lsl #16
        bic     r4, r2, #7      @ Number of double words to work with * 8
@@ -374,6 +458,11 @@ ARCH_LIBCFUN(memchr):
        bne     15b             @ (Flags from the subs above)
 
        pop     {r4,r5,r6,r7}
+       .cfi_def_cfa_offset 0
+       .cfi_restore r4
+       .cfi_restore r5
+       .cfi_restore r6
+       .cfi_restore r7
        and     r1,r1,#0xff     @ r1 back to a single character
        and     r2,r2,#7        @ Leave the count remaining as the number
                                @ after the double words have been done
@@ -425,11 +514,17 @@ ARCH_LIBCFUN(memchr):
 
 61:
        pop     {r4,r5,r6,r7}
+       .cfi_def_cfa_offset 0
+       .cfi_restore r4
+       .cfi_restore r5
+       .cfi_restore r6
+       .cfi_restore r7
        subs    r0,r0,#1
 #if __ARM_FEATURE_PAC_DEFAULT
        aut     ip, lr, sp
 #endif /* __ARM_FEATURE_PAC_DEFAULT */
        bx      lr
+       .cfi_endproc
 #else
   /* Defined in memchr-stub.c.  */
 #endif
diff --git a/libs/libc/machine/arm/armv8-m/arch_strcpy.S 
b/libs/libc/machine/arm/armv8-m/arch_strcpy.S
index 18ced75f3fc..7403e38d291 100644
--- a/libs/libc/machine/arm/armv8-m/arch_strcpy.S
+++ b/libs/libc/machine/arm/armv8-m/arch_strcpy.S
@@ -45,6 +45,85 @@
 #define tmp3          r6
 #define src_offset    r7
 
+/* Pointer-authentication prologue shared by both variants.  */
+       .macro pac_prologue
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       .endm
+
+#if defined(__ARM_FEATURE_MVE)
+
+/* MVE path: byte-copy the head until src is 16-byte aligned (copying and
+ * NUL-checking each byte), then copy one 16-byte block per iteration.  A
+ * block containing a NUL is re-copied byte-by-byte so nothing past the NUL
+ * is written.  dst is preserved for the return value.
+ */
+
+#define dst            r0              /* moving dst; start kept in r4 */
+#define src            r1
+#define data           r2
+#define tmp            r3
+#define start          r4
+
+       .syntax unified
+       .text
+       .align 2
+       .global ARCH_LIBCFUN(strcpy)
+       .thumb
+       .type ARCH_LIBCFUN(strcpy), %function
+
+ARCH_LIBCFUN(strcpy):
+       .cfi_sections .debug_frame
+       .cfi_startproc
+       pac_prologue
+       push    {r4, lr}
+       mov     start, dst
+
+.Lmve_head:
+       ldrb    data, [src]
+       strb    data, [dst]
+       cmp     data, #0
+       beq     .Lmve_ret
+       adds    src, src, #1
+       adds    dst, dst, #1
+       ands    tmp, src, #15
+       bne     .Lmve_head
+
+.Lmve_loop:
+       vldrb.8 q0, [src], #16
+       vminv.u8 data, q0               /* 0 iff a NUL is present */
+       cmp     data, #0
+       beq     .Lmve_found
+       vstrb.8 q0, [dst], #16
+       b       .Lmve_loop
+
+.Lmve_found:
+       subs    src, src, #16
+.Lmve_copy:
+       ldrb    data, [src], #1
+       strb    data, [dst], #1
+       cmp     data, #0
+       bne     .Lmve_copy
+
+.Lmve_ret:
+       mov     dst, start              /* return original dst */
+       pop     {r4, lr}
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       bx      lr
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strcpy), . - ARCH_LIBCFUN(strcpy)
+
+#else
+
+/* Byte masks used only by the non-MVE path below.  */
 #ifdef __ARM_BIG_ENDIAN
 #  define MASK_0          0xff000000
 #  define MASK_1          0xff0000
@@ -73,13 +152,7 @@
     .type   ARCH_LIBCFUN(strcpy), %function
 
 ARCH_LIBCFUN(strcpy):
-#if __ARM_FEATURE_PAC_DEFAULT
-#  if __ARM_FEATURE_BTI_DEFAULT
-    pacbti  ip, lr, sp
-#  else
-    pac     ip, lr, sp
-#  endif /* __ARM_FEATURE_BTI_DEFAULT */
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
+       pac_prologue
     push    {result, tmp1, tmp2, tmp3, src_offset}
     eor     tmp1, dst, src
     tst     tmp1, #3
@@ -328,5 +401,6 @@ offset_3:
 .cpy_done:
     *dst++ = 0;
 #endif  /* Pseudo code end */
+#endif /* __ARM_FEATURE_MVE */
 
 #endif
diff --git a/libs/libc/machine/arm/armv8-m/arch_strlen.S 
b/libs/libc/machine/arm/armv8-m/arch_strlen.S
index 0a5a20f24c1..16b24c4f62b 100644
--- a/libs/libc/machine/arm/armv8-m/arch_strlen.S
+++ b/libs/libc/machine/arm/armv8-m/arch_strlen.S
@@ -92,6 +92,78 @@
 #define S2HI           lsl
 #endif
 
+/* Pointer-authentication prologue shared by both variants.  */
+       .macro pac_prologue
+#if __ARM_FEATURE_PAC_DEFAULT
+#  if __ARM_FEATURE_BTI_DEFAULT
+       pacbti  ip, lr, sp
+#  else
+       pac     ip, lr, sp
+#  endif
+#endif
+       .endm
+
+#if defined(__ARM_FEATURE_MVE)
+
+/* MVE path: scan one 16-byte aligned block per iteration.  The head is
+ * scanned byte-by-byte until aligned (so the MVE loads never cross a page
+ * boundary); the horizontal minimum is zero iff a NUL is present.  On a
+ * hit the block is re-scanned byte-by-byte to locate the NUL.
+ */
+
+#define s              r0              /* start, kept for length */
+#define p              r1              /* moving pointer */
+#define data           r2
+#define tmp            r3
+
+       .thumb
+       .syntax unified
+       .global ARCH_LIBCFUN(strlen)
+       .thumb_func
+       .type ARCH_LIBCFUN(strlen), %function
+ARCH_LIBCFUN(strlen):
+       .cfi_sections .debug_frame
+       .cfi_startproc
+       pac_prologue
+       mov     p, s                    /* p = start of string */
+
+       /* Byte-scan the misaligned head until aligned or NUL.  */
+.Lmve_head:
+       ldrb    data, [p]
+       cmp     data, #0
+       beq     .Lmve_ret
+       adds    p, p, #1
+       ands    tmp, p, #15
+       bne     .Lmve_head
+
+.Lmve_loop:
+       vldrb.8 q0, [p], #16
+       vminv.u8 data, q0               /* 0 iff a NUL is present */
+       cmp     data, #0
+       beq     .Lmve_found
+       b       .Lmve_loop
+
+.Lmve_found:
+       subs    p, p, #16               /* back to the start of the block */
+.Lmve_block_scan:
+       ldrb    data, [p]
+       cmp     data, #0
+       beq     .Lmve_ret
+       adds    p, p, #1
+       b       .Lmve_block_scan
+
+.Lmve_ret:
+       subs    s, p, s                 /* length = NUL position - start */
+#if __ARM_FEATURE_PAC_DEFAULT
+       aut     ip, lr, sp
+#endif
+       bx      lr
+
+       .cfi_endproc
+       .size ARCH_LIBCFUN(strlen), . - ARCH_LIBCFUN(strlen)
+
+#else
+
        /* This code requires Thumb.  */
 #if __ARM_ARCH_PROFILE == 'M'
        .arch   armv7e-m
@@ -116,13 +188,7 @@
 #define tmp2           r5
 
 def_fn ARCH_LIBCFUN(strlen) p2align=6
-#if __ARM_FEATURE_PAC_DEFAULT
-#  if __ARM_FEATURE_BTI_DEFAULT
-       pacbti  ip, lr, sp
-#  else
-       pac     ip, lr, sp
-#  endif /* __ARM_FEATURE_BTI_DEFAULT */
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
+       pac_prologue
        pld     [srcin, #0]
        strd    r4, r5, [sp, #-8]!
        bic     src, srcin, #7
@@ -205,4 +271,6 @@ def_fn      ARCH_LIBCFUN(strlen) p2align=6
        b       .Lstart_realigned
        .size   ARCH_LIBCFUN(strlen), . - ARCH_LIBCFUN(strlen)
 
+#endif /* __ARM_FEATURE_MVE */
+
 #endif

Reply via email to