xref: /freebsd/contrib/arm-optimized-routines/string/test/memmove.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*
2  * memmove test.
3  *
4  * Copyright (c) 2019-2022, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
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(memmove, 0)
26 #if __aarch64__
27   F(__memmove_aarch64, 1)
28 # if __ARM_NEON
29   F(__memmove_aarch64_simd, 1)
30 # endif
31 # if __ARM_FEATURE_SVE
32   F(__memmove_aarch64_sve, 1)
33 # endif
34 #endif
35   {0, 0, 0}
36   // clang-format on
37 };
38 #undef F
39 
40 #define A 32
41 #define LEN 250000
42 static unsigned char *dbuf;
43 static unsigned char *sbuf;
44 static unsigned char wbuf[LEN + 2 * A];
45 
46 static void *
47 alignup (void *p)
48 {
49   return (void *) (((uintptr_t) p + A - 1) & -A);
50 }
51 
52 static void
53 test (const struct fun *fun, int dalign, int salign, int len)
54 {
55   unsigned char *src = alignup (sbuf);
56   unsigned char *dst = alignup (dbuf);
57   unsigned char *want = wbuf;
58   unsigned char *s = src + salign;
59   unsigned char *d = dst + dalign;
60   unsigned char *w = want + dalign;
61   void *p;
62   int i;
63 
64   if (err_count >= ERR_LIMIT)
65     return;
66   if (len > LEN || dalign >= A || salign >= A)
67     abort ();
68   for (i = 0; i < len + A; i++)
69     {
70       src[i] = '?';
71       want[i] = dst[i] = '*';
72     }
73   for (i = 0; i < len; i++)
74     s[i] = w[i] = 'a' + i % 23;
75 
76   p = fun->fun (d, s, len);
77   if (p != d)
78     ERR ("%s(%p,..) returned %p\n", fun->name, d, p);
79   for (i = 0; i < len + A; i++)
80     {
81       if (dst[i] != want[i])
82 	{
83 	  ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign,
84 	       len);
85 	  quoteat ("got", dst, len + A, i);
86 	  quoteat ("want", want, len + A, i);
87 	  break;
88 	}
89     }
90 }
91 
92 static void
93 test_overlap (const struct fun *fun, int dalign, int salign, int len)
94 {
95   unsigned char *src = alignup (sbuf);
96   unsigned char *dst = src;
97   unsigned char *want = wbuf;
98   unsigned char *s = src + salign;
99   unsigned char *d = dst + dalign;
100   unsigned char *w = wbuf + dalign;
101   void *p;
102 
103   if (err_count >= ERR_LIMIT)
104     return;
105   if (len > LEN || dalign >= A || salign >= A)
106     abort ();
107 
108   for (int i = 0; i < len + A; i++)
109     src[i] = want[i] = '?';
110 
111   for (int i = 0; i < len; i++)
112     s[i] = want[salign + i] = 'a' + i % 23;
113   for (int i = 0; i < len; i++)
114     w[i] = s[i];
115 
116   s = tag_buffer (s, len, fun->test_mte);
117   d = tag_buffer (d, len, fun->test_mte);
118   p = fun->fun (d, s, len);
119   untag_buffer (s, len, fun->test_mte);
120   untag_buffer (d, len, fun->test_mte);
121 
122   if (p != d)
123     ERR ("%s(%p,..) returned %p\n", fun->name, d, p);
124   for (int i = 0; i < len + A; i++)
125     {
126       if (dst[i] != want[i])
127 	{
128 	  ERR ("%s(align %d, align %d, %d) failed\n", fun->name, dalign, salign,
129 	       len);
130 	  quoteat ("got", dst, len + A, i);
131 	  quoteat ("want", want, len + A, i);
132 	  break;
133 	}
134     }
135 }
136 
137 int
138 main ()
139 {
140   dbuf = mte_mmap (LEN + 2 * A);
141   sbuf = mte_mmap (LEN + 2 * A);
142   int r = 0;
143   for (int i = 0; funtab[i].name; i++)
144     {
145       err_count = 0;
146       for (int d = 0; d < A; d++)
147 	for (int s = 0; s < A; s++)
148 	  {
149 	    int n;
150 	    for (n = 0; n < 100; n++)
151 	      {
152 		test (funtab + i, d, s, n);
153 		test_overlap (funtab + i, d, s, n);
154 	      }
155 	    for (; n < LEN; n *= 2)
156 	      {
157 		test (funtab + i, d, s, n);
158 		test_overlap (funtab + i, d, s, n);
159 	      }
160 	  }
161       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
162       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
163       if (err_count)
164 	r = -1;
165     }
166   return r;
167 }
168