1/* Measure strncasecmp functions.
2 Copyright (C) 2013-2022 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 "strncasecmp"
22#include "bench-string.h"
23
24typedef int (*proto_t) (const char *, const char *, size_t);
25static int simple_strncasecmp (const char *, const char *, size_t);
26
27IMPL (simple_strncasecmp, 0)
28IMPL (strncasecmp, 1)
29
30static int
31simple_strncasecmp (const char *s1, const char *s2, size_t n)
32{
33 int ret;
34
35 if (n == 0)
36 return 0;
37
38 while ((ret = ((unsigned char) tolower (*s1)
39 - (unsigned char) tolower (*s2))) == 0
40 && *s1++)
41 {
42 if (--n == 0)
43 return 0;
44 ++s2;
45 }
46 return ret;
47}
48
49static void
50do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
51 int exp_result)
52{
53 size_t i, iters = INNER_LOOP_ITERS;
54 timing_t start, stop, cur;
55
56 TIMING_NOW (start);
57 for (i = 0; i < iters; ++i)
58 {
59 CALL (impl, s1, s2, n);
60 }
61 TIMING_NOW (stop);
62
63 TIMING_DIFF (cur, start, stop);
64
65 TIMING_PRINT_MEAN ((double) cur, (double) iters);
66}
67
68static void
69do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
70 int exp_result)
71{
72 size_t i;
73 char *s1, *s2;
74
75 if (len == 0)
76 return;
77
78 align1 &= 7;
79 if (align1 + len + 1 >= page_size)
80 return;
81
82 align2 &= 7;
83 if (align2 + len + 1 >= page_size)
84 return;
85
86 s1 = (char *) (buf1 + align1);
87 s2 = (char *) (buf2 + align2);
88
89 for (i = 0; i < len; i++)
90 {
91 s1[i] = toupper (1 + 23 * i % max_char);
92 s2[i] = tolower (s1[i]);
93 }
94
95 s1[len] = s2[len] = 0;
96 s1[len + 1] = 23;
97 s2[len + 1] = 24 + exp_result;
98 if ((s2[len - 1] == 'z' && exp_result == -1)
99 || (s2[len - 1] == 'a' && exp_result == 1))
100 s1[len - 1] += exp_result;
101 else
102 s2[len - 1] -= exp_result;
103
104 printf (format: "Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
105
106 FOR_EACH_IMPL (impl, 0)
107 do_one_test (impl, s1, s2, n, exp_result);
108
109 putchar (c: '\n');
110}
111
112int
113test_main (void)
114{
115 size_t i;
116
117 test_init ();
118
119 printf (format: "%23s", "");
120 FOR_EACH_IMPL (impl, 0)
121 printf (format: "\t%s", impl->name);
122 putchar (c: '\n');
123
124 for (i = 1; i < 16; ++i)
125 {
126 do_test (align1: i, align2: i, n: i - 1, len: i, max_char: 127, exp_result: 0);
127
128 do_test (align1: i, align2: i, n: i, len: i, max_char: 127, exp_result: 0);
129 do_test (align1: i, align2: i, n: i, len: i, max_char: 127, exp_result: 1);
130 do_test (align1: i, align2: i, n: i, len: i, max_char: 127, exp_result: -1);
131
132 do_test (align1: i, align2: i, n: i + 1, len: i, max_char: 127, exp_result: 0);
133 do_test (align1: i, align2: i, n: i + 1, len: i, max_char: 127, exp_result: 1);
134 do_test (align1: i, align2: i, n: i + 1, len: i, max_char: 127, exp_result: -1);
135 }
136
137 for (i = 1; i < 10; ++i)
138 {
139 do_test (align1: 0, align2: 0, n: (2 << i) - 1, len: 2 << i, max_char: 127, exp_result: 0);
140 do_test (align1: 0, align2: 0, n: 2 << i, len: 2 << i, max_char: 254, exp_result: 0);
141 do_test (align1: 0, align2: 0, n: (2 << i) + 1, len: 2 << i, max_char: 127, exp_result: 0);
142
143 do_test (align1: 0, align2: 0, n: (2 << i) + 1, len: 2 << i, max_char: 254, exp_result: 0);
144
145 do_test (align1: 0, align2: 0, n: 2 << i, len: 2 << i, max_char: 127, exp_result: 1);
146 do_test (align1: 0, align2: 0, n: (2 << i) + 10, len: 2 << i, max_char: 127, exp_result: 1);
147
148 do_test (align1: 0, align2: 0, n: 2 << i, len: 2 << i, max_char: 254, exp_result: 1);
149 do_test (align1: 0, align2: 0, n: (2 << i) + 10, len: 2 << i, max_char: 254, exp_result: 1);
150
151 do_test (align1: 0, align2: 0, n: 2 << i, len: 2 << i, max_char: 127, exp_result: -1);
152 do_test (align1: 0, align2: 0, n: (2 << i) + 10, len: 2 << i, max_char: 127, exp_result: -1);
153
154 do_test (align1: 0, align2: 0, n: 2 << i, len: 2 << i, max_char: 254, exp_result: -1);
155 do_test (align1: 0, align2: 0, n: (2 << i) + 10, len: 2 << i, max_char: 254, exp_result: -1);
156 }
157
158 for (i = 1; i < 8; ++i)
159 {
160 do_test (align1: i, align2: 2 * i, n: (8 << i) - 1, len: 8 << i, max_char: 127, exp_result: 0);
161 do_test (align1: i, align2: 2 * i, n: 8 << i, len: 8 << i, max_char: 127, exp_result: 0);
162 do_test (align1: i, align2: 2 * i, n: (8 << i) + 100, len: 8 << i, max_char: 127, exp_result: 0);
163
164 do_test (align1: 2 * i, align2: i, n: (8 << i) - 1, len: 8 << i, max_char: 254, exp_result: 0);
165 do_test (align1: 2 * i, align2: i, n: 8 << i, len: 8 << i, max_char: 254, exp_result: 0);
166 do_test (align1: 2 * i, align2: i, n: (8 << i) + 100, len: 8 << i, max_char: 254, exp_result: 0);
167
168 do_test (align1: i, align2: 2 * i, n: 8 << i, len: 8 << i, max_char: 127, exp_result: 1);
169 do_test (align1: i, align2: 2 * i, n: (8 << i) + 100, len: 8 << i, max_char: 127, exp_result: 1);
170
171 do_test (align1: 2 * i, align2: i, n: 8 << i, len: 8 << i, max_char: 254, exp_result: 1);
172 do_test (align1: 2 * i, align2: i, n: (8 << i) + 100, len: 8 << i, max_char: 254, exp_result: 1);
173
174 do_test (align1: i, align2: 2 * i, n: 8 << i, len: 8 << i, max_char: 127, exp_result: -1);
175 do_test (align1: i, align2: 2 * i, n: (8 << i) + 100, len: 8 << i, max_char: 127, exp_result: -1);
176
177 do_test (align1: 2 * i, align2: i, n: 8 << i, len: 8 << i, max_char: 254, exp_result: -1);
178 do_test (align1: 2 * i, align2: i, n: (8 << i) + 100, len: 8 << i, max_char: 254, exp_result: -1);
179 }
180
181 return ret;
182}
183
184#include <support/test-driver.c>
185

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