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 #include <assert.h>
20 #include <stdint.h>
21
22 #define TEST_MAIN
23 #define TEST_NAME "rawmemchr"
24 #include "bench-string.h"
25
26 #include "json-lib.h"
27
28 typedef char *(*proto_t) (const char *, int);
29
30 char *
generic_rawmemchr(const char * s,int c)31 generic_rawmemchr (const char *s, int c)
32 {
33 if (c != 0)
34 return memchr (s, c, PTRDIFF_MAX);
35 return (char *)s + strlen (s);
36 }
37
38 IMPL (rawmemchr, 1)
39 IMPL (generic_rawmemchr, 0)
40
41 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,const char * s,int c,char * exp_res)42 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s, int c, char *exp_res)
43 {
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)
48 {
49 error (0, 0, "Wrong result in function %s %p %p", impl->name,
50 res, exp_res);
51 ret = 1;
52 return;
53 }
54
55 TIMING_NOW (start);
56 for (i = 0; i < iters; ++i)
57 {
58 CALL (impl, s, c);
59 }
60 TIMING_NOW (stop);
61
62 TIMING_DIFF (cur, start, stop);
63
64 json_element_double (json_ctx, (double) cur / (double) iters);
65 }
66
67 static void
do_test(json_ctx_t * json_ctx,size_t align,size_t pos,size_t len,int seek_char)68 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len, int seek_char)
69 {
70 size_t i;
71 char *result;
72
73 align &= 7;
74 if (align + len >= page_size)
75 return;
76
77 for (i = 0; i < len; ++i)
78 {
79 buf1[align + i] = 1 + 23 * i % 127;
80 if (buf1[align + i] == seek_char)
81 buf1[align + i] = seek_char + 1;
82 }
83 buf1[align + len] = 0;
84
85 assert (pos < len);
86
87 buf1[align + pos] = seek_char;
88 buf1[align + len] = -seek_char;
89 result = (char *) (buf1 + align + pos);
90
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");
96
97 FOR_EACH_IMPL (impl, 0)
98 do_one_test (json_ctx, impl, (char *) (buf1 + align), seek_char, result);
99
100 json_array_end (json_ctx);
101 json_element_object_end (json_ctx);
102 }
103
104 int
test_main(void)105 test_main (void)
106 {
107 json_ctx_t json_ctx;
108 size_t i;
109
110 test_init ();
111
112 json_init (&json_ctx, 0, stdout);
113
114 json_document_begin (&json_ctx);
115 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
116
117 json_attr_object_begin (&json_ctx, "functions");
118 json_attr_object_begin (&json_ctx, TEST_NAME);
119 json_attr_string (&json_ctx, "bench-variant", "");
120
121 json_array_begin (&json_ctx, "ifuncs");
122 FOR_EACH_IMPL (impl, 0)
123 json_element_string (&json_ctx, impl->name);
124 json_array_end (&json_ctx);
125
126 json_array_begin (&json_ctx, "results");
127
128 for (i = 1; i < 7; ++i)
129 {
130 do_test (&json_ctx, 0, 16 << i, 2048, 23);
131 do_test (&json_ctx, i, 64, 256, 23);
132 do_test (&json_ctx, 0, 16 << i, 2048, 0);
133 do_test (&json_ctx, i, 64, 256, 0);
134 }
135 for (i = 1; i < 32; ++i)
136 {
137 do_test (&json_ctx, 0, i, i + 1, 23);
138 do_test (&json_ctx, 0, i, i + 1, 0);
139 }
140
141 json_array_end (&json_ctx);
142 json_attr_object_end (&json_ctx);
143 json_attr_object_end (&json_ctx);
144 json_document_end (&json_ctx);
145
146 return ret;
147 }
148
149 #include <support/test-driver.c>
150