Update copyright notices with scripts/update-copyrights
[glibc.git] / benchtests / bench-strrchr.c
blobadceddee0751e97d651d1644af7d2ec2c48dd8f8
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/>. */
19 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcsrchr"
22 #else
23 # define TEST_NAME "strrchr"
24 #endif
25 #include "bench-string.h"
27 #ifdef WIDE
28 # include <wchar.h>
29 # define SIMPLE_STRRCHR simple_wcsrchr
30 # define STRRCHR wcsrchr
31 # define CHAR wchar_t
32 # define UCHAR wchar_t
33 # define BIG_CHAR WCHAR_MAX
34 # define SMALL_CHAR 1273
35 #else
36 # define SIMPLE_STRRCHR simple_strrchr
37 # define STRRCHR strrchr
38 # define CHAR char
39 # define UCHAR unsigned char
40 # define BIG_CHAR CHAR_MAX
41 # define SMALL_CHAR 127
42 #endif
44 typedef CHAR *(*proto_t) (const CHAR *, int);
45 CHAR *SIMPLE_STRRCHR (const CHAR *, int);
47 IMPL (SIMPLE_STRRCHR, 0)
48 IMPL (STRRCHR, 1)
50 CHAR *
51 SIMPLE_STRRCHR (const CHAR *s, int c)
53 const CHAR *ret = NULL;
55 for (; *s != '\0'; ++s)
56 if (*s == (CHAR) c)
57 ret = s;
59 return (CHAR *) (c == '\0' ? s : ret);
62 static void
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;
69 if (res != exp_res)
71 error (0, 0, "Wrong result in function %s %p %p", impl->name,
72 res, exp_res);
73 ret = 1;
74 return;
77 TIMING_NOW (start);
78 for (i = 0; i < iters; ++i)
80 CALL (impl, s, c);
82 TIMING_NOW (stop);
84 TIMING_DIFF (cur, start, stop);
86 TIMING_PRINT_MEAN ((double) cur, (double) iters);
89 static void
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. */
95 size_t i;
96 CHAR *result;
97 CHAR *buf = (CHAR *) buf1;
99 align &= 7;
100 if ( (align + len) * sizeof(CHAR) >= page_size)
101 return;
103 for (i = 0; i < len; ++i)
105 buf[align + i] = (random () * random ()) & max_char;
106 if (!buf[align + i])
107 buf[align + i] = (random () * random ()) & max_char;
108 if (!buf[align + i])
109 buf[align + i] = 1;
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;
115 if (pos < len)
117 buf[align + pos] = seek_char;
118 result = (CHAR *) (buf + align + pos);
120 else if (seek_char == 0)
121 result = (CHAR *) (buf + align + len);
122 else
123 result = NULL;
125 printf ("Length %4zd, alignment in bytes %2zd:", pos, align * sizeof(CHAR));
127 FOR_EACH_IMPL (impl, 0)
128 do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
130 putchar ('\n');
134 test_main (void)
136 size_t i;
138 test_init ();
140 printf ("%20s", "");
141 FOR_EACH_IMPL (impl, 0)
142 printf ("\t%s", impl->name);
143 putchar ('\n');
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);
181 return ret;
184 #include "../test-skeleton.c"