lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Measure strcpy functions. |
| 2 | Copyright (C) 2013-2015 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 | #ifdef WIDE |
| 20 | # include <wchar.h> |
| 21 | # define CHAR wchar_t |
| 22 | # define UCHAR wchar_t |
| 23 | # define sfmt "ls" |
| 24 | # define BIG_CHAR WCHAR_MAX |
| 25 | # define SMALL_CHAR 1273 |
| 26 | # define STRCMP wcscmp |
| 27 | # define MEMCMP wmemcmp |
| 28 | # define MEMSET wmemset |
| 29 | #else |
| 30 | # define CHAR char |
| 31 | # define UCHAR unsigned char |
| 32 | # define sfmt "s" |
| 33 | # define BIG_CHAR CHAR_MAX |
| 34 | # define SMALL_CHAR 127 |
| 35 | # define STRCMP strcmp |
| 36 | # define MEMCMP memcmp |
| 37 | # define MEMSET memset |
| 38 | #endif |
| 39 | |
| 40 | #ifndef STRCPY_RESULT |
| 41 | # define STRCPY_RESULT(dst, len) dst |
| 42 | # define TEST_MAIN |
| 43 | # ifndef WIDE |
| 44 | # define TEST_NAME "strcpy" |
| 45 | # else |
| 46 | # define TEST_NAME "wcscpy" |
| 47 | # endif |
| 48 | # include "bench-string.h" |
| 49 | # ifndef WIDE |
| 50 | # define SIMPLE_STRCPY simple_strcpy |
| 51 | # define STRCPY strcpy |
| 52 | # else |
| 53 | # define SIMPLE_STRCPY simple_wcscpy |
| 54 | # define STRCPY wcscpy |
| 55 | # endif |
| 56 | |
| 57 | CHAR *SIMPLE_STRCPY (CHAR *, const CHAR *); |
| 58 | |
| 59 | IMPL (SIMPLE_STRCPY, 0) |
| 60 | IMPL (STRCPY, 1) |
| 61 | |
| 62 | CHAR * |
| 63 | SIMPLE_STRCPY (CHAR *dst, const CHAR *src) |
| 64 | { |
| 65 | CHAR *ret = dst; |
| 66 | while ((*dst++ = *src++) != '\0'); |
| 67 | return ret; |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | typedef CHAR *(*proto_t) (CHAR *, const CHAR *); |
| 72 | |
| 73 | static void |
| 74 | do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, |
| 75 | size_t len __attribute__((unused))) |
| 76 | { |
| 77 | size_t i, iters = INNER_LOOP_ITERS; |
| 78 | timing_t start, stop, cur; |
| 79 | |
| 80 | if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len)) |
| 81 | { |
| 82 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 83 | CALL (impl, dst, src), STRCPY_RESULT (dst, len)); |
| 84 | ret = 1; |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (STRCMP (dst, src) != 0) |
| 89 | { |
| 90 | error (0, 0, |
| 91 | "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"", |
| 92 | impl->name, dst, src); |
| 93 | ret = 1; |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | TIMING_NOW (start); |
| 98 | for (i = 0; i < iters; ++i) |
| 99 | { |
| 100 | CALL (impl, dst, src); |
| 101 | } |
| 102 | TIMING_NOW (stop); |
| 103 | |
| 104 | TIMING_DIFF (cur, start, stop); |
| 105 | |
| 106 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | do_test (size_t align1, size_t align2, size_t len, int max_char) |
| 111 | { |
| 112 | size_t i; |
| 113 | CHAR *s1, *s2; |
| 114 | /* For wcscpy: align1 and align2 here mean alignment not in bytes, |
| 115 | but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)) |
| 116 | len for wcschr here isn't in bytes but it's number of wchar_t symbols. */ |
| 117 | align1 &= 7; |
| 118 | if ((align1 + len) * sizeof(CHAR) >= page_size) |
| 119 | return; |
| 120 | |
| 121 | align2 &= 7; |
| 122 | if ((align2 + len) * sizeof(CHAR) >= page_size) |
| 123 | return; |
| 124 | |
| 125 | s1 = (CHAR *) (buf1) + align1; |
| 126 | s2 = (CHAR *) (buf2) + align2; |
| 127 | |
| 128 | for (i = 0; i < len; i++) |
| 129 | s1[i] = 32 + 23 * i % (max_char - 32); |
| 130 | s1[len] = 0; |
| 131 | |
| 132 | printf ("Length %4zd, alignments in bytes %2zd/%2zd:", len, align1 * sizeof(CHAR), align2 * sizeof(CHAR)); |
| 133 | |
| 134 | FOR_EACH_IMPL (impl, 0) |
| 135 | do_one_test (impl, s2, s1, len); |
| 136 | |
| 137 | putchar ('\n'); |
| 138 | } |
| 139 | |
| 140 | int |
| 141 | test_main (void) |
| 142 | { |
| 143 | size_t i; |
| 144 | |
| 145 | test_init (); |
| 146 | |
| 147 | printf ("%23s", ""); |
| 148 | FOR_EACH_IMPL (impl, 0) |
| 149 | printf ("\t%s", impl->name); |
| 150 | putchar ('\n'); |
| 151 | |
| 152 | for (i = 0; i < 16; ++i) |
| 153 | { |
| 154 | do_test (0, 0, i, SMALL_CHAR); |
| 155 | do_test (0, 0, i, BIG_CHAR); |
| 156 | do_test (0, i, i, SMALL_CHAR); |
| 157 | do_test (i, 0, i, BIG_CHAR); |
| 158 | } |
| 159 | |
| 160 | for (i = 1; i < 8; ++i) |
| 161 | { |
| 162 | do_test (0, 0, 8 << i, SMALL_CHAR); |
| 163 | do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR); |
| 164 | } |
| 165 | |
| 166 | for (i = 1; i < 8; ++i) |
| 167 | { |
| 168 | do_test (i, 2 * i, 8 << i, SMALL_CHAR); |
| 169 | do_test (2 * i, i, 8 << i, BIG_CHAR); |
| 170 | do_test (i, i, 8 << i, SMALL_CHAR); |
| 171 | do_test (i, i, 8 << i, BIG_CHAR); |
| 172 | } |
| 173 | |
| 174 | for (i = 16; i <= 512; i+=4) |
| 175 | { |
| 176 | do_test (0, 4, i, SMALL_CHAR); |
| 177 | do_test (4, 0, i, BIG_CHAR); |
| 178 | do_test (4, 4, i, SMALL_CHAR); |
| 179 | do_test (2, 2, i, BIG_CHAR); |
| 180 | do_test (2, 6, i, SMALL_CHAR); |
| 181 | do_test (6, 2, i, BIG_CHAR); |
| 182 | do_test (1, 7, i, SMALL_CHAR); |
| 183 | do_test (7, 1, i, BIG_CHAR); |
| 184 | do_test (3, 4, i, SMALL_CHAR); |
| 185 | do_test (4, 3, i, BIG_CHAR); |
| 186 | do_test (5, 7, i, SMALL_CHAR); |
| 187 | do_test (7, 5, i, SMALL_CHAR); |
| 188 | } |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | #include "../test-skeleton.c" |