1 | /* Measure strcpy functions. |
2 | Copyright (C) 2013-2024 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 | #include "json-lib.h" |
30 | |
31 | #ifndef STRCPY_RESULT |
32 | # define STRCPY_RESULT(dst, len) dst |
33 | # define TEST_MAIN |
34 | # ifndef WIDE |
35 | # define TEST_NAME "strcpy" |
36 | # else |
37 | # define TEST_NAME "wcscpy" |
38 | # define generic_strcpy generic_wcscpy |
39 | # endif |
40 | # include "bench-string.h" |
41 | |
42 | CHAR * |
43 | generic_strcpy (CHAR *dst, const CHAR *src) |
44 | { |
45 | return MEMCPY (dst, src, STRLEN (src) + 1); |
46 | } |
47 | |
48 | IMPL (STRCPY, 1) |
49 | IMPL (generic_strcpy, 0) |
50 | |
51 | #endif |
52 | |
53 | typedef CHAR *(*proto_t) (CHAR *, const CHAR *); |
54 | |
55 | static void |
56 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *dst, const CHAR *src, |
57 | size_t len __attribute__ ((unused))) |
58 | { |
59 | size_t i, iters = INNER_LOOP_ITERS; |
60 | timing_t start, stop, cur; |
61 | |
62 | if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len)) |
63 | { |
64 | error (status: 0, errnum: 0, format: "Wrong result in function %s %p %p" , impl->name, |
65 | CALL (impl, dst, src), STRCPY_RESULT (dst, len)); |
66 | ret = 1; |
67 | return; |
68 | } |
69 | |
70 | if (STRCMP (dst, src) != 0) |
71 | { |
72 | error (status: 0, errnum: 0, |
73 | format: "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"" , |
74 | impl->name, dst, src); |
75 | ret = 1; |
76 | return; |
77 | } |
78 | |
79 | TIMING_NOW (start); |
80 | for (i = 0; i < iters; ++i) |
81 | { |
82 | CALL (impl, dst, src); |
83 | } |
84 | TIMING_NOW (stop); |
85 | |
86 | TIMING_DIFF (cur, start, stop); |
87 | |
88 | json_element_double (ctx: json_ctx, d: (double) cur / (double) iters); |
89 | } |
90 | |
91 | static void |
92 | do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, |
93 | int max_char) |
94 | { |
95 | size_t i; |
96 | CHAR *s1, *s2; |
97 | /* For wcscpy: align1 and align2 here mean alignment not in bytes, |
98 | but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)) |
99 | len for wcschr here isn't in bytes but it's number of wchar_t symbols. */ |
100 | align1 &= 7; |
101 | if ((align1 + len) * sizeof (CHAR) >= page_size) |
102 | return; |
103 | |
104 | align2 &= 7; |
105 | if ((align2 + len) * sizeof (CHAR) >= page_size) |
106 | return; |
107 | |
108 | s1 = (CHAR *) (buf1) + align1; |
109 | s2 = (CHAR *) (buf2) + align2; |
110 | |
111 | for (i = 0; i < len; i++) |
112 | s1[i] = 32 + 23 * i % (max_char - 32); |
113 | s1[len] = 0; |
114 | |
115 | json_element_object_begin (ctx: json_ctx); |
116 | json_attr_uint (ctx: json_ctx, name: "align1" , d: align1); |
117 | json_attr_uint (ctx: json_ctx, name: "align2" , d: align2); |
118 | json_attr_uint (ctx: json_ctx, name: "len" , d: len); |
119 | json_attr_uint (ctx: json_ctx, name: "max_char" , d: max_char); |
120 | |
121 | json_array_begin (ctx: json_ctx, name: "timings" ); |
122 | |
123 | FOR_EACH_IMPL (impl, 0) |
124 | do_one_test (json_ctx, impl, dst: s2, src: s1, len); |
125 | |
126 | json_array_end (ctx: json_ctx); |
127 | json_element_object_end (ctx: json_ctx); |
128 | } |
129 | |
130 | int |
131 | test_main (void) |
132 | { |
133 | json_ctx_t json_ctx; |
134 | size_t i; |
135 | |
136 | test_init (); |
137 | |
138 | json_init (ctx: &json_ctx, indent_level: 0, stdout); |
139 | |
140 | json_document_begin (ctx: &json_ctx); |
141 | json_attr_string (ctx: &json_ctx, name: "timing_type" , TIMING_TYPE); |
142 | |
143 | json_attr_object_begin (ctx: &json_ctx, name: "functions" ); |
144 | json_attr_object_begin (ctx: &json_ctx, TEST_NAME); |
145 | json_attr_string (ctx: &json_ctx, name: "bench-variant" , s: "" ); |
146 | |
147 | json_array_begin (ctx: &json_ctx, name: "ifuncs" ); |
148 | FOR_EACH_IMPL (impl, 0) |
149 | json_element_string (ctx: &json_ctx, s: impl->name); |
150 | json_array_end (ctx: &json_ctx); |
151 | |
152 | json_array_begin (ctx: &json_ctx, name: "results" ); |
153 | |
154 | for (i = 0; i < 16; ++i) |
155 | { |
156 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: i, SMALL_CHAR); |
157 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: i, BIG_CHAR); |
158 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: i, SMALL_CHAR); |
159 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, len: i, BIG_CHAR); |
160 | } |
161 | |
162 | for (i = 1; i < 8; ++i) |
163 | { |
164 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 8 << i, SMALL_CHAR); |
165 | do_test (json_ctx: &json_ctx, align1: 8 - i, align2: 2 * i, len: 8 << i, SMALL_CHAR); |
166 | } |
167 | |
168 | for (i = 1; i < 8; ++i) |
169 | { |
170 | do_test (json_ctx: &json_ctx, align1: i, align2: 2 * i, len: 8 << i, SMALL_CHAR); |
171 | do_test (json_ctx: &json_ctx, align1: 2 * i, align2: i, len: 8 << i, BIG_CHAR); |
172 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: 8 << i, SMALL_CHAR); |
173 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: 8 << i, BIG_CHAR); |
174 | } |
175 | |
176 | for (i = 16; i <= 512; i += 4) |
177 | { |
178 | do_test (json_ctx: &json_ctx, align1: 0, align2: 4, len: i, SMALL_CHAR); |
179 | do_test (json_ctx: &json_ctx, align1: 4, align2: 0, len: i, BIG_CHAR); |
180 | do_test (json_ctx: &json_ctx, align1: 4, align2: 4, len: i, SMALL_CHAR); |
181 | do_test (json_ctx: &json_ctx, align1: 2, align2: 2, len: i, BIG_CHAR); |
182 | do_test (json_ctx: &json_ctx, align1: 2, align2: 6, len: i, SMALL_CHAR); |
183 | do_test (json_ctx: &json_ctx, align1: 6, align2: 2, len: i, BIG_CHAR); |
184 | do_test (json_ctx: &json_ctx, align1: 1, align2: 7, len: i, SMALL_CHAR); |
185 | do_test (json_ctx: &json_ctx, align1: 7, align2: 1, len: i, BIG_CHAR); |
186 | do_test (json_ctx: &json_ctx, align1: 3, align2: 4, len: i, SMALL_CHAR); |
187 | do_test (json_ctx: &json_ctx, align1: 4, align2: 3, len: i, BIG_CHAR); |
188 | do_test (json_ctx: &json_ctx, align1: 5, align2: 7, len: i, SMALL_CHAR); |
189 | do_test (json_ctx: &json_ctx, align1: 7, align2: 5, len: i, SMALL_CHAR); |
190 | } |
191 | |
192 | for (i = 1; i < 2048; i += i) |
193 | { |
194 | do_test (json_ctx: &json_ctx, align1: 1, align2: 0, len: i, SMALL_CHAR); |
195 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, len: i, SMALL_CHAR); |
196 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: i, SMALL_CHAR); |
197 | do_test (json_ctx: &json_ctx, align1: i, align2: i, len: i, SMALL_CHAR); |
198 | } |
199 | |
200 | json_array_end (ctx: &json_ctx); |
201 | json_attr_object_end (ctx: &json_ctx); |
202 | json_attr_object_end (ctx: &json_ctx); |
203 | json_document_end (ctx: &json_ctx); |
204 | |
205 | return ret; |
206 | } |
207 | |
208 | #include <support/test-driver.c> |
209 | |