Add x86 init-arch to nptl
[glibc.git] / benchtests / bench-strlen.c
blob63b1e9368be1979edf7cca5f60650144e8ff8252
1 /* Measure STRLEN 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/>. */
19 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strlen"
22 #else
23 # define TEST_NAME "wcslen"
24 #endif
25 #include "bench-string.h"
27 #ifndef WIDE
28 # define STRLEN strlen
29 # define CHAR char
30 # define MAX_CHAR CHAR_MAX
31 #else
32 # include <wchar.h>
33 # define STRLEN wcslen
34 # define CHAR wchar_t
35 # define MAX_CHAR WCHAR_MAX
36 #endif
38 typedef size_t (*proto_t) (const CHAR *);
40 size_t
41 simple_STRLEN (const CHAR *s)
43 const CHAR *p;
45 for (p = s; *p; ++p);
46 return p - s;
49 #ifndef WIDE
50 size_t
51 builtin_strlen (const CHAR *p)
53 return __builtin_strlen (p);
55 IMPL (builtin_strlen, 0)
56 #endif
58 IMPL (simple_STRLEN, 0)
59 IMPL (STRLEN, 1)
62 static void
63 do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
65 size_t len = CALL (impl, s);
66 if (len != exp_len)
68 error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
69 len, exp_len);
70 ret = 1;
71 return;
74 if (HP_TIMING_AVAIL)
76 hp_timing_t start __attribute ((unused));
77 hp_timing_t stop __attribute ((unused));
78 hp_timing_t best_time = ~ (hp_timing_t) 0;
79 size_t i;
81 for (i = 0; i < 32; ++i)
83 HP_TIMING_NOW (start);
84 CALL (impl, s);
85 HP_TIMING_NOW (stop);
86 HP_TIMING_BEST (best_time, start, stop);
89 printf ("\t%zd", (size_t) best_time);
93 static void
94 do_test (size_t align, size_t len)
96 size_t i;
98 align &= 63;
99 if (align + sizeof(CHAR) * len >= page_size)
100 return;
102 CHAR *buf = (CHAR *) (buf1);
104 for (i = 0; i < len; ++i)
105 buf[align + i] = 1 + 11111 * i % MAX_CHAR;
106 buf[align + len] = 0;
108 if (HP_TIMING_AVAIL)
109 printf ("Length %4zd, alignment %2zd:", len, align);
111 FOR_EACH_IMPL (impl, 0)
112 do_one_test (impl, (CHAR *) (buf + align), len);
114 if (HP_TIMING_AVAIL)
115 putchar ('\n');
119 test_main (void)
121 size_t i;
123 test_init ();
125 printf ("%20s", "");
126 FOR_EACH_IMPL (impl, 0)
127 printf ("\t%s", impl->name);
128 putchar ('\n');
130 /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
132 for (i = 1; i < 8; ++i)
134 do_test (sizeof(CHAR) * i, i);
135 do_test (0, i);
138 for (i = 2; i <= 12; ++i)
140 do_test (0, 1 << i);
141 do_test (sizeof(CHAR) * 7, 1 << i);
142 do_test (sizeof(CHAR) * i, 1 << i);
143 do_test (sizeof(CHAR) * i, (size_t)((1 << i) / 1.5));
146 return ret;
149 #include "../test-skeleton.c"