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