]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-memcmp.c
benchtests: Make bench-memcmp print json
[thirdparty/glibc.git] / benchtests / bench-memcmp.c
1 /* Measure memcmp functions.
2 Copyright (C) 2013-2018 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 "wmemcmp"
22 #else
23 # define TEST_NAME "memcmp"
24 #endif
25 #include "bench-string.h"
26 #ifdef WIDE
27 # include <inttypes.h>
28 # include <wchar.h>
29
30 # define MEMCMP wmemcmp
31 # define MEMCPY wmemcpy
32 # define SIMPLE_MEMCMP simple_wmemcmp
33 # define CHAR wchar_t
34 # define UCHAR wchar_t
35 # define CHARBYTES 4
36 # define CHAR__MIN WCHAR_MIN
37 # define CHAR__MAX WCHAR_MAX
38 int
39 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
40 {
41 int ret = 0;
42 /* Warning!
43 wmemcmp has to use SIGNED comparison for elements.
44 memcmp has to use UNSIGNED comparison for elemnts.
45 */
46 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
47 return ret;
48 }
49 #else
50 # include <limits.h>
51
52 # define MEMCMP memcmp
53 # define MEMCPY memcpy
54 # define SIMPLE_MEMCMP simple_memcmp
55 # define CHAR char
56 # define MAX_CHAR 255
57 # define UCHAR unsigned char
58 # define CHARBYTES 1
59 # define CHAR__MIN CHAR_MIN
60 # define CHAR__MAX CHAR_MAX
61
62 int
63 simple_memcmp (const char *s1, const char *s2, size_t n)
64 {
65 int ret = 0;
66
67 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
68 return ret;
69 }
70 #endif
71
72 # include "json-lib.h"
73
74 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
75
76 IMPL (SIMPLE_MEMCMP, 0)
77 IMPL (MEMCMP, 1)
78
79 static void
80 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1,
81 const CHAR *s2, size_t len, int exp_result)
82 {
83 size_t i, iters = INNER_LOOP_ITERS;
84 timing_t start, stop, cur;
85
86 TIMING_NOW (start);
87 for (i = 0; i < iters; ++i)
88 {
89 CALL (impl, s1, s2, len);
90 }
91 TIMING_NOW (stop);
92
93 TIMING_DIFF (cur, start, stop);
94
95 json_element_double (json_ctx, (double) cur / (double) iters);
96 }
97
98 static void
99 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
100 int exp_result)
101 {
102 size_t i;
103 CHAR *s1, *s2;
104
105 if (len == 0)
106 return;
107
108 align1 &= 63;
109 if (align1 + (len + 1) * CHARBYTES >= page_size)
110 return;
111
112 align2 &= 63;
113 if (align2 + (len + 1) * CHARBYTES >= page_size)
114 return;
115
116 json_element_object_begin (json_ctx);
117 json_attr_uint (json_ctx, "length", (double) len);
118 json_attr_uint (json_ctx, "align1", (double) align1);
119 json_attr_uint (json_ctx, "align2", (double) align2);
120 json_array_begin (json_ctx, "timings");
121
122 FOR_EACH_IMPL (impl, 0)
123 {
124 s1 = (CHAR *) (buf1 + align1);
125 s2 = (CHAR *) (buf2 + align2);
126
127 for (i = 0; i < len; i++)
128 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
129
130 s1[len] = align1;
131 s2[len] = align2;
132 s2[len - 1] -= exp_result;
133
134 do_one_test (json_ctx, impl, s1, s2, len, exp_result);
135 realloc_bufs ();
136 }
137
138 json_array_end (json_ctx);
139 json_element_object_end (json_ctx);
140 }
141
142 int
143 test_main (void)
144 {
145 json_ctx_t json_ctx;
146 size_t i;
147
148 test_init ();
149
150 json_init (&json_ctx, 0, stdout);
151
152 json_document_begin (&json_ctx);
153 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
154
155 json_attr_object_begin (&json_ctx, "functions");
156 json_attr_object_begin (&json_ctx, TEST_NAME);
157 json_attr_string (&json_ctx, "bench-variant", "default");
158
159 json_array_begin (&json_ctx, "ifuncs");
160 FOR_EACH_IMPL (impl, 0)
161 json_element_string (&json_ctx, impl->name);
162 json_array_end (&json_ctx);
163
164 json_array_begin (&json_ctx, "results");
165 for (i = 1; i < 16; ++i)
166 {
167 do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 0);
168 do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 1);
169 do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, -1);
170 }
171
172 for (i = 0; i < 16; ++i)
173 {
174 do_test (&json_ctx, 0, 0, i, 0);
175 do_test (&json_ctx, 0, 0, i, 1);
176 do_test (&json_ctx, 0, 0, i, -1);
177 }
178
179 for (i = 1; i < 10; ++i)
180 {
181 do_test (&json_ctx, 0, 0, 2 << i, 0);
182 do_test (&json_ctx, 0, 0, 2 << i, 1);
183 do_test (&json_ctx, 0, 0, 2 << i, -1);
184 do_test (&json_ctx, 0, 0, 16 << i, 0);
185 do_test (&json_ctx, (8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
186 do_test (&json_ctx, 0, 0, 16 << i, 1);
187 do_test (&json_ctx, 0, 0, 16 << i, -1);
188 }
189
190 for (i = 1; i < 8; ++i)
191 {
192 do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
193 do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
194 do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
195 }
196
197 json_array_end (&json_ctx);
198 json_attr_object_end (&json_ctx);
199 json_attr_object_end (&json_ctx);
200 json_document_end (&json_ctx);
201
202 return ret;
203 }
204
205 #include <support/test-driver.c>