alpha: Fix tls-macros.h
[glibc.git] / benchtests / bench-strchr.c
blob710b59203d483b6c689d4b706d75dde861eaf94f
1 /* Measure STRCHR functions.
2 Copyright (C) 2013 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 if (HP_TIMING_AVAIL)
94 hp_timing_t start __attribute ((unused));
95 hp_timing_t stop __attribute ((unused));
96 hp_timing_t best_time = ~ (hp_timing_t) 0;
97 size_t i;
99 for (i = 0; i < 32; ++i)
101 HP_TIMING_NOW (start);
102 CALL (impl, s, c);
103 HP_TIMING_NOW (stop);
104 HP_TIMING_BEST (best_time, start, stop);
107 printf ("\t%zd", (size_t) best_time);
111 static void
112 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
113 /* For wcschr: align here means align not in bytes,
114 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
115 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
117 size_t i;
118 CHAR *result;
119 CHAR *buf = (CHAR *) buf1;
120 align &= 15;
121 if ((align + len) * sizeof (CHAR) >= page_size)
122 return;
124 for (i = 0; i < len; ++i)
126 buf[align + i] = 32 + 23 * i % max_char;
127 if (buf[align + i] == seek_char)
128 buf[align + i] = seek_char + 1;
129 else if (buf[align + i] == 0)
130 buf[align + i] = 1;
132 buf[align + len] = 0;
134 if (pos < len)
136 buf[align + pos] = seek_char;
137 result = buf + align + pos;
139 else if (seek_char == 0)
140 result = buf + align + len;
141 else
142 result = NULLRET (buf + align + len);
144 if (HP_TIMING_AVAIL)
145 printf ("Length %4zd, alignment in bytes %2zd:",
146 pos, align * sizeof (CHAR));
148 FOR_EACH_IMPL (impl, 0)
149 do_one_test (impl, buf + align, seek_char, result);
151 if (HP_TIMING_AVAIL)
152 putchar ('\n');
156 test_main (void)
158 size_t i;
160 test_init ();
162 printf ("%20s", "");
163 FOR_EACH_IMPL (impl, 0)
164 printf ("\t%s", impl->name);
165 putchar ('\n');
167 for (i = 1; i < 8; ++i)
169 do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
170 do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
173 for (i = 1; i < 8; ++i)
175 do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
176 do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
179 for (i = 0; i < 32; ++i)
181 do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
182 do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
185 for (i = 1; i < 8; ++i)
187 do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
188 do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
191 for (i = 1; i < 8; ++i)
193 do_test (i, 64, 256, 0, MIDDLE_CHAR);
194 do_test (i, 64, 256, 0, BIG_CHAR);
197 for (i = 0; i < 32; ++i)
199 do_test (0, i, i + 1, 0, MIDDLE_CHAR);
200 do_test (0, i, i + 1, 0, BIG_CHAR);
203 return ret;
206 #include "../test-skeleton.c"