Fix sign of input to bsloww1 (BZ #16623)
[glibc.git] / benchtests / bench-strtok.c
blob5e80c1a77587550ac21a14b442aabdfafdf81469
1 /* Measure strtok functions.
2 Copyright (C) 2013-2014 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 #define TEST_NAME "strtok"
21 #include "bench-string.h"
23 char *
24 simple_strtok (char *s1, char *s2)
26 static char *saveptr;
27 char *token;
28 ssize_t i = 0, j = 0;
29 int found = 0;
30 size_t s2len = strlen (s2);
32 if (s1 == NULL)
33 s1 = saveptr;
34 if (s1 == NULL || *s1 == '\0')
35 return NULL;
37 while (!found)
39 if (s1[i] == '\0')
41 saveptr = NULL;
42 return NULL;
44 for (j = 0; j < s2len; j++)
46 if (s1[i] == s2[j])
48 i++;
49 found = 0;
50 break;
52 found = 1;
55 token = s1 + i;
56 i++;
57 found = 0;
58 while (!found)
60 if (s1[i] == '\0')
62 saveptr = NULL;
63 return token;
65 for (j = 0; j < s2len; j++)
67 if (s1[i] == s2[j])
69 found = 1;
70 break;
73 i++;
75 s1[i - 1] = '\0';
76 saveptr = s1 + i;
77 return token;
80 typedef char *(*proto_t) (const char *, const char *);
82 IMPL (simple_strtok, 0)
83 IMPL (strtok, 1)
85 static void
86 do_one_test (impl_t * impl, const char *s1, const char *s2)
88 size_t i, iters = INNER_LOOP_ITERS;
89 timing_t start, stop, cur;
90 TIMING_NOW (start);
91 for (i = 0; i < iters; ++i)
93 CALL (impl, s1, s2);
94 CALL (impl, NULL, s2);
95 CALL (impl, NULL, s2);
97 TIMING_NOW (stop);
99 TIMING_DIFF (cur, start, stop);
101 TIMING_PRINT_MEAN ((double) cur, (double) iters);
106 static void
107 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int fail)
109 char *s2 = (char *) (buf2 + align2);
110 static const char d[] = "1234567890abcdef";
111 #define dl (sizeof (d) - 1)
112 char *ss2 = s2;
113 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
115 size_t t = l > dl ? dl : l;
116 ss2 = mempcpy (ss2, d, t);
118 s2[len2] = '\0';
120 printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:",
121 len1, len2, align1, align2, fail ? "fail" : "found");
123 FOR_EACH_IMPL (impl, 0)
125 char *s1 = (char *) (buf1 + align1);
126 if (fail)
128 char *ss1 = s1;
129 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
131 size_t t = l > dl ? dl : l;
132 memcpy (ss1, d, t);
133 ++ss1[len2 > 7 ? 7 : len2 - 1];
134 ss1 += t;
137 else
139 memset (s1, '0', len1);
140 memcpy (s1 + (len1 - len2) - 2, s2, len2);
141 if ((len1 / len2) > 4)
142 memcpy (s1 + (len1 - len2) - (3 * len2), s2, len2);
144 s1[len1] = '\0';
145 do_one_test (impl, s1, s2);
147 putchar ('\n');
150 static int
151 test_main (void)
153 test_init ();
155 printf ("%23s", "");
156 FOR_EACH_IMPL (impl, 0)
157 printf ("\t%s", impl->name);
158 putchar ('\n');
160 for (size_t klen = 2; klen < 32; ++klen)
161 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
163 do_test (0, 0, hlen, klen, 0);
164 do_test (0, 0, hlen, klen, 1);
165 do_test (0, 3, hlen, klen, 0);
166 do_test (0, 3, hlen, klen, 1);
167 do_test (0, 9, hlen, klen, 0);
168 do_test (0, 9, hlen, klen, 1);
169 do_test (0, 15, hlen, klen, 0);
170 do_test (0, 15, hlen, klen, 1);
172 do_test (3, 0, hlen, klen, 0);
173 do_test (3, 0, hlen, klen, 1);
174 do_test (3, 3, hlen, klen, 0);
175 do_test (3, 3, hlen, klen, 1);
176 do_test (3, 9, hlen, klen, 0);
177 do_test (3, 9, hlen, klen, 1);
178 do_test (3, 15, hlen, klen, 0);
179 do_test (3, 15, hlen, klen, 1);
181 do_test (9, 0, hlen, klen, 0);
182 do_test (9, 0, hlen, klen, 1);
183 do_test (9, 3, hlen, klen, 0);
184 do_test (9, 3, hlen, klen, 1);
185 do_test (9, 9, hlen, klen, 0);
186 do_test (9, 9, hlen, klen, 1);
187 do_test (9, 15, hlen, klen, 0);
188 do_test (9, 15, hlen, klen, 1);
190 do_test (15, 0, hlen, klen, 0);
191 do_test (15, 0, hlen, klen, 1);
192 do_test (15, 3, hlen, klen, 0);
193 do_test (15, 3, hlen, klen, 1);
194 do_test (15, 9, hlen, klen, 0);
195 do_test (15, 9, hlen, klen, 1);
196 do_test (15, 15, hlen, klen, 0);
197 do_test (15, 15, hlen, klen, 1);
199 do_test (0, 0, page_size - 1, 16, 0);
200 do_test (0, 0, page_size - 1, 16, 1);
202 return ret;
205 #include "../test-skeleton.c"