]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strcat.c
6b3b084ae19a931cb2bda07794380b5745ccfc86
[thirdparty/glibc.git] / benchtests / bench-strcat.c
1 /* Measure strcat functions.
2 Copyright (C) 2013-2018 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 TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strcat"
22 #else
23 # define TEST_NAME "wcscat"
24 #endif /* WIDE */
25 #include "bench-string.h"
26
27 #define BIG_CHAR MAX_CHAR
28
29 #ifndef WIDE
30 # define sfmt "s"
31 # define SIMPLE_STRCAT simple_strcat
32 # define SMALL_CHAR 127
33 #else
34 # define sfmt "ls"
35 # define SIMPLE_STRCAT simple_wcscat
36 # define SMALL_CHAR 1273
37 #endif /* WIDE */
38
39
40 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
41 CHAR *SIMPLE_STRCAT (CHAR *, const CHAR *);
42
43 IMPL (SIMPLE_STRCAT, 0)
44 IMPL (STRCAT, 1)
45
46 CHAR *
47 SIMPLE_STRCAT (CHAR *dst, const CHAR *src)
48 {
49 CHAR *ret = dst;
50 while (*dst++ != '\0');
51 --dst;
52 while ((*dst++ = *src++) != '\0');
53 return ret;
54 }
55
56 static void
57 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src)
58 {
59 size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS;
60 timing_t start, stop, cur;
61
62 if (CALL (impl, dst, src) != dst)
63 {
64 error (0, 0, "Wrong result in function %s %p %p", impl->name,
65 CALL (impl, dst, src), dst);
66 ret = 1;
67 return;
68 }
69
70 if (STRCMP (dst + k, src) != 0)
71 {
72 error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
73 impl->name, dst, src);
74 ret = 1;
75 return;
76 }
77
78 TIMING_NOW (start);
79 for (i = 0; i < iters; ++i)
80 {
81 dst[k] = '\0';
82 CALL (impl, dst, src);
83 }
84 TIMING_NOW (stop);
85
86 TIMING_DIFF (cur, start, stop);
87
88 TIMING_PRINT_MEAN ((double) cur, (double) iters);
89 }
90
91 static void
92 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
93 {
94 size_t i;
95 CHAR *s1, *s2;
96
97 align1 &= 7;
98 if ((align1 + len1) * sizeof (CHAR) >= page_size)
99 return;
100
101 align2 &= 7;
102 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
103 return;
104
105 s1 = (CHAR *) (buf1) + align1;
106 s2 = (CHAR *) (buf2) + align2;
107
108 for (i = 0; i < len1; ++i)
109 s1[i] = 32 + 23 * i % (max_char - 32);
110 s1[len1] = '\0';
111
112 for (i = 0; i < len2; i++)
113 s2[i] = 32 + 23 * i % (max_char - 32);
114
115 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2);
116
117 FOR_EACH_IMPL (impl, 0)
118 {
119 s2[len2] = '\0';
120 do_one_test (impl, s2, s1);
121 }
122
123 putchar ('\n');
124 }
125
126 int
127 test_main (void)
128 {
129 size_t i;
130
131 test_init ();
132
133 printf ("%28s", "");
134 FOR_EACH_IMPL (impl, 0)
135 printf ("\t%s", impl->name);
136 putchar ('\n');
137
138 for (i = 0; i < 16; ++i)
139 {
140 do_test (0, 0, i, i, SMALL_CHAR);
141 do_test (0, 0, i, i, BIG_CHAR);
142 do_test (0, i, i, i, SMALL_CHAR);
143 do_test (i, 0, i, i, BIG_CHAR);
144 }
145
146 for (i = 1; i < 8; ++i)
147 {
148 do_test (0, 0, 8 << i, 8 << i, SMALL_CHAR);
149 do_test (8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR);
150 do_test (0, 0, 8 << i, 2 << i, SMALL_CHAR);
151 do_test (8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR);
152 }
153
154 for (i = 1; i < 8; ++i)
155 {
156 do_test (i, 2 * i, 8 << i, 1, SMALL_CHAR);
157 do_test (2 * i, i, 8 << i, 1, BIG_CHAR);
158 do_test (i, i, 8 << i, 10, SMALL_CHAR);
159 do_test (i, i, 8 << i, 10, BIG_CHAR);
160 }
161
162 return ret;
163 }
164
165 #include <support/test-driver.c>