[powerpc] No need to enter "Ignore Exceptions Mode"
[glibc.git] / benchtests / bench-strcmp.c
blob581c410b03940513888a8ee51d76dc209b8651d6
1 /* Measure strcmp and wcscmp functions.
2 Copyright (C) 2013-2019 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/>. */
19 #define TEST_MAIN
20 #ifdef WIDE
21 # define TEST_NAME "wcscmp"
22 #else
23 # define TEST_NAME "strcmp"
24 #endif
25 #include "bench-string.h"
27 #ifdef WIDE
28 # define L(str) L##str
29 # define SIMPLE_STRCMP simple_wcscmp
30 # define CHARBYTESLOG 2
31 # define MIDCHAR 0x7fffffff
32 # define LARGECHAR 0xfffffffe
34 /* Wcscmp uses signed semantics for comparison, not unsigned */
35 /* Avoid using substraction since possible overflow */
37 int
38 simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
40 wchar_t c1, c2;
43 c1 = *s1++;
44 c2 = *s2++;
45 if (c2 == L'\0')
46 return c1 - c2;
48 while (c1 == c2);
50 return c1 < c2 ? -1 : 1;
53 #else
54 # include <limits.h>
56 # define L(str) str
57 # define SIMPLE_STRCMP simple_strcmp
58 # define CHARBYTESLOG 0
59 # define MIDCHAR 0x7f
60 # define LARGECHAR 0xfe
62 /* Strcmp uses unsigned semantics for comparison. */
63 int
64 simple_strcmp (const char *s1, const char *s2)
66 int ret;
68 while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
69 return ret;
72 #endif
74 # include "json-lib.h"
76 typedef int (*proto_t) (const CHAR *, const CHAR *);
78 IMPL (SIMPLE_STRCMP, 1)
79 IMPL (STRCMP, 1)
81 static void
82 do_one_test (json_ctx_t *json_ctx, impl_t *impl,
83 const CHAR *s1, const CHAR *s2,
84 int exp_result)
86 size_t i, iters = INNER_LOOP_ITERS8;
87 timing_t start, stop, cur;
89 TIMING_NOW (start);
90 for (i = 0; i < iters; ++i)
92 CALL (impl, s1, s2);
94 TIMING_NOW (stop);
96 TIMING_DIFF (cur, start, stop);
98 json_element_double (json_ctx, (double) cur / (double) iters);
101 static void
102 do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, int
103 max_char, int exp_result)
105 size_t i;
107 CHAR *s1, *s2;
109 if (len == 0)
110 return;
112 align1 &= 63;
113 if (align1 + (len + 1) * CHARBYTES >= page_size)
114 return;
116 align2 &= 63;
117 if (align2 + (len + 1) * CHARBYTES >= page_size)
118 return;
120 /* Put them close to the end of page. */
121 i = align1 + CHARBYTES * (len + 2);
122 s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
123 i = align2 + CHARBYTES * (len + 2);
124 s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
126 for (i = 0; i < len; i++)
127 s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
129 s1[len] = s2[len] = 0;
130 s1[len + 1] = 23;
131 s2[len + 1] = 24 + exp_result;
132 s2[len - 1] -= exp_result;
134 json_element_object_begin (json_ctx);
135 json_attr_uint (json_ctx, "length", (double) len);
136 json_attr_uint (json_ctx, "align1", (double) align1);
137 json_attr_uint (json_ctx, "align2", (double) align2);
138 json_array_begin (json_ctx, "timings");
140 FOR_EACH_IMPL (impl, 0)
141 do_one_test (json_ctx, impl, s1, s2, exp_result);
143 json_array_end (json_ctx);
144 json_element_object_end (json_ctx);
148 test_main (void)
150 json_ctx_t json_ctx;
151 size_t i;
153 test_init ();
155 json_init (&json_ctx, 0, stdout);
157 json_document_begin (&json_ctx);
158 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
160 json_attr_object_begin (&json_ctx, "functions");
161 json_attr_object_begin (&json_ctx, TEST_NAME);
162 json_attr_string (&json_ctx, "bench-variant", "default");
164 json_array_begin (&json_ctx, "ifuncs");
165 FOR_EACH_IMPL (impl, 0)
166 json_element_string (&json_ctx, impl->name);
167 json_array_end (&json_ctx);
169 json_array_begin (&json_ctx, "results");
171 for (i = 1; i < 32; ++i)
173 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
174 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
175 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
178 for (i = 1; i < 10 + CHARBYTESLOG; ++i)
180 do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 0);
181 do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 0);
182 do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 1);
183 do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 1);
184 do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, -1);
185 do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, -1);
186 do_test (&json_ctx, 0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
187 do_test (&json_ctx, CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
190 for (i = 1; i < 8; ++i)
192 do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
193 do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
194 do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
195 do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
196 do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
197 do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
200 json_array_end (&json_ctx);
201 json_attr_object_end (&json_ctx);
202 json_attr_object_end (&json_ctx);
203 json_document_end (&json_ctx);
205 return ret;
208 #include <support/test-driver.c>