1 /* Measure memchr functions.
2 Copyright (C) 2013-2019 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 <https://www.gnu.org/licenses/>. */
20 # define SMALL_CHAR 127
22 # define SMALL_CHAR 1273
25 #ifndef USE_AS_MEMRCHR
28 # define TEST_NAME "memchr"
30 # define TEST_NAME "wmemchr"
32 # include "bench-string.h"
35 # define SIMPLE_MEMCHR simple_memchr
37 # define SIMPLE_MEMCHR simple_wmemchr
40 typedef CHAR
*(*proto_t
) (const CHAR
*, int, size_t);
41 CHAR
*SIMPLE_MEMCHR (const CHAR
*, int, size_t);
43 IMPL (SIMPLE_MEMCHR
, 0)
47 SIMPLE_MEMCHR (const CHAR
*s
, int c
, size_t n
)
51 return (CHAR
*) s
- 1;
54 #endif /* !USE_AS_MEMRCHR */
57 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, size_t n
)
59 size_t i
, iters
= INNER_LOOP_ITERS_LARGE
;
60 timing_t start
, stop
, cur
;
63 for (i
= 0; i
< iters
; ++i
)
69 TIMING_DIFF (cur
, start
, stop
);
71 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
75 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
)
80 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
83 CHAR
*buf
= (CHAR
*) (buf1
);
85 for (i
= 0; i
< len
; ++i
)
87 buf
[align
+ i
] = 1 + 23 * i
% SMALL_CHAR
;
88 if (buf
[align
+ i
] == seek_char
)
89 buf
[align
+ i
] = seek_char
+ 1;
95 buf
[align
+ pos
] = seek_char
;
96 buf
[align
+ len
] = -seek_char
;
100 buf
[align
+ len
] = seek_char
;
103 printf ("Length %4zd, position %4zd, alignment %2zd:",
106 FOR_EACH_IMPL (impl
, 0)
107 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, len
);
120 FOR_EACH_IMPL (impl
, 0)
121 printf ("\t%s", impl
->name
);
124 for (i
= 1; i
< 8; ++i
)
126 do_test (0, 16 << i
, 2048, 23);
127 do_test (i
, 64, 256, 23);
128 do_test (0, 16 << i
, 2048, 0);
129 do_test (i
, 64, 256, 0);
130 #ifdef USE_AS_MEMRCHR
131 /* Also test the position close to the beginning for memrchr. */
132 do_test (0, i
, 256, 23);
133 do_test (0, i
, 256, 0);
134 do_test (i
, i
, 256, 23);
135 do_test (i
, i
, 256, 0);
138 for (i
= 1; i
< 32; ++i
)
140 do_test (0, i
, i
+ 1, 23);
141 do_test (0, i
, i
+ 1, 0);
142 do_test (i
, i
, i
+ 1, 23);
143 do_test (i
, i
, i
+ 1, 0);
144 #ifdef USE_AS_MEMRCHR
145 /* Also test the position close to the beginning for memrchr. */
146 do_test (0, 1, i
+ 1, 23);
147 do_test (0, 2, i
+ 1, 0);
154 #include <support/test-driver.c>