xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Measure memcmp functions. |
| 2 | Copyright (C) 2013-2016 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 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #define TEST_MAIN |
| 20 | #ifdef WIDE |
| 21 | # define TEST_NAME "wmemcmp" |
| 22 | #else |
| 23 | # define TEST_NAME "memcmp" |
| 24 | #endif |
| 25 | #include "bench-string.h" |
| 26 | #ifdef WIDE |
| 27 | # include <inttypes.h> |
| 28 | # include <wchar.h> |
| 29 | |
| 30 | # define MEMCMP wmemcmp |
| 31 | # define MEMCPY wmemcpy |
| 32 | # define SIMPLE_MEMCMP simple_wmemcmp |
| 33 | # define CHAR wchar_t |
| 34 | # define UCHAR wchar_t |
| 35 | # define CHARBYTES 4 |
| 36 | # define CHAR__MIN WCHAR_MIN |
| 37 | # define CHAR__MAX WCHAR_MAX |
| 38 | int |
| 39 | simple_wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n) |
| 40 | { |
| 41 | int ret = 0; |
| 42 | /* Warning! |
| 43 | wmemcmp has to use SIGNED comparison for elements. |
| 44 | memcmp has to use UNSIGNED comparison for elemnts. |
| 45 | */ |
| 46 | while (n-- && (ret = *s1 < *s2 ? -1 : *s1 == *s2 ? 0 : 1) == 0) {s1++; s2++;} |
| 47 | return ret; |
| 48 | } |
| 49 | #else |
| 50 | # include <limits.h> |
| 51 | |
| 52 | # define MEMCMP memcmp |
| 53 | # define MEMCPY memcpy |
| 54 | # define SIMPLE_MEMCMP simple_memcmp |
| 55 | # define CHAR char |
| 56 | # define MAX_CHAR 255 |
| 57 | # define UCHAR unsigned char |
| 58 | # define CHARBYTES 1 |
| 59 | # define CHAR__MIN CHAR_MIN |
| 60 | # define CHAR__MAX CHAR_MAX |
| 61 | |
| 62 | int |
| 63 | simple_memcmp (const char *s1, const char *s2, size_t n) |
| 64 | { |
| 65 | int ret = 0; |
| 66 | |
| 67 | while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0); |
| 68 | return ret; |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | typedef int (*proto_t) (const CHAR *, const CHAR *, size_t); |
| 73 | |
| 74 | IMPL (SIMPLE_MEMCMP, 0) |
| 75 | IMPL (MEMCMP, 1) |
| 76 | |
| 77 | static void |
| 78 | do_one_test (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t len, |
| 79 | int exp_result) |
| 80 | { |
| 81 | size_t i, iters = INNER_LOOP_ITERS; |
| 82 | timing_t start, stop, cur; |
| 83 | |
| 84 | TIMING_NOW (start); |
| 85 | for (i = 0; i < iters; ++i) |
| 86 | { |
| 87 | CALL (impl, s1, s2, len); |
| 88 | } |
| 89 | TIMING_NOW (stop); |
| 90 | |
| 91 | TIMING_DIFF (cur, start, stop); |
| 92 | |
| 93 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | do_test (size_t align1, size_t align2, size_t len, int exp_result) |
| 98 | { |
| 99 | size_t i; |
| 100 | CHAR *s1, *s2; |
| 101 | |
| 102 | if (len == 0) |
| 103 | return; |
| 104 | |
| 105 | align1 &= 63; |
| 106 | if (align1 + (len + 1) * CHARBYTES >= page_size) |
| 107 | return; |
| 108 | |
| 109 | align2 &= 63; |
| 110 | if (align2 + (len + 1) * CHARBYTES >= page_size) |
| 111 | return; |
| 112 | |
| 113 | s1 = (CHAR *) (buf1 + align1); |
| 114 | s2 = (CHAR *) (buf2 + align2); |
| 115 | |
| 116 | for (i = 0; i < len; i++) |
| 117 | s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % CHAR__MAX; |
| 118 | |
| 119 | s1[len] = align1; |
| 120 | s2[len] = align2; |
| 121 | s2[len - 1] -= exp_result; |
| 122 | |
| 123 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); |
| 124 | |
| 125 | FOR_EACH_IMPL (impl, 0) |
| 126 | do_one_test (impl, s1, s2, len, exp_result); |
| 127 | |
| 128 | putchar ('\n'); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | test_main (void) |
| 133 | { |
| 134 | size_t i; |
| 135 | |
| 136 | test_init (); |
| 137 | |
| 138 | printf ("%23s", ""); |
| 139 | FOR_EACH_IMPL (impl, 0) |
| 140 | printf ("\t%s", impl->name); |
| 141 | putchar ('\n'); |
| 142 | |
| 143 | for (i = 1; i < 16; ++i) |
| 144 | { |
| 145 | do_test (i * CHARBYTES, i * CHARBYTES, i, 0); |
| 146 | do_test (i * CHARBYTES, i * CHARBYTES, i, 1); |
| 147 | do_test (i * CHARBYTES, i * CHARBYTES, i, -1); |
| 148 | } |
| 149 | |
| 150 | for (i = 0; i < 16; ++i) |
| 151 | { |
| 152 | do_test (0, 0, i, 0); |
| 153 | do_test (0, 0, i, 1); |
| 154 | do_test (0, 0, i, -1); |
| 155 | } |
| 156 | |
| 157 | for (i = 1; i < 10; ++i) |
| 158 | { |
| 159 | do_test (0, 0, 2 << i, 0); |
| 160 | do_test (0, 0, 2 << i, 1); |
| 161 | do_test (0, 0, 2 << i, -1); |
| 162 | do_test (0, 0, 16 << i, 0); |
| 163 | do_test ((8 - i) * CHARBYTES, (2 * i) * CHARBYTES, 16 << i, 0); |
| 164 | do_test (0, 0, 16 << i, 1); |
| 165 | do_test (0, 0, 16 << i, -1); |
| 166 | } |
| 167 | |
| 168 | for (i = 1; i < 8; ++i) |
| 169 | { |
| 170 | do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 0); |
| 171 | do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, 1); |
| 172 | do_test (i * CHARBYTES, 2 * (i * CHARBYTES), 8 << i, -1); |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | #include "../test-skeleton.c" |