]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure strcmp and wcscmp functions. |
688903eb | 2 | Copyright (C) 2013-2018 Free Software Foundation, Inc. |
97020474 SP |
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 | # include <wchar.h> | |
29 | ||
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 | |
46 | ||
47 | /* Wcscmp uses signed semantics for comparison, not unsigned */ | |
48 | /* Avoid using substraction since possible overflow */ | |
49 | ||
50 | int | |
51 | simple_wcscmp (const wchar_t *s1, const wchar_t *s2) | |
52 | { | |
53 | wchar_t c1, c2; | |
54 | do | |
55 | { | |
56 | c1 = *s1++; | |
57 | c2 = *s2++; | |
58 | if (c2 == L'\0') | |
59 | return c1 - c2; | |
60 | } | |
61 | while (c1 == c2); | |
62 | ||
63 | return c1 < c2 ? -1 : 1; | |
64 | } | |
65 | ||
66 | int | |
67 | stupid_wcscmp (const wchar_t *s1, const wchar_t *s2) | |
68 | { | |
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; | |
73 | ||
74 | wchar_t c1, c2; | |
75 | ||
76 | while (n--) { | |
77 | c1 = *s1++; | |
78 | c2 = *s2++; | |
79 | if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0) | |
80 | break; | |
81 | } | |
82 | return ret; | |
83 | } | |
84 | ||
85 | #else | |
86 | # include <limits.h> | |
87 | ||
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 | |
104 | ||
105 | /* Strcmp uses unsigned semantics for comparison. */ | |
106 | int | |
107 | simple_strcmp (const char *s1, const char *s2) | |
108 | { | |
109 | int ret; | |
110 | ||
111 | while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++); | |
112 | return ret; | |
113 | } | |
114 | ||
115 | int | |
116 | stupid_strcmp (const char *s1, const char *s2) | |
117 | { | |
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; | |
122 | ||
123 | while (n--) | |
124 | if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0) | |
125 | break; | |
126 | return ret; | |
127 | } | |
128 | #endif | |
129 | ||
5f1603c3 SP |
130 | # include "json-lib.h" |
131 | ||
97020474 SP |
132 | typedef int (*proto_t) (const CHAR *, const CHAR *); |
133 | ||
134 | IMPL (STUPID_STRCMP, 1) | |
135 | IMPL (SIMPLE_STRCMP, 1) | |
136 | IMPL (STRCMP, 1) | |
137 | ||
138 | static void | |
5f1603c3 | 139 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, |
97020474 SP |
140 | const CHAR *s1, const CHAR *s2, |
141 | int exp_result) | |
142 | { | |
44558701 WN |
143 | size_t i, iters = INNER_LOOP_ITERS; |
144 | timing_t start, stop, cur; | |
145 | ||
146 | TIMING_NOW (start); | |
147 | for (i = 0; i < iters; ++i) | |
97020474 | 148 | { |
44558701 | 149 | CALL (impl, s1, s2); |
97020474 | 150 | } |
44558701 WN |
151 | TIMING_NOW (stop); |
152 | ||
153 | TIMING_DIFF (cur, start, stop); | |
154 | ||
5f1603c3 | 155 | json_element_double (json_ctx, (double) cur / (double) iters); |
97020474 SP |
156 | } |
157 | ||
158 | static void | |
5f1603c3 SP |
159 | do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, int |
160 | max_char, int exp_result) | |
97020474 SP |
161 | { |
162 | size_t i; | |
163 | ||
164 | CHAR *s1, *s2; | |
165 | ||
166 | if (len == 0) | |
167 | return; | |
168 | ||
169 | align1 &= 63; | |
170 | if (align1 + (len + 1) * CHARBYTES >= page_size) | |
171 | return; | |
172 | ||
173 | align2 &= 63; | |
174 | if (align2 + (len + 1) * CHARBYTES >= page_size) | |
175 | return; | |
176 | ||
177 | /* Put them close to the end of page. */ | |
178 | i = align1 + CHARBYTES * (len + 2); | |
179 | s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1); | |
180 | i = align2 + CHARBYTES * (len + 2); | |
181 | s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2); | |
182 | ||
183 | for (i = 0; i < len; i++) | |
184 | s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char; | |
185 | ||
186 | s1[len] = s2[len] = 0; | |
187 | s1[len + 1] = 23; | |
188 | s2[len + 1] = 24 + exp_result; | |
189 | s2[len - 1] -= exp_result; | |
190 | ||
5f1603c3 SP |
191 | json_element_object_begin (json_ctx); |
192 | json_attr_uint (json_ctx, "length", (double) len); | |
193 | json_attr_uint (json_ctx, "align1", (double) align1); | |
194 | json_attr_uint (json_ctx, "align2", (double) align2); | |
195 | json_array_begin (json_ctx, "timings"); | |
97020474 SP |
196 | |
197 | FOR_EACH_IMPL (impl, 0) | |
5f1603c3 | 198 | do_one_test (json_ctx, impl, s1, s2, exp_result); |
97020474 | 199 | |
5f1603c3 SP |
200 | json_array_end (json_ctx); |
201 | json_element_object_end (json_ctx); | |
97020474 SP |
202 | } |
203 | ||
204 | int | |
205 | test_main (void) | |
206 | { | |
5f1603c3 | 207 | json_ctx_t json_ctx; |
97020474 SP |
208 | size_t i; |
209 | ||
210 | test_init (); | |
211 | ||
5f1603c3 SP |
212 | json_init (&json_ctx, 0, stdout); |
213 | ||
214 | json_document_begin (&json_ctx); | |
215 | json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); | |
216 | ||
217 | json_attr_object_begin (&json_ctx, "functions"); | |
218 | json_attr_object_begin (&json_ctx, TEST_NAME); | |
219 | json_attr_string (&json_ctx, "bench-variant", "default"); | |
220 | ||
221 | json_array_begin (&json_ctx, "ifuncs"); | |
97020474 | 222 | FOR_EACH_IMPL (impl, 0) |
5f1603c3 SP |
223 | json_element_string (&json_ctx, impl->name); |
224 | json_array_end (&json_ctx); | |
225 | ||
226 | json_array_begin (&json_ctx, "results"); | |
97020474 SP |
227 | |
228 | for (i = 1; i < 32; ++i) | |
229 | { | |
5f1603c3 SP |
230 | do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0); |
231 | do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1); | |
232 | do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1); | |
97020474 SP |
233 | } |
234 | ||
235 | for (i = 1; i < 10 + CHARBYTESLOG; ++i) | |
236 | { | |
5f1603c3 SP |
237 | do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 0); |
238 | do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 0); | |
239 | do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 1); | |
240 | do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 1); | |
241 | do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, -1); | |
242 | do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, -1); | |
243 | do_test (&json_ctx, 0, CHARBYTES * i, 2 << i, MIDCHAR, 1); | |
244 | do_test (&json_ctx, CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1); | |
97020474 SP |
245 | } |
246 | ||
247 | for (i = 1; i < 8; ++i) | |
248 | { | |
5f1603c3 SP |
249 | do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0); |
250 | do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0); | |
251 | do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1); | |
252 | do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1); | |
253 | do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1); | |
254 | do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1); | |
97020474 SP |
255 | } |
256 | ||
5f1603c3 SP |
257 | json_array_end (&json_ctx); |
258 | json_attr_object_end (&json_ctx); | |
259 | json_attr_object_end (&json_ctx); | |
260 | json_document_end (&json_ctx); | |
261 | ||
97020474 SP |
262 | return ret; |
263 | } | |
264 | ||
b598e134 | 265 | #include <support/test-driver.c> |