xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Measure strpbrk functions. |
| 2 | Copyright (C) 2013-2016 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 | #ifndef WIDE |
| 20 | # define CHAR char |
| 21 | # define STRLEN strlen |
| 22 | # define STRCHR strchr |
| 23 | # define BIG_CHAR CHAR_MAX |
| 24 | # define SMALL_CHAR 127 |
| 25 | #else |
| 26 | # include <wchar.h> |
| 27 | # define CHAR wchar_t |
| 28 | # define STRLEN wcslen |
| 29 | # define STRCHR wcschr |
| 30 | # define BIG_CHAR WCHAR_MAX |
| 31 | # define SMALL_CHAR 1273 |
| 32 | #endif /* WIDE */ |
| 33 | |
| 34 | #ifndef STRPBRK_RESULT |
| 35 | # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL) |
| 36 | # define RES_TYPE CHAR * |
| 37 | # define TEST_MAIN |
| 38 | # ifndef WIDE |
| 39 | # define TEST_NAME "strpbrk" |
| 40 | # else |
| 41 | # define TEST_NAME "wcspbrk" |
| 42 | # endif /* WIDE */ |
| 43 | # include "bench-string.h" |
| 44 | |
| 45 | # ifndef WIDE |
| 46 | # define STRPBRK strpbrk |
| 47 | # define SIMPLE_STRPBRK simple_strpbrk |
| 48 | # define STUPID_STRPBRK stupid_strpbrk |
| 49 | # else |
| 50 | # include <wchar.h> |
| 51 | # define STRPBRK wcspbrk |
| 52 | # define SIMPLE_STRPBRK simple_wcspbrk |
| 53 | # define STUPID_STRPBRK stupid_wcspbrk |
| 54 | # endif /* WIDE */ |
| 55 | |
| 56 | typedef CHAR *(*proto_t) (const CHAR *, const CHAR *); |
| 57 | CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *); |
| 58 | CHAR *STUPID_STRPBRK (const CHAR *, const CHAR *); |
| 59 | |
| 60 | IMPL (STUPID_STRPBRK, 0) |
| 61 | IMPL (SIMPLE_STRPBRK, 0) |
| 62 | IMPL (STRPBRK, 1) |
| 63 | |
| 64 | CHAR * |
| 65 | SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej) |
| 66 | { |
| 67 | const CHAR *r; |
| 68 | CHAR c; |
| 69 | |
| 70 | while ((c = *s++) != '\0') |
| 71 | for (r = rej; *r != '\0'; ++r) |
| 72 | if (*r == c) |
| 73 | return (CHAR *) s - 1; |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | CHAR * |
| 78 | STUPID_STRPBRK (const CHAR *s, const CHAR *rej) |
| 79 | { |
| 80 | size_t ns = STRLEN (s), nrej = STRLEN (rej); |
| 81 | size_t i, j; |
| 82 | |
| 83 | for (i = 0; i < ns; ++i) |
| 84 | for (j = 0; j < nrej; ++j) |
| 85 | if (s[i] == rej[j]) |
| 86 | return (CHAR *) s + i; |
| 87 | return NULL; |
| 88 | } |
| 89 | #endif /* !STRPBRK_RESULT */ |
| 90 | |
| 91 | static void |
| 92 | do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res) |
| 93 | { |
| 94 | RES_TYPE res = CALL (impl, s, rej); |
| 95 | size_t i, iters = INNER_LOOP_ITERS; |
| 96 | timing_t start, stop, cur; |
| 97 | |
| 98 | if (res != exp_res) |
| 99 | { |
| 100 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 101 | (void *) res, (void *) exp_res); |
| 102 | ret = 1; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | TIMING_NOW (start); |
| 107 | for (i = 0; i < iters; ++i) |
| 108 | { |
| 109 | CALL (impl, s, rej); |
| 110 | } |
| 111 | TIMING_NOW (stop); |
| 112 | |
| 113 | TIMING_DIFF (cur, start, stop); |
| 114 | |
| 115 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | do_test (size_t align, size_t pos, size_t len) |
| 120 | { |
| 121 | size_t i; |
| 122 | int c; |
| 123 | RES_TYPE result; |
| 124 | CHAR *rej, *s; |
| 125 | |
| 126 | align &= 7; |
| 127 | if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240) |
| 128 | return; |
| 129 | |
| 130 | rej = (CHAR *) (buf2) + (random () & 255); |
| 131 | s = (CHAR *) (buf1) + align; |
| 132 | |
| 133 | for (i = 0; i < len; ++i) |
| 134 | { |
| 135 | rej[i] = random () & BIG_CHAR; |
| 136 | if (!rej[i]) |
| 137 | rej[i] = random () & BIG_CHAR; |
| 138 | if (!rej[i]) |
| 139 | rej[i] = 1 + (random () & SMALL_CHAR); |
| 140 | } |
| 141 | rej[len] = '\0'; |
| 142 | for (c = 1; c <= BIG_CHAR; ++c) |
| 143 | if (STRCHR (rej, c) == NULL) |
| 144 | break; |
| 145 | |
| 146 | for (i = 0; i < pos; ++i) |
| 147 | { |
| 148 | s[i] = random () & BIG_CHAR; |
| 149 | if (STRCHR (rej, s[i])) |
| 150 | { |
| 151 | s[i] = random () & BIG_CHAR; |
| 152 | if (STRCHR (rej, s[i])) |
| 153 | s[i] = c; |
| 154 | } |
| 155 | } |
| 156 | s[pos] = rej[random () % (len + 1)]; |
| 157 | if (s[pos]) |
| 158 | { |
| 159 | for (i = pos + 1; i < pos + 10; ++i) |
| 160 | s[i] = random () & BIG_CHAR; |
| 161 | s[i] = '\0'; |
| 162 | } |
| 163 | result = STRPBRK_RESULT (s, pos); |
| 164 | |
| 165 | printf ("Length %4zd, alignment %2zd, rej len %2zd:", pos, align, len); |
| 166 | |
| 167 | FOR_EACH_IMPL (impl, 0) |
| 168 | do_one_test (impl, s, rej, result); |
| 169 | |
| 170 | putchar ('\n'); |
| 171 | } |
| 172 | |
| 173 | int |
| 174 | test_main (void) |
| 175 | { |
| 176 | size_t i; |
| 177 | |
| 178 | test_init (); |
| 179 | |
| 180 | printf ("%32s", ""); |
| 181 | FOR_EACH_IMPL (impl, 0) |
| 182 | printf ("\t%s", impl->name); |
| 183 | putchar ('\n'); |
| 184 | |
| 185 | for (i = 0; i < 32; ++i) |
| 186 | { |
| 187 | do_test (0, 512, i); |
| 188 | do_test (i, 512, i); |
| 189 | } |
| 190 | |
| 191 | for (i = 1; i < 8; ++i) |
| 192 | { |
| 193 | do_test (0, 16 << i, 4); |
| 194 | do_test (i, 16 << i, 4); |
| 195 | } |
| 196 | |
| 197 | for (i = 1; i < 8; ++i) |
| 198 | do_test (i, 64, 10); |
| 199 | |
| 200 | for (i = 0; i < 64; ++i) |
| 201 | do_test (0, i, 6); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | #include "../test-skeleton.c" |