]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure strncmp functions. |
04277e02 | 2 | Copyright (C) 2013-2019 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 | |
cee82e70 SL |
20 | #ifdef WIDE |
21 | # define TEST_NAME "wcsncmp" | |
22 | #else | |
23 | # define TEST_NAME "strncmp" | |
24 | #endif /* !WIDE */ | |
97020474 | 25 | #include "bench-string.h" |
ad4e816e | 26 | #include "json-lib.h" |
97020474 | 27 | |
cee82e70 | 28 | #ifdef WIDE |
cee82e70 | 29 | # define L(str) L##str |
cee82e70 | 30 | # define SIMPLE_STRNCMP simple_wcsncmp |
97020474 | 31 | |
cee82e70 SL |
32 | /* Wcsncmp uses signed semantics for comparison, not unsigned. |
33 | Avoid using substraction since possible overflow. */ | |
34 | int | |
35 | simple_wcsncmp (const CHAR *s1, const CHAR *s2, size_t n) | |
36 | { | |
37 | wchar_t c1, c2; | |
38 | ||
39 | while (n--) | |
40 | { | |
41 | c1 = *s1++; | |
42 | c2 = *s2++; | |
43 | if (c1 == L ('\0') || c1 != c2) | |
44 | return c1 > c2 ? 1 : (c1 < c2 ? -1 : 0); | |
45 | } | |
46 | return 0; | |
47 | } | |
48 | ||
cee82e70 SL |
49 | #else |
50 | # define L(str) str | |
cee82e70 | 51 | # define SIMPLE_STRNCMP simple_strncmp |
cee82e70 SL |
52 | |
53 | /* Strncmp uses unsigned semantics for comparison. */ | |
97020474 SP |
54 | int |
55 | simple_strncmp (const char *s1, const char *s2, size_t n) | |
56 | { | |
57 | int ret = 0; | |
58 | ||
59 | while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0 | |
60 | && *s1++); | |
61 | return ret; | |
62 | } | |
63 | ||
cee82e70 SL |
64 | #endif /* !WIDE */ |
65 | ||
66 | typedef int (*proto_t) (const CHAR *, const CHAR *, size_t); | |
67 | ||
cee82e70 SL |
68 | IMPL (SIMPLE_STRNCMP, 0) |
69 | IMPL (STRNCMP, 1) | |
70 | ||
71 | ||
97020474 | 72 | static void |
ad4e816e SP |
73 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, const CHAR |
74 | *s2, size_t n, int exp_result) | |
97020474 | 75 | { |
44558701 WN |
76 | size_t i, iters = INNER_LOOP_ITERS; |
77 | timing_t start, stop, cur; | |
78 | ||
79 | TIMING_NOW (start); | |
80 | for (i = 0; i < iters; ++i) | |
97020474 | 81 | { |
44558701 | 82 | CALL (impl, s1, s2, n); |
97020474 | 83 | } |
44558701 WN |
84 | TIMING_NOW (stop); |
85 | ||
86 | TIMING_DIFF (cur, start, stop); | |
87 | ||
ad4e816e | 88 | json_element_double (json_ctx, (double) cur / (double) iters); |
97020474 SP |
89 | } |
90 | ||
91 | static void | |
ad4e816e SP |
92 | do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, |
93 | size_t n, int max_char, int exp_result) | |
97020474 SP |
94 | { |
95 | size_t i, align_n; | |
cee82e70 | 96 | CHAR *s1, *s2; |
97020474 | 97 | |
97020474 SP |
98 | align1 &= 15; |
99 | align2 &= 15; | |
cee82e70 | 100 | align_n = (page_size - n * CHARBYTES) & 15; |
97020474 | 101 | |
ad4e816e SP |
102 | json_element_object_begin (json_ctx); |
103 | json_attr_uint (json_ctx, "strlen", (double) len); | |
104 | json_attr_uint (json_ctx, "len", (double) n); | |
105 | json_attr_uint (json_ctx, "align1", (double) align1); | |
106 | json_attr_uint (json_ctx, "align2", (double) align2); | |
107 | json_array_begin (json_ctx, "timings"); | |
97020474 SP |
108 | |
109 | FOR_EACH_IMPL (impl, 0) | |
7bb3a8a5 | 110 | { |
014efdd7 | 111 | alloc_bufs (); |
7bb3a8a5 SP |
112 | s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES); |
113 | s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES); | |
114 | ||
115 | if (align1 < align_n) | |
116 | s1 = (CHAR *) ((char *) s1 - (align_n - align1)); | |
117 | ||
118 | if (align2 < align_n) | |
119 | s2 = (CHAR *) ((char *) s2 - (align_n - align2)); | |
120 | ||
121 | for (i = 0; i < n; i++) | |
122 | s1[i] = s2[i] = 1 + 23 * i % max_char; | |
123 | ||
124 | if (len < n) | |
125 | { | |
126 | s1[len] = 0; | |
127 | s2[len] = 0; | |
128 | if (exp_result < 0) | |
129 | s2[len] = 32; | |
130 | else if (exp_result > 0) | |
131 | s1[len] = 64; | |
132 | } | |
133 | ||
134 | do_one_test (json_ctx, impl, s1, s2, n, exp_result); | |
135 | } | |
97020474 | 136 | |
ad4e816e SP |
137 | json_array_end (json_ctx); |
138 | json_element_object_end (json_ctx); | |
97020474 SP |
139 | } |
140 | ||
141 | static void | |
ad4e816e SP |
142 | do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t |
143 | n, int max_char, int exp_result) | |
97020474 SP |
144 | { |
145 | size_t i; | |
cee82e70 | 146 | CHAR *s1, *s2; |
97020474 SP |
147 | |
148 | if (n == 0) | |
149 | return; | |
150 | ||
cee82e70 SL |
151 | align1 &= 63; |
152 | if (align1 + (n + 1) * CHARBYTES >= page_size) | |
97020474 SP |
153 | return; |
154 | ||
155 | align2 &= 7; | |
cee82e70 | 156 | if (align2 + (n + 1) * CHARBYTES >= page_size) |
97020474 SP |
157 | return; |
158 | ||
ad4e816e SP |
159 | json_element_object_begin (json_ctx); |
160 | json_attr_uint (json_ctx, "strlen", (double) len); | |
161 | json_attr_uint (json_ctx, "len", (double) n); | |
162 | json_attr_uint (json_ctx, "align1", (double) align1); | |
163 | json_attr_uint (json_ctx, "align2", (double) align2); | |
164 | json_array_begin (json_ctx, "timings"); | |
97020474 SP |
165 | |
166 | FOR_EACH_IMPL (impl, 0) | |
7bb3a8a5 | 167 | { |
014efdd7 | 168 | alloc_bufs (); |
7bb3a8a5 SP |
169 | s1 = (CHAR *) (buf1 + align1); |
170 | s2 = (CHAR *) (buf2 + align2); | |
171 | ||
172 | for (i = 0; i < n; i++) | |
173 | s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char; | |
174 | ||
175 | s1[n] = 24 + exp_result; | |
176 | s2[n] = 23; | |
177 | s1[len] = 0; | |
178 | s2[len] = 0; | |
179 | if (exp_result < 0) | |
180 | s2[len] = 32; | |
181 | else if (exp_result > 0) | |
182 | s1[len] = 64; | |
183 | if (len >= n) | |
184 | s2[n - 1] -= exp_result; | |
185 | ||
186 | do_one_test (json_ctx, impl, s1, s2, n, exp_result); | |
187 | } | |
97020474 | 188 | |
ad4e816e SP |
189 | json_array_end (json_ctx); |
190 | json_element_object_end (json_ctx); | |
97020474 SP |
191 | } |
192 | ||
193 | int | |
194 | test_main (void) | |
195 | { | |
ad4e816e | 196 | json_ctx_t json_ctx; |
97020474 SP |
197 | size_t i; |
198 | ||
199 | test_init (); | |
200 | ||
ad4e816e SP |
201 | json_init (&json_ctx, 0, stdout); |
202 | ||
203 | json_document_begin (&json_ctx); | |
204 | json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); | |
205 | ||
206 | json_attr_object_begin (&json_ctx, "functions"); | |
207 | json_attr_object_begin (&json_ctx, TEST_NAME); | |
208 | json_attr_string (&json_ctx, "bench-variant", "default"); | |
209 | ||
210 | json_array_begin (&json_ctx, "ifuncs"); | |
97020474 | 211 | FOR_EACH_IMPL (impl, 0) |
ad4e816e SP |
212 | json_element_string (&json_ctx, impl->name); |
213 | json_array_end (&json_ctx); | |
214 | ||
215 | json_array_begin (&json_ctx, "results"); | |
97020474 SP |
216 | |
217 | for (i =0; i < 16; ++i) | |
218 | { | |
ad4e816e SP |
219 | do_test (&json_ctx, 0, 0, 8, i, 127, 0); |
220 | do_test (&json_ctx, 0, 0, 8, i, 127, -1); | |
221 | do_test (&json_ctx, 0, 0, 8, i, 127, 1); | |
222 | do_test (&json_ctx, i, i, 8, i, 127, 0); | |
223 | do_test (&json_ctx, i, i, 8, i, 127, 1); | |
224 | do_test (&json_ctx, i, i, 8, i, 127, -1); | |
225 | do_test (&json_ctx, i, 2 * i, 8, i, 127, 0); | |
226 | do_test (&json_ctx, 2 * i, i, 8, i, 127, 1); | |
227 | do_test (&json_ctx, i, 3 * i, 8, i, 127, -1); | |
228 | do_test (&json_ctx, 0, 0, 8, i, 255, 0); | |
229 | do_test (&json_ctx, 0, 0, 8, i, 255, -1); | |
230 | do_test (&json_ctx, 0, 0, 8, i, 255, 1); | |
231 | do_test (&json_ctx, i, i, 8, i, 255, 0); | |
232 | do_test (&json_ctx, i, i, 8, i, 255, 1); | |
233 | do_test (&json_ctx, i, i, 8, i, 255, -1); | |
234 | do_test (&json_ctx, i, 2 * i, 8, i, 255, 0); | |
235 | do_test (&json_ctx, 2 * i, i, 8, i, 255, 1); | |
236 | do_test (&json_ctx, i, 3 * i, 8, i, 255, -1); | |
97020474 SP |
237 | } |
238 | ||
239 | for (i = 1; i < 8; ++i) | |
240 | { | |
ad4e816e SP |
241 | do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0); |
242 | do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 1); | |
243 | do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, -1); | |
244 | do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 0); | |
245 | do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, 1); | |
246 | do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 255, -1); | |
247 | do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 0); | |
248 | do_test (&json_ctx, 8 - i, 2 * i, 8 << i, 16 << i, 127, 1); | |
249 | do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 0); | |
250 | do_test (&json_ctx, 2 * i, i, 8 << i, 16 << i, 255, 1); | |
97020474 SP |
251 | } |
252 | ||
ad4e816e SP |
253 | do_test_limit (&json_ctx, 4, 0, 21, 20, 127, 0); |
254 | do_test_limit (&json_ctx, 0, 4, 21, 20, 127, 0); | |
255 | do_test_limit (&json_ctx, 8, 0, 25, 24, 127, 0); | |
256 | do_test_limit (&json_ctx, 0, 8, 25, 24, 127, 0); | |
97020474 SP |
257 | |
258 | for (i = 0; i < 8; ++i) | |
259 | { | |
ad4e816e SP |
260 | do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 127, 0); |
261 | do_test_limit (&json_ctx, 0, 0, 17 - i, 16 - i, 255, 0); | |
262 | do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 0); | |
263 | do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, 1); | |
264 | do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 127, -1); | |
265 | do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 0); | |
266 | do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, 1); | |
267 | do_test_limit (&json_ctx, 0, 0, 15 - i, 16 - i, 255, -1); | |
97020474 SP |
268 | } |
269 | ||
ad4e816e SP |
270 | json_array_end (&json_ctx); |
271 | json_attr_object_end (&json_ctx); | |
272 | json_attr_object_end (&json_ctx); | |
273 | json_document_end (&json_ctx); | |
274 | ||
97020474 SP |
275 | return ret; |
276 | } | |
277 | ||
b598e134 | 278 | #include <support/test-driver.c> |