1 /* Measure STRCHR 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 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcsrchr"
22 #else
23 # define TEST_NAME "strrchr"
24 #endif
25 #include "bench-string.h"
26 #include "json-lib.h"
27 
28 #define BIG_CHAR MAX_CHAR
29 
30 #ifdef WIDE
31 # define SIMPLE_STRRCHR simple_wcsrchr
32 # define SMALL_CHAR 1273
33 #else
34 # define SIMPLE_STRRCHR simple_strrchr
35 # define SMALL_CHAR 127
36 #endif
37 
38 typedef CHAR *(*proto_t) (const CHAR *, int);
39 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
40 
41 IMPL (SIMPLE_STRRCHR, 0)
42 IMPL (STRRCHR, 1)
43 
44 CHAR *
SIMPLE_STRRCHR(const CHAR * s,int c)45 SIMPLE_STRRCHR (const CHAR *s, int c)
46 {
47   const CHAR *ret = NULL;
48 
49   for (; *s != '\0'; ++s)
50     if (*s == (CHAR) c)
51       ret = s;
52 
53   return (CHAR *) (c == '\0' ? s : ret);
54 }
55 
56 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,const CHAR * s,int c,CHAR * exp_res)57 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, int c,
58 	     CHAR *exp_res)
59 {
60   CHAR *res = CALL (impl, s, c);
61   size_t i, iters = INNER_LOOP_ITERS8;
62   timing_t start, stop, cur;
63 
64   if (res != exp_res)
65     {
66       error (0, 0, "Wrong result in function %s %p %p", impl->name, res,
67 	     exp_res);
68       ret = 1;
69       return;
70     }
71 
72   TIMING_NOW (start);
73   for (i = 0; i < iters; ++i)
74     {
75       CALL (impl, s, c);
76     }
77   TIMING_NOW (stop);
78   TIMING_DIFF (cur, start, stop);
79 
80   json_element_double (json_ctx, (double) cur / (double) iters);
81 }
82 
83 static void
do_test(json_ctx_t * json_ctx,size_t align,size_t pos,size_t len,int seek_char,int max_char,size_t freq)84 do_test (json_ctx_t *json_ctx, size_t align, size_t pos, size_t len,
85 	 int seek_char, int max_char, size_t freq)
86 /* For wcsrchr: align here means align not in bytes,
87    but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
88    len for wcschr here isn't in bytes but it's number of wchar_t symbols.  */
89 {
90   size_t i;
91   size_t pos_chunk_sz = freq ? (pos / freq) : pos;
92   size_t last_pos = len;
93   CHAR *result;
94   CHAR *buf = (CHAR *) buf1;
95 
96   align &= (getpagesize () - 1);
97   if ((align + len) * sizeof (CHAR) >= page_size)
98     return;
99 
100   for (i = 0; i < len; ++i)
101     {
102       buf[align + i] = (random () * random ()) & max_char;
103       if (!buf[align + i])
104 	buf[align + i] = (random () * random ()) & max_char;
105       if (!buf[align + i])
106 	buf[align + i] = 1;
107       if ((i > pos || pos >= len) && buf[align + i] == seek_char)
108 	buf[align + i] = seek_char + 10 + (random () & 15);
109     }
110 
111   if (pos_chunk_sz == 0 && pos)
112     pos_chunk_sz = 1;
113 
114   for (i = pos_chunk_sz; i < pos && i < len; i += pos_chunk_sz)
115     {
116       buf[align + i] = seek_char;
117       last_pos = i;
118     }
119 
120   buf[align + len] = 0;
121 
122   if (pos < len)
123     {
124       buf[align + pos] = seek_char;
125       result = (CHAR *) (buf + align + pos);
126     }
127   else if (last_pos < len)
128     result = (CHAR *) (buf + align + last_pos);
129   else if (seek_char == 0)
130     result = (CHAR *) (buf + align + len);
131   else
132     result = NULL;
133 
134   json_element_object_begin (json_ctx);
135   json_attr_uint (json_ctx, "len", len);
136   json_attr_uint (json_ctx, "pos", pos);
137   json_attr_uint (json_ctx, "align", align);
138   json_attr_uint (json_ctx, "freq", freq);
139   json_attr_uint (json_ctx, "seek", seek_char);
140   json_attr_uint (json_ctx, "max_char", max_char);
141   json_array_begin (json_ctx, "timings");
142 
143   FOR_EACH_IMPL (impl, 0)
144     do_one_test (json_ctx, impl, (CHAR *) (buf + align), seek_char, result);
145 
146   json_array_end (json_ctx);
147   json_element_object_end (json_ctx);
148 }
149 
150 int
test_main(void)151 test_main (void)
152 {
153   json_ctx_t json_ctx;
154   size_t i, j;
155   int seek;
156 
157   test_init ();
158   json_init (&json_ctx, 0, stdout);
159 
160   json_document_begin (&json_ctx);
161   json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
162 
163   json_attr_object_begin (&json_ctx, "functions");
164   json_attr_object_begin (&json_ctx, TEST_NAME);
165   json_attr_string (&json_ctx, "bench-variant", "");
166 
167   json_array_begin (&json_ctx, "ifuncs");
168   FOR_EACH_IMPL (impl, 0)
169     json_element_string (&json_ctx, impl->name);
170   json_array_end (&json_ctx);
171 
172   json_array_begin (&json_ctx, "results");
173 
174   for (seek = 0; seek <= 23; seek += 23)
175     {
176       for (j = 1; j < 32; j += j)
177 	{
178 	  for (i = 1; i < 9; ++i)
179 	    {
180 	      do_test (&json_ctx, 0, 16 << i, 2048, seek, SMALL_CHAR, j);
181 	      do_test (&json_ctx, i, 16 << i, 2048, seek, SMALL_CHAR, j);
182 	    }
183 
184 	  for (i = 1; i < 8; ++i)
185 	    {
186 	      do_test (&json_ctx, i, 64, 256, seek, SMALL_CHAR, j);
187 	      do_test (&json_ctx, i, 64, 256, seek, BIG_CHAR, j);
188 
189 	      do_test (&json_ctx, i * 15, 64, 256, seek, SMALL_CHAR, j);
190 	      do_test (&json_ctx, i * 15, 64, 256, seek, BIG_CHAR, j);
191 	    }
192 
193 	  for (i = 0; i < 32; ++i)
194 	    {
195 	      do_test (&json_ctx, 0, i, i + 1, seek, SMALL_CHAR, j);
196 	      do_test (&json_ctx, 0, i, i + 1, seek, BIG_CHAR, j);
197 	      do_test (&json_ctx, getpagesize () - i / 2 - 1, i, i + 1, seek,
198 		       SMALL_CHAR, j);
199 	    }
200 	  if (seek == 0)
201 	    {
202 	      break;
203 	    }
204 	}
205     }
206 
207   json_array_end (&json_ctx);
208   json_attr_object_end (&json_ctx);
209   json_attr_object_end (&json_ctx);
210   json_document_end (&json_ctx);
211 
212   return ret;
213 }
214 
215 #include <support/test-driver.c>
216