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