Fix cexpl when compiled with latest GCC
[glibc.git] / benchtests / bench-strchr.c
blob5f93067725b834d43af0139e0b5b0348359b7425
1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2017 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 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
23 # else
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
26 #else
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
29 # else
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
32 #endif /* WIDE */
33 #include "bench-string.h"
35 #ifndef WIDE
36 # ifdef USE_FOR_STRCHRNUL
37 # define STRCHR strchrnul
38 # define stupid_STRCHR stupid_STRCHRNUL
39 # define simple_STRCHR simple_STRCHRNUL
40 # else
41 # define STRCHR strchr
42 # endif /* !USE_FOR_STRCHRNUL */
43 # define STRLEN strlen
44 # define CHAR char
45 # define BIG_CHAR CHAR_MAX
46 # define MIDDLE_CHAR 127
47 # define SMALL_CHAR 23
48 # define UCHAR unsigned char
49 #else
50 # include <wchar.h>
51 # ifdef USE_FOR_STRCHRNUL
52 # define STRCHR wcschrnul
53 # define stupid_STRCHR stupid_WCSCHRNUL
54 # define simple_STRCHR simple_WCSCHRNUL
55 # else
56 # define STRCHR wcschr
57 # endif /* !USE_FOR_STRCHRNUL */
58 # define STRLEN wcslen
59 # define CHAR wchar_t
60 # define BIG_CHAR WCHAR_MAX
61 # define MIDDLE_CHAR 1121
62 # define SMALL_CHAR 851
63 # define UCHAR wchar_t
64 #endif /* WIDE */
66 #ifdef USE_FOR_STRCHRNUL
67 # define NULLRET(endptr) endptr
68 #else
69 # define NULLRET(endptr) NULL
70 #endif /* !USE_FOR_STRCHRNUL */
73 typedef CHAR *(*proto_t) (const CHAR *, int);
75 CHAR *
76 simple_STRCHR (const CHAR *s, int c)
78 for (; *s != (CHAR) c; ++s)
79 if (*s == '\0')
80 return NULLRET ((CHAR *) s);
81 return (CHAR *) s;
84 CHAR *
85 stupid_STRCHR (const CHAR *s, int c)
87 size_t n = STRLEN (s) + 1;
89 while (n--)
90 if (*s++ == (CHAR) c)
91 return (CHAR *) s - 1;
92 return NULLRET ((CHAR *) s - 1);
95 IMPL (stupid_STRCHR, 0)
96 IMPL (simple_STRCHR, 0)
97 IMPL (STRCHR, 1)
99 static void
100 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
102 size_t i, iters = INNER_LOOP_ITERS;
103 timing_t start, stop, cur;
105 TIMING_NOW (start);
106 for (i = 0; i < iters; ++i)
108 CALL (impl, s, c);
110 TIMING_NOW (stop);
112 TIMING_DIFF (cur, start, stop);
114 TIMING_PRINT_MEAN ((double) cur, (double) iters);
117 static void
118 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
119 /* For wcschr: align here means align not in bytes,
120 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
121 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
123 size_t i;
124 CHAR *result;
125 CHAR *buf = (CHAR *) buf1;
126 align &= 15;
127 if ((align + len) * sizeof (CHAR) >= page_size)
128 return;
130 for (i = 0; i < len; ++i)
132 buf[align + i] = 32 + 23 * i % max_char;
133 if (buf[align + i] == seek_char)
134 buf[align + i] = seek_char + 1;
135 else if (buf[align + i] == 0)
136 buf[align + i] = 1;
138 buf[align + len] = 0;
140 if (pos < len)
142 buf[align + pos] = seek_char;
143 result = buf + align + pos;
145 else if (seek_char == 0)
146 result = buf + align + len;
147 else
148 result = NULLRET (buf + align + len);
150 printf ("Length %4zd, alignment in bytes %2zd:",
151 pos, align * sizeof (CHAR));
153 FOR_EACH_IMPL (impl, 0)
154 do_one_test (impl, buf + align, seek_char, result);
156 putchar ('\n');
160 test_main (void)
162 size_t i;
164 test_init ();
166 printf ("%20s", "");
167 FOR_EACH_IMPL (impl, 0)
168 printf ("\t%s", impl->name);
169 putchar ('\n');
171 for (i = 1; i < 8; ++i)
173 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
174 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
177 for (i = 1; i < 8; ++i)
179 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
180 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
183 for (i = 0; i < 32; ++i)
185 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
186 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
189 for (i = 1; i < 8; ++i)
191 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
192 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
195 for (i = 1; i < 8; ++i)
197 do_test (i, 64, 256, 0, MIDDLE_CHAR);
198 do_test (i, 64, 256, 0, BIG_CHAR);
201 for (i = 0; i < 32; ++i)
203 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
204 do_test (0, i, i + 1, 0, BIG_CHAR);
207 return ret;
210 #include <support/test-driver.c>