powerpc: Sync hwcap.h with kernel
[glibc.git] / benchtests / bench-strncat.c
blob8f3339dc787bcc4d4527b6324a11a2bb7df44e90
1 /* Measure strncat 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 "strncat"
22 #else
23 # define TEST_NAME "wcsncat"
24 #endif /* WIDE */
25 #include "bench-string.h"
27 #ifndef WIDE
28 # define STRNCAT strncat
29 # define CHAR char
30 # define SIMPLE_STRNCAT simple_strncat
31 # define STUPID_STRNCAT stupid_strncat
32 # define STRLEN strlen
33 # define MEMCMP memcmp
34 # define BIG_CHAR CHAR_MAX
35 # define SMALL_CHAR 127
36 #else
37 # include <wchar.h>
38 # define STRNCAT wcsncat
39 # define CHAR wchar_t
40 # define SIMPLE_STRNCAT simple_wcsncat
41 # define STUPID_STRNCAT stupid_wcsncat
42 # define STRLEN wcslen
43 # define MEMCMP wmemcmp
44 # define BIG_CHAR WCHAR_MAX
45 # define SMALL_CHAR 1273
46 #endif /* WIDE */
48 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
49 CHAR *STUPID_STRNCAT (CHAR *, const CHAR *, size_t);
50 CHAR *SIMPLE_STRNCAT (CHAR *, const CHAR *, size_t);
52 IMPL (STUPID_STRNCAT, 0)
53 IMPL (STRNCAT, 2)
55 CHAR *
56 STUPID_STRNCAT (CHAR *dst, const CHAR *src, size_t n)
58 CHAR *ret = dst;
59 while (*dst++ != '\0');
60 --dst;
61 while (n--)
62 if ((*dst++ = *src++) == '\0')
63 return ret;
64 *dst = '\0';
65 return ret;
68 static void
69 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n)
71 size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS;
72 timing_t start, stop, cur;
74 if (CALL (impl, dst, src, n) != dst)
76 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
77 CALL (impl, dst, src, n), dst);
78 ret = 1;
79 return;
82 size_t len = STRLEN (src);
83 if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
85 error (0, 0, "Incorrect concatenation in function %s",
86 impl->name);
87 ret = 1;
88 return;
90 if (n < len && dst[k + n] != '\0')
92 error (0, 0, "There is no zero in the end of output string in %s",
93 impl->name);
94 ret = 1;
95 return;
98 TIMING_NOW (start);
99 for (i = 0; i < iters; ++i)
101 dst[k] = '\0';
102 CALL (impl, dst, src, n);
104 TIMING_NOW (stop);
106 TIMING_DIFF (cur, start, stop);
108 TIMING_PRINT_MEAN ((double) cur, (double) iters);
111 static void
112 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
113 size_t n, int max_char)
115 size_t i;
116 CHAR *s1, *s2;
118 align1 &= 7;
119 if ((align1 + len1) * sizeof (CHAR) >= page_size)
120 return;
121 if ((align1 + n) * sizeof (CHAR) > page_size)
122 return;
123 align2 &= 7;
124 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
125 return;
126 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
127 return;
128 s1 = (CHAR *) (buf1) + align1;
129 s2 = (CHAR *) (buf2) + align2;
131 for (i = 0; i < len1; ++i)
132 s1[i] = 32 + 23 * i % (max_char - 32);
133 s1[len1] = '\0';
135 for (i = 0; i < len2; i++)
136 s2[i] = 32 + 23 * i % (max_char - 32);
138 printf ("Length %4zd/%4zd, alignment %2zd/%2zd, N %4zd:",
139 len1, len2, align1, align2, n);
141 FOR_EACH_IMPL (impl, 0)
143 s2[len2] = '\0';
144 do_one_test (impl, s2, s1, n);
147 putchar ('\n');
151 main (void)
153 size_t i, n;
155 test_init ();
157 printf ("%28s", "");
158 FOR_EACH_IMPL (impl, 0)
159 printf ("\t%s", impl->name);
160 putchar ('\n');
162 for (n = 2; n <= 2048; n*=4)
164 do_test (0, 2, 2, 2, n, SMALL_CHAR);
165 do_test (0, 0, 4, 4, n, SMALL_CHAR);
166 do_test (4, 0, 4, 4, n, BIG_CHAR);
167 do_test (0, 0, 8, 8, n, SMALL_CHAR);
168 do_test (0, 8, 8, 8, n, SMALL_CHAR);
170 for (i = 1; i < 8; ++i)
172 do_test (0, 0, 8 << i, 8 << i, n, SMALL_CHAR);
173 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, SMALL_CHAR);
174 do_test (0, 0, 8 << i, 2 << i, n, SMALL_CHAR);
175 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, SMALL_CHAR);
178 for (i = 1; i < 8; ++i)
180 do_test (i, 2 * i, 8 << i, 1, n, SMALL_CHAR);
181 do_test (2 * i, i, 8 << i, 1, n, BIG_CHAR);
182 do_test (i, i, 8 << i, 10, n, SMALL_CHAR);
186 return ret;