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 # define TEST_NAME "wcsrchr"
23 # define TEST_NAME "strrchr"
25 #include "bench-string.h"
29 # define SIMPLE_STRRCHR simple_wcsrchr
30 # define STRRCHR wcsrchr
32 # define UCHAR wchar_t
33 # define BIG_CHAR WCHAR_MAX
34 # define SMALL_CHAR 1273
36 # define SIMPLE_STRRCHR simple_strrchr
37 # define STRRCHR strrchr
39 # define UCHAR unsigned char
40 # define BIG_CHAR CHAR_MAX
41 # define SMALL_CHAR 127
44 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
45 CHAR
*SIMPLE_STRRCHR (const CHAR
*, int);
47 IMPL (SIMPLE_STRRCHR
, 0)
51 SIMPLE_STRRCHR (const CHAR
*s
, int c
)
53 const CHAR
*ret
= NULL
;
55 for (; *s
!= '\0'; ++s
)
59 return (CHAR
*) (c
== '\0' ? s
: ret
);
63 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, CHAR
*exp_res
)
65 CHAR
*res
= CALL (impl
, s
, c
);
66 size_t i
, iters
= INNER_LOOP_ITERS
;
67 timing_t start
, stop
, cur
;
71 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
78 for (i
= 0; i
< iters
; ++i
)
84 TIMING_DIFF (cur
, start
, stop
);
86 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
90 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
91 /* For wcsrchr: align here means align not in bytes,
92 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
93 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
97 CHAR
*buf
= (CHAR
*) buf1
;
100 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
103 for (i
= 0; i
< len
; ++i
)
105 buf
[align
+ i
] = (random () * random ()) & max_char
;
107 buf
[align
+ i
] = (random () * random ()) & max_char
;
110 if ((i
> pos
|| pos
>= len
) && buf
[align
+ i
] == seek_char
)
111 buf
[align
+ i
] = seek_char
+ 10 + (random () & 15);
113 buf
[align
+ len
] = 0;
117 buf
[align
+ pos
] = seek_char
;
118 result
= (CHAR
*) (buf
+ align
+ pos
);
120 else if (seek_char
== 0)
121 result
= (CHAR
*) (buf
+ align
+ len
);
125 printf ("Length %4zd, alignment in bytes %2zd:", len
, align
* sizeof (CHAR
));
127 FOR_EACH_IMPL (impl
, 0)
128 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, result
);
141 FOR_EACH_IMPL (impl
, 0)
142 printf ("\t%s", impl
->name
);
145 for (i
= 1; i
< 8; ++i
)
147 do_test (0, 16 << i
, 2048, 23, SMALL_CHAR
);
148 do_test (i
, 16 << i
, 2048, 23, SMALL_CHAR
);
151 for (i
= 1; i
< 8; ++i
)
153 do_test (i
, 64, 256, 23, SMALL_CHAR
);
154 do_test (i
, 64, 256, 23, BIG_CHAR
);
157 for (i
= 0; i
< 32; ++i
)
159 do_test (0, i
, i
+ 1, 23, SMALL_CHAR
);
160 do_test (0, i
, i
+ 1, 23, BIG_CHAR
);
163 for (i
= 1; i
< 8; ++i
)
165 do_test (0, 16 << i
, 2048, 0, SMALL_CHAR
);
166 do_test (i
, 16 << i
, 2048, 0, SMALL_CHAR
);
169 for (i
= 1; i
< 8; ++i
)
171 do_test (i
, 64, 256, 0, SMALL_CHAR
);
172 do_test (i
, 64, 256, 0, BIG_CHAR
);
175 for (i
= 0; i
< 32; ++i
)
177 do_test (0, i
, i
+ 1, 0, SMALL_CHAR
);
178 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
184 #include <support/test-driver.c>