1/* Measure memchr functions.
2 Copyright (C) 2013-2022 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 <https://www.gnu.org/licenses/>. */
18
19#ifndef WIDE
20# define SMALL_CHAR 127
21#else
22# define SMALL_CHAR 1273
23#endif /* WIDE */
24
25#ifndef USE_AS_MEMRCHR
26# define TEST_MAIN
27# ifndef WIDE
28# define TEST_NAME "memchr"
29# else
30# define TEST_NAME "wmemchr"
31# endif /* WIDE */
32# include "bench-string.h"
33
34# ifndef WIDE
35# define SIMPLE_MEMCHR simple_memchr
36# else
37# define SIMPLE_MEMCHR simple_wmemchr
38# endif /* WIDE */
39
40typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
41CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t);
42
43IMPL (SIMPLE_MEMCHR, 0)
44IMPL (MEMCHR, 1)
45
46CHAR *
47SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
48{
49 while (n--)
50 if (*s++ == (CHAR) c)
51 return (CHAR *) s - 1;
52 return NULL;
53}
54#endif /* !USE_AS_MEMRCHR */
55
56static void
57do_one_test (impl_t *impl, const CHAR *s, int c, size_t n)
58{
59 size_t i, iters = INNER_LOOP_ITERS_LARGE;
60 timing_t start, stop, cur;
61
62 TIMING_NOW (start);
63 for (i = 0; i < iters; ++i)
64 {
65 CALL (impl, s, c, n);
66 }
67 TIMING_NOW (stop);
68
69 TIMING_DIFF (cur, start, stop);
70
71 TIMING_PRINT_MEAN ((double) cur, (double) iters);
72}
73
74static void
75do_test (size_t align, size_t pos, size_t len, int seek_char)
76{
77 size_t i;
78
79 align &= 7;
80 if ((align + len) * sizeof (CHAR) >= page_size)
81 return;
82
83 CHAR *buf = (CHAR *) (buf1);
84
85 for (i = 0; i < len; ++i)
86 {
87 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
88 if (buf[align + i] == seek_char)
89 buf[align + i] = seek_char + 1;
90 }
91 buf[align + len] = 0;
92
93 if (pos < len)
94 {
95 buf[align + pos] = seek_char;
96 buf[align + len] = -seek_char;
97 }
98 else
99 {
100 buf[align + len] = seek_char;
101 }
102
103 printf (format: "Length %4zd, position %4zd, alignment %2zd:",
104 len, pos, align);
105
106 FOR_EACH_IMPL (impl, 0)
107 do_one_test (impl, s: (CHAR *) (buf + align), c: seek_char, n: len);
108
109 putchar (c: '\n');
110}
111
112int
113test_main (void)
114{
115 size_t i;
116
117 test_init ();
118
119 printf (format: "%20s", "");
120 FOR_EACH_IMPL (impl, 0)
121 printf (format: "\t%s", impl->name);
122 putchar (c: '\n');
123
124 for (i = 1; i < 8; ++i)
125 {
126 do_test (align: 0, pos: 16 << i, len: 2048, seek_char: 23);
127 do_test (align: i, pos: 64, len: 256, seek_char: 23);
128 do_test (align: 0, pos: 16 << i, len: 2048, seek_char: 0);
129 do_test (align: i, pos: 64, len: 256, seek_char: 0);
130#ifdef USE_AS_MEMRCHR
131 /* Also test the position close to the beginning for memrchr. */
132 do_test (0, i, 256, 23);
133 do_test (0, i, 256, 0);
134 do_test (i, i, 256, 23);
135 do_test (i, i, 256, 0);
136#endif
137 }
138 for (i = 1; i < 8; ++i)
139 {
140 do_test (align: i, pos: i << 5, len: 192, seek_char: 23);
141 do_test (align: i, pos: i << 5, len: 192, seek_char: 0);
142 do_test (align: i, pos: i << 5, len: 256, seek_char: 23);
143 do_test (align: i, pos: i << 5, len: 256, seek_char: 0);
144 do_test (align: i, pos: i << 5, len: 512, seek_char: 23);
145 do_test (align: i, pos: i << 5, len: 512, seek_char: 0);
146 }
147 for (i = 1; i < 32; ++i)
148 {
149 do_test (align: 0, pos: i, len: i + 1, seek_char: 23);
150 do_test (align: 0, pos: i, len: i + 1, seek_char: 0);
151 do_test (align: i, pos: i, len: i + 1, seek_char: 23);
152 do_test (align: i, pos: i, len: i + 1, seek_char: 0);
153 do_test (align: 0, pos: i, len: i - 1, seek_char: 23);
154 do_test (align: 0, pos: i, len: i - 1, seek_char: 0);
155 do_test (align: i, pos: i, len: i - 1, seek_char: 23);
156 do_test (align: i, pos: i, len: i - 1, seek_char: 0);
157#ifdef USE_AS_MEMRCHR
158 /* Also test the position close to the beginning for memrchr. */
159 do_test (0, 1, i + 1, 23);
160 do_test (0, 2, i + 1, 0);
161#endif
162 }
163
164 return ret;
165}
166
167#include <support/test-driver.c>
168

source code of glibc/benchtests/bench-memchr.c