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

source code of glibc/benchtests/bench-memcmp.c