Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 17 | #include <err.h> |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 21 | #include <benchmark/benchmark.h> |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 22 | #include <util.h> |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 24 | static void BM_string_memcmp(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 25 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 26 | const size_t src_alignment = state.range(1); |
| 27 | const size_t dst_alignment = state.range(2); |
| 28 | |
| 29 | std::vector<char> src; |
| 30 | std::vector<char> dst; |
| 31 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); |
| 32 | char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'x'); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 33 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 34 | while (state.KeepRunning()) { |
Elliott Hughes | e8693e7 | 2020-10-22 13:43:59 -0700 | [diff] [blame] | 35 | benchmark::DoNotOptimize(memcmp(dst_aligned, src_aligned, nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 38 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 39 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 40 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memcmp, "AT_ALIGNED_TWOBUF"); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 42 | static void BM_string_memcpy(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 43 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 44 | const size_t src_alignment = state.range(1); |
| 45 | const size_t dst_alignment = state.range(2); |
| 46 | |
| 47 | std::vector<char> src; |
| 48 | std::vector<char> dst; |
| 49 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); |
| 50 | char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 51 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 52 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 53 | memcpy(dst_aligned, src_aligned, nbytes); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 56 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 57 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 58 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memcpy, "AT_ALIGNED_TWOBUF"); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 59 | |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 60 | static void BM_string_memmove_non_overlapping(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 61 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 62 | const size_t src_alignment = state.range(1); |
| 63 | const size_t dst_alignment = state.range(2); |
| 64 | |
| 65 | std::vector<char> src; |
| 66 | std::vector<char> dst; |
| 67 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); |
| 68 | char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 69 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 70 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 71 | memmove(dst_aligned, src_aligned, nbytes); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 74 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 75 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 76 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memmove_non_overlapping, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 77 | |
| 78 | static void BM_string_memmove_overlap_dst_before_src(benchmark::State& state) { |
| 79 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 80 | const size_t alignment = state.range(1); |
| 81 | |
| 82 | std::vector<char> buf(3 * alignment + nbytes + 1, 'x'); |
| 83 | char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 84 | |
| 85 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 86 | memmove(buf_aligned, buf_aligned + 1, nbytes); // Worst-case overlap. |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 90 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 91 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memmove_overlap_dst_before_src, "AT_ALIGNED_ONEBUF"); |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 92 | |
| 93 | static void BM_string_memmove_overlap_src_before_dst(benchmark::State& state) { |
| 94 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 95 | const size_t alignment = state.range(1); |
| 96 | |
| 97 | std::vector<char> buf; |
| 98 | char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 99 | |
| 100 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 101 | memmove(buf_aligned + 1, buf_aligned, nbytes); // Worst-case overlap. |
Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 105 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 106 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memmove_overlap_src_before_dst, "AT_ALIGNED_ONEBUF"); |
Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 107 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 108 | static void BM_string_memset(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 109 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 110 | const size_t alignment = state.range(1); |
| 111 | |
| 112 | std::vector<char> buf; |
| 113 | char* buf_aligned = GetAlignedPtr(&buf, alignment, nbytes + 1); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 114 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 115 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 116 | memset(buf_aligned, 0, nbytes); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 119 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 120 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 121 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memset, "AT_ALIGNED_ONEBUF"); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 122 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 123 | static void BM_string_strlen(benchmark::State& state) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 124 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 125 | const size_t alignment = state.range(1); |
| 126 | |
| 127 | std::vector<char> buf; |
| 128 | char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); |
| 129 | buf_aligned[nbytes - 1] = '\0'; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 130 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 131 | while (state.KeepRunning()) { |
Elliott Hughes | e8693e7 | 2020-10-22 13:43:59 -0700 | [diff] [blame] | 132 | benchmark::DoNotOptimize(strlen(buf_aligned)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 135 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 136 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 137 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strlen, "AT_ALIGNED_ONEBUF"); |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 138 | |
| 139 | static void BM_string_strcat_copy_only(benchmark::State& state) { |
| 140 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 141 | const size_t src_alignment = state.range(1); |
| 142 | const size_t dst_alignment = state.range(2); |
| 143 | |
| 144 | std::vector<char> src; |
| 145 | std::vector<char> dst; |
| 146 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); |
| 147 | char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes + 2); |
| 148 | src_aligned[nbytes - 1] = '\0'; |
| 149 | dst_aligned[0] = 'y'; |
| 150 | dst_aligned[1] = 'y'; |
| 151 | dst_aligned[2] = '\0'; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 152 | |
| 153 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 154 | strcat(dst_aligned, src_aligned); |
| 155 | dst_aligned[2] = '\0'; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 159 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 160 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcat_copy_only, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 161 | |
| 162 | static void BM_string_strcat_seek_only(benchmark::State& state) { |
| 163 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 164 | const size_t src_alignment = state.range(1); |
| 165 | const size_t dst_alignment = state.range(2); |
| 166 | |
| 167 | std::vector<char> src; |
| 168 | std::vector<char> dst; |
| 169 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, 3, 'x'); |
| 170 | char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes + 2, 'y'); |
| 171 | src_aligned[2] = '\0'; |
| 172 | dst_aligned[nbytes - 1] = '\0'; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 173 | |
| 174 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 175 | strcat(dst_aligned, src_aligned); |
| 176 | dst_aligned[nbytes - 1] = '\0'; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 180 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 181 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcat_seek_only, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 182 | |
| 183 | static void BM_string_strcat_half_copy_half_seek(benchmark::State& state) { |
| 184 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 185 | const size_t src_alignment = state.range(1); |
| 186 | const size_t dst_alignment = state.range(2); |
| 187 | |
Christopher Ferris | e2188d4 | 2017-11-08 23:28:57 -0800 | [diff] [blame] | 188 | // Skip sizes that don't make sense. |
| 189 | if ((nbytes / 2) == 0) { |
| 190 | return; |
| 191 | } |
| 192 | |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 193 | std::vector<char> src; |
| 194 | std::vector<char> dst; |
| 195 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes / 2, 'x'); |
| 196 | char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); |
| 197 | src_aligned[nbytes / 2 - 1] = '\0'; |
| 198 | dst_aligned[nbytes / 2 - 1] = '\0'; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 199 | |
| 200 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 201 | strcat(dst_aligned, src_aligned); |
| 202 | dst_aligned[nbytes / 2 - 1] = '\0'; |
Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 206 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 207 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcat_half_copy_half_seek, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 208 | |
| 209 | static void BM_string_strcpy(benchmark::State& state) { |
| 210 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 211 | const size_t src_alignment = state.range(1); |
| 212 | const size_t dst_alignment = state.range(2); |
| 213 | |
| 214 | std::vector<char> src; |
| 215 | std::vector<char> dst; |
| 216 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); |
| 217 | char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); |
| 218 | src_aligned[nbytes - 1] = '\0'; |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 219 | |
| 220 | while (state.KeepRunning()) { |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 221 | strcpy(dst_aligned, src_aligned); |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 225 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 226 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcpy, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 227 | |
| 228 | static void BM_string_strcmp(benchmark::State& state) { |
| 229 | const size_t nbytes = state.range(0); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 230 | const size_t s1_alignment = state.range(1); |
| 231 | const size_t s2_alignment = state.range(2); |
| 232 | |
| 233 | std::vector<char> s1; |
| 234 | std::vector<char> s2; |
| 235 | char* s1_aligned = GetAlignedPtrFilled(&s1, s1_alignment, nbytes, 'x'); |
| 236 | char* s2_aligned = GetAlignedPtrFilled(&s2, s2_alignment, nbytes, 'x'); |
| 237 | s1_aligned[nbytes - 1] = '\0'; |
| 238 | s2_aligned[nbytes - 1] = '\0'; |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 239 | |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 240 | while (state.KeepRunning()) { |
Elliott Hughes | e8693e7 | 2020-10-22 13:43:59 -0700 | [diff] [blame] | 241 | benchmark::DoNotOptimize(strcmp(s1_aligned, s2_aligned)); |
Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 245 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 246 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcmp, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 247 | |
Haibo Huang | e466cc0 | 2018-08-21 12:55:37 -0700 | [diff] [blame] | 248 | static void BM_string_strncmp(benchmark::State& state) { |
| 249 | const size_t nbytes = state.range(0); |
| 250 | const size_t s1_alignment = state.range(1); |
| 251 | const size_t s2_alignment = state.range(2); |
| 252 | |
| 253 | std::vector<char> s1; |
| 254 | std::vector<char> s2; |
| 255 | char* s1_aligned = GetAlignedPtrFilled(&s1, s1_alignment, nbytes, 'x'); |
| 256 | char* s2_aligned = GetAlignedPtrFilled(&s2, s2_alignment, nbytes, 'x'); |
| 257 | |
Haibo Huang | e466cc0 | 2018-08-21 12:55:37 -0700 | [diff] [blame] | 258 | for (auto _ : state) { |
Elliott Hughes | e8693e7 | 2020-10-22 13:43:59 -0700 | [diff] [blame] | 259 | benchmark::DoNotOptimize(strncmp(s1_aligned, s2_aligned, nbytes)); |
Haibo Huang | e466cc0 | 2018-08-21 12:55:37 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 263 | } |
| 264 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strncmp, "AT_ALIGNED_TWOBUF"); |
| 265 | |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 266 | static void BM_string_strstr(benchmark::State& state) { |
| 267 | const size_t nbytes = state.range(0); |
| 268 | const size_t haystack_alignment = state.range(1); |
| 269 | const size_t needle_alignment = state.range(2); |
| 270 | |
| 271 | std::vector<char> haystack; |
| 272 | std::vector<char> needle; |
| 273 | char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x'); |
| 274 | char* needle_aligned = GetAlignedPtrFilled(&needle, needle_alignment, |
| 275 | std::min(nbytes, static_cast<size_t>(5)), 'x'); |
| 276 | |
| 277 | if (nbytes / 4 > 2) { |
| 278 | for (size_t i = 0; nbytes / 4 >= 2 && i < nbytes / 4 - 2; i++) { |
| 279 | haystack_aligned[4 * i + 3] = 'y'; |
| 280 | } |
| 281 | } |
| 282 | haystack_aligned[nbytes - 1] = '\0'; |
| 283 | needle_aligned[needle.size() - 1] = '\0'; |
| 284 | |
| 285 | while (state.KeepRunning()) { |
| 286 | if (strstr(haystack_aligned, needle_aligned) == nullptr) { |
Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 287 | errx(1, "ERROR: strstr failed to find valid substring."); |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 292 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 293 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strstr, "AT_ALIGNED_TWOBUF"); |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 294 | |
| 295 | static void BM_string_strchr(benchmark::State& state) { |
| 296 | const size_t nbytes = state.range(0); |
| 297 | const size_t haystack_alignment = state.range(1); |
| 298 | |
| 299 | std::vector<char> haystack; |
| 300 | char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x'); |
Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 301 | haystack_aligned[nbytes-1] = '\0'; |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 302 | |
| 303 | while (state.KeepRunning()) { |
| 304 | if (strchr(haystack_aligned, 'y') != nullptr) { |
Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 305 | errx(1, "ERROR: strchr found a chr where it should have failed."); |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); |
| 310 | } |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 311 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strchr, "AT_ALIGNED_ONEBUF"); |