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.

History
85 lines·2.5 KB·GAS AArch64 Raw Blame
1// memcpy.S: an AArch64 memcpy built from overlapping loads and Q-register pairs.
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..64 two Q-register pairs, one from each end
9// 65+ 64 bytes per iteration, then the last 64 from the end
10//
11// Uses only x0-x8 and v0-v3, 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
22 cmp x2, #32
24 cmp x2, #64
27 ldp q0, q1, [x1] // 33..64
28 ldp q2, q3, [x4, #-32]
29 stp q0, q1, [x0]
30 stp q2, q3, [x5, #-32]
31 ret
33.Lcopy17_32: // two 16-byte moves that meet in the middle
34 ldr q0, [x1]
35 ldr q1, [x4, #-16]
36 str q0, [x0]
37 str q1, [x5, #-16]
38 ret
40.Lcopy0_16:
41 cmp x2, #8
43 ldr x6, [x1] // 8..16
44 ldr x7, [x4, #-8]
45 str x6, [x0]
46 str x7, [x5, #-8]
47 ret
48.Lcopy0_8:
49 tbz x2, #2, .Lcopy0_4
50 ldr w6, [x1] // 4..7
51 ldr w7, [x4, #-4]
52 str w6, [x0]
53 str w7, [x5, #-4]
54 ret
55.Lcopy0_4:
56 cbz x2, .Lreturn // 1..3: first, middle and last byte
57 lsr x3, x2, #1
58 ldrb w6, [x1]
59 ldrb w7, [x4, #-1]
60 ldrb w8, [x1, x3]
61 strb w6, [x0]
62 strb w8, [x0, x3]
63 strb w7, [x5, #-1]
64.Lreturn:
65 ret
67.Lcopy_long:
68 mov x6, x0 // dst cursor
69 mov x7, x1 // src cursor
70 sub x8, x5, #64 // stop when 64 or fewer bytes remain
71.Lloop64:
72 ldp q0, q1, [x7]
73 ldp q2, q3, [x7, #32]
74 add x7, x7, #64
75 stp q0, q1, [x6]
76 stp q2, q3, [x6, #32]
77 add x6, x6, #64
78 cmp x6, x8
80 ldp q0, q1, [x4, #-64] // the last 64 bytes, from the end
81 ldp q2, q3, [x4, #-32]
82 stp q0, q1, [x5, #-64]
83 stp q2, q3, [x5, #-32]
84 ret