| 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 |  | 
 | 34 |   volatile int c __attribute__((unused)) = 0; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 35 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 36 |     c += memcmp(dst_aligned, src_aligned, nbytes); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 37 |   } | 
 | 38 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 39 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 40 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 41 | BIONIC_BENCHMARK(BM_string_memcmp); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 42 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 43 | static void BM_string_memcpy(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 44 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 45 |   const size_t src_alignment = state.range(1); | 
 | 46 |   const size_t dst_alignment = state.range(2); | 
 | 47 |  | 
 | 48 |   std::vector<char> src; | 
 | 49 |   std::vector<char> dst; | 
 | 50 |   char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); | 
 | 51 |   char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 52 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 53 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 54 |     memcpy(dst_aligned, src_aligned, nbytes); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 55 |   } | 
 | 56 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 57 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 58 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 59 | BIONIC_BENCHMARK(BM_string_memcpy); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 60 |  | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 61 | static void BM_string_memmove_non_overlapping(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 62 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 63 |   const size_t src_alignment = state.range(1); | 
 | 64 |   const size_t dst_alignment = state.range(2); | 
 | 65 |  | 
 | 66 |   std::vector<char> src; | 
 | 67 |   std::vector<char> dst; | 
 | 68 |   char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); | 
 | 69 |   char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 70 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 71 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 72 |     memmove(dst_aligned, src_aligned, nbytes); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 73 |   } | 
 | 74 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 75 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 76 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 77 | BIONIC_BENCHMARK(BM_string_memmove_non_overlapping); | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 78 |  | 
 | 79 | static void BM_string_memmove_overlap_dst_before_src(benchmark::State& state) { | 
 | 80 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 81 |   const size_t alignment = state.range(1); | 
 | 82 |  | 
 | 83 |   std::vector<char> buf(3 * alignment + nbytes + 1, 'x'); | 
 | 84 |   char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 85 |  | 
 | 86 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 87 |     memmove(buf_aligned, buf_aligned + 1, nbytes);  // Worst-case overlap. | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 88 |   } | 
 | 89 |  | 
 | 90 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 91 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 92 | BIONIC_BENCHMARK(BM_string_memmove_overlap_dst_before_src); | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 93 |  | 
 | 94 | static void BM_string_memmove_overlap_src_before_dst(benchmark::State& state) { | 
 | 95 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 96 |   const size_t alignment = state.range(1); | 
 | 97 |  | 
 | 98 |   std::vector<char> buf; | 
 | 99 |   char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 100 |  | 
 | 101 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 102 |     memmove(buf_aligned + 1, buf_aligned, nbytes);  // Worst-case overlap. | 
| Anders Lewis | 271be9b | 2017-06-07 13:00:38 -0700 | [diff] [blame] | 103 |   } | 
 | 104 |  | 
 | 105 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 106 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 107 | BIONIC_BENCHMARK(BM_string_memmove_overlap_src_before_dst); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 108 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 109 | static void BM_string_memset(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 110 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 111 |   const size_t alignment = state.range(1); | 
 | 112 |  | 
 | 113 |   std::vector<char> buf; | 
 | 114 |   char* buf_aligned = GetAlignedPtr(&buf, alignment, nbytes + 1); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 115 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 116 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 117 |     memset(buf_aligned, 0, nbytes); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 118 |   } | 
 | 119 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 120 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 121 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 122 | BIONIC_BENCHMARK(BM_string_memset); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 123 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 124 | static void BM_string_strlen(benchmark::State& state) { | 
| Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 125 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 126 |   const size_t alignment = state.range(1); | 
 | 127 |  | 
 | 128 |   std::vector<char> buf; | 
 | 129 |   char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); | 
 | 130 |   buf_aligned[nbytes - 1] = '\0'; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 131 |  | 
 | 132 |   volatile int c __attribute__((unused)) = 0; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 133 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 134 |     c += strlen(buf_aligned); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 135 |   } | 
 | 136 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 137 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 138 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 139 | BIONIC_BENCHMARK(BM_string_strlen); | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 140 |  | 
 | 141 | static void BM_string_strcat_copy_only(benchmark::State& state) { | 
 | 142 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 143 |   const size_t src_alignment = state.range(1); | 
 | 144 |   const size_t dst_alignment = state.range(2); | 
 | 145 |  | 
 | 146 |   std::vector<char> src; | 
 | 147 |   std::vector<char> dst; | 
 | 148 |   char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); | 
 | 149 |   char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes + 2); | 
 | 150 |   src_aligned[nbytes - 1] = '\0'; | 
 | 151 |   dst_aligned[0] = 'y'; | 
 | 152 |   dst_aligned[1] = 'y'; | 
 | 153 |   dst_aligned[2] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 154 |  | 
 | 155 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 156 |     strcat(dst_aligned, src_aligned); | 
 | 157 |     dst_aligned[2] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 158 |   } | 
 | 159 |  | 
 | 160 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 161 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 162 | BIONIC_BENCHMARK(BM_string_strcat_copy_only); | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 163 |  | 
 | 164 | static void BM_string_strcat_seek_only(benchmark::State& state) { | 
 | 165 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 166 |   const size_t src_alignment = state.range(1); | 
 | 167 |   const size_t dst_alignment = state.range(2); | 
 | 168 |  | 
 | 169 |   std::vector<char> src; | 
 | 170 |   std::vector<char> dst; | 
 | 171 |   char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, 3, 'x'); | 
 | 172 |   char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes + 2, 'y'); | 
 | 173 |   src_aligned[2] = '\0'; | 
 | 174 |   dst_aligned[nbytes - 1] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 175 |  | 
 | 176 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 177 |     strcat(dst_aligned, src_aligned); | 
 | 178 |     dst_aligned[nbytes - 1] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 179 |   } | 
 | 180 |  | 
 | 181 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 182 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 183 | BIONIC_BENCHMARK(BM_string_strcat_seek_only); | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 184 |  | 
 | 185 | static void BM_string_strcat_half_copy_half_seek(benchmark::State& state) { | 
 | 186 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 187 |   const size_t src_alignment = state.range(1); | 
 | 188 |   const size_t dst_alignment = state.range(2); | 
 | 189 |  | 
 | 190 |   std::vector<char> src; | 
 | 191 |   std::vector<char> dst; | 
 | 192 |   char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes / 2, 'x'); | 
 | 193 |   char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); | 
 | 194 |   src_aligned[nbytes / 2 - 1] = '\0'; | 
 | 195 |   dst_aligned[nbytes / 2 - 1] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 196 |  | 
 | 197 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 198 |     strcat(dst_aligned, src_aligned); | 
 | 199 |     dst_aligned[nbytes / 2 - 1] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 200 |   } | 
 | 201 |  | 
 | 202 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 203 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 204 | BIONIC_BENCHMARK(BM_string_strcat_half_copy_half_seek); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 205 |  | 
 | 206 | static void BM_string_strcpy(benchmark::State& state) { | 
 | 207 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 208 |   const size_t src_alignment = state.range(1); | 
 | 209 |   const size_t dst_alignment = state.range(2); | 
 | 210 |  | 
 | 211 |   std::vector<char> src; | 
 | 212 |   std::vector<char> dst; | 
 | 213 |   char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); | 
 | 214 |   char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); | 
 | 215 |   src_aligned[nbytes - 1] = '\0'; | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 216 |  | 
 | 217 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 218 |     strcpy(dst_aligned, src_aligned); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 219 |   } | 
 | 220 |  | 
 | 221 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 222 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 223 | BIONIC_BENCHMARK(BM_string_strcpy); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 224 |  | 
 | 225 | static void BM_string_strcmp(benchmark::State& state) { | 
 | 226 |   const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 227 |   const size_t s1_alignment = state.range(1); | 
 | 228 |   const size_t s2_alignment = state.range(2); | 
 | 229 |  | 
 | 230 |   std::vector<char> s1; | 
 | 231 |   std::vector<char> s2; | 
 | 232 |   char* s1_aligned = GetAlignedPtrFilled(&s1, s1_alignment, nbytes, 'x'); | 
 | 233 |   char* s2_aligned = GetAlignedPtrFilled(&s2, s2_alignment, nbytes, 'x'); | 
 | 234 |   s1_aligned[nbytes - 1] = '\0'; | 
 | 235 |   s2_aligned[nbytes - 1] = '\0'; | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 236 |  | 
 | 237 |   volatile int c __attribute__((unused)); | 
 | 238 |   while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 239 |     c = strcmp(s1_aligned, s2_aligned); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 240 |   } | 
 | 241 |  | 
 | 242 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 243 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 244 | BIONIC_BENCHMARK(BM_string_strcmp); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 245 |  | 
 | 246 | static void BM_string_strstr(benchmark::State& state) { | 
 | 247 |   const size_t nbytes = state.range(0); | 
 | 248 |   const size_t haystack_alignment = state.range(1); | 
 | 249 |   const size_t needle_alignment = state.range(2); | 
 | 250 |  | 
 | 251 |   std::vector<char> haystack; | 
 | 252 |   std::vector<char> needle; | 
 | 253 |   char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x'); | 
 | 254 |   char* needle_aligned = GetAlignedPtrFilled(&needle, needle_alignment, | 
 | 255 |                                              std::min(nbytes, static_cast<size_t>(5)), 'x'); | 
 | 256 |  | 
 | 257 |   if (nbytes / 4 > 2) { | 
 | 258 |     for (size_t i = 0; nbytes / 4 >= 2 && i < nbytes / 4 - 2; i++) { | 
 | 259 |       haystack_aligned[4 * i + 3] = 'y'; | 
 | 260 |     } | 
 | 261 |   } | 
 | 262 |   haystack_aligned[nbytes - 1] = '\0'; | 
 | 263 |   needle_aligned[needle.size() - 1] = '\0'; | 
 | 264 |  | 
 | 265 |   while (state.KeepRunning()) { | 
 | 266 |     if (strstr(haystack_aligned, needle_aligned) == nullptr) { | 
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 267 |       errx(1, "ERROR: strstr failed to find valid substring."); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 268 |     } | 
 | 269 |   } | 
 | 270 |  | 
 | 271 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 272 | } | 
 | 273 | BIONIC_BENCHMARK(BM_string_strstr); | 
 | 274 |  | 
 | 275 | static void BM_string_strchr(benchmark::State& state) { | 
 | 276 |   const size_t nbytes = state.range(0); | 
 | 277 |   const size_t haystack_alignment = state.range(1); | 
 | 278 |  | 
 | 279 |   std::vector<char> haystack; | 
 | 280 |   char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x'); | 
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 281 |   haystack_aligned[nbytes-1] = '\0'; | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 282 |  | 
 | 283 |   while (state.KeepRunning()) { | 
 | 284 |     if (strchr(haystack_aligned, 'y') != nullptr) { | 
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 285 |       errx(1, "ERROR: strchr found a chr where it should have failed."); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 286 |     } | 
 | 287 |   } | 
 | 288 |  | 
 | 289 |   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
 | 290 | } | 
 | 291 | BIONIC_BENCHMARK(BM_string_strchr); |