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