xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Measure memmem 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 | #define TEST_NAME "memmem" |
| 21 | #define BUF1PAGES 20 |
| 22 | #define ITERATIONS 500 |
| 23 | #include "bench-string.h" |
| 24 | |
| 25 | typedef char *(*proto_t) (const void *, size_t, const void *, size_t); |
| 26 | void *simple_memmem (const void *, size_t, const void *, size_t); |
| 27 | |
| 28 | IMPL (simple_memmem, 0) |
| 29 | IMPL (memmem, 1) |
| 30 | |
| 31 | void * |
| 32 | simple_memmem (const void *haystack, size_t haystack_len, const void *needle, |
| 33 | size_t needle_len) |
| 34 | { |
| 35 | const char *begin; |
| 36 | const char *const last_possible |
| 37 | = (const char *) haystack + haystack_len - needle_len; |
| 38 | |
| 39 | if (needle_len == 0) |
| 40 | /* The first occurrence of the empty string is deemed to occur at |
| 41 | the beginning of the string. */ |
| 42 | return (void *) haystack; |
| 43 | |
| 44 | /* Sanity check, otherwise the loop might search through the whole |
| 45 | memory. */ |
| 46 | if (__glibc_unlikely (haystack_len < needle_len)) |
| 47 | return NULL; |
| 48 | |
| 49 | for (begin = (const char *) haystack; begin <= last_possible; ++begin) |
| 50 | if (begin[0] == ((const char *) needle)[0] && |
| 51 | !memcmp ((const void *) &begin[1], |
| 52 | (const void *) ((const char *) needle + 1), |
| 53 | needle_len - 1)) |
| 54 | return (void *) begin; |
| 55 | |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | do_one_test (impl_t *impl, const void *haystack, size_t haystack_len, |
| 61 | const void *needle, size_t needle_len, const void *expected) |
| 62 | { |
| 63 | size_t i, iters = INNER_LOOP_ITERS; |
| 64 | timing_t start, stop, cur; |
| 65 | |
| 66 | TIMING_NOW (start); |
| 67 | for (i = 0; i < iters; ++i) |
| 68 | { |
| 69 | CALL (impl, haystack, haystack_len, needle, needle_len); |
| 70 | } |
| 71 | TIMING_NOW (stop); |
| 72 | |
| 73 | TIMING_DIFF (cur, start, stop); |
| 74 | |
| 75 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | do_test (const char *str, size_t len, size_t idx) |
| 80 | { |
| 81 | char tmpbuf[len]; |
| 82 | |
| 83 | memcpy (tmpbuf, buf1 + idx, len); |
| 84 | memcpy (buf1 + idx, str, len); |
| 85 | |
| 86 | printf ("String %s, offset %zd:", str, idx); |
| 87 | |
| 88 | FOR_EACH_IMPL (impl, 0) |
| 89 | do_one_test (impl, buf1, BUF1PAGES * page_size, str, len, buf1 + idx); |
| 90 | |
| 91 | memcpy (buf1 + idx, tmpbuf, len); |
| 92 | |
| 93 | putchar ('\n'); |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | do_random_tests (void) |
| 98 | { |
| 99 | for (size_t n = 0; n < ITERATIONS; ++n) |
| 100 | { |
| 101 | char tmpbuf[32]; |
| 102 | |
| 103 | size_t shift = random () % 11; |
| 104 | size_t rel = random () % ((2 << (shift + 1)) * 64); |
| 105 | size_t idx = MIN ((2 << shift) * 64 + rel, BUF1PAGES * page_size - 2); |
| 106 | size_t len = random () % (sizeof (tmpbuf) - 1) + 1; |
| 107 | len = MIN (len, BUF1PAGES * page_size - idx - 1); |
| 108 | memcpy (tmpbuf, buf1 + idx, len); |
| 109 | for (size_t i = random () % len / 2 + 1; i > 0; --i) |
| 110 | { |
| 111 | size_t off = random () % len; |
| 112 | char ch = '0' + random () % 10; |
| 113 | |
| 114 | buf1[idx + off] = ch; |
| 115 | } |
| 116 | |
| 117 | printf ("String %.*s, offset %zd:", (int) len, buf1 + idx, idx); |
| 118 | |
| 119 | FOR_EACH_IMPL (impl, 0) |
| 120 | do_one_test (impl, buf1, BUF1PAGES * page_size, buf1 + idx, len, |
| 121 | buf1 + idx); |
| 122 | |
| 123 | putchar ('\n'); |
| 124 | |
| 125 | memcpy (buf1 + idx, tmpbuf, len); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static const char *const strs[] = |
| 130 | { |
| 131 | "00000", "00112233", "0123456789", "0000111100001111", |
| 132 | "00000111110000022222", "012345678901234567890", |
| 133 | "abc0", "aaaa0", "abcabc0" |
| 134 | }; |
| 135 | |
| 136 | |
| 137 | int |
| 138 | test_main (void) |
| 139 | { |
| 140 | size_t i; |
| 141 | |
| 142 | test_init (); |
| 143 | |
| 144 | printf ("%23s", ""); |
| 145 | FOR_EACH_IMPL (impl, 0) |
| 146 | printf ("\t%s", impl->name); |
| 147 | putchar ('\n'); |
| 148 | |
| 149 | for (i = 0; i < BUF1PAGES * page_size; ++i) |
| 150 | buf1[i] = 60 + random () % 32; |
| 151 | |
| 152 | for (i = 0; i < sizeof (strs) / sizeof (strs[0]); ++i) |
| 153 | for (size_t j = 0; j < 120; j += 7) |
| 154 | { |
| 155 | size_t len = strlen (strs[i]); |
| 156 | |
| 157 | do_test (strs[i], len, j); |
| 158 | } |
| 159 | |
| 160 | do_random_tests (); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | #include "../test-skeleton.c" |