1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 # ifdef USE_FOR_STRCHRNUL
22 # define TEST_NAME "strchrnul"
24 # define TEST_NAME "strchr"
25 # endif /* !USE_FOR_STRCHRNUL */
27 # ifdef USE_FOR_STRCHRNUL
28 # define TEST_NAME "wcschrnul"
30 # define TEST_NAME "wcschr"
31 # endif /* !USE_FOR_STRCHRNUL */
33 #include "bench-string.h"
36 # ifdef USE_FOR_STRCHRNUL
37 # define STRCHR strchrnul
38 # define stupid_STRCHR stupid_STRCHRNUL
39 # define simple_STRCHR simple_STRCHRNUL
41 # define STRCHR strchr
42 # endif /* !USE_FOR_STRCHRNUL */
43 # define STRLEN strlen
45 # define BIG_CHAR CHAR_MAX
46 # define MIDDLE_CHAR 127
47 # define SMALL_CHAR 23
48 # define UCHAR unsigned char
51 # ifdef USE_FOR_STRCHRNUL
52 # define STRCHR wcschrnul
53 # define stupid_STRCHR stupid_WCSCHRNUL
54 # define simple_STRCHR simple_WCSCHRNUL
56 # define STRCHR wcschr
57 # endif /* !USE_FOR_STRCHRNUL */
58 # define STRLEN wcslen
60 # define BIG_CHAR WCHAR_MAX
61 # define MIDDLE_CHAR 1121
62 # define SMALL_CHAR 851
63 # define UCHAR wchar_t
66 #ifdef USE_FOR_STRCHRNUL
67 # define NULLRET(endptr) endptr
69 # define NULLRET(endptr) NULL
70 #endif /* !USE_FOR_STRCHRNUL */
73 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
76 simple_STRCHR (const CHAR
*s
, int c
)
78 for (; *s
!= (CHAR
) c
; ++s
)
80 return NULLRET ((CHAR
*) s
);
85 stupid_STRCHR (const CHAR
*s
, int c
)
87 size_t n
= STRLEN (s
) + 1;
91 return (CHAR
*) s
- 1;
92 return NULLRET ((CHAR
*) s
- 1);
95 IMPL (stupid_STRCHR
, 0)
96 IMPL (simple_STRCHR
, 0)
100 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
102 size_t i
, iters
= INNER_LOOP_ITERS
;
103 timing_t start
, stop
, cur
;
106 for (i
= 0; i
< iters
; ++i
)
112 TIMING_DIFF (cur
, start
, stop
);
114 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
118 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
119 /* For wcschr: align here means align not in bytes,
120 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
121 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
125 CHAR
*buf
= (CHAR
*) buf1
;
127 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
130 for (i
= 0; i
< len
; ++i
)
132 buf
[align
+ i
] = 32 + 23 * i
% max_char
;
133 if (buf
[align
+ i
] == seek_char
)
134 buf
[align
+ i
] = seek_char
+ 1;
135 else if (buf
[align
+ i
] == 0)
138 buf
[align
+ len
] = 0;
142 buf
[align
+ pos
] = seek_char
;
143 result
= buf
+ align
+ pos
;
145 else if (seek_char
== 0)
146 result
= buf
+ align
+ len
;
148 result
= NULLRET (buf
+ align
+ len
);
150 printf ("Length %4zd, alignment in bytes %2zd:",
151 pos
, align
* sizeof (CHAR
));
153 FOR_EACH_IMPL (impl
, 0)
154 do_one_test (impl
, buf
+ align
, seek_char
, result
);
167 FOR_EACH_IMPL (impl
, 0)
168 printf ("\t%s", impl
->name
);
171 for (i
= 1; i
< 8; ++i
)
173 do_test (0, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
174 do_test (i
, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
177 for (i
= 1; i
< 8; ++i
)
179 do_test (i
, 64, 256, SMALL_CHAR
, MIDDLE_CHAR
);
180 do_test (i
, 64, 256, SMALL_CHAR
, BIG_CHAR
);
183 for (i
= 0; i
< 32; ++i
)
185 do_test (0, i
, i
+ 1, SMALL_CHAR
, MIDDLE_CHAR
);
186 do_test (0, i
, i
+ 1, SMALL_CHAR
, BIG_CHAR
);
189 for (i
= 1; i
< 8; ++i
)
191 do_test (0, 16 << i
, 2048, 0, MIDDLE_CHAR
);
192 do_test (i
, 16 << i
, 2048, 0, MIDDLE_CHAR
);
195 for (i
= 1; i
< 8; ++i
)
197 do_test (i
, 64, 256, 0, MIDDLE_CHAR
);
198 do_test (i
, 64, 256, 0, BIG_CHAR
);
201 for (i
= 0; i
< 32; ++i
)
203 do_test (0, i
, i
+ 1, 0, MIDDLE_CHAR
);
204 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
210 #include <support/test-driver.c>