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.

Size test: every size from 0 to 1,024 at all 64 alignments

Browse files
Both routines, every size from 0 to 1,024 bytes, at every destination alignment from 0 to 63, with 0xEE guard bytes on both sides of the buffer: 131,200 calls. There is no libc for the target, so the test is freestanding and talks to Linux through svc. The build links it statically and runs it on Linux.

The build is red. 480 memset calls write past the end of the buffer, all of them between 65 and 79 bytes long. memcpy passes every call.
latchagentcommitted 1 parent c53541a commit 49199697141c9b2037b7087f14c49d70d93a2695
failing 4 steps in 2 ms
2 files changed +133−1
modified acompiled.toml +12−1
@@ -1,4 +1,5 @@
1−# Verified builds for neon-mem: both routines, assembled for AArch64 Linux.
1+# Verified builds for neon-mem. Both routines are assembled for AArch64, then
2+# the size test runs under Linux on an AArch64 CPU or qemu-aarch64.
23
34 [project]
45 isa = "arm64"
@@ -13,6 +14,16 @@ run = ["clang", "--target=aarch64-linux-gnu", "-c", "-g", "-o", "build/memcpy.o"
1314 name = "assemble memset"
1415 run = ["clang", "--target=aarch64-linux-gnu", "-c", "-g", "-o", "build/memset.o", "src/memset.S"]
1516
17+[[step]]
18+name = "link the size test"
19+run = ["clang", "--target=aarch64-linux-gnu", "-O2", "-ffreestanding", "-nostdlib", "-static", "-fuse-ld=lld", "-o", "build/sizes", "test/sizes.c", "build/memcpy.o", "build/memset.o"]
20+
21+[[step]]
22+name = "every size and alignment"
23+on = "linux"
24+run = ["./build/sizes"]
25+expect = "131200 calls, 0 failures"
26+
1627 [artifact]
1728 path = "build/memcpy.o"
1829 paths = ["build/memset.o"]
added test/sizes.c +121−0
@@ -0,0 +1,121 @@
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+
10+typedef unsigned long size_t;
11+typedef unsigned char u8;
12+
13+void *memcpy_neon(void *dst, const void *src, size_t n);
14+void *memset_neon(void *dst, int c, size_t n);
15+
16+#define MAX 1024
17+#define GUARD 64
18+
19+static u8 src[MAX + 2 * GUARD];
20+static u8 dst[MAX + 2 * GUARD + 64];
21+
22+static 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+
32+static 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+
40+static 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+
51+static int failures;
52+
53+static 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+
67+static 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+
81+static void guard(void)
82+{
83+ for (size_t i = 0; i < sizeof dst; i++)
84+ ((volatile u8 *)dst)[i] = 0xEE;
85+}
86+
87+static 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+}