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