1 /* Measure strcasecmp functions.
2 Copyright (C) 2013-2022 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/>. */
21 #define TEST_NAME "strcasecmp"
22 #include "bench-string.h"
25 typedef int (*proto_t
) (const char *, const char *);
26 static int simple_strcasecmp (const char *, const char *);
28 IMPL (simple_strcasecmp
, 0)
32 simple_strcasecmp (const char *s1
, const char *s2
)
36 while ((ret
= ((unsigned char) tolower (*s1
)
37 - (unsigned char) tolower (*s2
))) == 0
44 do_one_test (json_ctx_t
*json_ctx
, impl_t
*impl
, const char *s1
,
45 const char *s2
, int exp_result
)
47 size_t i
, iters
= INNER_LOOP_ITERS
;
48 timing_t start
, stop
, cur
;
49 int result
= CALL (impl
, s1
, s2
);
50 if ((exp_result
== 0 && result
!= 0)
51 || (exp_result
< 0 && result
>= 0)
52 || (exp_result
> 0 && result
<= 0))
54 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
61 for (i
= 0; i
< iters
; ++i
)
67 TIMING_DIFF (cur
, start
, stop
);
69 json_element_double (json_ctx
, (double) cur
/ (double) iters
);
73 do_test (json_ctx_t
*json_ctx
, size_t align1
, size_t align2
, size_t len
,
74 int max_char
, int exp_result
)
83 if (align1
+ len
+ 1 >= page_size
)
87 if (align2
+ len
+ 1 >= page_size
)
90 json_element_object_begin (json_ctx
);
91 json_attr_uint (json_ctx
, "length", len
);
92 json_attr_uint (json_ctx
, "align1", align1
);
93 json_attr_uint (json_ctx
, "align2", align2
);
94 json_attr_uint (json_ctx
, "max_char", max_char
);
95 json_array_begin (json_ctx
, "timings");
97 s1
= (char *) (buf1
+ align1
);
98 s2
= (char *) (buf2
+ align2
);
100 for (i
= 0; i
< len
; i
++)
102 s1
[i
] = toupper (1 + 23 * i
% max_char
);
103 s2
[i
] = tolower (s1
[i
]);
106 s1
[len
] = s2
[len
] = 0;
108 s2
[len
+ 1] = 24 + exp_result
;
109 if ((s2
[len
- 1] == 'z' && exp_result
== -1)
110 || (s2
[len
- 1] == 'a' && exp_result
== 1))
111 s1
[len
- 1] += exp_result
;
113 s2
[len
- 1] -= exp_result
;
115 FOR_EACH_IMPL (impl
, 0)
116 do_one_test (json_ctx
, impl
, s1
, s2
, exp_result
);
118 json_array_end (json_ctx
);
119 json_element_object_end (json_ctx
);
130 json_init (&json_ctx
, 0, stdout
);
132 json_document_begin (&json_ctx
);
133 json_attr_string (&json_ctx
, "timing_type", TIMING_TYPE
);
135 json_attr_object_begin (&json_ctx
, "functions");
136 json_attr_object_begin (&json_ctx
, TEST_NAME
);
137 json_attr_string (&json_ctx
, "bench-variant", "");
139 json_array_begin (&json_ctx
, "ifuncs");
140 FOR_EACH_IMPL (impl
, 0)
141 json_element_string (&json_ctx
, impl
->name
);
142 json_array_end (&json_ctx
);
144 json_array_begin (&json_ctx
, "results");
146 for (i
= 1; i
< 16; ++i
)
148 do_test (&json_ctx
, i
, i
, i
, 127, 0);
149 do_test (&json_ctx
, i
, i
, i
, 127, 1);
150 do_test (&json_ctx
, i
, i
, i
, 127, -1);
153 for (i
= 1; i
< 10; ++i
)
155 do_test (&json_ctx
, 0, 0, 2 << i
, 127, 0);
156 do_test (&json_ctx
, 0, 0, 2 << i
, 254, 0);
157 do_test (&json_ctx
, 0, 0, 2 << i
, 127, 1);
158 do_test (&json_ctx
, 0, 0, 2 << i
, 254, 1);
159 do_test (&json_ctx
, 0, 0, 2 << i
, 127, -1);
160 do_test (&json_ctx
, 0, 0, 2 << i
, 254, -1);
163 for (i
= 1; i
< 8; ++i
)
165 do_test (&json_ctx
, i
, 2 * i
, 8 << i
, 127, 0);
166 do_test (&json_ctx
, 2 * i
, i
, 8 << i
, 254, 0);
167 do_test (&json_ctx
, i
, 2 * i
, 8 << i
, 127, 1);
168 do_test (&json_ctx
, 2 * i
, i
, 8 << i
, 254, 1);
169 do_test (&json_ctx
, i
, 2 * i
, 8 << i
, 127, -1);
170 do_test (&json_ctx
, 2 * i
, i
, 8 << i
, 254, -1);
173 json_array_end (&json_ctx
);
174 json_attr_object_end (&json_ctx
);
175 json_attr_object_end (&json_ctx
);
176 json_document_end (&json_ctx
);
181 #include <support/test-driver.c>