1 /*
2  * Copyright (C) 2015 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 // The functions we want to benchmark are static, so include the source code.
18 #include "luni/src/main/native/libcore_io_Memory.cpp"
19 
20 #include <benchmark/benchmark.h>
21 
22 template<typename T, size_t ALIGN>
swap_bench(benchmark::State & state,void (* swap_func)(T *,const T *,size_t))23 void swap_bench(benchmark::State& state, void (*swap_func)(T*, const T*, size_t)) {
24   size_t num_elements = state.range(0);
25 
26   T* src;
27   T* dst;
28   T* src_elems;
29   T* dst_elems;
30 
31   if (ALIGN) {
32     src_elems = new T[num_elements + 1];
33     dst_elems = new T[num_elements + 1];
34 
35     src = reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(src_elems) + ALIGN);
36     dst = reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(dst_elems) + ALIGN);
37   } else {
38     src_elems = new T[num_elements];
39     dst_elems = new T[num_elements];
40 
41     src = src_elems;
42     dst = dst_elems;
43   }
44 
45   memset(dst, 0, sizeof(T) * num_elements);
46   memset(src, 0x12, sizeof(T) * num_elements);
47 
48   while (state.KeepRunning()) {
49     swap_func(src, dst, num_elements);
50   }
51 
52   delete[] src_elems;
53   delete[] dst_elems;
54 }
55 
56 #define AT_COMMON_VALUES \
57     Arg(10)->Arg(100)->Arg(1000)->Arg(1024*10)->Arg(1024*100)
58 
59 // Aligned.
60 
BM_swapShorts_aligned(benchmark::State & state)61 static void BM_swapShorts_aligned(benchmark::State& state) {
62   swap_bench<jshort, 0>(state, swapShorts);
63 }
64 BENCHMARK(BM_swapShorts_aligned)->AT_COMMON_VALUES;
65 
BM_swapInts_aligned(benchmark::State & state)66 static void BM_swapInts_aligned(benchmark::State& state) {
67   swap_bench<jint, 0>(state, swapInts);
68 }
69 BENCHMARK(BM_swapInts_aligned)->AT_COMMON_VALUES;
70 
BM_swapLongs_aligned(benchmark::State & state)71 static void BM_swapLongs_aligned(benchmark::State& state) {
72   swap_bench<jlong, 0>(state, swapLongs);
73 }
74 BENCHMARK(BM_swapLongs_aligned)->AT_COMMON_VALUES;
75 
76 // Unaligned 1.
77 
BM_swapShorts_unaligned_1(benchmark::State & state)78 static void BM_swapShorts_unaligned_1(benchmark::State& state) {
79   swap_bench<jshort, 1>(state, swapShorts);
80 }
81 BENCHMARK(BM_swapShorts_unaligned_1)->AT_COMMON_VALUES;
82 
BM_swapInts_unaligned_1(benchmark::State & state)83 static void BM_swapInts_unaligned_1(benchmark::State& state) {
84   swap_bench<jint, 1>(state, swapInts);
85 }
86 BENCHMARK(BM_swapInts_unaligned_1)->AT_COMMON_VALUES;
87 
BM_swapLongs_unaligned_1(benchmark::State & state)88 static void BM_swapLongs_unaligned_1(benchmark::State& state) {
89   swap_bench<jlong, 1>(state, swapLongs);
90 }
91 BENCHMARK(BM_swapLongs_unaligned_1)->AT_COMMON_VALUES;
92 
93 // Unaligned 2.
94 
BM_swapShorts_unaligned_2(benchmark::State & state)95 static void BM_swapShorts_unaligned_2(benchmark::State& state) {
96   swap_bench<jshort, 2>(state, swapShorts);
97 }
98 BENCHMARK(BM_swapShorts_unaligned_2)->AT_COMMON_VALUES;
99 
BM_swapInts_unaligned_2(benchmark::State & state)100 static void BM_swapInts_unaligned_2(benchmark::State& state) {
101   swap_bench<jint, 2>(state, swapInts);
102 }
103 BENCHMARK(BM_swapInts_unaligned_2)->AT_COMMON_VALUES;
104 
BM_swapLongs_unaligned_2(benchmark::State & state)105 static void BM_swapLongs_unaligned_2(benchmark::State& state) {
106   swap_bench<jlong, 2>(state, swapLongs);
107 }
108 BENCHMARK(BM_swapLongs_unaligned_2)->AT_COMMON_VALUES;
109 
110 
111 BENCHMARK_MAIN();
112