1 /* Measure STRCHR functions.
2 Copyright (C) 2013 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
);
68 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
76 hp_timing_t start
__attribute ((unused
));
77 hp_timing_t stop
__attribute ((unused
));
78 hp_timing_t best_time
= ~ (hp_timing_t
) 0;
81 for (i
= 0; i
< 32; ++i
)
83 HP_TIMING_NOW (start
);
86 HP_TIMING_BEST (best_time
, start
, stop
);
89 printf ("\t%zd", (size_t) best_time
);
94 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
95 /* For wcsrchr: align here means align not in bytes,
96 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
97 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
101 CHAR
*buf
= (CHAR
*) buf1
;
104 if ( (align
+ len
) * sizeof(CHAR
) >= page_size
)
107 for (i
= 0; i
< len
; ++i
)
109 buf
[align
+ i
] = (random () * random ()) & max_char
;
111 buf
[align
+ i
] = (random () * random ()) & max_char
;
114 if ((i
> pos
|| pos
>= len
) && buf
[align
+ i
] == seek_char
)
115 buf
[align
+ i
] = seek_char
+ 10 + (random () & 15);
117 buf
[align
+ len
] = 0;
121 buf
[align
+ pos
] = seek_char
;
122 result
= (CHAR
*) (buf
+ align
+ pos
);
124 else if (seek_char
== 0)
125 result
= (CHAR
*) (buf
+ align
+ len
);
130 printf ("Length %4zd, alignment in bytes %2zd:", pos
, align
* sizeof(CHAR
));
132 FOR_EACH_IMPL (impl
, 0)
133 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, result
);
147 FOR_EACH_IMPL (impl
, 0)
148 printf ("\t%s", impl
->name
);
151 for (i
= 1; i
< 8; ++i
)
153 do_test (0, 16 << i
, 2048, 23, SMALL_CHAR
);
154 do_test (i
, 16 << i
, 2048, 23, SMALL_CHAR
);
157 for (i
= 1; i
< 8; ++i
)
159 do_test (i
, 64, 256, 23, SMALL_CHAR
);
160 do_test (i
, 64, 256, 23, BIG_CHAR
);
163 for (i
= 0; i
< 32; ++i
)
165 do_test (0, i
, i
+ 1, 23, SMALL_CHAR
);
166 do_test (0, i
, i
+ 1, 23, BIG_CHAR
);
169 for (i
= 1; i
< 8; ++i
)
171 do_test (0, 16 << i
, 2048, 0, SMALL_CHAR
);
172 do_test (i
, 16 << i
, 2048, 0, SMALL_CHAR
);
175 for (i
= 1; i
< 8; ++i
)
177 do_test (i
, 64, 256, 0, SMALL_CHAR
);
178 do_test (i
, 64, 256, 0, BIG_CHAR
);
181 for (i
= 0; i
< 32; ++i
)
183 do_test (0, i
, i
+ 1, 0, SMALL_CHAR
);
184 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
190 #include "../test-skeleton.c"