hppa: Sync with pthread.h.
[platform/upstream/glibc.git] / benchtests / bench-strspn.c
1 /* Measure strspn functions.
2    Copyright (C) 2013-2015 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 "strspn"
21 #include "bench-string.h"
22
23 typedef size_t (*proto_t) (const char *, const char *);
24 size_t simple_strspn (const char *, const char *);
25 size_t stupid_strspn (const char *, const char *);
26
27 IMPL (stupid_strspn, 0)
28 IMPL (simple_strspn, 0)
29 IMPL (strspn, 1)
30
31 size_t
32 simple_strspn (const char *s, const char *acc)
33 {
34   const char *r, *str = s;
35   char c;
36
37   while ((c = *s++) != '\0')
38     {
39       for (r = acc; *r != '\0'; ++r)
40         if (*r == c)
41           break;
42       if (*r == '\0')
43         return s - str - 1;
44     }
45   return s - str - 1;
46 }
47
48 size_t
49 stupid_strspn (const char *s, const char *acc)
50 {
51   size_t ns = strlen (s), nacc = strlen (acc);
52   size_t i, j;
53
54   for (i = 0; i < ns; ++i)
55     {
56       for (j = 0; j < nacc; ++j)
57         if (s[i] == acc[j])
58           break;
59       if (j == nacc)
60         return i;
61     }
62   return i;
63 }
64
65 static void
66 do_one_test (impl_t *impl, const char *s, const char *acc, size_t exp_res)
67 {
68   size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS;
69   timing_t start, stop, cur;
70
71   if (res != exp_res)
72     {
73       error (0, 0, "Wrong result in function %s %p %p", impl->name,
74              (void *) res, (void *) exp_res);
75       ret = 1;
76       return;
77     }
78
79   TIMING_NOW (start);
80   for (i = 0; i < iters; ++i)
81     {
82       CALL (impl, s, acc);
83     }
84   TIMING_NOW (stop);
85
86   TIMING_DIFF (cur, start, stop);
87
88   TIMING_PRINT_MEAN ((double) cur, (double) iters);
89 }
90
91 static void
92 do_test (size_t align, size_t pos, size_t len)
93 {
94   size_t i;
95   char *acc, *s;
96
97   align &= 7;
98   if (align + pos + 10 >= page_size || len > 240 || ! len)
99     return;
100
101   acc = (char *) (buf2 + (random () & 255));
102   s = (char *) (buf1 + align);
103
104   for (i = 0; i < len; ++i)
105     {
106       acc[i] = random () & 255;
107       if (!acc[i])
108         acc[i] = random () & 255;
109       if (!acc[i])
110         acc[i] = 1 + (random () & 127);
111     }
112   acc[len] = '\0';
113
114   for (i = 0; i < pos; ++i)
115     s[i] = acc[random () % len];
116   s[pos] = random () & 255;
117   if (strchr (acc, s[pos]))
118     s[pos] = '\0';
119   else
120     {
121       for (i = pos + 1; i < pos + 10; ++i)
122         s[i] = random () & 255;
123       s[i] = '\0';
124     }
125
126   printf ("Length %4zd, alignment %2zd, acc len %2zd:", pos, align, len);
127
128   FOR_EACH_IMPL (impl, 0)
129     do_one_test (impl, s, acc, pos);
130
131   putchar ('\n');
132 }
133
134 int
135 test_main (void)
136 {
137   size_t i;
138
139   test_init ();
140
141   printf ("%32s", "");
142   FOR_EACH_IMPL (impl, 0)
143     printf ("\t%s", impl->name);
144   putchar ('\n');
145
146   for (i = 0; i < 32; ++i)
147     {
148       do_test (0, 512, i);
149       do_test (i, 512, i);
150     }
151
152   for (i = 1; i < 8; ++i)
153     {
154       do_test (0, 16 << i, 4);
155       do_test (i, 16 << i, 4);
156     }
157
158   for (i = 1; i < 8; ++i)
159     do_test (i, 64, 10);
160
161   for (i = 0; i < 64; ++i)
162     do_test (0, i, 6);
163
164   return ret;
165 }
166
167 #include "../test-skeleton.c"