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 bc6b2eaa30d3b32a708debac868a4690cb8e6afd Author: ganjing <[email protected]> AuthorDate: Fri Nov 20 19:31:39 2026 +0800 libc/machine/arm/armv8-r: speed up arch memmove. Rewrite the forward path to copy 16 bytes per iteration with NEON when available and a word at a time otherwise. The overlapping backward path stays a byte loop, so it does not regress against the generic C code. The previous implementation copied one byte per iteration in both directions. Signed-off-by: ganjing <[email protected]> --- libs/libc/machine/arm/armv8-r/arch_memmove.S | 125 +++++++++++++++++++-------- 1 file changed, 91 insertions(+), 34 deletions(-) diff --git a/libs/libc/machine/arm/armv8-r/arch_memmove.S b/libs/libc/machine/arm/armv8-r/arch_memmove.S index 918bee0ddd9..1b7b6e0cc03 100644 --- a/libs/libc/machine/arm/armv8-r/arch_memmove.S +++ b/libs/libc/machine/arm/armv8-r/arch_memmove.S @@ -33,40 +33,97 @@ #ifdef LIBC_BUILD_MEMMOVE - .thumb +/* Assumptions: + * + * ARMv7-A / ARMv7-R (ARM state), unaligned word accesses supported. + * Word-at-a-time copy: forward when dst <= src, backward when dst > src + * (overlapping). The previous implementation was a naive byte loop. + */ + +#define dst r0 +#define src r1 +#define n r2 +#define result r0 +#define wd r3 +#define cnt r4 +#define tail r5 +#define data r12 + .syntax unified - .global memmove - .type memmove, %function -memmove: - 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 -1: - ldrb r4, [r3, #-1]! - cmp r2, r3 - strb r4, [r1, #-1]! - bne 1b -2: - pop {r4} - 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} - bx lr - .size memmove, . - memmove + .text + .align 2 + .global ARCH_LIBCFUN(memmove) + .arm + .type ARCH_LIBCFUN(memmove), %function + +ARCH_LIBCFUN(memmove): + push {r4, r5, lr} + + cmp n, #0 + beq .Lret + + cmp dst, src + bcc .Lforward /* dst < src: forward */ + + /* dst >= src: overlap only when dst < src + n */ + add r3, src, n + cmp dst, r3 + bcs .Lforward /* dst >= src + n: no overlap */ + + /* Backward copy (dst > src, overlapping). Byte loop matches the + * generic C fallback so we never regress on the overlapping case. */ + add wd, dst, n + add src, src, n +.Lbw_loop: + ldrb data, [src, #-1]! + strb data, [wd, #-1]! + subs n, n, #1 + bne .Lbw_loop + b .Lret + +.Lforward: + mov wd, dst /* preserve dst for return value */ +#ifdef __ARM_NEON__ + /* NEON 16-byte block copy (d0/q0 is caller-saved scratch). */ + lsrs cnt, n, #4 /* number of 16-byte blocks */ + beq .Lfw_byte_tail +.Lfw_neon_loop: + vld1.8 {q0}, [src]! + vst1.8 {q0}, [wd]! + subs cnt, cnt, #1 + bne .Lfw_neon_loop + ands n, n, #15 /* remaining 0..15 bytes */ + beq .Lret +#else + 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 +#endif +.Lfw_byte_tail: + cmp n, #0 + beq .Lret + ldrb data, [src], #1 + strb data, [wd], #1 + subs n, n, #1 + bne .Lfw_byte_tail + +.Lret: + mov result, dst /* return original dst */ + pop {r4, r5, pc} + + .size ARCH_LIBCFUN(memmove), . - ARCH_LIBCFUN(memmove) #endif
