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

source code of glibc/benchtests/bench-strcpy.c