]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure STRCHR functions. |
b168057a | 2 | Copyright (C) 2013-2015 Free Software Foundation, Inc. |
97020474 SP |
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 | #ifndef WIDE | |
21 | # ifdef USE_FOR_STRCHRNUL | |
22 | # define TEST_NAME "strchrnul" | |
23 | # else | |
24 | # define TEST_NAME "strchr" | |
25 | # endif | |
26 | #else | |
27 | # define TEST_NAME "wcschr" | |
28 | #endif | |
29 | #include "bench-string.h" | |
30 | ||
31 | #ifndef WIDE | |
32 | # ifdef USE_FOR_STRCHRNUL | |
33 | # define STRCHR strchrnul | |
34 | # define stupid_STRCHR stupid_STRCHRNUL | |
35 | # define simple_STRCHR simple_STRCHRNUL | |
36 | # else | |
37 | # define STRCHR strchr | |
38 | # endif | |
39 | # define STRLEN strlen | |
40 | # define CHAR char | |
41 | # define BIG_CHAR CHAR_MAX | |
42 | # define MIDDLE_CHAR 127 | |
43 | # define SMALL_CHAR 23 | |
44 | # define UCHAR unsigned char | |
45 | #else | |
46 | # include <wchar.h> | |
47 | # define STRCHR wcschr | |
48 | # define STRLEN wcslen | |
49 | # define CHAR wchar_t | |
50 | # define BIG_CHAR WCHAR_MAX | |
51 | # define MIDDLE_CHAR 1121 | |
52 | # define SMALL_CHAR 851 | |
53 | # define UCHAR wchar_t | |
54 | #endif | |
55 | ||
56 | #ifdef USE_FOR_STRCHRNUL | |
57 | # define NULLRET(endptr) endptr | |
58 | #else | |
59 | # define NULLRET(endptr) NULL | |
60 | #endif | |
61 | ||
62 | ||
63 | typedef CHAR *(*proto_t) (const CHAR *, int); | |
64 | ||
65 | CHAR * | |
66 | simple_STRCHR (const CHAR *s, int c) | |
67 | { | |
68 | for (; *s != (CHAR) c; ++s) | |
69 | if (*s == '\0') | |
70 | return NULLRET ((CHAR *) s); | |
71 | return (CHAR *) s; | |
72 | } | |
73 | ||
74 | CHAR * | |
75 | stupid_STRCHR (const CHAR *s, int c) | |
76 | { | |
77 | size_t n = STRLEN (s) + 1; | |
78 | ||
79 | while (n--) | |
80 | if (*s++ == (CHAR) c) | |
81 | return (CHAR *) s - 1; | |
82 | return NULLRET ((CHAR *) s - 1); | |
83 | } | |
84 | ||
85 | IMPL (stupid_STRCHR, 0) | |
86 | IMPL (simple_STRCHR, 0) | |
87 | IMPL (STRCHR, 1) | |
88 | ||
89 | static void | |
90 | do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res) | |
91 | { | |
44558701 WN |
92 | size_t i, iters = INNER_LOOP_ITERS; |
93 | timing_t start, stop, cur; | |
94 | ||
95 | TIMING_NOW (start); | |
96 | for (i = 0; i < iters; ++i) | |
97020474 | 97 | { |
44558701 | 98 | CALL (impl, s, c); |
97020474 | 99 | } |
44558701 WN |
100 | TIMING_NOW (stop); |
101 | ||
102 | TIMING_DIFF (cur, start, stop); | |
103 | ||
104 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | |
97020474 SP |
105 | } |
106 | ||
107 | static void | |
108 | do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char) | |
109 | /* For wcschr: align here means align not in bytes, | |
110 | but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)) | |
111 | len for wcschr here isn't in bytes but it's number of wchar_t symbols. */ | |
112 | { | |
113 | size_t i; | |
114 | CHAR *result; | |
115 | CHAR *buf = (CHAR *) buf1; | |
116 | align &= 15; | |
117 | if ((align + len) * sizeof (CHAR) >= page_size) | |
118 | return; | |
119 | ||
120 | for (i = 0; i < len; ++i) | |
121 | { | |
122 | buf[align + i] = 32 + 23 * i % max_char; | |
123 | if (buf[align + i] == seek_char) | |
124 | buf[align + i] = seek_char + 1; | |
125 | else if (buf[align + i] == 0) | |
126 | buf[align + i] = 1; | |
127 | } | |
128 | buf[align + len] = 0; | |
129 | ||
130 | if (pos < len) | |
131 | { | |
132 | buf[align + pos] = seek_char; | |
133 | result = buf + align + pos; | |
134 | } | |
135 | else if (seek_char == 0) | |
136 | result = buf + align + len; | |
137 | else | |
138 | result = NULLRET (buf + align + len); | |
139 | ||
44558701 WN |
140 | printf ("Length %4zd, alignment in bytes %2zd:", |
141 | pos, align * sizeof (CHAR)); | |
97020474 SP |
142 | |
143 | FOR_EACH_IMPL (impl, 0) | |
144 | do_one_test (impl, buf + align, seek_char, result); | |
145 | ||
44558701 | 146 | putchar ('\n'); |
97020474 SP |
147 | } |
148 | ||
149 | int | |
150 | test_main (void) | |
151 | { | |
152 | size_t i; | |
153 | ||
154 | test_init (); | |
155 | ||
156 | printf ("%20s", ""); | |
157 | FOR_EACH_IMPL (impl, 0) | |
158 | printf ("\t%s", impl->name); | |
159 | putchar ('\n'); | |
160 | ||
161 | for (i = 1; i < 8; ++i) | |
162 | { | |
163 | do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR); | |
164 | do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR); | |
165 | } | |
166 | ||
167 | for (i = 1; i < 8; ++i) | |
168 | { | |
169 | do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR); | |
170 | do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR); | |
171 | } | |
172 | ||
173 | for (i = 0; i < 32; ++i) | |
174 | { | |
175 | do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR); | |
176 | do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR); | |
177 | } | |
178 | ||
179 | for (i = 1; i < 8; ++i) | |
180 | { | |
181 | do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR); | |
182 | do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR); | |
183 | } | |
184 | ||
185 | for (i = 1; i < 8; ++i) | |
186 | { | |
187 | do_test (i, 64, 256, 0, MIDDLE_CHAR); | |
188 | do_test (i, 64, 256, 0, BIG_CHAR); | |
189 | } | |
190 | ||
191 | for (i = 0; i < 32; ++i) | |
192 | { | |
193 | do_test (0, i, i + 1, 0, MIDDLE_CHAR); | |
194 | do_test (0, i, i + 1, 0, BIG_CHAR); | |
195 | } | |
196 | ||
197 | return ret; | |
198 | } | |
199 | ||
200 | #include "../test-skeleton.c" |