lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Measure memccpy functions. |
| 2 | Copyright (C) 2013-2015 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 "memccpy" |
| 21 | #include "bench-string.h" |
| 22 | |
| 23 | void *simple_memccpy (void *, const void *, int, size_t); |
| 24 | void *stupid_memccpy (void *, const void *, int, size_t); |
| 25 | |
| 26 | IMPL (stupid_memccpy, 0) |
| 27 | IMPL (simple_memccpy, 0) |
| 28 | IMPL (memccpy, 1) |
| 29 | |
| 30 | void * |
| 31 | simple_memccpy (void *dst, const void *src, int c, size_t n) |
| 32 | { |
| 33 | const char *s = src; |
| 34 | char *d = dst; |
| 35 | |
| 36 | while (n-- > 0) |
| 37 | if ((*d++ = *s++) == (char) c) |
| 38 | return d; |
| 39 | |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | void * |
| 44 | stupid_memccpy (void *dst, const void *src, int c, size_t n) |
| 45 | { |
| 46 | void *p = memchr (src, c, n); |
| 47 | |
| 48 | if (p != NULL) |
| 49 | return mempcpy (dst, src, p - src + 1); |
| 50 | |
| 51 | memcpy (dst, src, n); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | typedef void *(*proto_t) (void *, const void *, int c, size_t); |
| 56 | |
| 57 | static void |
| 58 | do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len, |
| 59 | size_t n) |
| 60 | { |
| 61 | void *expect = len > n ? NULL : (char *) dst + len; |
| 62 | size_t i, iters = INNER_LOOP_ITERS; |
| 63 | timing_t start, stop, cur; |
| 64 | |
| 65 | if (CALL (impl, dst, src, c, n) != expect) |
| 66 | { |
| 67 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 68 | CALL (impl, dst, src, c, n), expect); |
| 69 | ret = 1; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (memcmp (dst, src, len > n ? n : len) != 0) |
| 74 | { |
| 75 | error (0, 0, "Wrong result in function %s", impl->name); |
| 76 | ret = 1; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | TIMING_NOW (start); |
| 81 | for (i = 0; i < iters; ++i) |
| 82 | { |
| 83 | CALL (impl, dst, src, c, n); |
| 84 | } |
| 85 | TIMING_NOW (stop); |
| 86 | |
| 87 | TIMING_DIFF (cur, start, stop); |
| 88 | |
| 89 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | do_test (size_t align1, size_t align2, int c, size_t len, size_t n, |
| 94 | int max_char) |
| 95 | { |
| 96 | size_t i; |
| 97 | char *s1, *s2; |
| 98 | |
| 99 | align1 &= 7; |
| 100 | if (align1 + len >= page_size) |
| 101 | return; |
| 102 | |
| 103 | align2 &= 7; |
| 104 | if (align2 + len >= page_size) |
| 105 | return; |
| 106 | |
| 107 | s1 = (char *) (buf1 + align1); |
| 108 | s2 = (char *) (buf2 + align2); |
| 109 | |
| 110 | for (i = 0; i < len - 1; ++i) |
| 111 | { |
| 112 | s1[i] = 32 + 23 * i % (max_char - 32); |
| 113 | if (s1[i] == (char) c) |
| 114 | --s1[i]; |
| 115 | } |
| 116 | s1[len - 1] = c; |
| 117 | for (i = len; i + align1 < page_size && i < len + 64; ++i) |
| 118 | s1[i] = 32 + 32 * i % (max_char - 32); |
| 119 | |
| 120 | printf ("Length %4zd, n %4zd, char %d, alignment %2zd/%2zd:", len, n, c, align1, align2); |
| 121 | |
| 122 | FOR_EACH_IMPL (impl, 0) |
| 123 | do_one_test (impl, s2, s1, c, len, n); |
| 124 | |
| 125 | putchar ('\n'); |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | test_main (void) |
| 130 | { |
| 131 | size_t i; |
| 132 | |
| 133 | test_init (); |
| 134 | |
| 135 | printf ("%28s", ""); |
| 136 | FOR_EACH_IMPL (impl, 0) |
| 137 | printf ("\t%s", impl->name); |
| 138 | putchar ('\n'); |
| 139 | |
| 140 | for (i = 1; i < 8; ++i) |
| 141 | { |
| 142 | do_test (i, i, 12, 16, 16, 127); |
| 143 | do_test (i, i, 23, 16, 16, 255); |
| 144 | do_test (i, 2 * i, 28, 16, 16, 127); |
| 145 | do_test (2 * i, i, 31, 16, 16, 255); |
| 146 | do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127); |
| 147 | do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127); |
| 148 | do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255); |
| 149 | do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255); |
| 150 | } |
| 151 | |
| 152 | for (i = 1; i < 8; ++i) |
| 153 | { |
| 154 | do_test (0, 0, i, 4 << i, 8 << i, 127); |
| 155 | do_test (0, 0, i, 16 << i, 8 << i, 127); |
| 156 | do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127); |
| 157 | do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127); |
| 158 | } |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | #include "../test-skeleton.c" |