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