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·2 commits across 74 linesoldernewer
memset, built the same wayAug 18 1// memset.S: an AArch64 memset built the same way as memcpy_neon.
2//
3// void *memset_neon(void *dst, int c, size_t n);
4//
5// 0..15 scalar stores of the byte replicated across x6
6// 16..64 two to four overlapping Q stores
memset: a 65..128 class, like memcpy'sAug 26 7// 65..128 four Q-register pairs, two from each end
8// 129+ align to 16, 64 bytes per iteration, last 64 from the end
9//
10// The 65..128 class exists because a 64-byte loop starting at the first
11// aligned address can run past the end of a buffer shorter than 80 bytes.
13 .text
14 .p2align 6
15 .global memset_neon
16 .type memset_neon, %function
17memset_neon:
18 dup v0.16b, w1
19 add x5, x0, x2 // end
20 cmp x2, #16
21 b.lo .Lset0_15
22 cmp x2, #64
memset, built the same wayAug 18 24 str q0, [x0] // 16..64
25 str q0, [x5, #-16]
26 cmp x2, #32
27 b.ls .Lreturn
28 str q0, [x0, #16]
29 str q0, [x5, #-32]
30.Lreturn:
31 ret
32
33.Lset0_15:
34 fmov x6, d0 // the byte, eight times
35 tbz x2, #3, .Lset0_7
36 str x6, [x0] // 8..15
37 str x6, [x5, #-8]
38 ret
39.Lset0_7:
40 tbz x2, #2, .Lset0_3
41 str w6, [x0] // 4..7
42 str w6, [x5, #-4]
43 ret
44.Lset0_3:
45 cbz x2, .Lreturn
46 strb w6, [x0] // 1..3
47 tbz x2, #1, .Lreturn
48 strh w6, [x5, #-2]
49 ret
50
52 cmp x2, #128
53 b.hi .Lset_long
54 stp q0, q0, [x0] // 65..128
55 stp q0, q0, [x0, #32]
56 stp q0, q0, [x5, #-64]
57 stp q0, q0, [x5, #-32]
58 ret
59
memset, built the same wayAug 18 60.Lset_long:
61 str q0, [x0] // unaligned head
62 bic x6, x0, #15
63 add x6, x6, #16 // first aligned address past dst
64 sub x7, x5, #64
65.Lloop64:
66 stp q0, q0, [x6]
67 stp q0, q0, [x6, #32]
68 add x6, x6, #64
69 cmp x6, x7
70 b.lo .Lloop64
71 stp q0, q0, [x5, #-64] // the last 64 bytes, from the end
72 stp q0, q0, [x5, #-32]
73 ret