xref: /freebsd-src/contrib/arm-optimized-routines/string/test/memset.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * memset test.
331914882SAlex Richardson  *
45a02ffc3SAndrew Turner  * Copyright (c) 2019-2023, Arm Limited.
5072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #include <stdint.h>
931914882SAlex Richardson #include <stdio.h>
1031914882SAlex Richardson #include <stdlib.h>
1131914882SAlex Richardson #include <string.h>
1231914882SAlex Richardson #include "mte.h"
1331914882SAlex Richardson #include "stringlib.h"
1431914882SAlex Richardson #include "stringtest.h"
1531914882SAlex Richardson 
1631914882SAlex Richardson #define F(x, mte) {#x, x, mte},
1731914882SAlex Richardson 
1831914882SAlex Richardson static const struct fun
1931914882SAlex Richardson {
2031914882SAlex Richardson   const char *name;
2131914882SAlex Richardson   void *(*fun) (void *s, int c, size_t n);
2231914882SAlex Richardson   int test_mte;
2331914882SAlex Richardson } funtab[] = {
2431914882SAlex Richardson   // clang-format off
2531914882SAlex Richardson   F(memset, 0)
2631914882SAlex Richardson #if __aarch64__
2731914882SAlex Richardson   F(__memset_aarch64, 1)
28*f3087befSAndrew Turner # if __ARM_FEATURE_SVE
29*f3087befSAndrew Turner   F(__memset_aarch64_sve, 1)
30*f3087befSAndrew Turner # endif
315a02ffc3SAndrew Turner # if WANT_MOPS
325a02ffc3SAndrew Turner   F(__memset_aarch64_mops, 1)
335a02ffc3SAndrew Turner # endif
3431914882SAlex Richardson #elif __arm__
3531914882SAlex Richardson   F(__memset_arm, 0)
3631914882SAlex Richardson #endif
3731914882SAlex Richardson   {0, 0, 0}
3831914882SAlex Richardson   // clang-format on
3931914882SAlex Richardson };
4031914882SAlex Richardson #undef F
4131914882SAlex Richardson 
4231914882SAlex Richardson #define A 32
4331914882SAlex Richardson #define LEN 250000
4431914882SAlex Richardson static unsigned char *sbuf;
4531914882SAlex Richardson 
4631914882SAlex Richardson static void *
4731914882SAlex Richardson alignup (void *p)
4831914882SAlex Richardson {
4931914882SAlex Richardson   return (void *) (((uintptr_t) p + A - 1) & -A);
5031914882SAlex Richardson }
5131914882SAlex Richardson 
5231914882SAlex Richardson static void
5331914882SAlex Richardson test (const struct fun *fun, int salign, int c, int len)
5431914882SAlex Richardson {
5531914882SAlex Richardson   unsigned char *src = alignup (sbuf);
5631914882SAlex Richardson   unsigned char *s = src + salign;
5731914882SAlex Richardson   void *p;
5831914882SAlex Richardson   int i;
5931914882SAlex Richardson 
6031914882SAlex Richardson   if (err_count >= ERR_LIMIT)
6131914882SAlex Richardson     return;
6231914882SAlex Richardson   if (len > LEN || salign >= A)
6331914882SAlex Richardson     abort ();
6431914882SAlex Richardson   for (i = 0; i < len + A; i++)
6531914882SAlex Richardson     src[i] = '?';
6631914882SAlex Richardson   for (i = 0; i < len; i++)
6731914882SAlex Richardson     s[i] = 'a' + i % 23;
6831914882SAlex Richardson 
6931914882SAlex Richardson   s = tag_buffer (s, len, fun->test_mte);
7031914882SAlex Richardson   p = fun->fun (s, c, len);
7131914882SAlex Richardson   untag_buffer (s, len, fun->test_mte);
7231914882SAlex Richardson 
7331914882SAlex Richardson   if (p != s)
7431914882SAlex Richardson     ERR ("%s(%p,..) returned %p\n", fun->name, s, p);
7531914882SAlex Richardson 
7631914882SAlex Richardson   for (i = 0; i < salign; i++)
7731914882SAlex Richardson     {
7831914882SAlex Richardson       if (src[i] != '?')
7931914882SAlex Richardson 	{
8031914882SAlex Richardson 	  ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);
8131914882SAlex Richardson 	  quoteat ("got", src, len + A, i);
8231914882SAlex Richardson 	  return;
8331914882SAlex Richardson 	}
8431914882SAlex Richardson     }
8531914882SAlex Richardson   for (; i < salign + len; i++)
8631914882SAlex Richardson     {
8731914882SAlex Richardson       if (src[i] != (unsigned char) c)
8831914882SAlex Richardson 	{
8931914882SAlex Richardson 	  ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);
9031914882SAlex Richardson 	  quoteat ("got", src, len + A, i);
9131914882SAlex Richardson 	  return;
9231914882SAlex Richardson 	}
9331914882SAlex Richardson     }
9431914882SAlex Richardson   for (; i < len + A; i++)
9531914882SAlex Richardson     {
9631914882SAlex Richardson       if (src[i] != '?')
9731914882SAlex Richardson 	{
9831914882SAlex Richardson 	  ERR ("%s(align %d, %d, %d) failed\n", fun->name, salign, c, len);
9931914882SAlex Richardson 	  quoteat ("got", src, len + A, i);
10031914882SAlex Richardson 	  return;
10131914882SAlex Richardson 	}
10231914882SAlex Richardson     }
10331914882SAlex Richardson }
10431914882SAlex Richardson 
10531914882SAlex Richardson int
10631914882SAlex Richardson main ()
10731914882SAlex Richardson {
10831914882SAlex Richardson   sbuf = mte_mmap (LEN + 2 * A);
10931914882SAlex Richardson   int r = 0;
11031914882SAlex Richardson   for (int i = 0; funtab[i].name; i++)
11131914882SAlex Richardson     {
11231914882SAlex Richardson       err_count = 0;
11331914882SAlex Richardson       for (int s = 0; s < A; s++)
11431914882SAlex Richardson 	{
11531914882SAlex Richardson 	  int n;
11631914882SAlex Richardson 	  for (n = 0; n < 100; n++)
11731914882SAlex Richardson 	    {
11831914882SAlex Richardson 	      test (funtab + i, s, 0, n);
11931914882SAlex Richardson 	      test (funtab + i, s, 0x25, n);
12031914882SAlex Richardson 	      test (funtab + i, s, 0xaa25, n);
12131914882SAlex Richardson 	    }
12231914882SAlex Richardson 	  for (; n < LEN; n *= 2)
12331914882SAlex Richardson 	    {
12431914882SAlex Richardson 	      test (funtab + i, s, 0, n);
12531914882SAlex Richardson 	      test (funtab + i, s, 0x25, n);
12631914882SAlex Richardson 	      test (funtab + i, s, 0xaa25, n);
12731914882SAlex Richardson 	    }
12831914882SAlex Richardson 	}
12931914882SAlex Richardson       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
13031914882SAlex Richardson       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
13131914882SAlex Richardson       if (err_count)
13231914882SAlex Richardson 	r = -1;
13331914882SAlex Richardson     }
13431914882SAlex Richardson   return r;
13531914882SAlex Richardson }
136