elf: Ignore GLIBC_TUNABLES for setuid/setgid binaries
[glibc.git] / benchtests / bench-rawmemchr.c
blob088c516e59383f93e6cbcbe89825a866bebef518
1 /* Measure memchr functions.
2 Copyright (C) 2013-2023 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 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <stdint.h>
22 #define TEST_MAIN
23 #define TEST_NAME "rawmemchr"
24 #include "bench-string.h"
26 #include "json-lib.h"
28 typedef char *(*proto_t) (const char *, int);
30 char *
31 generic_rawmemchr (const char *s, int c)
33 if ((unsigned char) c != 0)
34 return memchr (s, c, PTRDIFF_MAX);
35 return (char *)s + strlen (s);
38 IMPL (rawmemchr, 1)
39 IMPL (generic_rawmemchr, 0)
41 static void
42 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s, int c, char *exp_res)
44 size_t i, iters = INNER_LOOP_ITERS_LARGE * 4;
45 timing_t start, stop, cur;
46 char *res = CALL (impl, s, c);
47 if (res != exp_res)
49 error (0, 0, "Wrong result in function %s %p %p", impl->name,
50 res, exp_res);
51 ret = 1;
52 return;
55 TIMING_NOW (start);
56 for (i = 0; i < iters; ++i)
58 CALL (impl, s, c);
60 TIMING_NOW (stop);
62 TIMING_DIFF (cur, start, stop);
64 json_element_double (json_ctx, (double) cur / (double) iters);
67 static void
68 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int seek_char)
70 size_t i;
71 char *result;
73 align &= getpagesize () - 1;
74 if (align + len >= page_size)
75 return;
77 for (i = 0; i < len; ++i)
79 buf1[align + i] = 1 + 23 * i % 127;
80 if (buf1[align + i] == seek_char)
81 buf1[align + i] = seek_char + 1;
83 buf1[align + len] = 0;
85 assert (pos < len);
87 buf1[align + pos] = seek_char;
88 buf1[align + len] = -seek_char;
89 result = (char *) (buf1 + align + pos);
91 json_element_object_begin (json_ctx);
92 json_attr_uint (json_ctx, "length", pos);
93 json_attr_uint (json_ctx, "alignment", align);
94 json_attr_uint (json_ctx, "char", seek_char);
95 json_array_begin (json_ctx, "timings");
97 FOR_EACH_IMPL (impl, 0)
98 do_one_test (json_ctx, impl, (char *) (buf1 + align), seek_char, result);
100 json_array_end (json_ctx);
101 json_element_object_end (json_ctx);
105 test_main (void)
107 json_ctx_t json_ctx;
108 size_t i;
109 test_init ();
111 json_init (&json_ctx, 0, stdout);
113 json_document_begin (&json_ctx);
114 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
116 json_attr_object_begin (&json_ctx, "functions");
117 json_attr_object_begin (&json_ctx, TEST_NAME);
118 json_attr_string (&json_ctx, "bench-variant", "");
120 json_array_begin (&json_ctx, "ifuncs");
121 FOR_EACH_IMPL (impl, 0)
122 json_element_string (&json_ctx, impl->name);
123 json_array_end (&json_ctx);
125 json_array_begin (&json_ctx, "results");
127 for (i = 1; i < 7; ++i)
129 do_test (&json_ctx, 0, 16 << i, 2048, 23);
130 do_test (&json_ctx, i, 64, 256, 23);
131 do_test (&json_ctx, 0, 16 << i, 2048, 0);
132 do_test (&json_ctx, i, 64, 256, 0);
134 for (i = 1; i < 32; ++i)
136 do_test (&json_ctx, 0, i, i + 1, 23);
137 do_test (&json_ctx, 0, i, i + 1, 0);
139 for (; i < 256; i += 32)
141 do_test (&json_ctx, 0, i, i + 1, 23);
142 do_test (&json_ctx, 0, i - 1, i, 23);
144 for (; i < 512; i += 64)
146 do_test (&json_ctx, 0, i, i + 1, 23);
147 do_test (&json_ctx, 0, i - 1, i, 23);
149 for (; i < 1024; i += 128)
151 do_test (&json_ctx, 0, i, i + 1, 23);
152 do_test (&json_ctx, 0, i - 1, i, 23);
154 for (; i < 2048; i += 256)
156 do_test (&json_ctx, 0, i, i + 1, 23);
157 do_test (&json_ctx, 0, i - 1, i, 23);
159 for (; i < 4096; i += 512)
161 do_test (&json_ctx, 0, i, i + 1, 23);
162 do_test (&json_ctx, 0, i - 1, i, 23);
165 json_array_end (&json_ctx);
166 json_attr_object_end (&json_ctx);
167 json_attr_object_end (&json_ctx);
168 json_document_end (&json_ctx);
170 return ret;
173 #include <support/test-driver.c>