Kevin Rocard | 2d98fd3 | 2017-11-09 22:12:51 -0800 | [diff] [blame^] | 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 | |
| 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 | |
| 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 | |
| 87 | BENCHMARK_MAIN(); |