]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure strspn functions. |
04277e02 | 2 | Copyright (C) 2013-2019 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 | |
f1ffad98 SL |
20 | #ifndef WIDE |
21 | # define TEST_NAME "strspn" | |
22 | #else | |
23 | # define TEST_NAME "wcsspn" | |
24 | #endif /* WIDE */ | |
97020474 SP |
25 | #include "bench-string.h" |
26 | ||
90d3320d WD |
27 | #define BIG_CHAR MAX_CHAR |
28 | ||
f1ffad98 | 29 | #ifndef WIDE |
f1ffad98 | 30 | # define SIMPLE_STRSPN simple_strspn |
f1ffad98 SL |
31 | # define SMALL_CHAR 127 |
32 | #else | |
f1ffad98 | 33 | # define SIMPLE_STRSPN simple_wcsspn |
f1ffad98 SL |
34 | # define SMALL_CHAR 1273 |
35 | #endif /* WIDE */ | |
36 | ||
37 | typedef size_t (*proto_t) (const CHAR *, const CHAR *); | |
38 | size_t SIMPLE_STRSPN (const CHAR *, const CHAR *); | |
f1ffad98 | 39 | |
f1ffad98 SL |
40 | IMPL (SIMPLE_STRSPN, 0) |
41 | IMPL (STRSPN, 1) | |
97020474 SP |
42 | |
43 | size_t | |
f1ffad98 | 44 | SIMPLE_STRSPN (const CHAR *s, const CHAR *acc) |
97020474 | 45 | { |
f1ffad98 SL |
46 | const CHAR *r, *str = s; |
47 | CHAR c; | |
97020474 SP |
48 | |
49 | while ((c = *s++) != '\0') | |
50 | { | |
51 | for (r = acc; *r != '\0'; ++r) | |
52 | if (*r == c) | |
53 | break; | |
54 | if (*r == '\0') | |
55 | return s - str - 1; | |
56 | } | |
57 | return s - str - 1; | |
58 | } | |
59 | ||
97020474 | 60 | static void |
f1ffad98 | 61 | do_one_test (impl_t *impl, const CHAR *s, const CHAR *acc, size_t exp_res) |
97020474 | 62 | { |
46ae0732 | 63 | size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS_MEDIUM; |
44558701 WN |
64 | timing_t start, stop, cur; |
65 | ||
97020474 SP |
66 | if (res != exp_res) |
67 | { | |
68 | error (0, 0, "Wrong result in function %s %p %p", impl->name, | |
69 | (void *) res, (void *) exp_res); | |
70 | ret = 1; | |
71 | return; | |
72 | } | |
73 | ||
44558701 WN |
74 | TIMING_NOW (start); |
75 | for (i = 0; i < iters; ++i) | |
97020474 | 76 | { |
44558701 | 77 | CALL (impl, s, acc); |
97020474 | 78 | } |
44558701 WN |
79 | TIMING_NOW (stop); |
80 | ||
81 | TIMING_DIFF (cur, start, stop); | |
82 | ||
83 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | |
97020474 SP |
84 | } |
85 | ||
86 | static void | |
87 | do_test (size_t align, size_t pos, size_t len) | |
88 | { | |
89 | size_t i; | |
f1ffad98 | 90 | CHAR *acc, *s; |
97020474 SP |
91 | |
92 | align &= 7; | |
f1ffad98 | 93 | if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || ! len) |
97020474 SP |
94 | return; |
95 | ||
f1ffad98 SL |
96 | acc = (CHAR *) (buf2) + (random () & 255); |
97 | s = (CHAR *) (buf1) + align; | |
97020474 SP |
98 | |
99 | for (i = 0; i < len; ++i) | |
100 | { | |
f1ffad98 | 101 | acc[i] = random () & BIG_CHAR; |
97020474 | 102 | if (!acc[i]) |
f1ffad98 | 103 | acc[i] = random () & BIG_CHAR; |
97020474 | 104 | if (!acc[i]) |
f1ffad98 | 105 | acc[i] = 1 + (random () & SMALL_CHAR); |
97020474 SP |
106 | } |
107 | acc[len] = '\0'; | |
108 | ||
109 | for (i = 0; i < pos; ++i) | |
110 | s[i] = acc[random () % len]; | |
f1ffad98 SL |
111 | s[pos] = random () & BIG_CHAR; |
112 | if (STRCHR (acc, s[pos])) | |
97020474 SP |
113 | s[pos] = '\0'; |
114 | else | |
115 | { | |
116 | for (i = pos + 1; i < pos + 10; ++i) | |
f1ffad98 | 117 | s[i] = random () & BIG_CHAR; |
97020474 SP |
118 | s[i] = '\0'; |
119 | } | |
120 | ||
44558701 | 121 | printf ("Length %4zd, alignment %2zd, acc len %2zd:", pos, align, len); |
97020474 SP |
122 | |
123 | FOR_EACH_IMPL (impl, 0) | |
124 | do_one_test (impl, s, acc, pos); | |
125 | ||
44558701 | 126 | putchar ('\n'); |
97020474 SP |
127 | } |
128 | ||
129 | int | |
130 | test_main (void) | |
131 | { | |
132 | size_t i; | |
133 | ||
134 | test_init (); | |
135 | ||
136 | printf ("%32s", ""); | |
137 | FOR_EACH_IMPL (impl, 0) | |
138 | printf ("\t%s", impl->name); | |
139 | putchar ('\n'); | |
140 | ||
141 | for (i = 0; i < 32; ++i) | |
142 | { | |
143 | do_test (0, 512, i); | |
144 | do_test (i, 512, i); | |
145 | } | |
146 | ||
147 | for (i = 1; i < 8; ++i) | |
148 | { | |
149 | do_test (0, 16 << i, 4); | |
150 | do_test (i, 16 << i, 4); | |
151 | } | |
152 | ||
153 | for (i = 1; i < 8; ++i) | |
154 | do_test (i, 64, 10); | |
155 | ||
156 | for (i = 0; i < 64; ++i) | |
157 | do_test (0, i, 6); | |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
b598e134 | 162 | #include <support/test-driver.c> |