Fix Wundef warning for __STDC_VERSION__
[glibc.git] / benchtests / bench-memcmp.c
blobcbada118a1c229f45246acd8bdc4ba13b4240f4f
1 /* Measure memcmp 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 "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>
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
38 int
39 simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
41 int ret = 0;
42 /* Warning!
43 wmemcmp has to use SIGNED comparison for elements.
44 memcmp has to use UNSIGNED comparison for elemnts.
46 while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;}
47 return ret;
49 #else
50 # include <limits.h>
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
62 int
63 simple_memcmp (const char *s1, const char *s2, size_t n)
65 int ret = 0;
67 while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
68 return ret;
70 #endif
72 typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
74 IMPL (SIMPLE_MEMCMP, 0)
75 IMPL (MEMCMP, 1)
77 static void
78 do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len,
79 int exp_result)
81 size_t i, iters = INNER_LOOP_ITERS;
82 timing_t start, stop, cur;
84 TIMING_NOW (start);
85 for (i = 0; i < iters; ++i)
87 CALL (impl, s1, s2, len);
89 TIMING_NOW (stop);
91 TIMING_DIFF (cur, start, stop);
93 TIMING_PRINT_MEAN ((double) cur, (double) iters);
96 static void
97 do_test (size_t align1, size_t align2, size_t len, int exp_result)
99 size_t i;
100 CHAR *s1, *s2;
102 if (len == 0)
103 return;
105 align1 &= 63;
106 if (align1 + (len + 1) * CHARBYTES >= page_size)
107 return;
109 align2 &= 63;
110 if (align2 + (len + 1) * CHARBYTES >= page_size)
111 return;
113 s1 = (CHAR *) (buf1 + align1);
114 s2 = (CHAR *) (buf2 + align2);
116 for (i = 0; i < len; i++)
117 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX;
119 s1[len] = align1;
120 s2[len] = align2;
121 s2[len - 1] -= exp_result;
123 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
125 FOR_EACH_IMPL (impl, 0)
126 do_one_test (impl, s1, s2, len, exp_result);
128 putchar ('\n');
132 test_main (void)
134 size_t i;
136 test_init ();
138 printf ("%23s", "");
139 FOR_EACH_IMPL (impl, 0)
140 printf ("\t%s", impl->name);
141 putchar ('\n');
143 for (i = 1; i < 16; ++i)
145 do_test (i * CHARBYTES, i * CHARBYTES, i, 0);
146 do_test (i * CHARBYTES, i * CHARBYTES, i, 1);
147 do_test (i * CHARBYTES, i * CHARBYTES, i, -1);
150 for (i = 0; i < 16; ++i)
152 do_test (0, 0, i, 0);
153 do_test (0, 0, i, 1);
154 do_test (0, 0, i, -1);
157 for (i = 1; i < 10; ++i)
159 do_test (0, 0, 2 << i, 0);
160 do_test (0, 0, 2 << i, 1);
161 do_test (0, 0, 2 << i, -1);
162 do_test (0, 0, 16 << i, 0);
163 do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0);
164 do_test (0, 0, 16 << i, 1);
165 do_test (0, 0, 16 << i, -1);
168 for (i = 1; i < 8; ++i)
170 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0);
171 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1);
172 do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1);
175 return ret;
177 #include "../test-skeleton.c"