1/* Measure strcasecmp functions.
2 Copyright (C) 2013-2024 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 <https://www.gnu.org/licenses/>. */
18
19#include <ctype.h>
20#define TEST_MAIN
21#define TEST_NAME "strcasecmp"
22#include "bench-string.h"
23#include "json-lib.h"
24
25typedef int (*proto_t) (const char *, const char *);
26
27IMPL (strcasecmp, 1)
28
29static void
30do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s1,
31 const char *s2, int exp_result)
32{
33 size_t i, iters = INNER_LOOP_ITERS8;
34 timing_t start, stop, cur;
35 int result = CALL (impl, s1, s2);
36 if ((exp_result == 0 && result != 0)
37 || (exp_result < 0 && result >= 0)
38 || (exp_result > 0 && result <= 0))
39 {
40 error (status: 0, errnum: 0, format: "Wrong result in function %s %d %d", impl->name,
41 result, exp_result);
42 ret = 1;
43 return;
44 }
45
46 TIMING_NOW (start);
47 for (i = 0; i < iters; ++i)
48 {
49 CALL (impl, s1, s2);
50 }
51 TIMING_NOW (stop);
52
53 TIMING_DIFF (cur, start, stop);
54
55 json_element_double (ctx: json_ctx, d: (double) cur / (double) iters);
56}
57
58static void
59do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
60 int max_char, int exp_result)
61{
62 size_t i;
63 char *s1, *s2;
64
65 if (len == 0)
66 return;
67
68 align1 &= 7;
69 if (align1 + len + 1 >= page_size)
70 return;
71
72 align2 &= 7;
73 if (align2 + len + 1 >= page_size)
74 return;
75
76 json_element_object_begin (ctx: json_ctx);
77 json_attr_uint (ctx: json_ctx, name: "length", d: len);
78 json_attr_uint (ctx: json_ctx, name: "align1", d: align1);
79 json_attr_uint (ctx: json_ctx, name: "align2", d: align2);
80 json_attr_uint (ctx: json_ctx, name: "max_char", d: max_char);
81 json_array_begin (ctx: json_ctx, name: "timings");
82
83 s1 = (char *) (buf1 + align1);
84 s2 = (char *) (buf2 + align2);
85
86 for (i = 0; i < len; i++)
87 {
88 s1[i] = toupper (1 + 23 * i % max_char);
89 s2[i] = tolower (s1[i]);
90 }
91
92 s1[len] = s2[len] = 0;
93 s1[len + 1] = 23;
94 s2[len + 1] = 24 + exp_result;
95 if ((s2[len - 1] == 'z' && exp_result == -1)
96 || (s2[len - 1] == 'a' && exp_result == 1))
97 s1[len - 1] += exp_result;
98 else
99 s2[len - 1] -= exp_result;
100
101 FOR_EACH_IMPL (impl, 0)
102 do_one_test (json_ctx, impl, s1, s2, exp_result);
103
104 json_array_end (ctx: json_ctx);
105 json_element_object_end (ctx: json_ctx);
106}
107
108int
109test_main (void)
110{
111 json_ctx_t json_ctx;
112 size_t i;
113
114 test_init ();
115
116 json_init (ctx: &json_ctx, indent_level: 0, stdout);
117
118 json_document_begin (ctx: &json_ctx);
119 json_attr_string (ctx: &json_ctx, name: "timing_type", TIMING_TYPE);
120
121 json_attr_object_begin (ctx: &json_ctx, name: "functions");
122 json_attr_object_begin (ctx: &json_ctx, TEST_NAME);
123 json_attr_string (ctx: &json_ctx, name: "bench-variant", s: "");
124
125 json_array_begin (ctx: &json_ctx, name: "ifuncs");
126 FOR_EACH_IMPL (impl, 0)
127 json_element_string (ctx: &json_ctx, s: impl->name);
128 json_array_end (ctx: &json_ctx);
129
130 json_array_begin (ctx: &json_ctx, name: "results");
131
132 for (i = 1; i < 16; ++i)
133 {
134 do_test (json_ctx: &json_ctx, align1: i, align2: i, len: i, max_char: 127, exp_result: 0);
135 do_test (json_ctx: &json_ctx, align1: i, align2: i, len: i, max_char: 127, exp_result: 1);
136 do_test (json_ctx: &json_ctx, align1: i, align2: i, len: i, max_char: 127, exp_result: -1);
137 }
138
139 for (i = 1; i < 10; ++i)
140 {
141 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2 << i, max_char: 127, exp_result: 0);
142 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2 << i, max_char: 254, exp_result: 0);
143 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2 << i, max_char: 127, exp_result: 1);
144 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2 << i, max_char: 254, exp_result: 1);
145 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2 << i, max_char: 127, exp_result: -1);
146 do_test (json_ctx: &json_ctx, align1: 0, align2: 0, len: 2 << i, max_char: 254, exp_result: -1);
147 }
148
149 for (i = 1; i < 8; ++i)
150 {
151 do_test (json_ctx: &json_ctx, align1: i, align2: 2 * i, len: 8 << i, max_char: 127, exp_result: 0);
152 do_test (json_ctx: &json_ctx, align1: 2 * i, align2: i, len: 8 << i, max_char: 254, exp_result: 0);
153 do_test (json_ctx: &json_ctx, align1: i, align2: 2 * i, len: 8 << i, max_char: 127, exp_result: 1);
154 do_test (json_ctx: &json_ctx, align1: 2 * i, align2: i, len: 8 << i, max_char: 254, exp_result: 1);
155 do_test (json_ctx: &json_ctx, align1: i, align2: 2 * i, len: 8 << i, max_char: 127, exp_result: -1);
156 do_test (json_ctx: &json_ctx, align1: 2 * i, align2: i, len: 8 << i, max_char: 254, exp_result: -1);
157 }
158
159 json_array_end (ctx: &json_ctx);
160 json_attr_object_end (ctx: &json_ctx);
161 json_attr_object_end (ctx: &json_ctx);
162 json_document_end (ctx: &json_ctx);
163
164 return ret;
165}
166
167#include <support/test-driver.c>
168

source code of glibc/benchtests/bench-strcasecmp.c