| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Measure strcat 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 | #ifndef WIDE | 
|  | 21 | # define TEST_NAME "strcat" | 
|  | 22 | #else | 
|  | 23 | # define TEST_NAME "wcscat" | 
|  | 24 | #endif /* WIDE */ | 
|  | 25 | #include "bench-string.h" | 
|  | 26 |  | 
|  | 27 | #ifndef WIDE | 
|  | 28 | # define STRCAT strcat | 
|  | 29 | # define CHAR char | 
|  | 30 | # define sfmt "s" | 
|  | 31 | # define SIMPLE_STRCAT simple_strcat | 
|  | 32 | # define STRLEN strlen | 
|  | 33 | # define STRCMP strcmp | 
|  | 34 | # define BIG_CHAR CHAR_MAX | 
|  | 35 | # define SMALL_CHAR 127 | 
|  | 36 | #else | 
|  | 37 | # include <wchar.h> | 
|  | 38 | # define STRCAT wcscat | 
|  | 39 | # define CHAR wchar_t | 
|  | 40 | # define sfmt "ls" | 
|  | 41 | # define SIMPLE_STRCAT simple_wcscat | 
|  | 42 | # define STRLEN wcslen | 
|  | 43 | # define STRCMP wcscmp | 
|  | 44 | # define BIG_CHAR WCHAR_MAX | 
|  | 45 | # define SMALL_CHAR 1273 | 
|  | 46 | #endif /* WIDE */ | 
|  | 47 |  | 
|  | 48 |  | 
|  | 49 | typedef CHAR *(*proto_t) (CHAR *, const CHAR *); | 
|  | 50 | CHAR *SIMPLE_STRCAT (CHAR *, const CHAR *); | 
|  | 51 |  | 
|  | 52 | IMPL (SIMPLE_STRCAT, 0) | 
|  | 53 | IMPL (STRCAT, 1) | 
|  | 54 |  | 
|  | 55 | CHAR * | 
|  | 56 | SIMPLE_STRCAT (CHAR *dst, const CHAR *src) | 
|  | 57 | { | 
|  | 58 | CHAR *ret = dst; | 
|  | 59 | while (*dst++ != '\0'); | 
|  | 60 | --dst; | 
|  | 61 | while ((*dst++ = *src++) != '\0'); | 
|  | 62 | return ret; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | static void | 
|  | 66 | do_one_test (impl_t *impl, CHAR *dst, const CHAR *src) | 
|  | 67 | { | 
|  | 68 | size_t k = STRLEN (dst), i, iters = INNER_LOOP_ITERS; | 
|  | 69 | timing_t start, stop, cur; | 
|  | 70 |  | 
|  | 71 | if (CALL (impl, dst, src) != dst) | 
|  | 72 | { | 
|  | 73 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | 
|  | 74 | CALL (impl, dst, src), dst); | 
|  | 75 | ret = 1; | 
|  | 76 | return; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | if (STRCMP (dst + k, src) != 0) | 
|  | 80 | { | 
|  | 81 | error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"", | 
|  | 82 | impl->name, dst, src); | 
|  | 83 | ret = 1; | 
|  | 84 | return; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | TIMING_NOW (start); | 
|  | 88 | for (i = 0; i < iters; ++i) | 
|  | 89 | { | 
|  | 90 | dst[k] = '\0'; | 
|  | 91 | CALL (impl, dst, src); | 
|  | 92 | } | 
|  | 93 | TIMING_NOW (stop); | 
|  | 94 |  | 
|  | 95 | TIMING_DIFF (cur, start, stop); | 
|  | 96 |  | 
|  | 97 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static void | 
|  | 101 | do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char) | 
|  | 102 | { | 
|  | 103 | size_t i; | 
|  | 104 | CHAR *s1, *s2; | 
|  | 105 |  | 
|  | 106 | align1 &= 7; | 
|  | 107 | if ((align1 + len1) * sizeof (CHAR) >= page_size) | 
|  | 108 | return; | 
|  | 109 |  | 
|  | 110 | align2 &= 7; | 
|  | 111 | if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size) | 
|  | 112 | return; | 
|  | 113 |  | 
|  | 114 | s1 = (CHAR *) (buf1) + align1; | 
|  | 115 | s2 = (CHAR *) (buf2) + align2; | 
|  | 116 |  | 
|  | 117 | for (i = 0; i < len1; ++i) | 
|  | 118 | s1[i] = 32 + 23 * i % (max_char - 32); | 
|  | 119 | s1[len1] = '\0'; | 
|  | 120 |  | 
|  | 121 | for (i = 0; i < len2; i++) | 
|  | 122 | s2[i] = 32 + 23 * i % (max_char - 32); | 
|  | 123 |  | 
|  | 124 | printf ("Length %4zd/%4zd, alignment %2zd/%2zd:", len1, len2, align1, align2); | 
|  | 125 |  | 
|  | 126 | FOR_EACH_IMPL (impl, 0) | 
|  | 127 | { | 
|  | 128 | s2[len2] = '\0'; | 
|  | 129 | do_one_test (impl, s2, s1); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | putchar ('\n'); | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | int | 
|  | 136 | test_main (void) | 
|  | 137 | { | 
|  | 138 | size_t i; | 
|  | 139 |  | 
|  | 140 | test_init (); | 
|  | 141 |  | 
|  | 142 | printf ("%28s", ""); | 
|  | 143 | FOR_EACH_IMPL (impl, 0) | 
|  | 144 | printf ("\t%s", impl->name); | 
|  | 145 | putchar ('\n'); | 
|  | 146 |  | 
|  | 147 | for (i = 0; i < 16; ++i) | 
|  | 148 | { | 
|  | 149 | do_test (0, 0, i, i, SMALL_CHAR); | 
|  | 150 | do_test (0, 0, i, i, BIG_CHAR); | 
|  | 151 | do_test (0, i, i, i, SMALL_CHAR); | 
|  | 152 | do_test (i, 0, i, i, BIG_CHAR); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | for (i = 1; i < 8; ++i) | 
|  | 156 | { | 
|  | 157 | do_test (0, 0, 8 << i, 8 << i, SMALL_CHAR); | 
|  | 158 | do_test (8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR); | 
|  | 159 | do_test (0, 0, 8 << i, 2 << i, SMALL_CHAR); | 
|  | 160 | do_test (8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | for (i = 1; i < 8; ++i) | 
|  | 164 | { | 
|  | 165 | do_test (i, 2 * i, 8 << i, 1, SMALL_CHAR); | 
|  | 166 | do_test (2 * i, i, 8 << i, 1, BIG_CHAR); | 
|  | 167 | do_test (i, i, 8 << i, 10, SMALL_CHAR); | 
|  | 168 | do_test (i, i, 8 << i, 10, BIG_CHAR); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | return ret; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | #include "../test-skeleton.c" |