1 /* Measure strcmp and wcscmp functions.
2 Copyright (C) 2013-2023 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 <https://www.gnu.org/licenses/>. */
21 # define TEST_NAME "wcscmp"
23 # define TEST_NAME "strcmp"
25 #include "bench-string.h"
29 # define CHARBYTESLOG 2
30 # define MIDCHAR 0x7fffffff
31 # define LARGECHAR 0xfffffffe
33 # define CHARBYTESLOG 0
35 # define LARGECHAR 0xfe
38 generic_strcmp (const char *s1
, const char *s2
);
40 IMPL (generic_strcmp
, 0)
44 typedef int (*proto_t
) (const CHAR
*, const CHAR
*);
49 do_one_test (json_ctx_t
*json_ctx
, impl_t
*impl
,
50 const CHAR
*s1
, const CHAR
*s2
,
53 size_t i
, iters
= INNER_LOOP_ITERS8
/ 2;
54 timing_t start
, stop
, cur
;
57 for (i
= 0; i
< iters
; ++i
)
63 TIMING_DIFF (cur
, start
, stop
);
65 json_element_double (json_ctx
, (double) cur
/ (double) iters
);
69 do_test (json_ctx_t
*json_ctx
, size_t align1
, size_t align2
, size_t len
,
70 int max_char
, int exp_result
, int at_end
)
79 align1
&= ~(CHARBYTES
- 1);
80 align2
&= ~(CHARBYTES
- 1);
82 align1
&= (getpagesize () - 1);
83 if (align1
+ (len
+ 1) * CHARBYTES
>= page_size
)
86 align2
&= (getpagesize () - 1);
87 if (align2
+ (len
+ 1) * CHARBYTES
>= page_size
)
90 /* Put them close to the end of page. */
93 i
= align1
+ CHARBYTES
* (len
+ 2);
94 align1
= ((page_size
- i
) / 16 * 16) + align1
;
95 i
= align2
+ CHARBYTES
* (len
+ 2);
96 align2
= ((page_size
- i
) / 16 * 16) + align2
;
99 s1
= (CHAR
*)(buf1
+ align1
);
100 s2
= (CHAR
*)(buf2
+ align2
);
102 for (i
= 0; i
< len
; i
++)
103 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
105 s1
[len
] = s2
[len
] = 0;
107 s2
[len
+ 1] = 24 + exp_result
;
108 s2
[len
- 1] -= exp_result
;
110 json_element_object_begin (json_ctx
);
111 json_attr_uint (json_ctx
, "length", (double)len
);
112 json_attr_uint (json_ctx
, "align1", (double)align1
);
113 json_attr_uint (json_ctx
, "align2", (double)align2
);
114 json_array_begin (json_ctx
, "timings");
116 FOR_EACH_IMPL (impl
, 0)
117 do_one_test (json_ctx
, impl
, s1
, s2
, exp_result
);
119 json_array_end (json_ctx
);
120 json_element_object_end (json_ctx
);
124 do_one_test_page_boundary (json_ctx_t
*json_ctx
, CHAR
*s1
, CHAR
*s2
,
125 size_t align1
, size_t align2
, size_t len
,
128 json_element_object_begin (json_ctx
);
129 json_attr_uint (json_ctx
, "length", (double) len
);
130 json_attr_uint (json_ctx
, "align1", (double) align1
);
131 json_attr_uint (json_ctx
, "align2", (double) align2
);
132 json_array_begin (json_ctx
, "timings");
133 FOR_EACH_IMPL (impl
, 0)
134 do_one_test (json_ctx
, impl
, s1
, s2
, exp_result
);
135 json_array_end (json_ctx
);
136 json_element_object_end (json_ctx
);
140 do_test_page_boundary (json_ctx_t
*json_ctx
)
142 /* To trigger bug 25933, we need a size that is equal to the vector
143 length times 4. In the case of AVX2 for Intel, we need 32 * 4. We
144 make this test generic and run it for all architectures as additional
145 boundary testing for such related algorithms. */
146 size_t size
= 32 * 4;
148 CHAR
*s1
= (CHAR
*) (buf1
+ (BUF1PAGES
- 1) * page_size
);
149 CHAR
*s2
= (CHAR
*) (buf2
+ (BUF1PAGES
- 1) * page_size
);
152 memset (s1
, 'a', page_size
);
153 memset (s2
, 'a', page_size
);
155 s1
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
156 s2
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
158 /* Iterate over a size that is just below where we expect the bug to
159 trigger up to the size we expect will trigger the bug e.g. [99-128].
160 Likewise iterate the start of two strings between 30 and 31 bytes
161 away from the boundary to simulate alignment changes. */
162 for (size_t s
= 99; s
<= size
; s
++)
163 for (size_t s1a
= 30; s1a
< 32; s1a
++)
164 for (size_t s2a
= 30; s2a
< 32; s2a
++)
166 size_t align1
= (page_size
/ CHARBYTES
- s
) - s1a
;
167 size_t align2
= (page_size
/ CHARBYTES
- s
) - s2a
;
168 CHAR
*s1p
= s1
+ align1
;
169 CHAR
*s2p
= s2
+ align2
;
170 len
= (page_size
/ CHARBYTES
) - 1 - align1
;
171 exp_result
= STRCMP (s1p
, s2p
);
172 do_one_test_page_boundary (json_ctx
, s1p
, s2p
, align1
, align2
,
182 size_t pg_sz
= getpagesize ();
186 json_init (&json_ctx
, 0, stdout
);
188 json_document_begin (&json_ctx
);
189 json_attr_string (&json_ctx
, "timing_type", TIMING_TYPE
);
191 json_attr_object_begin (&json_ctx
, "functions");
192 json_attr_object_begin (&json_ctx
, TEST_NAME
);
193 json_attr_string (&json_ctx
, "bench-variant", "default");
195 json_array_begin (&json_ctx
, "ifuncs");
196 FOR_EACH_IMPL (impl
, 0)
197 json_element_string (&json_ctx
, impl
->name
);
198 json_array_end (&json_ctx
);
200 json_array_begin (&json_ctx
, "results");
201 for (k
= 0; k
< 2; ++k
)
203 for (i
= 1; i
< 32; ++i
)
205 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 0, k
);
206 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, 1, k
);
207 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* i
, i
, MIDCHAR
, -1, k
);
210 for (i
= 1; i
<= 8192;)
212 /* No page crosses. */
213 do_test (&json_ctx
, 0, 0, i
, MIDCHAR
, 0, k
);
214 do_test (&json_ctx
, i
* CHARBYTES
, 0, i
, MIDCHAR
, 0, k
);
215 do_test (&json_ctx
, 0, i
* CHARBYTES
, i
, MIDCHAR
, 0, k
);
217 /* False page crosses. */
218 do_test (&json_ctx
, pg_sz
/ 2, pg_sz
/ 2 - CHARBYTES
, i
, MIDCHAR
, 0,
220 do_test (&json_ctx
, pg_sz
/ 2 - CHARBYTES
, pg_sz
/ 2, i
, MIDCHAR
, 0,
223 do_test (&json_ctx
, pg_sz
- (i
* CHARBYTES
), 0, i
, MIDCHAR
, 0, k
);
224 do_test (&json_ctx
, 0, pg_sz
- (i
* CHARBYTES
), i
, MIDCHAR
, 0, k
);
226 /* Real page cross. */
227 for (j
= 16; j
< 128; j
+= 16)
229 do_test (&json_ctx
, pg_sz
- j
, 0, i
, MIDCHAR
, 0, k
);
230 do_test (&json_ctx
, 0, pg_sz
- j
, i
, MIDCHAR
, 0, k
);
232 do_test (&json_ctx
, pg_sz
- j
, pg_sz
- j
/ 2, i
, MIDCHAR
, 0, k
);
233 do_test (&json_ctx
, pg_sz
- j
/ 2, pg_sz
- j
, i
, MIDCHAR
, 0, k
);
254 for (i
= 1; i
< 10 + CHARBYTESLOG
; ++i
)
256 do_test (&json_ctx
, 0, 0, 2 << i
, MIDCHAR
, 0, k
);
257 do_test (&json_ctx
, 0, 0, 2 << i
, LARGECHAR
, 0, k
);
258 do_test (&json_ctx
, 0, 0, 2 << i
, MIDCHAR
, 1, k
);
259 do_test (&json_ctx
, 0, 0, 2 << i
, LARGECHAR
, 1, k
);
260 do_test (&json_ctx
, 0, 0, 2 << i
, MIDCHAR
, -1, k
);
261 do_test (&json_ctx
, 0, 0, 2 << i
, LARGECHAR
, -1, k
);
262 do_test (&json_ctx
, 0, CHARBYTES
* i
, 2 << i
, MIDCHAR
, 1, k
);
263 do_test (&json_ctx
, CHARBYTES
* i
, CHARBYTES
* (i
+ 1), 2 << i
,
267 for (i
= 1; i
< 8; ++i
)
269 do_test (&json_ctx
, CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
,
271 do_test (&json_ctx
, 2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
,
273 do_test (&json_ctx
, CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
,
275 do_test (&json_ctx
, 2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
,
277 do_test (&json_ctx
, CHARBYTES
* i
, 2 * CHARBYTES
* i
, 8 << i
,
279 do_test (&json_ctx
, 2 * CHARBYTES
* i
, CHARBYTES
* i
, 8 << i
,
283 do_test_page_boundary (&json_ctx
);
285 json_array_end (&json_ctx
);
286 json_attr_object_end (&json_ctx
);
287 json_attr_object_end (&json_ctx
);
288 json_document_end (&json_ctx
);
293 #include <support/test-driver.c>
297 # define STRCMP generic_strcmp
298 # include <string/strcmp.c>