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·3 commits across 104 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
7// 17..32 two Q registers, one from each end
memcpy: 64-byte loop of LDP/STP Q pairsAug 2 8// 33..64 two Q-register pairs, one from each end
memcpy: align the destination before the long loopAug 9 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)
memcpy: align the destination before the long loopAug 9 13// Uses only x0-x8 and v0-v7, so nothing needs saving under AAPCS64.
15 .text
16 .p2align 6
17 .global memcpy_neon
18 .type memcpy_neon, %function
19memcpy_neon:
20 add x4, x1, x2 // src end
21 add x5, x0, x2 // dst end
22 cmp x2, #16
23 b.ls .Lcopy0_16
24 cmp x2, #32
26 cmp x2, #64
28 cmp x2, #128
30
memcpy: align the destination before the long loopAug 9 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]
memcpy: 64-byte loop of LDP/STP Q pairsAug 2 43 ldp q2, q3, [x4, #-32]
44 stp q0, q1, [x0]
45 stp q2, q3, [x5, #-32]
46 ret
47
48.Lcopy17_32: // two 16-byte moves that meet in the middle
50 ldr q1, [x4, #-16]
51 str q0, [x0]
52 str q1, [x5, #-16]
53 ret
54
55.Lcopy0_16:
56 cmp x2, #8
57 b.lo .Lcopy0_8
58 ldr x6, [x1] // 8..16
59 ldr x7, [x4, #-8]
60 str x6, [x0]
61 str x7, [x5, #-8]
62 ret
63.Lcopy0_8:
64 tbz x2, #2, .Lcopy0_4
65 ldr w6, [x1] // 4..7
66 ldr w7, [x4, #-4]
67 str w6, [x0]
68 str w7, [x5, #-4]
69 ret
70.Lcopy0_4:
71 cbz x2, .Lreturn // 1..3: first, middle and last byte
72 lsr x3, x2, #1
73 ldrb w6, [x1]
74 ldrb w7, [x4, #-1]
75 ldrb w8, [x1, x3]
76 strb w6, [x0]
77 strb w8, [x0, x3]
78 strb w7, [x5, #-1]
79.Lreturn:
80 ret
81
82.Lcopy_long:
memcpy: align the destination before the long loopAug 9 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
memcpy: 64-byte loop of LDP/STP Q pairsAug 2 89 sub x8, x5, #64 // stop when 64 or fewer bytes remain
90.Lloop64:
91 ldp q0, q1, [x7]
92 ldp q2, q3, [x7, #32]
93 add x7, x7, #64
94 stp q0, q1, [x6]
95 stp q2, q3, [x6, #32]
96 add x6, x6, #64
99 ldp q0, q1, [x4, #-64] // the last 64 bytes, from the end
100 ldp q2, q3, [x4, #-32]
101 stp q0, q1, [x5, #-64]
102 stp q2, q3, [x5, #-32]