benchtests: Use "=" instead of ":=" [BZ #28970]
[platform/upstream/glibc.git] / benchtests / bench-strcasecmp.c
1 /* Measure strcasecmp functions.
2    Copyright (C) 2013-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18
19 #include <ctype.h>
20 #define TEST_MAIN
21 #define TEST_NAME "strcasecmp"
22 #include "bench-string.h"
23
24 typedef int (*proto_t) (const char *, const char *);
25 static int simple_strcasecmp (const char *, const char *);
26
27 IMPL (simple_strcasecmp, 0)
28 IMPL (strcasecmp, 1)
29
30 static int
31 simple_strcasecmp (const char *s1, const char *s2)
32 {
33   int ret;
34
35   while ((ret = ((unsigned char) tolower (*s1)
36                  - (unsigned char) tolower (*s2))) == 0
37          && *s1++)
38     ++s2;
39   return ret;
40 }
41
42 static void
43 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
44 {
45   size_t i, iters = INNER_LOOP_ITERS;
46   timing_t start, stop, cur;
47   int result = CALL (impl, s1, s2);
48   if ((exp_result == 0 && result != 0)
49       || (exp_result < 0 && result >= 0)
50       || (exp_result > 0 && result <= 0))
51     {
52       error (0, 0, "Wrong result in function %s %d %d", impl->name,
53              result, exp_result);
54       ret = 1;
55       return;
56     }
57
58   TIMING_NOW (start);
59   for (i = 0; i < iters; ++i)
60     {
61       CALL (impl, s1, s2);
62     }
63   TIMING_NOW (stop);
64
65   TIMING_DIFF (cur, start, stop);
66
67   TIMING_PRINT_MEAN ((double) cur, (double) iters);
68 }
69
70 static void
71 do_test (size_t align1, size_t align2, size_t len, int max_char,
72          int exp_result)
73 {
74   size_t i;
75   char *s1, *s2;
76
77   if (len == 0)
78     return;
79
80   align1 &= 7;
81   if (align1 + len + 1 >= page_size)
82     return;
83
84   align2 &= 7;
85   if (align2 + len + 1 >= page_size)
86     return;
87
88   s1 = (char *) (buf1 + align1);
89   s2 = (char *) (buf2 + align2);
90
91   for (i = 0; i < len; i++)
92     {
93       s1[i] = toupper (1 + 23 * i % max_char);
94       s2[i] = tolower (s1[i]);
95     }
96
97   s1[len] = s2[len] = 0;
98   s1[len + 1] = 23;
99   s2[len + 1] = 24 + exp_result;
100   if ((s2[len - 1] == 'z' && exp_result == -1)
101       || (s2[len - 1] == 'a' && exp_result == 1))
102     s1[len - 1] += exp_result;
103   else
104     s2[len - 1] -= exp_result;
105
106   printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
107
108   FOR_EACH_IMPL (impl, 0)
109     do_one_test (impl, s1, s2, exp_result);
110
111   putchar ('\n');
112 }
113
114 int
115 test_main (void)
116 {
117   size_t i;
118
119   test_init ();
120
121   printf ("%23s", "");
122   FOR_EACH_IMPL (impl, 0)
123     printf ("\t%s", impl->name);
124   putchar ('\n');
125
126   for (i = 1; i < 16; ++i)
127     {
128       do_test (i, i, i, 127, 0);
129       do_test (i, i, i, 127, 1);
130       do_test (i, i, i, 127, -1);
131     }
132
133   for (i = 1; i < 10; ++i)
134     {
135       do_test (0, 0, 2 << i, 127, 0);
136       do_test (0, 0, 2 << i, 254, 0);
137       do_test (0, 0, 2 << i, 127, 1);
138       do_test (0, 0, 2 << i, 254, 1);
139       do_test (0, 0, 2 << i, 127, -1);
140       do_test (0, 0, 2 << i, 254, -1);
141     }
142
143   for (i = 1; i < 8; ++i)
144     {
145       do_test (i, 2 * i, 8 << i, 127, 0);
146       do_test (2 * i, i, 8 << i, 254, 0);
147       do_test (i, 2 * i, 8 << i, 127, 1);
148       do_test (2 * i, i, 8 << i, 254, 1);
149       do_test (i, 2 * i, 8 << i, 127, -1);
150       do_test (2 * i, i, 8 << i, 254, -1);
151     }
152
153   return ret;
154 }
155
156 #include <support/test-driver.c>