Update copyright notices with scripts/update-copyrights
[glibc.git] / benchtests / bench-strncmp.c
blobd050e8bf754df9fbd00d721ece49c13b6631b58a
1 /* Measure strncmp functions.
2 Copyright (C) 2013-2014 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 <http://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "strncmp"
21 #include "bench-string.h"
23 typedef int (*proto_t) (const char *, const char *, size_t);
24 int simple_strncmp (const char *, const char *, size_t);
25 int stupid_strncmp (const char *, const char *, size_t);
27 IMPL (stupid_strncmp, 0)
28 IMPL (simple_strncmp, 0)
29 IMPL (strncmp, 1)
31 int
32 simple_strncmp (const char *s1, const char *s2, size_t n)
34 int ret = 0;
36 while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
37 && *s1++);
38 return ret;
41 int
42 stupid_strncmp (const char *s1, const char *s2, size_t n)
44 size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
45 int ret = 0;
47 n = ns1 < n ? ns1 : n;
48 n = ns2 < n ? ns2 : n;
49 while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0);
50 return ret;
53 static void
54 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
55 int exp_result)
57 size_t i, iters = INNER_LOOP_ITERS;
58 timing_t start, stop, cur;
60 TIMING_NOW (start);
61 for (i = 0; i < iters; ++i)
63 CALL (impl, s1, s2, n);
65 TIMING_NOW (stop);
67 TIMING_DIFF (cur, start, stop);
69 TIMING_PRINT_MEAN ((double) cur, (double) iters);
72 static void
73 do_test_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char,
74 int exp_result)
76 size_t i, align_n;
77 char *s1, *s2;
79 if (n == 0)
81 s1 = (char*)(buf1 + page_size);
82 s2 = (char*)(buf2 + page_size);
83 printf ("Length %4zd/%4zd:", len, n);
85 FOR_EACH_IMPL (impl, 0)
86 do_one_test (impl, s1, s2, n, 0);
88 putchar ('\n');
90 return;
93 align1 &= 15;
94 align2 &= 15;
95 align_n = (page_size - n) & 15;
97 s1 = (char*)(buf1 + page_size - n);
98 s2 = (char*)(buf2 + page_size - n);
100 if (align1 < align_n)
101 s1 -= (align_n - align1);
103 if (align2 < align_n)
104 s2 -= (align_n - align2);
106 for (i = 0; i < n; i++)
107 s1[i] = s2[i] = 1 + 23 * i % max_char;
109 if (len < n)
111 s1[len] = 0;
112 s2[len] = 0;
113 if (exp_result < 0)
114 s2[len] = 32;
115 else if (exp_result > 0)
116 s1[len] = 64;
119 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
121 FOR_EACH_IMPL (impl, 0)
122 do_one_test (impl, s1, s2, n, exp_result);
124 putchar ('\n');
127 static void
128 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
129 int exp_result)
131 size_t i;
132 char *s1, *s2;
134 if (n == 0)
135 return;
137 align1 &= 7;
138 if (align1 + n + 1 >= page_size)
139 return;
141 align2 &= 7;
142 if (align2 + n + 1 >= page_size)
143 return;
145 s1 = (char*)(buf1 + align1);
146 s2 = (char*)(buf2 + align2);
148 for (i = 0; i < n; i++)
149 s1[i] = s2[i] = 1 + 23 * i % max_char;
151 s1[n] = 24 + exp_result;
152 s2[n] = 23;
153 s1[len] = 0;
154 s2[len] = 0;
155 if (exp_result < 0)
156 s2[len] = 32;
157 else if (exp_result > 0)
158 s1[len] = 64;
159 if (len >= n)
160 s2[n - 1] -= exp_result;
162 printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
164 FOR_EACH_IMPL (impl, 0)
165 do_one_test (impl, (char*)s1, (char*)s2, n, exp_result);
167 putchar ('\n');
171 test_main (void)
173 size_t i;
175 test_init ();
177 printf ("%23s", "");
178 FOR_EACH_IMPL (impl, 0)
179 printf ("\t%s", impl->name);
180 putchar ('\n');
182 for (i =0; i < 16; ++i)
184 do_test (0, 0, 8, i, 127, 0);
185 do_test (0, 0, 8, i, 127, -1);
186 do_test (0, 0, 8, i, 127, 1);
187 do_test (i, i, 8, i, 127, 0);
188 do_test (i, i, 8, i, 127, 1);
189 do_test (i, i, 8, i, 127, -1);
190 do_test (i, 2 * i, 8, i, 127, 0);
191 do_test (2 * i, i, 8, i, 127, 1);
192 do_test (i, 3 * i, 8, i, 127, -1);
193 do_test (0, 0, 8, i, 255, 0);
194 do_test (0, 0, 8, i, 255, -1);
195 do_test (0, 0, 8, i, 255, 1);
196 do_test (i, i, 8, i, 255, 0);
197 do_test (i, i, 8, i, 255, 1);
198 do_test (i, i, 8, i, 255, -1);
199 do_test (i, 2 * i, 8, i, 255, 0);
200 do_test (2 * i, i, 8, i, 255, 1);
201 do_test (i, 3 * i, 8, i, 255, -1);
204 for (i = 1; i < 8; ++i)
206 do_test (0, 0, 8 << i, 16 << i, 127, 0);
207 do_test (0, 0, 8 << i, 16 << i, 127, 1);
208 do_test (0, 0, 8 << i, 16 << i, 127, -1);
209 do_test (0, 0, 8 << i, 16 << i, 255, 0);
210 do_test (0, 0, 8 << i, 16 << i, 255, 1);
211 do_test (0, 0, 8 << i, 16 << i, 255, -1);
212 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
213 do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
214 do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
215 do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
218 do_test_limit (0, 0, 0, 0, 127, 0);
219 do_test_limit (4, 0, 21, 20, 127, 0);
220 do_test_limit (0, 4, 21, 20, 127, 0);
221 do_test_limit (8, 0, 25, 24, 127, 0);
222 do_test_limit (0, 8, 25, 24, 127, 0);
224 for (i = 0; i < 8; ++i)
226 do_test_limit (0, 0, 17 - i, 16 - i, 127, 0);
227 do_test_limit (0, 0, 17 - i, 16 - i, 255, 0);
228 do_test_limit (0, 0, 15 - i, 16 - i, 127, 0);
229 do_test_limit (0, 0, 15 - i, 16 - i, 127, 1);
230 do_test_limit (0, 0, 15 - i, 16 - i, 127, -1);
231 do_test_limit (0, 0, 15 - i, 16 - i, 255, 0);
232 do_test_limit (0, 0, 15 - i, 16 - i, 255, 1);
233 do_test_limit (0, 0, 15 - i, 16 - i, 255, -1);
236 return ret;
239 #include "../test-skeleton.c"