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 
40 typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
41 CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t);
42 
43 IMPL (SIMPLE_MEMCHR, 0)
44 IMPL (MEMCHR, 1)
45 
46 CHAR *
SIMPLE_MEMCHR(const CHAR * s,int c,size_t n)47 SIMPLE_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 
56 #include "json-lib.h"
57 
58 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,const CHAR * s,int c,size_t n)59 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
60 	     size_t n)
61 {
62   size_t i, iters = INNER_LOOP_ITERS_LARGE;
63   timing_t start, stop, cur;
64 
65   TIMING_NOW (start);
66   for (i = 0; i < iters; ++i)
67     {
68       CALL (impl, s, c, n);
69     }
70   TIMING_NOW (stop);
71 
72   TIMING_DIFF (cur, start, stop);
73 
74   json_element_double (json_ctx, (double) cur / (double) iters);
75 }
76 
77 static void
do_test(json_ctx_t * json_ctx,size_t align,size_t pos,size_t len,int seek_char,int invert_pos)78 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
79 	 int seek_char, int invert_pos)
80 {
81   size_t i;
82 
83   align &= getpagesize () - 1;
84   if ((align + len) * sizeof (CHAR) >= page_size)
85     return;
86 
87   CHAR *buf = (CHAR *) (buf1);
88 
89   for (i = 0; i < len; ++i)
90     {
91       buf[align + i] = 1 + 23 * i % SMALL_CHAR;
92       if (buf[align + i] == seek_char)
93 	buf[align + i] = seek_char + 1;
94     }
95   buf[align + len] = 0;
96 
97   if (pos < len)
98     {
99       if (invert_pos)
100 	buf[align + len - pos] = seek_char;
101       else
102 	buf[align + pos] = seek_char;
103       buf[align + len] = -seek_char;
104     }
105   else
106     {
107       buf[align + len] = seek_char;
108     }
109 
110   json_element_object_begin (json_ctx);
111   json_attr_uint (json_ctx, "align", align);
112   json_attr_uint (json_ctx, "pos", pos);
113   json_attr_uint (json_ctx, "len", len);
114   json_attr_uint (json_ctx, "seek_char", seek_char);
115   json_attr_uint (json_ctx, "invert_pos", invert_pos);
116 
117   json_array_begin (json_ctx, "timings");
118 
119   FOR_EACH_IMPL (impl, 0)
120     do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, len);
121 
122   json_array_end (json_ctx);
123   json_element_object_end (json_ctx);
124 }
125 
126 int
test_main(void)127 test_main (void)
128 {
129   size_t i;
130   int repeats;
131   json_ctx_t json_ctx;
132   test_init ();
133 
134   json_init (&json_ctx, 0, stdout);
135 
136   json_document_begin (&json_ctx);
137   json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
138 
139   json_attr_object_begin (&json_ctx, "functions");
140   json_attr_object_begin (&json_ctx, TEST_NAME);
141   json_attr_string (&json_ctx, "bench-variant", "");
142 
143   json_array_begin (&json_ctx, "ifuncs");
144   FOR_EACH_IMPL (impl, 0)
145     json_element_string (&json_ctx, impl->name);
146   json_array_end (&json_ctx);
147 
148   json_array_begin (&json_ctx, "results");
149 
150   for (repeats = 0; repeats < 2; ++repeats)
151     {
152       for (i = 1; i < 8; ++i)
153 	{
154 	  do_test (&json_ctx, 0, 16 << i, 2048, 23, repeats);
155 	  do_test (&json_ctx, i, 64, 256, 23, repeats);
156 	  do_test (&json_ctx, 0, 16 << i, 2048, 0, repeats);
157 	  do_test (&json_ctx, i, 64, 256, 0, repeats);
158 
159 	  do_test (&json_ctx, getpagesize () - 15, 64, 256, 0, repeats);
160 #ifdef USE_AS_MEMRCHR
161 	  /* Also test the position close to the beginning for memrchr.  */
162 	  do_test (&json_ctx, 0, i, 256, 23, repeats);
163 	  do_test (&json_ctx, 0, i, 256, 0, repeats);
164 	  do_test (&json_ctx, i, i, 256, 23, repeats);
165 	  do_test (&json_ctx, i, i, 256, 0, repeats);
166 #endif
167 	}
168       for (i = 1; i < 8; ++i)
169 	{
170 	  do_test (&json_ctx, i, i << 5, 192, 23, repeats);
171 	  do_test (&json_ctx, i, i << 5, 192, 0, repeats);
172 	  do_test (&json_ctx, i, i << 5, 256, 23, repeats);
173 	  do_test (&json_ctx, i, i << 5, 256, 0, repeats);
174 	  do_test (&json_ctx, i, i << 5, 512, 23, repeats);
175 	  do_test (&json_ctx, i, i << 5, 512, 0, repeats);
176 
177 	  do_test (&json_ctx, getpagesize () - 15, i << 5, 256, 23, repeats);
178 	}
179       for (i = 1; i < 32; ++i)
180 	{
181 	  do_test (&json_ctx, 0, i, i + 1, 23, repeats);
182 	  do_test (&json_ctx, 0, i, i + 1, 0, repeats);
183 	  do_test (&json_ctx, i, i, i + 1, 23, repeats);
184 	  do_test (&json_ctx, i, i, i + 1, 0, repeats);
185 	  do_test (&json_ctx, 0, i, i - 1, 23, repeats);
186 	  do_test (&json_ctx, 0, i, i - 1, 0, repeats);
187 	  do_test (&json_ctx, i, i, i - 1, 23, repeats);
188 	  do_test (&json_ctx, i, i, i - 1, 0, repeats);
189 
190 	  do_test (&json_ctx, getpagesize () / 2, i, i + 1, 23, repeats);
191 	  do_test (&json_ctx, getpagesize () / 2, i, i + 1, 0, repeats);
192 	  do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 23, repeats);
193 	  do_test (&json_ctx, getpagesize () / 2 + i, i, i + 1, 0, repeats);
194 	  do_test (&json_ctx, getpagesize () / 2, i, i - 1, 23, repeats);
195 	  do_test (&json_ctx, getpagesize () / 2, i, i - 1, 0, repeats);
196 	  do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 23, repeats);
197 	  do_test (&json_ctx, getpagesize () / 2 + i, i, i - 1, 0, repeats);
198 
199 	  do_test (&json_ctx, getpagesize () - 15, i, i - 1, 23, repeats);
200 	  do_test (&json_ctx, getpagesize () - 15, i, i - 1, 0, repeats);
201 
202 	  do_test (&json_ctx, getpagesize () - 15, i, i + 1, 23, repeats);
203 	  do_test (&json_ctx, getpagesize () - 15, i, i + 1, 0, repeats);
204 
205 #ifdef USE_AS_MEMRCHR
206 	  do_test (&json_ctx, 0, 1, i + 1, 23, repeats);
207 	  do_test (&json_ctx, 0, 2, i + 1, 0, repeats);
208 #endif
209 	}
210 #ifndef USE_AS_MEMRCHR
211       break;
212 #endif
213     }
214 
215   json_array_end (&json_ctx);
216   json_attr_object_end (&json_ctx);
217   json_attr_object_end (&json_ctx);
218   json_document_end (&json_ctx);
219 
220   return ret;
221 }
222 
223 #include <support/test-driver.c>
224