1 /* Measure memcmp functions.
2 Copyright (C) 2013-2021 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 "wmemcmp"
23 # define TEST_NAME "memcmp"
25 #include "bench-string.h"
28 # define SIMPLE_MEMCMP simple_wmemcmp
30 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
34 wmemcmp has to use SIGNED comparison for elements.
35 memcmp has to use UNSIGNED comparison for elemnts.
37 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
43 # define SIMPLE_MEMCMP simple_memcmp
46 simple_memcmp (const char *s1, const char *s2, size_t n)
50 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
55 # include "json-lib.h"
57 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
59 IMPL (SIMPLE_MEMCMP, 0)
63 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1,
64 const CHAR *s2, size_t len, int exp_result)
66 size_t i, iters = INNER_LOOP_ITERS8;
67 timing_t start, stop, cur;
70 for (i = 0; i < iters; ++i)
72 CALL (impl, s1, s2, len);
76 TIMING_DIFF (cur, start, stop);
78 json_element_double (json_ctx, (double) cur / (double) iters);
82 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
91 align1 &= (4096 - CHARBYTES);
92 if (align1 + (len + 1) * CHARBYTES >= page_size)
95 align2 &= (4096 - CHARBYTES);
96 if (align2 + (len + 1) * CHARBYTES >= page_size)
99 json_element_object_begin (json_ctx);
100 json_attr_uint (json_ctx, "length", (double) len);
101 json_attr_uint (json_ctx, "align1", (double) align1);
102 json_attr_uint (json_ctx, "align2", (double) align2);
103 json_attr_uint (json_ctx, "result", (double) exp_result);
104 json_array_begin (json_ctx, "timings");
106 FOR_EACH_IMPL (impl, 0)
108 s1 = (CHAR *) (buf1 + align1);
109 s2 = (CHAR *) (buf2 + align2);
111 for (i = 0; i < len; i++)
112 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % MAX_CHAR;
116 s2[len - 1] -= exp_result;
118 do_one_test (json_ctx, impl, s1, s2, len, exp_result);
122 json_array_end (json_ctx);
123 json_element_object_end (json_ctx);
134 json_init (&json_ctx, 0, stdout);
136 json_document_begin (&json_ctx);
137 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
139 json_attr_object_begin (&json_ctx, "functions");
140 json_attr_object_begin (&json_ctx, TEST_NAME);
141 json_attr_string (&json_ctx, "bench-variant", "default");
143 json_array_begin (&json_ctx, "ifuncs");
144 FOR_EACH_IMPL (impl, 0)
145 json_element_string (&json_ctx, impl->name);
146 json_array_end (&json_ctx);
148 json_array_begin (&json_ctx, "results");
149 for (i = 1; i < 32; ++i)
151 do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 0);
152 do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 1);
153 do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, -1);
156 for (i = 0; i < 32; ++i)
158 do_test (&json_ctx, 0, 0, i, 0);
159 do_test (&json_ctx, 0, 0, i, 1);
160 do_test (&json_ctx, 0, 0, i, -1);
161 do_test (&json_ctx, 4096 - i, 0, i, 0);
162 do_test (&json_ctx, 4096 - i, 0, i, 1);
163 do_test (&json_ctx, 4096 - i, 0, i, -1);
166 for (i = 33; i < 385; i += 32)
168 do_test (&json_ctx, 0, 0, i, 0);
169 do_test (&json_ctx, 0, 0, i, 1);
170 do_test (&json_ctx, 0, 0, i, -1);
171 do_test (&json_ctx, i, 0, i, 0);
172 do_test (&json_ctx, 0, i, i, 1);
173 do_test (&json_ctx, i, i, i, -1);
176 for (i = 1; i < 10; ++i)
178 do_test (&json_ctx, 0, 0, 2 << i, 0);
179 do_test (&json_ctx, 0, 0, 2 << i, 1);
180 do_test (&json_ctx, 0, 0, 2 << i, -1);
181 do_test (&json_ctx, (8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
182 do_test (&json_ctx, 0, 0, 16 << i, 0);
183 do_test (&json_ctx, 0, 0, 16 << i, 1);
184 do_test (&json_ctx, 0, 0, 16 << i, -1);
185 do_test (&json_ctx, i, 0, 2 << i, 0);
186 do_test (&json_ctx, 0, i, 2 << i, 1);
187 do_test (&json_ctx, i, i, 2 << i, -1);
188 do_test (&json_ctx, i, 0, 16 << i, 0);
189 do_test (&json_ctx, 0, i, 16 << i, 1);
190 do_test (&json_ctx, i, i, 16 << i, -1);
193 for (i = 1; i < 10; ++i)
195 do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
196 do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
197 do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
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);
208 #include <support/test-driver.c>