1/* Measure strncat 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#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
36typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
37
38CHAR *
39generic_strncat (CHAR *dst, const CHAR *src, size_t n)
40{
41 CHAR *end = dst + STRLEN (dst);
42 n = STRNLEN (src, n);
43 end[n] = 0;
44 MEMCPY (end, src, n);
45 return dst;
46}
47
48IMPL (STRNCAT, 2)
49IMPL (generic_strncat, 0)
50
51static void
52do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n)
53{
54 size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS8;
55 timing_t start, stop, cur;
56
57 if (CALL (impl, dst, src, n) != dst)
58 {
59 error (status: 0, errnum: 0, format: "Wrong result in function %s %p != %p", impl->name,
60 CALL (impl, dst, src, n), dst);
61 ret = 1;
62 return;
63 }
64
65 size_t len = STRLEN (src);
66 if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
67 {
68 error (status: 0, errnum: 0, format: "Incorrect concatenation in function %s",
69 impl->name);
70 ret = 1;
71 return;
72 }
73 if (n < len && dst[k + n] != '\0')
74 {
75 error (status: 0, errnum: 0, format: "There is no zero in the end of output string in %s",
76 impl->name);
77 ret = 1;
78 return;
79 }
80
81 TIMING_NOW (start);
82 for (i = 0; i < iters; ++i)
83 {
84 dst[k] = '\0';
85 CALL (impl, dst, src, n);
86 }
87 TIMING_NOW (stop);
88
89 TIMING_DIFF (cur, start, stop);
90
91 TIMING_PRINT_MEAN ((double) cur, (double) iters);
92}
93
94static void
95do_test (size_t align1, size_t align2, size_t len1, size_t len2,
96 size_t n, int max_char)
97{
98 size_t i;
99 CHAR *s1, *s2;
100
101 align1 &= 7;
102 if ((align1 + len1) * sizeof (CHAR) >= page_size)
103 return;
104 if ((align1 + n) * sizeof (CHAR) > page_size)
105 return;
106 align2 &= 7;
107 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
108 return;
109 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
110 return;
111 s1 = (CHAR *) (buf1) + align1;
112 s2 = (CHAR *) (buf2) + align2;
113
114 for (i = 0; i < len1; ++i)
115 s1[i] = 32 + 23 * i % (max_char - 32);
116 s1[len1] = '\0';
117
118 for (i = 0; i < len2; i++)
119 s2[i] = 32 + 23 * i % (max_char - 32);
120
121 printf (format: "Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
122 len1, len2, align1, align2, n);
123
124 FOR_EACH_IMPL (impl, 0)
125 {
126 s2[len2] = '\0';
127 do_one_test (impl, dst: s2, src: s1, n);
128 }
129
130 putchar (c: '\n');
131}
132
133int
134main (void)
135{
136 size_t i, n;
137
138 test_init ();
139
140 printf (format: "%28s", "");
141 FOR_EACH_IMPL (impl, 0)
142 printf (format: "\t%s", impl->name);
143 putchar (c: '\n');
144
145 for (n = 2; n <= 2048; n*=4)
146 {
147 do_test (align1: 0, align2: 2, len1: 2, len2: 2, n, SMALL_CHAR);
148 do_test (align1: 0, align2: 0, len1: 4, len2: 4, n, SMALL_CHAR);
149 do_test (align1: 4, align2: 0, len1: 4, len2: 4, n, BIG_CHAR);
150 do_test (align1: 0, align2: 0, len1: 8, len2: 8, n, SMALL_CHAR);
151 do_test (align1: 0, align2: 8, len1: 8, len2: 8, n, SMALL_CHAR);
152
153 for (i = 1; i < 8; ++i)
154 {
155 do_test (align1: 0, align2: 0, len1: 8 << i, len2: 8 << i, n, SMALL_CHAR);
156 do_test (align1: 8 - i, align2: 2 * i, len1: 8 << i, len2: 8 << i, n, SMALL_CHAR);
157 do_test (align1: 0, align2: 0, len1: 8 << i, len2: 2 << i, n, SMALL_CHAR);
158 do_test (align1: 8 - i, align2: 2 * i, len1: 8 << i, len2: 2 << i, n, SMALL_CHAR);
159 }
160
161 for (i = 1; i < 8; ++i)
162 {
163 do_test (align1: i, align2: 2 * i, len1: 8 << i, len2: 1, n, SMALL_CHAR);
164 do_test (align1: 2 * i, align2: i, len1: 8 << i, len2: 1, n, BIG_CHAR);
165 do_test (align1: i, align2: i, len1: 8 << i, len2: 10, n, SMALL_CHAR);
166 }
167 }
168
169 return ret;
170}
171

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