Remove powerpc, sparc fdim inlines (bug 22987).
[glibc.git] / benchtests / bench-string.h
blob94aaafdaf2e1319b8cebb5baaf51f02b52182d5e
1 /* Measure string and memory functions.
2 Copyright (C) 2013-2018 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 #include <getopt.h>
20 #include <sys/cdefs.h>
22 /* We are compiled under _ISOMAC, so libc-symbols.h does not do this
23 for us. */
24 #include "config.h"
25 #ifdef HAVE_CC_INHIBIT_LOOP_TO_LIBCALL
26 # define inhibit_loop_to_libcall \
27 __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
28 #else
29 # define inhibit_loop_to_libcall
30 #endif
32 typedef struct
34 const char *name;
35 void (*fn) (void);
36 long test;
37 } impl_t;
38 extern impl_t __start_impls[], __stop_impls[];
40 #define IMPL(name, test) \
41 impl_t tst_ ## name \
42 __attribute__ ((section ("impls"), aligned (sizeof (void *)))) \
43 = { __STRING (name), (void (*) (void))name, test };
45 #ifdef TEST_MAIN
47 # ifndef _GNU_SOURCE
48 # define _GNU_SOURCE
49 # endif
51 # undef __USE_STRING_INLINES
53 # include <stdio.h>
54 # include <stdlib.h>
55 # include <string.h>
56 # include <sys/mman.h>
57 # include <sys/param.h>
58 # include <unistd.h>
59 # include <fcntl.h>
60 # include <error.h>
61 # include <errno.h>
62 # include <time.h>
63 # include <ifunc-impl-list.h>
64 # define GL(x) _##x
65 # define GLRO(x) _##x
66 # include "bench-timing.h"
69 # define TEST_FUNCTION test_main
70 # ifndef TIMEOUT
71 # define TIMEOUT (4 * 60)
72 # endif
73 # define OPT_ITERATIONS 10000
74 # define OPT_RANDOM 10001
75 # define OPT_SEED 10002
77 # define INNER_LOOP_ITERS 64
79 unsigned char *buf1, *buf2;
80 int ret, do_srandom;
81 unsigned int seed;
82 size_t page_size;
84 # ifndef ITERATIONS
85 size_t iterations = 100000;
86 # define ITERATIONS_OPTIONS \
87 { "iterations", required_argument, NULL, OPT_ITERATIONS },
88 # define ITERATIONS_PROCESS \
89 case OPT_ITERATIONS: \
90 iterations = strtoul (optarg, NULL, 0); \
91 break;
92 # define ITERATIONS iterations
93 # else
94 # define ITERATIONS_OPTIONS
95 # define ITERATIONS_PROCESS
96 # endif
98 # define CMDLINE_OPTIONS ITERATIONS_OPTIONS \
99 { "random", no_argument, NULL, OPT_RANDOM }, \
100 { "seed", required_argument, NULL, OPT_SEED },
102 static void __attribute__ ((used))
103 cmdline_process_function (int c)
105 switch (c)
107 ITERATIONS_PROCESS
108 case OPT_RANDOM:
110 int fdr = open ("/dev/urandom", O_RDONLY);
111 if (fdr < 0 || read (fdr, &seed, sizeof(seed)) != sizeof (seed))
112 seed = time (NULL);
113 if (fdr >= 0)
114 close (fdr);
115 do_srandom = 1;
116 break;
119 case OPT_SEED:
120 seed = strtoul (optarg, NULL, 0);
121 do_srandom = 1;
122 break;
125 # define CMDLINE_PROCESS cmdline_process_function
126 # define CALL(impl, ...) \
127 (* (proto_t) (impl)->fn) (__VA_ARGS__)
129 # ifdef TEST_NAME
130 /* Increase size of FUNC_LIST if assert is triggered at run-time. */
131 static struct libc_ifunc_impl func_list[32];
132 static int func_count;
133 static int impl_count = -1;
134 static impl_t *impl_array;
136 # define FOR_EACH_IMPL(impl, notall) \
137 impl_t *impl; \
138 int count; \
139 if (impl_count == -1) \
141 impl_count = 0; \
142 if (func_count != 0) \
144 int f; \
145 impl_t *skip = NULL, *a; \
146 for (impl = __start_impls; impl < __stop_impls; ++impl) \
147 if (strcmp (impl->name, TEST_NAME) == 0) \
148 skip = impl; \
149 else \
150 impl_count++; \
151 a = impl_array = malloc ((impl_count + func_count) * \
152 sizeof (impl_t)); \
153 for (impl = __start_impls; impl < __stop_impls; ++impl) \
154 if (impl != skip) \
155 *a++ = *impl; \
156 for (f = 0; f < func_count; f++) \
157 if (func_list[f].usable) \
159 a->name = func_list[f].name; \
160 a->fn = func_list[f].fn; \
161 a->test = 1; \
162 a++; \
164 impl_count = a - impl_array; \
166 else \
168 impl_count = __stop_impls - __start_impls; \
169 impl_array = __start_impls; \
172 impl = impl_array; \
173 for (count = 0; count < impl_count; ++count, ++impl) \
174 if (!notall || impl->test)
175 # else /* !TEST_NAME */
176 # define FOR_EACH_IMPL(impl, notall) \
177 for (impl_t *impl = __start_impls; impl < __stop_impls; ++impl) \
178 if (!notall || impl->test)
179 # endif /* !TEST_NAME */
181 # ifndef BUF1PAGES
182 # define BUF1PAGES 1
183 # endif
185 static void
186 alloc_bufs (void)
188 page_size = 2 * getpagesize ();
189 # ifdef MIN_PAGE_SIZE
190 if (page_size < MIN_PAGE_SIZE)
191 page_size = MIN_PAGE_SIZE;
192 # endif
193 buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
194 MAP_PRIVATE | MAP_ANON, -1, 0);
195 if (buf1 == MAP_FAILED)
196 error (EXIT_FAILURE, errno, "mmap failed for buf1");
197 if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
198 error (EXIT_FAILURE, errno, "mprotect failed for buf1");
199 buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
200 MAP_PRIVATE | MAP_ANON, -1, 0);
201 if (buf2 == MAP_FAILED)
202 error (EXIT_FAILURE, errno, "mmap failed for buf2");
203 if (mprotect (buf2 + page_size, page_size, PROT_NONE))
204 error (EXIT_FAILURE, errno, "mprotect failed for buf2");
207 static void
208 __attribute__ ((unused))
209 realloc_bufs (void)
211 int ret = 0;
213 if (buf1)
214 ret = munmap (buf1, (BUF1PAGES + 1) * page_size);
216 if (ret != 0)
217 error (EXIT_FAILURE, errno, "munmap failed for buf1");
219 if (buf2)
220 ret = munmap (buf2, 2 * page_size);
222 if (ret != 0)
223 error (EXIT_FAILURE, errno, "munmap failed for buf2");
225 alloc_bufs ();
228 static void
229 test_init (void)
231 # ifdef TEST_NAME
232 func_count = __libc_ifunc_impl_list (TEST_NAME, func_list,
233 (sizeof func_list
234 / sizeof func_list[0]));
235 # endif
237 alloc_bufs ();
239 if (do_srandom)
241 printf ("Setting seed to 0x%x\n", seed);
242 srandom (seed);
246 #endif /* TEST_MAIN */