| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Measure memset 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 TEST_BZERO | 
|  | 21 | # define TEST_NAME "bzero" | 
|  | 22 | #else | 
|  | 23 | # ifndef WIDE | 
|  | 24 | #  define TEST_NAME "memset" | 
|  | 25 | # else | 
|  | 26 | #  define TEST_NAME "wmemset" | 
|  | 27 | # endif /* WIDE */ | 
|  | 28 | #endif /* !TEST_BZERO */ | 
|  | 29 | #define MIN_PAGE_SIZE 131072 | 
|  | 30 | #include "bench-string.h" | 
|  | 31 |  | 
|  | 32 | #ifndef WIDE | 
|  | 33 | # define MEMSET memset | 
|  | 34 | # define CHAR char | 
|  | 35 | # define SIMPLE_MEMSET simple_memset | 
|  | 36 | # define MEMCMP memcmp | 
|  | 37 | #else | 
|  | 38 | # include <wchar.h> | 
|  | 39 | # define MEMSET wmemset | 
|  | 40 | # define CHAR wchar_t | 
|  | 41 | # define SIMPLE_MEMSET simple_wmemset | 
|  | 42 | # define MEMCMP wmemcmp | 
|  | 43 | #endif /* WIDE */ | 
|  | 44 |  | 
|  | 45 | CHAR *SIMPLE_MEMSET (CHAR *, int, size_t); | 
|  | 46 |  | 
|  | 47 | #ifdef TEST_BZERO | 
|  | 48 | typedef void (*proto_t) (char *, size_t); | 
|  | 49 | void simple_bzero (char *, size_t); | 
|  | 50 | void builtin_bzero (char *, size_t); | 
|  | 51 |  | 
|  | 52 | IMPL (simple_bzero, 0) | 
|  | 53 | IMPL (builtin_bzero, 0) | 
|  | 54 | IMPL (bzero, 1) | 
|  | 55 |  | 
|  | 56 | void | 
|  | 57 | simple_bzero (char *s, size_t n) | 
|  | 58 | { | 
|  | 59 | SIMPLE_MEMSET (s, 0, n); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | void | 
|  | 63 | builtin_bzero (char *s, size_t n) | 
|  | 64 | { | 
|  | 65 | __builtin_bzero (s, n); | 
|  | 66 | } | 
|  | 67 | #else | 
|  | 68 | typedef CHAR *(*proto_t) (CHAR *, int, size_t); | 
|  | 69 |  | 
|  | 70 | IMPL (SIMPLE_MEMSET, 0) | 
|  | 71 | # ifndef WIDE | 
|  | 72 | char *builtin_memset (char *, int, size_t); | 
|  | 73 | IMPL (builtin_memset, 0) | 
|  | 74 | # endif /* !WIDE */ | 
|  | 75 | IMPL (MEMSET, 1) | 
|  | 76 |  | 
|  | 77 | # ifndef WIDE | 
|  | 78 | char * | 
|  | 79 | builtin_memset (char *s, int c, size_t n) | 
|  | 80 | { | 
|  | 81 | return __builtin_memset (s, c, n); | 
|  | 82 | } | 
|  | 83 | # endif /* !WIDE */ | 
|  | 84 | #endif /* !TEST_BZERO */ | 
|  | 85 |  | 
|  | 86 | CHAR * | 
|  | 87 | inhibit_loop_to_libcall | 
|  | 88 | SIMPLE_MEMSET (CHAR *s, int c, size_t n) | 
|  | 89 | { | 
|  | 90 | CHAR *r = s, *end = s + n; | 
|  | 91 | while (r < end) | 
|  | 92 | *r++ = c; | 
|  | 93 | return s; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | static void | 
|  | 97 | do_one_test (impl_t *impl, CHAR *s, int c __attribute ((unused)), size_t n) | 
|  | 98 | { | 
|  | 99 | size_t i, iters = INNER_LOOP_ITERS; | 
|  | 100 | timing_t start, stop, cur; | 
|  | 101 | CHAR tstbuf[n]; | 
|  | 102 | #ifdef TEST_BZERO | 
|  | 103 | simple_bzero (tstbuf, n); | 
|  | 104 | CALL (impl, s, n); | 
|  | 105 | if (memcmp (s, tstbuf, n) != 0) | 
|  | 106 | #else | 
|  | 107 | CHAR *res = CALL (impl, s, c, n); | 
|  | 108 | if (res != s | 
|  | 109 | || SIMPLE_MEMSET (tstbuf, c, n) != tstbuf | 
|  | 110 | || MEMCMP (s, tstbuf, n) != 0) | 
|  | 111 | #endif /* !TEST_BZERO */ | 
|  | 112 | { | 
|  | 113 | error (0, 0, "Wrong result in function %s", impl->name); | 
|  | 114 | ret = 1; | 
|  | 115 | return; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | TIMING_NOW (start); | 
|  | 119 | for (i = 0; i < iters; ++i) | 
|  | 120 | { | 
|  | 121 | #ifdef TEST_BZERO | 
|  | 122 | CALL (impl, s, n); | 
|  | 123 | #else | 
|  | 124 | CALL (impl, s, c, n); | 
|  | 125 | #endif /* !TEST_BZERO */ | 
|  | 126 | } | 
|  | 127 | TIMING_NOW (stop); | 
|  | 128 |  | 
|  | 129 | TIMING_DIFF (cur, start, stop); | 
|  | 130 |  | 
|  | 131 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | static void | 
|  | 135 | do_test (size_t align, int c, size_t len) | 
|  | 136 | { | 
|  | 137 | align &= 7; | 
|  | 138 | if ((align + len) * sizeof (CHAR) > page_size) | 
|  | 139 | return; | 
|  | 140 |  | 
|  | 141 | printf ("Length %4zd, alignment %2zd, c %2d:", len, align, c); | 
|  | 142 |  | 
|  | 143 | FOR_EACH_IMPL (impl, 0) | 
|  | 144 | do_one_test (impl, (CHAR *) (buf1) + align, c, len); | 
|  | 145 |  | 
|  | 146 | putchar ('\n'); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | int | 
|  | 150 | test_main (void) | 
|  | 151 | { | 
|  | 152 | size_t i; | 
|  | 153 | int c = 0; | 
|  | 154 |  | 
|  | 155 | test_init (); | 
|  | 156 |  | 
|  | 157 | printf ("%24s", ""); | 
|  | 158 | FOR_EACH_IMPL (impl, 0) | 
|  | 159 | printf ("\t%s", impl->name); | 
|  | 160 | putchar ('\n'); | 
|  | 161 |  | 
|  | 162 | #ifndef TEST_BZERO | 
|  | 163 | for (c = -65; c <= 130; c += 65) | 
|  | 164 | #endif | 
|  | 165 | { | 
|  | 166 | for (i = 0; i < 18; ++i) | 
|  | 167 | do_test (0, c, 1 << i); | 
|  | 168 | for (i = 1; i < 32; ++i) | 
|  | 169 | { | 
|  | 170 | do_test (i, c, i); | 
|  | 171 | if (i & (i - 1)) | 
|  | 172 | do_test (0, c, i); | 
|  | 173 | } | 
|  | 174 | for (i = 32; i < 512; i+=32) | 
|  | 175 | { | 
|  | 176 | do_test (0, c, i); | 
|  | 177 | do_test (i, c, i); | 
|  | 178 | } | 
|  | 179 | do_test (1, c, 14); | 
|  | 180 | do_test (3, c, 1024); | 
|  | 181 | do_test (4, c, 64); | 
|  | 182 | do_test (2, c, 25); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | return ret; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | #include "../test-skeleton.c" |