Fix cexpl when compiled with latest GCC
[glibc.git] / benchtests / bench-strcmp.c
blob1fd0d1e191cc7f05412db71b92d4c2ad71ab3881
1 /* Measure strcmp and wcscmp functions.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcscmp"
22 #else
23 # define TEST_NAME "strcmp"
24 #endif
25 #include "bench-string.h"
27 #ifdef WIDE
28 # include <wchar.h>
30 # define L(str) L##str
31 # define STRCMP wcscmp
32 # define STRCPY wcscpy
33 # define STRLEN wcslen
34 # define MEMCPY wmemcpy
35 # define SIMPLE_STRCMP simple_wcscmp
36 # define STUPID_STRCMP stupid_wcscmp
37 # define CHAR wchar_t
38 # define UCHAR wchar_t
39 # define CHARBYTES 4
40 # define CHARBYTESLOG 2
41 # define CHARALIGN __alignof__ (CHAR)
42 # define MIDCHAR 0x7fffffff
43 # define LARGECHAR 0xfffffffe
44 # define CHAR__MAX WCHAR_MAX
45 # define CHAR__MIN WCHAR_MIN
47 /* Wcscmp uses signed semantics for comparison, not unsigned */
48 /* Avoid using substraction since possible overflow */
50 int
51 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
53 wchar_t c1, c2;
56 c1 = *s1++;
57 c2 = *s2++;
58 if (c2 == L'\0')
59 return c1 - c2;
61 while (c1 == c2);
63 return c1 < c2 ? -1 : 1;
66 int
67 stupid_wcscmp (const wchar_t *s1, const wchar_t *s2)
69 size_t ns1 = wcslen (s1) + 1;
70 size_t ns2 = wcslen (s2) + 1;
71 size_t n = ns1 < ns2 ? ns1 : ns2;
72 int ret = 0;
74 wchar_t c1, c2;
76 while (n--) {
77 c1 = *s1++;
78 c2 = *s2++;
79 if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
80 break;
82 return ret;
85 #else
86 # include <limits.h>
88 # define L(str) str
89 # define STRCMP strcmp
90 # define STRCPY strcpy
91 # define STRLEN strlen
92 # define MEMCPY memcpy
93 # define SIMPLE_STRCMP simple_strcmp
94 # define STUPID_STRCMP stupid_strcmp
95 # define CHAR char
96 # define UCHAR unsigned char
97 # define CHARBYTES 1
98 # define CHARBYTESLOG 0
99 # define CHARALIGN 1
100 # define MIDCHAR 0x7f
101 # define LARGECHAR 0xfe
102 # define CHAR__MAX CHAR_MAX
103 # define CHAR__MIN CHAR_MIN
105 /* Strcmp uses unsigned semantics for comparison. */
107 simple_strcmp (const char *s1, const char *s2)
109 int ret;
111 while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
112 return ret;
116 stupid_strcmp (const char *s1, const char *s2)
118 size_t ns1 = strlen (s1) + 1;
119 size_t ns2 = strlen (s2) + 1;
120 size_t n = ns1 < ns2 ? ns1 : ns2;
121 int ret = 0;
123 while (n--)
124 if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
125 break;
126 return ret;
128 #endif
130 typedef int (*proto_t) (const CHAR *, const CHAR *);
132 IMPL (STUPID_STRCMP, 1)
133 IMPL (SIMPLE_STRCMP, 1)
134 IMPL (STRCMP, 1)
136 static void
137 do_one_test (impl_t *impl,
138 const CHAR *s1, const CHAR *s2,
139 int exp_result)
141 size_t i, iters = INNER_LOOP_ITERS;
142 timing_t start, stop, cur;
144 TIMING_NOW (start);
145 for (i = 0; i < iters; ++i)
147 CALL (impl, s1, s2);
149 TIMING_NOW (stop);
151 TIMING_DIFF (cur, start, stop);
153 TIMING_PRINT_MEAN ((double) cur, (double) iters);
156 static void
157 do_test (size_t align1, size_t align2, size_t len, int max_char,
158 int exp_result)
160 size_t i;
162 CHAR *s1, *s2;
164 if (len == 0)
165 return;
167 align1 &= 63;
168 if (align1 + (len + 1) * CHARBYTES >= page_size)
169 return;
171 align2 &= 63;
172 if (align2 + (len + 1) * CHARBYTES >= page_size)
173 return;
175 /* Put them close to the end of page. */
176 i = align1 + CHARBYTES * (len + 2);
177 s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
178 i = align2 + CHARBYTES * (len + 2);
179 s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
181 for (i = 0; i < len; i++)
182 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
184 s1[len] = s2[len] = 0;
185 s1[len + 1] = 23;
186 s2[len + 1] = 24 + exp_result;
187 s2[len - 1] -= exp_result;
189 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
191 FOR_EACH_IMPL (impl, 0)
192 do_one_test (impl, s1, s2, exp_result);
194 putchar ('\n');
198 test_main (void)
200 size_t i;
202 test_init ();
204 printf ("%23s", "");
205 FOR_EACH_IMPL (impl, 0)
206 printf ("\t%s", impl->name);
207 putchar ('\n');
209 for (i = 1; i < 32; ++i)
211 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
212 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
213 do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
216 for (i = 1; i < 10 + CHARBYTESLOG; ++i)
218 do_test (0, 0, 2 << i, MIDCHAR, 0);
219 do_test (0, 0, 2 << i, LARGECHAR, 0);
220 do_test (0, 0, 2 << i, MIDCHAR, 1);
221 do_test (0, 0, 2 << i, LARGECHAR, 1);
222 do_test (0, 0, 2 << i, MIDCHAR, -1);
223 do_test (0, 0, 2 << i, LARGECHAR, -1);
224 do_test (0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
225 do_test (CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
228 for (i = 1; i < 8; ++i)
230 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
231 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
232 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
233 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
234 do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
235 do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
238 return ret;
241 #include <support/test-driver.c>