1 /* Measure STRCHR functions.
2 Copyright (C) 2013-2022 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/>. */
21 # define TEST_NAME "wcsrchr"
23 # define TEST_NAME "strrchr"
25 #include "bench-string.h"
27 #define BIG_CHAR MAX_CHAR
30 # define SIMPLE_STRRCHR simple_wcsrchr
31 # define SMALL_CHAR 1273
33 # define SIMPLE_STRRCHR simple_strrchr
34 # define SMALL_CHAR 127
37 typedef CHAR
*(*proto_t
) (const CHAR
*, int);
38 CHAR
*SIMPLE_STRRCHR (const CHAR
*, int);
40 IMPL (SIMPLE_STRRCHR
, 0)
44 SIMPLE_STRRCHR (const CHAR
*s
, int c
)
46 const CHAR
*ret
= NULL
;
48 for (; *s
!= '\0'; ++s
)
52 return (CHAR
*) (c
== '\0' ? s
: ret
);
56 do_one_test (impl_t
*impl
, const CHAR
*s
, int c
, CHAR
*exp_res
)
58 CHAR
*res
= CALL (impl
, s
, c
);
59 size_t i
, iters
= INNER_LOOP_ITERS8
;
60 timing_t start
, stop
, cur
;
64 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
71 for (i
= 0; i
< iters
; ++i
)
77 TIMING_DIFF (cur
, start
, stop
);
79 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
83 do_test (size_t align
, size_t pos
, size_t len
, int seek_char
, int max_char
)
84 /* For wcsrchr: align here means align not in bytes,
85 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
86 len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
90 CHAR
*buf
= (CHAR
*) buf1
;
93 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
96 for (i
= 0; i
< len
; ++i
)
98 buf
[align
+ i
] = (random () * random ()) & max_char
;
100 buf
[align
+ i
] = (random () * random ()) & max_char
;
103 if ((i
> pos
|| pos
>= len
) && buf
[align
+ i
] == seek_char
)
104 buf
[align
+ i
] = seek_char
+ 10 + (random () & 15);
106 buf
[align
+ len
] = 0;
110 buf
[align
+ pos
] = seek_char
;
111 result
= (CHAR
*) (buf
+ align
+ pos
);
113 else if (seek_char
== 0)
114 result
= (CHAR
*) (buf
+ align
+ len
);
118 printf ("Length %4zd, alignment in bytes %2zd:", len
, align
* sizeof (CHAR
));
120 FOR_EACH_IMPL (impl
, 0)
121 do_one_test (impl
, (CHAR
*) (buf
+ align
), seek_char
, result
);
134 FOR_EACH_IMPL (impl
, 0)
135 printf ("\t%s", impl
->name
);
138 for (i
= 1; i
< 8; ++i
)
140 do_test (0, 16 << i
, 2048, 23, SMALL_CHAR
);
141 do_test (i
, 16 << i
, 2048, 23, SMALL_CHAR
);
144 for (i
= 1; i
< 8; ++i
)
146 do_test (i
, 64, 256, 23, SMALL_CHAR
);
147 do_test (i
, 64, 256, 23, BIG_CHAR
);
150 for (i
= 0; i
< 32; ++i
)
152 do_test (0, i
, i
+ 1, 23, SMALL_CHAR
);
153 do_test (0, i
, i
+ 1, 23, BIG_CHAR
);
156 for (i
= 1; i
< 8; ++i
)
158 do_test (0, 16 << i
, 2048, 0, SMALL_CHAR
);
159 do_test (i
, 16 << i
, 2048, 0, SMALL_CHAR
);
162 for (i
= 1; i
< 8; ++i
)
164 do_test (i
, 64, 256, 0, SMALL_CHAR
);
165 do_test (i
, 64, 256, 0, BIG_CHAR
);
168 for (i
= 0; i
< 32; ++i
)
170 do_test (0, i
, i
+ 1, 0, SMALL_CHAR
);
171 do_test (0, i
, i
+ 1, 0, BIG_CHAR
);
177 #include <support/test-driver.c>