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