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: align the destination before the long loop
Browse filesAbove 128 bytes, the first 16 bytes go out unaligned and the loop starts at the first 16-byte boundary in dst, so every store in the loop lands on one. The loop can now start up to 15 bytes in, so its first 64 bytes can end 79 bytes in, past the end of anything shorter. 65..128 gets a class of its own: four Q pairs, two from each end.
failing
1 step in 1 ms
1 file changed
+24−5
@@ -6,9 +6,11 @@
66 // 0..16 overlapping scalar loads from both ends, no loop
77 // 17..32 two Q registers, one from each end
88 // 33..64 two Q-register pairs, one from each end
9−// 65+ 64 bytes per iteration, then the last 64 from the end
9+// 65..128 four Q-register pairs, two from each end
10+// 129+ align dst to 16, copy 64 bytes per iteration, finish with the
11+// last 64 bytes from the end (they may overlap the loop's work)
1012 //
11−// Uses only x0-x8 and v0-v3, so nothing needs saving under AAPCS64.
13+// Uses only x0-x8 and v0-v7, so nothing needs saving under AAPCS64.
1214
1315 .text
1416 .p2align 6
@@ -22,9 +24,22 @@ memcpy_neon:
2224 cmp x2, #32
2325 b.ls .Lcopy17_32
2426 cmp x2, #64
27+ b.ls .Lcopy33_64
28+ cmp x2, #128
2529 b.hi .Lcopy_long
2630
27− ldp q0, q1, [x1] // 33..64
31+ ldp q0, q1, [x1] // 65..128
32+ ldp q4, q5, [x1, #32]
33+ ldp q6, q7, [x4, #-64]
34+ ldp q2, q3, [x4, #-32]
35+ stp q0, q1, [x0]
36+ stp q4, q5, [x0, #32]
37+ stp q6, q7, [x5, #-64]
38+ stp q2, q3, [x5, #-32]
39+ ret
40+
41+.Lcopy33_64:
42+ ldp q0, q1, [x1]
2843 ldp q2, q3, [x4, #-32]
2944 stp q0, q1, [x0]
3045 stp q2, q3, [x5, #-32]
@@ -65,8 +80,12 @@ memcpy_neon:
6580 ret
6681
6782 .Lcopy_long:
68− mov x6, x0 // dst cursor
69− mov x7, x1 // src cursor
83+ ldr q0, [x1] // first 16 bytes, unaligned
84+ str q0, [x0]
85+ neg x3, x0
86+ and x3, x3, #15 // bytes until dst is 16-byte aligned
87+ add x6, x0, x3 // aligned dst cursor
88+ add x7, x1, x3 // matching src cursor
7089 sub x8, x5, #64 // stop when 64 or fewer bytes remain
7190 .Lloop64:
7291 ldp q0, q1, [x7]