]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-strpbrk.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / benchtests / bench-strpbrk.c
1 /* Measure strpbrk functions.
2 Copyright (C) 2013-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #define BIG_CHAR MAX_CHAR
20
21 #ifndef WIDE
22 # define SMALL_CHAR 127
23 #else
24 # define SMALL_CHAR 1273
25 #endif /* WIDE */
26
27 #ifndef STRPBRK_RESULT
28 # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
29 # define RES_TYPE CHAR *
30 # define TEST_MAIN
31 # ifndef WIDE
32 # define TEST_NAME "strpbrk"
33 # else
34 # define TEST_NAME "wcspbrk"
35 # endif /* WIDE */
36 # include "bench-string.h"
37
38 # ifndef WIDE
39 # define SIMPLE_STRPBRK simple_strpbrk
40 # define STUPID_STRPBRK stupid_strpbrk
41 # else
42 # define SIMPLE_STRPBRK simple_wcspbrk
43 # define STUPID_STRPBRK stupid_wcspbrk
44 # endif /* WIDE */
45
46 typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
47 CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *);
48 CHAR *STUPID_STRPBRK (const CHAR *, const CHAR *);
49
50 IMPL (STUPID_STRPBRK, 0)
51 IMPL (SIMPLE_STRPBRK, 0)
52 IMPL (STRPBRK, 1)
53
54 CHAR *
55 SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej)
56 {
57 const CHAR *r;
58 CHAR c;
59
60 while ((c = *s++) != '\0')
61 for (r = rej; *r != '\0'; ++r)
62 if (*r == c)
63 return (CHAR *) s - 1;
64 return NULL;
65 }
66
67 CHAR *
68 STUPID_STRPBRK (const CHAR *s, const CHAR *rej)
69 {
70 size_t ns = STRLEN (s), nrej = STRLEN (rej);
71 size_t i, j;
72
73 for (i = 0; i < ns; ++i)
74 for (j = 0; j < nrej; ++j)
75 if (s[i] == rej[j])
76 return (CHAR *) s + i;
77 return NULL;
78 }
79 #endif /* !STRPBRK_RESULT */
80
81 static void
82 do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res)
83 {
84 RES_TYPE res = CALL (impl, s, rej);
85 size_t i, iters = INNER_LOOP_ITERS;
86 timing_t start, stop, cur;
87
88 if (res != exp_res)
89 {
90 error (0, 0, "Wrong result in function %s %p %p", impl->name,
91 (void *) res, (void *) exp_res);
92 ret = 1;
93 return;
94 }
95
96 TIMING_NOW (start);
97 for (i = 0; i < iters; ++i)
98 {
99 CALL (impl, s, rej);
100 }
101 TIMING_NOW (stop);
102
103 TIMING_DIFF (cur, start, stop);
104
105 TIMING_PRINT_MEAN ((double) cur, (double) iters);
106 }
107
108 static void
109 do_test (size_t align, size_t pos, size_t len)
110 {
111 size_t i;
112 int c;
113 RES_TYPE result;
114 CHAR *rej, *s;
115
116 align &= 7;
117 if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
118 return;
119
120 rej = (CHAR *) (buf2) + (random () & 255);
121 s = (CHAR *) (buf1) + align;
122
123 for (i = 0; i < len; ++i)
124 {
125 rej[i] = random () & BIG_CHAR;
126 if (!rej[i])
127 rej[i] = random () & BIG_CHAR;
128 if (!rej[i])
129 rej[i] = 1 + (random () & SMALL_CHAR);
130 }
131 rej[len] = '\0';
132 for (c = 1; c <= BIG_CHAR; ++c)
133 if (STRCHR (rej, c) == NULL)
134 break;
135
136 for (i = 0; i < pos; ++i)
137 {
138 s[i] = random () & BIG_CHAR;
139 if (STRCHR (rej, s[i]))
140 {
141 s[i] = random () & BIG_CHAR;
142 if (STRCHR (rej, s[i]))
143 s[i] = c;
144 }
145 }
146 s[pos] = rej[random () % (len + 1)];
147 if (s[pos])
148 {
149 for (i = pos + 1; i < pos + 10; ++i)
150 s[i] = random () & BIG_CHAR;
151 s[i] = '\0';
152 }
153 result = STRPBRK_RESULT (s, pos);
154
155 printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len);
156
157 FOR_EACH_IMPL (impl, 0)
158 do_one_test (impl, s, rej, result);
159
160 putchar ('\n');
161 }
162
163 int
164 test_main (void)
165 {
166 size_t i;
167
168 test_init ();
169
170 printf ("%32s", "");
171 FOR_EACH_IMPL (impl, 0)
172 printf ("\t%s", impl->name);
173 putchar ('\n');
174
175 for (i = 0; i < 32; ++i)
176 {
177 do_test (0, 512, i);
178 do_test (i, 512, i);
179 }
180
181 for (i = 1; i < 8; ++i)
182 {
183 do_test (0, 16 << i, 4);
184 do_test (i, 16 << i, 4);
185 }
186
187 for (i = 1; i < 8; ++i)
188 do_test (i, 64, 10);
189
190 for (i = 0; i < 64; ++i)
191 do_test (0, i, 6);
192
193 return ret;
194 }
195
196 #include <support/test-driver.c>