xref: /freebsd/contrib/arm-optimized-routines/string/test/strncmp.c (revision 31914882fca502069810b9e9ddea4bcd8136a4f4)
1*31914882SAlex Richardson /*
2*31914882SAlex Richardson  * strncmp test.
3*31914882SAlex Richardson  *
4*31914882SAlex Richardson  * Copyright (c) 2019-2020, Arm Limited.
5*31914882SAlex Richardson  * SPDX-License-Identifier: MIT
6*31914882SAlex Richardson  */
7*31914882SAlex Richardson 
8*31914882SAlex Richardson #include <stdint.h>
9*31914882SAlex Richardson #include <stdio.h>
10*31914882SAlex Richardson #include <stdlib.h>
11*31914882SAlex Richardson #include <string.h>
12*31914882SAlex Richardson #include "mte.h"
13*31914882SAlex Richardson #include "stringlib.h"
14*31914882SAlex Richardson #include "stringtest.h"
15*31914882SAlex Richardson 
16*31914882SAlex Richardson #define F(x, mte) {#x, x, mte},
17*31914882SAlex Richardson 
18*31914882SAlex Richardson static const struct fun
19*31914882SAlex Richardson {
20*31914882SAlex Richardson   const char *name;
21*31914882SAlex Richardson   int (*fun) (const char *, const char *, size_t);
22*31914882SAlex Richardson   int test_mte;
23*31914882SAlex Richardson } funtab[] = {
24*31914882SAlex Richardson   // clang-format off
25*31914882SAlex Richardson   F(strncmp, 0)
26*31914882SAlex Richardson #if __aarch64__
27*31914882SAlex Richardson   F(__strncmp_aarch64, 0)
28*31914882SAlex Richardson   F(__strncmp_aarch64_mte, 1)
29*31914882SAlex Richardson # if __ARM_FEATURE_SVE
30*31914882SAlex Richardson   F(__strncmp_aarch64_sve, 1)
31*31914882SAlex Richardson # endif
32*31914882SAlex Richardson #endif
33*31914882SAlex Richardson   {0, 0, 0}
34*31914882SAlex Richardson   // clang-format on
35*31914882SAlex Richardson };
36*31914882SAlex Richardson #undef F
37*31914882SAlex Richardson 
38*31914882SAlex Richardson #define A 32
39*31914882SAlex Richardson #define LEN 250000
40*31914882SAlex Richardson static char *s1buf;
41*31914882SAlex Richardson static char *s2buf;
42*31914882SAlex Richardson 
43*31914882SAlex Richardson static void *
44*31914882SAlex Richardson alignup (void *p)
45*31914882SAlex Richardson {
46*31914882SAlex Richardson   return (void *) (((uintptr_t) p + A - 1) & -A);
47*31914882SAlex Richardson }
48*31914882SAlex Richardson 
49*31914882SAlex Richardson static void
50*31914882SAlex Richardson test (const struct fun *fun, int s1align, int s2align, int maxlen, int diffpos,
51*31914882SAlex Richardson       int len, int delta)
52*31914882SAlex Richardson {
53*31914882SAlex Richardson   char *src1 = alignup (s1buf);
54*31914882SAlex Richardson   char *src2 = alignup (s2buf);
55*31914882SAlex Richardson   char *s1 = src1 + s1align;
56*31914882SAlex Richardson   char *s2 = src2 + s2align;
57*31914882SAlex Richardson   int r;
58*31914882SAlex Richardson 
59*31914882SAlex Richardson   if (err_count >= ERR_LIMIT)
60*31914882SAlex Richardson     return;
61*31914882SAlex Richardson   if (len > LEN || s1align >= A || s2align >= A)
62*31914882SAlex Richardson     abort ();
63*31914882SAlex Richardson   if (diffpos >= len)
64*31914882SAlex Richardson     abort ();
65*31914882SAlex Richardson   if ((diffpos < 0) != (delta == 0))
66*31914882SAlex Richardson     abort ();
67*31914882SAlex Richardson 
68*31914882SAlex Richardson   for (int i = 0; i < len + A; i++)
69*31914882SAlex Richardson     src1[i] = src2[i] = '?';
70*31914882SAlex Richardson   for (int i = 0; i < len; i++)
71*31914882SAlex Richardson     s1[i] = s2[i] = 'a' + i % 23;
72*31914882SAlex Richardson   if (delta)
73*31914882SAlex Richardson     s1[diffpos] += delta;
74*31914882SAlex Richardson   s1[len] = s2[len] = '\0';
75*31914882SAlex Richardson 
76*31914882SAlex Richardson   size_t mte_len = maxlen < len + 1 ? maxlen : len + 1;
77*31914882SAlex Richardson   s1 = tag_buffer (s1, mte_len, fun->test_mte);
78*31914882SAlex Richardson   s2 = tag_buffer (s2, mte_len, fun->test_mte);
79*31914882SAlex Richardson   r = fun->fun (s1, s2, maxlen);
80*31914882SAlex Richardson   untag_buffer (s1, mte_len, fun->test_mte);
81*31914882SAlex Richardson   untag_buffer (s2, mte_len, fun->test_mte);
82*31914882SAlex Richardson 
83*31914882SAlex Richardson   if (diffpos >= maxlen)
84*31914882SAlex Richardson     {
85*31914882SAlex Richardson       diffpos = -1;
86*31914882SAlex Richardson       delta = 0;
87*31914882SAlex Richardson     }
88*31914882SAlex Richardson   if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0))
89*31914882SAlex Richardson     {
90*31914882SAlex Richardson       ERR (
91*31914882SAlex Richardson 	"%s(align %d, align %d, %d) (len=%d, diffpos=%d) failed, returned %d\n",
92*31914882SAlex Richardson 	fun->name, s1align, s2align, maxlen, len, diffpos, r);
93*31914882SAlex Richardson       quoteat ("src1", src1, len + A, diffpos);
94*31914882SAlex Richardson       quoteat ("src2", src2, len + A, diffpos);
95*31914882SAlex Richardson     }
96*31914882SAlex Richardson }
97*31914882SAlex Richardson 
98*31914882SAlex Richardson int
99*31914882SAlex Richardson main ()
100*31914882SAlex Richardson {
101*31914882SAlex Richardson   s1buf = mte_mmap (LEN + 2 * A + 1);
102*31914882SAlex Richardson   s2buf = mte_mmap (LEN + 2 * A + 1);
103*31914882SAlex Richardson   int r = 0;
104*31914882SAlex Richardson   for (int i = 0; funtab[i].name; i++)
105*31914882SAlex Richardson     {
106*31914882SAlex Richardson       err_count = 0;
107*31914882SAlex Richardson       for (int d = 0; d < A; d++)
108*31914882SAlex Richardson 	for (int s = 0; s < A; s++)
109*31914882SAlex Richardson 	  {
110*31914882SAlex Richardson 	    int n;
111*31914882SAlex Richardson 	    test (funtab + i, d, s, 0, -1, 0, 0);
112*31914882SAlex Richardson 	    test (funtab + i, d, s, 1, -1, 0, 0);
113*31914882SAlex Richardson 	    test (funtab + i, d, s, 0, -1, 1, 0);
114*31914882SAlex Richardson 	    test (funtab + i, d, s, 1, -1, 1, 0);
115*31914882SAlex Richardson 	    test (funtab + i, d, s, 2, -1, 1, 0);
116*31914882SAlex Richardson 	    test (funtab + i, d, s, 1, 0, 1, 1);
117*31914882SAlex Richardson 	    test (funtab + i, d, s, 1, 0, 1, -1);
118*31914882SAlex Richardson 	    for (n = 2; n < 100; n++)
119*31914882SAlex Richardson 	      {
120*31914882SAlex Richardson 		test (funtab + i, d, s, n, -1, n, 0);
121*31914882SAlex Richardson 		test (funtab + i, d, s, n, n / 2, n, 1);
122*31914882SAlex Richardson 		test (funtab + i, d, s, n / 2, -1, n, 0);
123*31914882SAlex Richardson 		test (funtab + i, d, s, n / 2, n / 2, n, -1);
124*31914882SAlex Richardson 	      }
125*31914882SAlex Richardson 	    for (; n < LEN; n *= 2)
126*31914882SAlex Richardson 	      {
127*31914882SAlex Richardson 		test (funtab + i, d, s, n, -1, n, 0);
128*31914882SAlex Richardson 		test (funtab + i, d, s, n, n / 2, n, -1);
129*31914882SAlex Richardson 		test (funtab + i, d, s, n / 2, -1, n, 0);
130*31914882SAlex Richardson 		test (funtab + i, d, s, n / 2, n / 2, n, 1);
131*31914882SAlex Richardson 	      }
132*31914882SAlex Richardson 	  }
133*31914882SAlex Richardson       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
134*31914882SAlex Richardson       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
135*31914882SAlex Richardson       if (err_count)
136*31914882SAlex Richardson 	r = -1;
137*31914882SAlex Richardson     }
138*31914882SAlex Richardson   return r;
139*31914882SAlex Richardson }
140