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