]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strcpy.c
hurd: Add no-op version of __res_enable_icmp [BZ #24047]
[thirdparty/glibc.git] / benchtests / bench-strcpy.c
1 /* Measure strcpy functions.
2 Copyright (C) 2013-2019 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 #define BIG_CHAR MAX_CHAR
20
21 #ifdef WIDE
22 # define sfmt "ls"
23 # define SMALL_CHAR 1273
24 #else
25 # define sfmt "s"
26 # define SMALL_CHAR 127
27 #endif
28
29 #ifndef STRCPY_RESULT
30 # define STRCPY_RESULT(dst, len) dst
31 # define TEST_MAIN
32 # ifndef WIDE
33 # define TEST_NAME "strcpy"
34 # else
35 # define TEST_NAME "wcscpy"
36 # endif
37 # include "bench-string.h"
38 # ifndef WIDE
39 # define SIMPLE_STRCPY simple_strcpy
40 # else
41 # define SIMPLE_STRCPY simple_wcscpy
42 # endif
43
44 CHAR *SIMPLE_STRCPY (CHAR *, const CHAR *);
45
46 IMPL (SIMPLE_STRCPY, 0)
47 IMPL (STRCPY, 1)
48
49 CHAR *
50 SIMPLE_STRCPY (CHAR *dst, const CHAR *src)
51 {
52 CHAR *ret = dst;
53 while ((*dst++ = *src++) != '\0');
54 return ret;
55 }
56 #endif
57
58 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
59
60 static void
61 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src,
62 size_t len __attribute__((unused)))
63 {
64 size_t i, iters = INNER_LOOP_ITERS;
65 timing_t start, stop, cur;
66
67 if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
68 {
69 error (0, 0, "Wrong result in function %s %p %p", impl->name,
70 CALL (impl, dst, src), STRCPY_RESULT (dst, len));
71 ret = 1;
72 return;
73 }
74
75 if (STRCMP (dst, src) != 0)
76 {
77 error (0, 0,
78 "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
79 impl->name, dst, src);
80 ret = 1;
81 return;
82 }
83
84 TIMING_NOW (start);
85 for (i = 0; i < iters; ++i)
86 {
87 CALL (impl, dst, src);
88 }
89 TIMING_NOW (stop);
90
91 TIMING_DIFF (cur, start, stop);
92
93 TIMING_PRINT_MEAN ((double) cur, (double) iters);
94 }
95
96 static void
97 do_test (size_t align1, size_t align2, size_t len, int max_char)
98 {
99 size_t i;
100 CHAR *s1, *s2;
101 /* For wcscpy: align1 and align2 here mean alignment not in bytes,
102 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
103 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
104 align1 &= 7;
105 if ((align1 + len) * sizeof (CHAR) >= page_size)
106 return;
107
108 align2 &= 7;
109 if ((align2 + len) * sizeof (CHAR) >= page_size)
110 return;
111
112 s1 = (CHAR *) (buf1) + align1;
113 s2 = (CHAR *) (buf2) + align2;
114
115 for (i = 0; i < len; i++)
116 s1[i] = 32 + 23 * i % (max_char - 32);
117 s1[len] = 0;
118
119 printf ("Length %4zd, alignments in bytes %2zd/%2zd:", len,
120 align1 * sizeof (CHAR), align2 * sizeof (CHAR));
121
122 FOR_EACH_IMPL (impl, 0)
123 do_one_test (impl, s2, s1, len);
124
125 putchar ('\n');
126 }
127
128 int
129 test_main (void)
130 {
131 size_t i;
132
133 test_init ();
134
135 printf ("%23s", "");
136 FOR_EACH_IMPL (impl, 0)
137 printf ("\t%s", impl->name);
138 putchar ('\n');
139
140 for (i = 0; i < 16; ++i)
141 {
142 do_test (0, 0, i, SMALL_CHAR);
143 do_test (0, 0, i, BIG_CHAR);
144 do_test (0, i, i, SMALL_CHAR);
145 do_test (i, 0, i, BIG_CHAR);
146 }
147
148 for (i = 1; i < 8; ++i)
149 {
150 do_test (0, 0, 8 << i, SMALL_CHAR);
151 do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
152 }
153
154 for (i = 1; i < 8; ++i)
155 {
156 do_test (i, 2 * i, 8 << i, SMALL_CHAR);
157 do_test (2 * i, i, 8 << i, BIG_CHAR);
158 do_test (i, i, 8 << i, SMALL_CHAR);
159 do_test (i, i, 8 << i, BIG_CHAR);
160 }
161
162 for (i = 16; i <= 512; i+=4)
163 {
164 do_test (0, 4, i, SMALL_CHAR);
165 do_test (4, 0, i, BIG_CHAR);
166 do_test (4, 4, i, SMALL_CHAR);
167 do_test (2, 2, i, BIG_CHAR);
168 do_test (2, 6, i, SMALL_CHAR);
169 do_test (6, 2, i, BIG_CHAR);
170 do_test (1, 7, i, SMALL_CHAR);
171 do_test (7, 1, i, BIG_CHAR);
172 do_test (3, 4, i, SMALL_CHAR);
173 do_test (4, 3, i, BIG_CHAR);
174 do_test (5, 7, i, SMALL_CHAR);
175 do_test (7, 5, i, SMALL_CHAR);
176 }
177
178 return ret;
179 }
180
181 #include <support/test-driver.c>