1 /*
2 * Copyright (C) 2019 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
17 #include <ctype.h>
18
19 #include <array>
20 #include <numeric>
21 #include <random>
22
23 #include <benchmark/benchmark.h>
24 #include "util.h"
25
RandomAscii()26 static std::array<int, 128> RandomAscii() {
27 std::array<int, 128> result;
28 std::iota(result.begin(), result.end(), 0);
29 std::shuffle(result.begin(), result.end(), std::mt19937{std::random_device{}()});
30 return result;
31 }
32
33 #define CTYPE_BENCHMARK(__benchmark, fn) \
34 static void __benchmark##_##fn(benchmark::State& state) { \
35 auto chars = RandomAscii(); \
36 for (auto _ : state) { \
37 for (char ch : chars) { \
38 benchmark::DoNotOptimize(fn(ch)); \
39 } \
40 } \
41 state.SetBytesProcessed(state.iterations() * chars.size()); \
42 } \
43 BIONIC_BENCHMARK(__benchmark##_##fn)
44
45 CTYPE_BENCHMARK(BM_ctype, isalpha);
46 CTYPE_BENCHMARK(BM_ctype, isalnum);
47 CTYPE_BENCHMARK(BM_ctype, isascii);
48 CTYPE_BENCHMARK(BM_ctype, isblank);
49 CTYPE_BENCHMARK(BM_ctype, iscntrl);
50 CTYPE_BENCHMARK(BM_ctype, isgraph);
51 CTYPE_BENCHMARK(BM_ctype, islower);
52 CTYPE_BENCHMARK(BM_ctype, isprint);
53 CTYPE_BENCHMARK(BM_ctype, ispunct);
54 CTYPE_BENCHMARK(BM_ctype, isspace);
55 CTYPE_BENCHMARK(BM_ctype, isupper);
56 CTYPE_BENCHMARK(BM_ctype, isxdigit);
57
58 CTYPE_BENCHMARK(BM_ctype, toascii);
59 CTYPE_BENCHMARK(BM_ctype, tolower);
60 CTYPE_BENCHMARK(BM_ctype, _tolower);
61 CTYPE_BENCHMARK(BM_ctype, toupper);
62 CTYPE_BENCHMARK(BM_ctype, _toupper);
63