1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2014 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"
27 # define TEST_NAME "wcschr"
29 #include "bench-string.h"
32 # ifdef USE_FOR_STRCHRNUL
33 # define STRCHR strchrnul
34 # define stupid_STRCHR stupid_STRCHRNUL
35 # define simple_STRCHR simple_STRCHRNUL
37 # define STRCHR strchr
39 # define STRLEN strlen
41 # define BIG_CHAR CHAR_MAX
42 # define MIDDLE_CHAR 127
43 # define SMALL_CHAR 23
44 # define UCHAR unsigned char
47 # define STRCHR wcschr
48 # define STRLEN wcslen
50 # define BIG_CHAR WCHAR_MAX
51 # define MIDDLE_CHAR 1121
52 # define SMALL_CHAR 851
53 # define UCHAR wchar_t
56 #ifdef USE_FOR_STRCHRNUL
57 # define NULLRET(endptr) endptr
59 # define NULLRET(endptr) NULL
63 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
66 simple_STRCHR (const CHAR
*s
, int c
)
68 for (; *s
!= (CHAR
) c
; ++s
)
70 return NULLRET ((CHAR
*) s
);
75 stupid_STRCHR (const CHAR
*s
, int c
)
77 size_t n
= STRLEN (s
) + 1;
81 return (CHAR
*) s
- 1;
82 return NULLRET ((CHAR
*) s
- 1);
85 IMPL (stupid_STRCHR
, 0)
86 IMPL (simple_STRCHR
, 0)
90 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, const CHAR
*exp_res
)
92 size_t i
, iters
= INNER_LOOP_ITERS
;
93 timing_t start
, stop
, cur
;
96 for (i
= 0; i
< iters
; ++i
)
102 TIMING_DIFF (cur
, start
, stop
);
104 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
108 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
109 /* For wcschr: align here means align not in bytes,
110 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
111 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
115 CHAR
*buf
= (CHAR
*) buf1
;
117 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
120 for (i
= 0; i
< len
; ++i
)
122 buf
[align
+ i
] = 32 + 23 * i
% max_char
;
123 if (buf
[align
+ i
] == seek_char
)
124 buf
[align
+ i
] = seek_char
+ 1;
125 else if (buf
[align
+ i
] == 0)
128 buf
[align
+ len
] = 0;
132 buf
[align
+ pos
] = seek_char
;
133 result
= buf
+ align
+ pos
;
135 else if (seek_char
== 0)
136 result
= buf
+ align
+ len
;
138 result
= NULLRET (buf
+ align
+ len
);
140 printf ("Length %4zd, alignment in bytes %2zd:",
141 pos
, align
* sizeof (CHAR
));
143 FOR_EACH_IMPL (impl
, 0)
144 do_one_test (impl
, buf
+ align
, seek_char
, result
);
157 FOR_EACH_IMPL (impl
, 0)
158 printf ("\t%s", impl
->name
);
161 for (i
= 1; i
< 8; ++i
)
163 do_test (0, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
164 do_test (i
, 16 << i
, 2048, SMALL_CHAR
, MIDDLE_CHAR
);
167 for (i
= 1; i
< 8; ++i
)
169 do_test (i
, 64, 256, SMALL_CHAR
, MIDDLE_CHAR
);
170 do_test (i
, 64, 256, SMALL_CHAR
, BIG_CHAR
);
173 for (i
= 0; i
< 32; ++i
)
175 do_test (0, i
, i
+ 1, SMALL_CHAR
, MIDDLE_CHAR
);
176 do_test (0, i
, i
+ 1, SMALL_CHAR
, BIG_CHAR
);
179 for (i
= 1; i
< 8; ++i
)
181 do_test (0, 16 << i
, 2048, 0, MIDDLE_CHAR
);
182 do_test (i
, 16 << i
, 2048, 0, MIDDLE_CHAR
);
185 for (i
= 1; i
< 8; ++i
)
187 do_test (i
, 64, 256, 0, MIDDLE_CHAR
);
188 do_test (i
, 64, 256, 0, BIG_CHAR
);
191 for (i
= 0; i
< 32; ++i
)
193 do_test (0, i
, i
+ 1, 0, MIDDLE_CHAR
);
194 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
200 #include "../test-skeleton.c"