AArch64: Improve strrchr
[glibc.git] / benchtests / bench-strtok.c
blob10260e9b47b5c6880eb2872a1d796e88dd6fddf4
1 /* Measure strtok functions.
2 Copyright (C) 2013-2023 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 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "strtok"
21 #include "bench-string.h"
23 char *
24 oldstrtok (char *s, const char *delim)
26 static char *olds;
27 char *token;
29 if (s == NULL)
30 s = olds;
32 /* Scan leading delimiters. */
33 s += strspn (s, delim);
34 if (*s == '\0')
36 olds = s;
37 return NULL;
40 /* Find the end of the token. */
41 token = s;
42 s = strpbrk (token, delim);
43 if (s == NULL)
44 /* This token finishes the string. */
45 olds = rawmemchr (token, '\0');
46 else
48 /* Terminate the token and make OLDS point past it. */
49 *s = '\0';
50 olds = s + 1;
52 return token;
55 typedef char *(*proto_t) (const char *, const char *);
57 IMPL (oldstrtok, 0)
58 IMPL (strtok, 1)
60 static void
61 do_one_test (impl_t * impl, const char *s1, const char *s2)
63 size_t i, iters = INNER_LOOP_ITERS_SMALL;
64 timing_t start, stop, cur;
65 TIMING_NOW (start);
66 for (i = 0; i < iters; ++i)
68 CALL (impl, s1, s2);
69 CALL (impl, NULL, s2);
70 CALL (impl, NULL, s2);
72 TIMING_NOW (stop);
74 TIMING_DIFF (cur, start, stop);
76 TIMING_PRINT_MEAN ((double) cur, (double) iters);
81 static void
82 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int fail)
84 char *s2 = (char *) (buf2 + align2);
85 static const char d[] = "1234567890abcdef";
86 #define dl (sizeof (d) - 1)
87 char *ss2 = s2;
88 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
90 size_t t = l > dl ? dl : l;
91 ss2 = mempcpy (ss2, d, t);
93 s2[len2] = '\0';
95 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
96 len1, len2, align1, align2, fail ? "fail" : "found");
98 FOR_EACH_IMPL (impl, 0)
100 char *s1 = (char *) (buf1 + align1);
101 if (fail)
103 char *ss1 = s1;
104 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
106 size_t t = l > dl ? dl : l;
107 memcpy (ss1, d, t);
108 ++ss1[len2 > 7 ? 7 : len2 - 1];
109 ss1 += t;
112 else
114 memset (s1, '0', len1);
115 memcpy (s1 + (len1 - len2) - 2, s2, len2);
116 if ((len1 / len2) > 4)
117 memcpy (s1 + (len1 - len2) - (3 * len2), s2, len2);
119 s1[len1] = '\0';
120 do_one_test (impl, s1, s2);
122 putchar ('\n');
125 static int
126 test_main (void)
128 test_init ();
130 printf ("%23s", "");
131 FOR_EACH_IMPL (impl, 0)
132 printf ("\t%s", impl->name);
133 putchar ('\n');
135 for (size_t klen = 2; klen < 32; ++klen)
136 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
138 do_test (0, 0, hlen, klen, 0);
139 do_test (0, 0, hlen, klen, 1);
140 do_test (0, 3, hlen, klen, 0);
141 do_test (0, 3, hlen, klen, 1);
142 do_test (0, 9, hlen, klen, 0);
143 do_test (0, 9, hlen, klen, 1);
144 do_test (0, 15, hlen, klen, 0);
145 do_test (0, 15, hlen, klen, 1);
147 do_test (3, 0, hlen, klen, 0);
148 do_test (3, 0, hlen, klen, 1);
149 do_test (3, 3, hlen, klen, 0);
150 do_test (3, 3, hlen, klen, 1);
151 do_test (3, 9, hlen, klen, 0);
152 do_test (3, 9, hlen, klen, 1);
153 do_test (3, 15, hlen, klen, 0);
154 do_test (3, 15, hlen, klen, 1);
156 do_test (9, 0, hlen, klen, 0);
157 do_test (9, 0, hlen, klen, 1);
158 do_test (9, 3, hlen, klen, 0);
159 do_test (9, 3, hlen, klen, 1);
160 do_test (9, 9, hlen, klen, 0);
161 do_test (9, 9, hlen, klen, 1);
162 do_test (9, 15, hlen, klen, 0);
163 do_test (9, 15, hlen, klen, 1);
165 do_test (15, 0, hlen, klen, 0);
166 do_test (15, 0, hlen, klen, 1);
167 do_test (15, 3, hlen, klen, 0);
168 do_test (15, 3, hlen, klen, 1);
169 do_test (15, 9, hlen, klen, 0);
170 do_test (15, 9, hlen, klen, 1);
171 do_test (15, 15, hlen, klen, 0);
172 do_test (15, 15, hlen, klen, 1);
174 do_test (0, 0, page_size - 1, 16, 0);
175 do_test (0, 0, page_size - 1, 16, 1);
177 return ret;
180 #include <support/test-driver.c>