| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 41 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memcmp, "AT_ALIGNED_TWOBUF"); | 
| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 59 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memcpy, "AT_ALIGNED_TWOBUF"); | 
| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 77 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memmove_non_overlapping, "AT_ALIGNED_TWOBUF"); | 
| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 92 | 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] | 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 107 | 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] | 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 122 | BIONIC_BENCHMARK_WITH_ARG(BM_string_memset, "AT_ALIGNED_ONEBUF"); | 
| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 139 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strlen, "AT_ALIGNED_ONEBUF"); | 
| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 162 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcat_copy_only, "AT_ALIGNED_TWOBUF"); | 
| 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 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 183 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcat_seek_only, "AT_ALIGNED_TWOBUF"); | 
| 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 |  | 
| Christopher Ferris | e2188d4 | 2017-11-08 23:28:57 -0800 | [diff] [blame] | 190 | // Skip sizes that don't make sense. | 
|  | 191 | if ((nbytes / 2) == 0) { | 
|  | 192 | return; | 
|  | 193 | } | 
|  | 194 |  | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 195 | std::vector<char> src; | 
|  | 196 | std::vector<char> dst; | 
|  | 197 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes / 2, 'x'); | 
|  | 198 | char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); | 
|  | 199 | src_aligned[nbytes / 2 - 1] = '\0'; | 
|  | 200 | dst_aligned[nbytes / 2 - 1] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 201 |  | 
|  | 202 | while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 203 | strcat(dst_aligned, src_aligned); | 
|  | 204 | dst_aligned[nbytes / 2 - 1] = '\0'; | 
| Anders Lewis | a99d052 | 2017-06-12 11:24:01 -0700 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
|  | 207 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 208 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 209 | 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] | 210 |  | 
|  | 211 | static void BM_string_strcpy(benchmark::State& state) { | 
|  | 212 | const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 213 | const size_t src_alignment = state.range(1); | 
|  | 214 | const size_t dst_alignment = state.range(2); | 
|  | 215 |  | 
|  | 216 | std::vector<char> src; | 
|  | 217 | std::vector<char> dst; | 
|  | 218 | char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); | 
|  | 219 | char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); | 
|  | 220 | src_aligned[nbytes - 1] = '\0'; | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 221 |  | 
|  | 222 | while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 223 | strcpy(dst_aligned, src_aligned); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
|  | 226 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 227 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 228 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcpy, "AT_ALIGNED_TWOBUF"); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 229 |  | 
|  | 230 | static void BM_string_strcmp(benchmark::State& state) { | 
|  | 231 | const size_t nbytes = state.range(0); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 232 | const size_t s1_alignment = state.range(1); | 
|  | 233 | const size_t s2_alignment = state.range(2); | 
|  | 234 |  | 
|  | 235 | std::vector<char> s1; | 
|  | 236 | std::vector<char> s2; | 
|  | 237 | char* s1_aligned = GetAlignedPtrFilled(&s1, s1_alignment, nbytes, 'x'); | 
|  | 238 | char* s2_aligned = GetAlignedPtrFilled(&s2, s2_alignment, nbytes, 'x'); | 
|  | 239 | s1_aligned[nbytes - 1] = '\0'; | 
|  | 240 | s2_aligned[nbytes - 1] = '\0'; | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 241 |  | 
|  | 242 | volatile int c __attribute__((unused)); | 
|  | 243 | while (state.KeepRunning()) { | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 244 | c = strcmp(s1_aligned, s2_aligned); | 
| Anders Lewis | 1c487e1 | 2017-06-12 12:33:06 -0700 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
|  | 247 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 248 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 249 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strcmp, "AT_ALIGNED_TWOBUF"); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 250 |  | 
| Haibo Huang | e466cc0 | 2018-08-21 12:55:37 -0700 | [diff] [blame] | 251 | static void BM_string_strncmp(benchmark::State& state) { | 
|  | 252 | const size_t nbytes = state.range(0); | 
|  | 253 | const size_t s1_alignment = state.range(1); | 
|  | 254 | const size_t s2_alignment = state.range(2); | 
|  | 255 |  | 
|  | 256 | std::vector<char> s1; | 
|  | 257 | std::vector<char> s2; | 
|  | 258 | char* s1_aligned = GetAlignedPtrFilled(&s1, s1_alignment, nbytes, 'x'); | 
|  | 259 | char* s2_aligned = GetAlignedPtrFilled(&s2, s2_alignment, nbytes, 'x'); | 
|  | 260 |  | 
|  | 261 | volatile int c __attribute__((unused)); | 
|  | 262 | for (auto _ : state) { | 
|  | 263 | c = strncmp(s1_aligned, s2_aligned, nbytes); | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 267 | } | 
|  | 268 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strncmp, "AT_ALIGNED_TWOBUF"); | 
|  | 269 |  | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 270 | static void BM_string_strstr(benchmark::State& state) { | 
|  | 271 | const size_t nbytes = state.range(0); | 
|  | 272 | const size_t haystack_alignment = state.range(1); | 
|  | 273 | const size_t needle_alignment = state.range(2); | 
|  | 274 |  | 
|  | 275 | std::vector<char> haystack; | 
|  | 276 | std::vector<char> needle; | 
|  | 277 | char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x'); | 
|  | 278 | char* needle_aligned = GetAlignedPtrFilled(&needle, needle_alignment, | 
|  | 279 | std::min(nbytes, static_cast<size_t>(5)), 'x'); | 
|  | 280 |  | 
|  | 281 | if (nbytes / 4 > 2) { | 
|  | 282 | for (size_t i = 0; nbytes / 4 >= 2 && i < nbytes / 4 - 2; i++) { | 
|  | 283 | haystack_aligned[4 * i + 3] = 'y'; | 
|  | 284 | } | 
|  | 285 | } | 
|  | 286 | haystack_aligned[nbytes - 1] = '\0'; | 
|  | 287 | needle_aligned[needle.size() - 1] = '\0'; | 
|  | 288 |  | 
|  | 289 | while (state.KeepRunning()) { | 
|  | 290 | if (strstr(haystack_aligned, needle_aligned) == nullptr) { | 
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 291 | errx(1, "ERROR: strstr failed to find valid substring."); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 296 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 297 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strstr, "AT_ALIGNED_TWOBUF"); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 298 |  | 
|  | 299 | static void BM_string_strchr(benchmark::State& state) { | 
|  | 300 | const size_t nbytes = state.range(0); | 
|  | 301 | const size_t haystack_alignment = state.range(1); | 
|  | 302 |  | 
|  | 303 | std::vector<char> haystack; | 
|  | 304 | char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x'); | 
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 305 | haystack_aligned[nbytes-1] = '\0'; | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 306 |  | 
|  | 307 | while (state.KeepRunning()) { | 
|  | 308 | if (strchr(haystack_aligned, 'y') != nullptr) { | 
| Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 309 | errx(1, "ERROR: strchr found a chr where it should have failed."); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 310 | } | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 314 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 315 | BIONIC_BENCHMARK_WITH_ARG(BM_string_strchr, "AT_ALIGNED_ONEBUF"); |