]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure strncpy functions. |
2b778ceb | 2 | Copyright (C) 2013-2021 Free Software Foundation, Inc. |
97020474 SP |
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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
97020474 | 18 | |
90d3320d WD |
19 | #define BIG_CHAR MAX_CHAR |
20 | ||
d183b96e | 21 | #ifdef WIDE |
d183b96e | 22 | # define SMALL_CHAR 1273 |
d183b96e | 23 | #else |
d183b96e | 24 | # define SMALL_CHAR 127 |
d183b96e SL |
25 | #endif /* !WIDE */ |
26 | ||
97020474 SP |
27 | #ifndef STRNCPY_RESULT |
28 | # define STRNCPY_RESULT(dst, len, n) dst | |
29 | # define TEST_MAIN | |
d183b96e SL |
30 | # ifndef WIDE |
31 | # define TEST_NAME "strncpy" | |
32 | # else | |
33 | # define TEST_NAME "wcsncpy" | |
648279f4 | 34 | # define generic_strncpy generic_wcsncpy |
d183b96e | 35 | # endif /* WIDE */ |
97020474 | 36 | # include "bench-string.h" |
d183b96e SL |
37 | |
38 | CHAR * | |
648279f4 | 39 | generic_strncpy (CHAR *dst, const CHAR *src, size_t n) |
97020474 | 40 | { |
648279f4 WD |
41 | size_t nc = STRNLEN (src, n); |
42 | if (nc != n) | |
43 | MEMSET (dst + nc, 0, n - nc); | |
44 | return MEMCPY (dst, src, nc); | |
97020474 SP |
45 | } |
46 | ||
648279f4 WD |
47 | IMPL (STRNCPY, 1) |
48 | IMPL (generic_strncpy, 0) | |
97020474 | 49 | |
d183b96e | 50 | #endif /* !STRNCPY_RESULT */ |
97020474 | 51 | |
d183b96e | 52 | typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t); |
97020474 SP |
53 | |
54 | static void | |
d183b96e | 55 | do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n) |
97020474 | 56 | { |
d0645912 | 57 | size_t i, iters = INNER_LOOP_ITERS_LARGE * (4 / CHARBYTES); |
44558701 WN |
58 | timing_t start, stop, cur; |
59 | ||
97020474 SP |
60 | if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n)) |
61 | { | |
62 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | |
63 | CALL (impl, dst, src, n), dst); | |
64 | ret = 1; | |
65 | return; | |
66 | } | |
67 | ||
d183b96e | 68 | if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0) |
97020474 SP |
69 | { |
70 | error (0, 0, "Wrong result in function %s", impl->name); | |
71 | ret = 1; | |
72 | return; | |
73 | } | |
74 | ||
75 | if (n > len) | |
76 | { | |
77 | size_t i; | |
78 | ||
79 | for (i = len; i < n; ++i) | |
80 | if (dst [i] != '\0') | |
81 | { | |
82 | error (0, 0, "Wrong result in function %s", impl->name); | |
83 | ret = 1; | |
84 | return; | |
85 | } | |
86 | } | |
87 | ||
44558701 WN |
88 | TIMING_NOW (start); |
89 | for (i = 0; i < iters; ++i) | |
97020474 | 90 | { |
44558701 WN |
91 | CALL (impl, dst, src, n); |
92 | } | |
93 | TIMING_NOW (stop); | |
97020474 | 94 | |
44558701 | 95 | TIMING_DIFF (cur, start, stop); |
97020474 | 96 | |
44558701 | 97 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
97020474 SP |
98 | } |
99 | ||
100 | static void | |
101 | do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char) | |
102 | { | |
103 | size_t i; | |
d183b96e | 104 | CHAR *s1, *s2; |
97020474 | 105 | |
d183b96e SL |
106 | /* For wcsncpy: align1 and align2 here mean alignment not in bytes, |
107 | but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */ | |
97020474 | 108 | align1 &= 7; |
d183b96e | 109 | if ((align1 + len) * sizeof (CHAR) >= page_size) |
97020474 SP |
110 | return; |
111 | ||
112 | align2 &= 7; | |
d183b96e | 113 | if ((align2 + len) * sizeof (CHAR) >= page_size) |
97020474 SP |
114 | return; |
115 | ||
d183b96e SL |
116 | s1 = (CHAR *) (buf1) + align1; |
117 | s2 = (CHAR *) (buf2) + align2; | |
97020474 SP |
118 | |
119 | for (i = 0; i < len; ++i) | |
120 | s1[i] = 32 + 23 * i % (max_char - 32); | |
121 | s1[len] = 0; | |
d183b96e SL |
122 | for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64; |
123 | ++i) | |
97020474 SP |
124 | s1[i] = 32 + 32 * i % (max_char - 32); |
125 | ||
44558701 | 126 | printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2); |
97020474 SP |
127 | |
128 | FOR_EACH_IMPL (impl, 0) | |
129 | do_one_test (impl, s2, s1, len, n); | |
130 | ||
44558701 | 131 | putchar ('\n'); |
97020474 SP |
132 | } |
133 | ||
b598e134 | 134 | static int |
97020474 SP |
135 | test_main (void) |
136 | { | |
137 | size_t i; | |
138 | ||
139 | test_init (); | |
140 | ||
141 | printf ("%28s", ""); | |
142 | FOR_EACH_IMPL (impl, 0) | |
143 | printf ("\t%s", impl->name); | |
144 | putchar ('\n'); | |
145 | ||
146 | for (i = 1; i < 8; ++i) | |
147 | { | |
d183b96e SL |
148 | do_test (i, i, 16, 16, SMALL_CHAR); |
149 | do_test (i, i, 16, 16, BIG_CHAR); | |
150 | do_test (i, 2 * i, 16, 16, SMALL_CHAR); | |
151 | do_test (2 * i, i, 16, 16, BIG_CHAR); | |
152 | do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR); | |
153 | do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR); | |
154 | do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR); | |
155 | do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR); | |
97020474 SP |
156 | } |
157 | ||
158 | for (i = 1; i < 8; ++i) | |
159 | { | |
d183b96e SL |
160 | do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR); |
161 | do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR); | |
162 | do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR); | |
163 | do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR); | |
97020474 SP |
164 | } |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
b598e134 | 169 | #include <support/test-driver.c> |