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

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