xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Measure strncasecmp 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 | #include <ctype.h> |
| 20 | #define TEST_MAIN |
| 21 | #define TEST_NAME "strncasecmp" |
| 22 | #include "bench-string.h" |
| 23 | |
| 24 | typedef int (*proto_t) (const char *, const char *, size_t); |
| 25 | static int simple_strncasecmp (const char *, const char *, size_t); |
| 26 | static int stupid_strncasecmp (const char *, const char *, size_t); |
| 27 | |
| 28 | IMPL (stupid_strncasecmp, 0) |
| 29 | IMPL (simple_strncasecmp, 0) |
| 30 | IMPL (strncasecmp, 1) |
| 31 | |
| 32 | static int |
| 33 | simple_strncasecmp (const char *s1, const char *s2, size_t n) |
| 34 | { |
| 35 | int ret; |
| 36 | |
| 37 | if (n == 0) |
| 38 | return 0; |
| 39 | |
| 40 | while ((ret = ((unsigned char) tolower (*s1) |
| 41 | - (unsigned char) tolower (*s2))) == 0 |
| 42 | && *s1++) |
| 43 | { |
| 44 | if (--n == 0) |
| 45 | return 0; |
| 46 | ++s2; |
| 47 | } |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | static int |
| 52 | stupid_strncasecmp (const char *s1, const char *s2, size_t max) |
| 53 | { |
| 54 | size_t ns1 = strlen (s1) + 1; |
| 55 | size_t ns2 = strlen (s2) + 1; |
| 56 | size_t n = ns1 < ns2 ? ns1 : ns2; |
| 57 | if (n > max) |
| 58 | n = max; |
| 59 | int ret = 0; |
| 60 | |
| 61 | while (n--) |
| 62 | { |
| 63 | if ((ret = ((unsigned char) tolower (*s1) |
| 64 | - (unsigned char) tolower (*s2))) != 0) |
| 65 | break; |
| 66 | ++s1; |
| 67 | ++s2; |
| 68 | } |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n, |
| 74 | int exp_result) |
| 75 | { |
| 76 | size_t i, iters = INNER_LOOP_ITERS; |
| 77 | timing_t start, stop, cur; |
| 78 | |
| 79 | TIMING_NOW (start); |
| 80 | for (i = 0; i < iters; ++i) |
| 81 | { |
| 82 | CALL (impl, s1, s2, n); |
| 83 | } |
| 84 | TIMING_NOW (stop); |
| 85 | |
| 86 | TIMING_DIFF (cur, start, stop); |
| 87 | |
| 88 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char, |
| 93 | int exp_result) |
| 94 | { |
| 95 | size_t i; |
| 96 | char *s1, *s2; |
| 97 | |
| 98 | if (len == 0) |
| 99 | return; |
| 100 | |
| 101 | align1 &= 7; |
| 102 | if (align1 + len + 1 >= page_size) |
| 103 | return; |
| 104 | |
| 105 | align2 &= 7; |
| 106 | if (align2 + len + 1 >= page_size) |
| 107 | return; |
| 108 | |
| 109 | s1 = (char *) (buf1 + align1); |
| 110 | s2 = (char *) (buf2 + align2); |
| 111 | |
| 112 | for (i = 0; i < len; i++) |
| 113 | { |
| 114 | s1[i] = toupper (1 + 23 * i % max_char); |
| 115 | s2[i] = tolower (s1[i]); |
| 116 | } |
| 117 | |
| 118 | s1[len] = s2[len] = 0; |
| 119 | s1[len + 1] = 23; |
| 120 | s2[len + 1] = 24 + exp_result; |
| 121 | if ((s2[len - 1] == 'z' && exp_result == -1) |
| 122 | || (s2[len - 1] == 'a' && exp_result == 1)) |
| 123 | s1[len - 1] += exp_result; |
| 124 | else |
| 125 | s2[len - 1] -= exp_result; |
| 126 | |
| 127 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); |
| 128 | |
| 129 | FOR_EACH_IMPL (impl, 0) |
| 130 | do_one_test (impl, s1, s2, n, exp_result); |
| 131 | |
| 132 | putchar ('\n'); |
| 133 | } |
| 134 | |
| 135 | int |
| 136 | test_main (void) |
| 137 | { |
| 138 | size_t i; |
| 139 | |
| 140 | test_init (); |
| 141 | |
| 142 | printf ("%23s", ""); |
| 143 | FOR_EACH_IMPL (impl, 0) |
| 144 | printf ("\t%s", impl->name); |
| 145 | putchar ('\n'); |
| 146 | |
| 147 | for (i = 1; i < 16; ++i) |
| 148 | { |
| 149 | do_test (i, i, i - 1, i, 127, 0); |
| 150 | |
| 151 | do_test (i, i, i, i, 127, 0); |
| 152 | do_test (i, i, i, i, 127, 1); |
| 153 | do_test (i, i, i, i, 127, -1); |
| 154 | |
| 155 | do_test (i, i, i + 1, i, 127, 0); |
| 156 | do_test (i, i, i + 1, i, 127, 1); |
| 157 | do_test (i, i, i + 1, i, 127, -1); |
| 158 | } |
| 159 | |
| 160 | for (i = 1; i < 10; ++i) |
| 161 | { |
| 162 | do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0); |
| 163 | do_test (0, 0, 2 << i, 2 << i, 254, 0); |
| 164 | do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0); |
| 165 | |
| 166 | do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0); |
| 167 | |
| 168 | do_test (0, 0, 2 << i, 2 << i, 127, 1); |
| 169 | do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1); |
| 170 | |
| 171 | do_test (0, 0, 2 << i, 2 << i, 254, 1); |
| 172 | do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1); |
| 173 | |
| 174 | do_test (0, 0, 2 << i, 2 << i, 127, -1); |
| 175 | do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1); |
| 176 | |
| 177 | do_test (0, 0, 2 << i, 2 << i, 254, -1); |
| 178 | do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1); |
| 179 | } |
| 180 | |
| 181 | for (i = 1; i < 8; ++i) |
| 182 | { |
| 183 | do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0); |
| 184 | do_test (i, 2 * i, 8 << i, 8 << i, 127, 0); |
| 185 | do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0); |
| 186 | |
| 187 | do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0); |
| 188 | do_test (2 * i, i, 8 << i, 8 << i, 254, 0); |
| 189 | do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0); |
| 190 | |
| 191 | do_test (i, 2 * i, 8 << i, 8 << i, 127, 1); |
| 192 | do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1); |
| 193 | |
| 194 | do_test (2 * i, i, 8 << i, 8 << i, 254, 1); |
| 195 | do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1); |
| 196 | |
| 197 | do_test (i, 2 * i, 8 << i, 8 << i, 127, -1); |
| 198 | do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1); |
| 199 | |
| 200 | do_test (2 * i, i, 8 << i, 8 << i, 254, -1); |
| 201 | do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1); |
| 202 | } |
| 203 | |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | #include "../test-skeleton.c" |