]> git.ipfire.org Git - thirdparty/glibc.git/blame - benchtests/bench-memcmp.c
Copy over string performance tests into benchtests
[thirdparty/glibc.git] / benchtests / bench-memcmp.c
CommitLineData
97020474
SP
1/* Measure memcmp functions.
2 Copyright (C) 2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#define TEST_MAIN
20#ifdef WIDE
21# define TEST_NAME "wmemcmp"
22#else
23# define TEST_NAME "memcmp"
24#endif
25#include "bench-string.h"
26#ifdef WIDE
27# include <inttypes.h>
28# include <wchar.h>
29
30# define MEMCMP wmemcmp
31# define MEMCPY wmemcpy
32# define SIMPLE_MEMCMP simple_wmemcmp
33# define CHAR wchar_t
34# define UCHAR wchar_t
35# define CHARBYTES 4
36# define CHAR__MIN WCHAR_MIN
37# define CHAR__MAX WCHAR_MAX
38int
39simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
40{
41 int ret = 0;
42 /* Warning!
43 wmemcmp has to use SIGNED comparison for elements.
44 memcmp has to use UNSIGNED comparison for elemnts.
45 */
46 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
47 return ret;
48}
49#else
50# include <limits.h>
51
52# define MEMCMP memcmp
53# define MEMCPY memcpy
54# define SIMPLE_MEMCMP simple_memcmp
55# define CHAR char
56# define MAX_CHAR 255
57# define UCHAR unsigned char
58# define CHARBYTES 1
59# define CHAR__MIN CHAR_MIN
60# define CHAR__MAX CHAR_MAX
61
62int
63simple_memcmp (const char *s1, const char *s2, size_t n)
64{
65 int ret = 0;
66
67 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
68 return ret;
69}
70#endif
71
72typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
73
74IMPL (SIMPLE_MEMCMP, 0)
75IMPL (MEMCMP, 1)
76
77static void
78do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
79 int exp_result)
80{
81 if (HP_TIMING_AVAIL)
82 {
83 hp_timing_t start __attribute ((unused));
84 hp_timing_t stop __attribute ((unused));
85 hp_timing_t best_time = ~ (hp_timing_t) 0;
86 size_t i;
87
88 for (i = 0; i < 32; ++i)
89 {
90 HP_TIMING_NOW (start);
91 CALL (impl, s1, s2, len);
92 HP_TIMING_NOW (stop);
93 HP_TIMING_BEST (best_time, start, stop);
94 }
95
96 printf ("\t%zd", (size_t) best_time);
97 }
98}
99
100static void
101do_test (size_t align1, size_t align2, size_t len, int exp_result)
102{
103 size_t i;
104 CHAR *s1, *s2;
105
106 if (len == 0)
107 return;
108
109 align1 &= 63;
110 if (align1 + (len + 1) * CHARBYTES >= page_size)
111 return;
112
113 align2 &= 63;
114 if (align2 + (len + 1) * CHARBYTES >= page_size)
115 return;
116
117 s1 = (CHAR *) (buf1 + align1);
118 s2 = (CHAR *) (buf2 + align2);
119
120 for (i = 0; i < len; i++)
121 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
122
123 s1[len] = align1;
124 s2[len] = align2;
125 s2[len - 1] -= exp_result;
126
127 if (HP_TIMING_AVAIL)
128 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
129
130 FOR_EACH_IMPL (impl, 0)
131 do_one_test (impl, s1, s2, len, exp_result);
132
133 if (HP_TIMING_AVAIL)
134 putchar ('\n');
135}
136
137int
138test_main (void)
139{
140 size_t i;
141
142 test_init ();
143
144 printf ("%23s", "");
145 FOR_EACH_IMPL (impl, 0)
146 printf ("\t%s", impl->name);
147 putchar ('\n');
148
149 for (i = 1; i < 16; ++i)
150 {
151 do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
152 do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
153 do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
154 }
155
156 for (i = 0; i < 16; ++i)
157 {
158 do_test (0, 0, i, 0);
159 do_test (0, 0, i, 1);
160 do_test (0, 0, i, -1);
161 }
162
163 for (i = 1; i < 10; ++i)
164 {
165 do_test (0, 0, 2 << i, 0);
166 do_test (0, 0, 2 << i, 1);
167 do_test (0, 0, 2 << i, -1);
168 do_test (0, 0, 16 << i, 0);
169 do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
170 do_test (0, 0, 16 << i, 1);
171 do_test (0, 0, 16 << i, -1);
172 }
173
174 for (i = 1; i < 8; ++i)
175 {
176 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
177 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
178 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
179 }
180
181 return ret;
182}
183#include "../test-skeleton.c"