]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strncpy.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / benchtests / bench-strncpy.c
1 /* Measure strncpy functions.
2 Copyright (C) 2013-2016 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 #ifdef WIDE
20 # include <wchar.h>
21 # define CHAR wchar_t
22 # define UCHAR wchar_t
23 # define BIG_CHAR WCHAR_MAX
24 # define SMALL_CHAR 1273
25 # define MEMCMP wmemcmp
26 # define MEMSET wmemset
27 # define STRNLEN wcsnlen
28 #else
29 # define CHAR char
30 # define UCHAR unsigned char
31 # define BIG_CHAR CHAR_MAX
32 # define SMALL_CHAR 127
33 # define MEMCMP memcmp
34 # define MEMSET memset
35 # define STRNLEN strnlen
36 #endif /* !WIDE */
37
38
39 #ifndef STRNCPY_RESULT
40 # define STRNCPY_RESULT(dst, len, n) dst
41 # define TEST_MAIN
42 # ifndef WIDE
43 # define TEST_NAME "strncpy"
44 # else
45 # define TEST_NAME "wcsncpy"
46 # endif /* WIDE */
47 # include "bench-string.h"
48 # ifndef WIDE
49 # define SIMPLE_STRNCPY simple_strncpy
50 # define STUPID_STRNCPY stupid_strncpy
51 # define STRNCPY strncpy
52 # else
53 # define SIMPLE_STRNCPY simple_wcsncpy
54 # define STUPID_STRNCPY stupid_wcsncpy
55 # define STRNCPY wcsncpy
56 # endif /* WIDE */
57
58 CHAR *SIMPLE_STRNCPY (CHAR *, const CHAR *, size_t);
59 CHAR *STUPID_STRNCPY (CHAR *, const CHAR *, size_t);
60
61 IMPL (STUPID_STRNCPY, 0)
62 IMPL (SIMPLE_STRNCPY, 0)
63 IMPL (STRNCPY, 1)
64
65 CHAR *
66 SIMPLE_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
67 {
68 CHAR *ret = dst;
69 while (n--)
70 if ((*dst++ = *src++) == '\0')
71 {
72 while (n--)
73 *dst++ = '\0';
74 return ret;
75 }
76 return ret;
77 }
78
79 CHAR *
80 STUPID_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
81 {
82 size_t nc = STRNLEN (src, n);
83 size_t i;
84
85 for (i = 0; i < nc; ++i)
86 dst[i] = src[i];
87 for (; i < n; ++i)
88 dst[i] = '\0';
89 return dst;
90 }
91 #endif /* !STRNCPY_RESULT */
92
93 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
94
95 static void
96 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n)
97 {
98 size_t i, iters = INNER_LOOP_ITERS;
99 timing_t start, stop, cur;
100
101 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
102 {
103 error (0, 0, "Wrong result in function %s %p %p", impl->name,
104 CALL (impl, dst, src, n), dst);
105 ret = 1;
106 return;
107 }
108
109 if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
110 {
111 error (0, 0, "Wrong result in function %s", impl->name);
112 ret = 1;
113 return;
114 }
115
116 if (n > len)
117 {
118 size_t i;
119
120 for (i = len; i < n; ++i)
121 if (dst [i] != '\0')
122 {
123 error (0, 0, "Wrong result in function %s", impl->name);
124 ret = 1;
125 return;
126 }
127 }
128
129 TIMING_NOW (start);
130 for (i = 0; i < iters; ++i)
131 {
132 CALL (impl, dst, src, n);
133 }
134 TIMING_NOW (stop);
135
136 TIMING_DIFF (cur, start, stop);
137
138 TIMING_PRINT_MEAN ((double) cur, (double) iters);
139 }
140
141 static void
142 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
143 {
144 size_t i;
145 CHAR *s1, *s2;
146
147 /* For wcsncpy: align1 and align2 here mean alignment not in bytes,
148 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
149 align1 &= 7;
150 if ((align1 + len) * sizeof (CHAR) >= page_size)
151 return;
152
153 align2 &= 7;
154 if ((align2 + len) * sizeof (CHAR) >= page_size)
155 return;
156
157 s1 = (CHAR *) (buf1) + align1;
158 s2 = (CHAR *) (buf2) + align2;
159
160 for (i = 0; i < len; ++i)
161 s1[i] = 32 + 23 * i % (max_char - 32);
162 s1[len] = 0;
163 for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
164 ++i)
165 s1[i] = 32 + 32 * i % (max_char - 32);
166
167 printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2);
168
169 FOR_EACH_IMPL (impl, 0)
170 do_one_test (impl, s2, s1, len, n);
171
172 putchar ('\n');
173 }
174
175 int
176 test_main (void)
177 {
178 size_t i;
179
180 test_init ();
181
182 printf ("%28s", "");
183 FOR_EACH_IMPL (impl, 0)
184 printf ("\t%s", impl->name);
185 putchar ('\n');
186
187 for (i = 1; i < 8; ++i)
188 {
189 do_test (i, i, 16, 16, SMALL_CHAR);
190 do_test (i, i, 16, 16, BIG_CHAR);
191 do_test (i, 2 * i, 16, 16, SMALL_CHAR);
192 do_test (2 * i, i, 16, 16, BIG_CHAR);
193 do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
194 do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
195 do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
196 do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
197 }
198
199 for (i = 1; i < 8; ++i)
200 {
201 do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
202 do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
203 do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
204 do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
205 }
206
207 return ret;
208 }
209
210 #include "../test-skeleton.c"