1 | /* Measure strpbrk 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 | #ifndef WIDE |
22 | # define SMALL_CHAR 127 |
23 | #else |
24 | # define SMALL_CHAR 1273 |
25 | #endif /* WIDE */ |
26 | |
27 | #ifndef STRPBRK_RESULT |
28 | # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL) |
29 | # define RES_TYPE CHAR * |
30 | # define TEST_MAIN |
31 | # ifndef WIDE |
32 | # define TEST_NAME "strpbrk" |
33 | # else |
34 | # define TEST_NAME "wcspbrk" |
35 | # endif /* WIDE */ |
36 | # include "bench-string.h" |
37 | |
38 | typedef CHAR *(*proto_t) (const CHAR *, const CHAR *); |
39 | |
40 | IMPL (STRPBRK, 1) |
41 | |
42 | #endif /* !STRPBRK_RESULT */ |
43 | |
44 | #include "json-lib.h" |
45 | |
46 | static void |
47 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s, |
48 | const CHAR *rej, RES_TYPE exp_res) |
49 | { |
50 | RES_TYPE res = CALL (impl, s, rej); |
51 | size_t i, iters = INNER_LOOP_ITERS8 / CHARBYTES; |
52 | timing_t start, stop, cur; |
53 | |
54 | if (res != exp_res) |
55 | { |
56 | error (status: 0, errnum: 0, format: "Wrong result in function %s %p %p" , impl->name, |
57 | (void *) res, (void *) exp_res); |
58 | ret = 1; |
59 | return; |
60 | } |
61 | |
62 | TIMING_NOW (start); |
63 | for (i = 0; i < iters; ++i) |
64 | { |
65 | CALL (impl, s, rej); |
66 | } |
67 | TIMING_NOW (stop); |
68 | |
69 | TIMING_DIFF (cur, start, stop); |
70 | |
71 | json_element_double (ctx: json_ctx, d: (double)cur / (double)iters); |
72 | } |
73 | |
74 | static void |
75 | do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t pos, |
76 | size_t len) |
77 | { |
78 | size_t i; |
79 | int c; |
80 | RES_TYPE result; |
81 | CHAR *rej, *s; |
82 | |
83 | align1 &= 7; |
84 | if ((align1 + pos + 10) * sizeof (CHAR) >= page_size || len > 240) |
85 | return; |
86 | if ((align2 + len) * sizeof (CHAR) >= page_size) |
87 | return; |
88 | |
89 | rej = (CHAR *) (buf2) + align2; |
90 | s = (CHAR *) (buf1) + align1; |
91 | |
92 | for (i = 0; i < len; ++i) |
93 | { |
94 | rej[i] = random () & BIG_CHAR; |
95 | if (!rej[i]) |
96 | rej[i] = random () & BIG_CHAR; |
97 | if (!rej[i]) |
98 | rej[i] = 1 + (random () & SMALL_CHAR); |
99 | } |
100 | rej[len] = '\0'; |
101 | for (c = 1; c <= BIG_CHAR; ++c) |
102 | if (STRCHR (rej, c) == NULL) |
103 | break; |
104 | |
105 | for (i = 0; i < pos; ++i) |
106 | { |
107 | s[i] = random () & BIG_CHAR; |
108 | if (STRCHR (rej, s[i])) |
109 | { |
110 | s[i] = random () & BIG_CHAR; |
111 | if (STRCHR (rej, s[i])) |
112 | s[i] = c; |
113 | } |
114 | } |
115 | s[pos] = rej[random () % (len + 1)]; |
116 | if (s[pos]) |
117 | { |
118 | for (i = pos + 1; i < pos + 10; ++i) |
119 | s[i] = random () & BIG_CHAR; |
120 | s[i] = '\0'; |
121 | } |
122 | result = STRPBRK_RESULT (s, pos); |
123 | |
124 | json_element_object_begin (ctx: json_ctx); |
125 | json_attr_uint (ctx: json_ctx, name: "len" , d: len); |
126 | json_attr_uint (ctx: json_ctx, name: "pos" , d: pos); |
127 | json_attr_uint (ctx: json_ctx, name: "align1" , d: align1); |
128 | json_attr_uint (ctx: json_ctx, name: "align2" , d: align2); |
129 | json_array_begin (ctx: json_ctx, name: "timings" ); |
130 | |
131 | FOR_EACH_IMPL (impl, 0) |
132 | do_one_test (json_ctx, impl, s, rej, exp_res: result); |
133 | |
134 | json_array_end (ctx: json_ctx); |
135 | json_element_object_end (ctx: json_ctx); |
136 | } |
137 | |
138 | int |
139 | test_main (void) |
140 | { |
141 | json_ctx_t json_ctx; |
142 | size_t i; |
143 | |
144 | test_init (); |
145 | |
146 | json_init (ctx: &json_ctx, indent_level: 0, stdout); |
147 | |
148 | json_document_begin (ctx: &json_ctx); |
149 | json_attr_string (ctx: &json_ctx, name: "timing_type" , TIMING_TYPE); |
150 | |
151 | json_attr_object_begin (ctx: &json_ctx, name: "functions" ); |
152 | json_attr_object_begin (ctx: &json_ctx, TEST_NAME); |
153 | json_attr_string (ctx: &json_ctx, name: "bench-variant" , s: "" ); |
154 | |
155 | json_array_begin (ctx: &json_ctx, name: "ifuncs" ); |
156 | FOR_EACH_IMPL (impl, 0) |
157 | json_element_string (ctx: &json_ctx, s: impl->name); |
158 | json_array_end (ctx: &json_ctx); |
159 | |
160 | json_array_begin (ctx: &json_ctx, name: "results" ); |
161 | |
162 | |
163 | for (i = 0; i < 32; ++i) |
164 | { |
165 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, pos: 512, len: i); |
166 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, pos: 512, len: i); |
167 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, pos: 512, len: i); |
168 | do_test (json_ctx: &json_ctx, align1: i, align2: i, pos: 512, len: i); |
169 | |
170 | } |
171 | |
172 | for (i = 1; i < 8; ++i) |
173 | { |
174 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, pos: 16 << i, len: 4); |
175 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, pos: 16 << i, len: 4); |
176 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, pos: 16 << i, len: 4); |
177 | do_test (json_ctx: &json_ctx, align1: i, align2: i, pos: 16 << i, len: 4); |
178 | } |
179 | |
180 | for (i = 1; i < 8; ++i) |
181 | { |
182 | do_test (json_ctx: &json_ctx, align1: i, align2: 0, pos: 64, len: 10); |
183 | do_test (json_ctx: &json_ctx, align1: i, align2: i, pos: 64, len: 10); |
184 | } |
185 | |
186 | for (i = 0; i < 64; ++i) |
187 | { |
188 | do_test (json_ctx: &json_ctx, align1: 0, align2: 0, pos: i, len: 6); |
189 | do_test (json_ctx: &json_ctx, align1: 0, align2: i, pos: i, len: 6); |
190 | } |
191 | |
192 | json_array_end (ctx: &json_ctx); |
193 | json_attr_object_end (ctx: &json_ctx); |
194 | json_attr_object_end (ctx: &json_ctx); |
195 | json_document_end (ctx: &json_ctx); |
196 | |
197 | return ret; |
198 | } |
199 | |
200 | #include <support/test-driver.c> |
201 | |