]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure STRLEN 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 SP |
18 | |
19 | #define TEST_MAIN | |
20 | #ifndef WIDE | |
21 | # define TEST_NAME "strlen" | |
22 | #else | |
23 | # define TEST_NAME "wcslen" | |
648279f4 WD |
24 | # define generic_strlen generic_wcslen |
25 | # define memchr_strlen wcschr_wcslen | |
97020474 SP |
26 | #endif |
27 | #include "bench-string.h" | |
28 | ||
953a5a4a SP |
29 | #include "json-lib.h" |
30 | ||
97020474 SP |
31 | typedef size_t (*proto_t) (const CHAR *); |
32 | ||
5289f1f5 WD |
33 | size_t generic_strlen (const CHAR *); |
34 | size_t memchr_strlen (const CHAR *); | |
97020474 | 35 | |
5289f1f5 WD |
36 | IMPL (memchr_strlen, 0) |
37 | IMPL (generic_strlen, 0) | |
97020474 | 38 | |
5289f1f5 WD |
39 | size_t |
40 | memchr_strlen (const CHAR *p) | |
41 | { | |
42 | return (const CHAR *)MEMCHR (p, 0, PTRDIFF_MAX) - p; | |
43 | } | |
44 | ||
97020474 SP |
45 | IMPL (STRLEN, 1) |
46 | ||
47 | ||
48 | static void | |
953a5a4a | 49 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, size_t exp_len) |
97020474 | 50 | { |
afe23eb0 | 51 | size_t len = CALL (impl, s), i, iters = INNER_LOOP_ITERS_LARGE; |
44558701 WN |
52 | timing_t start, stop, cur; |
53 | ||
97020474 SP |
54 | if (len != exp_len) |
55 | { | |
56 | error (0, 0, "Wrong result in function %s %zd %zd", impl->name, | |
57 | len, exp_len); | |
58 | ret = 1; | |
59 | return; | |
60 | } | |
61 | ||
44558701 WN |
62 | TIMING_NOW (start); |
63 | for (i = 0; i < iters; ++i) | |
97020474 | 64 | { |
44558701 | 65 | CALL (impl, s); |
97020474 | 66 | } |
44558701 WN |
67 | TIMING_NOW (stop); |
68 | ||
69 | TIMING_DIFF (cur, start, stop); | |
70 | ||
953a5a4a | 71 | json_element_double (json_ctx, (double) cur / (double) iters); |
97020474 SP |
72 | } |
73 | ||
74 | static void | |
953a5a4a | 75 | do_test (json_ctx_t *json_ctx, size_t align, size_t len) |
97020474 SP |
76 | { |
77 | size_t i; | |
78 | ||
79 | align &= 63; | |
c4f50205 | 80 | if (align + sizeof (CHAR) * len >= page_size) |
97020474 SP |
81 | return; |
82 | ||
953a5a4a SP |
83 | json_element_object_begin (json_ctx); |
84 | json_attr_uint (json_ctx, "length", len); | |
85 | json_attr_uint (json_ctx, "alignment", align); | |
86 | json_array_begin (json_ctx, "timings"); | |
87 | ||
97020474 SP |
88 | |
89 | FOR_EACH_IMPL (impl, 0) | |
34f86d61 SP |
90 | { |
91 | CHAR *buf = (CHAR *) (buf1); | |
92 | ||
93 | for (i = 0; i < len; ++i) | |
94 | buf[align + i] = 1 + 11111 * i % MAX_CHAR; | |
95 | buf[align + len] = 0; | |
96 | ||
97 | do_one_test (json_ctx, impl, (CHAR *) (buf + align), len); | |
98 | alloc_bufs (); | |
99 | } | |
97020474 | 100 | |
953a5a4a SP |
101 | json_array_end (json_ctx); |
102 | json_element_object_end (json_ctx); | |
97020474 SP |
103 | } |
104 | ||
105 | int | |
106 | test_main (void) | |
107 | { | |
953a5a4a | 108 | json_ctx_t json_ctx; |
97020474 SP |
109 | size_t i; |
110 | ||
111 | test_init (); | |
112 | ||
953a5a4a SP |
113 | json_init (&json_ctx, 0, stdout); |
114 | ||
115 | json_document_begin (&json_ctx); | |
116 | json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); | |
117 | ||
118 | json_attr_object_begin (&json_ctx, "functions"); | |
119 | json_attr_object_begin (&json_ctx, TEST_NAME); | |
120 | json_attr_string (&json_ctx, "bench-variant", ""); | |
121 | ||
122 | json_array_begin (&json_ctx, "ifuncs"); | |
97020474 | 123 | FOR_EACH_IMPL (impl, 0) |
953a5a4a SP |
124 | json_element_string (&json_ctx, impl->name); |
125 | json_array_end (&json_ctx); | |
97020474 | 126 | |
953a5a4a | 127 | json_array_begin (&json_ctx, "results"); |
97020474 SP |
128 | /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/ |
129 | ||
130 | for (i = 1; i < 8; ++i) | |
131 | { | |
c4f50205 | 132 | do_test (&json_ctx, sizeof (CHAR) * i, i); |
953a5a4a | 133 | do_test (&json_ctx, 0, i); |
97020474 SP |
134 | } |
135 | ||
136 | for (i = 2; i <= 12; ++i) | |
137 | { | |
953a5a4a | 138 | do_test (&json_ctx, 0, 1 << i); |
c4f50205 JM |
139 | do_test (&json_ctx, sizeof (CHAR) * 7, 1 << i); |
140 | do_test (&json_ctx, sizeof (CHAR) * i, 1 << i); | |
141 | do_test (&json_ctx, sizeof (CHAR) * i, (size_t)((1 << i) / 1.5)); | |
97020474 SP |
142 | } |
143 | ||
953a5a4a SP |
144 | json_array_end (&json_ctx); |
145 | json_attr_object_end (&json_ctx); | |
146 | json_attr_object_end (&json_ctx); | |
147 | json_document_end (&json_ctx); | |
148 | ||
97020474 SP |
149 | return ret; |
150 | } | |
151 | ||
b598e134 | 152 | #include <support/test-driver.c> |
5289f1f5 WD |
153 | |
154 | #define libc_hidden_builtin_def(X) | |
155 | #ifndef WIDE | |
156 | # undef STRLEN | |
157 | # define STRLEN generic_strlen | |
158 | # include <string/strlen.c> | |
159 | #else | |
160 | # define WCSLEN generic_strlen | |
161 | # include <wcsmbs/wcslen.c> | |
162 | #endif |