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