]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure memchr functions. |
bfff8b1b | 2 | Copyright (C) 2013-2017 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 | ||
88eefd34 SL |
19 | #ifndef WIDE |
20 | # define CHAR char | |
21 | # define SMALL_CHAR 127 | |
22 | #else | |
23 | # include <wchar.h> | |
24 | # define CHAR wchar_t | |
25 | # define SMALL_CHAR 1273 | |
26 | #endif /* WIDE */ | |
27 | ||
e029e2e5 AZ |
28 | #ifndef USE_AS_MEMRCHR |
29 | # define TEST_MAIN | |
88eefd34 SL |
30 | # ifndef WIDE |
31 | # define TEST_NAME "memchr" | |
32 | # else | |
33 | # define TEST_NAME "wmemchr" | |
34 | # endif /* WIDE */ | |
e029e2e5 | 35 | # include "bench-string.h" |
97020474 | 36 | |
88eefd34 SL |
37 | # ifndef WIDE |
38 | # define MEMCHR memchr | |
39 | # define SIMPLE_MEMCHR simple_memchr | |
40 | # else | |
41 | # define MEMCHR wmemchr | |
42 | # define SIMPLE_MEMCHR simple_wmemchr | |
43 | # endif /* WIDE */ | |
44 | ||
45 | typedef CHAR *(*proto_t) (const CHAR *, int, size_t); | |
46 | CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t); | |
97020474 | 47 | |
88eefd34 SL |
48 | IMPL (SIMPLE_MEMCHR, 0) |
49 | IMPL (MEMCHR, 1) | |
97020474 | 50 | |
88eefd34 SL |
51 | CHAR * |
52 | SIMPLE_MEMCHR (const CHAR *s, int c, size_t n) | |
97020474 SP |
53 | { |
54 | while (n--) | |
88eefd34 SL |
55 | if (*s++ == (CHAR) c) |
56 | return (CHAR *) s - 1; | |
97020474 SP |
57 | return NULL; |
58 | } | |
88eefd34 | 59 | #endif /* !USE_AS_MEMRCHR */ |
97020474 SP |
60 | |
61 | static void | |
88eefd34 | 62 | do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res) |
97020474 | 63 | { |
88eefd34 | 64 | CHAR *res = CALL (impl, s, c, n); |
44558701 WN |
65 | size_t i, iters = INNER_LOOP_ITERS; |
66 | timing_t start, stop, cur; | |
67 | ||
97020474 SP |
68 | if (res != exp_res) |
69 | { | |
70 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | |
71 | res, exp_res); | |
72 | ret = 1; | |
73 | return; | |
74 | } | |
75 | ||
44558701 WN |
76 | TIMING_NOW (start); |
77 | for (i = 0; i < iters; ++i) | |
97020474 | 78 | { |
44558701 | 79 | CALL (impl, s, c, n); |
97020474 | 80 | } |
44558701 WN |
81 | TIMING_NOW (stop); |
82 | ||
83 | TIMING_DIFF (cur, start, stop); | |
84 | ||
85 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | |
97020474 SP |
86 | } |
87 | ||
88 | static void | |
89 | do_test (size_t align, size_t pos, size_t len, int seek_char) | |
90 | { | |
91 | size_t i; | |
88eefd34 | 92 | CHAR *result; |
97020474 SP |
93 | |
94 | align &= 7; | |
88eefd34 | 95 | if ((align + len) * sizeof (CHAR) >= page_size) |
97020474 SP |
96 | return; |
97 | ||
88eefd34 SL |
98 | CHAR *buf = (CHAR *) (buf1); |
99 | ||
97020474 SP |
100 | for (i = 0; i < len; ++i) |
101 | { | |
88eefd34 SL |
102 | buf[align + i] = 1 + 23 * i % SMALL_CHAR; |
103 | if (buf[align + i] == seek_char) | |
104 | buf[align + i] = seek_char + 1; | |
97020474 | 105 | } |
88eefd34 | 106 | buf[align + len] = 0; |
97020474 SP |
107 | |
108 | if (pos < len) | |
109 | { | |
88eefd34 SL |
110 | buf[align + pos] = seek_char; |
111 | buf[align + len] = -seek_char; | |
112 | result = (CHAR *) (buf + align + pos); | |
97020474 SP |
113 | } |
114 | else | |
115 | { | |
116 | result = NULL; | |
88eefd34 | 117 | buf[align + len] = seek_char; |
97020474 SP |
118 | } |
119 | ||
6b69f98d L |
120 | printf ("Length %4zd, position %4zd, alignment %2zd:", |
121 | len, pos, align); | |
97020474 SP |
122 | |
123 | FOR_EACH_IMPL (impl, 0) | |
88eefd34 | 124 | do_one_test (impl, (CHAR *) (buf + align), seek_char, len, result); |
97020474 | 125 | |
44558701 | 126 | putchar ('\n'); |
97020474 SP |
127 | } |
128 | ||
129 | int | |
130 | test_main (void) | |
131 | { | |
132 | size_t i; | |
133 | ||
134 | test_init (); | |
135 | ||
136 | printf ("%20s", ""); | |
137 | FOR_EACH_IMPL (impl, 0) | |
138 | printf ("\t%s", impl->name); | |
139 | putchar ('\n'); | |
140 | ||
141 | for (i = 1; i < 8; ++i) | |
142 | { | |
143 | do_test (0, 16 << i, 2048, 23); | |
144 | do_test (i, 64, 256, 23); | |
145 | do_test (0, 16 << i, 2048, 0); | |
146 | do_test (i, 64, 256, 0); | |
6b69f98d L |
147 | #ifdef USE_AS_MEMRCHR |
148 | /* Also test the position close to the beginning for memrchr. */ | |
149 | do_test (0, i, 256, 23); | |
150 | do_test (0, i, 256, 0); | |
151 | do_test (i, i, 256, 23); | |
152 | do_test (i, i, 256, 0); | |
153 | #endif | |
97020474 SP |
154 | } |
155 | for (i = 1; i < 32; ++i) | |
156 | { | |
157 | do_test (0, i, i + 1, 23); | |
158 | do_test (0, i, i + 1, 0); | |
6b69f98d L |
159 | do_test (i, i, i + 1, 23); |
160 | do_test (i, i, i + 1, 0); | |
161 | #ifdef USE_AS_MEMRCHR | |
162 | /* Also test the position close to the beginning for memrchr. */ | |
163 | do_test (0, 1, i + 1, 23); | |
164 | do_test (0, 2, i + 1, 0); | |
165 | #endif | |
97020474 SP |
166 | } |
167 | ||
168 | return ret; | |
169 | } | |
170 | ||
b598e134 | 171 | #include <support/test-driver.c> |