Add C2X _PRINTF_NAN_LEN_MAX
[glibc.git] / benchtests / bench-strcasecmp.c
bloba19a81adc42fc99733ab4c4d1e3bdd79e6b7c4d9
1 /* Measure strcasecmp functions.
2 Copyright (C) 2013-2021 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 #include <ctype.h>
20 #define TEST_MAIN
21 #define TEST_NAME "strcasecmp"
22 #include "bench-string.h"
24 typedef int (*proto_t) (const char *, const char *);
25 static int simple_strcasecmp (const char *, const char *);
27 IMPL (simple_strcasecmp, 0)
28 IMPL (strcasecmp, 1)
30 static int
31 simple_strcasecmp (const char *s1, const char *s2)
33 int ret;
35 while ((ret = ((unsigned char) tolower (*s1)
36 - (unsigned char) tolower (*s2))) == 0
37 && *s1++)
38 ++s2;
39 return ret;
42 static void
43 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
45 size_t i, iters = INNER_LOOP_ITERS;
46 timing_t start, stop, cur;
47 int result = CALL (impl, s1, s2);
48 if ((exp_result == 0 && result != 0)
49 || (exp_result < 0 && result >= 0)
50 || (exp_result > 0 && result <= 0))
52 error (0, 0, "Wrong result in function %s %d %d", impl->name,
53 result, exp_result);
54 ret = 1;
55 return;
58 TIMING_NOW (start);
59 for (i = 0; i < iters; ++i)
61 CALL (impl, s1, s2);
63 TIMING_NOW (stop);
65 TIMING_DIFF (cur, start, stop);
67 TIMING_PRINT_MEAN ((double) cur, (double) iters);
70 static void
71 do_test (size_t align1, size_t align2, size_t len, int max_char,
72 int exp_result)
74 size_t i;
75 char *s1, *s2;
77 if (len == 0)
78 return;
80 align1 &= 7;
81 if (align1 + len + 1 >= page_size)
82 return;
84 align2 &= 7;
85 if (align2 + len + 1 >= page_size)
86 return;
88 s1 = (char *) (buf1 + align1);
89 s2 = (char *) (buf2 + align2);
91 for (i = 0; i < len; i++)
93 s1[i] = toupper (1 + 23 * i % max_char);
94 s2[i] = tolower (s1[i]);
97 s1[len] = s2[len] = 0;
98 s1[len + 1] = 23;
99 s2[len + 1] = 24 + exp_result;
100 if ((s2[len - 1] == 'z' && exp_result == -1)
101 || (s2[len - 1] == 'a' && exp_result == 1))
102 s1[len - 1] += exp_result;
103 else
104 s2[len - 1] -= exp_result;
106 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
108 FOR_EACH_IMPL (impl, 0)
109 do_one_test (impl, s1, s2, exp_result);
111 putchar ('\n');
115 test_main (void)
117 size_t i;
119 test_init ();
121 printf ("%23s", "");
122 FOR_EACH_IMPL (impl, 0)
123 printf ("\t%s", impl->name);
124 putchar ('\n');
126 for (i = 1; i < 16; ++i)
128 do_test (i, i, i, 127, 0);
129 do_test (i, i, i, 127, 1);
130 do_test (i, i, i, 127, -1);
133 for (i = 1; i < 10; ++i)
135 do_test (0, 0, 2 << i, 127, 0);
136 do_test (0, 0, 2 << i, 254, 0);
137 do_test (0, 0, 2 << i, 127, 1);
138 do_test (0, 0, 2 << i, 254, 1);
139 do_test (0, 0, 2 << i, 127, -1);
140 do_test (0, 0, 2 << i, 254, -1);
143 for (i = 1; i < 8; ++i)
145 do_test (i, 2 * i, 8 << i, 127, 0);
146 do_test (2 * i, i, 8 << i, 254, 0);
147 do_test (i, 2 * i, 8 << i, 127, 1);
148 do_test (2 * i, i, 8 << i, 254, 1);
149 do_test (i, 2 * i, 8 << i, 127, -1);
150 do_test (2 * i, i, 8 << i, 254, -1);
153 return ret;
156 #include <support/test-driver.c>