packaging: add 64bit libs on 32bit build env
[platform/upstream/linaro-glibc.git] / benchtests / bench-strncmp.c
1 /* Measure strncmp functions.
2    Copyright (C) 2013-2014 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    <http://www.gnu.org/licenses/>.  */
18
19 #define TEST_MAIN
20 #define TEST_NAME "strncmp"
21 #include "bench-string.h"
22
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);
26
27 IMPL (stupid_strncmp, 0)
28 IMPL (simple_strncmp, 0)
29 IMPL (strncmp, 1)
30
31 int
32 simple_strncmp (const char *s1, const char *s2, size_t n)
33 {
34   int ret = 0;
35
36   while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
37          && *s1++);
38   return ret;
39 }
40
41 int
42 stupid_strncmp (const char *s1, const char *s2, size_t n)
43 {
44   size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
45   int ret = 0;
46
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;
51 }
52
53 static void
54 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
55              int exp_result)
56 {
57   size_t i, iters = INNER_LOOP_ITERS;
58   timing_t start, stop, cur;
59
60   TIMING_NOW (start);
61   for (i = 0; i < iters; ++i)
62     {
63       CALL (impl, s1, s2, n);
64     }
65   TIMING_NOW (stop);
66
67   TIMING_DIFF (cur, start, stop);
68
69   TIMING_PRINT_MEAN ((double) cur, (double) iters);
70 }
71
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)
75 {
76   size_t i, align_n;
77   char *s1, *s2;
78
79   if (n == 0)
80     {
81       s1 = (char*)(buf1 + page_size);
82       s2 = (char*)(buf2 + page_size);
83       printf ("Length %4zd/%4zd:", len, n);
84
85       FOR_EACH_IMPL (impl, 0)
86         do_one_test (impl, s1, s2, n, 0);
87
88       putchar ('\n');
89
90       return;
91     }
92
93   align1 &= 15;
94   align2 &= 15;
95   align_n = (page_size - n) & 15;
96
97   s1 = (char*)(buf1 + page_size - n);
98   s2 = (char*)(buf2 + page_size - n);
99
100   if (align1 < align_n)
101     s1 -= (align_n - align1);
102
103   if (align2 < align_n)
104     s2 -= (align_n - align2);
105
106   for (i = 0; i < n; i++)
107     s1[i] = s2[i] = 1 + 23 * i % max_char;
108
109   if (len < n)
110     {
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;
117     }
118
119   printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
120
121   FOR_EACH_IMPL (impl, 0)
122     do_one_test (impl, s1, s2, n, exp_result);
123
124   putchar ('\n');
125 }
126
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)
130 {
131   size_t i;
132   char *s1, *s2;
133
134   if (n == 0)
135     return;
136
137   align1 &= 7;
138   if (align1 + n + 1 >= page_size)
139     return;
140
141   align2 &= 7;
142   if (align2 + n + 1 >= page_size)
143     return;
144
145   s1 = (char*)(buf1 + align1);
146   s2 = (char*)(buf2 + align2);
147
148   for (i = 0; i < n; i++)
149     s1[i] = s2[i] = 1 + 23 * i % max_char;
150
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;
161
162   printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len, n, align1, align2);
163
164   FOR_EACH_IMPL (impl, 0)
165     do_one_test (impl, (char*)s1, (char*)s2, n, exp_result);
166
167   putchar ('\n');
168 }
169
170 int
171 test_main (void)
172 {
173   size_t i;
174
175   test_init ();
176
177   printf ("%23s", "");
178   FOR_EACH_IMPL (impl, 0)
179     printf ("\t%s", impl->name);
180   putchar ('\n');
181
182   for (i =0; i < 16; ++i)
183     {
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);
202     }
203
204   for (i = 1; i < 8; ++i)
205     {
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);
216     }
217
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);
223
224   for (i = 0; i < 8; ++i)
225     {
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);
234     }
235
236   return ret;
237 }
238
239 #include "../test-skeleton.c"