• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 <cstddef>
18 #include <random>
19 #include <vector>
20 
21 #include <benchmark/benchmark.h>
22 
23 #include <audio_utils/primitives.h>
24 
BM_MemcpyToFloatFromFloatWithClamping(benchmark::State & state)25 static void BM_MemcpyToFloatFromFloatWithClamping(benchmark::State& state) {
26     const size_t count = state.range(0);
27     const float srcMax = state.range(1);
28     const float absMax = 1.413;
29 
30     std::vector<float> src(count);
31     std::vector<float> dst(count);
32     std::vector<float> expected(count);
33 
34     // Initialize src buffer with deterministic pseudo-random values
35     std::minstd_rand gen(count);
36     std::uniform_real_distribution<> dis(-srcMax, srcMax);
37     for (size_t i = 0; i < count; i++) {
38         src[i] = dis(gen);
39         expected[i] = fmin(absMax, fmax(-absMax, src[i]));
40     }
41 
42     // Run the test
43     while (state.KeepRunning()) {
44         benchmark::DoNotOptimize(src.data());
45         benchmark::DoNotOptimize(dst.data());
46         memcpy_to_float_from_float_with_clamping(dst.data(), src.data(), count, 1.413);
47         benchmark::ClobberMemory();
48     }
49 
50     if (expected != dst) {
51         state.SkipWithError("Incorrect clamping!");
52     }
53     state.SetComplexityN(state.range(0));
54 }
55 
56 BENCHMARK(BM_MemcpyToFloatFromFloatWithClamping)->RangeMultiplier(2)->Ranges({{10, 8<<12}, {1, 2}});
57 
BM_MemcpyFloat(benchmark::State & state)58 static void BM_MemcpyFloat(benchmark::State& state) {
59     const size_t count = state.range(0);
60 
61     std::vector<float> src(count);
62     std::vector<float> dst(count);
63 
64     // Initialize src buffer with deterministic pseudo-random values
65     std::minstd_rand gen(count);
66     std::uniform_real_distribution<> dis;
67     for (size_t i = 0; i < count; i++) {
68         src[i] = dis(gen);
69     }
70 
71     // Run the test
72     while (state.KeepRunning()) {
73         benchmark::DoNotOptimize(src.data());
74         benchmark::DoNotOptimize(dst.data());
75         memcpy(dst.data(), src.data(), count * sizeof(float));
76         benchmark::ClobberMemory();
77     }
78 
79     if (src != dst) {
80         state.SkipWithError("Incorrect memcpy!");
81     }
82     state.SetComplexityN(state.range(0));
83 }
84 
85 BENCHMARK(BM_MemcpyFloat)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
86 
BM_MemcpyToFloatFromI16(benchmark::State & state)87 static void BM_MemcpyToFloatFromI16(benchmark::State& state) {
88     const size_t count = state.range(0);
89 
90     std::vector<int16_t> src(count);
91     std::vector<float> dst(count);
92 
93     // Initialize src buffer with deterministic pseudo-random values
94     std::minstd_rand gen(count);
95     std::uniform_int_distribution<> dis(INT16_MIN, INT16_MAX);
96     for (size_t i = 0; i < count; i++) {
97         src[i] = dis(gen);
98     }
99 
100     // Run the test
101     while (state.KeepRunning()) {
102         benchmark::DoNotOptimize(src.data());
103         benchmark::DoNotOptimize(dst.data());
104         memcpy_to_float_from_i16(dst.data(), src.data(), count);
105         benchmark::ClobberMemory();
106     }
107 
108     state.SetComplexityN(state.range(0));
109 }
110 
111 BENCHMARK(BM_MemcpyToFloatFromI16)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
112 
113 
BM_MemcpyToI16FromFloat(benchmark::State & state)114 static void BM_MemcpyToI16FromFloat(benchmark::State& state) {
115     const size_t count = state.range(0);
116 
117     std::vector<float> src(count);
118     std::vector<int16_t> dst(count);
119 
120     // Initialize src buffer with deterministic pseudo-random values
121     std::minstd_rand gen(count);
122     std::uniform_real_distribution<> dis;
123     for (size_t i = 0; i < count; i++) {
124         src[i] = dis(gen);
125     }
126 
127     // Run the test
128     while (state.KeepRunning()) {
129         benchmark::DoNotOptimize(src.data());
130         benchmark::DoNotOptimize(dst.data());
131         memcpy_to_i16_from_float(dst.data(), src.data(), count);
132         benchmark::ClobberMemory();
133     }
134 
135     state.SetComplexityN(state.range(0));
136 }
137 
138 BENCHMARK(BM_MemcpyToI16FromFloat)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
139 
140 BENCHMARK_MAIN();
141