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.

Code
Blame·4 commits across 100 linesoldernewer
memcpy: 64-byte loop of LDP/STP Q pairsAug 2 1// memcpy.S: an AArch64 memcpy built from overlapping loads and Q-register pairs.
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
memcpy: test for the long path secondSep 9 7// 17..128 one to four Q-register pairs, also from both ends
memcpy: align the destination before the long loopAug 9 8// 129+ align dst to 16, copy 64 bytes per iteration, finish with the
9// last 64 bytes from the end (they may overlap the loop's work)
memcpy: align the destination before the long loopAug 9 11// Uses only x0-x8 and v0-v7, so nothing needs saving under AAPCS64.
13 .text
14 .p2align 6
15 .global memcpy_neon
16 .type memcpy_neon, %function
17memcpy_neon:
18 add x4, x1, x2 // src end
19 add x5, x0, x2 // dst end
20 cmp x2, #16
21 b.ls .Lcopy0_16
memcpy: test for the long path secondSep 9 27 // 17..32: two 16-byte moves that meet in the middle
29 ldr q1, [x4, #-16]
30 str q0, [x0]
31 str q1, [x5, #-16]
32 ret
33
34.Lcopy0_16:
35 cmp x2, #8
36 b.lo .Lcopy0_8
37 ldr x6, [x1] // 8..16
38 ldr x7, [x4, #-8]
39 str x6, [x0]
40 str x7, [x5, #-8]
41 ret
42.Lcopy0_8:
43 tbz x2, #2, .Lcopy0_4
44 ldr w6, [x1] // 4..7
45 ldr w7, [x4, #-4]
46 str w6, [x0]
47 str w7, [x5, #-4]
48 ret
49.Lcopy0_4:
50 cbz x2, .Lreturn // 1..3: first, middle and last byte
51 lsr x3, x2, #1
52 ldrb w6, [x1]
53 ldrb w7, [x4, #-1]
54 ldrb w8, [x1, x3]
55 strb w6, [x0]
56 strb w8, [x0, x3]
57 strb w7, [x5, #-1]
58.Lreturn:
59 ret
60
62 ldp q0, q1, [x1]
63 ldp q2, q3, [x4, #-32]
64 cmp x2, #64
66 stp q0, q1, [x0] // 33..64
67 stp q2, q3, [x5, #-32]
68 ret
69.Lcopy65_128:
70 ldp q4, q5, [x1, #32]
71 ldp q6, q7, [x4, #-64]
72 stp q0, q1, [x0]
73 stp q4, q5, [x0, #32]
74 stp q6, q7, [x5, #-64]
75 stp q2, q3, [x5, #-32]
76 ret
77
memcpy: align the destination before the long loopAug 9 79 ldr q0, [x1] // first 16 bytes, unaligned
80 str q0, [x0]
81 neg x3, x0
82 and x3, x3, #15 // bytes until dst is 16-byte aligned
83 add x6, x0, x3 // aligned dst cursor
84 add x7, x1, x3 // matching src cursor
memcpy: 64-byte loop of LDP/STP Q pairsAug 2 85 sub x8, x5, #64 // stop when 64 or fewer bytes remain
86.Lloop64:
87 ldp q0, q1, [x7]
88 ldp q2, q3, [x7, #32]
89 add x7, x7, #64
90 stp q0, q1, [x6]
91 stp q2, q3, [x6, #32]
92 add x6, x6, #64
95 ldp q0, q1, [x4, #-64] // the last 64 bytes, from the end
96 ldp q2, q3, [x4, #-32]
97 stp q0, q1, [x5, #-64]
98 stp q2, q3, [x5, #-32]