xref: /freebsd/contrib/arm-optimized-routines/string/test/strcmp.c (revision 1349891a0eed79625faafa5ad354d65ff9ea6012)
1 /*
2  * strcmp 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   int (*fun) (const char *s1, const char *s2);
22   int test_mte;
23 } funtab[] = {
24   // clang-format off
25   F(strcmp, 0)
26 #if __aarch64__
27   F(__strcmp_aarch64, 0)
28   F(__strcmp_aarch64_mte, 1)
29 # if __ARM_FEATURE_SVE
30   F(__strcmp_aarch64_sve, 1)
31 # endif
32 #elif __arm__
33 # if __ARM_ARCH >= 7 && __ARM_ARCH_ISA_ARM >= 1
34   F(__strcmp_arm, 0)
35 # elif __ARM_ARCH == 6 && __ARM_ARCH_6M__ >= 1
36   F(__strcmp_armv6m, 0)
37 # endif
38 #endif
39   {0, 0, 0}
40   // clang-format on
41 };
42 #undef F
43 
44 #define A 32
45 #define LEN 250000
46 static char *s1buf;
47 static char *s2buf;
48 
49 static void *
50 alignup (void *p)
51 {
52   return (void *) (((uintptr_t) p + A - 1) & -A);
53 }
54 
55 static void
56 test (const struct fun *fun, int s1align, int s2align, int len, int diffpos,
57       int delta)
58 {
59   char *src1 = alignup (s1buf);
60   char *src2 = alignup (s2buf);
61   char *s1 = src1 + s1align;
62   char *s2 = src2 + s2align;
63   int r;
64 
65   if (err_count >= ERR_LIMIT)
66     return;
67   if (len > LEN || s1align >= A || s2align >= A)
68     abort ();
69   if (diffpos >= len)
70     abort ();
71   if ((diffpos < 0) != (delta == 0))
72     abort ();
73 
74   for (int i = 0; i < len + A; i++)
75     src1[i] = src2[i] = '?';
76   for (int i = 0; i < len; i++)
77     s1[i] = s2[i] = 'a' + i % 23;
78   if (delta)
79     s1[diffpos] += delta;
80   s1[len] = s2[len] = '\0';
81 
82   s1 = tag_buffer (s1, len + 1, fun->test_mte);
83   s2 = tag_buffer (s2, len + 1, fun->test_mte);
84   r = fun->fun (s1, s2);
85   untag_buffer (s1, len + 1, fun->test_mte);
86   untag_buffer (s2, len + 1, fun->test_mte);
87 
88   if ((delta == 0 && r != 0) || (delta > 0 && r <= 0) || (delta < 0 && r >= 0))
89     {
90       ERR ("%s(align %d, align %d, %d) failed, returned %d\n", fun->name,
91 	   s1align, s2align, len, r);
92       quoteat ("src1", src1, len + A, diffpos);
93       quoteat ("src2", src2, len + A, diffpos);
94     }
95 }
96 
97 int
98 main ()
99 {
100   s1buf = mte_mmap (LEN + 2 * A + 1);
101   s2buf = mte_mmap (LEN + 2 * A + 1);
102   int r = 0;
103   for (int i = 0; funtab[i].name; i++)
104     {
105       err_count = 0;
106       for (int d = 0; d < A; d++)
107 	for (int s = 0; s < A; s++)
108 	  {
109 	    int n;
110 	    test (funtab + i, d, s, 0, -1, 0);
111 	    test (funtab + i, d, s, 1, -1, 0);
112 	    test (funtab + i, d, s, 1, 0, 1);
113 	    test (funtab + i, d, s, 1, 0, -1);
114 	    for (n = 2; n < 100; n++)
115 	      {
116 		test (funtab + i, d, s, n, -1, 0);
117 		test (funtab + i, d, s, n, n - 1, -1);
118 		test (funtab + i, d, s, n, n / 2, 1);
119 	      }
120 	    for (; n < LEN; n *= 2)
121 	      {
122 		test (funtab + i, d, s, n, -1, 0);
123 		test (funtab + i, d, s, n, n / 2, -1);
124 	      }
125 	  }
126       char *pass = funtab[i].test_mte && mte_enabled () ? "MTE PASS" : "PASS";
127       printf ("%s %s\n", err_count ? "FAIL" : pass, funtab[i].name);
128       if (err_count)
129 	r = -1;
130     }
131   return r;
132 }
133