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: 64-byte loop of LDP/STP Q pairs

Browse files
Two LDP Q and two STP Q per iteration move 64 bytes, where the old loop moved 16. 33..64 bytes get a class of their own, one Q pair from each end, so the loop only starts above 64 bytes and its first iteration is always whole.
opcode-7agentcommitted 1 parent 8729fd2 commit fa86651d8bc6b58be5190c5f28fcc87fe9d7039b
failing 1 step in 0 ms
1 file changed +26−11
modified src/memcpy.S +26−11
@@ -1,13 +1,14 @@
1−// memcpy.S: an AArch64 memcpy built from overlapping loads.
1+// memcpy.S: an AArch64 memcpy built from overlapping loads and Q-register pairs.
22 //
33 // void *memcpy_neon(void *dst, const void *src, size_t n);
44 //
55 // Size classes:
66 // 0..16 overlapping scalar loads from both ends, no loop
77 // 17..32 two Q registers, one from each end
8−// 33+ 16 bytes per iteration, then the last 16 from the end
8+// 33..64 two Q-register pairs, one from each end
9+// 65+ 64 bytes per iteration, then the last 64 from the end
910 //
10−// Uses only x0-x8 and v0-v1, so nothing needs saving under AAPCS64.
11+// Uses only x0-x8 and v0-v3, so nothing needs saving under AAPCS64.
1112
1213 .text
1314 .p2align 6
@@ -19,9 +20,17 @@ memcpy_neon:
1920 cmp x2, #16
2021 b.ls .Lcopy0_16
2122 cmp x2, #32
23+ b.ls .Lcopy17_32
24+ cmp x2, #64
2225 b.hi .Lcopy_long
2326
24− // 17..32: two 16-byte moves that meet in the middle
27+ ldp q0, q1, [x1] // 33..64
28+ ldp q2, q3, [x4, #-32]
29+ stp q0, q1, [x0]
30+ stp q2, q3, [x5, #-32]
31+ ret
32+
33+.Lcopy17_32: // two 16-byte moves that meet in the middle
2534 ldr q0, [x1]
2635 ldr q1, [x4, #-16]
2736 str q0, [x0]
@@ -58,13 +67,19 @@ memcpy_neon:
5867 .Lcopy_long:
5968 mov x6, x0 // dst cursor
6069 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
70+ sub x8, x5, #64 // stop when 64 or fewer bytes remain
71+.Lloop64:
72+ ldp q0, q1, [x7]
73+ ldp q2, q3, [x7, #32]
74+ add x7, x7, #64
75+ stp q0, q1, [x6]
76+ stp q2, q3, [x6, #32]
77+ add x6, x6, #64
6578 cmp x6, x8
66− b.lo .Lloop16
67− ldr q0, [x4, #-16] // the last 16 bytes, from the end
68− str q0, [x5, #-16]
79+ b.lo .Lloop64
80+ ldp q0, q1, [x4, #-64] // the last 64 bytes, from the end
81+ ldp q2, q3, [x4, #-32]
82+ stp q0, q1, [x5, #-64]
83+ stp q2, q3, [x5, #-32]
6984 ret
7085 .size memcpy_neon, . - memcpy_neon