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·1 commit across 121 linesoldernewer
Size test: every size from 0 to 1,024 at all 64 alignmentsAug 25 1// sizes.c: every size from 0 to 1024 at every destination alignment from 0
2// to 63, with guard bytes on both sides of the buffer.
3//
4// There is no libc for the target, so this is freestanding: it talks to
5// Linux with svc and checks every byte with its own loops.
6//
7// clang --target=aarch64-linux-gnu -O2 -ffreestanding -nostdlib -static
8// -fuse-ld=lld test/sizes.c build/memcpy.o build/memset.o
9
10typedef unsigned long size_t;
11typedef unsigned char u8;
12
13void *memcpy_neon(void *dst, const void *src, size_t n);
14void *memset_neon(void *dst, int c, size_t n);
15
16#define MAX 1024
17#define GUARD 64
18
19static u8 src[MAX + 2 * GUARD];
20static u8 dst[MAX + 2 * GUARD + 64];
21
22static long sys3(long nr, long a, long b, long c)
23{
24 register long x8 __asm__("x8") = nr;
25 register long x0 __asm__("x0") = a;
26 register long x1 __asm__("x1") = b;
27 register long x2 __asm__("x2") = c;
28 __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2) : "memory");
29 return x0;
30}
31
32static void say(const char *s)
33{
34 size_t n = 0;
35 while (s[n])
36 n++;
37 sys3(64, 1, (long)s, (long)n); // write(1, s, n)
38}
39
40static void say_number(unsigned long v)
41{
42 char text[24];
43 int i = sizeof text;
44 text[--i] = 0;
45 do
46 text[--i] = (char)('0' + v % 10);
47 while (v /= 10);
48 say(text + i);
49}
50
51static int failures;
52
53static void fail(const char *what, size_t n, size_t align, const char *how)
54{
55 if (failures++ >= 8)
56 return;
57 say(what);
58 say("(n=");
59 say_number(n);
60 say(", align=");
61 say_number(align);
62 say("): ");
63 say(how);
64 say("\n");
65}
66
67static void check(const char *what, size_t n, size_t align, const u8 *expect)
68{
69 size_t start = GUARD + align;
70 for (size_t i = 0; i < start; i++)
71 if (dst[i] != 0xEE)
72 return fail(what, n, align, "wrote before the buffer");
73 for (size_t i = 0; i < n; i++)
74 if (dst[start + i] != (expect ? expect[i] : 0x5A))
75 return fail(what, n, align, "wrong bytes");
76 for (size_t i = start + n; i < sizeof dst; i++)
77 if (dst[i] != 0xEE)
78 return fail(what, n, align, "wrote past the end");
79}
80
81static void guard(void)
82{
83 for (size_t i = 0; i < sizeof dst; i++)
84 ((volatile u8 *)dst)[i] = 0xEE;
85}
86
87static int run(void)
88{
89 struct { char sysname[65], nodename[65], release[65], version[65], machine[65], domain[65]; } uts;
90 for (size_t i = 0; i < sizeof src; i++)
91 src[i] = (u8)(i * 131 + 7);
92
93 for (size_t align = 0; align < 64; align++)
94 for (size_t n = 0; n <= MAX; n++) {
95 guard();
96 memcpy_neon(dst + GUARD + align, src + GUARD, n);
97 check("memcpy", n, align, src + GUARD);
98
99 guard();
100 memset_neon(dst + GUARD + align, 0x5A, n);
101 check("memset", n, align, 0);
102 }
103
104 say("sizes: ");
105 say_number(2 * 64 * (MAX + 1));
106 say(" calls, ");
107 say_number(failures);
108 say(" failures");
109 if (sys3(160, (long)&uts, 0, 0) == 0) { // uname
110 say(" on ");
111 say(uts.machine);
112 }
113 say("\n");
114 return failures != 0;
115}
116
117__attribute__((noreturn)) void _start(void)
118{
119 sys3(94, run(), 0, 0); // exit_group
120 __builtin_unreachable();
121}