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