]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strcmp.c
f5e8b9cc95f40597ff321adeaa864ca01b6a96cc
[thirdparty/glibc.git] / benchtests / bench-strcmp.c
1 /* Measure strcmp and wcscmp functions.
2 Copyright (C) 2013-2018 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 <http://www.gnu.org/licenses/>. */
18
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"
26
27 #ifdef WIDE
28 # define L(str) L##str
29 # define SIMPLE_STRCMP simple_wcscmp
30 # define STUPID_STRCMP stupid_wcscmp
31 # define CHARBYTESLOG 2
32 # define MIDCHAR 0x7fffffff
33 # define LARGECHAR 0xfffffffe
34
35 /* Wcscmp uses signed semantics for comparison, not unsigned */
36 /* Avoid using substraction since possible overflow */
37
38 int
39 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
40 {
41 wchar_t c1, c2;
42 do
43 {
44 c1 = *s1++;
45 c2 = *s2++;
46 if (c2 == L'\0')
47 return c1 - c2;
48 }
49 while (c1 == c2);
50
51 return c1 < c2 ? -1 : 1;
52 }
53
54 int
55 stupid_wcscmp (const wchar_t *s1, const wchar_t *s2)
56 {
57 size_t ns1 = wcslen (s1) + 1;
58 size_t ns2 = wcslen (s2) + 1;
59 size_t n = ns1 < ns2 ? ns1 : ns2;
60 int ret = 0;
61
62 wchar_t c1, c2;
63
64 while (n--) {
65 c1 = *s1++;
66 c2 = *s2++;
67 if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
68 break;
69 }
70 return ret;
71 }
72
73 #else
74 # include <limits.h>
75
76 # define L(str) str
77 # define SIMPLE_STRCMP simple_strcmp
78 # define STUPID_STRCMP stupid_strcmp
79 # define CHARBYTESLOG 0
80 # define MIDCHAR 0x7f
81 # define LARGECHAR 0xfe
82
83 /* Strcmp uses unsigned semantics for comparison. */
84 int
85 simple_strcmp (const char *s1, const char *s2)
86 {
87 int ret;
88
89 while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
90 return ret;
91 }
92
93 int
94 stupid_strcmp (const char *s1, const char *s2)
95 {
96 size_t ns1 = strlen (s1) + 1;
97 size_t ns2 = strlen (s2) + 1;
98 size_t n = ns1 < ns2 ? ns1 : ns2;
99 int ret = 0;
100
101 while (n--)
102 if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
103 break;
104 return ret;
105 }
106 #endif
107
108 # include "json-lib.h"
109
110 typedef int (*proto_t) (const CHAR *, const CHAR *);
111
112 IMPL (STUPID_STRCMP, 1)
113 IMPL (SIMPLE_STRCMP, 1)
114 IMPL (STRCMP, 1)
115
116 static void
117 do_one_test (json_ctx_t *json_ctx, impl_t *impl,
118 const CHAR *s1, const CHAR *s2,
119 int exp_result)
120 {
121 size_t i, iters = INNER_LOOP_ITERS;
122 timing_t start, stop, cur;
123
124 TIMING_NOW (start);
125 for (i = 0; i < iters; ++i)
126 {
127 CALL (impl, s1, s2);
128 }
129 TIMING_NOW (stop);
130
131 TIMING_DIFF (cur, start, stop);
132
133 json_element_double (json_ctx, (double) cur / (double) iters);
134 }
135
136 static void
137 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, int
138 max_char, int exp_result)
139 {
140 size_t i;
141
142 CHAR *s1, *s2;
143
144 if (len == 0)
145 return;
146
147 align1 &= 63;
148 if (align1 + (len + 1) * CHARBYTES >= page_size)
149 return;
150
151 align2 &= 63;
152 if (align2 + (len + 1) * CHARBYTES >= page_size)
153 return;
154
155 /* Put them close to the end of page. */
156 i = align1 + CHARBYTES * (len + 2);
157 s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
158 i = align2 + CHARBYTES * (len + 2);
159 s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
160
161 for (i = 0; i < len; i++)
162 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
163
164 s1[len] = s2[len] = 0;
165 s1[len + 1] = 23;
166 s2[len + 1] = 24 + exp_result;
167 s2[len - 1] -= exp_result;
168
169 json_element_object_begin (json_ctx);
170 json_attr_uint (json_ctx, "length", (double) len);
171 json_attr_uint (json_ctx, "align1", (double) align1);
172 json_attr_uint (json_ctx, "align2", (double) align2);
173 json_array_begin (json_ctx, "timings");
174
175 FOR_EACH_IMPL (impl, 0)
176 do_one_test (json_ctx, impl, s1, s2, exp_result);
177
178 json_array_end (json_ctx);
179 json_element_object_end (json_ctx);
180 }
181
182 int
183 test_main (void)
184 {
185 json_ctx_t json_ctx;
186 size_t i;
187
188 test_init ();
189
190 json_init (&json_ctx, 0, stdout);
191
192 json_document_begin (&json_ctx);
193 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
194
195 json_attr_object_begin (&json_ctx, "functions");
196 json_attr_object_begin (&json_ctx, TEST_NAME);
197 json_attr_string (&json_ctx, "bench-variant", "default");
198
199 json_array_begin (&json_ctx, "ifuncs");
200 FOR_EACH_IMPL (impl, 0)
201 json_element_string (&json_ctx, impl->name);
202 json_array_end (&json_ctx);
203
204 json_array_begin (&json_ctx, "results");
205
206 for (i = 1; i < 32; ++i)
207 {
208 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
209 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
210 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
211 }
212
213 for (i = 1; i < 10 + CHARBYTESLOG; ++i)
214 {
215 do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 0);
216 do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 0);
217 do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 1);
218 do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 1);
219 do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, -1);
220 do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, -1);
221 do_test (&json_ctx, 0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
222 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
223 }
224
225 for (i = 1; i < 8; ++i)
226 {
227 do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
228 do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
229 do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
230 do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
231 do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
232 do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
233 }
234
235 json_array_end (&json_ctx);
236 json_attr_object_end (&json_ctx);
237 json_attr_object_end (&json_ctx);
238 json_document_end (&json_ctx);
239
240 return ret;
241 }
242
243 #include <support/test-driver.c>