1 | /* Measure STRCHR functions. |
2 | Copyright (C) 2013-2022 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #define TEST_MAIN |
20 | #ifdef WIDE |
21 | # define TEST_NAME "wcsrchr" |
22 | #else |
23 | # define TEST_NAME "strrchr" |
24 | #endif |
25 | #include "bench-string.h" |
26 | |
27 | #define BIG_CHAR MAX_CHAR |
28 | |
29 | #ifdef WIDE |
30 | # define SIMPLE_STRRCHR simple_wcsrchr |
31 | # define SMALL_CHAR 1273 |
32 | #else |
33 | # define SIMPLE_STRRCHR simple_strrchr |
34 | # define SMALL_CHAR 127 |
35 | #endif |
36 | |
37 | typedef CHAR *(*proto_t) (const CHAR *, int); |
38 | CHAR *SIMPLE_STRRCHR (const CHAR *, int); |
39 | |
40 | IMPL (SIMPLE_STRRCHR, 0) |
41 | IMPL (STRRCHR, 1) |
42 | |
43 | CHAR * |
44 | SIMPLE_STRRCHR (const CHAR *s, int c) |
45 | { |
46 | const CHAR *ret = NULL; |
47 | |
48 | for (; *s != '\0'; ++s) |
49 | if (*s == (CHAR) c) |
50 | ret = s; |
51 | |
52 | return (CHAR *) (c == '\0' ? s : ret); |
53 | } |
54 | |
55 | static void |
56 | do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res) |
57 | { |
58 | CHAR *res = CALL (impl, s, c); |
59 | size_t i, iters = INNER_LOOP_ITERS8; |
60 | timing_t start, stop, cur; |
61 | |
62 | if (res != exp_res) |
63 | { |
64 | error (status: 0, errnum: 0, format: "Wrong result in function %s %p %p" , impl->name, |
65 | res, exp_res); |
66 | ret = 1; |
67 | return; |
68 | } |
69 | |
70 | TIMING_NOW (start); |
71 | for (i = 0; i < iters; ++i) |
72 | { |
73 | CALL (impl, s, c); |
74 | } |
75 | TIMING_NOW (stop); |
76 | |
77 | TIMING_DIFF (cur, start, stop); |
78 | |
79 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
80 | } |
81 | |
82 | static void |
83 | do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char) |
84 | /* For wcsrchr: align here means align not in bytes, |
85 | but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)) |
86 | len for wcschr here isn't in bytes but it's number of wchar_t symbols. */ |
87 | { |
88 | size_t i; |
89 | CHAR *result; |
90 | CHAR *buf = (CHAR *) buf1; |
91 | |
92 | align &= 7; |
93 | if ((align + len) * sizeof (CHAR) >= page_size) |
94 | return; |
95 | |
96 | for (i = 0; i < len; ++i) |
97 | { |
98 | buf[align + i] = (random () * random ()) & max_char; |
99 | if (!buf[align + i]) |
100 | buf[align + i] = (random () * random ()) & max_char; |
101 | if (!buf[align + i]) |
102 | buf[align + i] = 1; |
103 | if ((i > pos || pos >= len) && buf[align + i] == seek_char) |
104 | buf[align + i] = seek_char + 10 + (random () & 15); |
105 | } |
106 | buf[align + len] = 0; |
107 | |
108 | if (pos < len) |
109 | { |
110 | buf[align + pos] = seek_char; |
111 | result = (CHAR *) (buf + align + pos); |
112 | } |
113 | else if (seek_char == 0) |
114 | result = (CHAR *) (buf + align + len); |
115 | else |
116 | result = NULL; |
117 | |
118 | printf (format: "Length %4zd, alignment in bytes %2zd:" , len, align * sizeof (CHAR)); |
119 | |
120 | FOR_EACH_IMPL (impl, 0) |
121 | do_one_test (impl, s: (CHAR *) (buf + align), c: seek_char, exp_res: result); |
122 | |
123 | putchar (c: '\n'); |
124 | } |
125 | |
126 | int |
127 | test_main (void) |
128 | { |
129 | size_t i; |
130 | |
131 | test_init (); |
132 | |
133 | printf (format: "%20s" , "" ); |
134 | FOR_EACH_IMPL (impl, 0) |
135 | printf (format: "\t%s" , impl->name); |
136 | putchar (c: '\n'); |
137 | |
138 | for (i = 1; i < 8; ++i) |
139 | { |
140 | do_test (align: 0, pos: 16 << i, len: 2048, seek_char: 23, SMALL_CHAR); |
141 | do_test (align: i, pos: 16 << i, len: 2048, seek_char: 23, SMALL_CHAR); |
142 | } |
143 | |
144 | for (i = 1; i < 8; ++i) |
145 | { |
146 | do_test (align: i, pos: 64, len: 256, seek_char: 23, SMALL_CHAR); |
147 | do_test (align: i, pos: 64, len: 256, seek_char: 23, BIG_CHAR); |
148 | } |
149 | |
150 | for (i = 0; i < 32; ++i) |
151 | { |
152 | do_test (align: 0, pos: i, len: i + 1, seek_char: 23, SMALL_CHAR); |
153 | do_test (align: 0, pos: i, len: i + 1, seek_char: 23, BIG_CHAR); |
154 | } |
155 | |
156 | for (i = 1; i < 8; ++i) |
157 | { |
158 | do_test (align: 0, pos: 16 << i, len: 2048, seek_char: 0, SMALL_CHAR); |
159 | do_test (align: i, pos: 16 << i, len: 2048, seek_char: 0, SMALL_CHAR); |
160 | } |
161 | |
162 | for (i = 1; i < 8; ++i) |
163 | { |
164 | do_test (align: i, pos: 64, len: 256, seek_char: 0, SMALL_CHAR); |
165 | do_test (align: i, pos: 64, len: 256, seek_char: 0, BIG_CHAR); |
166 | } |
167 | |
168 | for (i = 0; i < 32; ++i) |
169 | { |
170 | do_test (align: 0, pos: i, len: i + 1, seek_char: 0, SMALL_CHAR); |
171 | do_test (align: 0, pos: i, len: i + 1, seek_char: 0, BIG_CHAR); |
172 | } |
173 | |
174 | return ret; |
175 | } |
176 | |
177 | #include <support/test-driver.c> |
178 | |