Update copyright notices with scripts/update-copyrights
[glibc.git] / benchtests / bench-strchr.c
blobf1cea91f283bacafc239b938aed9a2984662047c
1 /* Measure STRCHR 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 #ifndef WIDE
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
23 # else
24 # define TEST_NAME "strchr"
25 # endif
26 #else
27 # define TEST_NAME "wcschr"
28 #endif
29 #include "bench-string.h"
31 #ifndef WIDE
32 # ifdef USE_FOR_STRCHRNUL
33 # define STRCHR strchrnul
34 # define stupid_STRCHR stupid_STRCHRNUL
35 # define simple_STRCHR simple_STRCHRNUL
36 # else
37 # define STRCHR strchr
38 # endif
39 # define STRLEN strlen
40 # define CHAR char
41 # define BIG_CHAR CHAR_MAX
42 # define MIDDLE_CHAR 127
43 # define SMALL_CHAR 23
44 # define UCHAR unsigned char
45 #else
46 # include <wchar.h>
47 # define STRCHR wcschr
48 # define STRLEN wcslen
49 # define CHAR wchar_t
50 # define BIG_CHAR WCHAR_MAX
51 # define MIDDLE_CHAR 1121
52 # define SMALL_CHAR 851
53 # define UCHAR wchar_t
54 #endif
56 #ifdef USE_FOR_STRCHRNUL
57 # define NULLRET(endptr) endptr
58 #else
59 # define NULLRET(endptr) NULL
60 #endif
63 typedef CHAR *(*proto_t) (const CHAR *, int);
65 CHAR *
66 simple_STRCHR (const CHAR *s, int c)
68 for (; *s != (CHAR) c; ++s)
69 if (*s == '\0')
70 return NULLRET ((CHAR *) s);
71 return (CHAR *) s;
74 CHAR *
75 stupid_STRCHR (const CHAR *s, int c)
77 size_t n = STRLEN (s) + 1;
79 while (n--)
80 if (*s++ == (CHAR) c)
81 return (CHAR *) s - 1;
82 return NULLRET ((CHAR *) s - 1);
85 IMPL (stupid_STRCHR, 0)
86 IMPL (simple_STRCHR, 0)
87 IMPL (STRCHR, 1)
89 static void
90 do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
92 size_t i, iters = INNER_LOOP_ITERS;
93 timing_t start, stop, cur;
95 TIMING_NOW (start);
96 for (i = 0; i < iters; ++i)
98 CALL (impl, s, c);
100 TIMING_NOW (stop);
102 TIMING_DIFF (cur, start, stop);
104 TIMING_PRINT_MEAN ((double) cur, (double) iters);
107 static void
108 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
109 /* For wcschr: align here means align not in bytes,
110 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
111 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
113 size_t i;
114 CHAR *result;
115 CHAR *buf = (CHAR *) buf1;
116 align &= 15;
117 if ((align + len) * sizeof (CHAR) >= page_size)
118 return;
120 for (i = 0; i < len; ++i)
122 buf[align + i] = 32 + 23 * i % max_char;
123 if (buf[align + i] == seek_char)
124 buf[align + i] = seek_char + 1;
125 else if (buf[align + i] == 0)
126 buf[align + i] = 1;
128 buf[align + len] = 0;
130 if (pos < len)
132 buf[align + pos] = seek_char;
133 result = buf + align + pos;
135 else if (seek_char == 0)
136 result = buf + align + len;
137 else
138 result = NULLRET (buf + align + len);
140 printf ("Length %4zd, alignment in bytes %2zd:",
141 pos, align * sizeof (CHAR));
143 FOR_EACH_IMPL (impl, 0)
144 do_one_test (impl, buf + align, seek_char, result);
146 putchar ('\n');
150 test_main (void)
152 size_t i;
154 test_init ();
156 printf ("%20s", "");
157 FOR_EACH_IMPL (impl, 0)
158 printf ("\t%s", impl->name);
159 putchar ('\n');
161 for (i = 1; i < 8; ++i)
163 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
164 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
167 for (i = 1; i < 8; ++i)
169 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
170 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
173 for (i = 0; i < 32; ++i)
175 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
176 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
179 for (i = 1; i < 8; ++i)
181 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
182 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
185 for (i = 1; i < 8; ++i)
187 do_test (i, 64, 256, 0, MIDDLE_CHAR);
188 do_test (i, 64, 256, 0, BIG_CHAR);
191 for (i = 0; i < 32; ++i)
193 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
194 do_test (0, i, i + 1, 0, BIG_CHAR);
197 return ret;
200 #include "../test-skeleton.c"