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 74eb151239f446b807a5e5b84ee3f8509e984b7f
Author: ganjing <[email protected]>
AuthorDate: Fri Nov 20 19:31:39 2026 +0800

    libc/machine/arm/armv8-m: speed up arch memmove.
    
    Rewrite the forward path to use an MVE low-overhead loop copying 16
    bytes per iteration when available and a word at a time otherwise. The
    overlapping backward path copies a word at a time from the end. The
    previous implementation copied one byte per iteration in both directions.
    The scratch register moves from r12 to r6 so r12 keeps the PAC signature
    and is checked with aut on return.
    
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/machine/arm/armv8-m/arch_memmove.S | 130 ++++++++++++++++++++-------
 1 file changed, 97 insertions(+), 33 deletions(-)

diff --git a/libs/libc/machine/arm/armv8-m/arch_memmove.S 
b/libs/libc/machine/arm/armv8-m/arch_memmove.S
index 1240e4e0255..32002e1cbf0 100644
--- a/libs/libc/machine/arm/armv8-m/arch_memmove.S
+++ b/libs/libc/machine/arm/armv8-m/arch_memmove.S
@@ -33,6 +33,14 @@
 
 #ifdef LIBC_BUILD_MEMMOVE
 
+/* Assumptions:
+ *
+ * ARMv7-M / ARMv8-M (Thumb-2), unaligned word accesses supported.
+ * When __ARM_FEATURE_MVE is defined the forward (non-overlapping) path
+ * uses an MVE low-overhead byte loop; the backward (overlapping) path
+ * uses a word-at-a-time copy from the end.
+ */
+
 #ifndef __ARM_FEATURE_BTI_DEFAULT
 #  define __ARM_FEATURE_BTI_DEFAULT 0
 #endif
@@ -41,10 +49,20 @@
 #  define __ARM_FEATURE_PAC_DEFAULT 0
 #endif
 
+#define dst            r0
+#define src            r1
+#define n              r2
+#define result         r0              /* overlaps dst; dst is preserved */
+#define wd             r3              /* moving dst pointer (forward) */
+#define cnt            r4              /* word/byte count */
+#define tail           r5              /* trailing byte count */
+#define data           r6
+
        .thumb
        .syntax unified
        .global ARCH_LIBCFUN(memmove)
-       .type   ARCH_LIBCFUN(memmove), %function
+       .type ARCH_LIBCFUN(memmove), %function
+       .thumb_func
 ARCH_LIBCFUN(memmove):
        .cfi_sections .debug_frame
        .cfi_startproc
@@ -53,43 +71,89 @@ ARCH_LIBCFUN(memmove):
        pacbti  ip, lr, sp
 #  else
        pac     ip, lr, sp
-#  endif /* __ARM_FEATURE_BTI_DEFAULT */
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
-       cmp     r0, r1
-       push    {r4}
-       bls     3f
-       adds    r3, r1, r2
-       cmp     r0, r3
-       bcs     3f
-       adds    r1, r0, r2
-       cbz     r2, 2f
-       subs    r2, r3, r2
+#  endif
+#endif
+       push    {r4, r5, r6, lr}
+       .cfi_def_cfa_offset 16
+       .cfi_offset lr, -4
+       .cfi_offset r6, -8
+       .cfi_offset r5, -12
+       .cfi_offset r4, -16
+
+       cbz     n, .Lret
+
+       cmp     dst, src
+       bcc     .Lforward               /* dst < src: forward copy */
+       /* dst >= src: overlap only when dst < src + n */
+       adds    r3, src, n
+       cmp     dst, r3
+       bcs     .Lforward               /* dst >= src + n: no overlap, forward 
*/
+
+       /* Backward copy (dst > src, overlapping).  */
+       add     wd, dst, n              /* dst end */
+       add     src, src, n             /* src end */
+       lsrs    cnt, n, #2              /* word count */
+       lsls    tail, cnt, #2
+       subs    tail, n, tail           /* trailing bytes (n % 4) */
+.Lbw_byte_tail:
+       cbz     tail, .Lbw_word
+       ldrb    data, [src, #-1]!
+       strb    data, [wd, #-1]!
+       subs    tail, tail, #1
+       bne     .Lbw_byte_tail
+.Lbw_word:
+       cbz     cnt, .Lret
+       ldr     data, [src, #-4]!
+       str     data, [wd, #-4]!
+       subs    cnt, cnt, #1
+       bne     .Lbw_word
+       b       .Lret
+
+.Lforward:
+#ifdef __ARM_FEATURE_MVE
+       wlstp.8 lr, n, 2f
+       mov     wd, dst                 /* preserve dst for return value */
 1:
-       ldrb    r4, [r3, #-1]!
-       cmp     r2, r3
-       strb    r4, [r1, #-1]!
-       bne     1b
+       vldrb.8 q0, [src], #16
+       vstrb.8 q0, [wd], #16
+       letp    lr, 1b
 2:
-       pop     {r4}
-#if __ARM_FEATURE_PAC_DEFAULT
-       aut     ip, lr, sp
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
-       bx      lr
-3:
-       cmp     r2, #0
-       beq     2b
-       add     r2, r2, r1
-       subs    r3, r0, #1
-4:
-       ldrb    r4, [r1], #1
-       cmp     r2, r1
-       strb    r4, [r3, #1]!
-       bne     4b
-       pop     {r4}
+       b       .Lret
+#else
+       mov     wd, dst                 /* preserve dst for return value */
+       /* Align wd (dst) to 4 bytes; byte head.  */
+       ands    tail, wd, #3
+       beq     .Lfw_word_loop
+.Lfw_head:
+       ldrb    data, [src], #1
+       strb    data, [wd], #1
+       subs    n, n, #1
+       beq     .Lret
+       ands    tail, wd, #3
+       bne     .Lfw_head
+.Lfw_word_loop:
+       cmp     n, #4
+       blo     .Lfw_byte_tail
+       ldr     data, [src], #4
+       str     data, [wd], #4
+       subs    n, n, #4
+       b       .Lfw_word_loop
+.Lfw_byte_tail:
+       cbz     n, .Lret
+       ldrb    data, [src], #1
+       strb    data, [wd], #1
+       subs    n, n, #1
+       bne     .Lfw_byte_tail
+#endif
+
+.Lret:
+       mov     result, dst             /* return original dst */
+       pop     {r4, r5, r6, lr}
 #if __ARM_FEATURE_PAC_DEFAULT
        aut     ip, lr, sp
-#endif /* __ARM_FEATURE_PAC_DEFAULT */
+#endif
        bx      lr
+
        .cfi_endproc
        .size ARCH_LIBCFUN(memmove), . - ARCH_LIBCFUN(memmove)
 

Reply via email to