S390: Optimize strcat and wcscat.
[glibc.git] / benchtests / bench-strcat.c
blob1abf6d3d06c40a3469bf471fa5d8d7ca237e363c
1 /* Measure strcat functions.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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"
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 */
49 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
50 CHAR *SIMPLE_STRCAT (CHAR *, const CHAR *);
52 IMPL (SIMPLE_STRCAT, 0)
53 IMPL (STRCAT, 1)
55 CHAR *
56 SIMPLE_STRCAT (CHAR *dst, const CHAR *src)
58 CHAR *ret = dst;
59 while (*dst++ != '\0');
60 --dst;
61 while ((*dst++ = *src++) != '\0');
62 return ret;
65 static void
66 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src)
68 size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS;
69 timing_t start, stop, cur;
71 if (CALL (impl, dst, src) != dst)
73 error (0, 0, "Wrong result in function %s %p %p", impl->name,
74 CALL (impl, dst, src), dst);
75 ret = 1;
76 return;
79 if (STRCMP (dst + k, src) != 0)
81 error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
82 impl->name, dst, src);
83 ret = 1;
84 return;
87 TIMING_NOW (start);
88 for (i = 0; i < iters; ++i)
90 dst[k] = '\0';
91 CALL (impl, dst, src);
93 TIMING_NOW (stop);
95 TIMING_DIFF (cur, start, stop);
97 TIMING_PRINT_MEAN ((double) cur, (double) iters);
100 static void
101 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
103 size_t i;
104 CHAR *s1, *s2;
106 align1 &= 7;
107 if ((align1 + len1) * sizeof (CHAR) >= page_size)
108 return;
110 align2 &= 7;
111 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
112 return;
114 s1 = (CHAR *) (buf1) + align1;
115 s2 = (CHAR *) (buf2) + align2;
117 for (i = 0; i < len1; ++i)
118 s1[i] = 32 + 23 * i % (max_char - 32);
119 s1[len1] = '\0';
121 for (i = 0; i < len2; i++)
122 s2[i] = 32 + 23 * i % (max_char - 32);
124 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2);
126 FOR_EACH_IMPL (impl, 0)
128 s2[len2] = '\0';
129 do_one_test (impl, s2, s1);
132 putchar ('\n');
136 test_main (void)
138 size_t i;
140 test_init ();
142 printf ("%28s", "");
143 FOR_EACH_IMPL (impl, 0)
144 printf ("\t%s", impl->name);
145 putchar ('\n');
147 for (i = 0; i < 16; ++i)
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);
155 for (i = 1; i < 8; ++i)
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);
163 for (i = 1; i < 8; ++i)
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);
171 return ret;
174 #include "../test-skeleton.c"