xref: /freebsd/contrib/arm-optimized-routines/string/test/memcpy.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*
2  * memcpy test.
3  *
4  * Copyright (c) 2019-2020, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "mte.h"
13 #include "stringlib.h"
14 #include "stringtest.h"
15 
16 #define F(x, mte) {#x, x, mte},
17 
18 static const struct fun
19 {
20   const char *name;
21   void *(*fun) (void *, const void *, size_t);
22   int test_mte;
23 } funtab[] = {
24   // clang-format off
25   F(memcpy, 0)
26 #if __aarch64__
27   F(__memcpy_aarch64, 1)
28 # if __ARM_NEON
29   F(__memcpy_aarch64_simd, 1)
30 # endif
31 #elif __arm__
32   F(__memcpy_arm, 0)
33 #endif
34   {0, 0, 0}
35   // clang-format on
36 };
37 #undef F
38 
39 #define A 32
40 #define LEN 250000
41 static unsigned char *dbuf;
42 static unsigned char *sbuf;
43 static unsigned char wbuf[LEN + 2 * A];
44 
45 static void *
46 alignup (void *p)
47 {
48   return (void *) (((uintptr_t) p + A - 1) & -A);
49 }
50 
51 static void
52 test (const struct fun *fun, int dalign, int salign, int len)
53 {
54   unsigned char *src = alignup (sbuf);
55   unsigned char *dst = alignup (dbuf);
56   unsigned char *want = wbuf;
57   unsigned char *s = src + salign;
58   unsigned char *d = dst + dalign;
59   unsigned char *w = want + dalign;
60   void *p;
61   int i;
62 
63   if (err_count >= ERR_LIMIT)
64     return;
65   if (len > LEN || dalign >= A || salign >= A)
66     abort ();
67   for (i = 0; i < len + A; i++)
68     {
69       src[i] = '?';
70       want[i] = dst[i] = '*';
71     }
72   for (i = 0; i < len; i++)
73     s[i] = w[i] = 'a' + i % 23;
74 
75   s = tag_buffer (s, len, fun->test_mte);
76   d = tag_buffer (d, len, fun->test_mte);
77   p = fun->fun (d, s, len);
78   untag_buffer (s, len, fun->test_mte);
79   untag_buffer (d, len, fun->test_mte);
80 
81   if (p != d)
82     ERR ("%s(%p,..) returned %p\n", fun->name, d, p);
83   for (i = 0; i < len + A; i++)
84     {
85       if (dst[i] != want[i])
86 	{
87 	  ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign,
88 	       len);
89 	  quoteat ("got", dst, len + A, i);
90 	  quoteat ("want", want, len + A, i);
91 	  break;
92 	}
93     }
94 }
95 
96 int
97 main ()
98 {
99   dbuf = mte_mmap (LEN + 2 * A);
100   sbuf = mte_mmap (LEN + 2 * A);
101   int r = 0;
102   for (int i = 0; funtab[i].name; i++)
103     {
104       err_count = 0;
105       for (int d = 0; d < A; d++)
106 	for (int s = 0; s < A; s++)
107 	  {
108 	    int n;
109 	    for (n = 0; n < 100; n++)
110 	      test (funtab + i, d, s, n);
111 	    for (; n < LEN; n *= 2)
112 	      test (funtab + i, d, s, n);
113 	  }
114       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
115       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
116       if (err_count)
117 	r = -1;
118     }
119   return r;
120 }
121