]>
Commit | Line | Data |
---|---|---|
97020474 | 1 | /* Measure memcmp functions. |
04277e02 | 2 | Copyright (C) 2013-2019 Free Software Foundation, Inc. |
97020474 SP |
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 | |
97020474 | 27 | |
97020474 | 28 | # define SIMPLE_MEMCMP simple_wmemcmp |
97020474 SP |
29 | int |
30 | simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n) | |
31 | { | |
32 | int ret = 0; | |
33 | /* Warning! | |
34 | wmemcmp has to use SIGNED comparison for elements. | |
35 | memcmp has to use UNSIGNED comparison for elemnts. | |
36 | */ | |
37 | while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;} | |
38 | return ret; | |
39 | } | |
40 | #else | |
41 | # include <limits.h> | |
42 | ||
97020474 | 43 | # define SIMPLE_MEMCMP simple_memcmp |
97020474 SP |
44 | |
45 | int | |
46 | simple_memcmp (const char *s1, const char *s2, size_t n) | |
47 | { | |
48 | int ret = 0; | |
49 | ||
50 | while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0); | |
51 | return ret; | |
52 | } | |
53 | #endif | |
54 | ||
96e6a716 SP |
55 | # include "json-lib.h" |
56 | ||
97020474 SP |
57 | typedef int (*proto_t) (const CHAR *, const CHAR *, size_t); |
58 | ||
59 | IMPL (SIMPLE_MEMCMP, 0) | |
60 | IMPL (MEMCMP, 1) | |
61 | ||
62 | static void | |
96e6a716 SP |
63 | do_one_test (json_ctx_t *json_ctx, impl_t *impl, const CHAR *s1, |
64 | const CHAR *s2, size_t len, int exp_result) | |
97020474 | 65 | { |
44558701 WN |
66 | size_t i, iters = INNER_LOOP_ITERS; |
67 | timing_t start, stop, cur; | |
68 | ||
69 | TIMING_NOW (start); | |
70 | for (i = 0; i < iters; ++i) | |
97020474 | 71 | { |
44558701 | 72 | CALL (impl, s1, s2, len); |
97020474 | 73 | } |
44558701 WN |
74 | TIMING_NOW (stop); |
75 | ||
76 | TIMING_DIFF (cur, start, stop); | |
77 | ||
96e6a716 | 78 | json_element_double (json_ctx, (double) cur / (double) iters); |
97020474 SP |
79 | } |
80 | ||
81 | static void | |
96e6a716 SP |
82 | do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, |
83 | int exp_result) | |
97020474 SP |
84 | { |
85 | size_t i; | |
86 | CHAR *s1, *s2; | |
87 | ||
88 | if (len == 0) | |
89 | return; | |
90 | ||
91 | align1 &= 63; | |
92 | if (align1 + (len + 1) * CHARBYTES >= page_size) | |
93 | return; | |
94 | ||
95 | align2 &= 63; | |
96 | if (align2 + (len + 1) * CHARBYTES >= page_size) | |
97 | return; | |
98 | ||
96e6a716 SP |
99 | json_element_object_begin (json_ctx); |
100 | json_attr_uint (json_ctx, "length", (double) len); | |
101 | json_attr_uint (json_ctx, "align1", (double) align1); | |
102 | json_attr_uint (json_ctx, "align2", (double) align2); | |
103 | json_array_begin (json_ctx, "timings"); | |
97020474 | 104 | |
3dfcbfa1 SP |
105 | FOR_EACH_IMPL (impl, 0) |
106 | { | |
107 | s1 = (CHAR *) (buf1 + align1); | |
108 | s2 = (CHAR *) (buf2 + align2); | |
97020474 | 109 | |
3dfcbfa1 | 110 | for (i = 0; i < len; i++) |
90d3320d | 111 | s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % MAX_CHAR; |
97020474 | 112 | |
3dfcbfa1 SP |
113 | s1[len] = align1; |
114 | s2[len] = align2; | |
115 | s2[len - 1] -= exp_result; | |
97020474 | 116 | |
96e6a716 | 117 | do_one_test (json_ctx, impl, s1, s2, len, exp_result); |
014efdd7 | 118 | alloc_bufs (); |
3dfcbfa1 | 119 | } |
97020474 | 120 | |
96e6a716 SP |
121 | json_array_end (json_ctx); |
122 | json_element_object_end (json_ctx); | |
97020474 SP |
123 | } |
124 | ||
125 | int | |
126 | test_main (void) | |
127 | { | |
96e6a716 | 128 | json_ctx_t json_ctx; |
97020474 SP |
129 | size_t i; |
130 | ||
131 | test_init (); | |
132 | ||
96e6a716 SP |
133 | json_init (&json_ctx, 0, stdout); |
134 | ||
135 | json_document_begin (&json_ctx); | |
136 | json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); | |
137 | ||
138 | json_attr_object_begin (&json_ctx, "functions"); | |
139 | json_attr_object_begin (&json_ctx, TEST_NAME); | |
140 | json_attr_string (&json_ctx, "bench-variant", "default"); | |
141 | ||
142 | json_array_begin (&json_ctx, "ifuncs"); | |
97020474 | 143 | FOR_EACH_IMPL (impl, 0) |
96e6a716 SP |
144 | json_element_string (&json_ctx, impl->name); |
145 | json_array_end (&json_ctx); | |
97020474 | 146 | |
96e6a716 | 147 | json_array_begin (&json_ctx, "results"); |
97020474 SP |
148 | for (i = 1; i < 16; ++i) |
149 | { | |
96e6a716 SP |
150 | do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 0); |
151 | do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, 1); | |
152 | do_test (&json_ctx, i * CHARBYTES, i * CHARBYTES, i, -1); | |
97020474 SP |
153 | } |
154 | ||
155 | for (i = 0; i < 16; ++i) | |
156 | { | |
96e6a716 SP |
157 | do_test (&json_ctx, 0, 0, i, 0); |
158 | do_test (&json_ctx, 0, 0, i, 1); | |
159 | do_test (&json_ctx, 0, 0, i, -1); | |
97020474 SP |
160 | } |
161 | ||
162 | for (i = 1; i < 10; ++i) | |
163 | { | |
96e6a716 SP |
164 | do_test (&json_ctx, 0, 0, 2 << i, 0); |
165 | do_test (&json_ctx, 0, 0, 2 << i, 1); | |
166 | do_test (&json_ctx, 0, 0, 2 << i, -1); | |
167 | do_test (&json_ctx, 0, 0, 16 << i, 0); | |
168 | do_test (&json_ctx, (8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0); | |
169 | do_test (&json_ctx, 0, 0, 16 << i, 1); | |
170 | do_test (&json_ctx, 0, 0, 16 << i, -1); | |
97020474 SP |
171 | } |
172 | ||
173 | for (i = 1; i < 8; ++i) | |
174 | { | |
96e6a716 SP |
175 | do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0); |
176 | do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1); | |
177 | do_test (&json_ctx, i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1); | |
97020474 SP |
178 | } |
179 | ||
96e6a716 SP |
180 | json_array_end (&json_ctx); |
181 | json_attr_object_end (&json_ctx); | |
182 | json_attr_object_end (&json_ctx); | |
183 | json_document_end (&json_ctx); | |
184 | ||
97020474 SP |
185 | return ret; |
186 | } | |
b598e134 AZ |
187 | |
188 | #include <support/test-driver.c> |