]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure STRLEN functions. |
f7a9f785 | 2 | Copyright (C) 2013-2016 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 | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #define TEST_MAIN | |
20 | #ifndef WIDE | |
21 | # define TEST_NAME "strlen" | |
22 | #else | |
23 | # define TEST_NAME "wcslen" | |
24 | #endif | |
25 | #include "bench-string.h" | |
26 | ||
27 | #ifndef WIDE | |
28 | # define STRLEN strlen | |
29 | # define CHAR char | |
30 | # define MAX_CHAR CHAR_MAX | |
31 | #else | |
32 | # include <wchar.h> | |
33 | # define STRLEN wcslen | |
34 | # define CHAR wchar_t | |
35 | # define MAX_CHAR WCHAR_MAX | |
36 | #endif | |
37 | ||
38 | typedef size_t (*proto_t) (const CHAR *); | |
39 | ||
40 | size_t | |
41 | simple_STRLEN (const CHAR *s) | |
42 | { | |
43 | const CHAR *p; | |
44 | ||
45 | for (p = s; *p; ++p); | |
46 | return p - s; | |
47 | } | |
48 | ||
49 | #ifndef WIDE | |
50 | size_t | |
51 | builtin_strlen (const CHAR *p) | |
52 | { | |
53 | return __builtin_strlen (p); | |
54 | } | |
55 | IMPL (builtin_strlen, 0) | |
56 | #endif | |
57 | ||
58 | IMPL (simple_STRLEN, 0) | |
59 | IMPL (STRLEN, 1) | |
60 | ||
61 | ||
62 | static void | |
63 | do_one_test (impl_t *impl, const CHAR *s, size_t exp_len) | |
64 | { | |
44558701 WN |
65 | size_t len = CALL (impl, s), i, iters = INNER_LOOP_ITERS; |
66 | timing_t start, stop, cur; | |
67 | ||
97020474 SP |
68 | if (len != exp_len) |
69 | { | |
70 | error (0, 0, "Wrong result in function %s %zd %zd", impl->name, | |
71 | len, exp_len); | |
72 | ret = 1; | |
73 | return; | |
74 | } | |
75 | ||
44558701 WN |
76 | TIMING_NOW (start); |
77 | for (i = 0; i < iters; ++i) | |
97020474 | 78 | { |
44558701 | 79 | CALL (impl, s); |
97020474 | 80 | } |
44558701 WN |
81 | TIMING_NOW (stop); |
82 | ||
83 | TIMING_DIFF (cur, start, stop); | |
84 | ||
85 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | |
97020474 SP |
86 | } |
87 | ||
88 | static void | |
89 | do_test (size_t align, size_t len) | |
90 | { | |
91 | size_t i; | |
92 | ||
93 | align &= 63; | |
94 | if (align + sizeof(CHAR) * len >= page_size) | |
95 | return; | |
96 | ||
97 | CHAR *buf = (CHAR *) (buf1); | |
98 | ||
99 | for (i = 0; i < len; ++i) | |
100 | buf[align + i] = 1 + 11111 * i % MAX_CHAR; | |
101 | buf[align + len] = 0; | |
102 | ||
44558701 | 103 | printf ("Length %4zd, alignment %2zd:", len, align); |
97020474 SP |
104 | |
105 | FOR_EACH_IMPL (impl, 0) | |
106 | do_one_test (impl, (CHAR *) (buf + align), len); | |
107 | ||
44558701 | 108 | putchar ('\n'); |
97020474 SP |
109 | } |
110 | ||
111 | int | |
112 | test_main (void) | |
113 | { | |
114 | size_t i; | |
115 | ||
116 | test_init (); | |
117 | ||
118 | printf ("%20s", ""); | |
119 | FOR_EACH_IMPL (impl, 0) | |
120 | printf ("\t%s", impl->name); | |
121 | putchar ('\n'); | |
122 | ||
123 | /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/ | |
124 | ||
125 | for (i = 1; i < 8; ++i) | |
126 | { | |
127 | do_test (sizeof(CHAR) * i, i); | |
128 | do_test (0, i); | |
129 | } | |
130 | ||
131 | for (i = 2; i <= 12; ++i) | |
132 | { | |
133 | do_test (0, 1 << i); | |
134 | do_test (sizeof(CHAR) * 7, 1 << i); | |
135 | do_test (sizeof(CHAR) * i, 1 << i); | |
136 | do_test (sizeof(CHAR) * i, (size_t)((1 << i) / 1.5)); | |
137 | } | |
138 | ||
139 | return ret; | |
140 | } | |
141 | ||
b598e134 | 142 | #include <support/test-driver.c> |