|
|
Message-ID: <20251208174940.949856-14-bill.roberts@arm.com>
Date: Mon, 8 Dec 2025 11:44:56 -0600
From: Bill Roberts <bill.roberts@....com>
To: musl@...ts.openwall.com
Cc: Bill Roberts <bill.roberts@....com>
Subject: [RFC 13/14] aarch64: rewrite memcpy routine in C using inline asm
Rewrite the AArch64 memcpy routine from assembly into
implementations using inline assembly.
This change eliminates the need for handwritten function prologues and
epilogues in memcpy.S, which simplifies maintenance and allows the compiler
to automatically insert architecture features such as BTI landing pads and
pointer authentication (PAC) sequences where applicable.
Moving to C also enables the compiler to manage register allocation,
stack usage, and ABI compliance automatically while keeping the low-level
behavior (bitmasks and register accesses) explicit and verifiable.
No functional changes intended.
Signed-off-by: Bill Roberts <bill.roberts@....com>
---
src/string/aarch64/memcpy.S | 186 ------------------------------------
src/string/aarch64/memcpy.c | 168 ++++++++++++++++++++++++++++++++
2 files changed, 168 insertions(+), 186 deletions(-)
delete mode 100644 src/string/aarch64/memcpy.S
create mode 100644 src/string/aarch64/memcpy.c
diff --git a/src/string/aarch64/memcpy.S b/src/string/aarch64/memcpy.S
deleted file mode 100644
index 48bb8a8d..00000000
--- a/src/string/aarch64/memcpy.S
+++ /dev/null
@@ -1,186 +0,0 @@
-/*
- * memcpy - copy memory area
- *
- * Copyright (c) 2012-2020, Arm Limited.
- * SPDX-License-Identifier: MIT
- */
-
-/* Assumptions:
- *
- * ARMv8-a, AArch64, unaligned accesses.
- *
- */
-
-#define dstin x0
-#define src x1
-#define count x2
-#define dst x3
-#define srcend x4
-#define dstend x5
-#define A_l x6
-#define A_lw w6
-#define A_h x7
-#define B_l x8
-#define B_lw w8
-#define B_h x9
-#define C_l x10
-#define C_lw w10
-#define C_h x11
-#define D_l x12
-#define D_h x13
-#define E_l x14
-#define E_h x15
-#define F_l x16
-#define F_h x17
-#define G_l count
-#define G_h dst
-#define H_l src
-#define H_h srcend
-#define tmp1 x14
-
-/* This implementation of memcpy uses unaligned accesses and branchless
- sequences to keep the code small, simple and improve performance.
-
- Copies are split into 3 main cases: small copies of up to 32 bytes, medium
- copies of up to 128 bytes, and large copies. The overhead of the overlap
- check is negligible since it is only required for large copies.
-
- Large copies use a software pipelined loop processing 64 bytes per iteration.
- The destination pointer is 16-byte aligned to minimize unaligned accesses.
- The loop tail is handled by always copying 64 bytes from the end.
-*/
-
-.global memcpy
-.type memcpy,%function
-memcpy:
- add srcend, src, count
- add dstend, dstin, count
- cmp count, 128
- b.hi .Lcopy_long
- cmp count, 32
- b.hi .Lcopy32_128
-
- /* Small copies: 0..32 bytes. */
- cmp count, 16
- b.lo .Lcopy16
- ldp A_l, A_h, [src]
- ldp D_l, D_h, [srcend, -16]
- stp A_l, A_h, [dstin]
- stp D_l, D_h, [dstend, -16]
- ret
-
- /* Copy 8-15 bytes. */
-.Lcopy16:
- tbz count, 3, .Lcopy8
- ldr A_l, [src]
- ldr A_h, [srcend, -8]
- str A_l, [dstin]
- str A_h, [dstend, -8]
- ret
-
- .p2align 3
- /* Copy 4-7 bytes. */
-.Lcopy8:
- tbz count, 2, .Lcopy4
- ldr A_lw, [src]
- ldr B_lw, [srcend, -4]
- str A_lw, [dstin]
- str B_lw, [dstend, -4]
- ret
-
- /* Copy 0..3 bytes using a branchless sequence. */
-.Lcopy4:
- cbz count, .Lcopy0
- lsr tmp1, count, 1
- ldrb A_lw, [src]
- ldrb C_lw, [srcend, -1]
- ldrb B_lw, [src, tmp1]
- strb A_lw, [dstin]
- strb B_lw, [dstin, tmp1]
- strb C_lw, [dstend, -1]
-.Lcopy0:
- ret
-
- .p2align 4
- /* Medium copies: 33..128 bytes. */
-.Lcopy32_128:
- ldp A_l, A_h, [src]
- ldp B_l, B_h, [src, 16]
- ldp C_l, C_h, [srcend, -32]
- ldp D_l, D_h, [srcend, -16]
- cmp count, 64
- b.hi .Lcopy128
- stp A_l, A_h, [dstin]
- stp B_l, B_h, [dstin, 16]
- stp C_l, C_h, [dstend, -32]
- stp D_l, D_h, [dstend, -16]
- ret
-
- .p2align 4
- /* Copy 65..128 bytes. */
-.Lcopy128:
- ldp E_l, E_h, [src, 32]
- ldp F_l, F_h, [src, 48]
- cmp count, 96
- b.ls .Lcopy96
- ldp G_l, G_h, [srcend, -64]
- ldp H_l, H_h, [srcend, -48]
- stp G_l, G_h, [dstend, -64]
- stp H_l, H_h, [dstend, -48]
-.Lcopy96:
- stp A_l, A_h, [dstin]
- stp B_l, B_h, [dstin, 16]
- stp E_l, E_h, [dstin, 32]
- stp F_l, F_h, [dstin, 48]
- stp C_l, C_h, [dstend, -32]
- stp D_l, D_h, [dstend, -16]
- ret
-
- .p2align 4
- /* Copy more than 128 bytes. */
-.Lcopy_long:
-
- /* Copy 16 bytes and then align dst to 16-byte alignment. */
-
- ldp D_l, D_h, [src]
- and tmp1, dstin, 15
- bic dst, dstin, 15
- sub src, src, tmp1
- add count, count, tmp1 /* Count is now 16 too large. */
- ldp A_l, A_h, [src, 16]
- stp D_l, D_h, [dstin]
- ldp B_l, B_h, [src, 32]
- ldp C_l, C_h, [src, 48]
- ldp D_l, D_h, [src, 64]!
- subs count, count, 128 + 16 /* Test and readjust count. */
- b.ls .Lcopy64_from_end
-
-.Lloop64:
- stp A_l, A_h, [dst, 16]
- ldp A_l, A_h, [src, 16]
- stp B_l, B_h, [dst, 32]
- ldp B_l, B_h, [src, 32]
- stp C_l, C_h, [dst, 48]
- ldp C_l, C_h, [src, 48]
- stp D_l, D_h, [dst, 64]!
- ldp D_l, D_h, [src, 64]!
- subs count, count, 64
- b.hi .Lloop64
-
- /* Write the last iteration and copy 64 bytes from the end. */
-.Lcopy64_from_end:
- ldp E_l, E_h, [srcend, -64]
- stp A_l, A_h, [dst, 16]
- ldp A_l, A_h, [srcend, -48]
- stp B_l, B_h, [dst, 32]
- ldp B_l, B_h, [srcend, -32]
- stp C_l, C_h, [dst, 48]
- ldp C_l, C_h, [srcend, -16]
- stp D_l, D_h, [dst, 64]
- stp E_l, E_h, [dstend, -64]
- stp A_l, A_h, [dstend, -48]
- stp B_l, B_h, [dstend, -32]
- stp C_l, C_h, [dstend, -16]
- ret
-
-.size memcpy,.-memcpy
diff --git a/src/string/aarch64/memcpy.c b/src/string/aarch64/memcpy.c
new file mode 100644
index 00000000..7ecf4cde
--- /dev/null
+++ b/src/string/aarch64/memcpy.c
@@ -0,0 +1,168 @@
+/*
+ * memcpy - copy memory area
+ *
+ * Copyright (c) 2012-2020, Arm Limited.
+ * SPDX-License-Identifier: MIT
+ */
+
+/* Assumptions:
+ *
+ * ARMv8-a, AArch64, unaligned accesses.
+ *
+ This implementation of memcpy uses unaligned accesses and branchless
+ sequences to keep the code small, simple and improve performance.
+
+ Copies are split into 3 main cases: small copies of up to 32 bytes, medium
+ copies of up to 128 bytes, and large copies. The overhead of the overlap
+ check is negligible since it is only required for large copies.
+
+ Large copies use a software pipelined loop processing 64 bytes per iteration.
+ The destination pointer is 16-byte aligned to minimize unaligned accesses.
+ The loop tail is handled by always copying 64 bytes from the end.
+*/
+
+#include <stddef.h>
+
+void *memcpy(void *dstin, const void *src, size_t count)
+{
+ __asm__ volatile(
+ // === begin original memcpy.S body, using x0/x1/x2 directly ===
+ "add x4, x1, x2\n\t"
+ "add x5, x0, x2\n\t"
+ "cmp x2, 128\n\t"
+ "b.hi .Lcopy_long\n\t"
+ "cmp x2, 32\n\t"
+ "b.hi .Lcopy32_128\n\t"
+
+ /* Small copies: 0..32 bytes. */
+ "cmp x2, 16\n\t"
+ "b.lo .Lcopy16\n\t"
+ "ldp x6, x7, [x1]\n\t"
+ "ldp x12, x13, [x4, -16]\n\t"
+ "stp x6, x7, [x0]\n\t"
+ "stp x12, x13, [x5, -16]\n\t"
+ "ret\n"
+
+ /* Copy 8-15 bytes. */
+ ".Lcopy16:\n\t"
+ "tbz x2, 3, .Lcopy8\n\t"
+ "ldr x6, [x1]\n\t"
+ "ldr x7, [x4, -8]\n\t"
+ "str x6, [x0]\n\t"
+ "str x7, [x5, -8]\n\t"
+ "ret\n\t"
+
+ ".p2align 3\n"
+ /* Copy 4-7 bytes. */
+ ".Lcopy8:\n\t"
+ "tbz x2, 2, .Lcopy4\n\t"
+ "ldr w6, [x1]\n\t"
+ "ldr w8, [x4, -4]\n\t"
+ "str w6, [x0]\n\t"
+ "str w8, [x5, -4]\n\t"
+ "ret\n"
+
+ /* Copy 0..3 bytes using a branchless sequence. */
+ ".Lcopy4:\n\t"
+ "cbz x2, .Lcopy0\n\t"
+ "lsr x14, x2, 1\n\t"
+ "ldrb w6, [x1]\n\t"
+ "ldrb w10, [x4, -1]\n\t"
+ "ldrb w8, [x1, x14]\n\t"
+ "strb w6, [x0]\n\t"
+ "strb w8, [x0, x14]\n\t"
+ "strb w10, [x5, -1]\n"
+ ".Lcopy0:\n\t"
+ "ret\n\t"
+
+ ".p2align 4\n"
+ /* Medium copies: 33..128 bytes. */
+ ".Lcopy32_128:\n\t"
+ "ldp x6, x7, [x1]\n\t"
+ "ldp x8, x9, [x1, 16]\n\t"
+ "ldp x10, x11, [x4, -32]\n\t"
+ "ldp x12, x13, [x4, -16]\n\t"
+ "cmp x2, 64\n\t"
+ "b.hi .Lcopy128\n\t"
+ "stp x6, x7, [x0]\n\t"
+ "stp x8, x9, [x0, 16]\n\t"
+ "stp x10, x11, [x5, -32]\n\t"
+ "stp x12, x13, [x5, -16]\n\t"
+ "ret\n\t"
+
+ ".p2align 4\n"
+ /* Copy 65..128 bytes. */
+ ".Lcopy128:\n\t"
+ "ldp x14, x15, [x1, 32]\n\t"
+ "ldp x16, x17, [x1, 48]\n\t"
+ "cmp x2, 96\n\t"
+ "b.ls .Lcopy96\n\t"
+ "ldp x2, x3, [x4, -64]\n\t" // G_l, G_h
+ "ldp x1, x4, [x4, -48]\n\t" // H_l, H_h (uses x1/x4 temporarily)
+ "stp x2, x3, [x5, -64]\n\t"
+ "stp x1, x4, [x5, -48]\n"
+ ".Lcopy96:\n\t"
+ "stp x6, x7, [x0]\n\t"
+ "stp x8, x9, [x0, 16]\n\t"
+ "stp x14, x15, [x0, 32]\n\t"
+ "stp x16, x17, [x0, 48]\n\t"
+ "stp x10, x11, [x5, -32]\n\t"
+ "stp x12, x13, [x5, -16]\n\t"
+ "ret\n\t"
+
+ ".p2align 4\n"
+ /* Copy more than 128 bytes. */
+ ".Lcopy_long:\n"
+ /* Copy 16 bytes and then align dst to 16-byte alignment. */
+ "ldp x12, x13, [x1]\n\t"
+ "and x14, x0, 15\n\t"
+ "bic x3, x0, 15\n\t" // dst = x3
+ "sub x1, x1, x14\n\t" // src -= tmp1
+ "add x2, x2, x14\n\t" // count += tmp1 (now 16 too large)
+ "ldp x6, x7, [x1, 16]\n\t"
+ "stp x12, x13, [x0]\n\t"
+ "ldp x8, x9, [x1, 32]\n\t"
+ "ldp x10, x11, [x1, 48]\n\t"
+ "ldp x12, x13, [x1, 64]!\n\t"
+ "subs x2, x2, 128 + 16\n\t"
+ "b.ls .Lcopy64_from_end\n"
+
+ ".Lloop64:\n\t"
+ "stp x6, x7, [x3, 16]\n\t"
+ "ldp x6, x7, [x1, 16]\n\t"
+ "stp x8, x9, [x3, 32]\n\t"
+ "ldp x8, x9, [x1, 32]\n\t"
+ "stp x10, x11, [x3, 48]\n\t"
+ "ldp x10, x11, [x1, 48]\n\t"
+ "stp x12, x13, [x3, 64]!\n\t"
+ "ldp x12, x13, [x1, 64]!\n\t"
+ "subs x2, x2, 64\n\t"
+ "b.hi .Lloop64\n"
+
+ /* Write the last iteration and copy 64 bytes from the end. */
+ ".Lcopy64_from_end:\n\t"
+ "ldp x14, x15, [x4, -64]\n\t"
+ "stp x6, x7, [x3, 16]\n\t"
+ "ldp x6, x7, [x4, -48]\n\t"
+ "stp x8, x9, [x3, 32]\n\t"
+ "ldp x8, x9, [x4, -32]\n\t"
+ "stp x10, x11, [x3, 48]\n\t"
+ "ldp x10, x11, [x4, -16]\n\t"
+ "stp x12, x13, [x3, 64]\n\t"
+ "stp x14, x15, [x5, -64]\n\t"
+ "stp x6, x7, [x5, -48]\n\t"
+ "stp x8, x9, [x5, -32]\n\t"
+ "stp x10, x11, [x5, -16]\n\t"
+ "ret\n\t"
+ :
+ :
+ : "x3","x4","x5",
+ "x6","x7","x8","x9",
+ "x10","x11","x12","x13",
+ "x14","x15","x16","x17",
+ "cc","memory"
+ );
+
+ __builtin_unreachable(); // we always return via the 'ret' in asm
+}
+
--
2.51.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.