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