alpha: Fix tls-macros.h
[glibc.git] / benchtests / bench-strrchr.c
blob400ac80e2cb280ed34a5ef1c11295d251582eb5a
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 #ifdef WIDE
21 # define TEST_NAME "wcsrchr"
22 #else
23 # define TEST_NAME "strrchr"
24 #endif
25 #include "bench-string.h"
27 #ifdef WIDE
28 # include <wchar.h>
29 # define SIMPLE_STRRCHR simple_wcsrchr
30 # define STRRCHR wcsrchr
31 # define CHAR wchar_t
32 # define UCHAR wchar_t
33 # define BIG_CHAR WCHAR_MAX
34 # define SMALL_CHAR 1273
35 #else
36 # define SIMPLE_STRRCHR simple_strrchr
37 # define STRRCHR strrchr
38 # define CHAR char
39 # define UCHAR unsigned char
40 # define BIG_CHAR CHAR_MAX
41 # define SMALL_CHAR 127
42 #endif
44 typedef CHAR *(*proto_t) (const CHAR *, int);
45 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
47 IMPL (SIMPLE_STRRCHR, 0)
48 IMPL (STRRCHR, 1)
50 CHAR *
51 SIMPLE_STRRCHR (const CHAR *s, int c)
53 const CHAR *ret = NULL;
55 for (; *s != '\0'; ++s)
56 if (*s == (CHAR) c)
57 ret = s;
59 return (CHAR *) (c == '\0' ? s : ret);
62 static void
63 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
65 CHAR *res = CALL (impl, s, c);
66 if (res != exp_res)
68 error (0, 0, "Wrong result in function %s %p %p", impl->name,
69 res, exp_res);
70 ret = 1;
71 return;
74 if (HP_TIMING_AVAIL)
76 hp_timing_t start __attribute ((unused));
77 hp_timing_t stop __attribute ((unused));
78 hp_timing_t best_time = ~ (hp_timing_t) 0;
79 size_t i;
81 for (i = 0; i < 32; ++i)
83 HP_TIMING_NOW (start);
84 CALL (impl, s, c);
85 HP_TIMING_NOW (stop);
86 HP_TIMING_BEST (best_time, start, stop);
89 printf ("\t%zd", (size_t) best_time);
93 static void
94 do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
95 /* For wcsrchr: align here means align not in bytes,
96 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
97 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
99 size_t i;
100 CHAR *result;
101 CHAR *buf = (CHAR *) buf1;
103 align &= 7;
104 if ( (align + len) * sizeof(CHAR) >= page_size)
105 return;
107 for (i = 0; i < len; ++i)
109 buf[align + i] = (random () * random ()) & max_char;
110 if (!buf[align + i])
111 buf[align + i] = (random () * random ()) & max_char;
112 if (!buf[align + i])
113 buf[align + i] = 1;
114 if ((i > pos || pos >= len) && buf[align + i] == seek_char)
115 buf[align + i] = seek_char + 10 + (random () & 15);
117 buf[align + len] = 0;
119 if (pos < len)
121 buf[align + pos] = seek_char;
122 result = (CHAR *) (buf + align + pos);
124 else if (seek_char == 0)
125 result = (CHAR *) (buf + align + len);
126 else
127 result = NULL;
129 if (HP_TIMING_AVAIL)
130 printf ("Length %4zd, alignment in bytes %2zd:", pos, align * sizeof(CHAR));
132 FOR_EACH_IMPL (impl, 0)
133 do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
135 if (HP_TIMING_AVAIL)
136 putchar ('\n');
140 test_main (void)
142 size_t i;
144 test_init ();
146 printf ("%20s", "");
147 FOR_EACH_IMPL (impl, 0)
148 printf ("\t%s", impl->name);
149 putchar ('\n');
151 for (i = 1; i < 8; ++i)
153 do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
154 do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
157 for (i = 1; i < 8; ++i)
159 do_test (i, 64, 256, 23, SMALL_CHAR);
160 do_test (i, 64, 256, 23, BIG_CHAR);
163 for (i = 0; i < 32; ++i)
165 do_test (0, i, i + 1, 23, SMALL_CHAR);
166 do_test (0, i, i + 1, 23, BIG_CHAR);
169 for (i = 1; i < 8; ++i)
171 do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
172 do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
175 for (i = 1; i < 8; ++i)
177 do_test (i, 64, 256, 0, SMALL_CHAR);
178 do_test (i, 64, 256, 0, BIG_CHAR);
181 for (i = 0; i < 32; ++i)
183 do_test (0, i, i + 1, 0, SMALL_CHAR);
184 do_test (0, i, i + 1, 0, BIG_CHAR);
187 return ret;
190 #include "../test-skeleton.c"