acompiled

The catalog contains seeded project examples. Your workspace stores your own projects and conversations.

yuki-ito/neon-mem

Public
Star6

memcpy and memset for AArch64, from overlapping loads and LDP/STP Q pairs. Every build checks every size up to 1,024 bytes at all 64 alignments.

memcpy: small sizes from overlapping loads

Browse files
No copy of 32 bytes or fewer runs a loop. 0..16 bytes move as two overlapping scalar loads and stores, one from each end, with 1..3 done as the first, middle and last byte. 17..32 move as two Q registers. Longer copies run 16 bytes at a time and finish with the last 16 bytes from the end.
yuki-itocommitted first commit commit 8729fd28b7b05cfc9b0cd875124cda04c1555251
failing 1 step in 1 ms
3 files changed +96−0
added README.md +13−0
@@ -0,0 +1,13 @@
1+# neon-mem
2+
3+`memcpy` for AArch64, written around one idea: never loop over a byte count you can cover with two overlapping loads and stores.
4+
5+| Size | memcpy |
6+|---|---|
7+| 0–3 | first, middle and last byte |
8+| 4–7 | two overlapping W moves |
9+| 8–16 | two overlapping X moves |
10+| 17–32 | two overlapping Q moves |
11+| 33+ | a 16-byte Q loop, then the last 16 from the end |
12+
13+`memset` comes next, built the same way.
added acompiled.toml +13−0
@@ -0,0 +1,13 @@
1+# Verified builds for neon-mem: memcpy_neon, assembled for AArch64 Linux.
2+
3+[project]
4+isa = "arm64"
5+assembler = "gas"
6+target = "AArch64 Linux, AAPCS64"
7+
8+[[step]]
9+name = "assemble memcpy"
10+run = ["clang", "--target=aarch64-linux-gnu", "-c", "-g", "-o", "build/memcpy.o", "src/memcpy.S"]
11+
12+[artifact]
13+path = "build/memcpy.o"
added src/memcpy.S +70−0
@@ -0,0 +1,70 @@
1+// memcpy.S: an AArch64 memcpy built from overlapping loads.
2+//
3+// void *memcpy_neon(void *dst, const void *src, size_t n);
4+//
5+// Size classes:
6+// 0..16 overlapping scalar loads from both ends, no loop
7+// 17..32 two Q registers, one from each end
8+// 33+ 16 bytes per iteration, then the last 16 from the end
9+//
10+// Uses only x0-x8 and v0-v1, so nothing needs saving under AAPCS64.
11+
12+ .text
13+ .p2align 6
14+ .global memcpy_neon
15+ .type memcpy_neon, %function
16+memcpy_neon:
17+ add x4, x1, x2 // src end
18+ add x5, x0, x2 // dst end
19+ cmp x2, #16
20+ b.ls .Lcopy0_16
21+ cmp x2, #32
22+ b.hi .Lcopy_long
23+
24+ // 17..32: two 16-byte moves that meet in the middle
25+ ldr q0, [x1]
26+ ldr q1, [x4, #-16]
27+ str q0, [x0]
28+ str q1, [x5, #-16]
29+ ret
30+
31+.Lcopy0_16:
32+ cmp x2, #8
33+ b.lo .Lcopy0_8
34+ ldr x6, [x1] // 8..16
35+ ldr x7, [x4, #-8]
36+ str x6, [x0]
37+ str x7, [x5, #-8]
38+ ret
39+.Lcopy0_8:
40+ tbz x2, #2, .Lcopy0_4
41+ ldr w6, [x1] // 4..7
42+ ldr w7, [x4, #-4]
43+ str w6, [x0]
44+ str w7, [x5, #-4]
45+ ret
46+.Lcopy0_4:
47+ cbz x2, .Lreturn // 1..3: first, middle and last byte
48+ lsr x3, x2, #1
49+ ldrb w6, [x1]
50+ ldrb w7, [x4, #-1]
51+ ldrb w8, [x1, x3]
52+ strb w6, [x0]
53+ strb w8, [x0, x3]
54+ strb w7, [x5, #-1]
55+.Lreturn:
56+ ret
57+
58+.Lcopy_long:
59+ mov x6, x0 // dst cursor
60+ mov x7, x1 // src cursor
61+ sub x8, x5, #16 // stop when 16 or fewer bytes remain
62+.Lloop16:
63+ ldr q0, [x7], #16
64+ str q0, [x6], #16
65+ cmp x6, x8
66+ b.lo .Lloop16
67+ ldr q0, [x4, #-16] // the last 16 bytes, from the end
68+ str q0, [x5, #-16]
69+ ret
70+ .size memcpy_neon, . - memcpy_neon