1/* Measure strncat 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#ifndef WIDE
21# define TEST_NAME "strncat"
22#else
23# define TEST_NAME "wcsncat"
24# define generic_strncat generic_wcsncat
25#endif /* WIDE */
26#include "bench-string.h"
27
28#define BIG_CHAR MAX_CHAR
29
30#ifndef WIDE
31# define SMALL_CHAR 127
32#else
33# define SMALL_CHAR 1273
34#endif /* WIDE */
35
36#include "json-lib.h"
37
38typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
39
40CHAR *
41generic_strncat (CHAR *dst, const CHAR *src, size_t n)
42{
43 CHAR *end = dst + STRLEN (dst);
44 n = STRNLEN (src, n);
45 end[n] = 0;
46 MEMCPY (end, src, n);
47 return dst;
48}
49
50IMPL (STRNCAT, 2)
51IMPL (generic_strncat, 0)
52
53static void
54do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src,
55 size_t n)
56{
57 size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS8;
58 timing_t start, stop, cur;
59
60 if (CALL (impl, dst, src, n) != dst)
61 {
62 error (status: 0, errnum: 0, format: "Wrong result in function %s %p != %p", impl->name,
63 CALL (impl, dst, src, n), dst);
64 ret = 1;
65 return;
66 }
67
68 size_t len = STRLEN (src);
69 if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
70 {
71 error (status: 0, errnum: 0, format: "Incorrect concatenation in function %s", impl->name);
72 ret = 1;
73 return;
74 }
75 if (n < len && dst[k + n] != '\0')
76 {
77 error (status: 0, errnum: 0, format: "There is no zero in the end of output string in %s",
78 impl->name);
79 ret = 1;
80 return;
81 }
82
83 TIMING_NOW (start);
84 for (i = 0; i < iters; ++i)
85 {
86 dst[k] = '\0';
87 CALL (impl, dst, src, n);
88 }
89 TIMING_NOW (stop);
90
91 TIMING_DIFF (cur, start, stop);
92
93 json_element_double (ctx: json_ctx, d: (double) cur / (double) iters);
94}
95
96static void
97do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len1,
98 size_t len2, size_t n, int max_char)
99{
100 size_t i;
101 CHAR *s1, *s2;
102
103 align1 &= 7;
104 if ((align1 + len1) * sizeof (CHAR) >= page_size)
105 return;
106 if ((align1 + n) * sizeof (CHAR) > page_size)
107 return;
108 align2 &= 7;
109 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
110 return;
111 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
112 return;
113 s1 = (CHAR *) (buf1) + align1;
114 s2 = (CHAR *) (buf2) + align2;
115
116 for (i = 0; i < len1; ++i)
117 s1[i] = 32 + 23 * i % (max_char - 32);
118 s1[len1] = '\0';
119
120 for (i = 0; i < len2; i++)
121 s2[i] = 32 + 23 * i % (max_char - 32);
122
123 json_element_object_begin (ctx: json_ctx);
124 json_attr_uint (ctx: json_ctx, name: "align1", d: align1);
125 json_attr_uint (ctx: json_ctx, name: "align2", d: align2);
126 json_attr_uint (ctx: json_ctx, name: "len1", d: len1);
127 json_attr_uint (ctx: json_ctx, name: "len2", d: len2);
128 json_attr_uint (ctx: json_ctx, name: "n", d: n);
129 json_attr_uint (ctx: json_ctx, name: "max_char", d: max_char);
130
131 json_array_begin (ctx: json_ctx, name: "timings");
132
133 FOR_EACH_IMPL (impl, 0)
134 {
135 s2[len2] = '\0';
136 do_one_test (json_ctx, impl, dst: s2, src: s1, n);
137 }
138
139 json_array_end (ctx: json_ctx);
140 json_element_object_end (ctx: json_ctx);
141}
142
143int
144main (void)
145{
146 json_ctx_t json_ctx;
147 size_t i, n;
148
149 test_init ();
150
151 json_init (ctx: &json_ctx, indent_level: 0, stdout);
152
153 json_document_begin (ctx: &json_ctx);
154 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
155
156 json_attr_object_begin (ctx: &json_ctx, name: "functions");
157 json_attr_object_begin (ctx: &json_ctx, TEST_NAME);
158 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "");
159
160 json_array_begin (ctx: &json_ctx, name: "ifuncs");
161 FOR_EACH_IMPL (impl, 0)
162 json_element_string (ctx: &json_ctx, s: impl->name);
163 json_array_end (ctx: &json_ctx);
164
165 json_array_begin (ctx: &json_ctx, name: "results");
166
167 for (n = 2; n <= 2048; n *= 4)
168 {
169 do_test (json_ctx: &json_ctx, align1: 0, align2: 2, len1: 2, len2: 2, n, SMALL_CHAR);
170 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len1: 4, len2: 4, n, SMALL_CHAR);
171 do_test (json_ctx: &json_ctx, align1: 4, align2: 0, len1: 4, len2: 4, n, BIG_CHAR);
172 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len1: 8, len2: 8, n, SMALL_CHAR);
173 do_test (json_ctx: &json_ctx, align1: 0, align2: 8, len1: 8, len2: 8, n, SMALL_CHAR);
174
175 for (i = 1; i < 8; ++i)
176 {
177 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len1: 8 << i, len2: 8 << i, n, SMALL_CHAR);
178 do_test (json_ctx: &json_ctx, align1: 8 - i, align2: 2 * i, len1: 8 << i, len2: 8 << i, n, SMALL_CHAR);
179 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len1: 8 << i, len2: 2 << i, n, SMALL_CHAR);
180 do_test (json_ctx: &json_ctx, align1: 8 - i, align2: 2 * i, len1: 8 << i, len2: 2 << i, n, SMALL_CHAR);
181 }
182
183 for (i = 1; i < 8; ++i)
184 {
185 do_test (json_ctx: &json_ctx, align1: i, align2: 2 * i, len1: 8 << i, len2: 1, n, SMALL_CHAR);
186 do_test (json_ctx: &json_ctx, align1: 2 * i, align2: i, len1: 8 << i, len2: 1, n, BIG_CHAR);
187 do_test (json_ctx: &json_ctx, align1: i, align2: i, len1: 8 << i, len2: 10, n, SMALL_CHAR);
188 }
189 }
190
191 for (i = 128; i < 2048; i += i)
192 {
193 for (n = i - 64; n <= i + 64; n += 32)
194 {
195 do_test (json_ctx: &json_ctx, align1: 1, align2: 0, len1: i, len2: i, n, SMALL_CHAR);
196 do_test (json_ctx: &json_ctx, align1: 0, align2: i, len1: i, len2: i, n, SMALL_CHAR);
197 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len1: i, len2: i, n, SMALL_CHAR);
198 do_test (json_ctx: &json_ctx, align1: i, align2: i, len1: i, len2: i, n, SMALL_CHAR);
199 do_test (json_ctx: &json_ctx, align1: 1, align2: 0, len1: i, len2: n, n: i, SMALL_CHAR);
200 do_test (json_ctx: &json_ctx, align1: 0, align2: i, len1: i, len2: n, n: i, SMALL_CHAR);
201 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len1: i, len2: n, n: i, SMALL_CHAR);
202 do_test (json_ctx: &json_ctx, align1: i, align2: i, len1: i, len2: n, n: i, SMALL_CHAR);
203 }
204 }
205
206 json_array_end (ctx: &json_ctx);
207 json_attr_object_end (ctx: &json_ctx);
208 json_attr_object_end (ctx: &json_ctx);
209 json_document_end (ctx: &json_ctx);
210
211 return ret;
212}
213

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