1 /* Measure strncasecmp 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 <ctype.h>
20 #define TEST_MAIN
21 #define TEST_NAME "strncasecmp"
22 #include "bench-string.h"
23 #include "json-lib.h"
24 
25 typedef int (*proto_t) (const char *, const char *, size_t);
26 static int simple_strncasecmp (const char *, const char *, size_t);
27 
28 IMPL (simple_strncasecmp, 0)
29 IMPL (strncasecmp, 1)
30 
31 static int
simple_strncasecmp(const char * s1,const char * s2,size_t n)32 simple_strncasecmp (const char *s1, const char *s2, size_t n)
33 {
34   int ret;
35 
36   if (n == 0)
37     return 0;
38 
39   while ((ret = ((unsigned char) tolower (*s1)
40 		 - (unsigned char) tolower (*s2))) == 0
41 	 && *s1++)
42     {
43       if (--n == 0)
44 	return 0;
45       ++s2;
46     }
47   return ret;
48 }
49 
50 static void
do_one_test(json_ctx_t * json_ctx,impl_t * impl,const char * s1,const char * s2,size_t n,int exp_result)51 do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s1,
52              const char *s2, size_t n, int exp_result)
53 {
54   size_t i, iters = INNER_LOOP_ITERS;
55   timing_t start, stop, cur;
56 
57   TIMING_NOW (start);
58   for (i = 0; i < iters; ++i)
59     {
60       CALL (impl, s1, s2, n);
61     }
62   TIMING_NOW (stop);
63 
64   TIMING_DIFF (cur, start, stop);
65 
66   json_element_double (json_ctx, (double) cur / (double) iters);
67 }
68 
69 static void
do_test(json_ctx_t * json_ctx,size_t align1,size_t align2,size_t n,size_t len,int max_char,int exp_result)70 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t n,
71          size_t len, int max_char, int exp_result)
72 {
73   size_t i;
74   char *s1, *s2;
75 
76   if (len == 0)
77     return;
78 
79   align1 &= 7;
80   if (align1 + len + 1 >= page_size)
81     return;
82 
83   align2 &= 7;
84   if (align2 + len + 1 >= page_size)
85     return;
86 
87   s1 = (char *) (buf1 + align1);
88   s2 = (char *) (buf2 + align2);
89 
90   for (i = 0; i < len; i++)
91     {
92       s1[i] = toupper (1 + 23 * i % max_char);
93       s2[i] = tolower (s1[i]);
94     }
95 
96   s1[len] = s2[len] = 0;
97   s1[len + 1] = 23;
98   s2[len + 1] = 24 + exp_result;
99   if ((s2[len - 1] == 'z' && exp_result == -1)
100       || (s2[len - 1] == 'a' && exp_result == 1))
101     s1[len - 1] += exp_result;
102   else
103     s2[len - 1] -= exp_result;
104 
105   json_element_object_begin (json_ctx);
106   json_attr_uint (json_ctx, "length", len);
107   json_attr_uint (json_ctx, "n", n);
108   json_attr_uint (json_ctx, "align1", align1);
109   json_attr_uint (json_ctx, "align2", align2);
110   json_attr_uint (json_ctx, "max_char", max_char);
111   json_array_begin (json_ctx, "timings");
112 
113   FOR_EACH_IMPL (impl, 0)
114     do_one_test (json_ctx, impl, s1, s2, n, exp_result);
115 
116   json_array_end (json_ctx);
117   json_element_object_end (json_ctx);
118 }
119 
120 int
test_main(void)121 test_main (void)
122 {
123   json_ctx_t json_ctx;
124   size_t i;
125 
126   test_init ();
127 
128   json_init (&json_ctx, 0, stdout);
129 
130   json_document_begin (&json_ctx);
131   json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
132 
133   json_attr_object_begin (&json_ctx, "functions");
134   json_attr_object_begin (&json_ctx, TEST_NAME);
135   json_attr_string (&json_ctx, "bench-variant", "");
136 
137   json_array_begin (&json_ctx, "ifuncs");
138   FOR_EACH_IMPL (impl, 0)
139     json_element_string (&json_ctx, impl->name);
140   json_array_end (&json_ctx);
141 
142   json_array_begin (&json_ctx, "results");
143 
144   for (i = 1; i < 16; ++i)
145     {
146       do_test (&json_ctx, i, i, i - 1, i, 127, 0);
147 
148       do_test (&json_ctx, i, i, i, i, 127, 0);
149       do_test (&json_ctx, i, i, i, i, 127, 1);
150       do_test (&json_ctx, i, i, i, i, 127, -1);
151 
152       do_test (&json_ctx, i, i, i + 1, i, 127, 0);
153       do_test (&json_ctx, i, i, i + 1, i, 127, 1);
154       do_test (&json_ctx, i, i, i + 1, i, 127, -1);
155     }
156 
157   for (i = 1; i < 10; ++i)
158     {
159       do_test (&json_ctx, 0, 0, (2 << i) - 1, 2 << i, 127, 0);
160       do_test (&json_ctx, 0, 0, 2 << i, 2 << i, 254, 0);
161       do_test (&json_ctx, 0, 0, (2 << i) + 1, 2 << i, 127, 0);
162 
163       do_test (&json_ctx, 0, 0, (2 << i) + 1, 2 << i, 254, 0);
164 
165       do_test (&json_ctx, 0, 0, 2 << i, 2 << i, 127, 1);
166       do_test (&json_ctx, 0, 0, (2 << i) + 10, 2 << i, 127, 1);
167 
168       do_test (&json_ctx, 0, 0, 2 << i, 2 << i, 254, 1);
169       do_test (&json_ctx, 0, 0, (2 << i) + 10, 2 << i, 254, 1);
170 
171       do_test (&json_ctx, 0, 0, 2 << i, 2 << i, 127, -1);
172       do_test (&json_ctx, 0, 0, (2 << i) + 10, 2 << i, 127, -1);
173 
174       do_test (&json_ctx, 0, 0, 2 << i, 2 << i, 254, -1);
175       do_test (&json_ctx, 0, 0, (2 << i) + 10, 2 << i, 254, -1);
176     }
177 
178   for (i = 1; i < 8; ++i)
179     {
180       do_test (&json_ctx, i, 2 * i, (8 << i) - 1, 8 << i, 127, 0);
181       do_test (&json_ctx, i, 2 * i, 8 << i, 8 << i, 127, 0);
182       do_test (&json_ctx, i, 2 * i, (8 << i) + 100, 8 << i, 127, 0);
183 
184       do_test (&json_ctx, 2 * i, i, (8 << i) - 1, 8 << i, 254, 0);
185       do_test (&json_ctx, 2 * i, i, 8 << i, 8 << i, 254, 0);
186       do_test (&json_ctx, 2 * i, i, (8 << i) + 100, 8 << i, 254, 0);
187 
188       do_test (&json_ctx, i, 2 * i, 8 << i, 8 << i, 127, 1);
189       do_test (&json_ctx, i, 2 * i, (8 << i) + 100, 8 << i, 127, 1);
190 
191       do_test (&json_ctx, 2 * i, i, 8 << i, 8 << i, 254, 1);
192       do_test (&json_ctx, 2 * i, i, (8 << i) + 100, 8 << i, 254, 1);
193 
194       do_test (&json_ctx, i, 2 * i, 8 << i, 8 << i, 127, -1);
195       do_test (&json_ctx, i, 2 * i, (8 << i) + 100, 8 << i, 127, -1);
196 
197       do_test (&json_ctx, 2 * i, i, 8 << i, 8 << i, 254, -1);
198       do_test (&json_ctx, 2 * i, i, (8 << i) + 100, 8 << i, 254, -1);
199     }
200 
201   json_array_end (&json_ctx);
202   json_attr_object_end (&json_ctx);
203   json_attr_object_end (&json_ctx);
204   json_document_end (&json_ctx);
205 
206   return ret;
207 }
208 
209 #include <support/test-driver.c>
210