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.
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.
12 .text
13 .p2align 6
14 .global memcpy_neon
15 .type memcpy_neon, %function
16memcpy_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
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
31.Lcopy0_16:
32 cmp x2, #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:
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:
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
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
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