1 /*
2  * memcpy test.
3  *
4  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5  * See https://llvm.org/LICENSE.txt for license information.
6  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7  */
8 
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "stringlib.h"
14 
15 static const struct fun
16 {
17 	const char *name;
18 	void *(*fun)(void *, const void *, size_t);
19 } funtab[] = {
20 #define F(x) {#x, x},
21 F(memcpy)
22 #if __aarch64__
23 F(__memcpy_aarch64)
24 # if __ARM_NEON
25 F(__memcpy_aarch64_simd)
26 # endif
27 #elif __arm__
28 F(__memcpy_arm)
29 #endif
30 #undef F
31 	{0, 0}
32 };
33 
34 static int test_status;
35 #define ERR(...) (test_status=1, printf(__VA_ARGS__))
36 
37 #define A 32
38 #define LEN 250000
39 static unsigned char dbuf[LEN+2*A];
40 static unsigned char sbuf[LEN+2*A];
41 static unsigned char wbuf[LEN+2*A];
42 
alignup(void * p)43 static void *alignup(void *p)
44 {
45 	return (void*)(((uintptr_t)p + A-1) & -A);
46 }
47 
test(const struct fun * fun,int dalign,int salign,int len)48 static void test(const struct fun *fun, int dalign, int salign, int len)
49 {
50 	unsigned char *src = alignup(sbuf);
51 	unsigned char *dst = alignup(dbuf);
52 	unsigned char *want = wbuf;
53 	unsigned char *s = src + salign;
54 	unsigned char *d = dst + dalign;
55 	unsigned char *w = want + dalign;
56 	void *p;
57 	int i;
58 
59 	if (len > LEN || dalign >= A || salign >= A)
60 		abort();
61 	for (i = 0; i < len+A; i++) {
62 		src[i] = '?';
63 		want[i] = dst[i] = '*';
64 	}
65 	for (i = 0; i < len; i++)
66 		s[i] = w[i] = 'a' + i%23;
67 
68 	p = fun->fun(d, s, len);
69 	if (p != d)
70 		ERR("%s(%p,..) returned %p\n", fun->name, d, p);
71 	for (i = 0; i < len+A; i++) {
72 		if (dst[i] != want[i]) {
73 			ERR("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign, len);
74 			ERR("got : %.*s\n", dalign+len+1, dst);
75 			ERR("want: %.*s\n", dalign+len+1, want);
76 			break;
77 		}
78 	}
79 }
80 
main()81 int main()
82 {
83 	int r = 0;
84 	for (int i=0; funtab[i].name; i++) {
85 		test_status = 0;
86 		for (int d = 0; d < A; d++)
87 			for (int s = 0; s < A; s++) {
88 				int n;
89 				for (n = 0; n < 100; n++)
90 					test(funtab+i, d, s, n);
91 				for (; n < LEN; n *= 2)
92 					test(funtab+i, d, s, n);
93 			}
94 		printf("%s %s\n", test_status ? "FAIL" : "PASS", funtab[i].name);
95 		if (test_status)
96 			r = -1;
97 	}
98 	return r;
99 }
100